Skip to content

Commit 72e9220

Browse files
committed
Add additional functions including measurement timestemp
1 parent 5014a1e commit 72e9220

File tree

5 files changed

+162
-2
lines changed

5 files changed

+162
-2
lines changed

software/src/communication.c

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
CallbackValue_int32_t callback_value_current;
3333
CallbackValue_int32_t callback_value_voltage;
3434
CallbackValue_int32_t callback_value_power;
35+
CallbackValueTimeState callback_value_time_current;
36+
CallbackValueTimeState callback_value_time_voltage;
37+
CallbackValueTimeState callback_value_time_power;
3538

3639
BootloaderHandleMessageResponse handle_message(const void *message, void *response) {
3740
switch(tfp_get_fid_from_message(message)) {
@@ -48,6 +51,16 @@ BootloaderHandleMessageResponse handle_message(const void *message, void *respon
4851
case FID_GET_CONFIGURATION: return get_configuration(message, response);
4952
case FID_SET_CALIBRATION: return set_calibration(message);
5053
case FID_GET_CALIBRATION: return get_calibration(message, response);
54+
case FID_GET_CURRENT_TIME: return get_value_time(message, response, &ina226.current, &ina226.current_time);
55+
case FID_SET_CURRENT_TIME_CALLBACK_CONFIGURATION: return set_value_time_callback_configuration(message, &callback_value_time_current);
56+
case FID_GET_CURRENT_TIME_CALLBACK_CONFIGURATION: return get_value_time_callback_configuration(message, response, &callback_value_time_current);
57+
case FID_GET_VOLTAGE_TIME: return get_value_time(message, response, &ina226.voltage, &ina226.voltage_time);
58+
case FID_SET_VOLTAGE_TIME_CALLBACK_CONFIGURATION: return set_value_time_callback_configuration(message, &callback_value_time_voltage);
59+
case FID_GET_VOLTAGE_TIME_CALLBACK_CONFIGURATION: return get_value_time_callback_configuration(message, response, &callback_value_time_voltage);
60+
case FID_GET_POWER_TIME: return get_value_time(message, response, &ina226.power, &ina226.current_time);
61+
case FID_SET_POWER_TIME_CALLBACK_CONFIGURATION: return set_value_time_callback_configuration(message, &callback_value_time_power);
62+
case FID_GET_POWER_TIME_CALLBACK_CONFIGURATION: return get_value_time_callback_configuration(message, response, &callback_value_time_power);
63+
case FID_GET_TIME: return get_time(message, response);
5164
default: return HANDLE_MESSAGE_RESPONSE_NOT_SUPPORTED;
5265
}
5366
}
@@ -103,6 +116,34 @@ BootloaderHandleMessageResponse get_calibration(const GetCalibration *data, GetC
103116
return HANDLE_MESSAGE_RESPONSE_NEW_MESSAGE;
104117
}
105118

119+
BootloaderHandleMessageResponse get_value_time(const GetValueTime *data, GetValueTime_Response *response, const int32_t *value, const uint32_t *time) {
120+
response->header.length = sizeof(GetValueTime_Response);
121+
response->value = *value;
122+
response->time = *time;
123+
124+
return HANDLE_MESSAGE_RESPONSE_NEW_MESSAGE;
125+
}
126+
127+
BootloaderHandleMessageResponse set_value_time_callback_configuration(const SetValueTimeCallbackConfiguration *data, CallbackValueTimeState *state) {
128+
state->enabled = data->enable;
129+
130+
return HANDLE_MESSAGE_RESPONSE_EMPTY;
131+
}
132+
133+
BootloaderHandleMessageResponse get_value_time_callback_configuration(const GetValueTimeCallbackConfiguration *data, GetValueTimeCallbackConfiguration_Response *response, CallbackValueTimeState *state) {
134+
response->header.length = sizeof(GetValueTimeCallbackConfiguration_Response);
135+
response->enable = state->enabled;
136+
137+
return HANDLE_MESSAGE_RESPONSE_NEW_MESSAGE;
138+
}
139+
140+
BootloaderHandleMessageResponse get_time(const GetTime *data, GetTime_Response *response) {
141+
response->header.length = sizeof(GetTime_Response);
142+
response->time = system_timer_get_ms();
143+
144+
return HANDLE_MESSAGE_RESPONSE_NEW_MESSAGE;
145+
}
146+
106147
bool handle_current_callback(void) {
107148
return handle_callback_value_callback_int32_t(&callback_value_current, FID_CALLBACK_CURRENT);
108149
}
@@ -115,6 +156,42 @@ bool handle_power_callback(void) {
115156
return handle_callback_value_callback_int32_t(&callback_value_power, FID_CALLBACK_POWER);
116157
}
117158

159+
bool handle_value_time_callback(CallbackValueTimeState *state, const uint8_t fid, const int32_t *value, const uint32_t *time) {
160+
if(!state->is_buffered) {
161+
if(!state->enabled || state->last_time == *time) {
162+
return false;
163+
}
164+
165+
tfp_make_default_header(&state->cb.header, bootloader_get_uid(), sizeof(ValueTime_Callback), fid);
166+
state->cb.value = *value;
167+
state->cb.time = *time;
168+
169+
state->last_time = *time;
170+
}
171+
172+
if(bootloader_spitfp_is_send_possible(&bootloader_status.st)) {
173+
bootloader_spitfp_send_ack_and_message(&bootloader_status, (uint8_t*)&state->cb, sizeof(ValueTime_Callback));
174+
state->is_buffered = false;
175+
return true;
176+
} else {
177+
state->is_buffered = true;
178+
}
179+
180+
return false;
181+
}
182+
183+
bool handle_current_time_callback(void) {
184+
return handle_value_time_callback(&callback_value_time_current, FID_CALLBACK_CURRENT_TIME, &ina226.current, &ina226.current_time);
185+
}
186+
187+
bool handle_voltage_time_callback(void) {
188+
return handle_value_time_callback(&callback_value_time_voltage, FID_CALLBACK_VOLTAGE_TIME, &ina226.voltage, &ina226.voltage_time);
189+
}
190+
191+
bool handle_power_time_callback(void) {
192+
return handle_value_time_callback(&callback_value_time_power, FID_CALLBACK_POWER_TIME, &ina226.power, &ina226.current_time);
193+
}
194+
118195
void communication_tick(void) {
119196
communication_callback_tick();
120197
}
@@ -124,5 +201,15 @@ void communication_init(void) {
124201
callback_value_init_int32_t(&callback_value_voltage, ina226_get_voltage);
125202
callback_value_init_int32_t(&callback_value_power, ina226_get_power);
126203

204+
callback_value_time_current.is_buffered = false;
205+
callback_value_time_current.enabled = false;
206+
callback_value_time_current.last_time = 0;
207+
callback_value_time_voltage.is_buffered = false;
208+
callback_value_time_voltage.enabled = false;
209+
callback_value_time_voltage.last_time = 0;
210+
callback_value_time_power.is_buffered = false;
211+
callback_value_time_power.enabled = false;
212+
callback_value_time_power.last_time = 0;
213+
127214
communication_callback_init();
128215
}

software/src/communication.h

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,23 @@ void communication_init(void);
8282
#define FID_GET_CONFIGURATION 14
8383
#define FID_SET_CALIBRATION 15
8484
#define FID_GET_CALIBRATION 16
85+
#define FID_GET_CURRENT_TIME 17
86+
#define FID_SET_CURRENT_TIME_CALLBACK_CONFIGURATION 18
87+
#define FID_GET_CURRENT_TIME_CALLBACK_CONFIGURATION 19
88+
#define FID_GET_VOLTAGE_TIME 21
89+
#define FID_SET_VOLTAGE_TIME_CALLBACK_CONFIGURATION 22
90+
#define FID_GET_VOLTAGE_TIME_CALLBACK_CONFIGURATION 23
91+
#define FID_GET_POWER_TIME 25
92+
#define FID_SET_POWER_TIME_CALLBACK_CONFIGURATION 26
93+
#define FID_GET_POWER_TIME_CALLBACK_CONFIGURATION 27
94+
#define FID_GET_TIME 29
8595

8696
#define FID_CALLBACK_CURRENT 4
8797
#define FID_CALLBACK_VOLTAGE 8
8898
#define FID_CALLBACK_POWER 12
99+
#define FID_CALLBACK_CURRENT_TIME 20
100+
#define FID_CALLBACK_VOLTAGE_TIME 24
101+
#define FID_CALLBACK_POWER_TIME 28
89102

90103
typedef struct {
91104
TFPMessageHeader header;
@@ -125,24 +138,80 @@ typedef struct {
125138
uint16_t current_divisor;
126139
} __attribute__((__packed__)) GetCalibration_Response;
127140

141+
typedef struct {
142+
TFPMessageHeader header;
143+
} __attribute__((__packed__)) GetValueTime;
144+
145+
typedef struct {
146+
TFPMessageHeader header;
147+
int32_t value;
148+
uint32_t time;
149+
} __attribute__((__packed__)) GetValueTime_Response;
150+
151+
typedef struct {
152+
TFPMessageHeader header;
153+
bool enable;
154+
} __attribute__((__packed__)) SetValueTimeCallbackConfiguration;
155+
156+
typedef struct {
157+
TFPMessageHeader header;
158+
} __attribute__((__packed__)) GetValueTimeCallbackConfiguration;
159+
160+
typedef struct {
161+
TFPMessageHeader header;
162+
bool enable;
163+
} __attribute__((__packed__)) GetValueTimeCallbackConfiguration_Response;
164+
165+
typedef struct {
166+
TFPMessageHeader header;
167+
int32_t value;
168+
uint32_t time;
169+
} __attribute__((__packed__)) ValueTime_Callback;
170+
171+
typedef struct {
172+
TFPMessageHeader header;
173+
} __attribute__((__packed__)) GetTime;
174+
175+
typedef struct {
176+
TFPMessageHeader header;
177+
uint32_t time;
178+
} __attribute__((__packed__)) GetTime_Response;
179+
180+
typedef struct {
181+
bool is_buffered;
182+
ValueTime_Callback cb;
183+
184+
bool enabled;
185+
uint32_t last_time;
186+
} CallbackValueTimeState;
128187

129188
// Function prototypes
130189
BootloaderHandleMessageResponse set_configuration(const SetConfiguration *data);
131190
BootloaderHandleMessageResponse get_configuration(const GetConfiguration *data, GetConfiguration_Response *response);
132191
BootloaderHandleMessageResponse set_calibration(const SetCalibration *data);
133192
BootloaderHandleMessageResponse get_calibration(const GetCalibration *data, GetCalibration_Response *response);
193+
BootloaderHandleMessageResponse get_value_time(const GetValueTime *data, GetValueTime_Response *response, const int32_t *value, const uint32_t *time);
194+
BootloaderHandleMessageResponse set_value_time_callback_configuration(const SetValueTimeCallbackConfiguration *data, CallbackValueTimeState *state);
195+
BootloaderHandleMessageResponse get_value_time_callback_configuration(const GetValueTimeCallbackConfiguration *data, GetValueTimeCallbackConfiguration_Response *response, CallbackValueTimeState *state);
196+
BootloaderHandleMessageResponse get_time(const GetTime *data, GetTime_Response *response);
134197

135198
// Callbacks
136199
bool handle_current_callback(void);
137200
bool handle_voltage_callback(void);
138201
bool handle_power_callback(void);
202+
bool handle_current_time_callback(void);
203+
bool handle_voltage_time_callback(void);
204+
bool handle_power_time_callback(void);
139205

140206
#define COMMUNICATION_CALLBACK_TICK_WAIT_MS 1
141-
#define COMMUNICATION_CALLBACK_HANDLER_NUM 3
207+
#define COMMUNICATION_CALLBACK_HANDLER_NUM 6
142208
#define COMMUNICATION_CALLBACK_LIST_INIT \
143209
handle_current_callback, \
144210
handle_voltage_callback, \
145211
handle_power_callback, \
212+
handle_current_time_callback, \
213+
handle_voltage_time_callback, \
214+
handle_power_time_callback, \
146215

147216

148217
#endif

software/src/configs/config.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333

3434
#define FIRMWARE_VERSION_MAJOR 2
3535
#define FIRMWARE_VERSION_MINOR 0
36-
#define FIRMWARE_VERSION_REVISION 3
36+
#define FIRMWARE_VERSION_REVISION 4
3737

3838
#include "config_custom_bootloader.h"
3939

software/src/ina226.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@ void ina226_tick(void) {
157157
switch(ina226.state) {
158158
case INA226_STATE_READ_VOLTAGE: {
159159
ina226.voltage = ((((buffer[0] << 8) | buffer[1]) * VOLTAGE_ADC_MV_MUL) / VOLTAGE_ADC_MV_DIV) * ina226.cal_v_multiplier / ina226.cal_v_divisor;
160+
ina226.voltage_time = system_timer_get_ms();
160161
ina226.state = INA226_STATE_READ_CURRENT;
161162
//logd("voltage: %d\n\r", ina226.voltage);
162163
break;
@@ -165,6 +166,7 @@ void ina226_tick(void) {
165166
case INA226_STATE_READ_CURRENT: {
166167
ina226.current = ((((int16_t)((buffer[0] << 8) | buffer[1])) * CURRENT_ADC_MA_MUL) / CURRENT_ADC_MA_DIV) * ina226.cal_c_multiplier / ina226.cal_c_divisor;
167168
ina226.power = (ina226.voltage * ina226.current) / 1000;
169+
ina226.current_time = system_timer_get_ms();
168170
ina226.state = INA226_STATE_READ_MASK;
169171
//logd("current: %d\n\r", ina226.current);
170172
break;

software/src/ina226.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ typedef struct {
8888
int32_t voltage;
8989
int32_t current;
9090
int32_t power;
91+
uint32_t voltage_time;
92+
uint32_t current_time;
9193
bool new_calibration;
9294
bool new_configuration;
9395
bool new_mask;

0 commit comments

Comments
 (0)