1 // *********************************************************************
    2 //   Custom DPA Handler code example - Peripheral memory mapping       *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-PeripheralMemoryMapping.c,v $
    7 // Version: $Revision: 1.38 $
    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 // Default IQRF include (modify the path according to your setup)
   23 #include "IQRF.h"
   24 
   25 // Uncomment to implement Custom DPA Handler for Coordinator
   26 //#define COORDINATOR_CUSTOM_HANDLER
   27 
   28 // Default DPA header (modify the path according to your setup)
   29 #include "DPA.h"
   30 // Default Custom DPA Handler header (modify the path according to your setup)
   31 #include "DPAcustomHandler.h"
   32 
   33 // The application demonstrates mapping of the actual MCU peripherals to the RAM peripheral
   34 // PeripheralRam[0].0 = LEDR status (read/write)
   35 // PeripheralRam[0].1 = LEDG status (read/write)
   36 // PeripheralRam[1].0 = LEDR status invert request (write)
   37 // PeripheralRam[1].1 = LEDG status invert request (write)
   38 // PeripheralRam[2].0 = Button status (read)
   39 // PeripheralRam[3].0 = Temperature (read)
   40 
   41 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   42 //############################################################################################
   43 bit CustomDpaHandler()
   44 //############################################################################################
   45 {
   46   // Peripheral RAM mapping structure
   47   struct
   48   {
   49     // Actual LED state
   50     uns8    LEDstate;
   51     // Request to invert LED state
   52     uns8    LEDinvert;
   53     // Button state
   54     uns8    Button;
   55     // Measured temperature
   56     uns8    Temperature;
   57   } Mapping @ PeripheralRam;
   58 
   59   // Handler presence mark
   60   clrwdt();
   61 
   62   // Flags that the RAM was written
   63   static bit ramWritten;
   64 
   65   // Detect DPA event to handle (unused event handlers can be commented out or even deleted)
   66   switch ( GetDpaEvent() )
   67   {
   68 #ifdef DpaEvent_Interrupt
   69     // -------------------------------------------------
   70     case DpaEvent_Interrupt:
   71       // Do an extra quick background interrupt work
   72       // ! 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.
   73       // ! 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.
   74       // ! 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.
   75       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   76       // ! Make sure race condition does not occur when accessing those variables at other places.
   77       // ! 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.
   78       // ! Do not call any OS functions except setINDFx().
   79       // ! Do not use any OS variables especially for writing access.
   80       // ! 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.
   81 
   82       return Carry;
   83 #endif
   84 
   85       // -------------------------------------------------
   86     case DpaEvent_ReceiveDpaRequest:
   87       // Called after DPA request was received
   88 
   89       // RAM peripheral read request
   90       // (could be optimized by checking the RAM address to read from and based on its value skipping e.g. temperature measurement)
   91       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_READ )
   92       {
   93         // Read button state
   94         W = 0;
   95         if ( buttonPressed )
   96           W = 1;
   97         Mapping.Button = W;
   98         // Read temperature
   99         Mapping.Temperature = getTemperature();
  100       }
  101 
  102       break;
  103 
  104       // -------------------------------------------------
  105     case DpaEvent_Notification:
  106       // Called after DPA request was processed and after DPA response was sent
  107 
  108       // Anything written to the RAM?
  109       // (could be optimized by checking the RAM address that was written to and based on its value skipping LEDs update)
  110       if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_WRITE )
  111         ramWritten = TRUE;
  112 
  113       break;
  114 
  115       // -------------------------------------------------
  116     case DpaEvent_AfterRouting:
  117       // Called after Notification and after routing of the DPA response was finished
  118 
  119       // Something was written to the RAM?
  120       // Update LEDs at the defined after routing time i.e. at the same time at all nodes when addressed by broadcast
  121       if ( ramWritten )
  122       {
  123         ramWritten = FALSE;
  124 
  125         // Update LEDs state
  126         Mapping.LEDstate ^= Mapping.LEDinvert;
  127         Mapping.LEDinvert = 0;
  128 
  129         // Update LEDR
  130         if ( Mapping.LEDstate.0 )
  131           setLEDR();
  132         else
  133           stopLEDR(); // Must be used when pulseLEDR is used
  134 
  135         // Update LEDG
  136         if ( Mapping.LEDstate.1 )
  137           setLEDG();
  138         else
  139           stopLEDG(); // Must be used when pulseLEDR is used
  140 
  141         // Make sure LED is visible at LP mode
  142         waitMS( 20 );
  143       }
  144 
  145       break;
  146 
  147       // -------------------------------------------------
  148     case DpaEvent_DpaRequest:
  149       // Called to interpret DPA request for peripherals
  150       // -------------------------------------------------
  151       // Peripheral enumeration
  152       if ( IsDpaEnumPeripheralsRequest() )
  153       {
  154         _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x000F;
  155         _DpaMessage.EnumPeripheralsAnswer.HWPIDver |= 0xABCD;
  156 
  157 DpaHandleReturnTRUE:
  158         return TRUE;
  159       }
  160 
  161       break;
  162   }
  163 
  164   return FALSE;
  165 }
  166 //############################################################################################
  167 // 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)
  168 #include "DPAcustomHandler.h"
  169 //############################################################################################