1 // *********************************************************************
    2 //   Custom DPA Handler code example - HW peripheral memory mapping    *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-LED-MemoryMapping.c,v $
    7 // Version: $Revision: 1.20 $
    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 // The application demonstrates memory mapping of the actual MCU peripheral to the DPA RAM peripheral
   22 // byte @ PeripheralRam[0] = controls red LED
   23 // byte @ PeripheralRam[1] = controls green LED
   24 //
   25 // bytes @ PeripheralRam[0..1]
   26 //   0: LED off
   27 //   1: LED on
   28 //   2: LED pulse
   29 //   3: LED pulsing
   30 //   other: no change
   31 
   32 // Default IQRF include (modify the path according to your setup)
   33 #include "IQRF.h"
   34 
   35 // Default DPA header (modify the path according to your setup)
   36 #include "DPA.h"
   37 // Default Custom DPA Handler header (modify the path according to your setup)
   38 #include "DPAcustomHandler.h"
   39 
   40 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   41 //############################################################################################
   42 bit CustomDpaHandler()
   43 //############################################################################################
   44 {
   45   // Handler presence mark
   46   clrwdt();
   47 
   48   // Detect DPA event to handle (unused event handlers can be commented out or even deleted)
   49   switch ( GetDpaEvent() )
   50   {
   51 #ifdef DpaEvent_Interrupt
   52     // -------------------------------------------------
   53     case DpaEvent_Interrupt:
   54       // Do an extra quick background interrupt work
   55       // ! 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.
   56       // ! 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.
   57       // ! 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.
   58       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   59       // ! Make sure race condition does not occur when accessing those variables at other places.
   60       // ! 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.
   61       // ! Do not call any OS functions except setINDFx().
   62       // ! Do not use any OS variables especially for writing access.
   63       // ! 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.
   64 
   65       return Carry;
   66 #endif
   67 
   68       // -------------------------------------------------
   69     case DpaEvent_Notification:
   70       // Called after DPA request was processed and after DPA response was sent
   71 
   72       // Anything written to the RAM?
   73       // (could be optimized by checking the RAM address that was written to and based on its value control the appropriate LED)
   74       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_WRITE )
   75       {
   76         // Control red LED
   77         switch ( PeripheralRam[0] )
   78         {
   79           // Switch the LED off
   80           case 0:
   81             stopLEDR();
   82             break;
   83 
   84             // Switch the LED on (first make sure that the optional pulsing is disabled)
   85           case 1:
   86             setLEDR();
   87             // Make sure LED is visible at LP mode
   88             waitMS( 20 );
   89             break;
   90 
   91             // Make one pulse
   92           case 2:
   93             pulseLEDR();
   94             // Make sure LED is visible at LP mode
   95             waitMS( 20 );
   96             break;
   97 
   98             // Start pulsing
   99           case 3:
  100             pulsingLEDR();
  101             break;
  102         }
  103 
  104         // Control green LED
  105         switch ( PeripheralRam[1] )
  106         {
  107           // Switch the LED off
  108           case 0:
  109             stopLEDG();
  110             break;
  111 
  112             // Switch the LED on (first make sure that the optional pulsing is disabled)
  113           case 1:
  114             setLEDG();
  115             // Make sure LED is visible at LP mode
  116             waitMS( 20 );
  117             break;
  118 
  119             // Make one pulse
  120           case 2:
  121             pulseLEDG();
  122             // Make sure LED is visible at LP mode
  123             waitMS( 20 );
  124             break;
  125 
  126             // Start pulsing
  127           case 3:
  128             pulsingLEDG();
  129             break;
  130         }
  131       }
  132 
  133       break;
  134   }
  135 
  136   return FALSE;
  137 }
  138 //############################################################################################
  139 // 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)
  140 #include "DPAcustomHandler.h"
  141 //############################################################################################