diff --git a/cube/User/Inc/mbusComm.h b/cube/User/Inc/mbusComm.h index 1758efe..51d88ec 100644 --- a/cube/User/Inc/mbusComm.h +++ b/cube/User/Inc/mbusComm.h @@ -11,7 +11,7 @@ typedef struct { uint8_t index; } t_mbusFrameField; -#define MBUSDEVICE_NAMELENGTH 16 +#define MBUSDEVICE_NAMELENGTH 24 #define MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS 4 typedef struct { char deviceName[MBUSDEVICE_NAMELENGTH]; @@ -19,7 +19,13 @@ typedef struct { t_mbusFrameField consideredField[MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS]; } t_mbusDevice; -void mbusCommRequest(t_mbusDevice *mbusDevice); + +typedef enum { + MBCRR_TRIGGERED = 0, + MBCRR_BUSY = 1 +} e_mbusCommRequestResult; + +e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice); void mbusCommTxCpltCallback(UART_HandleTypeDef *huart); void mbusCommRxCpltCallback(UART_HandleTypeDef *huart); diff --git a/cube/User/Src/main2.c b/cube/User/Src/main2.c index ed20da6..5fff9c4 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -24,8 +24,10 @@ void my_errorHandler() { show(LED_RED, ON); } -void helloMeterbus(void *handle) { - static t_mbusDevice device = { + +static uint8_t numOfDevices = 8; +static t_mbusDevice devices[] = { + { .deviceName = "Total Power", .address = 80, .consideredField = { @@ -34,15 +36,93 @@ void helloMeterbus(void *handle) { { .label = "", .index = 0 }, { .label = "", .index = 0 } } - }; + }, + { + .deviceName = "Computer Power", + .address = 85, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + }, + { + .deviceName = "Dryer Power", + .address = 81, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + }, + { + .deviceName = "Laundry Power", + .address = 82, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + }, + { + .deviceName = "Dishwasher Power", + .address = 83, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + }, + { + .deviceName = "Light Power", + .address = 84, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + }, + { + .deviceName = "Freezer Power", + .address = 86, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + }, + { + .deviceName = "Fridge Power", + .address = 87, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + } + } +}; + +void triggerMBusRequest(void *handle) { + static uint8_t deviceIndex = 0; static uint32_t cnt = 0; - logMsg("*** NEW REQUEST %d ***", cnt); - cnt++; - mbusCommRequest(&device); - // static char msg[] = "Hello"; - // HAL_UART_Transmit_IT(&mbusUart, &msg, strlen(msg)); + logMsg(""); + logMsg("*** NEW REQUEST %s %d %d ***", devices[deviceIndex].deviceName, deviceIndex, cnt); + mbusCommRequest(&(devices[deviceIndex])); + + cnt++; + deviceIndex++; + if (deviceIndex >= numOfDevices) { + deviceIndex = 0; + } } @@ -53,7 +133,7 @@ void my_setup_2() { frontendInit(); frontendSetThreshold(240); - schAdd(helloMeterbus, NULL, 0, 10000); + schAdd(triggerMBusRequest, NULL, 0, 10000); } void my_loop() { diff --git a/cube/User/Src/mbusComm.c b/cube/User/Src/mbusComm.c index 3f9c7a1..5bd84d4 100644 --- a/cube/User/Src/mbusComm.c +++ b/cube/User/Src/mbusComm.c @@ -33,7 +33,7 @@ typedef enum { MBCR_ERROR_STATE_ENGINE__STOP, MBCR_ERROR_STATE_ENGINE__ILLEGAL_STATE, MBCR_ERROR_STATE_ENGINE__UNKNOWN -} t_mbusCommResult; +} e_mbusCommResult; typedef enum { MBCS_IDLE, @@ -80,7 +80,7 @@ typedef struct { uint8_t receiveCnt; uint8_t receivedOctet; bool receiving; - t_mbusCommResult result; + e_mbusCommResult result; t_longframe frame; t_mbusDevice *device; } t_mbusCommHandle; @@ -389,7 +389,9 @@ void mbusCommRxCpltCallback(UART_HandleTypeDef *huart) { } } -void mbusCommRequest(t_mbusDevice *mbusDevice) { +e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) { + e_mbusCommRequestResult res = MBCRR_BUSY; + if (mbusCommHandle.state == MBCS_IDLE) { mbusCommHandle.state = MBCS_SEND; mbusCommHandle.retryCnt = 0; @@ -397,8 +399,9 @@ void mbusCommRequest(t_mbusDevice *mbusDevice) { mbusCommHandle.addr = mbusDevice->address; mbusCommHandle.device = mbusDevice; schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0); - } else { - // busy + res = MBCRR_TRIGGERED; } + + return res; }