1 // ****************************************************************************
    2 //   Custom DPA Handler code example - Sending asynchronous request from Node *
    3 // ****************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-AsyncRequest.c,v $
    7 // Version: $Revision: 1.42 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   12 //   2020/01/09  Release for DPA 4.12
   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 // Default DPA header (modify the path according to your setup)
   26 #include "DPA.h"
   27 // Default Custom DPA Handler header (modify the path according to your setup)
   28 #include "DPAcustomHandler.h"
   29 
   30 // This example shows how to send asynchronous request from Node to the Coordinator or its master device.
   31 // ! Please do not forget to set an API variable AsyncReqAtCoordinator at Coordinator to allow an asynchronous request execution there.
   32 
   33 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   34 //############################################################################################
   35 bit CustomDpaHandler()
   36 //############################################################################################
   37 {
   38   // Handler presence mark
   39   clrwdt();
   40 
   41   // Packet ID
   42   static uns8 pid;
   43 
   44   // Detect DPA event to handle
   45   switch ( GetDpaEvent() )
   46   {
   47 #ifdef DpaEvent_Interrupt
   48     // -------------------------------------------------
   49     case DpaEvent_Interrupt:
   50       // Do an extra quick background interrupt work
   51       // ! 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.
   52       // ! 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.
   53       // ! 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.
   54       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   55       // ! Make sure race condition does not occur when accessing those variables at other places.
   56       // ! 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.
   57       // ! Do not call any OS functions except setINDFx().
   58       // ! Do not use any OS variables especially for writing access.
   59       // ! 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.
   60 
   61       return TRUE;
   62 #endif
   63 
   64       // -------------------------------------------------
   65     case DpaEvent_Idle:
   66     {
   67       // Do a background work when RF packet is not received
   68       uns16 btnCnt = 1000 + 1;
   69       while ( buttonPressed && --btnCnt != 0 )
   70         waitMS( 1 );
   71 
   72       // Button was pressed for at least the 100 ms
   73       if ( btnCnt < 1000 - 100 )
   74       {
   75         // Packet ID
   76         PID = ++pid;
   77         // Number of hops = my VRN
   78         RTHOPS = ntwVRN;
   79         // Fake the last sender in case there was no communication at all from the network before
   80         bank0 uns8 lastTX @ usedBank0[0x13];
   81         lastTX = ntwVRN;
   82 
   83         // No DPA Params used
   84         _DpaParams = 0;
   85 
   86         // Data is sent to the Coordinator
   87         if ( btnCnt == 0 )
   88         {
   89           // To the coordinator master if button pressed > 0.5s
   90           _NADR = COORDINATOR_ADDRESS;
   91           pulseLEDR();
   92         }
   93         else
   94         {
   95           // Otherwise to coordinator the local device
   96           _NADR = LOCAL_ADDRESS;
   97           pulseLEDG();
   98         }
   99         _NADRhigh = 0;
  100 
  101         // Use IO peripheral to work with LEDs even if LED peripherals are not present
  102         _PNUM = PNUM_LEDR;
  103         // Make a LEDR pulse
  104         _PCMD = CMD_LED_PULSE;
  105         // Any HWPID
  106         _HWPID = HWPID_DoNotCheck;
  107 
  108         // Setup the correct length of data
  109         _DpaDataLength = 0;
  110 
  111         // Transmit DPA message with DPA Value equal the lastRSSI (can be any other value)
  112         DpaApiRfTxDpaPacket( lastRSSI, 0x7F );
  113 
  114         // Wait for the button to be released (could be done in cleaner way w/o active waiting at this point of code ...)
  115         while ( buttonPressed )
  116           clrwdt();
  117       }
  118     }
  119     break;
  120   }
  121 
  122 DpaHandleReturnFALSE:
  123   return FALSE;
  124 }
  125 //############################################################################################
  126 // 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)
  127 #include "DPAcustomHandler.h"
  128 //############################################################################################