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