1 // *********************************************************************** 2 // Custom DPA Handler code example - External device connected to UART * 3 // *********************************************************************** 4 // Copyright (c) IQRF Tech s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-UART.c,v $ 7 // Version: $Revision: 1.9 $ 8 // Date: $Date: 2017/10/19 07:48:08 $ 9 // 10 // Revision history: 11 // 2017/03/13 Release for DPA 3.00 12 // 2016/02/08 Release for DPA 2.26 13 // 14 // ********************************************************************* 15 16 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/ 17 18 // Default IQRF include (modify the path according to your setup) 19 #include "IQRF.h" 20 21 // Default DPA header (modify the path according to your setup) 22 #include "DPA.h" 23 // Default Custom DPA Handler header (modify the path according to your setup) 24 #include "DPAcustomHandler.h" 25 26 // The application demonstrates simple handler used to connect external device using UART 27 28 // Before uploading this example make sure: 29 // 1. Autoexec is enabled at HWP Configuration 30 // 2. Peripheral UART is enabled at HWP Configuration 31 // 3. External EEPROM upload is selected at IQRF IDE. Please note external EEPROM cannot be uploaded using RFPGM. 32 33 //############################################################################################ 34 35 // HW Profile ID 36 #define HWPid 0x123F 37 // HW Profile ID Version 38 #define HWPidVer 0x0000 39 40 // Bonding button, must be often (depends on used DCTR module) redefined because it might conflict with UART TX signal 41 #define IsButton ( !PORTA.0 ) 42 43 //############################################################################################ 44 #pragma cdata[ __EEESTART + AUTOEXEC_EEEPROM_ADDR ] = \ 45 /* 1. Open UART at 9600 baud */ \ 46 6, PNUM_UART, CMD_UART_OPEN, 0xff, 0xff, DpaBaud_9600, \ 47 /* 2. End of Autoexec */ \ 48 0 49 50 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 51 //############################################################################################ 52 bit CustomDpaHandler() 53 //############################################################################################ 54 { 55 // Handler presence mark 56 clrwdt(); 57 58 // Detect DPA event to handle 59 switch ( GetDpaEvent() ) 60 { 61 // ------------------------------------------------- 62 case DpaEvent_Interrupt: 63 // Do an extra quick background interrupt work 64 // ! 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. 65 // ! 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. 66 // ! 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. 67 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 68 // ! Make sure race condition does not occur when accessing those variables at other places. 69 // ! 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. 70 // ! Do not call any OS functions except setINDFx(). 71 // ! Do not use any OS variables especially for writing access. 72 // ! 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. 73 74 DpaHandleReturnTRUE: 75 return TRUE; 76 77 // Define custom button (un)bonding only when a button is redefined 78 #ifdef IsButton 79 // ------------------------------------------------- 80 case DpaEvent_Reset: 81 // Called after module is reset 82 83 // Handle (un)bonding 84 if ( !amIBonded() ) 85 { 86 // If and while node is not bonded 87 for ( ;; ) 88 { 89 // The button must be pressed before bonding 90 while ( !IsButton ) 91 { 92 // Do and Wait for a button press indication 93 pulseLEDR(); 94 waitDelay( 25 ); 95 } 96 97 // Bonding 98 if ( bondRequestAdvanced() ) 99 { 100 // Set DPA API variable 101 NodeWasBonded = TRUE; 102 // Button must be released 103 do 104 { 105 pulseLEDG(); 106 } while ( IsButton ); 107 // and exit 108 break; 109 } 110 } 111 } 112 else 113 { 114 // Bonded 115 uns8 pressCnt @ userReg0; 116 pressCnt = 2000 / ( 5L * 10 ) + 1; 117 do 118 { 119 waitDelay( 5 ); 120 pulseLEDG(); 121 } while ( IsButton && --pressCnt != 0 ); // one short LED pulse does not matter, do - while is shorter than while 122 stopLEDG(); 123 124 if ( pressCnt == 0 ) 125 { 126 pressCnt = 500 / ( 10L * 10 ) + 1; 127 do 128 { 129 waitDelay( 10 ); 130 } while ( IsButton && --pressCnt != 0 ); // one enter loop phase does not matter, do - while is shorter than while 131 132 if ( pressCnt != 0 ) 133 { 134 _LEDR = 1; 135 removeBond(); 136 waitDelay( 100 ); 137 stopLEDR(); 138 } 139 } 140 } 141 142 goto DpaHandleReturnTRUE; 143 #endif 144 145 // ------------------------------------------------- 146 case DpaEvent_DpaRequest: 147 // Called to interpret DPA request for peripherals 148 // ------------------------------------------------- 149 // Peripheral enumeration 150 if ( IsDpaEnumPeripheralsRequest() ) 151 { 152 // We do not implement user peripheral 153 //_DpaMessage.EnumPeripheralsAnswer.UserPerNr = 0; 154 // Report HWPID 155 _DpaMessage.EnumPeripheralsAnswer.HWPID = HWPid; 156 _DpaMessage.EnumPeripheralsAnswer.HWPIDver = HWPidVer; 157 158 goto DpaHandleReturnTRUE; 159 } 160 } 161 162 return FALSE; 163 } 164 165 //############################################################################################ 166 // 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) 167 #include "DPAcustomHandler.h" 168 //############################################################################################