Browse Source

Some redundant core refactorized.

klaute 2 years ago
parent
commit
03a0135f20
2 changed files with 68 additions and 56 deletions
  1. 1
    1
      firmware/command_ctrl.ino
  2. 67
    55
      firmware/firmware.ino

+ 1
- 1
firmware/command_ctrl.ino View File

@@ -394,7 +394,7 @@ void cc_disableClk(void)
394 394
 void cc_saveDefaults(void)
395 395
 {
396 396
 
397
-  saveEEPValues();
397
+  writeEEPROMConfig();
398 398
 
399 399
   Serial.write(MSG_SOM1);
400 400
   Serial.write(MSG_SOM2);

+ 67
- 55
firmware/firmware.ino View File

@@ -97,68 +97,66 @@ void loop()
97 97
 
98 98
 /*****************************************************************************/
99 99
 
100
-void saveEEPValues()
100
+void write32BitEEPROM(uint8_t addr, uint32_t value)
101 101
 {
102
+  EEPROM.write(addr    , (uint8_t)((value & 0xff000000) >> 24));
103
+  EEPROM.write(addr + 1, (uint8_t)((value & 0x00ff0000) >> 16));
104
+  EEPROM.write(addr + 2, (uint8_t)((value & 0x0000ff00) >>  8));
105
+  EEPROM.write(addr + 3, (uint8_t) (value & 0x000000ff));
106
+}
107
+
108
+void write16BitEEPROM(uint8_t addr, uint16_t value)
109
+{
110
+  EEPROM.write(addr    , (uint8_t)((value & 0xff00) >> 8));
111
+  EEPROM.write(addr + 1, (uint8_t) (value & 0x00ff));
112
+}
102 113
 
103
-  EEPROM.write( 0, (uint8_t)((start_freq & 0xff000000) >> 24));
104
-  EEPROM.write( 1, (uint8_t)((start_freq & 0x00ff0000) >> 16));
105
-  EEPROM.write( 2, (uint8_t)((start_freq & 0x0000ff00) >>  8));
106
-  EEPROM.write( 3, (uint8_t) (start_freq & 0x000000ff));
107
-  EEPROM.write( 4, (uint8_t)((end_freq & 0xff000000) >> 24));
108
-  EEPROM.write( 5, (uint8_t)((end_freq & 0x00ff0000) >> 16));
109
-  EEPROM.write( 6, (uint8_t)((end_freq & 0x0000ff00) >>  8));
110
-  EEPROM.write( 7, (uint8_t) (end_freq & 0x000000ff));
111
-  EEPROM.write( 8, (uint8_t)((step_freq & 0xff000000) >> 24));
112
-  EEPROM.write( 9, (uint8_t)((step_freq & 0x00ff0000) >> 16));
113
-  EEPROM.write(10, (uint8_t)((step_freq & 0x0000ff00) >>  8));
114
-  EEPROM.write(11, (uint8_t) (step_freq & 0x000000ff));
115
-  EEPROM.write(12, (uint8_t)((intervall & 0xff00) >> 8));
116
-  EEPROM.write(13, (uint8_t) (intervall & 0x00ff));
117
-  EEPROM.write(14, (uint8_t) drive_str);
114
+void writeEEPROMConfig()
115
+{
116
+
117
+  write32BitEEPROM( 0, start_freq);
118
+  write32BitEEPROM( 4,   end_freq);
119
+  write32BitEEPROM( 8,  step_freq);
120
+  write16BitEEPROM(12,  intervall);
121
+  EEPROM.write(14, (uint8_t)drive_str);
122
+
123
+}
118 124
 
125
+/*****************************************************************************/
126
+
127
+uint32_t read32BitEEPROM(uint8_t addr)
128
+{
129
+  uint32_t tmp  = (uint32_t)EEPROM.read(addr    ) << 24;
130
+           tmp += (uint32_t)EEPROM.read(addr + 1) << 16;
131
+           tmp += (uint32_t)EEPROM.read(addr + 2) <<  8;
132
+           tmp += (uint32_t)EEPROM.read(addr + 3);
133
+
134
+  return tmp;
135
+}
136
+
137
+uint16_t read16BitEEPROM(uint8_t addr)
138
+{
139
+  uint16_t tmp  = (uint16_t)EEPROM.read(addr    ) << 8;
140
+           tmp += (uint16_t)EEPROM.read(addr + 1);
119 141
 }
120 142
 
143
+/*****************************************************************************/
144
+
121 145
 void readEEPValues()
122 146
 {
123
-  uint32_t tmp_start_freq  = (uint32_t)EEPROM.read(0) << 24;
124
-           tmp_start_freq += (uint32_t)EEPROM.read(1) << 16;
125
-           tmp_start_freq += (uint32_t)EEPROM.read(2) <<  8;
126
-           tmp_start_freq += (uint32_t)EEPROM.read(3);
127
-  if (tmp_start_freq < 1)
128
-    tmp_start_freq = 1;
129
-  if (tmp_start_freq > 150000000)
130
-    tmp_start_freq = 150000000;
131
-
132
-  start_freq = tmp_start_freq;
133
-
134
-  uint32_t tmp_end_freq  = (uint32_t)EEPROM.read(4) << 24;
135
-           tmp_end_freq += (uint32_t)EEPROM.read(5) << 16;
136
-           tmp_end_freq += (uint32_t)EEPROM.read(6) <<  8;
137
-           tmp_end_freq += (uint32_t)EEPROM.read(7);
138
-  if (tmp_end_freq < 1)
139
-    tmp_end_freq = 1;
140
-  if (tmp_end_freq > 150000000)
141
-    tmp_end_freq = 150000000;
142
-
143
-  end_freq = tmp_end_freq;
144
-
145
-  uint32_t tmp_step_freq  = (uint32_t)EEPROM.read( 8) << 24;
146
-           tmp_step_freq += (uint32_t)EEPROM.read( 9) << 16;
147
-           tmp_step_freq += (uint32_t)EEPROM.read(10) <<  8;
148
-           tmp_step_freq += (uint32_t)EEPROM.read(11);
149
-  if (tmp_step_freq < 1)
150
-    tmp_step_freq = 1;
151
-  if (tmp_step_freq > 150000000)
152
-    tmp_step_freq = 150000000;
153
-
154
-  step_freq = tmp_step_freq;
155
-
156
-  uint16_t tmp_intervall  = (uint16_t)EEPROM.read(12) << 8;
157
-           tmp_intervall += (uint16_t)EEPROM.read(13);
158
-  if (tmp_intervall < 1)
159
-    tmp_intervall = 1;
160
-  if (tmp_intervall > 150000000)
161
-    tmp_intervall = 150000000;
147
+  uint32_t tmp_start_freq = read32BitEEPROM(0);
148
+
149
+  start_freq = keepFreqRange(tmp_start_freq);
150
+
151
+  uint32_t tmp_end_freq = read32BitEEPROM(4);
152
+
153
+  end_freq = keepFreqRange(tmp_end_freq);
154
+
155
+  uint32_t tmp_step_freq  = read32BitEEPROM(8);
156
+
157
+  step_freq = keepFreqRange(tmp_step_freq);
158
+
159
+  uint16_t tmp_intervall = read16BitEEPROM(12);
162 160
 
163 161
   intervall = tmp_intervall;
164 162
 
@@ -176,3 +174,17 @@ void readEEPValues()
176 174
 
177 175
 /*****************************************************************************/
178 176
 
177
+uint32_t keepFreqRange(uint32_t freq)
178
+{
179
+  uint32_t f = freq;
180
+
181
+  if (freq < 1)
182
+    f = 1;
183
+  else if (freq > 150000000)
184
+    f = 150000000;
185
+
186
+  return f;
187
+}
188
+
189
+/*****************************************************************************/
190
+