From dd624d4d48419b714e5dbceba69b144ce363936a Mon Sep 17 00:00:00 2001 From: "Sebastian H. Gabrielli" Date: Sat, 27 Apr 2024 14:52:46 +0200 Subject: [PATCH] Reading config parameter works --- prosjekt.X/command-handler.c | 62 +++++++++++++++++++++++++++++++----- prosjekt.X/command-handler.h | 9 ++++-- prosjekt.X/main.c | 3 +- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/prosjekt.X/command-handler.c b/prosjekt.X/command-handler.c index e5aec24..ad91602 100644 --- a/prosjekt.X/command-handler.c +++ b/prosjekt.X/command-handler.c @@ -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,8 +128,32 @@ 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 diff --git a/prosjekt.X/command-handler.h b/prosjekt.X/command-handler.h index a03432d..cf0f428 100644 --- a/prosjekt.X/command-handler.h +++ b/prosjekt.X/command-handler.h @@ -42,7 +42,8 @@ typedef enum { // Enum of all valid config options // TODO: Move into config header file typedef enum { - CNF_NONE, // No config option + SAMPLE_TIME = 0x01, // Time between each fan speed sample + CNF_NONE, // No config option } config_option_t; // Enum of all valid fans @@ -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); diff --git a/prosjekt.X/main.c b/prosjekt.X/main.c index 35b71c9..57d6a81 100644 --- a/prosjekt.X/main.c +++ b/prosjekt.X/main.c @@ -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;