1 // ************************************************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation - MCU temperature indicator *
    3 // ************************************************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral-McuTempIndicator.c,v $
    7 // Version: $Revision: 1.12 $
    8 // Date:    $Date: 2018/01/03 09:55:12 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //   2016/09/12  Release for DPA 2.28
   13 //   2015/08/05  Release for DPA 2.20
   14 //   2014/05/26  Release for DPA 2.11
   15 //   2014/10/31  Release for DPA 2.10
   16 //   2014/05/26  Release for DPA 2.01
   17 //
   18 // *********************************************************************
   19 
   20 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   21 
   22 // This example implements the user peripheral reading PIC MCU temperature indicator
   23 
   24 // Default IQRF include (modify the path according to your setup)
   25 #include "IQRF.h"
   26 
   27 // Default DPA header (modify the path according to your setup)
   28 #include "DPA.h"
   29 // Default Custom DPA Handler header (modify the path according to your setup)
   30 #include "DPAcustomHandler.h"
   31 
   32 //############################################################################################
   33 
   34 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   35 //############################################################################################
   36 bit CustomDpaHandler()
   37 //############################################################################################
   38 {
   39   // Handler presence mark
   40   clrwdt();
   41 
   42   // Detect DPA event to handle
   43   switch ( GetDpaEvent() )
   44   {
   45     // -------------------------------------------------
   46     case DpaEvent_DpaRequest:
   47       // Called to interpret DPA request for peripherals
   48       // -------------------------------------------------
   49       // Peripheral enumeration
   50       if ( IsDpaEnumPeripheralsRequest() )
   51       {
   52         // We implement 1 user peripheral
   53         _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 1;
   54         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   55         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F;
   56         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0xabcd;
   57 
   58 DpaHandleReturnTRUE:
   59         return TRUE;
   60       }
   61       // -------------------------------------------------
   62       // Get information about peripheral
   63       else if ( IsDpaPeripheralInfoRequest() )
   64       {
   65         if ( _PNUM == PNUM_USER + 0 )
   66         {
   67           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_USER_AREA;
   68           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ;
   69           goto DpaHandleReturnTRUE;
   70         }
   71 
   72         break;
   73       }
   74       // -------------------------------------------------
   75       else
   76       {
   77         // Handle peripheral command
   78         if ( _PNUM == PNUM_USER + 0 )
   79         {
   80           // Check command
   81           switch ( _PCMD )
   82           {
   83             case 0:
   84               // -------------------------------------------------
   85               // Read temperature
   86               if ( _DpaDataLength != 0 )
   87                 DpaApiReturnPeripheralError( ERROR_DATA_LEN );
   88 
   89               uns16 temperature  @ _DpaMessage.Response.PData;
   90 
   91               // TSRNG: Temperature Indicator Range Selection bit(
   92               TSRNG = 0;
   93               // TSEN: Temperature Indicator Enable bit
   94               TSEN = 1;
   95               // ADC result 
   96               // - right justified
   97               // - Fosc/64
   98               // - VREF- is connected to VSS
   99               // - VREF+ is connected to VDD
  100               ADCON1 = 0b1.110.0.0.00;
  101               // ADC setting (temp indicator)
  102               ADCON0 = 0b0.11101.01;
  103               waitMS( 1 );
  104               // start ADC
  105               GO = 1;
  106               // wait for ADC finish
  107               while ( GO );
  108 
  109               temperature.low8 = ADRESL;
  110               temperature.high8 = ADRESH;
  111 
  112               TSEN = 0;
  113 
  114               _DpaDataLength = sizeof( temperature );
  115               goto DpaHandleReturnTRUE;
  116 
  117             default:
  118               // -------------------------------------------------
  119               // Invalid command
  120               DpaApiReturnPeripheralError( ERROR_PCMD );
  121           }
  122         }
  123       }
  124   }
  125 
  126   return FALSE;
  127 }
  128 
  129 //############################################################################################
  130 
  131 //############################################################################################
  132 // Default Custom DPA Handler header; 2nd include to implement Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup) 
  133 #include "DPAcustomHandler.h"
  134 //############################################################################################