1 // *********************************************************************
    2 //   Custom DPA Handler code example - Shows autobonding after reset   *
    3 // *********************************************************************
    4 // Copyright (c) IQRF Tech s.r.o.
    5 //
    6 // File:    $RCSfile: CustomDpaHandler-Autobond.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 //   2018/10/25  Release for DPA 3.03
   13 //   2017/03/13  Release for DPA 3.00
   14 //   2015/08/05  Release for DPA 2.20
   15 //   2014/10/31  Release for DPA 2.10
   16 //   2014/04/30  Release for DPA 2.00
   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 // If the node is not bonded, then it tries to bond itself while paying attention not to cause RF collision with other nodes/coordinator
   31 
   32 // Random value
   33 static uns8 rand;
   34 // Generates next random value
   35 uns8 Rand();
   36 
   37 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location!
   38 //############################################################################################
   39 bit CustomDpaHandler()
   40 //############################################################################################
   41 {
   42   // Handler presence mark
   43   clrwdt();
   44 
   45   // Detect DPA event to handle
   46   switch ( GetDpaEvent() )
   47   {
   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 DpaHandleReturnTRUE:
   62       return TRUE;
   63 
   64       // -------------------------------------------------
   65     case DpaEvent_Reset:
   66       // Called after module is reset
   67 
   68       // Do default unbonding procedure but override bonding one
   69       if ( !amIBonded() )
   70       {
   71         // Indicate start of the bonding by a few rapid LEDG pulses lasting 2s
   72         pulsingLEDG();
   73         waitDelay( 200 );
   74         stopLEDG();
   75 
   76         // Generate non-zero random number (seed)
   77         moduleInfo();
   78         rand = bufferINFO[0];
   79         if ( rand == 0 )
   80           rand.0 = 1;
   81 
   82         // Gap size doubles with every attempt to run bonding
   83         uns8 gapSize = 1;
   84         for ( ;; )
   85         {
   86           // Wait random time
   87           waitMS( Rand() );
   88           // Indicate the bonding attempt by LEDG
   89           pulseLEDG();
   90 
   91           // There must be a long no-network traffic interval to try bonding (listen before talk algorithm)
   92           uns8 loop = 200;
   93           do
   94           {
   95             Rand();
   96             waitMS( 1 );
   97             // Check RFIC
   98             if ( wasRFICrestarted() )
   99               DpaApiSetRfDefaults();
  100           } while ( !checkRF( 20 ) && --loop != 0 );
  101 
  102           // Was there no traffic?
  103           if ( loop == 0 )
  104           {
  105             // Yes, try bonding indicated by LEDR
  106             pulseLEDR();
  107 
  108             // Bonding using 3 service channels
  109             _3CHTX = TRUE;
  110             bondRequestAdvanced();
  111           }
  112 
  113           // Are we bonded?
  114           if ( amIBonded() )
  115           {
  116             // Optionally get the user data
  117             //copyMemoryBlock( hostUserDataReceived, PeripheralRam, sizeof( hostUserDataReceived ) );
  118             // Yes, do a long LEDG pulse
  119             setLEDG();
  120             waitDelay( 25 );
  121             stopLEDG();
  122             // and return 
  123             goto DpaHandleReturnTRUE;
  124           }
  125 
  126           // Randomly wait for the next attempt to bond
  127           loop = gapSize;
  128           uns8 rndTime = ( Rand() & 0b11111 ) + 10;
  129           do
  130           {
  131             waitMS( rndTime );
  132           } while ( --loop != 0 );
  133 
  134           // Next gap size
  135           Carry = 1;
  136           gapSize = rl( gapSize );
  137         }
  138       }
  139 
  140       break;
  141   }
  142 
  143   return FALSE;
  144 }
  145 
  146 
  147 //############################################################################################
  148 uns8 Rand()
  149 //############################################################################################
  150 {
  151   rand = lsr( rand );
  152   W = 0b10111000;   // x^8 + x^6 + x^5 + x^4 + 1
  153   if ( Carry )
  154     rand ^= W;
  155 
  156   return rand;
  157 }
  158 
  159 //############################################################################################
  160 // 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) 
  161 #include "DPAcustomHandler.h"
  162 //############################################################################################