1 // ********************************************************************* 2 // Custom DPA Handler code example - User peripheral implementation * 3 // ********************************************************************* 4 // Copyright (c) IQRF Tech s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-LED-UserPeripheral.c,v $ 7 // Version: $Revision: 1.19 $ 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/07/26 Release for DPA 2.01 15 // 16 // ********************************************************************* 17 18 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/ 19 20 // This example implements one user peripheral having one command. 21 // PNUM = PNUM_USER + 0 = 0x20 22 // PCMD = 0 23 // 2 bytes of PData 24 // byte PData[0] controls red LED 25 // byte PData[1] controls green LED 26 // 27 // bytes @ PData[0..1] 28 // 0: LED off 29 // 1: LED on 30 // 2: LED pulse 31 // 3: LED pulsing 32 // other: no change 33 34 // Default IQRF include (modify the path according to your setup) 35 #include "IQRF.h" 36 37 // Uncomment to compile Custom DPA Handler for Coordinator 38 //#define COORDINATOR_CUSTOM_HANDLER 39 40 // Default DPA header (modify the path according to your setup) 41 #include "DPA.h" 42 // Default Custom DPA Handler header (modify the path according to your setup) 43 #include "DPAcustomHandler.h" 44 45 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 46 //############################################################################################ 47 bit CustomDpaHandler() 48 //############################################################################################ 49 { 50 // Handler presence mark 51 clrwdt(); 52 53 // Detect DPA event to handle 54 switch ( GetDpaEvent() ) 55 { 56 #ifdef DpaEvent_Interrupt 57 // ------------------------------------------------- 58 case DpaEvent_Interrupt: 59 // Do an extra quick background interrupt work 60 // ! 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. 61 // ! 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. 62 // ! 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. 63 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 64 // ! Make sure race condition does not occur when accessing those variables at other places. 65 // ! 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. 66 // ! Do not call any OS functions except setINDFx(). 67 // ! Do not use any OS variables especially for writing access. 68 // ! 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. 69 70 return Carry; 71 #endif 72 73 // ------------------------------------------------- 74 case DpaEvent_DpaRequest: 75 // Called to interpret DPA request for peripherals 76 // ------------------------------------------------- 77 // Peripheral enumeration 78 if ( IsDpaEnumPeripheralsRequest() ) 79 { 80 // We implement 1 user peripheral 81 _DpaMessage.EnumPeripheralsAnswer.UserPerNr = 1; 82 FlagUserPer( _DpaMessage.EnumPeripheralsAnswer.UserPer, PNUM_USER + 0 ); 83 _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F; 84 _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0xCAFE; 85 86 DpaHandleReturnTRUE: 87 return TRUE; 88 } 89 // ------------------------------------------------- 90 // Get information about peripheral 91 else if ( IsDpaPeripheralInfoRequest() ) 92 { 93 if ( _PNUM == PNUM_USER + 0 ) 94 { 95 _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_USER_AREA; 96 _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_WRITE; 97 goto DpaHandleReturnTRUE; 98 } 99 100 break; 101 } 102 // ------------------------------------------------- 103 else 104 { 105 // Handle peripheral 106 if ( _PNUM == PNUM_USER + 0 ) 107 { 108 // Process commands 109 if ( _PCMD != 0 ) 110 DpaApiReturnPeripheralError( ERROR_PCMD ); 111 112 // Check that there is a correct data length 113 if ( _DpaDataLength != 2 ) 114 DpaApiReturnPeripheralError( ERROR_DATA_LEN ); 115 116 // Control red LED 117 switch ( _DpaMessage.Request.PData[0] ) 118 { 119 // Switch the LED off 120 case 0: 121 stopLEDR(); 122 break; 123 124 // Switch the LED on (first make sure that the optional pulsing is disabled) 125 case 1: 126 stopLEDR(); 127 _LEDR = 1; 128 // Make sure LED is visible at LP mode 129 waitMS( 20 ); 130 break; 131 132 // Make one pulse 133 case 2: 134 pulseLEDR(); 135 // Make sure LED is visible at LP mode 136 waitMS( 20 ); 137 break; 138 139 // Start pulsing 140 case 3: 141 pulsingLEDR(); 142 // Make sure LED is visible at LP mode 143 waitMS( 20 ); 144 break; 145 } 146 147 // Control green LED 148 switch ( _DpaMessage.Request.PData[1] ) 149 { 150 // Switch the LED off 151 case 0: 152 stopLEDG(); 153 break; 154 155 // Switch the LED on (first make sure that the optional pulsing is disabled) 156 case 1: 157 stopLEDG(); 158 _LEDG = 1; 159 // Make sure LED is visible at LP mode 160 waitMS( 20 ); 161 break; 162 163 // Make one pulse 164 case 2: 165 pulseLEDG(); 166 // Make sure LED is visible at LP mode 167 waitMS( 20 ); 168 break; 169 170 // Start pulsing 171 case 3: 172 pulsingLEDG(); 173 // Make sure LED is visible at LP mode 174 waitMS( 20 ); 175 break; 176 } 177 178 // Return no data 179 _DpaDataLength = 0; 180 // Return TRUE to indicate peripheral was handled 181 goto DpaHandleReturnTRUE; 182 } 183 } 184 } 185 186 return FALSE; 187 } 188 189 //############################################################################################ 190 // 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) 191 #include "DPAcustomHandler.h" 192 //############################################################################################