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.16 $ 8 // Date: $Date: 2017/10/19 07:48:56 $ 9 // 10 // Revision history: 11 // 2017/08/14 Release for DPA 3.01 12 // 2017/03/13 Release for DPA 3.00 13 // 2015/08/05 Release for DPA 2.20 14 // 15 // ********************************************************************* 16 17 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/ 18 19 // Default IQRF include (modify the path according to your setup) 20 #include "IQRF.h" 21 22 // Default DPA header (modify the path according to your setup) 23 #include "DPA.h" 24 // Default Custom DPA Handler header (modify the path according to your setup) 25 #include "DPAcustomHandler.h" 26 27 // This code illustrates bonding using a button connected to the custom pin. 28 // Except going to sleep the code behaves the same way as the standard bonding procedure. 29 30 #define USE_DpaEvent_BondingButton 31 32 // Custom bonding button. Change to the pin and an active level of your choice. 33 #define IsButton ( !PORTA.5 ) 34 //#define IsButton buttonPressed 35 36 // 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 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 #ifdef USE_DpaEvent_BondingButton 66 // ------------------------------------------------- 67 case DpaEvent_BondingButton: 68 // Called to allow a bonding button customization 69 70 // Some sample user data passed 71 UserBondingData[3] = 0x12; 72 UserBondingData[2] = 0x34; 73 UserBondingData[1] = 0x56; 74 UserBondingData[0] = 0x78; 75 76 userReg1.0 = 0; 77 if ( IsButton ) 78 userReg1.0 = 1; 79 80 goto DpaHandleReturnTRUE; 81 82 #else 83 // ------------------------------------------------- 84 case DpaEvent_Reset: 85 // Called after module is reset 86 87 // Handle (un)bonding 88 if ( !amIBonded() ) 89 { 90 // If and while node is not bonded 91 for ( ;; ) 92 { 93 // The button must be pressed before bonding 94 while ( !IsButton ) 95 { 96 // Do and Wait for a button press indication 97 pulseLEDR(); 98 waitDelay( 25 ); 99 } 100 101 // Bonding 102 pulseLEDR(); 103 // Some sample user data passed 104 UserBondingData[3] = 0x12; 105 UserBondingData[2] = 0x34; 106 UserBondingData[1] = 0x56; 107 UserBondingData[0] = 0x78; 108 // Check RFIC 109 if ( wasRFICrestarted() ) 110 DpaApiSetRfDefaults(); 111 112 // Bonding OK? 113 if ( bondRequestAdvanced() ) 114 { 115 // Set DPA API variable 116 NodeWasBonded = TRUE; 117 // Save bonding data to peripheral RAM just as example 118 copyMemoryBlock( UserBondingData, PeripheralRam, sizeof( UserBondingData ) ); 119 // Button must be released 120 do 121 { 122 pulseLEDG(); // Shorter than _LEDG = 1 123 } while ( IsButton ); 124 // and exit 125 break; 126 } 127 } 128 } 129 else 130 { 131 // Bonded 132 uns8 pressCnt @ userReg0; 133 // Set LEDG on for 2.0 s 134 pressCnt = 2000 / ( 5L * 10 ) + 1; 135 do 136 { 137 waitDelay( 5 ); 138 pulseLEDG(); // Shorter than _LEDG = 1 139 } while ( IsButton && --pressCnt != 0 ); // one short LED pulse does not matter, do - while is shorter than while 140 stopLEDG(); // Shorter than _LEDG = 0 141 142 // Was button pressed after LEDG was on for 2.0 s? 143 if ( pressCnt == 0 ) 144 { 145 pressCnt = 500 / ( 10L * 10 ) + 1; 146 do 147 { 148 waitDelay( 10 ); 149 } while ( IsButton && --pressCnt != 0 ); // one enter loop phase does not matter, do - while is shorter than while 150 151 // Was button released within 0.5 s? 152 if ( pressCnt != 0 ) 153 { 154 // Indicated unbonding 155 _LEDR = 1; 156 removeBond(); 157 waitDelay( 100 ); 158 stopLEDR(); // Shorter than _LEDR = 0 159 } 160 } 161 } 162 163 goto DpaHandleReturnTRUE; 164 #endif 165 } 166 167 return FALSE; 168 } 169 170 //############################################################################################ 171 // 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) 172 #include "DPAcustomHandler.h" 173 //############################################################################################