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