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