Mercurial > public > ostc4
comparison Small_CPU/Src/uart.c @ 922:7c996354b8ac Evo_2_23
Moved UART6 into a separate unit:
UART6 connects internal devices. As a first step the existing code sections have been moved into a new unit. As well the code of the external GNSS sensor has been copied into this unit as starting point for the further development. Later the internal part can be integrated into the common uart (code cleanup).
author | Ideenmodellierer |
---|---|
date | Sun, 03 Nov 2024 20:53:05 +0100 |
parents | eb4109d7d1e9 |
children | effadaa3a1f7 |
comparison
equal
deleted
inserted
replaced
921:eb4109d7d1e9 | 922:7c996354b8ac |
---|---|
26 #include "uartProtocol_GNSS.h" | 26 #include "uartProtocol_GNSS.h" |
27 #include "externalInterface.h" | 27 #include "externalInterface.h" |
28 #include "data_exchange.h" | 28 #include "data_exchange.h" |
29 #include <string.h> /* memset */ | 29 #include <string.h> /* memset */ |
30 | 30 |
31 #ifdef ENABLE_GPIO_V2 | |
32 extern UART_HandleTypeDef huart6; | |
33 extern void UART6_RxCpltCallback(UART_HandleTypeDef *huart); | |
34 extern void UART6_TxCpltCallback(UART_HandleTypeDef *huart); | |
35 #endif | |
31 | 36 |
32 /* Private variables ---------------------------------------------------------*/ | 37 /* Private variables ---------------------------------------------------------*/ |
33 | 38 |
34 | 39 |
35 #define TX_BUF_SIZE (40u) /* max length for commands */ | 40 #define TX_BUF_SIZE (40u) /* max length for commands */ |
36 #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */ | 41 #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */ |
37 #define CHUNKS_PER_BUFFER (6u) | 42 #define CHUNKS_PER_BUFFER (6u) |
38 | 43 |
39 | 44 |
40 | 45 |
41 DMA_HandleTypeDef hdma_usart1_rx, hdma_usart1_tx, hdma_usart6_rx, hdma_usart6_tx; | 46 DMA_HandleTypeDef hdma_usart1_rx, hdma_usart1_tx; |
42 | 47 |
43 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */ | 48 uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */ |
44 uint8_t txBuffer[CHUNK_SIZE]; /* tx uses less bytes */ | 49 uint8_t txBuffer[CHUNK_SIZE]; /* tx uses less bytes */ |
45 uint8_t txBufferQue[TX_BUF_SIZE]; /* In MUX mode command may be send shortly after each other => allow q 1 entry que */ | 50 uint8_t txBufferQue[TX_BUF_SIZE]; /* In MUX mode command may be send shortly after each other => allow q 1 entry que */ |
46 uint8_t txBufferQueLen; | 51 uint8_t txBufferQueLen; |
47 | |
48 uint8_t rxBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */ | |
49 uint8_t txBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */ | |
50 | 52 |
51 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */ | 53 static uint8_t rxWriteIndex; /* Index of the data item which is analysed */ |
52 static uint8_t rxReadIndex; /* Index at which new data is stared */ | 54 static uint8_t rxReadIndex; /* Index at which new data is stared */ |
53 static uint8_t lastCmdIndex; /* Index of last command which has not been completely received */ | 55 static uint8_t lastCmdIndex; /* Index of last command which has not been completely received */ |
54 static uint8_t dmaRxActive; /* Indicator if DMA reception needs to be started */ | 56 static uint8_t dmaRxActive; /* Indicator if DMA reception needs to be started */ |
152 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn); | 154 HAL_NVIC_EnableIRQ(DMA2_Stream5_IRQn); |
153 HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 2, 1); | 155 HAL_NVIC_SetPriority(DMA2_Stream7_IRQn, 2, 1); |
154 HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn); | 156 HAL_NVIC_EnableIRQ(DMA2_Stream7_IRQn); |
155 } | 157 } |
156 | 158 |
157 | |
158 void GNSS_IO_init() { | |
159 | |
160 GPIO_InitTypeDef GPIO_InitStruct = { 0 }; | |
161 /* Peripheral clock enable */ | |
162 __HAL_RCC_USART6_CLK_ENABLE() | |
163 ; | |
164 | |
165 __HAL_RCC_GPIOA_CLK_ENABLE() | |
166 ; | |
167 /**USART6 GPIO Configuration | |
168 PA11 ------> USART6_TX | |
169 PA12 ------> USART6_RX | |
170 */ | |
171 GPIO_InitStruct.Pin = GPIO_PIN_11 | GPIO_PIN_12; | |
172 GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; | |
173 GPIO_InitStruct.Pull = GPIO_NOPULL; | |
174 GPIO_InitStruct.Speed = GPIO_SPEED_FAST; | |
175 GPIO_InitStruct.Alternate = GPIO_AF8_USART6; | |
176 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); | |
177 | |
178 /* USART6 DMA Init */ | |
179 /* USART6_RX Init */ | |
180 hdma_usart6_rx.Instance = DMA2_Stream1; | |
181 hdma_usart6_rx.Init.Channel = DMA_CHANNEL_5; | |
182 hdma_usart6_rx.Init.Direction = DMA_PERIPH_TO_MEMORY; | |
183 hdma_usart6_rx.Init.PeriphInc = DMA_PINC_DISABLE; | |
184 hdma_usart6_rx.Init.MemInc = DMA_MINC_ENABLE; | |
185 hdma_usart6_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE; | |
186 hdma_usart6_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; | |
187 hdma_usart6_rx.Init.Mode = DMA_NORMAL; | |
188 hdma_usart6_rx.Init.Priority = DMA_PRIORITY_LOW; | |
189 hdma_usart6_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; | |
190 HAL_DMA_Init(&hdma_usart6_rx); | |
191 | |
192 __HAL_LINKDMA(&huart6, hdmarx, hdma_usart6_rx); | |
193 | |
194 /* USART6_TX Init */ | |
195 hdma_usart6_tx.Instance = DMA2_Stream6; | |
196 hdma_usart6_tx.Init.Channel = DMA_CHANNEL_6; | |
197 hdma_usart6_tx.Init.Direction = DMA_MEMORY_TO_PERIPH; | |
198 hdma_usart6_tx.Init.PeriphInc = DMA_PINC_DISABLE; | |
199 hdma_usart6_tx.Init.MemInc = DMA_MINC_ENABLE; | |
200 hdma_usart6_tx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE; | |
201 hdma_usart6_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE; | |
202 hdma_usart6_tx.Init.Mode = DMA_NORMAL; | |
203 hdma_usart6_tx.Init.Priority = DMA_PRIORITY_LOW; | |
204 hdma_usart6_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE; | |
205 HAL_DMA_Init(&hdma_usart6_tx); | |
206 | |
207 __HAL_LINKDMA(&huart6, hdmatx, hdma_usart6_tx); | |
208 | |
209 /* USART6 interrupt Init */ | |
210 HAL_NVIC_SetPriority(USART6_IRQn, 0, 0); | |
211 HAL_NVIC_EnableIRQ(USART6_IRQn); | |
212 | |
213 MX_USART6_DMA_Init(); | |
214 | |
215 } | |
216 | |
217 void MX_USART6_DMA_Init() { | |
218 /* DMA controller clock enable */ | |
219 __HAL_RCC_DMA2_CLK_ENABLE(); | |
220 | |
221 /* DMA interrupt init */ | |
222 /* DMA2_Stream2_IRQn interrupt configuration */ | |
223 HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0); | |
224 HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn); | |
225 /* DMA2_Stream6_IRQn interrupt configuration */ | |
226 HAL_NVIC_SetPriority(DMA2_Stream6_IRQn, 0, 0); | |
227 HAL_NVIC_EnableIRQ(DMA2_Stream6_IRQn); | |
228 } | |
229 | |
230 | |
231 void MX_USART6_UART_DeInit(void) | |
232 { | |
233 HAL_DMA_Abort(&hdma_usart6_rx); | |
234 HAL_DMA_DeInit(&hdma_usart6_rx); | |
235 HAL_DMA_Abort(&hdma_usart6_tx); | |
236 HAL_DMA_DeInit(&hdma_usart6_tx); | |
237 HAL_UART_DeInit(&huart6); | |
238 HAL_UART_DeInit(&huart6); | |
239 } | |
240 | |
241 void MX_USART6_UART_Init(void) { | |
242 huart6.Instance = USART6; | |
243 huart6.Init.BaudRate = 9600; | |
244 huart6.Init.WordLength = UART_WORDLENGTH_8B; | |
245 huart6.Init.StopBits = UART_STOPBITS_1; | |
246 huart6.Init.Parity = UART_PARITY_NONE; | |
247 huart6.Init.Mode = UART_MODE_TX_RX; | |
248 huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE; | |
249 huart6.Init.OverSampling = UART_OVERSAMPLING_16; | |
250 HAL_UART_Init(&huart6); | |
251 } | |
252 | |
253 void UART_MUX_SelectAddress(uint8_t muxAddress) | 159 void UART_MUX_SelectAddress(uint8_t muxAddress) |
254 { | 160 { |
255 uint8_t indexstr[4]; | 161 uint8_t indexstr[4]; |
256 | 162 |
257 if(muxAddress <= MAX_MUX_CHANNEL) | 163 if(muxAddress <= MAX_MUX_CHANNEL) |
307 memcpy(txBufferQue, cmdString, cmdLength); | 213 memcpy(txBufferQue, cmdString, cmdLength); |
308 txBufferQueLen = cmdLength; | 214 txBufferQueLen = cmdLength; |
309 } | 215 } |
310 } | 216 } |
311 | 217 |
312 void UART_SendCmdUbx(uint8_t *cmd, uint8_t len) | 218 void UART_SendCmdUbx(const uint8_t *cmd, uint8_t len) |
313 { | 219 { |
314 if(len < TX_BUF_SIZE) /* A longer string is an indication for a missing 0 termination */ | 220 if(len < TX_BUF_SIZE) /* A longer string is an indication for a missing 0 termination */ |
315 { | 221 { |
316 if(dmaRxActive == 0) | 222 if(dmaRxActive == 0) |
317 { | 223 { |
393 { | 299 { |
394 rxWriteIndex = 0; | 300 rxWriteIndex = 0; |
395 } | 301 } |
396 UART_StartDMA_Receiption(); | 302 UART_StartDMA_Receiption(); |
397 } | 303 } |
304 #ifdef ENABLE_GPIO_V2 | |
305 if(huart == &huart6) | |
306 { | |
307 UART6_RxCpltCallback(huart); | |
308 } | |
309 #endif | |
398 } | 310 } |
399 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) | 311 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) |
400 { | 312 { |
401 if(huart == &huart1) | 313 if(huart == &huart1) |
402 { | 314 { |
408 HAL_UART_Transmit_DMA(&huart1,txBuffer,txBufferQueLen); | 320 HAL_UART_Transmit_DMA(&huart1,txBuffer,txBufferQueLen); |
409 dmaTxActive = 1; | 321 dmaTxActive = 1; |
410 txBufferQueLen = 0; | 322 txBufferQueLen = 0; |
411 } | 323 } |
412 } | 324 } |
325 #ifdef ENABLE_GPIO_V2 | |
326 if(huart == &huart6) | |
327 { | |
328 UART6_TxCpltCallback(huart); | |
329 } | |
330 #endif | |
413 } | 331 } |
414 | 332 |
415 uint8_t isEndIndication(uint8_t index) | 333 uint8_t isEndIndication(uint8_t index) |
416 { | 334 { |
417 uint8_t ret = 0; | 335 uint8_t ret = 0; |
543 | 461 |
544 uint8_t UART_isComActive(uint8_t sensorId) | 462 uint8_t UART_isComActive(uint8_t sensorId) |
545 { | 463 { |
546 uint8_t active = 1; | 464 uint8_t active = 1; |
547 | 465 |
548 uint8_t ComState = externalInterface_GetSensorState(sensorId + EXT_INTERFACE_MUX_OFFSET); | |
549 | |
550 if(time_elapsed_ms(LastCmdRequestTick, HAL_GetTick()) > 300) /* UART activity should be inactive 300ms after last command */ | 466 if(time_elapsed_ms(LastCmdRequestTick, HAL_GetTick()) > 300) /* UART activity should be inactive 300ms after last command */ |
551 { | 467 { |
552 active = 0; | 468 active = 0; |
553 } | 469 } |
554 return active; | 470 return active; |