Reading config parameter works

This commit is contained in:
Sebastian H. Gabrielli 2024-04-27 14:52:46 +02:00
parent fb24365469
commit dd624d4d48
3 changed files with 63 additions and 11 deletions

View File

@ -54,7 +54,7 @@ void parse_command(uint8_t command[], uint8_t command_len) {
return;
}
// Validate that we have a first parameter, else requirements for command are
// Validate that we have a second parameter, else requirements for command are
// not fulfilled. return unknown command.
if (command_len < 2) {
context.command = UNKNOWN_COMMAND;
@ -69,7 +69,11 @@ void parse_command(uint8_t command[], uint8_t command_len) {
// Configuration parameters
case 0x11: // Read config
case 0x21: // Write config
// TODO: Handle parameters for config
if (param == 0x01) {
context.conf = SAMPLE_TIME;
} else {
context.conf = CNF_NONE;
}
break;
// Voltage parameters
@ -104,9 +108,9 @@ void parse_command(uint8_t command[], uint8_t command_len) {
break;
}
////////////////////////////////
// Second parameter selection //
////////////////////////////////
/////////////////////////////////
// Third parameter selection //
/////////////////////////////////
// Check if the command does not require a second parameter. If it does not,
// return. Only config write requires a second parameter.
@ -114,7 +118,7 @@ void parse_command(uint8_t command[], uint8_t command_len) {
return;
}
// Validate that we have a first parameter, else requirements for command are
// Validate that we have a third parameter, else requirements for command are
// not fulfilled. return unknown command.
if (command_len < 3) {
context.command = UNKNOWN_COMMAND;
@ -124,7 +128,31 @@ void parse_command(uint8_t command[], uint8_t command_len) {
// Store the parameter
param = command[2];
// TODO: Handle the config parameters
context.conf = param;
////////////////////////////////
// Fourth parameter selection //
////////////////////////////////
// Validate that we have a fourth parameter, else requirements for command are
// not fulfilled. return unknown command. Second parameter is u16, thus two bytes.
if (command_len < 5) {
context.command = UNKNOWN_COMMAND;
return;
}
// Extract the value
union {
uint16_t value;
uint8_t bytes[2];
} config_value;
config_value.bytes[0] = command[3];
config_value.bytes[1] = command[4];
// Store the value
context.conf_val = config_value.value;
// exit
return;
@ -135,7 +163,25 @@ uint8_t route_command(int pos) {
case WRITE_CONFIG:
return WRITE_CONFIG;
case READ_CONFIG:
return READ_CONFIG;
{
// Validate that pos is within the valid range
if (pos >= 2) { return 0x00; }
// Config only has one parameter so we sent that parameter
// Create a union to store the data
union {
uint16_t value;
uint8_t bytes[2];
} config_value;
config_value.value = config.ms_fanspeed_sample_rate;
uint8_t data = config_value.bytes[1-pos];
// Return the corresponding data byte
return data;
}
break;
case READ_VOLTAGE:
{
// Create a union to store the data

View File

@ -42,6 +42,7 @@ typedef enum {
// Enum of all valid config options
// TODO: Move into config header file
typedef enum {
SAMPLE_TIME = 0x01, // Time between each fan speed sample
CNF_NONE, // No config option
} config_option_t;
@ -59,13 +60,17 @@ typedef struct {
src_voltage_t src_voltage; // The selected voltage source
fans_t fan; // The selected fan
config_option_t conf; // The configuration option to cange
// TODO: Add config value field for writing
uint16_t conf_val; // The value of the config option to change
} command_context_t;
// Fan history variables
extern volatile uint16_t fan1_history[512];
extern volatile uint16_t fan2_history[512];
// Config
extern volatile config_t config;
extern volatile bool store_config;
// Parses the input string and outputs one of the valid commands
void parse_command(uint8_t *command, uint8_t command_len);

View File

@ -40,7 +40,8 @@ int main() {
stdout = &USART_stream;
// Read the stored config struct
config = read_struct_from_EEPROM();
//config = read_struct_from_EEPROM();
config.ms_fanspeed_sample_rate = 500;
PORTB.DIRSET = PIN3_bm;