1 // **************************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation - ADC *
    3 // **************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral-ADC.c,v $
    7 // Version: $Revision: 1.28 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //   2015/08/05  Release for DPA 2.20
   13 //   2014/10/31  Release for DPA 2.10
   14 //   2014/04/30  Release for DPA 2.00
   15 //
   16 // *********************************************************************
   17 
   18 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   19 
   20 // This example implements the user peripherals ADC
   21 // PNUM = 0x20 and PCMD = 0 returns 2 bytes with 10b ADC result from pin C5 (AN4) at PData
   22 // PNUM = 0x21 and PCMD = 0 returns 2 bytes with 10b ADC result from pin C1 (AN0) at PData
   23 // Works with TR-72 having connected pins RA.5, RC.6 and RB.4
   24 // At DDC-SE-01 allows to read:
   25 // * Light intensity measurement using a photoresistor
   26 // * Voltage measurement using a potentiometer
   27 
   28 // Default IQRF include (modify the path according to your setup)
   29 #include "IQRF.h"
   30 
   31 // Default DPA header (modify the path according to your setup)
   32 #include "DPA.h"
   33 // Default Custom DPA Handler header (modify the path according to your setup)
   34 #include "DPAcustomHandler.h"
   35 
   36 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   37 //############################################################################################
   38 bit CustomDpaHandler()
   39 //############################################################################################
   40 {
   41   // Handler presence mark
   42   clrwdt();
   43 
   44   // Detect DPA event to handle
   45   switch ( GetDpaEvent() )
   46   {
   47     // -------------------------------------------------
   48     case DpaEvent_Interrupt:
   49       // Do an extra quick background interrupt work
   50       // ! The time spent handling this event is critical.If there is no interrupt to handle return immediately otherwise keep the code as fast as possible.
   51       // ! Make sure the event is the 1st case in the main switch statement at the handler routine.This ensures that the event is handled as the 1st one.
   52       // ! It is desirable that this event is handled with immediate return even if it is not used by the custom handler because the Interrupt event is raised on every MCU interrupt and the “empty” return handler ensures the shortest possible interrupt routine response time.
   53       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   54       // ! Make sure race condition does not occur when accessing those variables at other places.
   55       // ! Make sure( inspect.lst file generated by C compiler ) compiler does not create any hidden temporary local variable( occurs when using division, multiplication or bit shifts ) at the event handler code.The name of such variable is usually Cnumbercnt.
   56       // ! Do not call any OS functions except setINDFx().
   57       // ! Do not use any OS variables especially for writing access.
   58       // ! All above rules apply also to any other function being called from the event handler code, although calling any function from Interrupt event is not recommended because of additional MCU stack usage.
   59 
   60 DpaHandleReturnTRUE:
   61       return TRUE;
   62 
   63       // -------------------------------------------------
   64     case DpaEvent_Init:
   65       // Do a one time initialization before main loop starts
   66 
   67       // C5 (AN4) as input
   68       TRISA.5 = 1;
   69       TRISC.6 = 1;
   70       TRISB.4 = 1;
   71 
   72       break;
   73 
   74       // -------------------------------------------------
   75     case DpaEvent_DpaRequest:
   76       // Called to interpret DPA request for peripherals
   77       // -------------------------------------------------
   78       // Peripheral enumeration
   79       if ( IsDpaEnumPeripheralsRequest() )
   80       {
   81         // We implement 1 user peripheral
   82         _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 2;
   83         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   84         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 1 );
   85         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F;
   86         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0xabcd;
   87 
   88         goto DpaHandleReturnTRUE;
   89       }
   90       // -------------------------------------------------
   91       // Get information about peripheral
   92       else if ( IsDpaPeripheralInfoRequest() )
   93       {
   94         if ( _PNUM == PNUM_USER + 0 || _PNUM == PNUM_USER + 1 )
   95         {
   96           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_ADC;
   97           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ;
   98           goto DpaHandleReturnTRUE;
   99         }
  100 
  101         break;
  102       }
  103       // -------------------------------------------------
  104       else
  105       {
  106         // Handle peripheral command
  107         if ( _PNUM == PNUM_USER + 0 || _PNUM == PNUM_USER + 1 )
  108         {
  109           // Check command
  110           if ( _PCMD != 0 )
  111             DpaApiReturnPeripheralError( ERROR_PCMD );
  112 
  113           // Check data length
  114           if ( _DpaDataLength != 0 )
  115             DpaApiReturnPeripheralError( ERROR_DATA_LEN );
  116 
  117           if ( _PNUM == PNUM_USER + 0 )
  118           {
  119             // ADC init (for more info see PIC datasheet) pin C5 (AN4) as analog input
  120             ANSELA.5 = 1;
  121             // ADC setting (AN4 channel)
  122             ADCON0 = 0b0.00100.01;
  123           }
  124           else
  125           {
  126             // ADC init (for more info see PIC datasheet) pin C1 (AN0) as analog input
  127             ANSELA.0 = 1;
  128             // ADC setting (AN0 channel)
  129             ADCON0 = 0b0.00000.01;
  130           }
  131 
  132           // ADC result - right justified, Fosc/8
  133           ADCON1 = 0b10010000;
  134 
  135           // start ADC
  136           GO = 1;
  137           // wait for ADC finish
  138           while ( GO );
  139 
  140           // 10b result is stored in ADRESH and ADRESL
  141           _DpaMessage.Response.PData[1] = ADRESH & 0x03;
  142           _DpaMessage.Response.PData[0] = ADRESL;
  143           _DpaDataLength = sizeof( uns16 );
  144 
  145           goto DpaHandleReturnTRUE;
  146         }
  147       }
  148   }
  149 
  150   return FALSE;
  151 }
  152 
  153 //############################################################################################
  154 // 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)
  155 #include "DPAcustomHandler.h"
  156 //############################################################################################