Mercurial > public > ostc4
annotate Small_CPU/Src/uart.c @ 856:af035b883382 Evo_2_23
Do not show SP tab if PSCR mode is active:
In the previous version the setpoint tab was shown even if operation mode PSCR was selected. SPs do not matter for PSCR operation and will not be shown in the new version.
author | Ideenmodellierer |
---|---|
date | Sun, 05 May 2024 17:20:56 +0200 |
parents | c3dd461ca3f9 |
children | ad96f99ebc78 |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file uart.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @version V0.0.1 | |
6 * @date 27-March-2014 | |
7 * @brief button control | |
8 * | |
9 @verbatim | |
10 ============================================================================== | |
11 ##### How to use ##### | |
12 ============================================================================== | |
13 @endverbatim | |
14 ****************************************************************************** | |
15 * @attention | |
16 * | |
17 * <h2><center>© COPYRIGHT(c) 2015 heinrichs weikamp</center></h2> | |
18 * | |
19 ****************************************************************************** | |
20 */ | |
21 /* Includes ------------------------------------------------------------------*/ | |
22 #include "uart.h" | |
794 | 23 #include "uartProtocol_O2.h" |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
24 #include "uartProtocol_Co2.h" |
842
c3dd461ca3f9
Migrated Sentinel protocol to new UART structure:
Ideenmodellierer
parents:
809
diff
changeset
|
25 #include "uartProtocol_Sentinel.h" |
662 | 26 #include "externalInterface.h" |
27 #include "data_exchange.h" | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
28 #include <string.h> /* memset */ |
38 | 29 |
30 /* Private variables ---------------------------------------------------------*/ | |
31 | |
794 | 32 |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
33 |
781 | 34 #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */ |
35 #define CHUNKS_PER_BUFFER (5u) | |
794 | 36 |
662 | 37 UART_HandleTypeDef huart1; |
38 | |
39 DMA_HandleTypeDef hdma_usart1_rx; | |
38 | 40 |
662 | 41 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow fariations in buffer read time */ |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
42 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */ |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
43 static uint8_t rxReadIndex; /* Index at which new data is stared */ |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
44 static uint8_t lastCmdIndex; /* Index of last command which has not been completly received */ |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
45 static uint8_t dmaActive; /* Indicator if DMA reception needs to be started */ |
794 | 46 |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
47 |
38 | 48 /* Exported functions --------------------------------------------------------*/ |
49 | |
794 | 50 |
662 | 51 void MX_USART1_UART_Init(void) |
38 | 52 { |
662 | 53 /* regular init */ |
54 | |
55 huart1.Instance = USART1; | |
803
96ffad0a4e57
Cleanup initialisation / deinitialization:
Ideenmodellierer
parents:
798
diff
changeset
|
56 huart1.Init.BaudRate = 19200; |
662 | 57 huart1.Init.WordLength = UART_WORDLENGTH_8B; |
58 huart1.Init.StopBits = UART_STOPBITS_1; | |
59 huart1.Init.Parity = UART_PARITY_NONE; | |
60 huart1.Init.Mode = UART_MODE_TX_RX; | |
61 huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE; | |
62 huart1.Init.OverSampling = UART_OVERSAMPLING_16; | |
63 | |
64 HAL_UART_Init(&huart1); | |
65 | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
66 MX_USART1_DMA_Init(); |
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
67 |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
68 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer)); |
662 | 69 rxReadIndex = 0; |
70 lastCmdIndex = 0; | |
71 rxWriteIndex = 0; | |
72 dmaActive = 0; | |
73 } | |
38 | 74 |
662 | 75 void MX_USART1_UART_DeInit(void) |
76 { | |
704
f1b40364b0af
Added protocol functions for UART DiveO2 sensor:
Ideenmodellierer
parents:
690
diff
changeset
|
77 HAL_DMA_Abort(&hdma_usart1_rx); |
662 | 78 HAL_DMA_DeInit(&hdma_usart1_rx); |
79 HAL_UART_DeInit(&huart1); | |
803
96ffad0a4e57
Cleanup initialisation / deinitialization:
Ideenmodellierer
parents:
798
diff
changeset
|
80 dmaActive = 0; |
662 | 81 } |
82 | |
83 void MX_USART1_DMA_Init() | |
84 { | |
85 /* DMA controller clock enable */ | |
86 __DMA2_CLK_ENABLE(); | |
87 | |
88 /* Peripheral DMA init*/ | |
89 hdma_usart1_rx.Instance = DMA2_Stream5; | |
90 hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4; | |
91 hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; //DMA_MEMORY_TO_PERIPH; | |
92 hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE; | |
93 hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE; | |
94 hdma_usart1_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE; | |
95 hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; | |
96 hdma_usart1_rx.Init.Mode = DMA_NORMAL; | |
97 hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW; | |
98 hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; | |
99 HAL_DMA_Init(&hdma_usart1_rx); | |
100 | |
101 __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx); | |
102 | |
103 /* DMA interrupt init */ | |
104 HAL_NVIC_SetPriority(DMA2_Stream5_IRQn, 0, 0); | |
105 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn); | |
38 | 106 } |
107 | |
794 | 108 void UART_MUX_SelectAddress(uint8_t muxAddress) |
779
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
109 { |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
110 uint8_t indexstr[4]; |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
111 |
794 | 112 if(muxAddress <= MAX_MUX_CHANNEL) |
779
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
113 { |
794 | 114 indexstr[0] = '~'; |
115 indexstr[1] = muxAddress; | |
116 indexstr[2] = 0x0D; | |
117 indexstr[3] = 0x0A; | |
118 | |
119 HAL_UART_Transmit(&huart1,indexstr,4,10); | |
779
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
120 } |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
121 } |
0b5f45448eb6
Added UART multiplexer support for DiveO2:
Ideenmodellierer
parents:
747
diff
changeset
|
122 |
794 | 123 |
124 void UART_SendCmdString(uint8_t *cmdString) | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
125 { |
794 | 126 uint8_t cmdLength = strlen((char*)cmdString); |
127 | |
128 if(cmdLength < 20) /* A longer string is an indication for a missing 0 termination */ | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
129 { |
794 | 130 if(dmaActive == 0) |
131 { | |
132 UART_StartDMA_Receiption(); | |
133 } | |
134 HAL_UART_Transmit(&huart1,cmdString,cmdLength,10); | |
135 } | |
136 } | |
137 | |
729
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
138 |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
139 void StringToInt(char *pstr, uint32_t *puInt32) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
140 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
141 uint8_t index = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
142 uint32_t result = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
143 while((pstr[index] >= '0') && (pstr[index] <= '9')) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
144 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
145 result *=10; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
146 result += pstr[index] - '0'; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
147 index++; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
148 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
149 *puInt32 = result; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
150 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
151 |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
152 void StringToUInt64(char *pstr, uint64_t *puint64) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
153 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
154 uint8_t index = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
155 uint64_t result = 0; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
156 while((pstr[index] >= '0') && (pstr[index] <= '9')) |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
157 { |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
158 result *=10; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
159 result += pstr[index] - '0'; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
160 index++; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
161 } |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
162 *puint64 = result; |
d646a0f724a7
Added auto detection functionality for sensors connected to the external interface:
Ideenmodellierer
parents:
725
diff
changeset
|
163 } |
662 | 164 |
742 | 165 void UART_StartDMA_Receiption() |
166 { | |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
167 if(dmaActive == 0) |
742 | 168 { |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
169 if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE)) |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
170 { |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
171 dmaActive = 1; |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
172 } |
742 | 173 } |
174 } | |
690 | 175 |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
176 void UART_ChangeBaudrate(uint32_t newBaudrate) |
38 | 177 { |
809 | 178 uint8_t dmaWasActive = dmaActive; |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
179 // HAL_DMA_Abort(&hdma_usart1_rx); |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
180 MX_USART1_UART_DeInit(); |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
181 //HAL_UART_Abort(&huart1); |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
182 //HAL_DMA_DeInit(&hdma_usart1_rx); |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
183 |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
184 |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
185 // huart1.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq()/2, newBaudrate); |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
186 huart1.Init.BaudRate = newBaudrate; |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
187 HAL_UART_Init(&huart1); |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
188 MX_USART1_DMA_Init(); |
809 | 189 if(dmaWasActive) |
794 | 190 { |
809 | 191 memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer)); |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
192 rxReadIndex = 0; |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
193 rxWriteIndex = 0; |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
194 dmaActive = 0; |
742 | 195 UART_StartDMA_Receiption(); |
662 | 196 } |
38 | 197 } |
690 | 198 |
662 | 199 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) |
38 | 200 { |
662 | 201 if(huart == &huart1) |
202 { | |
203 dmaActive = 0; | |
204 rxWriteIndex+=CHUNK_SIZE; | |
205 if(rxWriteIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) | |
206 { | |
207 rxWriteIndex = 0; | |
208 } | |
787
aeb72882f30a
Dev Bugfx Empty buffer indication and stability improvments:
Ideenmodellierer
parents:
785
diff
changeset
|
209 if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE) || (rxWriteIndex == rxReadIndex)) /* start next transfer if we did not catch up with read index */ |
662 | 210 { |
809 | 211 UART_StartDMA_Receiption(); |
662 | 212 } |
213 } | |
38 | 214 } |
215 | |
794 | 216 void UART_ReadData(uint8_t sensorType) |
217 { | |
218 uint8_t localRX = rxReadIndex; | |
38 | 219 |
794 | 220 while((rxBuffer[localRX]!=BUFFER_NODATA)) |
221 { | |
222 switch (sensorType) | |
223 { | |
224 case SENSOR_MUX: | |
225 case SENSOR_DIGO2: uartO2_ProcessData(rxBuffer[localRX]); | |
226 break; | |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
227 #ifdef ENABLE_CO2_SUPPORT |
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
228 case SENSOR_CO2: uartCo2_ProcessData(rxBuffer[localRX]); |
794 | 229 break; |
798
e9eba334b942
Migrated CO2 protocol implementation to new format:
Ideenmodellierer
parents:
794
diff
changeset
|
230 #endif |
842
c3dd461ca3f9
Migrated Sentinel protocol to new UART structure:
Ideenmodellierer
parents:
809
diff
changeset
|
231 #ifdef ENABLE_SENTINEL_MODE |
c3dd461ca3f9
Migrated Sentinel protocol to new UART structure:
Ideenmodellierer
parents:
809
diff
changeset
|
232 case SENSOR_SENTINEL: uartSentinel_ProcessData(rxBuffer[localRX]); |
c3dd461ca3f9
Migrated Sentinel protocol to new UART structure:
Ideenmodellierer
parents:
809
diff
changeset
|
233 break; |
c3dd461ca3f9
Migrated Sentinel protocol to new UART structure:
Ideenmodellierer
parents:
809
diff
changeset
|
234 #endif |
794 | 235 default: |
236 break; | |
237 } | |
238 | |
239 rxBuffer[localRX] = BUFFER_NODATA; | |
240 localRX++; | |
241 rxReadIndex++; | |
242 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) | |
243 { | |
244 localRX = 0; | |
245 rxReadIndex = 0; | |
246 } | |
247 } | |
248 } | |
249 | |
250 void UART_FlushRxBuffer(void) | |
251 { | |
252 while(rxBuffer[rxReadIndex] != BUFFER_NODATA) | |
253 { | |
254 rxBuffer[rxReadIndex] = BUFFER_NODATA; | |
255 rxReadIndex++; | |
256 if(rxReadIndex >= CHUNK_SIZE * CHUNKS_PER_BUFFER) | |
257 { | |
258 rxReadIndex = 0; | |
259 } | |
260 } | |
261 } | |
662 | 262 |
809 | 263 |
264 | |
38 | 265 /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/ |