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.31 $
    8 // Date:    $Date: 2018/06/14 15:57:09 $
    9 //
   10 // Revision history:
   11 //   2017/03/13  Release for DPA 3.00
   12 //   2015/08/05  Release for DPA 2.20
   13 //   2014/10/31  Release for DPA 2.10
   14 //   2014/04/30  Release for DPA 2.00
   15 //
   16 // ****************************************************************************
   17 
   18 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   19 
   20 // Default IQRF include (modify the path according to your setup)
   21 #include "IQRF.h"
   22 
   23 // Uncomment to implement Custom DPA Handler for Coordinator (actually not needed as we do not used EEPROMs)
   24 //#define COORDINATOR_CUSTOM_HANDLER
   25 
   26 // Default DPA header (modify the path according to your setup)
   27 #include "DPA.h"
   28 // Default Custom DPA Handler header (modify the path according to your setup)
   29 #include "DPAcustomHandler.h"
   30 
   31 // This example shows how to send asynchronous request from Node to the coordinator or its master device
   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         // Try to find out whether Coordinator peripheral is on, so we are [CN] device and have to switch the channel and mode
   76         // Enumerate peripherals
   77         _PNUM = PNUM_ENUMERATION;
   78         _PCMD = CMD_GET_PER_INFO;
   79         _DpaDataLength = 0;
   80         DpaApiLocalRequest();
   81         // Remember if peripheral is on 
   82         bit isCoordinatorPeripheralOn = FALSE;
   83         if ( ( _DpaMessage.EnumPeripheralsAnswer.EmbeddedPers[PNUM_COORDINATOR / 8] & ( 1 << ( PNUM_COORDINATOR % 8 ) ) ) != 0 )
   84           isCoordinatorPeripheralOn = TRUE;
   85 
   86         // Packet ID
   87         PID = ++pid;
   88         // Number of hops = my VRN
   89         RTHOPS = ntwVRN;
   90         // No DPA Params used
   91         _DpaParams = 0;
   92 
   93         // Data is sent to the Coordinator
   94         if ( btnCnt == 0 )
   95         {
   96           // To the coordinator master if button pressed > 0.5s
   97           _NADR = COORDINATOR_ADDRESS;
   98           pulseLEDR();
   99         }
  100         else
  101         {
  102           // Otherwise to coordinator the local device
  103           _NADR = LOCAL_ADDRESS;
  104           pulseLEDG();
  105         }
  106         _NADRhigh = 0;
  107 
  108         // Use IO peripheral to work with LEDs even if LED peripherals are not present
  109         _PNUM = PNUM_IO;
  110         // Make a LEDR pulse
  111         _PCMD = CMD_IO_SET;
  112         // Any HWPID
  113         _HWPID = HWPID_DoNotCheck;
  114 
  115         // LEDR=1 => Set PORTA.2 to 1
  116         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Port = PNUM_IO_PORTA;  // PORTA
  117         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Mask = 0b0000.0100;    // bit 2
  118         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Value = 0b0000.0100;   // bit 2 = 1
  119 
  120         // 64 ms pulse
  121         _DpaMessage.PerIoDirectionAndSet_Request.Delays[1].Header = PNUM_IO_DELAY;  // delay
  122         _DpaMessage.PerIoDirectionAndSet_Request.Delays[1].Delay = 64;              // 64
  123 
  124         // LEDR=0 => Set PORTA.2 to 0
  125         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[2].Port = PNUM_IO_PORTA;  // PORTA
  126         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[2].Mask = 0b0000.0100;    // bit 2
  127         _DpaMessage.PerIoDirectionAndSet_Request.Triplets[2].Value = 0b0000.0000;   // bit 2 = 0
  128 
  129         // Setup the correct length of data
  130         _DpaDataLength = 3 * sizeof( _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0] );
  131 
  132         if ( isCoordinatorPeripheralOn )
  133         {
  134           // Work again as a node
  135           setNodeMode();
  136           // A channel of the main network
  137           DpaApiSetRfDefaults();
  138         }
  139 
  140         // Transmit DPA message with DPA Value equal the lastRSSI (can be any other value)
  141         DpaApiRfTxDpaPacket( lastRSSI, 0x7F );
  142 
  143         // Wait for the button to be released (could be done in cleaner way w/o active waiting at this point of code ...)
  144         while ( buttonPressed )
  145           clrwdt();
  146       }
  147     }
  148     break;
  149   }
  150 
  151 DpaHandleReturnFALSE:
  152   return FALSE;
  153 }
  154 //############################################################################################
  155 // 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) 
  156 #include "DPAcustomHandler.h"
  157 //############################################################################################