1 // *********************************************************************
    2 //   Custom DPA Handler code example - User peripheral implementation  *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-LED-UserPeripheral.c,v $
    7 // Version: $Revision: 1.24 $
    8 // Date:    $Date: 2018/10/25 09:51:28 $
    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/07/26  Release for DPA 2.01
   16 //
   17 // *********************************************************************
   18 
   19 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   20 
   21 // This example implements one user peripheral having one command.
   22 // PNUM = PNUM_USER + 0 = 0x20
   23 // PCMD = 0
   24 // 2 bytes of PData
   25 //   byte PData[0] controls red LED
   26 //   byte PData[1] controls green LED
   27 //
   28 // bytes @ PData[0..1]
   29 //  0: LED off
   30 //  1: LED on
   31 //  2: LED pulse
   32 //  3: LED pulsing
   33 //  other: no change
   34 
   35 // Default IQRF include (modify the path according to your setup)
   36 #include "IQRF.h"
   37 
   38 // Uncomment to compile Custom DPA Handler for Coordinator
   39 //#define COORDINATOR_CUSTOM_HANDLER
   40 
   41 // Default DPA header (modify the path according to your setup)
   42 #include "DPA.h"
   43 // Default Custom DPA Handler header (modify the path according to your setup)
   44 #include "DPAcustomHandler.h"
   45 
   46 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   47 //############################################################################################
   48 bit CustomDpaHandler()
   49 //############################################################################################
   50 {
   51   // Handler presence mark
   52   clrwdt();
   53 
   54   // Detect DPA event to handle
   55   switch ( GetDpaEvent() )
   56   {
   57 #ifdef DpaEvent_Interrupt
   58     // -------------------------------------------------
   59     case DpaEvent_Interrupt:
   60       // Do an extra quick background interrupt work
   61       // ! 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.
   62       // ! 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.
   63       // ! 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.
   64       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   65       // ! Make sure race condition does not occur when accessing those variables at other places.
   66       // ! 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.
   67       // ! Do not call any OS functions except setINDFx().
   68       // ! Do not use any OS variables especially for writing access.
   69       // ! 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.
   70 
   71       return Carry;
   72 #endif    
   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 = 1;
   83         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   84         _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F;
   85         _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0xCAFE;
   86 
   87 DpaHandleReturnTRUE:
   88         return TRUE;
   89       }
   90       // -------------------------------------------------
   91       // Get information about peripheral
   92       else if ( IsDpaPeripheralInfoRequest() )
   93       {
   94         if ( _PNUM == PNUM_USER + 0 )
   95         {
   96           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_USER_AREA;
   97           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_WRITE;
   98           goto DpaHandleReturnTRUE;
   99         }
  100 
  101         break;
  102       }
  103       // -------------------------------------------------
  104       else
  105       {
  106         // Handle peripheral
  107         if ( _PNUM == PNUM_USER + 0 )
  108         {
  109           // Process commands
  110           if ( _PCMD != 0 )
  111             DpaApiReturnPeripheralError( ERROR_PCMD );
  112 
  113           // Check that there is a correct data length
  114           if ( _DpaDataLength != 2 )
  115             DpaApiReturnPeripheralError( ERROR_DATA_LEN );
  116 
  117           // Control red LED
  118           switch ( _DpaMessage.Request.PData[0] )
  119           {
  120             // Switch the LED off
  121             case 0:
  122               stopLEDR();
  123               break;
  124 
  125               // Switch the LED on (first make sure that the optional pulsing is disabled)
  126             case 1:
  127               stopLEDR();
  128               setLEDR();
  129               // Make sure LED is visible at LP mode
  130               waitMS( 20 );
  131               break;
  132 
  133               // Make one pulse
  134             case 2:
  135               pulseLEDR();
  136               // Make sure LED is visible at LP mode
  137               waitMS( 20 );
  138               break;
  139 
  140               // Start pulsing
  141             case 3:
  142               pulsingLEDR();
  143               // Make sure LED is visible at LP mode
  144               waitMS( 20 );
  145               break;
  146           }
  147 
  148           // Control green LED
  149           switch ( _DpaMessage.Request.PData[1] )
  150           {
  151             // Switch the LED off
  152             case 0:
  153               stopLEDG();
  154               break;
  155 
  156               // Switch the LED on (first make sure that the optional pulsing is disabled)
  157             case 1:
  158               setLEDG();
  159               // Make sure LED is visible at LP mode
  160               waitMS( 20 );
  161               break;
  162 
  163               // Make one pulse
  164             case 2:
  165               pulseLEDG();
  166               // Make sure LED is visible at LP mode
  167               waitMS( 20 );
  168               break;
  169 
  170               // Start pulsing
  171             case 3:
  172               pulsingLEDG();
  173               // Make sure LED is visible at LP mode
  174               waitMS( 20 );
  175               break;
  176           }
  177 
  178           // Return no data
  179           _DpaDataLength = 0;
  180           // Return TRUE to indicate peripheral was handled
  181           goto DpaHandleReturnTRUE;
  182         }
  183       }
  184   }
  185 
  186   return FALSE;
  187 }
  188 
  189 //############################################################################################
  190 // 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)
  191 #include "DPAcustomHandler.h"
  192 //############################################################################################