1 // ********************************************************************* 2 // Custom DPA Handler code - Handling buttons * 3 // ********************************************************************* 4 // Copyright (c) MICRORISC s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-Buttons.c,v $ 7 // Version: $Revision: 1.8 $ 8 // Date: $Date: 2021/04/26 15:13:50 $ 9 // 10 // Revision history: 11 // 2019/01/10 Release for DPA 4.00 12 // 13 // ********************************************************************* 14 15 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/ 16 17 // Default IQRF include (modify the path according to your setup) 18 #include "IQRF.h" 19 20 // Default DPA header (modify the path according to your setup) 21 #include "DPA.h" 22 // Default Custom DPA Handler header (modify the path according to your setup) 23 #include "DPAcustomHandler.h" 24 25 //############################################################################################ 26 27 // Button descriptor 28 typedef struct 29 { 30 // Timer to measure debouncing, recommended to be the 1st field to save the code 31 uns8 Timer; 32 // Last button state [must be initialized to 0xFF if the initial state of the button is not sure to be HIGH] 33 uns8 LastState; 34 // Button's PIC port: 0 = PORTA, 1 = PORTB, ... [must be initialized] 35 uns8 Port; 36 // Button's bit mask (only one bit must be set) [must be initialized] 37 uns8 ButtonMask; 38 // Debounce interval measured in the timer ticks + 1 [must be initialized] 39 uns8 DebounceDelay; 40 // 0 if button is HIGH, 1 if button is LOW 41 uns8 ButtonIsLow; 42 } TButton; 43 44 // Handles the button and sets ButtonIsLow appropriately 45 void HandleButton( uns16 pButton @ FSR0 ); 46 47 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 48 //############################################################################################ 49 bit CustomDpaHandler() 50 //############################################################################################ 51 { 52 // Handler presence mark 53 clrwdt(); 54 55 // We demonstrate 2 buttons by 1 physical button but with different debouncing value 56 static TButton ButtonFast, ButtonSlow; 57 58 // Detect DPA event to handle (unused event handlers can be commented out or even deleted) 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 // If TMR6 interrupt occurred 75 if ( TMR6IF ) 76 { 77 // Unmask interrupt 78 TMR6IF = FALSE; 79 80 // Handle buttons 81 HandleButton( &ButtonFast ); 82 HandleButton( &ButtonSlow ); 83 84 // Demonstration of the fast button 85 if ( ButtonFast.ButtonIsLow.0 ) 86 setLEDR(); 87 else 88 stopLEDR(); 89 90 // Demonstration of the slow button 91 if ( ButtonSlow.ButtonIsLow.0 ) 92 setLEDG(); 93 else 94 stopLEDG(); 95 } 96 97 return Carry; 98 99 // ------------------------------------------------- 100 case DpaEvent_Init: 101 // Do a one time initialization before main loop starts 102 103 // Initialize buttons' descriptors (CC5X does not support initialization of the static variables) 104 105 // Fast button @ IQRF standard button 106 ButtonFast.LastState = 0xFF; 107 ButtonFast.Port = 1; // PORTB 108 ButtonFast.ButtonMask = 0b0001.0000; // PORTB.4 109 ButtonFast.DebounceDelay = 2 + 1 /* x64ms */; 110 111 // Slow button @ IQRF standard button 112 ButtonSlow.LastState = 0xFF; 113 ButtonSlow.Port = 1; // PORTB 114 ButtonSlow.ButtonMask = 0b0001.0000; // PORTB.4 115 ButtonSlow.DebounceDelay = 15 + 1 /* x64ms */; 116 117 // Setup TMR6 for 64 ms interval 118 PR6 = 250 - 1; 119 T6CON = 0b0.1111.1.11; 120 TMR6IE = TRUE; 121 break; 122 123 // ------------------------------------------------- 124 case DpaEvent_AfterSleep: 125 // Called on wake-up from sleep 126 TMR6IE = TRUE; 127 TMR6ON = TRUE; 128 break; 129 130 // ------------------------------------------------- 131 case DpaEvent_BeforeSleep: 132 // Called before going to sleep (the same handling as DpaEvent_DisableInterrupts event) 133 // ------------------------------------------------- 134 case DpaEvent_DisableInterrupts: 135 // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond, and Run RFPGM) 136 // Must not use TMR6 any more 137 TMR6ON = FALSE; 138 TMR6IE = FALSE; 139 break; 140 } 141 142 return FALSE; 143 } 144 145 //############################################################################################ 146 void HandleButton( uns16 pButton @ FSR0 ) 147 //############################################################################################ 148 { 149 // FSR0 points to the button's descriptor 150 // (Local variables must be static as they are used inside interrupt routine) 151 152 // Timer is active (non zero)? 153 W = FSR0[offsetof( TButton, Timer )]; 154 if ( W != 0 ) // Note: must not modify W 155 { 156 // Yes, decrement the timer 157 FSR0 += offsetof( TButton, Timer ); // Note: Timer is the 1st field of TButton so this generates no code and must not modify W 158 setINDF0( W - 1 ); 159 FSR0 += -(int8)offsetof( TButton, Timer ); // Note: Timer is the 1st field of TButton so this generates no code, casting fixes CC5X bug 160 } 161 162 // FSR1 will point to the button's port 163 FSR1L = FSR0[offsetof( TButton, Port )] + ( &PORTA & 0xFF ); 164 FSR1H = &PORTA >> 8; 165 166 // Current state of the button, only the button bit is used, other bits are masked to zero 167 static uns8 state; 168 state = *FSR1 & FSR0[offsetof( TButton, ButtonMask )]; 169 170 // Button state changed? 171 if ( ( state ^ FSR0[offsetof( TButton, LastState )] ) != 0 ) 172 { 173 FSR0 += offsetof( TButton, Timer ); // Note: Timer is the 1st field of TButton so this generates no code and must not modify W 174 // Initialize timer for debouncing 175 setINDF0( FSR0[offsetof( TButton, DebounceDelay ) - offsetof( TButton, Timer )] ); 176 // Adjust pointer to LastState field 177 FSR0 += (int8)( offsetof( TButton, LastState ) - offsetof( TButton, Timer ) ); // Note: casting fixes CC5X bug 178 // Store the current state as the last one 179 setINDF0( state ); 180 return; // Note: saves Flash by direct GOTO setINDF0 181 } 182 183 // Timer is almost over? 184 if ( FSR0[offsetof( TButton, Timer )] != 1 ) 185 // No 186 return; 187 188 // The button state was stable for the debounce interval, prepare the pointer to store the new state 189 FSR0 += offsetof( TButton, ButtonIsLow ); 190 // Ready for new state "button is HIGH" 191 W = FALSE; 192 // Is button LOW? 193 if ( state == 0 ) // Note: must not modify W 194 // Ready for new state "button is LOW" 195 W = TRUE; 196 197 // Set the button external state 198 setINDF0( W ); 199 } 200 201 //############################################################################################ 202 // 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) 203 #include "DPAcustomHandler.h" 204 //############################################################################################ 205