# HG changeset patch # User Ideenmodellierer # Date 1730644855 -3600 # Node ID f72613a152dd4851f2413a631b09b433fd80b73f # Parent 0d6c4b40fae420e14875958774ad8dd90d36a531 Switch external interface tx communication to DMA: In the previous version the transmitting of data was done in polling mode. With the introduction of new sensors the length of commands send to the sensor may increase and have a impact to the runtim behavior of the SW. To avoid this the DMA transfers for TX has been activated. diff -r 0d6c4b40fae4 -r f72613a152dd Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c --- a/Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c Sun Nov 03 15:37:00 2024 +0100 +++ b/Common/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c Sun Nov 03 15:40:55 2024 +0100 @@ -182,8 +182,6 @@ static void UART_EndRxTransfer(UART_HandleTypeDef *huart); static void UART_DMATransmitCplt(DMA_HandleTypeDef *hdma); static void UART_DMAReceiveCplt(DMA_HandleTypeDef *hdma); -static void UART_DMATxHalfCplt(DMA_HandleTypeDef *hdma); -static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma); static void UART_DMAError(DMA_HandleTypeDef *hdma); static void UART_DMAAbortOnError(DMA_HandleTypeDef *hdma); static void UART_DMATxAbortCallback(DMA_HandleTypeDef *hdma); @@ -896,7 +894,7 @@ huart->hdmatx->XferCpltCallback = UART_DMATransmitCplt; /* Set the UART DMA Half transfer complete callback */ - huart->hdmatx->XferHalfCpltCallback = UART_DMATxHalfCplt; + huart->hdmatx->XferHalfCpltCallback = NULL; /* Set the DMA error callback */ huart->hdmatx->XferErrorCallback = UART_DMAError; @@ -960,7 +958,7 @@ huart->hdmarx->XferCpltCallback = UART_DMAReceiveCplt; /* Set the UART DMA Half transfer complete callback */ - huart->hdmarx->XferHalfCpltCallback = UART_DMARxHalfCplt; + huart->hdmarx->XferHalfCpltCallback = NULL; /* Set the DMA error callback */ huart->hdmarx->XferErrorCallback = UART_DMAError; @@ -2012,6 +2010,7 @@ } } +#ifdef HALF_COMPLETE_NEEDED /** * @brief DMA UART transmit process half complete callback * @param hdma pointer to a DMA_HandleTypeDef structure that contains @@ -2024,7 +2023,7 @@ HAL_UART_TxHalfCpltCallback(huart); } - +#endif /** * @brief DMA UART receive process complete callback. * @param hdma DMA handle @@ -2058,12 +2057,14 @@ * the configuration information for the specified DMA module. * @retval None */ +#if 0 static void UART_DMARxHalfCplt(DMA_HandleTypeDef *hdma) { UART_HandleTypeDef* huart = (UART_HandleTypeDef*)((DMA_HandleTypeDef*)hdma)->Parent; HAL_UART_RxHalfCpltCallback(huart); } +#endif /** * @brief DMA UART communication error callback. diff -r 0d6c4b40fae4 -r f72613a152dd Small_CPU/Inc/externalInterface.h --- a/Small_CPU/Inc/externalInterface.h Sun Nov 03 15:37:00 2024 +0100 +++ b/Small_CPU/Inc/externalInterface.h Sun Nov 03 15:40:55 2024 +0100 @@ -49,7 +49,8 @@ EXT_INTERFACE_UART_OFF = 0, EXT_INTERFACE_UART_CO2, EXT_INTERFACE_UART_SENTINEL, - EXT_INTERFACE_UART_O2 + EXT_INTERFACE_UART_O2, + EXT_INTERFACE_UART_GNSS }; @@ -71,6 +72,12 @@ DETECTION_CO2_2, DETECTION_CO2_3, #endif +#ifdef ENABLE_GNSS_SUPPORT + DETECTION_GNSS_0, /* check UART channel for connected gnss sensor */ + DETECTION_GNSS_1, + DETECTION_GNSS_2, + DETECTION_GNSS_3, +#endif #ifdef ENABLE_SENTINEL_MODE DETECTION_SENTINEL, /* check UART channel for connected Sentinel */ DETECTION_SENTINEL2, diff -r 0d6c4b40fae4 -r f72613a152dd Small_CPU/Inc/uart.h --- a/Small_CPU/Inc/uart.h Sun Nov 03 15:37:00 2024 +0100 +++ b/Small_CPU/Inc/uart.h Sun Nov 03 15:40:55 2024 +0100 @@ -25,7 +25,7 @@ #include "stm32f4xx_hal.h" -#define BUFFER_NODATA_LOW ('~') /* The read function needs a signiture which indicates that no data for processing is available.*/ +#define BUFFER_NODATA_LOW (0x15) /* The read function needs a signiture which indicates that no data for processing is available.*/ #define BUFFER_NODATA_HIGH (0xA5) @@ -53,6 +53,7 @@ #ifdef ENABLE_SENTINEL_MODE void UART_HandleSentinelData(void); #endif +void UART_clearRxBuffer(void); uint8_t UART_isCO2Connected(); uint8_t UART_isSentinelConnected(); void UART_setTargetChannel(uint8_t channel); diff -r 0d6c4b40fae4 -r f72613a152dd Small_CPU/Src/externalInterface.c --- a/Small_CPU/Src/externalInterface.c Sun Nov 03 15:37:00 2024 +0100 +++ b/Small_CPU/Src/externalInterface.c Sun Nov 03 15:40:55 2024 +0100 @@ -105,7 +105,6 @@ uint16_t coeff; activeChannel = 0; timeoutCnt = 0; - externalInterfacePresent = 0; delayAdcConversion = 0; if(externalInterface_StartConversion(activeChannel) == HAL_OK) { @@ -132,8 +131,6 @@ { uint8_t index = 0; /* init data values */ - externalV33_On = 0; - externalADC_On = 0; externalUART_Protocol = 0; externalCO2Value = 0; externalCO2SignalStrength = 0; @@ -216,7 +213,6 @@ } timeoutCnt = 0; } - } if(timeoutCnt++ >= ADC_TIMEOUT) { @@ -382,6 +378,7 @@ case EXT_INTERFACE_UART_CO2: case EXT_INTERFACE_UART_O2: case EXT_INTERFACE_UART_SENTINEL: + case EXT_INTERFACE_UART_GNSS: if((externalAutoDetect <= DETECTION_START) || ((protocol == EXT_INTERFACE_UART_O2) && (externalAutoDetect >= DETECTION_UARTMUX) && (externalAutoDetect <= DETECTION_DIGO2_3)) @@ -576,14 +573,14 @@ for(index2 = 0; index2 < MAX_ADC_CHANNEL; index2++) /* Unmap old mirror instances */ { - if((pMap[index2] == SENSOR_DIGO2M) || (pMap[index2] == SENSOR_CO2M)) + if((pMap[index2] == SENSOR_DIGO2M) || (pMap[index2] == SENSOR_CO2M) || (pMap[index2] == SENSOR_GNSSM)) { pMap[index2] = SENSOR_NONE; } } /* Map Mux O2 sensors to ADC Slot if ADC slot is not in use */ - for(index = 0; index < EXT_INTERFACE_SENSOR_CNT-1; index++) + for(index = EXT_INTERFACE_MUX_OFFSET; index < EXT_INTERFACE_SENSOR_CNT-1; index++) { if(pMap[index] == SENSOR_DIGO2) { @@ -598,7 +595,7 @@ } } } - for(index = 0; index < EXT_INTERFACE_SENSOR_CNT-1; index++) + for(index = EXT_INTERFACE_MUX_OFFSET; index < EXT_INTERFACE_SENSOR_CNT-1; index++) { if(pMap[index] == SENSOR_CO2) { @@ -613,7 +610,7 @@ } } } - for(index = 0; index < EXT_INTERFACE_SENSOR_CNT-1; index++) + for(index = EXT_INTERFACE_MUX_OFFSET; index < EXT_INTERFACE_SENSOR_CNT-1; index++) { if(pMap[index] == SENSOR_GNSS) { @@ -659,10 +656,11 @@ { static uint8_t sensorIndex = 0; static uint8_t uartMuxChannel = 0; +#ifdef ENABLE_GNSS_SUPPORT +static uint8_t detectionDelayCnt = 0; +#endif uint8_t index = 0; -#ifdef ENABLE_GNSS_SUPPORT - uint8_t detectionDelayCnt = 0; -#endif + uint8_t cntSensor = 0; uint8_t cntUARTSensor = 0; #ifdef ENABLE_CO2_SUPPORT @@ -841,6 +839,7 @@ #if defined ENABLE_SENTINEL_MODE || defined ENABLE_GNSS_SUPPORT #ifdef ENABLE_GNSS_SUPPORT externalAutoDetect = DETECTION_GNSS_0; + externalInterface_SwitchUART(EXT_INTERFACE_UART_GNSS); #else #ifdef ENABLE_SENTINEL_MODE externalAutoDetect = DETECTION_SENTINEL; @@ -867,9 +866,8 @@ tmpSensorMap[EXT_INTERFACE_MUX_OFFSET] = SENSOR_GNSS; externalInterface_SensorState[EXT_INTERFACE_MUX_OFFSET] = UART_COMMON_INIT; externalInterface_CheckBaudrate(SENSOR_GNSS); - externalInterfaceMuxReqIntervall = 200; /* iterations needed for module config */ - detectionDelayCnt = 3; - // uartGnss_SendCmd(GNSSCMD_MODE_POLL, cmdString, &cmdLength); + externalInterfaceMuxReqIntervall = 500; /* iterations needed for module config */ + detectionDelayCnt = 6; } break; case DETECTION_GNSS_0: @@ -1058,7 +1056,9 @@ { index = 0; } - if(((pmap[index + EXT_INTERFACE_MUX_OFFSET] == SENSOR_DIGO2) || (pmap[index + EXT_INTERFACE_MUX_OFFSET] == SENSOR_CO2) || (pmap[index + EXT_INTERFACE_MUX_OFFSET] == SENSOR_GNSS)) + if(((pmap[index + EXT_INTERFACE_MUX_OFFSET] == SENSOR_DIGO2) + || (pmap[index + EXT_INTERFACE_MUX_OFFSET] == SENSOR_CO2) + || (pmap[index + EXT_INTERFACE_MUX_OFFSET] == SENSOR_GNSS)) && (index != activeUartChannel)) { newChannel = index; @@ -1159,9 +1159,11 @@ if((externalInterface_SensorState[activeSensorId] == UART_O2_REQ_O2) /* timeout */ || (externalInterface_SensorState[activeSensorId] == UART_O2_REQ_RAW) - || (externalInterface_SensorState[activeSensorId] == UART_CO2_OPERATING)) + || (externalInterface_SensorState[activeSensorId] == UART_CO2_OPERATING) + || (externalInterface_SensorState[activeSensorId] == UART_GNSS_GET_PVT)) { forceMuxChannel = 1; + externalInterface_SensorState[activeSensorId] = UART_O2_IDLE; switch(pmap[activeSensorId]) { case SENSOR_DIGO2: setExternalInterfaceChannel(activeSensorId,0.0); diff -r 0d6c4b40fae4 -r f72613a152dd Small_CPU/Src/stm32f4xx_hal_msp_v3.c --- a/Small_CPU/Src/stm32f4xx_hal_msp_v3.c Sun Nov 03 15:37:00 2024 +0100 +++ b/Small_CPU/Src/stm32f4xx_hal_msp_v3.c Sun Nov 03 15:40:55 2024 +0100 @@ -242,6 +242,9 @@ GPIO_InitStruct.Pin = GPIO_PIN_10; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); + + HAL_NVIC_SetPriority(USART1_IRQn, 1, 3); + HAL_NVIC_EnableIRQ(USART1_IRQn); } } diff -r 0d6c4b40fae4 -r f72613a152dd Small_CPU/Src/uart.c --- a/Small_CPU/Src/uart.c Sun Nov 03 15:37:00 2024 +0100 +++ b/Small_CPU/Src/uart.c Sun Nov 03 15:40:55 2024 +0100 @@ -23,6 +23,7 @@ #include "uartProtocol_O2.h" #include "uartProtocol_Co2.h" #include "uartProtocol_Sentinel.h" +#include "uartProtocol_GNSS.h" #include "externalInterface.h" #include "data_exchange.h" #include /* memset */ @@ -31,9 +32,9 @@ /* Private variables ---------------------------------------------------------*/ - +#define TX_BUF_SIZE (40u) /* max length for commands */ #define CHUNK_SIZE (25u) /* the DMA will handle chunk size transfers */ -#define CHUNKS_PER_BUFFER (5u) +#define CHUNKS_PER_BUFFER (6u) @@ -41,7 +42,7 @@ uint8_t rxBuffer[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */ uint8_t txBuffer[CHUNK_SIZE]; /* tx uses less bytes */ -uint8_t txBufferQue[CHUNK_SIZE]; /* In MUX mode command may be send shortly after each other => allow q 1 entry que */ +uint8_t txBufferQue[TX_BUF_SIZE]; /* In MUX mode command may be send shortly after each other => allow q 1 entry que */ uint8_t txBufferQueLen; uint8_t rxBufferUart6[CHUNK_SIZE * CHUNKS_PER_BUFFER]; /* The complete buffer has a X * chunk size to allow variations in buffer read time */ @@ -54,11 +55,11 @@ static uint8_t dmaTxActive; /* Indicator if DMA reception needs to be started */ - +static uint8_t isEndIndication(uint8_t index); /* Exported functions --------------------------------------------------------*/ -void clearRxBuffer(void) +void UART_clearRxBuffer(void) { uint16_t index = 0; do @@ -66,6 +67,9 @@ rxBuffer[index++] = BUFFER_NODATA_LOW; rxBuffer[index++] = BUFFER_NODATA_HIGH; } while (index < sizeof(rxBuffer)); + + rxReadIndex = 0; + rxWriteIndex = 0; } void MX_USART1_UART_Init(void) @@ -84,10 +88,7 @@ MX_USART1_DMA_Init(); - HAL_NVIC_SetPriority(USART1_IRQn, 1, 3); - HAL_NVIC_EnableIRQ(USART1_IRQn); - - clearRxBuffer(); + UART_clearRxBuffer(); rxReadIndex = 0; lastCmdIndex = 0; rxWriteIndex = 0; @@ -286,7 +287,7 @@ if(dmaTxActive == 0) { - if(cmdLength < CHUNK_SIZE) /* A longer string is an indication for a missing 0 termination */ + if(cmdLength < TX_BUF_SIZE) /* A longer string is an indication for a missing 0 termination */ { if(dmaRxActive == 0) { @@ -308,14 +309,17 @@ void UART_SendCmdUbx(uint8_t *cmd, uint8_t len) { - if(len < CHUNK_SIZE) /* A longer string is an indication for a missing 0 termination */ + if(len < TX_BUF_SIZE) /* A longer string is an indication for a missing 0 termination */ { if(dmaRxActive == 0) { UART_StartDMA_Receiption(); } memcpy(txBuffer, cmd, len); - HAL_UART_Transmit_DMA(&huart1,txBuffer,len); + if(HAL_OK == HAL_UART_Transmit_DMA(&huart1,txBuffer,len)) + { + dmaTxActive = 1; + } } } @@ -350,38 +354,30 @@ { if(dmaRxActive == 0) { - if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE)) - { - dmaRxActive = 1; - } + if(((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE)) || ((isEndIndication(rxWriteIndex)) && (isEndIndication(rxWriteIndex + 1)))) /* start next transfer if we did not catch up with read index */ + { + if(HAL_OK == HAL_UART_Receive_DMA (&huart1, &rxBuffer[rxWriteIndex], CHUNK_SIZE)) + { + dmaRxActive = 1; + } + } } } void UART_ChangeBaudrate(uint32_t newBaudrate) { - uint8_t dmaWasActive = dmaRxActive; -// HAL_DMA_Abort(&hdma_usart1_rx); - MX_USART1_UART_DeInit(); - //HAL_UART_Abort(&huart1); - //HAL_DMA_DeInit(&hdma_usart1_rx); - - -// huart1.Instance->BRR = UART_BRR_SAMPLING8(HAL_RCC_GetPCLK2Freq()/2, newBaudrate); + MX_USART1_UART_DeInit(); huart1.Init.BaudRate = newBaudrate; HAL_UART_Init(&huart1); MX_USART1_DMA_Init(); HAL_NVIC_SetPriority(USART1_IRQn, 1, 3); HAL_NVIC_EnableIRQ(USART1_IRQn); - if(dmaWasActive) - { - clearRxBuffer(); - rxReadIndex = 0; - rxWriteIndex = 0; - dmaRxActive = 0; - txBufferQueLen = 0; - UART_StartDMA_Receiption(); - } + UART_clearRxBuffer(); + rxReadIndex = 0; + rxWriteIndex = 0; + dmaRxActive = 0; + txBufferQueLen = 0; } void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) @@ -394,10 +390,7 @@ { rxWriteIndex = 0; } - if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE) || (rxWriteIndex == rxReadIndex)) /* start next transfer if we did not catch up with read index */ - { - UART_StartDMA_Receiption(); - } + UART_StartDMA_Receiption(); } } void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart) @@ -508,6 +501,11 @@ huart1.gState = HAL_UART_STATE_READY; dmaTxActive = 0; } + if(huart1.hdmarx->State == HAL_DMA_STATE_READY) + { + huart1.RxState = HAL_UART_STATE_READY; + dmaRxActive = 0; + } } void UART_FlushRxBuffer(void)