1 // ***********************************************************************
    2 //   Custom DPA Handler code example - Shows custom bonding using button *
    3 // ***********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Bonding.c,v $
    7 // Version: $Revision: 1.31 $
    8 // Date:    $Date: 2019/06/06 13:12:17 $
    9 //
   10 // Revision history:
   11 //   2019/06/03  Release for DPA 4.02
   12 //   2019/01/10  Release for DPA 4.00
   13 //   2018/10/25  Release for DPA 3.03
   14 //   2017/08/14  Release for DPA 3.01
   15 //   2017/03/13  Release for DPA 3.00
   16 //   2015/08/05  Release for DPA 2.20
   17 //
   18 // *********************************************************************
   19 
   20 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/
   21 
   22 // Default IQRF include (modify the path according to your setup)
   23 #include "IQRF.h"
   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 // This code illustrates bonding using a button connected to the custom pin.
   31 // Except going to sleep the code behaves the same way as the standard bonding procedure.
   32 
   33 // Define to just reassign a bonding button
   34 // #define  USE_DpaEvent_BondingButton
   35 
   36 // Custom bonding button. Change to the pin and an active level of your choice.
   37 //#define   IsButton  ( !PORTA.5 )
   38 #define IsButton  buttonPressed
   39 
   40 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   41 //############################################################################################
   42 bit CustomDpaHandler()
   43 //############################################################################################
   44 {
   45   // Handler presence mark
   46   clrwdt();
   47 
   48   // Detect DPA event to handle
   49   switch ( GetDpaEvent() )
   50   {
   51     // -------------------------------------------------
   52     case DpaEvent_Interrupt:
   53       // Do an extra quick background interrupt work
   54       // ! 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.
   55       // ! 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.
   56       // ! 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.
   57       // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy.
   58       // ! Make sure race condition does not occur when accessing those variables at other places.
   59       // ! 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.
   60       // ! Do not call any OS functions except setINDFx().
   61       // ! Do not use any OS variables especially for writing access.
   62       // ! 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.
   63       return Carry;
   64 
   65 #ifdef USE_DpaEvent_BondingButton
   66       // -------------------------------------------------
   67     case DpaEvent_BondingButton:
   68       // Called to allow a bonding button customization 
   69 
   70       userReg1.0 = 0;
   71       if ( IsButton )
   72         userReg1.0 = 1;
   73 
   74       return TRUE;
   75 #else
   76       // -------------------------------------------------
   77     case DpaEvent_Reset:
   78       // Called after module is reset
   79 
   80       // Handle (un)bonding
   81       if ( !amIBonded() )
   82       {
   83         // Not bonded
   84 
   85         // If and while node is not bonded
   86         for ( ;; )
   87         {
   88           // If button is not pressed, be ready to be SmartConnected
   89           while ( !IsButton )
   90           {
   91             // Do and wait for an indication
   92             pulseLEDR();
   93             waitMS( 6 );
   94             // Make sure to use LP mode
   95             setRFmode( _WPE | _RX_LP | _TX_LP );
   96 
   97             // Set sometimes a new service channel for SmartConnect, otherwise the last one
   98             static uns8 lastServiceChannel;
   99             static uns8 channelCnt;
  100             // Time to set a new service channel?
  101             if ( ( channelCnt & 0x03 ) == 0 )
  102               // Set a new service channel
  103               lastServiceChannel = 0;
  104             // Next "timer"
  105             channelCnt++;
  106 
  107             // Set the SmartConnect service channel
  108             setServiceChannel( lastServiceChannel );
  109             // Save last service channel used
  110             lastServiceChannel = param2 + 1;
  111 
  112             // Try SmartConnect
  113             _checkRFcfg_PQT = TRUE;
  114             toutRF = 6;
  115             RFRXpacket();
  116             answerSystemPacket();
  117             // Restore RF settings (and make sure Spirit1 is not locked)
  118             wasRFICrestarted();
  119             DpaApiSetRfDefaults();
  120             // SmartConnect success?
  121             if ( amIBonded() )
  122               goto _NodeWasBonded;
  123           }
  124 
  125           // Bonding by button indication
  126           pulseLEDR();
  127           // Traditional bonding using 3 service channels
  128           _3CHTX = TRUE;
  129           // Bonded?
  130           if ( bondRequestAdvanced() )
  131           {
  132 _NodeWasBonded:
  133             // Indicate bonding was done until a button is released
  134             setLEDG();
  135             while ( IsButton );  // Note: WDT is not used yet
  136             // Always indicate LEDG even when button was not pressed
  137             pulseLEDG();
  138             // Always make a gap before next pulseLEDR so LEDG is visible
  139             waitDelay( 50 );
  140             // Exit the loop
  141             break;
  142           }
  143         }
  144       }
  145       else
  146       {
  147         // Bonded
  148 
  149         // Set LEDG on for 2.0 s if button is pressed
  150         startDelay( 200 );
  151         setLEDG();
  152         while ( IsButton && isDelay() );
  153         stopLEDG();
  154 
  155         // Was button pressed after LEDG was on for 2.0 s?
  156         if ( !isDelay() )
  157         {
  158           startDelay( 50 );
  159           do
  160           {
  161             // Was button released within 0.5 s after LED went off?
  162             if ( !IsButton )
  163             {
  164               // Unbond
  165               setLEDR();
  166               removeBond();
  167               waitDelay( 100 );
  168               stopLEDR();
  169               break;
  170             }
  171           } while ( isDelay() );
  172         }
  173       }
  174 
  175       return TRUE;
  176 #endif
  177   }
  178 
  179   return FALSE;
  180 }
  181 
  182 //############################################################################################
  183 // 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) 
  184 #include "DPAcustomHandler.h"
  185 //############################################################################################