From e94427e2d85dfc409a4f89c263d727dedcdb3a52 Mon Sep 17 00:00:00 2001 From: "Michael J. Chudobiak" Date: Thu, 16 Aug 2012 12:27:50 -0400 Subject: put flash functions in separate files --- CMakeLists.txt | 1 + flash.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ flash.h | 11 +++++++++++ globals.c | 45 --------------------------------------------- globals.h | 5 ----- instr-daemon.c | 3 ++- parser.c | 9 +++++---- 7 files changed, 64 insertions(+), 55 deletions(-) create mode 100644 flash.c create mode 100644 flash.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 882db13..9155bb3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -16,6 +16,7 @@ add_executable(instr-daemon instr-daemon.c globals.c i2c.c lcd.c + flash.c dummy_functions.c ) add_executable(instr-client instr-client.c) diff --git a/flash.c b/flash.c new file mode 100644 index 0000000..28dd42a --- /dev/null +++ b/flash.c @@ -0,0 +1,45 @@ +#include "globals.h" + +int readUserBlock(FlashStruct *mem) +{ + // read the flash.copy file into the globals.Flash struct + // and return the number of bytes read in + + // if the file does not exist, then "return 0"; + return 0; +} + +void writeUserBlock(FlashStruct *mem, int addr, int numbytes) +{ + // check if flash.copy file exists + // - if it does, update the requested address and + // number of bytes + + // - if does not, create the file and + // set addr = 0, numbytes = sizeof (mem) + // so that entire struct will be written, + // instead of just the requested range + + // All writing should be done in a super-safe + // way. Non-corruption, even during a power-off transient, + // is the priority here. We do not want instruments + // losing configuration data ever, because that + // means expensive repairs. +} + +void initFlash(FlashStruct *mem) +{ + if (readUserBlock(mem) > 0) return; + + // uninitialized device! + mem->flash_start = (char) 99; + strcpy(mem->aux_error_message,"FIXME"); + mem->channels = (short) 1; + mem->enable_avrq_extra_ampls = (char) 12; + mem->ChanKey_frequency = (char) 0; + + // save the default Flash config, for nonvolatile persistence + writeUserBlock(mem, 0, sizeof(mem)); +} + + diff --git a/flash.h b/flash.h new file mode 100644 index 0000000..c1de542 --- /dev/null +++ b/flash.h @@ -0,0 +1,11 @@ +#ifndef FLASH_H_ +#define FLASH_H_ + +#include "globals.h" + +void initFlash(FlashStruct *mem); +int readUserBlock(FlashStruct *mem); +void writeUserBlock(FlashStruct *mem, int addr, int numbytes); + +#endif + diff --git a/globals.c b/globals.c index 66007d3..c5ecbc2 100644 --- a/globals.c +++ b/globals.c @@ -5,48 +5,3 @@ GlobalStruct globals = { .error_queue = {0}, .number_of_errors = 0 }; - - -int readUserBlock(void) -{ - // read the flash.copy file into the globals.Flash struct - // and return the number of bytes read in - - // if the file does not exist, then "return 0"; - return 0; -} - -void writeUserBlock(int addr, int numbytes) -{ - // check if flash.copy file exists - // - if it does, update the requested address and - // number of bytes - - // - if does not, create the file and - // set addr = 0, numbytes = sizeof (mem) - // so that entire struct will be written, - // instead of just the requested range - - // All writing should be done in a super-safe - // way. Non-corruption, even during a power-off transient, - // is the priority here. We do not want instruments - // losing configuration data ever, because that - // means expensive repairs. -} - -void initFlash(void) -{ - if (readUserBlock() > 0) return; - - // uninitialized device! - globals.Flash.flash_start = (char) 99; - strcpy(globals.Flash.aux_error_message,"FIXME"); - globals.Flash.channels = (short) 1; - globals.Flash.enable_avrq_extra_ampls = (char) 12; - globals.Flash.ChanKey_frequency = (char) 0; - - // save the default Flash config, for nonvolatile persistence - writeUserBlock(0, sizeof(globals.Flash)); -} - - diff --git a/globals.h b/globals.h index 79f3362..9062026 100644 --- a/globals.h +++ b/globals.h @@ -400,10 +400,5 @@ typedef struct { extern GlobalStruct globals; -void initFlash(void); -int readUserBlock(void); -void writeUserBlock(int addr, int numbytes); - - #endif diff --git a/instr-daemon.c b/instr-daemon.c index be023ec..ad63329 100644 --- a/instr-daemon.c +++ b/instr-daemon.c @@ -1,6 +1,7 @@ #include "socket-common.h" #include "response.h" #include "lcd.h" +#include "flash.h" #include "globals.h" #include #include @@ -230,7 +231,7 @@ int main(int argc, char **argv) g_type_init (); LCD_initialize(); - initFlash (); + initFlash (&globals.Flash); //register stdin channel stdinChannel = g_io_channel_unix_new(0); diff --git a/parser.c b/parser.c index 528a052..40a6d74 100644 --- a/parser.c +++ b/parser.c @@ -10,6 +10,7 @@ END DESCRIPTION **********************************************************/ #include "parser.h" +#include "flash.h" #include //STATICS @@ -902,7 +903,7 @@ static int Go_Str_eprom_47(gchar** response, int channel, char *loc_string,char *(char *)(&globals.Flash.flash_start + eprom_loc+i)=store_string[i]; } *(char *)(&globals.Flash.flash_start + eprom_loc + i)=(char) 0; /* end of string */ - writeUserBlock(eprom_loc, strlen(store_string)+1); + writeUserBlock(&globals.Flash, eprom_loc, strlen(store_string)+1); return OK; } else { return OutOfRange; @@ -937,7 +938,7 @@ static int Go_int_eprom_48(gchar** response, int channel, char *loc_string,char case command_param_units: the_number=(short) atoi(store_string); *(short *)(&globals.Flash.flash_start + eprom_loc) = the_number; - writeUserBlock(eprom_loc, sizeof (the_number)); + writeUserBlock(&globals.Flash, eprom_loc, sizeof (the_number)); return OK; break; @@ -970,7 +971,7 @@ static int Go_Float_eprom51(gchar** response, int channel, char *loc_string,char case command_param_units: the_number=atof(store_string); *(float *)(&globals.Flash.flash_start + eprom_loc)=the_number; - writeUserBlock(eprom_loc, sizeof(the_number)); + writeUserBlock(&globals.Flash, eprom_loc, sizeof(the_number)); return OK; break; @@ -1003,7 +1004,7 @@ static int Go_char_eprom_70(gchar** response, int channel, char *loc_string,char case command_param_units: the_number=(char) atoi(store_string); *(char *)(&globals.Flash.flash_start + eprom_loc)=the_number; - writeUserBlock(eprom_loc, sizeof(the_number)); + writeUserBlock(&globals.Flash, eprom_loc, sizeof(the_number)); return OK; break; -- cgit