Do radio settings solely by frequency
authorKeith Packard <keithp@keithp.com>
Tue, 27 Mar 2012 06:35:35 +0000 (23:35 -0700)
committerKeith Packard <keithp@keithp.com>
Tue, 27 Mar 2012 06:35:35 +0000 (23:35 -0700)
Compute the radio setting needed based on the calibration value
provided and the requested frequency.

Signed-off-by: Keith Packard <keithp@keithp.com>
14 files changed:
src/core/ao.h
src/core/ao_cmd.c
src/core/ao_config.c
src/core/ao_freq.c [new file with mode: 0644]
src/product/Makefile.telebt
src/product/Makefile.teledongle
src/product/Makefile.telelaunch
src/product/Makefile.telemetrum
src/product/Makefile.telemini
src/product/Makefile.telenano
src/product/ao_terraui.c
src/teleshield-v0.1/Makefile
src/teleterra-v0.2/Makefile
src/tidongle/Makefile

index 8c5335c..ce9a1f7 100644 (file)
@@ -1539,7 +1539,6 @@ ao_igniter_init(void);
 struct ao_radio_channel {
        char            name[AO_CHANNEL_NAME_LEN];
        uint32_t        kHz;
-       uint32_t        radio_setting;
 };
 #endif
 
@@ -1559,6 +1558,7 @@ struct ao_config {
        uint32_t        radio_setting;          /* minor version 7 */
        uint8_t         radio_enable;           /* minor version 8 */
        uint8_t         aes_key[AO_AES_LEN];    /* minor version 9 */
+       uint32_t        frequency;              /* minor version 10 */
 #if HAS_RADIO_CHANNELS
        struct ao_radio_channel radio_channels[AO_NUM_CHANNELS];        /* minor version 10 */
 #endif
@@ -1959,4 +1959,10 @@ ao_battery_init(void);
 uint32_t
 ao_sqrt(uint32_t op);
 
+/*
+ * ao_freq.c
+ */
+
+int32_t ao_freq_to_set(int32_t freq, int32_t cal);
+
 #endif /* _AO_H_ */
index 14cb756..cde68b3 100644 (file)
@@ -263,9 +263,9 @@ static __pdata uint8_t              ao_ncmds;
 static void
 help(void)
 {
-       register uint8_t cmds;
-       register uint8_t cmd;
-       register __code struct ao_cmds * cs;
+       __pdata uint8_t cmds;
+       __pdata uint8_t cmd;
+       __code struct ao_cmds * __pdata cs;
 
        for (cmds = 0; cmds < ao_ncmds; cmds++) {
                cs = ao_cmds[cmds];
index 86bbc47..6fcebe1 100644 (file)
@@ -64,6 +64,12 @@ ao_config_put(void)
 }
 #endif
 
+static void
+ao_config_set_radio(void)
+{
+       ao_config.radio_setting = ao_freq_to_set(ao_config.frequency, ao_config.radio_cal);
+}
+
 static void
 _ao_config_get(void)
 {
@@ -105,24 +111,23 @@ _ao_config_get(void)
                        ao_config.ignite_mode = AO_CONFIG_DEFAULT_IGNITE_MODE;
                if (ao_config.minor < 6)
                        ao_config.pad_orientation = AO_CONFIG_DEFAULT_PAD_ORIENTATION;
-               if (ao_config.minor < 7)
-                       ao_config.radio_setting = ao_config.radio_cal;
                if (ao_config.minor < 8)
                        ao_config.radio_enable = TRUE;
                if (ao_config.minor < 9)
                        memset(&ao_config.aes_key, 0, AO_AES_LEN);
-#if HAS_RADIO_CHANNELS
                if (ao_config.minor < 10) {
+                       ao_config.frequency = 434550;
+#if HAS_RADIO_CHANNELS
                        ao_xmemset(&ao_config.radio_channels, '\0', sizeof (ao_config.radio_channels));
                        ao_xmemcpy(&ao_config.radio_channels[0].name[0],
                                   CODE_TO_XDATA("Channel 0"), sizeof("Channel 0"));
                        ao_config.radio_channels[0].kHz = 434550;
-                       ao_config.radio_channels[0].radio_setting = ao_config.radio_cal;
-               }
 #endif                            
+               }
                ao_config.minor = AO_CONFIG_MINOR;
                ao_config_dirty = 1;
        }
+       ao_config_set_radio();
        ao_config_loaded = 1;
 }
 
