1 // ********************************************************************* 2 // Custom DPA Handler code example - UART repeater * 3 // ********************************************************************* 4 // Copyright (c) IQRF Tech s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-UARTrepeater.c,v $ 7 // Version: $Revision: 1.15 $ 8 // Date: $Date: 2017/08/09 12:24:47 $ 9 // 10 // Revision history: 11 // 2017/03/13 Release for DPA 3.00 12 // 2016/09/12 Release for DPA 2.28 13 // 2015/08/05 Release for DPA 2.20 14 // 2014/10/31 Release for DPA 2.10 15 // 16 // ********************************************************************* 17 18 // Online DPA documentation http://www.iqrf.org/DpaTechGuide/ 19 20 // Default IQRF include (modify the path according to your setup) 21 #include "IQRF.h" 22 23 // Default DPA header (modify the path according to your setup) 24 #include "DPA.h" 25 // Default Custom DPA Handler header (modify the path according to your setup) 26 #include "DPAcustomHandler.h" 27 28 // This example opens its UART peripheral and periodically TX data previously RX from UART, the data are available at peripheral RAM too 29 // This example works only at STD mode, not at LP mode 30 31 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 32 //############################################################################################ 33 bit CustomDpaHandler() 34 //############################################################################################ 35 { 36 // Handler presence mark 37 clrwdt(); 38 39 // Detect DPA event to handle 40 switch ( GetDpaEvent() ) 41 { 42 // ------------------------------------------------- 43 case DpaEvent_Interrupt: 44 // Do an extra quick background interrupt work 45 // ! 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. 46 // ! 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. 47 // ! 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. 48 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 49 // ! Make sure race condition does not occur when accessing those variables at other places. 50 // ! 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. 51 // ! Do not call any OS functions except setINDFx(). 52 // ! Do not use any OS variables especially for writing access. 53 // ! 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. 54 55 DpaHandleReturnTRUE: 56 return TRUE; 57 58 // ------------------------------------------------- 59 case DpaEvent_Idle: 60 // Do a quick background work when RF packet is not received 61 62 // No data RX from UART yet 63 _DpaDataLength = 0; 64 for ( ;; ) 65 { 66 // TX and RX data to/from UART 67 _PNUM = PNUM_UART; 68 _PCMD = CMD_UART_WRITE_READ; 69 // Wait 0 ms for RX 70 _DpaMessage.PerUartSpiWriteRead_Request.ReadTimeout = 0; 71 // Copy previously RX data to TX 72 copyMemoryBlock( PeripheralRam + offsetof( TPerUartSpiWriteRead_Request, WrittenData ), _DpaMessage.PerUartSpiWriteRead_Request.WrittenData, _DpaDataLength ); 73 // One more byte to the request 74 _DpaDataLength++; 75 // TX/RX from UART 76 DpaApiLocalRequest(); 77 // Error TX/RX UART? 78 if ( _PNUM == PNUM_ERROR_FLAG ) 79 { 80 _LEDR = 1; 81 break; 82 } 83 84 // No data RX? 85 if ( _DpaDataLength == 0 ) 86 break; 87 88 // Indicate RX data 89 pulseLEDG(); 90 // Copy to RAM peripheral RX data length and ... 91 PeripheralRam[0] = _DpaDataLength; 92 // RX data too 93 copyMemoryBlock( _DpaMessage.Response.PData, PeripheralRam + 1, _DpaDataLength ); 94 } 95 break; 96 97 // ------------------------------------------------- 98 case DpaEvent_Init: 99 // Do a one time initialization work before main loop starts 100 101 // Open UART peripheral 102 _PNUM = PNUM_UART; 103 _PCMD = CMD_UART_OPEN; 104 _DpaMessage.PerUartOpen_Request.BaudRate = DpaBaud_115200; 105 _DpaDataLength = sizeof( _DpaMessage.PerUartOpen_Request ); 106 DpaApiLocalRequest(); 107 // Error opening UART? 108 if ( _PNUM == PNUM_ERROR_FLAG ) 109 _LEDR = 1; 110 111 break; 112 } 113 114 return FALSE; 115 } 116 //############################################################################################ 117 // 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) 118 #include "DPAcustomHandler.h" 119 //############################################################################################