refactoring meterbus

This commit is contained in:
Wolfgang Hottgenroth 2020-11-24 14:47:21 +01:00
parent 0397d61e56
commit a9b71f481a
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
3 changed files with 21 additions and 8 deletions

View File

@ -16,6 +16,7 @@ void ringbufferInit(ringbuffer_t *handle, uint32_t bufferSize);
void ringbufferFree(ringbuffer_t *handle);
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen);
int ringbufferPutOne(ringbuffer_t *handle, uint8_t data);
bool ringbufferEmpty(ringbuffer_t *handle);
int ringbufferGetOne(ringbuffer_t *handle); // if positive, cast to uint8_t and be happy, if negative error

View File

@ -89,7 +89,7 @@ typedef struct {
uint8_t receiveCnt;
bool waitForOctet;
bool receiving;
ringbuffer_t *receiveBuffer;
ringbuffer_t receiveBuffer;
e_mbusCommResult result;
t_longframe frame;
t_mbusDevice *device;
@ -260,6 +260,7 @@ void mbusCommISR() {
if (((isrflags & USART_SR_RXNE) != RESET) || ((isrflags & USART_SR_ORE) != RESET)) {
uint8_t data = (uint8_t)(mbusUart.Instance->DR & (uint8_t)0x00FF);
coloredMsg(LOG_RED, false, "mbc isr 0x%02x", data);
ringbufferPutOne(&(mbusCommHandle.receiveBuffer), data);
return;
} else {
coloredMsg(LOG_RED, false, "mbc isr error 0x%02x", isrflags);
@ -275,7 +276,9 @@ void mbusCommExec() {
if (mbusCommHandle.waitForOctet) {
// when data available, take from buffer and put into receivedOctet
// otherwise return
return;
if (-1 == (receivedOctet = ringbufferGetOne(&(mbusCommHandle.receiveBuffer)))) {
return;
}
}
switch (mbusCommHandle.state) {
@ -693,16 +696,12 @@ void mbusCommInit() {
coloredMsg(LOG_GREEN, true, "mbc mci initializing Meterbus communication");
// enable receive interrupts
/* Enable the UART Parity Error Interrupt */
__HAL_UART_ENABLE_IT(&mbusUart, UART_IT_PE);
/* Enable the UART Error Interrupt: (Frame error, noise error, overrun error) */
__HAL_UART_ENABLE_IT(&mbusUart, UART_IT_ERR);
/* Enable the UART Data Register not empty Interrupt */
__HAL_UART_ENABLE_IT(&mbusUart, UART_IT_RXNE);
ringbufferInit(&(mbusCommHandle.receiveBuffer), 256);
// FIXME
schAdd(mbusCommScheduler, NULL, 0, 1000);
}

View File

@ -19,6 +19,19 @@ void ringbufferFree(ringbuffer_t *handle) {
handle->bufferWriteIdx = 0;
}
int ringbufferPutOne(ringbuffer_t *handle, uint8_t data) {
int retCode = -1;
if (handle->bufferReadIdx != handle->bufferWriteIdx) {
*(handle->buffer + handle->bufferWriteIdx) = data;
handle->bufferWriteIdx += 1;
if (handle->bufferWriteIdx == handle->bufferSize) {
handle->bufferWriteIdx = 0;
}
retCode = 0;
}
return retCode;
}
int ringbufferPut(ringbuffer_t *handle, uint8_t *data, uint32_t dataLen) {
uint32_t freeSpace = 0;
if (handle->bufferReadIdx == handle->bufferWriteIdx) {