Mercurial > public > ostc4
changeset 809:9602a7338f28
DevBugfix CO2 sensor in streaming mode:
In standalone mode (no multiplexer) the CO2 sensor is working in streaming mode => it could happen that the OSTC received data before the scaling factor was requested. As result the CO2 values were calculated with a zero value. This problem has been fixed.
In addition some code cleanup has been done. Unifying usage of changeBaudrate function instead of old baud selection based on protocol request of Discovery firmware.
author | Ideenmodellierer |
---|---|
date | Sun, 27 Aug 2023 20:51:13 +0200 |
parents | ea3267866120 |
children | e6827fcd7604 |
files | Small_CPU/Src/externalInterface.c Small_CPU/Src/uart.c Small_CPU/Src/uartProtocol_Co2.c |
diffstat | 3 files changed, 41 insertions(+), 20 deletions(-) [+] |
line wrap: on
line diff
--- a/Small_CPU/Src/externalInterface.c Sat Aug 26 13:38:13 2023 +0200 +++ b/Small_CPU/Src/externalInterface.c Sun Aug 27 20:51:13 2023 +0200 @@ -94,6 +94,7 @@ static void externalInface_MapUartToLegacyADC(uint8_t* pMap); +static void externalInterface_CheckBaudrate(uint8_t sensorType); void externalInterface_Init(void) { @@ -744,14 +745,18 @@ #ifdef ENABLE_CO2_SUPPORT if(externalAutoDetect == DETECTION_CO2_0) { - UART_MUX_SelectAddress(0); + tmpSensorMap[uartMuxChannel + EXT_INTERFACE_MUX_OFFSET] = SENSOR_NONE; + if(foundSensorMap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) + { + UART_MUX_SelectAddress(0); + } activeUartChannel = 0; tmpSensorMap[uartMuxChannel - 1 + EXT_INTERFACE_MUX_OFFSET] = SENSOR_NONE; uartMuxChannel = 1; tmpSensorMap[EXT_INTERFACE_MUX_OFFSET] = SENSOR_CO2; externalInterface_SensorState[EXT_INTERFACE_MUX_OFFSET] = UART_COMMON_INIT; - externalInterface_SwitchUART(EXT_INTERFACE_UART_CO2 >> 8); - if(tmpSensorMap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* switch sensor operation mode depending on HW config */ + externalInterface_CheckBaudrate(SENSOR_CO2); + if(foundSensorMap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* switch sensor operation mode depending on HW config */ { uartCo2_SendCmd(CO2CMD_MODE_POLL, cmdString, &cmdLength); } @@ -771,13 +776,13 @@ } else if(foundSensorMap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) { - externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); + externalInterface_CheckBaudrate(SENSOR_DIGO2); UART_MUX_SelectAddress(uartMuxChannel); activeUartChannel = uartMuxChannel; tmpSensorMap[uartMuxChannel - 1 + EXT_INTERFACE_MUX_OFFSET] = SENSOR_NONE; tmpSensorMap[EXT_INTERFACE_MUX_OFFSET + uartMuxChannel] = SENSOR_CO2; externalInterface_SensorState[EXT_INTERFACE_MUX_OFFSET + uartMuxChannel] = UART_COMMON_INIT; - externalInterface_SwitchUART(EXT_INTERFACE_UART_CO2 >> 8); + externalInterface_CheckBaudrate(SENSOR_CO2); uartCo2_SendCmd(CO2CMD_MODE_POLL, cmdString, &cmdLength); externalAutoDetect++; uartMuxChannel++; @@ -932,7 +937,6 @@ void externalInterface_CheckBaudrate(uint8_t sensorType) { - static uint32_t lastBaudRate = 0; uint32_t newBaudrate = 0; switch(sensorType) @@ -943,10 +947,9 @@ default: newBaudrate = 19200; break; } - if(lastBaudRate != newBaudrate) + if(huart1.Init.BaudRate != newBaudrate) { UART_ChangeBaudrate(newBaudrate); - lastBaudRate = newBaudrate; } } @@ -967,15 +970,16 @@ if(activeUartChannel == 0xFF) { + MX_USART1_UART_Init(); activeUartChannel = ExternalInterface_SelectUsedMuxChannel(0); uartO2_SetChannel(activeUartChannel); switch(pmap[activeUartChannel + EXT_INTERFACE_MUX_OFFSET]) { - case SENSOR_CO2: externalInterface_SwitchUART(EXT_INTERFACE_UART_CO2 >> 8); + case SENSOR_CO2: externalInterface_CheckBaudrate(SENSOR_CO2); break; default: - case SENSOR_DIGO2: externalInterface_SwitchUART(EXT_INTERFACE_UART_O2 >> 8); + case SENSOR_DIGO2: externalInterface_CheckBaudrate(SENSOR_DIGO2); break; } if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX)
--- a/Small_CPU/Src/uart.c Sat Aug 26 13:38:13 2023 +0200 +++ b/Small_CPU/Src/uart.c Sun Aug 27 20:51:13 2023 +0200 @@ -202,7 +202,7 @@ void UART_ChangeBaudrate(uint32_t newBaudrate) { - + uint8_t dmaWasActive = dmaActive; // HAL_DMA_Abort(&hdma_usart1_rx); MX_USART1_UART_DeInit(); //HAL_UART_Abort(&huart1); @@ -213,8 +213,9 @@ huart1.Init.BaudRate = newBaudrate; HAL_UART_Init(&huart1); MX_USART1_DMA_Init(); - if(dmaActive) + if(dmaWasActive) { + memset(rxBuffer,BUFFER_NODATA,sizeof(rxBuffer)); rxReadIndex = 0; rxWriteIndex = 0; dmaActive = 0; @@ -392,10 +393,7 @@ } if((rxWriteIndex / CHUNK_SIZE) != (rxReadIndex / CHUNK_SIZE) || (rxWriteIndex == rxReadIndex)) /* start next transfer if we did not catch up with read index */ { - if(externalInterface_GetUARTProtocol() != 0) - { - UART_StartDMA_Receiption(); - } + UART_StartDMA_Receiption(); } } } @@ -441,7 +439,8 @@ rxReadIndex = 0; } } - } + + /************************ (C) COPYRIGHT heinrichs weikamp *****END OF FILE****/
--- a/Small_CPU/Src/uartProtocol_Co2.c Sat Aug 26 13:38:13 2023 +0200 +++ b/Small_CPU/Src/uartProtocol_Co2.c Sun Aug 27 20:51:13 2023 +0200 @@ -39,6 +39,8 @@ void uartCo2_SendCmd(uint8_t CO2Cmd, uint8_t *cmdString, uint8_t *cmdLength) { + *cmdLength = 0; + switch (CO2Cmd) { case CO2CMD_MODE_POLL: *cmdLength = snprintf((char*)cmdString, 10, "K 2\r\n"); @@ -80,6 +82,7 @@ if(localComState == UART_CO2_INIT) { + CO2Connected = 0; externalInterface_SetCO2Scale(0.0); UART_StartDMA_Receiption(); localComState = UART_CO2_SETUP; @@ -100,6 +103,7 @@ if(localComState == UART_CO2_CALIBRATE) { uartCo2_SendCmd(CO2CMD_CALIBRATE, cmdString, &cmdLength); + localComState = UART_CO2_IDLE; } else if(pmap[EXT_INTERFACE_SENSOR_CNT-1] == SENSOR_MUX) /* sensor is working in polling mode if mux is connected to avoid interference with other sensors */ { @@ -118,6 +122,7 @@ else { localComState = UART_CO2_OPERATING; /* sensor in streaming mode if not connected to mux => operating */ + UART_StartDMA_Receiption(); } } lastComState = localComState; @@ -159,19 +164,32 @@ if(rxState == RX_Data5) { rxState = RX_DataComplete; - CO2Connected = 1; } } else /* protocol error data has max 5 digits */ { - rxState = RX_Ready; + if(rxState != RX_DataComplete) /* commands will not answer with number values */ + { + rxState = RX_Ready; + } } } if((data == ' ') || (data == '\n')) /* Abort data detection */ { if(rxState == RX_DataComplete) { - localComState = UART_CO2_IDLE; + CO2Connected = 1; + if(localComState == UART_CO2_SETUP) + { + if(dataType == '.') + { + localComState = UART_CO2_IDLE; + } + } + else + { + localComState = UART_CO2_IDLE; + } if(externalInterface_GetCO2State() == 0) { externalInterface_SetCO2State(EXT_INTERFACE_33V_ON);