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