first cut at turnon scripts for EasyTimer v2
[fw/altos] / src / drivers / ao_packet_master.c
index e97a66488ce11116f9f11694d38b68ca359abc47..688ab6db557db23a3140b329c409ac8647ea1bfd 100644 (file)
@@ -3,7 +3,8 @@
  *
  * 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.
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #include "ao.h"
 
-static char
-ao_packet_getchar(void) __critical
+static int
+ao_packet_getchar(void)
 {
-       char c;
-       while ((c = ao_packet_pollchar()) == AO_READ_AGAIN) {
+       int c;
+
+       /* No need to block interrupts in this function as
+        * all packet variables are only modified from task
+        * context, not an interrupt handler
+        */
+       while ((c = _ao_packet_pollchar()) == AO_READ_AGAIN) {
                if (!ao_packet_enable)
                        break;
                if (ao_packet_master_sleeping)
@@ -33,25 +39,31 @@ ao_packet_getchar(void) __critical
 }
 
 static void
-ao_packet_echo(void) __reentrant
+ao_packet_echo(void) 
 {
-       char    c;
+       int     c;
        while (ao_packet_enable) {
                c = ao_packet_getchar();
                if (c != AO_READ_AGAIN)
-                       putchar(c);
+                       putchar((char) c);
        }
        ao_exit();
 }
 
-static __xdata struct ao_task  ao_packet_echo_task;
-static __xdata uint16_t                ao_packet_master_delay;
-static __xdata uint16_t                ao_packet_master_time;
+static struct ao_task  ao_packet_echo_task;
+static AO_TICK_TYPE    ao_packet_master_delay;
+static AO_TICK_TYPE    ao_packet_master_time;
 
 #define AO_PACKET_MASTER_DELAY_SHORT   AO_MS_TO_TICKS(100)
 #define AO_PACKET_MASTER_DELAY_LONG    AO_MS_TO_TICKS(1000)
 #define AO_PACKET_MASTER_DELAY_TIMEOUT AO_MS_TO_TICKS(2000)
 
+#if HAS_RADIO_RATE
+#define AO_PACKET_MASTER_RECV_DELAY    AO_MS_TO_TICKS(100) << (ao_config.radio_rate << 1)
+#else
+#define AO_PACKET_MASTER_RECV_DELAY    AO_MS_TO_TICKS(100)
+#endif
+
 static void
 ao_packet_master_busy(void)
 {
@@ -62,33 +74,31 @@ ao_packet_master_busy(void)
 static void
 ao_packet_master_check_busy(void)
 {
-       int16_t idle;
+       AO_TICK_SIGNED  idle;
        if (ao_packet_master_delay != AO_PACKET_MASTER_DELAY_SHORT)
                return;
-       idle = (int16_t) (ao_time() - ao_packet_master_time);
+       idle = (AO_TICK_SIGNED) (ao_time() - ao_packet_master_time);
 
        if (idle > AO_PACKET_MASTER_DELAY_TIMEOUT)
                ao_packet_master_delay = AO_PACKET_MASTER_DELAY_LONG;
 }
 
-void
+static void
 ao_packet_master(void)
 {
        ao_config_get();
-       ao_tx_packet.addr = ao_serial_number;
+       ao_tx_packet.addr = (uint8_t) ao_serial_number;
        ao_tx_packet.len = AO_PACKET_SYN;
        ao_packet_master_time = ao_time();
        ao_packet_master_delay = AO_PACKET_MASTER_DELAY_SHORT;
        while (ao_packet_enable) {
                uint8_t r;
-               ao_xmemcpy(ao_tx_packet.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
+               memcpy(ao_tx_packet.callsign, ao_config.callsign, AO_MAX_CALLSIGN);
                ao_packet_send();
                if (ao_tx_packet.len)
                        ao_packet_master_busy();
                ao_packet_master_check_busy();
-               ao_alarm(ao_packet_master_delay);
-               r = ao_packet_recv();
-               ao_clear_alarm();
+               r = ao_packet_recv(AO_PACKET_MASTER_RECV_DELAY);
                if (r) {
                        /* if we can transmit data, do so */
                        if (ao_packet_tx_used && ao_tx_packet.len == 0)
@@ -96,9 +106,7 @@ ao_packet_master(void)
                        if (ao_rx_packet.packet.len)
                                ao_packet_master_busy();
                        ao_packet_master_sleeping = 1;
-                       ao_alarm(ao_packet_master_delay);
-                       ao_sleep(&ao_packet_master_sleeping);
-                       ao_clear_alarm();
+                       ao_sleep_for(&ao_packet_master_sleeping, ao_packet_master_delay);
                        ao_packet_master_sleeping = 0;
                }
        }
@@ -106,10 +114,11 @@ ao_packet_master(void)
 }
 
 static void
-ao_packet_forward(void) __reentrant
+ao_packet_forward(void) 
 {
        char c;
        ao_packet_enable = 1;
+       ao_packet_tx_used = 0;
        ao_cmd_white();
 
        flush();
@@ -140,10 +149,10 @@ ao_packet_forward(void) __reentrant
 static void
 ao_packet_signal(void)
 {
-       printf ("RSSI: %d\n", AO_RSSI_FROM_RADIO(ao_packet_last_rssi));
+       printf ("RSSI: %d\n", ao_radio_rssi);
 }
 
-__code struct ao_cmds ao_packet_master_cmds[] = {
+const struct ao_cmds ao_packet_master_cmds[] = {
        { ao_packet_forward,    "p\0Remote packet link." },
        { ao_packet_signal,     "s\0Report signal strength." },
        { 0,    NULL },