1 // **************************************************************************** 2 // Custom DPA Handler code example - Sending asynchronous request from Node * 3 // **************************************************************************** 4 // Copyright (c) MICRORISC s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-AsyncRequest.c,v $ 7 // Version: $Revision: 1.35 $ 8 // Date: $Date: 2021/04/26 15:13:50 $ 9 // 10 // Revision history: 11 // 2020/01/09 Release for DPA 4.12 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 (actually not needed as we do not used EEPROMs) 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 // This example shows how to send asynchronous request from Node to the Coordinator or its master device. 33 // ! Please do not forget to set an API variable AsyncReqAtCoordinator at Coordinator to allow an asynchronous request execution there. 34 35 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 36 //############################################################################################ 37 bit CustomDpaHandler() 38 //############################################################################################ 39 { 40 // Handler presence mark 41 clrwdt(); 42 43 // Packet ID 44 static uns8 pid; 45 46 // Detect DPA event to handle 47 switch ( GetDpaEvent() ) 48 { 49 #ifdef DpaEvent_Interrupt 50 // ------------------------------------------------- 51 case DpaEvent_Interrupt: 52 // Do an extra quick background interrupt work 53 // ! 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. 54 // ! 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. 55 // ! 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. 56 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 57 // ! Make sure race condition does not occur when accessing those variables at other places. 58 // ! 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. 59 // ! Do not call any OS functions except setINDFx(). 60 // ! Do not use any OS variables especially for writing access. 61 // ! 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. 62 63 return TRUE; 64 #endif 65 66 // ------------------------------------------------- 67 case DpaEvent_Idle: 68 { 69 // Do a background work when RF packet is not received 70 uns16 btnCnt = 1000 + 1; 71 while ( buttonPressed && --btnCnt != 0 ) 72 waitMS( 1 ); 73 74 // Button was pressed for at least the 100 ms 75 if ( btnCnt < 1000 - 100 ) 76 { 77 // Packet ID 78 PID = ++pid; 79 // Number of hops = my VRN 80 RTHOPS = ntwVRN; 81 // Fake the last sender in case there was no communication at all from the network before 82 bank0 uns8 lastTX @ usedBank0[0x13]; 83 lastTX = ntwVRN; 84 85 // No DPA Params used 86 _DpaParams = 0; 87 88 // Data is sent to the Coordinator 89 if ( btnCnt == 0 ) 90 { 91 // To the coordinator master if button pressed > 0.5s 92 _NADR = COORDINATOR_ADDRESS; 93 pulseLEDR(); 94 } 95 else 96 { 97 // Otherwise to coordinator the local device 98 _NADR = LOCAL_ADDRESS; 99 pulseLEDG(); 100 } 101 _NADRhigh = 0; 102 103 // Use IO peripheral to work with LEDs even if LED peripherals are not present 104 _PNUM = PNUM_IO; 105 // Make a LEDR pulse 106 _PCMD = CMD_IO_SET; 107 // Any HWPID 108 _HWPID = HWPID_DoNotCheck; 109 110 // LEDR=1 => Set PORTA.2 to 1 111 _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Port = PNUM_IO_PORTA; // PORTA 112 _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Mask = 0b0000.0100; // bit 2 113 _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0].Value = 0b0000.0100; // bit 2 = 1 114 115 // 64 ms pulse 116 _DpaMessage.PerIoDirectionAndSet_Request.Delays[1].Header = PNUM_IO_DELAY; // delay 117 _DpaMessage.PerIoDirectionAndSet_Request.Delays[1].Delay = 64; // 64 118 119 // LEDR=0 => Set PORTA.2 to 0 120 _DpaMessage.PerIoDirectionAndSet_Request.Triplets[2].Port = PNUM_IO_PORTA; // PORTA 121 _DpaMessage.PerIoDirectionAndSet_Request.Triplets[2].Mask = 0b0000.0100; // bit 2 122 _DpaMessage.PerIoDirectionAndSet_Request.Triplets[2].Value = 0b0000.0000; // bit 2 = 0 123 124 // Setup the correct length of data 125 _DpaDataLength = 3 * sizeof( _DpaMessage.PerIoDirectionAndSet_Request.Triplets[0] ); 126 127 // Transmit DPA message with DPA Value equal the lastRSSI (can be any other value) 128 DpaApiRfTxDpaPacket( lastRSSI, 0x7F ); 129 130 // Wait for the button to be released (could be done in cleaner way w/o active waiting at this point of code ...) 131 while ( buttonPressed ) 132 clrwdt(); 133 } 134 } 135 break; 136 } 137 138 DpaHandleReturnFALSE: 139 return FALSE; 140 } 141 //############################################################################################ 142 // 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) 143 #include "DPAcustomHandler.h" 144 //############################################################################################