1 // **********************************************************************************
    2 //   Custom DPA Handler code example - Reading by FRC                               *
    3 // **********************************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Coordinator-FRCandSleep.c,v $
    7 // Version: $Revision: 1.32 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   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 //
   16 // **********************************************************************************
   17 
   18 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   19 
   20 // Default IQRF include (modify the path according to your setup)
   21 #include "IQRF.h"
   22 
   23 // Implement Custom DPA Handler for Coordinator
   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 // Application periodically sends a FRC request 0x81 and waits till nodes wake up from sleep
   32 // Result is send to the Interface master
   33 
   34 // Sleep time between FRC requests, unit is 2.097
   35 #define SLEEP_TIME  5
   36 
   37 // Calculation of the 10ms measurements from the C point of view, add 5% gap
   38 #define SleepIn10msPlusGap  ( ( ( (uns24)SLEEP_TIME ) * ( 2097 * 1.05 ) ) / 10 )
   39 
   40 #if SleepIn10msPlusGap >= 0x8000
   41 #error Too long sleep time to be measured by DpaTicks.
   42 #error Maximum time is 312 s.
   43 #endif
   44 
   45 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   46 //############################################################################################
   47 bit CustomDpaHandler()
   48 //############################################################################################
   49 {
   50   // Handler presence mark
   51   clrwdt();
   52 
   53   // Detect DPA event to handle
   54   switch ( GetDpaEvent() )
   55   {
   56     // -------------------------------------------------
   57     case DpaEvent_Init:
   58 
   59       // Give GSM GW time to start SPI master
   60       waitDelay( 100 );
   61 
   62       break;
   63 
   64       // -------------------------------------------------
   65     case DpaEvent_Idle:
   66       // Do a quick background work when RF packet is not received
   67 
   68       // Time undercount?
   69       if ( DpaTicks.15 == 1 )
   70       {
   71         pulseLEDG();
   72         // Use FRC peripheral
   73         _PNUM = PNUM_FRC;
   74         // Run FRC
   75         _PCMD = CMD_FRC_SEND;
   76         // FRC = FRC_AcknowledgedBroadcastBytes
   77         _DpaMessage.PerFrcSend_Request.FrcCommand = FRC_AcknowledgedBroadcastBytes;
   78         // Length of DPA Request
   79         // DPA request length
   80         _DpaMessage.PerFrcSend_Request.UserData[0] = 8;
   81         // OS_SLEEP request
   82         _DpaMessage.PerFrcSend_Request.UserData[1] = PNUM_OS;
   83         _DpaMessage.PerFrcSend_Request.UserData[2] = CMD_OS_SLEEP;
   84         // Any HWPID
   85         _DpaMessage.PerFrcSend_Request.UserData[3] = ( HWPID_DoNotCheck >> 0 ) & 0xFF;
   86         _DpaMessage.PerFrcSend_Request.UserData[4] = ( HWPID_DoNotCheck >> 8 ) & 0xFF;
   87         // Sleep time
   88         _DpaMessage.PerFrcSend_Request.UserData[5] = ( SLEEP_TIME >> 0 ) & 0xFF;;
   89         _DpaMessage.PerFrcSend_Request.UserData[6] = ( SLEEP_TIME >> 8 ) & 0xFF;
   90         // LEDG pulse ofter wake up
   91         _DpaMessage.PerFrcSend_Request.UserData[7] = 0b0.0100;
   92         // Total length of data
   93         _DpaDataLength = 8 + sizeof( _DpaMessage.PerFrcSend_Request.FrcCommand );
   94         // Run FRC
   95         DpaApiLocalRequest();
   96         // Send to IFace master
   97         _NADR = COORDINATOR_ADDRESS;
   98         _NADRhigh = 0;
   99         DpaApiSendToIFaceMaster( 0, 0 );
  100 
  101         // Use FRC peripheral
  102         _PNUM = PNUM_FRC;
  103         // Run FRC extra result
  104         _PCMD = CMD_FRC_EXTRARESULT;
  105         // Total length of data
  106         _DpaDataLength = 0;
  107         // Run FRC extra result
  108         DpaApiLocalRequest();
  109         // Send to IFace master
  110         _NADR = COORDINATOR_ADDRESS;
  111         _NADRhigh = 0;
  112         DpaApiSendToIFaceMaster( 0, 0 );
  113 
  114         pulseLEDR();
  115 
  116         // Disable interrupts to make sure 16b variable DpaTicks access is atomic
  117         GIE = FALSE;
  118         // Setup "timer" for sleep time, unit is 10ms, add 5% safe gap
  119         DpaTicks = (uns16)SleepIn10msPlusGap;
  120         GIE = TRUE;
  121       }
  122       break;
  123   }
  124 
  125   return FALSE;
  126 }
  127 
  128 //############################################################################################
  129 // 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)
  130 #include "DPAcustomHandler.h"
  131 //############################################################################################