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.21 $ 8 // Date: $Date: 2017/08/09 12:24:47 $ 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 // 2014/04/30 Release for DPA 2.00 15 // 16 // ********************************************************************* 17 18 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/ 19 20 // Default IQRF include (modify the path according to your setup) 21 #include "IQRF.h" 22 23 // Default DPA header (modify the path according to your setup) 24 #include "DPA.h" 25 // Default Custom DPA Handler header (modify the path according to your setup) 26 #include "DPAcustomHandler.h" 27 28 // If the node is not bonded, then it tries to bond itself while paying attention not to cause RF collision with other nodes/coordinator 29 // This example works only at STD mode, not at LP mode 30 31 // The example will not work at demo DPA version because demo DPA does not support the following Custom DPA Handler events: DpaEvent_Interrupt, DpaEvent_BeforeSleep, DpaEvent_AfterSleep, DpaEvent_Reset, DpaEvent_DisableInterrupts, DpaEvent_ReceiveDpaResponse, DpaEvent_IFaceReceive, DpaEvent_ReceiveDpaRequest, DpaEvent_BeforeSendingDpaResponse 32 33 // Random value 34 static uns8 rand; 35 // Generates next random value 36 uns8 Rand(); 37 38 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 39 //############################################################################################ 40 bit CustomDpaHandler() 41 //############################################################################################ 42 { 43 // Handler presence mark 44 clrwdt(); 45 46 // Detect DPA event to handle 47 switch ( GetDpaEvent() ) 48 { 49 // ------------------------------------------------- 50 case DpaEvent_Interrupt: 51 // Do an extra quick background interrupt work 52 // ! 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. 53 // ! 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. 54 // ! 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. 55 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 56 // ! Make sure race condition does not occur when accessing those variables at other places. 57 // ! 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. 58 // ! Do not call any OS functions except setINDFx(). 59 // ! Do not use any OS variables especially for writing access. 60 // ! 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. 61 62 DpaHandleReturnTRUE: 63 return TRUE; 64 65 // ------------------------------------------------- 66 case DpaEvent_Reset: 67 // Called after module is reset 68 69 // Do default unbonding procedure but override bonding one 70 if ( !amIBonded() ) 71 { 72 // Indicate start of the bonding by a few rapid LEDG pulses lasting 2s 73 pulsingLEDG(); 74 waitDelay( 200 ); 75 stopLEDG(); 76 77 // Generate non-zero random number (seed) 78 moduleInfo(); 79 rand = bufferINFO[0]; 80 if ( rand == 0 ) 81 rand.0 = 1; 82 83 // Gap size doubles with every attempt to run bonding 84 uns8 gapSize = 1; 85 for ( ;; ) 86 { 87 // Wait random time 88 waitMS( Rand() ); 89 // Indicate the bonding attempt by LEDG 90 pulseLEDG(); 91 92 // There must be a long no-network traffic interval to try bonding (listen before talk algorithm) 93 uns8 loop = 200; 94 do 95 { 96 Rand(); 97 waitMS( 1 ); 98 // Check RFIC 99 if ( wasRFICrestarted() ) 100 DpaApiSetRfDefaults(); 101 } while ( !checkRF( 20 ) && --loop != 0 ); 102 103 // Was there no traffic? 104 if ( loop == 0 ) 105 { 106 // Yes, try bonding indicated by LEDR 107 pulseLEDR(); 108 109 // Optionally pass the user data 110 //UserBondingData[3] = 0x12; 111 //UserBondingData[2] = 0x34; 112 //UserBondingData[1] = 0x56; 113 //UserBondingData[0] = 0x78; 114 115 bondRequestAdvanced(); 116 } 117 118 // Are we bonded? 119 if ( amIBonded() ) 120 { 121 // Optionally get the user data 122 //copyMemoryBlock( UserBondingData, PeripheralRam, sizeof( UserBondingData ) ); 123 // Set DPA API variable 124 NodeWasBonded = TRUE; 125 // Yes, do a long LEDG pulse 126 _LEDG = 1; 127 waitDelay( 25 ); 128 stopLEDG(); 129 // and return 130 goto DpaHandleReturnTRUE; 131 } 132 133 // Randomly wait for the next attempt to bond 134 loop = gapSize; 135 uns8 rndTime = ( Rand() & 0b11111 ) + 10; 136 do 137 { 138 waitMS( rndTime ); 139 } while ( --loop != 0 ); 140 141 // Next gap size 142 Carry = 1; 143 gapSize = rl( gapSize ); 144 } 145 } 146 147 break; 148 } 149 150 return FALSE; 151 } 152 153 154 //############################################################################################ 155 uns8 Rand() 156 //############################################################################################ 157 { 158 rand = lsr( rand ); 159 W = 0b10111000; // x^8 + x^6 + x^5 + x^4 + 1 160 if ( Carry ) 161 rand ^= W; 162 163 return rand; 164 } 165 166 //############################################################################################ 167 // 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) 168 #include "DPAcustomHandler.h" 169 //############################################################################################