1 // ********************************************************************* 2 // Custom DPA Handler code example - Shows autobonding after reset * 3 // ********************************************************************* 4 // Copyright (c) MICRORISC s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-Autobond.c,v $ 7 // Version: $Revision: 1.42 $ 8 // Date: $Date: 2022/02/25 09:41:25 $ 9 // 10 // Revision history: 11 // 2022/02/24 Release for DPA 4.17 12 // 2019/06/03 Release for DPA 4.02 13 // 2018/10/25 Release for DPA 3.03 14 // 2017/03/13 Release for DPA 3.00 15 // 2015/08/05 Release for DPA 2.20 16 // 2014/10/31 Release for DPA 2.10 17 // 2014/04/30 Release for DPA 2.00 18 // 19 // ********************************************************************* 20 21 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/ 22 23 // Default IQRF include (modify the path according to your setup) 24 #include "IQRF.h" 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 // If the node is not bonded, then it tries to bond itself while paying attention not to cause RF collision with other nodes/coordinator 32 33 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 34 //############################################################################################ 35 bit CustomDpaHandler() 36 //############################################################################################ 37 { 38 // Handler presence mark 39 clrwdt(); 40 41 // Detect DPA event to handle 42 switch ( GetDpaEvent() ) 43 { 44 // ------------------------------------------------- 45 case DpaEvent_Interrupt: 46 // Do an extra quick background interrupt work 47 // ! 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. 48 // ! 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. 49 // ! 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. 50 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 51 // ! Make sure race condition does not occur when accessing those variables at other places. 52 // ! 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. 53 // ! Do not call any OS functions except setINDFx(). 54 // ! Do not use any OS variables especially for writing access. 55 // ! 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. 56 57 DpaHandleReturnTRUE: 58 return TRUE; 59 60 // ------------------------------------------------- 61 case DpaEvent_Reset: 62 // Called after module is reset 63 64 // Do default unbonding procedure but override bonding one 65 if ( !amIBonded() ) 66 { 67 // Indicate start of the bonding by a few rapid LEDG pulses lasting 2s 68 pulsingLEDG(); 69 waitDelay( 200 ); 70 stopLEDG(); 71 72 // Gap size doubles with every attempt to run bonding 73 uns8 gapSize = 1; 74 for ( ;; ) 75 { 76 // Wait random time 77 waitMS( _DpaApiRandom() ); 78 // Indicate the bonding attempt by LEDG 79 pulseLEDG(); 80 81 // There must be a long no-network traffic interval to try bonding (listen before talk algorithm) 82 uns8 loop = 200; 83 do 84 { 85 _DpaApiRandom(); 86 waitMS( 1 ); 87 // Check RFIC 88 if ( wasRFICrestarted() ) 89 DpaApiSetRfDefaults(); 90 } while ( !checkRF( 20 ) && --loop != 0 ); 91 92 // Was there no traffic? 93 if ( loop == 0 ) 94 { 95 // Yes, indicate bonding attempt by LEDR 96 pulseLEDR(); 97 98 // Bonding using 3 service channels 99 _3CHTX = TRUE; 100 bondRequestAdvanced(); 101 } 102 103 // Are we bonded? 104 if ( amIBonded() ) 105 { 106 // Optionally get the user data 107 //copyMemoryBlock( hostUserDataReceived, PeripheralRam, sizeof( hostUserDataReceived ) ); 108 // Yes, do a long LEDG pulse 109 setLEDG(); 110 waitDelay( 25 ); 111 stopLEDG(); 112 // and return 113 goto DpaHandleReturnTRUE; 114 } 115 116 // Randomly wait for the next attempt to bond 117 loop = gapSize; 118 uns8 rndTime = ( _DpaApiRandom() & 0b11111 ) + 10; 119 do 120 { 121 waitMS( rndTime ); 122 } while ( --loop != 0 ); 123 124 // Next gap size 125 gapSize <<= 1; 126 gapSize |= 1; 127 } 128 } 129 130 break; 131 } 132 133 return FALSE; 134 } 135 136 //############################################################################################ 137 // 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) 138 #include "DPAcustomHandler.h" 139 //############################################################################################