diff --git a/cube/User/Inc/logger.h b/cube/User/Inc/logger.h index d4a9fbd..0ec1775 100644 --- a/cube/User/Inc/logger.h +++ b/cube/User/Inc/logger.h @@ -6,6 +6,8 @@ #include +// Disabling this option is preferred. However, when debugging system hangs +// this option needs to be enabled. // #define LOGGER_OUTPUT_BY_INTERRUPT typedef enum { diff --git a/cube/User/Inc/mbusComm.h b/cube/User/Inc/mbusComm.h index f118108..328f952 100644 --- a/cube/User/Inc/mbusComm.h +++ b/cube/User/Inc/mbusComm.h @@ -35,7 +35,8 @@ typedef struct { uint32_t errorCnt; } t_mbusCommStats; -e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice); +// e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice); +void mbusCommInit(); void mbusCommEnable(bool enable); 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 6342d3b..24c674b 100644 --- a/cube/User/Src/main2.c +++ b/cube/User/Src/main2.c @@ -32,159 +32,6 @@ void my_errorHandler() { } -static uint8_t numOfDevices = 8; -static t_mbusDevice devices[] = { - { - .deviceName = "Total Power", - .address = 80, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 17 }, - { .label = "", .index = 0 }, - { .label = "", .index = 0 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Computer Power", - .address = 85, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Dryer Power", - .address = 81, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Laundry Power", - .address = 82, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Dishwasher Power", - .address = 83, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Light Power", - .address = 84, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 15, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Freezer Power", - .address = 86, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - }, - { - .deviceName = "Fridge Power", - .address = 87, - .consideredField = { - { .label = "energy", .index = 0 }, - { .label = "power", .index = 4 }, - { .label = "voltage", .index = 2 }, - { .label = "current", .index = 3 } - }, - .requests = 0, - .failures = 0, - .period = 60, - .delay = 0, - .waiting = false - } -}; - - -void triggerMBusRequest(void *handle) { - static uint8_t deviceIndex = 0; - - if (devices[deviceIndex].waiting) { - e_mbusCommRequestResult r = mbusCommRequest(&(devices[deviceIndex])); - if (r == MBCRR_TRIGGERED) { - devices[deviceIndex].waiting = false; - deviceIndex++; - } - } else { - deviceIndex++; - } - if (deviceIndex >= numOfDevices) { - deviceIndex = 0; - } -} - -void scheduleMBusRequest(void *handle) { - for (uint8_t i = 0; i < numOfDevices; i++) { - devices[i].delay -= 1; - if (devices[i].delay <= 0) { - devices[i].delay = devices[i].period; - devices[i].waiting = true; - coloredMsg(LOG_GREEN, false, "*** Scheduled: %s", devices[i].deviceName); - } - } -} - void my_setup_2() { show(LED_RED, OFF); show(LED_GREEN, BLINK); @@ -200,8 +47,7 @@ void my_setup_2() { frontendInit(); frontendSetThreshold(240); - schAdd(scheduleMBusRequest, NULL, 0, 1000); - schAdd(triggerMBusRequest, NULL, 0, 100); + mbusCommInit(); } void my_loop() { diff --git a/cube/User/Src/mbusComm.c b/cube/User/Src/mbusComm.c index 66d743a..d44f016 100644 --- a/cube/User/Src/mbusComm.c +++ b/cube/User/Src/mbusComm.c @@ -11,6 +11,7 @@ #include #include #include +#include #include @@ -424,7 +425,7 @@ void mbusCommEnable(bool enable) { mbusCommEnabled = enable; } -e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) { +static e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) { e_mbusCommRequestResult res = MBCRR_BUSY; if (mbusCommEnabled) { @@ -457,3 +458,195 @@ e_mbusCommRequestResult mbusCommRequest(t_mbusDevice *mbusDevice) { return res; } + +static uint8_t numOfDevices = 8; +static t_mbusDevice devices[] = { + { + .deviceName = "Total Power", + .address = 80, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 17 }, + { .label = "", .index = 0 }, + { .label = "", .index = 0 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Computer Power", + .address = 85, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Dryer Power", + .address = 81, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Laundry Power", + .address = 82, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Dishwasher Power", + .address = 83, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Light Power", + .address = 84, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 15, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Freezer Power", + .address = 86, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + }, + { + .deviceName = "Fridge Power", + .address = 87, + .consideredField = { + { .label = "energy", .index = 0 }, + { .label = "power", .index = 4 }, + { .label = "voltage", .index = 2 }, + { .label = "current", .index = 3 } + }, + .requests = 0, + .failures = 0, + .period = 60, + .delay = 0, + .waiting = false + } +}; + + +static void triggerMBusRequest(void *handle) { + static uint8_t deviceIndex = 0; + + if (devices[deviceIndex].waiting) { + e_mbusCommRequestResult r = mbusCommRequest(&(devices[deviceIndex])); + if (r == MBCRR_TRIGGERED) { + devices[deviceIndex].waiting = false; + deviceIndex++; + } + } else { + deviceIndex++; + } + if (deviceIndex >= numOfDevices) { + deviceIndex = 0; + } +} + + +static void mbusCommScheduler(void *handle) { + static uint8_t state = 0; + + switch (state) { + case 0: + if (isNetworkAvailable()) { + coloredMsg(LOG_GREEN, true, "mbc mcs activate scheduler by network"); + schAdd(triggerMBusRequest, NULL, 0, 100); + state = 1; + } + break; + + case 1: + if (! isNetworkAvailable()) { + coloredMsg(LOG_GREEN, true, "mbc mcs deactivate scheduler by network"); + schDel(triggerMBusRequest, NULL); + state = 0; + } + if (! mbusCommEnabled) { + coloredMsg(LOG_GREEN, true, "mbc mcs deactivate scheduler by request"); + schDel(triggerMBusRequest, NULL); + state = 2; + } + for (uint8_t i = 0; i < numOfDevices; i++) { + devices[i].delay -= 1; + if (devices[i].delay <= 0) { + devices[i].delay = devices[i].period; + devices[i].waiting = true; + coloredMsg(LOG_GREEN, false, "*** Scheduled: %s", devices[i].deviceName); + } + } + break; + + case 2: + if (mbusCommEnabled) { + coloredMsg(LOG_GREEN, true, "mbc mcs activate scheduler by request"); + schAdd(triggerMBusRequest, NULL, 0, 100); + state = 1; + } + break; + } +} + +void mbusCommInit() { + coloredMsg(LOG_GREEN, true, "mbc mci initializing Meterbus communication"); + schAdd(mbusCommScheduler, NULL, 0, 1000); +} +