comparison Discovery/Src/ostc.c @ 1037:2af07aa38531 GasConsumption

Merge with external development branches: Some features have been prepared for integration: Profiles, DMA UART on Firmware part, Bluetooth discovery and messges logging for development phase. All these new function are deactivated by compile switch and may be activated using the configuration.h for testing purpose.
author Ideenmodellierer
date Mon, 15 Sep 2025 21:12:44 +0200
parents 5865f0aeb438
children 1d7c7a36df15
comparison
equal deleted inserted replaced
1029:e938901f6386 1037:2af07aa38531
25 /// You should have received a copy of the GNU General Public License 25 /// You should have received a copy of the GNU General Public License
26 /// along with this program. If not, see <http://www.gnu.org/licenses/>. 26 /// along with this program. If not, see <http://www.gnu.org/licenses/>.
27 ////////////////////////////////////////////////////////////////////////////// 27 //////////////////////////////////////////////////////////////////////////////
28 28
29 /* Includes ------------------------------------------------------------------*/ 29 /* Includes ------------------------------------------------------------------*/
30 #include "configuration.h"
30 #include "ostc.h" 31 #include "ostc.h"
31 #include "stm32f4xx_hal.h" 32 #include "stm32f4xx_hal.h"
33 #include "cv_heartbeat.h"
32 34
33 #ifndef BOOTLOADER_STANDALONE 35 #ifndef BOOTLOADER_STANDALONE
34 #include "tCCR.h" 36 #include "tCCR.h"
35 #endif 37 #endif
36 38
43 #ifdef USART_PIEZO 45 #ifdef USART_PIEZO
44 UART_HandleTypeDef UartPiezoTxHandle; 46 UART_HandleTypeDef UartPiezoTxHandle;
45 #endif 47 #endif
46 UART_HandleTypeDef UartIR_HUD_Handle; 48 UART_HandleTypeDef UartIR_HUD_Handle;
47 49
50 #ifdef ENABLE_USART_RADIO
51 UART_HandleTypeDef UartRadio_Handle;
52 #endif
53
48 __IO ITStatus UartReady = RESET; 54 __IO ITStatus UartReady = RESET;
49 __IO ITStatus UartReadyHUD = RESET; 55 __IO ITStatus UartReadyHUD = RESET;
50 56
51 /* Private types -------------------------------------------------------------*/ 57 /* Private types -------------------------------------------------------------*/
52 58
53 /* Private variables ---------------------------------------------------------*/ 59 /* Private variables ---------------------------------------------------------*/
54 60
55 /* Private variables with external access via get_xxx() function -------------*/ 61 /* Private variables with external access via get_xxx() function -------------*/
56 static uint8_t hardwareDisplay = 0; //< either OSTC4 LCD (=0) or new Screen (=1) 62 static uint8_t hardwareDisplay = 0; //< either OSTC4 LCD (=0) or new Screen (=1)
63
64 #ifdef ENABLE_PULSE_SENSOR_BT
65 static DMA_HandleTypeDef hdma_uart_BT_rx;
66 #endif
67
68 #ifdef ENABLE_USART_RADIO
69 static DMA_HandleTypeDef hdma_uart_radio_rx;
70 #endif
71
72 static uint16_t rxBufRead = 0;
73 static uint16_t rxBufWrite = 0;
74 static uint8_t rxBufferUart[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */
75
57 /* Private function prototypes -----------------------------------------------*/ 76 /* Private function prototypes -----------------------------------------------*/
58 77
59 /* Exported functions --------------------------------------------------------*/ 78 /* Exported functions --------------------------------------------------------*/
60 79
61 /** SPI init function 80 /** SPI init function
376 UartIR_HUD_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE; 395 UartIR_HUD_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
377 UartIR_HUD_Handle.Init.Mode = UART_MODE_TX_RX; 396 UartIR_HUD_Handle.Init.Mode = UART_MODE_TX_RX;
378 397
379 HAL_UART_Init(&UartIR_HUD_Handle); 398 HAL_UART_Init(&UartIR_HUD_Handle);
380 #endif 399 #endif
381 } 400
401 #ifdef ENABLE_USART_RADIO
402 UartRadio_Handle.Instance = USART_RADIO;
403 UartRadio_Handle.Init.BaudRate = 9600;
404 UartRadio_Handle.Init.WordLength = UART_WORDLENGTH_8B;
405 UartRadio_Handle.Init.StopBits = UART_STOPBITS_1;
406 UartRadio_Handle.Init.Parity = UART_PARITY_NONE;
407 UartRadio_Handle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
408 UartRadio_Handle.Init.Mode = UART_MODE_RX;
409
410 HAL_UART_Init(&UartRadio_Handle);
411 #endif
412
413 }
414
415 #ifdef ENABLE_PULSE_SENSOR_BT
416 void MX_UART_BT_Init_DMA()
417 {
418
419 __DMA2_CLK_ENABLE();
420 __HAL_RCC_DMA2_CLK_ENABLE();
421
422 hdma_uart_BT_rx.Instance = DMA2_Stream2;
423 hdma_uart_BT_rx.Init.Channel = DMA_CHANNEL_4;
424 hdma_uart_BT_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
425 hdma_uart_BT_rx.Init.PeriphInc = DMA_PINC_DISABLE;
426 hdma_uart_BT_rx.Init.MemInc = DMA_MINC_ENABLE;
427 hdma_uart_BT_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
428 hdma_uart_BT_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
429 hdma_uart_BT_rx.Init.Mode = DMA_NORMAL;
430 hdma_uart_BT_rx.Init.Priority = DMA_PRIORITY_LOW;
431 hdma_uart_BT_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
432 HAL_DMA_Init(&hdma_uart_BT_rx);
433
434 __HAL_LINKDMA(&UartHandle, hdmarx, hdma_uart_BT_rx);
435
436 HAL_NVIC_SetPriority(DMA2_Stream2_IRQn, 0, 0);
437 HAL_NVIC_EnableIRQ(DMA2_Stream2_IRQn);
438 }
439 #endif
440
441 #ifdef ENABLE_USART_RADIO
442 void MX_UART_RADIO_Init_DMA()
443 {
444
445 __DMA2_CLK_ENABLE();
446 __HAL_RCC_DMA2_CLK_ENABLE();
447
448 hdma_uart_radio_rx.Instance = DMA2_Stream1;
449 hdma_uart_radio_rx.Init.Channel = DMA_CHANNEL_4;
450 hdma_uart_radio_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
451 hdma_uart_radio_rx.Init.PeriphInc = DMA_PINC_DISABLE;
452 hdma_uart_radio_rx.Init.MemInc = DMA_MINC_ENABLE;
453 hdma_uart_radio_rx.Init.PeriphDataAlignment = DMA_MDATAALIGN_BYTE;
454 hdma_uart_radio_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
455 hdma_uart_radio_rx.Init.Mode = DMA_NORMAL;
456 hdma_uart_radio_rx.Init.Priority = DMA_PRIORITY_LOW;
457 hdma_uart_radio_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
458 HAL_DMA_Init(&hdma_uart_radio_rx);
459
460 __HAL_LINKDMA(&UartRadio_Handle, hdmarx, hdma_uart_radio_rx);
461
462 HAL_NVIC_SetPriority(DMA2_Stream1_IRQn, 0, 0);
463 HAL_NVIC_EnableIRQ(DMA2_Stream1_IRQn);
464 }
465 #endif
466
467
468 uint8_t UART_getChar()
469 {
470 uint8_t retChar = 0;
471
472 if((rxBufRead != rxBufWrite) && (rxBufferUart[rxBufRead] != 0))
473 {
474 retChar = rxBufferUart[rxBufRead];
475 rxBufferUart[rxBufRead++] = 0;
476 if(rxBufRead == CHUNK_SIZE * CHUNKS_PER_BUFFER)
477 {
478 rxBufRead = 0;
479 }
480 }
481 return retChar;
482 }
483 #ifdef ENABLE_PULSE_SENSOR_BT
484 void UART_StartDMARx()
485 {
486 HAL_UART_Receive_DMA (&UartHandle, &rxBufferUart[rxBufWrite], CHUNK_SIZE);
487 rxBufWrite += CHUNK_SIZE;
488 if(rxBufWrite >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
489 {
490 rxBufWrite = 0;
491 }
492 }
493 void DMA2_Stream2_IRQHandler(void)
494 {
495 HAL_DMA_IRQHandler(&hdma_uart_BT_rx);
496 }
497 #endif
498
499 #ifdef ENABLE_USART_RADIO
500 void UART_StartDMARxRadio()
501 {
502 HAL_UART_Receive_DMA (&UartRadio_Handle, &rxBufferUart[rxBufWrite], CHUNK_SIZE);
503 rxBufWrite += CHUNK_SIZE;
504 if(rxBufWrite >= CHUNK_SIZE * CHUNKS_PER_BUFFER)
505 {
506 rxBufWrite = 0;
507 }
508 }
509
510 void DMA2_Stream2_IRQHandler(void)
511 {
512 HAL_DMA_IRQHandler(&hdma_uart_radio_rx);
513 }
514 #endif
382 515
383 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) 516 void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
384 { 517 {
385 if(huart == &UartHandle) 518 if(huart == &UartHandle)
386 UartReady = SET; 519 UartReady = SET;
388 521
389 522
390 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) 523 void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
391 { 524 {
392 if(huart == &UartHandle) 525 if(huart == &UartHandle)
393 UartReady = SET; 526 {
527 #ifdef ENABLE_PULSE_SENSOR_BT
528 if(cv_heartbeat_getState() != SENSOR_HB_OFFLINE)
529 {
530 UART_StartDMARx();
531 }
532 else
533 {
534 UartReady = SET;
535 }
536 #else
537 UartReady = SET;
538 #endif
539 }
394 else 540 else
395 if(huart == &UartIR_HUD_Handle) 541 if(huart == &UartIR_HUD_Handle)
396 { 542 {
397 #ifndef BOOTLOADER_STANDALONE 543 #ifndef BOOTLOADER_STANDALONE
398 tCCR_SetRXIndication(); 544 tCCR_SetRXIndication();