1 // *********************************************************************
    2 //   Custom DPA Handler - XLP long term sleep and wake up              *
    3 // *********************************************************************
    4 // Copyright (c) MICRORISC s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-XLPstandBy.c,v $
    7 // Version: $Revision: 1.12 $
    8 // Date:    $Date: 2022/02/25 09:41:25 $
    9 //
   10 // Revision history:
   11 //   2022/02/24  Release for DPA 4.17
   12 //   2019/01/10  Release for DPA 4.00
   13 //
   14 // *********************************************************************
   15 
   16 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/
   17 
   18 // Default IQRF include (modify the path according to your setup)
   19 #include "IQRF.h"
   20 
   21 // Default DPA header (modify the path according to your setup)
   22 #include "DPA.h"
   23 // Default Custom DPA Handler header (modify the path according to your setup)
   24 #include "DPAcustomHandler.h"
   25 
   26 //############################################################################################
   27 bit CustomDpaHandler()
   28 //############################################################################################
   29 {
   30   // Handler presence mark
   31   clrwdt();
   32 
   33   static bit doXLPstandBy;
   34 
   35   // Detect DPA event to handle (unused event handlers can be commented out or even deleted)
   36   switch ( GetDpaEvent() )
   37   {
   38     // -------------------------------------------------
   39     case DpaEvent_Interrupt:
   40       // Do an extra quick background interrupt work
   41 
   42       return Carry;
   43 
   44       // -------------------------------------------------
   45     case DpaEvent_Notification:
   46       // Called after DPA request was processed and after DPA response was sent
   47 
   48       if ( doXLPstandBy )
   49       {
   50         doXLPstandBy = FALSE;
   51 
   52         setNonetMode();
   53         setRFmode( _WPE | _RX_XLP | _TX_XLP );
   54 
   55         setLEDG();
   56         setLEDR();
   57         waitDelay( 100 );
   58 
   59         uns8 cnt;
   60         for ( ;; )
   61         {
   62           cnt++;
   63           if ( cnt.0 )
   64             setLEDG();
   65           else
   66             setLEDR();
   67           waitMS( 1 );
   68           stopLEDR();
   69           stopLEDG();
   70 
   71           toutRF = 2 * 10;
   72           if ( RFRXpacket() && DLEN == 1 && bufferRF[0] == 'W' )
   73           {
   74             setLEDG();
   75             setLEDR();
   76             waitDelay( 100 );
   77             stopLEDR();
   78             stopLEDG();
   79             break;
   80           }
   81         }
   82 
   83         DpaApiSetRfDefaults();
   84         setNodeMode();
   85       }
   86       break;
   87 
   88       // -------------------------------------------------
   89     case DpaEvent_DpaRequest:
   90       // Called to interpret DPA request for peripherals
   91       IfDpaEnumPeripherals_Else_PeripheralInfo_Else_PeripheralRequest()
   92       {
   93         // -------------------------------------------------
   94         // Peripheral enumeration
   95 
   96         _DpaMessage.EnumPeripheralsAnswer.UserPerNr |=  1;
   97         FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 );
   98         _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x581F;
   99 
  100 DpaHandleReturnTRUE:
  101         return TRUE;
  102       }
  103       else
  104       {
  105         // -------------------------------------------------
  106         // Get information about peripheral
  107 
  108         if ( _PNUM == PNUM_USER + 0 )
  109         {
  110           _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_USER_AREA;
  111           _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_WRITE;
  112           goto DpaHandleReturnTRUE;
  113         }
  114 
  115         break;
  116       }
  117 
  118       // -------------------------------------------------
  119       // Handle peripheral command
  120 
  121       if ( _PNUM == PNUM_USER + 0 && _PCMD == 0 && _DpaDataLength == 0 )
  122       {
  123         if ( ( DpaApiReadConfigByte( CFGIND_DPA_PERIPHERALS + PNUM_COORDINATOR / 8 ) & ( 1 << ( PNUM_COORDINATOR % 8 ) ) ) != 0 )
  124         {
  125           // Coordinator
  126           setNonetMode();
  127           setRFpower( 7 );
  128           setRFmode( _TX_XLP );
  129 
  130           PIN = 0;
  131           DLEN = 1;
  132           bufferRF[0] = 'W';
  133           RFTXpacket();
  134 
  135           DpaApiSetRfDefaults();
  136           setCoordinatorMode();
  137         }
  138         else
  139         {
  140           // Node
  141           doXLPstandBy = TRUE;
  142         }
  143 
  144         goto DpaHandleReturnTRUE;
  145       }
  146 
  147       break;
  148   }
  149 
  150 DpaHandleReturnFALSE:
  151   return FALSE;
  152 }
  153 //############################################################################################
  154 // 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)
  155 #include "DPAcustomHandler.h"
  156 //############################################################################################