1 // *********************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation  *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-UserPeripheral.c,v $
    7 // Version: $Revision: 1.26 $
    8 // Date:    $Date: 2017/08/09 12:24:47 $
    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 http://www.iqrf.org/DpaTechGuide/
   19 
   20 // This example implements the user peripheral.
   21 // It is actually a double color LED peripheral using the same commands as default DPA LED peripherals
   22 
   23 // The example will not work at demo DPA version because demo DPA does not support PCMD != 0
   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             _LEDG = _LEDR = 1;
  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             _LEDG = _LEDR = 0;
  118             goto WriteNoError;
  119 
  120             // Pulse LEDs 
  121           case CMD_LED_PULSE:
  122             pulseLEDR();
  123             pulseLEDG();
  124             // Make sure LED is visible at LP mode
  125             waitMS( 10 );
  126             goto WriteNoError;
  127 
  128             // Get LEDs state
  129           case CMD_LED_GET:
  130             W = 0;
  131             if ( _LEDR != 0 && _LEDG != 0 )
  132               W = 1;
  133 
  134             _DpaMessage.Response.PData[0] = W;
  135             _DpaDataLength = 1;
  136             goto DpaHandleReturnTRUE;
  137 
  138             // Invalid command
  139           default:
  140             DpaApiReturnPeripheralError( ERROR_PCMD );
  141         }
  142       }
  143   }
  144 
  145   return FALSE;
  146 }
  147 
  148 //############################################################################################
  149 // 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) 
  150 #include "DPAcustomHandler.h"
  151 //############################################################################################