From 03a0135f2015a1e4547eec129ddadfb01851592a Mon Sep 17 00:00:00 2001 From: klaute Date: Fri, 14 Oct 2016 09:46:04 +0200 Subject: [PATCH] Some redundant core refactorized. --- firmware/command_ctrl.ino | 2 +- firmware/firmware.ino | 110 +++++++++++++++++++++----------------- 2 files changed, 62 insertions(+), 50 deletions(-) diff --git a/firmware/command_ctrl.ino b/firmware/command_ctrl.ino index e394cdc..69434ed 100644 --- a/firmware/command_ctrl.ino +++ b/firmware/command_ctrl.ino @@ -394,7 +394,7 @@ void cc_disableClk(void) void cc_saveDefaults(void) { - saveEEPValues(); + writeEEPROMConfig(); Serial.write(MSG_SOM1); Serial.write(MSG_SOM2); diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 6a85bdd..8567a36 100644 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -97,68 +97,66 @@ void loop() /*****************************************************************************/ -void saveEEPValues() +void write32BitEEPROM(uint8_t addr, uint32_t value) +{ + EEPROM.write(addr , (uint8_t)((value & 0xff000000) >> 24)); + EEPROM.write(addr + 1, (uint8_t)((value & 0x00ff0000) >> 16)); + EEPROM.write(addr + 2, (uint8_t)((value & 0x0000ff00) >> 8)); + EEPROM.write(addr + 3, (uint8_t) (value & 0x000000ff)); +} + +void write16BitEEPROM(uint8_t addr, uint16_t value) +{ + EEPROM.write(addr , (uint8_t)((value & 0xff00) >> 8)); + EEPROM.write(addr + 1, (uint8_t) (value & 0x00ff)); +} + +void writeEEPROMConfig() { - EEPROM.write( 0, (uint8_t)((start_freq & 0xff000000) >> 24)); - EEPROM.write( 1, (uint8_t)((start_freq & 0x00ff0000) >> 16)); - EEPROM.write( 2, (uint8_t)((start_freq & 0x0000ff00) >> 8)); - EEPROM.write( 3, (uint8_t) (start_freq & 0x000000ff)); - EEPROM.write( 4, (uint8_t)((end_freq & 0xff000000) >> 24)); - EEPROM.write( 5, (uint8_t)((end_freq & 0x00ff0000) >> 16)); - EEPROM.write( 6, (uint8_t)((end_freq & 0x0000ff00) >> 8)); - EEPROM.write( 7, (uint8_t) (end_freq & 0x000000ff)); - EEPROM.write( 8, (uint8_t)((step_freq & 0xff000000) >> 24)); - EEPROM.write( 9, (uint8_t)((step_freq & 0x00ff0000) >> 16)); - EEPROM.write(10, (uint8_t)((step_freq & 0x0000ff00) >> 8)); - EEPROM.write(11, (uint8_t) (step_freq & 0x000000ff)); - EEPROM.write(12, (uint8_t)((intervall & 0xff00) >> 8)); - EEPROM.write(13, (uint8_t) (intervall & 0x00ff)); - EEPROM.write(14, (uint8_t) drive_str); + write32BitEEPROM( 0, start_freq); + write32BitEEPROM( 4, end_freq); + write32BitEEPROM( 8, step_freq); + write16BitEEPROM(12, intervall); + EEPROM.write(14, (uint8_t)drive_str); } +/*****************************************************************************/ + +uint32_t read32BitEEPROM(uint8_t addr) +{ + uint32_t tmp = (uint32_t)EEPROM.read(addr ) << 24; + tmp += (uint32_t)EEPROM.read(addr + 1) << 16; + tmp += (uint32_t)EEPROM.read(addr + 2) << 8; + tmp += (uint32_t)EEPROM.read(addr + 3); + + return tmp; +} + +uint16_t read16BitEEPROM(uint8_t addr) +{ + uint16_t tmp = (uint16_t)EEPROM.read(addr ) << 8; + tmp += (uint16_t)EEPROM.read(addr + 1); +} + +/*****************************************************************************/ + void readEEPValues() { - uint32_t tmp_start_freq = (uint32_t)EEPROM.read(0) << 24; - tmp_start_freq += (uint32_t)EEPROM.read(1) << 16; - tmp_start_freq += (uint32_t)EEPROM.read(2) << 8; - tmp_start_freq += (uint32_t)EEPROM.read(3); - if (tmp_start_freq < 1) - tmp_start_freq = 1; - if (tmp_start_freq > 150000000) - tmp_start_freq = 150000000; + uint32_t tmp_start_freq = read32BitEEPROM(0); - start_freq = tmp_start_freq; + start_freq = keepFreqRange(tmp_start_freq); - uint32_t tmp_end_freq = (uint32_t)EEPROM.read(4) << 24; - tmp_end_freq += (uint32_t)EEPROM.read(5) << 16; - tmp_end_freq += (uint32_t)EEPROM.read(6) << 8; - tmp_end_freq += (uint32_t)EEPROM.read(7); - if (tmp_end_freq < 1) - tmp_end_freq = 1; - if (tmp_end_freq > 150000000) - tmp_end_freq = 150000000; + uint32_t tmp_end_freq = read32BitEEPROM(4); - end_freq = tmp_end_freq; + end_freq = keepFreqRange(tmp_end_freq); - uint32_t tmp_step_freq = (uint32_t)EEPROM.read( 8) << 24; - tmp_step_freq += (uint32_t)EEPROM.read( 9) << 16; - tmp_step_freq += (uint32_t)EEPROM.read(10) << 8; - tmp_step_freq += (uint32_t)EEPROM.read(11); - if (tmp_step_freq < 1) - tmp_step_freq = 1; - if (tmp_step_freq > 150000000) - tmp_step_freq = 150000000; + uint32_t tmp_step_freq = read32BitEEPROM(8); - step_freq = tmp_step_freq; + step_freq = keepFreqRange(tmp_step_freq); - uint16_t tmp_intervall = (uint16_t)EEPROM.read(12) << 8; - tmp_intervall += (uint16_t)EEPROM.read(13); - if (tmp_intervall < 1) - tmp_intervall = 1; - if (tmp_intervall > 150000000) - tmp_intervall = 150000000; + uint16_t tmp_intervall = read16BitEEPROM(12); intervall = tmp_intervall; @@ -176,3 +174,17 @@ void readEEPValues() /*****************************************************************************/ +uint32_t keepFreqRange(uint32_t freq) +{ + uint32_t f = freq; + + if (freq < 1) + f = 1; + else if (freq > 150000000) + f = 150000000; + + return f; +} + +/*****************************************************************************/ +