@@ -196,6 +201,26 @@ ao_config_radio_channel_set(void) __reentrant
        ao_radio_recv_abort();
 }
 
+void
+ao_config_frequency_show(void) __reentrant
+{
+       printf("Frequency: %ld\n",
+              ao_config.frequency);
+}
+
+void
+ao_config_frequency_set(void) __reentrant
+{
+       ao_cmd_decimal();
+       if (ao_cmd_status != ao_cmd_success)
+               return;
+       _ao_config_edit_start();
+       ao_config.frequency = ao_cmd_lex_u32;
+       ao_config_set_radio();
+       _ao_config_edit_finish();
+       ao_radio_recv_abort();
+}
+
 #if HAS_ADC
 
 void
@@ -315,7 +340,8 @@ ao_config_radio_cal_set(void) __reentrant
        if (ao_cmd_status != ao_cmd_success)
                return;
        _ao_config_edit_start();
-       ao_config.radio_setting = ao_config.radio_cal = ao_cmd_lex_u32;
+       ao_config.radio_cal = ao_cmd_lex_u32;
+       ao_config_set_radio();
        _ao_config_edit_finish();
 }
 
@@ -394,25 +420,6 @@ ao_config_pad_orientation_set(void) __reentrant
 }
 #endif
 
-void
-ao_config_radio_setting_show(void) __reentrant
-{
-       printf("Radio setting: %ld\n", ao_config.radio_setting);
-}
-
-void
-ao_config_radio_setting_set(void) __reentrant
-{
-       ao_cmd_decimal();
-       if (ao_cmd_status != ao_cmd_success)
-               return;
-       _ao_config_edit_start();
-       ao_config.radio_setting = ao_cmd_lex_u32;
-       ao_config.radio_channel = 0;
-       _ao_config_edit_finish();
-       ao_radio_recv_abort();
-}
-
 void
 ao_config_radio_enable_show(void) __reentrant
 {
@@ -464,11 +471,10 @@ ao_config_radio_config_show(void) __reentrant
        uint8_t i;
        for (i = 0; i < AO_NUM_CHANNELS; i++)
                if (ao_config.radio_channels[i].name[0]) {
-                       printf("%2d %-16.16s %ld %ld\n",
+                       printf("%2d %-16.16s %ld\n",
                               i,
                               ao_config.radio_channels[i].name,
-                              ao_config.radio_channels[i].kHz,
-                              ao_config.radio_channels[i].radio_setting);
+                              ao_config.radio_channels[i].kHz);
                }
 }
 
@@ -498,8 +504,6 @@ ao_config_radio_config_set(void) __reentrant
        }
        ao_cmd_decimal();
        ch->kHz = ao_cmd_lex_u32;
-       ao_cmd_decimal();
-       ch->radio_setting = ao_cmd_lex_u32;
        _ao_config_edit_finish();
 }
 #endif
