delay enabling frontend after end of sending
This commit is contained in:
parent
7a12bba4df
commit
73c1360978
@ -28,17 +28,17 @@ void frontendSetThreshold(int32_t threshold) {
|
|||||||
|
|
||||||
void frontendEnable() {
|
void frontendEnable() {
|
||||||
frontendEnabled = true;
|
frontendEnabled = true;
|
||||||
|
show(DEBUG_1, ON);
|
||||||
}
|
}
|
||||||
|
|
||||||
void frontendDisable() {
|
void frontendDisable() {
|
||||||
frontendEnabled = false;
|
frontendEnabled = false;
|
||||||
|
show(DEBUG_1, OFF);
|
||||||
}
|
}
|
||||||
|
|
||||||
void frontendAdcCallback(ADC_HandleTypeDef* hadc) {
|
void frontendAdcCallback(ADC_HandleTypeDef* hadc) {
|
||||||
static int32_t holdValue = 0;
|
static int32_t holdValue = 0;
|
||||||
|
|
||||||
// show(DEBUG_2, TOGGLE);
|
|
||||||
|
|
||||||
if (frontendEnabled) {
|
if (frontendEnabled) {
|
||||||
int32_t currentValue = (int32_t) HAL_ADC_GetValue(hadc);
|
int32_t currentValue = (int32_t) HAL_ADC_GetValue(hadc);
|
||||||
|
|
||||||
@ -52,10 +52,8 @@ void frontendAdcCallback(ADC_HandleTypeDef* hadc) {
|
|||||||
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_SET);
|
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_SET);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (holdValue != 0) {
|
holdValue = 0;
|
||||||
holdValue = 0;
|
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_SET);
|
||||||
HAL_GPIO_WritePin(Frontend_Out_GPIO_Port, Frontend_Out_Pin, GPIO_PIN_SET);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -24,6 +24,12 @@ static const char MBUS_TOPIC[] = "IoT/MBGW3/Measurement";
|
|||||||
|
|
||||||
static const uint8_t MBUS_QUERY_CMD = 0x5b;
|
static const uint8_t MBUS_QUERY_CMD = 0x5b;
|
||||||
|
|
||||||
|
// Delay between end of last bit of sent request telegram
|
||||||
|
// and enabling the frontend for receiving the response.
|
||||||
|
// Standard says client must wait at least 11 bit times.
|
||||||
|
// 1 / 2400 * 11 = 4.5e-3ms
|
||||||
|
static const uint32_t DELAY_AFTER_SENDING = 4; // ms
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
MBCR_SUCCESS = 0,
|
MBCR_SUCCESS = 0,
|
||||||
MBCR_ERROR_TIMEOUT,
|
MBCR_ERROR_TIMEOUT,
|
||||||
@ -47,6 +53,7 @@ typedef enum {
|
|||||||
MBCS_SEND_CONTINUED,
|
MBCS_SEND_CONTINUED,
|
||||||
MBCS_SENDING,
|
MBCS_SENDING,
|
||||||
MBCS_SENDING_DONE,
|
MBCS_SENDING_DONE,
|
||||||
|
MBCS_WAIT_AFTER_SENDING,
|
||||||
MBCS_ENABLE_FRONTEND,
|
MBCS_ENABLE_FRONTEND,
|
||||||
MBCS_START1,
|
MBCS_START1,
|
||||||
MBCS_LENGTH1,
|
MBCS_LENGTH1,
|
||||||
@ -258,8 +265,6 @@ static void parseAndPrintFrame() {
|
|||||||
|
|
||||||
|
|
||||||
void mbusCommISR() {
|
void mbusCommISR() {
|
||||||
show(DEBUG_1, TOGGLE);
|
|
||||||
|
|
||||||
uint32_t isrflags = READ_REG(mbusUart.Instance->SR);
|
uint32_t isrflags = READ_REG(mbusUart.Instance->SR);
|
||||||
uint32_t cr1its = READ_REG(mbusUart.Instance->CR1);
|
uint32_t cr1its = READ_REG(mbusUart.Instance->CR1);
|
||||||
|
|
||||||
@ -306,10 +311,16 @@ void mbusCommISR() {
|
|||||||
mbusCommHandle.sendBuffer.readIdx += 1;
|
mbusCommHandle.sendBuffer.readIdx += 1;
|
||||||
if (mbusCommHandle.sendBuffer.readIdx == mbusCommHandle.sendBuffer.writeIdx) {
|
if (mbusCommHandle.sendBuffer.readIdx == mbusCommHandle.sendBuffer.writeIdx) {
|
||||||
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TXE);
|
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TXE);
|
||||||
mbusCommHandle.state = MBCS_SENDING_DONE;
|
__HAL_UART_ENABLE_IT(&mbusUart, UART_IT_TC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// advance the state for the engine only when the last octet is shifted out completely
|
||||||
|
if (((isrflags & USART_SR_TC) != RESET) && ((cr1its & USART_CR1_TCIE) != RESET)) {
|
||||||
|
__HAL_UART_DISABLE_IT(&mbusUart, UART_IT_TC);
|
||||||
|
mbusCommHandle.state = MBCS_SENDING_DONE;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -317,6 +328,7 @@ void mbusCommExec() {
|
|||||||
static uint8_t userdataIdx = 0;
|
static uint8_t userdataIdx = 0;
|
||||||
static uint8_t calculatedChksum = 0;
|
static uint8_t calculatedChksum = 0;
|
||||||
uint8_t receivedOctet = 0;
|
uint8_t receivedOctet = 0;
|
||||||
|
static uint32_t delayAfterSendStartTime = 0;
|
||||||
|
|
||||||
if ((mbusCommHandle.startTime != 0) && ((mbusCommHandle.startTime + 2500) < HAL_GetTick())) {
|
if ((mbusCommHandle.startTime != 0) && ((mbusCommHandle.startTime + 2500) < HAL_GetTick())) {
|
||||||
mbusCommHandle.state = MBCS_TIMEOUT;
|
mbusCommHandle.state = MBCS_TIMEOUT;
|
||||||
@ -368,7 +380,16 @@ void mbusCommExec() {
|
|||||||
|
|
||||||
case MBCS_SENDING_DONE:
|
case MBCS_SENDING_DONE:
|
||||||
//coloredMsg(LOG_YELLOW, false, "mbc hre [%d] state SENDING_DONE", mbusCommHandle.requestId);
|
//coloredMsg(LOG_YELLOW, false, "mbc hre [%d] state SENDING_DONE", mbusCommHandle.requestId);
|
||||||
mbusCommHandle.state = MBCS_ENABLE_FRONTEND;
|
delayAfterSendStartTime = HAL_GetTick();
|
||||||
|
mbusCommHandle.state = MBCS_WAIT_AFTER_SENDING;
|
||||||
|
break;
|
||||||
|
|
||||||
|
case MBCS_WAIT_AFTER_SENDING:
|
||||||
|
// Avoids switching on the frontend while there is still noise from sending on the loop
|
||||||
|
// Make sure to wait not too long otherwise the beginning of the response is missed
|
||||||
|
if ((delayAfterSendStartTime + DELAY_AFTER_SENDING) < HAL_GetTick()) {
|
||||||
|
mbusCommHandle.state = MBCS_ENABLE_FRONTEND;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case MBCS_ENABLE_FRONTEND:
|
case MBCS_ENABLE_FRONTEND:
|
||||||
@ -511,6 +532,9 @@ void mbusCommExec() {
|
|||||||
// coloredMsg(LOG_RED, false, "mbc hre [%d] state ERROR", mbusCommHandle.requestId);
|
// coloredMsg(LOG_RED, false, "mbc hre [%d] state ERROR", mbusCommHandle.requestId);
|
||||||
coloredMsg(LOG_RED, true, "mbc hre [%d] error", mbusCommHandle.requestId);
|
coloredMsg(LOG_RED, true, "mbc hre [%d] error", mbusCommHandle.requestId);
|
||||||
show(LED_RED, ON);
|
show(LED_RED, ON);
|
||||||
|
// disable frontend immediately in case of error since no more data in relevant
|
||||||
|
// this avoids strange noise on RX line/led
|
||||||
|
frontendDisable();
|
||||||
mbusCommHandle.state = MBCS_ERROR_CONTINUED;
|
mbusCommHandle.state = MBCS_ERROR_CONTINUED;
|
||||||
// no break
|
// no break
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user