1 // *********************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation  *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral.c,v $
    7 // Version: $Revision: 1.34 $
    8 // Date:    $Date: 2021/04/26 15:13:51 $
    9 //
   10 // Revision history:
   11 //   2018/10/25  Release for DPA 3.03
   12 //   2017/03/13  Release for DPA 3.00
   13 //   2015/08/05  Release for DPA 2.20
   14 //   2014/10/31  Release for DPA 2.10
   15 //   2014/04/30  Release for DPA 2.00
   16 //
   17 // *********************************************************************
   18 
   19 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   20 
   21 // This example implements the user peripheral.
   22 // It is actually a double color LED peripheral using the same commands as default DPA LED peripherals
   23 
   24 // Default IQRF include (modify the path according to your setup)
   25 #include "IQRF.h"
   26 
   27 // Uncomment to implement Custom DPA Handler for Coordinator
   28 //#define COORDINATOR_CUSTOM_HANDLER
   29 
   30 // Default DPA header (modify the path according to your setup)
   31 #include "DPA.h"
   32 // Default Custom DPA Handler header (modify the path according to your setup)
   33 #include "DPAcustomHandler.h"
   34 
   35 //############################################################################################
   36 
   37 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   38 //############################################################################################
   39 bit CustomDpaHandler()
   40 //############################################################################################
   41 {
   42   // Handler presence mark
   43   clrwdt();
   44 
   45   // Detect DPA event to handle
   46   switch ( GetDpaEvent() )
   47   {
   48     // -------------------------------------------------
   49     case DpaEvent_Interrupt:
   50       // Do an extra quick background interrupt work
   51       // ! 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.
   52       // ! 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.
   53       // ! 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.
   54       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   55       // ! Make sure race condition does not occur when accessing those variables at other places.
   56       // ! 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.
   57       // ! Do not call any OS functions except setINDFx().
   58       // ! Do not use any OS variables especially for writing access.
   59       // ! 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.
   60 
   61       return Carry;
   62 
   63       // -------------------------------------------------
   64     case DpaEvent_DpaRequest:
   65       // Called to interpret DPA request for peripherals
   66       // -------------------------------------------------
   67       // Peripheral enumeration
   68       IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
   69       {
   70         // We implement 1 user peripheral
   71         _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 1;
   72         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   73         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F;
   74         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0x1234;
   75 
   76 DpaHandleReturnTRUE:
   77         return TRUE;
   78       }
   79       // -------------------------------------------------
   80       // Get information about peripheral
   81       else
   82       {
   83         if ( _PNUM == PNUM_USER + 0 )
   84         {
   85           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_LED;
   86           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ_WRITE;
   87           _DpaMessage.PeripheralInfoAnswer.Par1 = LED_COLOR_UNKNOWN;
   88           goto DpaHandleReturnTRUE;
   89         }
   90 
   91         break;
   92       }
   93       // -------------------------------------------------
   94 
   95       // Handle peripheral command
   96       if ( _PNUM == PNUM_USER + 0 )
   97       {
   98         // Check that there is no data
   99         if ( _DpaDataLength != 0 )
  100           DpaApiReturnPeripheralError( ERROR_DATA_LEN );
  101 
  102         // Process commands
  103         switch ( _PCMD )
  104         {
  105           // LEDs ON
  106           case CMD_LED_SET_ON:
  107             setLEDR();
  108             setLEDG();
  109             // Make sure LED is visible at LP mode
  110             waitMS( 10 );
  111 WriteNoError:
  112             _DpaDataLength = 0;
  113             goto DpaHandleReturnTRUE;
  114 
  115             // LEDs OFF
  116           case CMD_LED_SET_OFF:
  117             stopLEDR();
  118             stopLEDG();
  119             goto WriteNoError;
  120 
  121             // Pulse LEDs 
  122           case CMD_LED_PULSE:
  123             pulseLEDR();
  124             pulseLEDG();
  125             // Make sure LED is visible at LP mode
  126             waitMS( 10 );
  127             goto WriteNoError;
  128 
  129             // Flashing LEDs 
  130           case CMD_LED_FLASHING:
  131             pulsingLEDR();
  132             pulsingLEDG();
  133             goto WriteNoError;
  134 
  135             // Invalid command
  136           default:
  137             DpaApiReturnPeripheralError( ERROR_PCMD );
  138         }
  139       }
  140   }
  141 
  142   return FALSE;
  143 }
  144 
  145 //############################################################################################
  146 // 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) 
  147 #include "DPAcustomHandler.h"
  148 //############################################################################################