1 // ************************************************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation - MCU temperature indicator *
    3 // ************************************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral-McuTempIndicator.c,v $
    7 // Version: $Revision: 1.22 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   12 //   2017/03/13  Release for DPA 3.00
   13 //   2016/09/12  Release for DPA 2.28
   14 //   2015/08/05  Release for DPA 2.20
   15 //   2014/05/26  Release for DPA 2.11
   16 //   2014/10/31  Release for DPA 2.10
   17 //   2014/05/26  Release for DPA 2.01
   18 //
   19 // *********************************************************************
   20 
   21 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   22 
   23 // This example implements the user peripheral reading PIC MCU temperature indicator
   24 
   25 // Default IQRF include (modify the path according to your setup)
   26 #include "IQRF.h"
   27 
   28 // Default DPA header (modify the path according to your setup)
   29 #include "DPA.h"
   30 // Default Custom DPA Handler header (modify the path according to your setup)
   31 #include "DPAcustomHandler.h"
   32 
   33 //############################################################################################
   34 
   35 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   36 //############################################################################################
   37 bit CustomDpaHandler()
   38 //############################################################################################
   39 {
   40   // Handler presence mark
   41   clrwdt();
   42 
   43   // Detect DPA event to handle
   44   switch ( GetDpaEvent() )
   45   {
   46     // -------------------------------------------------
   47     case DpaEvent_DpaRequest:
   48       // Called to interpret DPA request for peripherals
   49       // -------------------------------------------------
   50       // Peripheral enumeration
   51       if ( IsDpaEnumPeripheralsRequest() )
   52       {
   53         // We implement 1 user peripheral
   54         _DpaMessage.EnumPeripheralsAnswer.UserPerNr |=  1;
   55         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   56         _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x000F;
   57         _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= 0xabcd;
   58 
   59 DpaHandleReturnTRUE:
   60         return TRUE;
   61       }
   62       // -------------------------------------------------
   63       // Get information about peripheral
   64       else if ( IsDpaPeripheralInfoRequest() )
   65       {
   66         if ( _PNUM == PNUM_USER + 0 )
   67         {
   68           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_USER_AREA;
   69           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ;
   70           goto DpaHandleReturnTRUE;
   71         }
   72 
   73         break;
   74       }
   75       // -------------------------------------------------
   76       else
   77       {
   78         // Handle peripheral command
   79         if ( _PNUM == PNUM_USER + 0 )
   80         {
   81           // Check command
   82           switch ( _PCMD )
   83           {
   84             case 0:
   85               // -------------------------------------------------
   86               // Read temperature
   87               if ( _DpaDataLength != 0 )
   88                 DpaApiReturnPeripheralError( ERROR_DATA_LEN );
   89 
   90               uns16 temperature  @ _DpaMessage.Response.PData;
   91 
   92 #if defined( TR7xG )
   93               // Enable ADC
   94               ADCMD = 0;
   95 
   96               // Might not be needed if ADC registers are kept default
   97               {
   98                 // Start reseting ADC registers from ADCON0 to ADPCH
   99                 // VREF- is connected to AVSS, VREF+ is connected to VDD
  100                 FSR0 = &ADCON0;
  101                 do
  102                 {
  103                   setINDF0( 0 );
  104                   FSR0++;
  105                   // Stop reseting at 1st GPR register, ADPCH is the last implemented register before GPR
  106                 } while ( !FSR0L.5 );
  107               }
  108 
  109               // Right justified and enabled
  110               ADCON0 = 0b1000.0100;
  111               // ADC Conversion Clock = FOSC/64
  112               ADCLK = 64 / 2 - 1;
  113               // Positive Input Channel = Temperature Indicator
  114               setADPCH( 0b00.111101 );
  115 #else
  116               // ADC result
  117               // - right justified
  118               // - Fosc/64
  119               // - VREF- is connected to VSS
  120               // - VREF+ is connected to VDD
  121               ADCON1 = 0b1.110.0.0.00;
  122               // ADC setting (temp indicator)
  123               ADCON0 = 0b0.11101.01;
  124 #endif
  125 
  126               // TSRNG: Temperature Indicator Range Selection bit, low range
  127               TSRNG = 0;
  128               // TSEN: Temperature Indicator Enable bit
  129               TSEN = 1;
  130 
  131               // Short delay to stabilize
  132               waitMS( 1 );
  133               // start ADC
  134               _GO = 1;
  135               // wait for ADC finish
  136               while ( _GO );
  137 
  138               temperature.low8 = ADRESL;
  139               temperature.high8 = ADRESH;
  140 
  141               TSEN = 0;
  142 
  143               _DpaDataLength = sizeof( temperature );
  144               goto DpaHandleReturnTRUE;
  145 
  146             default:
  147               // -------------------------------------------------
  148               // Invalid command
  149               DpaApiReturnPeripheralError( ERROR_PCMD );
  150           }
  151         }
  152       }
  153   }
  154 
  155   return FALSE;
  156 }
  157 
  158 //############################################################################################
  159 
  160 //############################################################################################
  161 // Default Custom DPA Handler header; 2nd include implementing a Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup)
  162 #include "DPAcustomHandler.h"
  163 //############################################################################################