1 // ********************************************************************************* 2 // Custom DPA Handler code example - User peripheral implementation - SPI slave * 3 // ********************************************************************************* 4 // Copyright (c) MICRORISC s.r.o. 5 // 6 // File: $RCSfile: CustomDpaHandler-UserPeripheral-SPIslave.c,v $ 7 // Version: $Revision: 1.14 $ 8 // Date: $Date: 2022/02/25 09:41:25 $ 9 // 10 // Revision history: 11 // 2022/02/24 Release for DPA 4.17 12 // 2020/09/03 Release for DPA 4.15 13 // 14 // ********************************************************************* 15 16 // Online DPA documentation https://doc.iqrf.org/DpaTechGuide/ 17 18 // This example implements Slave SPI in STD mode as a user peripheral the standard way it was implemented as embedded peripheral in the prior DPA 4.15 19 // The implementation uses the structure TPerUartWriteRead_Request as it was in the past. 20 21 // Default IQRF include (modify the path according to your setup) 22 #include "IQRF.h" 23 24 // Default DPA header (modify the path according to your setup) 25 #include "DPA.h" 26 // Default Custom DPA Handler header (modify the path according to your setup) 27 #include "DPAcustomHandler.h" 28 29 //############################################################################################ 30 31 // Defines some previously defined symbols 32 #define PNUM_SPI 0x08 33 #define CMD_SPI_WRITE_READ 0 34 #define PERIPHERAL_SPI_MAX_DATA_LENGTH sizeof( _DpaMessage.PerUartWriteRead_Request.WrittenData ) 35 36 // Must be the 1st defined function in the source code in order to be placed at the correct FLASH location! 37 //############################################################################################ 38 bit CustomDpaHandler() 39 //############################################################################################ 40 { 41 #pragma updateBank default = UserBank_01 42 43 // Handler presence mark 44 clrwdt(); 45 46 // Detect DPA event to handle 47 switch ( GetDpaEvent() ) 48 { 49 // ------------------------------------------------- 50 case DpaEvent_Interrupt: 51 // Do an extra quick background interrupt work 52 // ! 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. 53 // ! 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. 54 // ! 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. 55 // ! Only global variables or local ones marked by static keyword can be used to allow reentrancy. 56 // ! Make sure race condition does not occur when accessing those variables at other places. 57 // ! 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. 58 // ! Do not call any OS functions except setINDFx(). 59 // ! Do not use any OS variables especially for writing access. 60 // ! 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. 61 return Carry; 62 63 // ------------------------------------------------- 64 case DpaEvent_Init: 65 // Do a one time initialization before main loop starts 66 case DpaEvent_AfterSleep: 67 // Called after woken up after sleep 68 69 enableSPI(); 70 break; 71 72 // ------------------------------------------------- 73 case DpaEvent_BeforeSleep: 74 // Called before going to sleep 75 case DpaEvent_DisableInterrupts: 76 // Called when device needs all hardware interrupts to be disabled (before Reset, Restart, LoadCode, Remove bond and run RFPGM) 77 78 disableSPI(); 79 break; 80 81 // ------------------------------------------------- 82 case DpaEvent_DpaRequest: 83 // Called to interpret DPA request for peripherals 84 // ------------------------------------------------- 85 // Peripheral enumeration 86 if ( IsDpaEnumPeripheralsRequest() ) 87 { 88 // We re-implement 1 embedded SPI peripheral even when it is not enabled in the configuration 89 _DpaMessage.EnumPeripheralsAnswer.EmbeddedPers[PNUM_SPI / 8] |= 1 << ( PNUM_SPI % 8 ); 90 91 _DpaMessage.EnumPeripheralsAnswer.HWPID |= 0x571F; 92 93 DpaHandleReturnTRUE: 94 return TRUE; 95 } 96 // ------------------------------------------------- 97 // Get information about peripheral 98 else if ( IsDpaPeripheralInfoRequest() ) 99 { 100 if ( _PNUM == PNUM_SPI ) 101 { 102 _DpaMessage.PeripheralInfoAnswer.PerT = PERIPHERAL_TYPE_SPI; 103 _DpaMessage.PeripheralInfoAnswer.PerTE = PERIPHERAL_TYPE_EXTENDED_READ_WRITE; 104 _DpaMessage.PeripheralInfoAnswer.Par1 = sizeof( _DpaMessage.PerUartWriteRead_Request.WrittenData ); 105 goto DpaHandleReturnTRUE; 106 } 107 108 break; 109 } 110 // ------------------------------------------------- 111 else 112 { 113 // Handle peripheral command 114 if ( _PNUM == PNUM_SPI ) 115 { 116 // Check DPA command value 117 if ( _PCMD != CMD_SPI_WRITE_READ ) 118 { 119 W = ERROR_PCMD; 120 121 _DpaApiReturnPeripheralErrorW: 122 DpaApiReturnPeripheralError( W ); 123 } 124 125 // Check data length 126 if ( _DpaDataLength < sizeof( _DpaMessage.PerUartWriteRead_Request.ReadTimeout ) ) 127 { 128 W = ERROR_DATA_LEN; 129 goto _DpaApiReturnPeripheralErrorW; 130 } 131 132 // Any data to write to SPI? 133 if ( --_DpaDataLength != 0 ) 134 { 135 // SPI write 136 stopSPI(); 137 setFSR01( _FSR_RF, _FSR_COM ); 138 FSR0 += offsetof( TPerUartWriteRead_Request, WrittenData ); 139 copyMemoryBlock( FSR0, FSR1, _DpaDataLength ); 140 startSPI( _DpaDataLength ); 141 } 142 143 // No data read yet 144 _DpaDataLength = 0; 145 // Any data to read? 146 if ( _DpaMessage.PerUartWriteRead_Request.ReadTimeout != 0xff ) 147 { 148 // Wait specified time before reading 149 while ( --_DpaMessage.PerUartWriteRead_Request.ReadTimeout != (uns8)-1 ) 150 waitMS( 10 ); 151 152 // SPI read 153 if ( !getStatusSPI() && _SPIRX ) 154 { 155 if ( _SPICRCok ) 156 { 157 setFSR01( _FSR_COM, _FSR_RF ); 158 // Limit maximum data length 159 _DpaDataLength = SPIpacketLength; 160 if ( _DpaDataLength > PERIPHERAL_SPI_MAX_DATA_LENGTH ) 161 _DpaDataLength = PERIPHERAL_SPI_MAX_DATA_LENGTH; 162 163 copyMemoryBlock( FSR0, FSR1, _DpaDataLength ); 164 } 165 166 startSPI( 0 ); 167 } 168 } 169 170 goto DpaHandleReturnTRUE; 171 } 172 } 173 } 174 175 return FALSE; 176 } 177 178 //############################################################################################ 179 // Default Custom DPA Handler header; 2nd include implementing a Code bumper to detect too long code of the Custom DPA Handler (modify the path according to your setup) 180 #include "DPAcustomHandler.h" 181 //############################################################################################