1 // ********************************************************************* 2 // Custom DPA Handler code example - Peripheral memory mapping * 3 // ********************************************************************* 4 // Copyright (c) MICRORISC s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-PeripheralMemoryMapping.c,v $ 7 // Version: $Revision: 1.31 $ 8 // Date: $Date: 2021/06/09 09:39:30 $ 9 // 10 // Revision history: 11 // 2018/10/25 Release for DPA 3.03 12 // 2017/03/13 Release for DPA 3.00 13 // 2015/08/05 Release for DPA 2.20 14 // 2014/10/31 Release for DPA 2.10 15 // 2014/04/30 Release for DPA 2.00 16 // 17 // ********************************************************************* 18 19 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/ 20 21 // Default IQRF include (modify the path according to your setup) 22 #include "IQRF.h" 23 24 // Uncomment to implement Custom DPA Handler for Coordinator 25 //#define COORDINATOR_CUSTOM_HANDLER 26 27 // Default DPA header (modify the path according to your setup) 28 #include "DPA.h" 29 // Default Custom DPA Handler header (modify the path according to your setup) 30 #include "DPAcustomHandler.h" 31 32 // The application demonstrates mapping of the actual MCU peripherals to the RAM peripheral 33 // PeripheralRam[0].0 = LEDR status (read/write) 34 // PeripheralRam[0].1 = LEDG status (read/write) 35 // PeripheralRam[1].0 = LEDR status invert request (write) 36 // PeripheralRam[1].1 = LEDG status invert request (write) 37 // PeripheralRam[2].0 = Button status (read) 38 // PeripheralRam[3].0 = Temperature (read) 39 40 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 41 //############################################################################################ 42 bit CustomDpaHandler() 43 //############################################################################################ 44 { 45 // Peripheral RAM mapping structure 46 struct 47 { 48 // Actual LED state 49 uns8 LEDstate; 50 // Request to invert LED state 51 uns8 LEDinvert; 52 // Button state 53 uns8 Button; 54 // Measured temperature 55 uns8 Temperature; 56 } Mapping @ PeripheralRam; 57 58 // Handler presence mark 59 clrwdt(); 60 61 // Flags that the RAM was written 62 static bit ramWritten; 63 64 // Detect DPA event to handle (unused event handlers can be commented out or even deleted) 65 switch ( GetDpaEvent() ) 66 { 67 #ifdef DpaEvent_Interrupt 68 // ------------------------------------------------- 69 case DpaEvent_Interrupt: 70 // Do an extra quick background interrupt work 71 // ! 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. 72 // ! 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. 73 // ! 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. 74 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 75 // ! Make sure race condition does not occur when accessing those variables at other places. 76 // ! 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. 77 // ! Do not call any OS functions except setINDFx(). 78 // ! Do not use any OS variables especially for writing access. 79 // ! 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. 80 81 return Carry; 82 #endif 83 84 // ------------------------------------------------- 85 case DpaEvent_ReceiveDpaRequest: 86 // Called after DPA request was received 87 88 // RAM peripheral read request 89 // (could be optimized by checking the RAM address to read from and based on its value skipping e.g. temperature measurement) 90 if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_READ ) 91 { 92 // Read button state 93 W = 0; 94 if ( buttonPressed ) 95 W = 1; 96 Mapping.Button = W; 97 // Read temperature 98 Mapping.Temperature = getTemperature(); 99 } 100 101 break; 102 103 // ------------------------------------------------- 104 case DpaEvent_Notification: 105 // Called after DPA request was processed and after DPA response was sent 106 107 // Anything written to the RAM? 108 // (could be optimized by checking the RAM address that was written to and based on its value skipping LEDs update) 109 if ( _PNUM == PNUM_RAM && _PCMD == CMD_RAM_WRITE ) 110 ramWritten = TRUE; 111 112 break; 113 114 // ------------------------------------------------- 115 case DpaEvent_AfterRouting: 116 // Called after Notification and after routing of the DPA response was finished 117 118 // Something was written to the RAM? 119 // Update LEDs at the defined after routing time i.e. at the same time at all nodes when addressed by broadcast 120 if ( ramWritten ) 121 { 122 ramWritten = FALSE; 123 124 // Update LEDs state 125 Mapping.LEDstate ^= Mapping.LEDinvert; 126 Mapping.LEDinvert = 0; 127 128 // Update LEDR 129 if ( Mapping.LEDstate.0 ) 130 setLEDR(); 131 else 132 stopLEDR(); // Must be used when pulseLEDR is used 133 134 // Update LEDG 135 if ( Mapping.LEDstate.1 ) 136 setLEDG(); 137 else 138 stopLEDG(); // Must be used when pulseLEDR is used 139 140 // Make sure LED is visible at LP mode 141 waitMS( 20 ); 142 } 143 144 break; 145 146 // ------------------------------------------------- 147 case DpaEvent_DpaRequest: 148 // Called to interpret DPA request for peripherals 149 // ------------------------------------------------- 150 // Peripheral enumeration 151 if ( IsDpaEnumPeripheralsRequest() ) 152 { 153 _DpaMessage.EnumPeripheralsAnswer.HWPID = 0x000F; 154 _DpaMessage.EnumPeripheralsAnswer.HWPIDver = 0xABCD; 155 156 DpaHandleReturnTRUE: 157 return TRUE; 158 } 159 160 break; 161 } 162 163 return FALSE; 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 //############################################################################################