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 8c5335c4bd73a86d9e9edb30a848f0cd18caa1e7..ce9a1f70e0ce4ff39673cb2eef6ea395730d023d 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 14cb7569b5df9b0e9840afc8d494cccd5f1386ba..cde68b3991ca062cc4117c1e6a2e96e1f01de035 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 86bbc473fd44f9bbfc8e08fa23aed61fb37c41ce..6fcebe1e8c4b9347183e77b8105ddc82071b3821 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 ea18ff1853aeacd63d9bf098531ab05ab451e62b..e53aa2c43e60f33232a8850ab21cdfbfb24220e0 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 cf33d1f1757ff188744fe297f44543b2ba6701aa..27e7d24be1b1a59448f374d97185e709d1731283 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 97764517cf828857fb5790ca8cf433e249053c18..8aab50e045891745d9b0fd8068a9b3bf58316e7f 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 52c723cabe27fe36ec99a6d384c6dbc41b0a09bb..e4f55f8872fe9423d2c5642ac64d76ef17a59882 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 75beeae480492381320b264dbd2bd89e04f244df..82ec42a2c0279ce0483ac54e9dc21472c0cbb939 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 b30ca78970ace6658c0c784e44e58ad8d3b18639..cf27527c1a110b45d882d5380d4d4857537fe3cd 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 05157cb18e06c13e8acbf777aae51e4cc6b965dc..9b40c42aedf5aed1bed5b6c931c5898aef41c252 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 a21a6052423e642da89eee6c7bd87b1fa6bc9804..4478047663daae2470609e845f28a894aa504c13 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 0747bfbf7a79982af9edffdc5b36b4734619645c..4967d1874722a54f44510f584de90acc51d28a02 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 698d612c799591b2132ee2e9f5d0ca4fe878acd0..149b00c54eb35487a54935c7ccac3261b83be365 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 \