first use of libmbus

This commit is contained in:
Wolfgang Hottgenroth 2020-11-03 14:31:59 +01:00
parent 43fe49f90d
commit b1e2277ef5
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
3 changed files with 105 additions and 16 deletions

View File

@ -11,7 +11,7 @@ typedef struct {
uint8_t index; uint8_t index;
} t_mbusFrameField; } t_mbusFrameField;
#define MBUSDEVICE_NAMELENGTH 16 #define MBUSDEVICE_NAMELENGTH 24
#define MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS 4 #define MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS 4
typedef struct { typedef struct {
char deviceName[MBUSDEVICE_NAMELENGTH]; char deviceName[MBUSDEVICE_NAMELENGTH];
@ -19,7 +19,13 @@ typedef struct {
t_mbusFrameField consideredField[MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS]; t_mbusFrameField consideredField[MBUSDEVICE_NUM_OF_CONSIDEREDFIELDS];
} t_mbusDevice; } 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 mbusCommTxCpltCallback(UART_HandleTypeDef *huart);
void mbusCommRxCpltCallback(UART_HandleTypeDef *huart); void mbusCommRxCpltCallback(UART_HandleTypeDef *huart);

View File

@ -24,8 +24,10 @@ void my_errorHandler() {
show(LED_RED, ON); show(LED_RED, ON);
} }
void helloMeterbus(void *handle) {
static t_mbusDevice device = { static uint8_t numOfDevices = 8;
static t_mbusDevice devices[] = {
{
.deviceName = "Total Power", .deviceName = "Total Power",
.address = 80, .address = 80,
.consideredField = { .consideredField = {
@ -34,15 +36,93 @@ void helloMeterbus(void *handle) {
{ .label = "", .index = 0 }, { .label = "", .index = 0 },
{ .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 }
}
}
}; };
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));
void triggerMBusRequest(void *handle) {
static uint8_t deviceIndex = 0;
static uint32_t cnt = 0;
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(); frontendInit();
frontendSetThreshold(240); frontendSetThreshold(240);
schAdd(helloMeterbus, NULL, 0, 10000); schAdd(triggerMBusRequest, NULL, 0, 10000);
} }
void my_loop() { void my_loop() {

View File

@ -33,7 +33,7 @@ typedef enum {
MBCR_ERROR_STATE_ENGINE__STOP, MBCR_ERROR_STATE_ENGINE__STOP,
MBCR_ERROR_STATE_ENGINE__ILLEGAL_STATE, MBCR_ERROR_STATE_ENGINE__ILLEGAL_STATE,
MBCR_ERROR_STATE_ENGINE__UNKNOWN MBCR_ERROR_STATE_ENGINE__UNKNOWN
} t_mbusCommResult; } e_mbusCommResult;
typedef enum { typedef enum {
MBCS_IDLE, MBCS_IDLE,
@ -80,7 +80,7 @@ typedef struct {
uint8_t receiveCnt; uint8_t receiveCnt;
uint8_t receivedOctet; uint8_t receivedOctet;
bool receiving; bool receiving;
t_mbusCommResult result; e_mbusCommResult result;
t_longframe frame; t_longframe frame;
t_mbusDevice *device; t_mbusDevice *device;
} t_mbusCommHandle; } 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) { if (mbusCommHandle.state == MBCS_IDLE) {
mbusCommHandle.state = MBCS_SEND; mbusCommHandle.state = MBCS_SEND;
mbusCommHandle.retryCnt = 0; mbusCommHandle.retryCnt = 0;
@ -397,8 +399,9 @@ void mbusCommRequest(t_mbusDevice *mbusDevice) {
mbusCommHandle.addr = mbusDevice->address; mbusCommHandle.addr = mbusDevice->address;
mbusCommHandle.device = mbusDevice; mbusCommHandle.device = mbusDevice;
schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0); schAdd(handleRequestEngine, (void*) &mbusCommHandle, 0, 0);
} else { res = MBCRR_TRIGGERED;
// busy }
}
return res;
} }