@@ -528,10 +532,10 @@ __code struct ao_config_var ao_config_vars[] = {
 #endif /* HAS_ADC */
        { "r <channel>\0Radio channel (freq = 434.550 + chan * .1)",
          ao_config_radio_channel_set,  ao_config_radio_channel_show },
+       { "F <freq>\0Frequency (kHz)",
+         ao_config_frequency_set, ao_config_frequency_show },
        { "c <call>\0Callsign (8 char max)",
          ao_config_callsign_set,       ao_config_callsign_show },
-       { "R <setting>\0Radio freq control (freq = 434.550 * setting/cal)",
-         ao_config_radio_setting_set,  ao_config_radio_setting_show },
        { "e <0 disable, 1 enable>\0Enable telemetry and RDF",
          ao_config_radio_enable_set, ao_config_radio_enable_show },
 #if HAS_ACCEL
diff --git a/src/core/ao_freq.c b/src/core/ao_freq.c
new file mode 100644 (file)
index 0000000..13bcb38
--- /dev/null
@@ -0,0 +1,52 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <ao.h>
+
+/*
+ * The provided 'calibration' value is
+ * that needed to tune the radio to precisely 434550kHz.
+ * Use that to 'walk' to the target frequency by following
+ * a 'bresenham' line from 434550kHz to the target
+ * frequency, and updating the radio setting along the way
+ */
+
+int32_t ao_freq_to_set(int32_t freq, int32_t cal) {
+       __pdata int32_t set = 0;
+       uint8_t neg = 0;
+       __pdata int32_t error = -434550 / 2;
+
+       freq -= 434550;
+       if (freq < 0) {
+               neg = 1;
+               freq = -freq;
+       }
+       for (;;) {
+               if (freq == 0 && error <= 0)
+                       break;
+               if (error > 0) {
+                       error -= 434550;
+                       set++;
+               } else {
+                       error += cal;
+                       freq--;
+               }
+       }
+       if (neg)
+               set = -set;
+       return cal + set;
+}
index ea18ff1..e53aa2c 100644 (file)
@@ -28,7 +28,8 @@ CORE_SRC = \
        ao_panic.c \
        ao_state.c \
        ao_stdio.c \
-       ao_task.c
+       ao_task.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_aes.c \
index cf33d1f..27e7d24 100644 (file)
@@ -31,7 +31,8 @@ CORE_SRC = \
        ao_rssi.c \
        ao_state.c \
        ao_stdio.c \
-       ao_task.c
+       ao_task.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_aes.c \
index 9776451..8aab50e 100644 (file)
@@ -27,7 +27,8 @@ CORE_SRC = \
        ao_panic.c \
        ao_stdio.c \
        ao_storage.c \
-       ao_task.c
+       ao_task.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_adc.c \
index 52c723c..e4f55f8 100644 (file)
@@ -40,7 +40,8 @@ CORE_SRC = \
        ao_log.c \
        ao_log_big.c \
        ao_report.c \
-       ao_telemetry.c
+       ao_telemetry.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_adc.c \
index 75beeae..82ec42a 100644 (file)
@@ -33,7 +33,8 @@ CORE_SRC = \
        ao_stdio.c \
        ao_storage.c \
        ao_task.c \
-       ao_telemetry.c
+       ao_telemetry.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_adc.c \
index b30ca78..cf27527 100644 (file)
@@ -33,7 +33,8 @@ CORE_SRC = \
        ao_stdio.c \
        ao_storage.c \
        ao_task.c \
-       ao_telemetry.c
+       ao_telemetry.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_adc.c \
index 05157cb..9b40c42 100644 (file)
@@ -459,7 +459,7 @@ ao_terraui_config(void) __reentrant
        
 
        for (chan = 0; chan < AO_NUM_CHANNELS; chan++)
-               if (ao_config.radio_channels[chan].radio_setting == ao_config.radio_setting)
+               if (ao_config.radio_channels[chan].kHz == ao_config.frequency)
                        break;
        if (chan == AO_NUM_CHANNELS)
                chan = 0;
index a21a605..4478047 100644 (file)
@@ -41,7 +41,8 @@ CORE_SRC = \
        ao_state.c \
        ao_storage.c \
        ao_stdio.c \
-       ao_task.c
+       ao_task.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_dbg.c \
index 0747bfb..4967d18 100644 (file)
@@ -28,7 +28,8 @@ CORE_SRC = \
        ao_sqrt.c \
        ao_stdio.c \
        ao_storage.c \
-       ao_task.c
+       ao_task.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_battery.c \
index 698d612..149b00c 100644 (file)
@@ -26,7 +26,8 @@ CORE_SRC = \
        ao_rssi.c \
        ao_state.c \
        ao_stdio.c \
-       ao_task.c
+       ao_task.c \
+       ao_freq.c
 
 CC1111_SRC = \
        ao_aes.c \