Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions opendps/opendps.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,31 +332,48 @@ set_param_status_t opendps_set_parameter(char *name, char *value)
set_param_status_t opendps_set_calibration(char *name, float *value)
{
past_id_t param;
float min_val, max_val;

if (strcmp(name,"A_ADC_K")==0){
param = past_A_ADC_K;
min_val = 0.1f; max_val = 100.0f; // Current ADC gain coefficient
} else if(strcmp(name,"A_ADC_C")==0){
param = past_A_ADC_C;
min_val = -1000.0f; max_val = 1000.0f; // Current ADC offset
} else if(strcmp(name,"A_DAC_K")==0){
param = past_A_DAC_K;
min_val = 0.01f; max_val = 10.0f; // Current DAC gain coefficient
} else if(strcmp(name,"A_DAC_C")==0){
param = past_A_DAC_C;
min_val = 0.0f; max_val = 1000.0f; // Current DAC offset
} else if(strcmp(name,"V_ADC_K")==0){
param = past_V_ADC_K;
min_val = 1.0f; max_val = 100.0f; // Voltage ADC gain coefficient
} else if(strcmp(name,"V_ADC_C")==0){
param = past_V_ADC_C;
min_val = -1000.0f; max_val = 1000.0f; // Voltage ADC offset
} else if(strcmp(name,"V_DAC_K")==0){
param = past_V_DAC_K;
min_val = 0.01f; max_val = 1.0f; // Voltage DAC gain coefficient
} else if(strcmp(name,"V_DAC_C")==0){
param = past_V_DAC_C;
min_val = 0.0f; max_val = 100.0f; // Voltage DAC offset
} else if(strcmp(name,"VIN_ADC_K")==0){
param = past_VIN_ADC_K;
min_val = 1.0f; max_val = 100.0f; // Input voltage ADC gain coefficient
} else if(strcmp(name,"VIN_ADC_C")==0){
param = past_VIN_ADC_C;
min_val = -1000.0f; max_val = 1000.0f; // Input voltage ADC offset
} else {
return ps_not_supported;
}

// Validate calibration parameter is within reasonable bounds
if (*value < min_val || *value > max_val) {
dbg_printf("Error: calibration value %f out of range [%f, %f]\n", *value, min_val, max_val);
return ps_range_error;
}

if (!past_write_unit(&g_past, param, (void*) value, sizeof(*value))) {
dbg_printf("Error: past write opendps set calibration failed!\n");
return ps_flash_error;
Expand Down
161 changes: 74 additions & 87 deletions opendps/protocol_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,18 @@ typedef enum {
cmd_success_but_i_actually_sent_my_own_status_thank_you_very_much,
} command_status_t;

typedef struct __attribute__((packed)) {
int16_t temp1;
int16_t temp2;
} temperature_report_payload_t;

typedef struct __attribute__((packed)) {
uint16_t chunk_size;
uint16_t crc;
} upgrade_start_payload_t;



static uint8_t frame_buffer[MAX_FRAME_LENGTH];
static uint32_t rx_idx = 0;
static bool receiving_frame = false;
Expand Down Expand Up @@ -196,28 +208,30 @@ static command_status_t handle_list_functions(void)
static command_status_t handle_set_parameters(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
char *name = 0, *value = 0;
command_t cmd;
set_param_status_t stats[OPENDPS_MAX_PARAMETERS];
uint32_t status_index = 0;
{
start_frame_unpacking(frame);
unpack8(frame, &cmd);
(void) cmd;
do {
/** Extract all occurences of <name>=<value>\0 ... */
name = value = 0;
/** This is quite ugly, please don't look */
name = (char*) &frame->buffer[frame->unpack_pos];
frame->unpack_pos += strlen(name) + 1;
frame->length -= strlen(name) + 1;
value = (char*) &frame->buffer[frame->unpack_pos];
frame->unpack_pos += strlen(value) + 1;
frame->length -= strlen(value) + 1;
if (name && value) {
stats[status_index++] = opendps_set_parameter(name, value);
}
} while(frame->length && status_index < OPENDPS_MAX_PARAMETERS);
while (frame->length && status_index < OPENDPS_MAX_PARAMETERS) {
char *name = (char*) &frame->buffer[frame->unpack_pos];
size_t rem = frame->length;
size_t name_len = 0;
while (name_len < rem && name[name_len]) name_len++;
if (name_len == rem) break;
frame->unpack_pos += name_len + 1;
frame->length -= name_len + 1;
rem -= name_len + 1;
char *value = (char*) &frame->buffer[frame->unpack_pos];
size_t value_len = 0;
while (value_len < rem && value[value_len]) value_len++;
if (value_len == rem) break;
frame->unpack_pos += value_len + 1;
frame->length -= value_len + 1;
stats[status_index++] = opendps_set_parameter(name, value);
}
}

{
Expand All @@ -237,29 +251,29 @@ static command_status_t handle_set_parameters(frame_t *frame)
static command_status_t handle_set_calibration(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
char *name = 0;
float *value = 0;
command_t cmd;
set_param_status_t stats[OPENDPS_MAX_PARAMETERS];
uint32_t status_index = 0;
{
start_frame_unpacking(frame);
unpack8(frame, &cmd);
(void) cmd;
do {
/** Extract all occurences of <name>=<value> ... */
name = 0;
/** This is quite ugly (again), please don't look */
name = (char*) &frame->buffer[frame->unpack_pos];
frame->unpack_pos += strlen(name) + sizeof(char);
frame->length -= strlen(name) + sizeof(char);
value = (float*) &frame->buffer[frame->unpack_pos];
while (frame->length && status_index < OPENDPS_MAX_PARAMETERS) {
char *name = (char*) &frame->buffer[frame->unpack_pos];
size_t rem = frame->length;
size_t name_len = 0;
while (name_len < rem && name[name_len]) name_len++;
if (name_len == rem) break;
frame->unpack_pos += name_len + 1;
frame->length -= name_len + 1;
rem -= name_len + 1;
if (rem < sizeof(float)) break;
float f;
memcpy(&f, &frame->buffer[frame->unpack_pos], sizeof(float));
frame->unpack_pos += sizeof(float);
frame->length -= sizeof(float);
if (name && value) {
stats[status_index++] = opendps_set_calibration(name, value);
}
} while(frame->length && status_index < OPENDPS_MAX_PARAMETERS);
stats[status_index++] = opendps_set_calibration(name, &f);
}
}

{
Expand Down Expand Up @@ -305,45 +319,30 @@ static command_status_t handle_list_parameters(void)
static command_status_t handle_enable_output(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
uint8_t cmd;
uint8_t enable_byte;
start_frame_unpacking(frame);
unpack8(frame, &cmd);
(void) cmd;
unpack8(frame, &enable_byte);
bool enable = !!enable_byte;
if (opendps_enable_output(enable)) {
return cmd_success;
} else {
return cmd_failed;
}
if (frame->length < 2) return cmd_failed;

return opendps_enable_output(!!frame->buffer[1]) ? cmd_success : cmd_failed;
}

static command_status_t handle_set_brightness(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
uint8_t cmd;
uint8_t brightness_pct;
start_frame_unpacking(frame);
unpack8(frame, &cmd);
(void) cmd;
unpack8(frame, &brightness_pct);
hw_set_backlight(brightness_pct);
if (frame->length < 2) return cmd_failed;

hw_set_backlight(frame->buffer[1]);
return cmd_success;
}

#ifdef CONFIG_THERMAL_LOCKOUT
static command_status_t handle_temperature(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
command_t cmd;
int16_t temp1, temp2;
start_frame_unpacking(frame);
unpack8(frame, &cmd);
(void) cmd;
unpack16(frame, (uint16_t*) &temp1);
unpack16(frame, (uint16_t*) &temp2);
opendps_set_temperature(temp1, temp2);
if (frame->length < 1 + sizeof(temperature_report_payload_t)) return cmd_failed;

temperature_report_payload_t payload;
memcpy(&payload, &frame->buffer[1], sizeof(payload));

opendps_set_temperature(payload.temp1, payload.temp2);
return cmd_success;
}
#endif // CONFIG_THERMAL_LOCKOUT
Expand Down Expand Up @@ -434,13 +433,10 @@ static command_status_t handle_clear_calibration(void)
static command_status_t handle_wifi_status(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
command_status_t success = cmd_failed;
wifi_status_t status;
if (protocol_unpack_wifi_status(frame, &status)) {
success = cmd_success;
opendps_update_wifi_status(status);
}
return success;
if (frame->length < 2) return cmd_failed;

opendps_update_wifi_status((wifi_status_t)frame->buffer[1]);
return cmd_success;
}

/**
Expand All @@ -452,13 +448,10 @@ static command_status_t handle_wifi_status(frame_t *frame)
static command_status_t handle_lock(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
command_status_t success = cmd_failed;
uint8_t status;
if (protocol_unpack_lock(frame, &status)) {
success = cmd_success;
opendps_lock(status);
}
return success;
if (frame->length < 2) return cmd_failed;

opendps_lock(frame->buffer[1]);
return cmd_success;
}

/**
Expand All @@ -470,13 +463,14 @@ static command_status_t handle_lock(frame_t *frame)
static command_status_t handle_upgrade_start(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
command_status_t success = cmd_failed;
uint16_t chunk_size, crc;
if (protocol_unpack_upgrade_start(frame, &chunk_size, &crc)) {
bootcom_put(0xfedebeda, (chunk_size << 16) | crc);
opendps_upgrade_start();
}
return success;
if (frame->length < 1 + sizeof(upgrade_start_payload_t)) return cmd_failed;

upgrade_start_payload_t payload;
memcpy(&payload, &frame->buffer[1], sizeof(payload));

bootcom_put(0xfedebeda, (payload.chunk_size << 16) | payload.crc);
opendps_upgrade_start();
return cmd_failed;
}

/**
Expand All @@ -488,16 +482,9 @@ static command_status_t handle_upgrade_start(frame_t *frame)
static command_status_t handle_change_screen(frame_t *frame)
{
emu_printf("%s\n", __FUNCTION__);
uint8_t cmd, screen_id;
start_frame_unpacking(frame);
unpack8(frame, &cmd);
(void) cmd;
unpack8(frame, &screen_id);
if (opendps_change_screen(screen_id)) {
return cmd_success;
} else {
return cmd_failed;
}
if (frame->length < 2) return cmd_failed;

return opendps_change_screen(frame->buffer[1]) ? cmd_success : cmd_failed;
}

/**
Expand Down
6 changes: 6 additions & 0 deletions opendps/pwrctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,8 @@ uint32_t pwrctl_calc_vin(uint16_t raw)
float value = vin_adc_k_coef * raw + vin_adc_c_coef;
if (value <= 0)
return 0;
else if (value > 65000) /** Sanity limit for input voltage (65V max) */
return 65000;
else
return value + 0.5f; /** Add 0.5f to value so it is correctly rounded when it is truncated */
}
Expand All @@ -257,6 +259,8 @@ uint32_t pwrctl_calc_vout(uint16_t raw)
float value = v_adc_k_coef * raw + v_adc_c_coef;
if (value <= 0)
return 0;
else if (value > 60000) /** Sanity limit for output voltage (60V max for DPS5015) */
return 60000;
else
return value + 0.5f; /** Add 0.5f to value so it is correctly rounded when it is truncated */
}
Expand Down Expand Up @@ -287,6 +291,8 @@ uint32_t pwrctl_calc_iout(uint16_t raw)
float value = a_adc_k_coef * raw + a_adc_c_coef;
if (value <= 0)
return 0;
else if (value > 20000) /** Sanity limit for current (20A max to handle all DPS models) */
return 20000;
else
return value + 0.5f; /** Add 0.5f to value so correct rounding is done when truncated */
}
Expand Down