altos: Switch telemini from v0.1 to v1.0
[fw/altos] / src / ao_btm.c
index 491e4be3b6a367078d19213d4c9daf8521ceebd9..44155ec142aa934863daa384500726f503a3af09 100644 (file)
 
 #include "ao.h"
 
-uint8_t                        ao_btm_running;
 int8_t                 ao_btm_stdio;
 __xdata uint8_t                ao_btm_connected;
 
-void
-ao_btm_putchar(char c);
-
 #define AO_BTM_MAX_REPLY       16
 __xdata char           ao_btm_reply[AO_BTM_MAX_REPLY];
 
@@ -81,32 +77,6 @@ ao_btm_echo(uint8_t echo)
        ao_stdios[ao_btm_stdio].echo = echo;
 }
 
-/*
- * A command line pre-processor to detect connect/disconnect messages
- * and update the internal state
- */
-
-uint8_t
-ao_cmd_filter(void)
-{
-       if (ao_cur_stdio != ao_btm_stdio)
-               return 0;
-       ao_cmd_lex();
-       while (ao_cmd_lex_c != '\n') {
-               if (ao_match_word("CONNECT"))
-                       return 1;
-               if (ao_match_word("DISCONNECT"))
-                       return 1;
-               if (ao_match_word("ERROR"))
-                       return 1;
-               if (ao_match_word("OK"))
-                       return 1;
-               ao_cmd_lex();
-       }
-       ao_cmd_status = 0;
-       return 0;
-}
-
 /*
  * Delay between command charaters; the BT module
  * can't keep up with 57600 baud
@@ -196,6 +166,10 @@ ao_btm(void)
         */
        ao_delay(AO_SEC_TO_TICKS(3));
 
+#if HAS_BEEP
+       ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
+#endif
+
        /*
         * The first time we connect, the BTM-180 comes up at 19200 baud.
         * After that, it will remember and come up at 57600 baud. So, see
@@ -226,7 +200,6 @@ ao_btm(void)
                                    NULL);
        ao_btm_echo(0);
 
-       ao_btm_running = 1;
        for (;;) {
                while (!ao_btm_connected)
                        ao_sleep(&ao_btm_connected);
@@ -239,26 +212,50 @@ ao_btm(void)
 
 __xdata struct ao_task ao_btm_task;
 
+#if BT_LINK_ON_P2
+#define BT_PICTL_ICON  PICTL_P2ICON
+#define BT_PIFG                P2IFG
+#define BT_PDIR                P2DIR
+#define BT_PINP                P2INP
+#define BT_IEN2_PIE    IEN2_P2IE
+#endif
+#if BT_LINK_ON_P1
+#define BT_PICTL_ICON  PICTL_P1ICON
+#define BT_PIFG                P1IFG
+#define BT_PDIR                P1DIR
+#define BT_PINP                P1INP
+#define BT_IEN2_PIE    IEN2_P1IE
+#endif
+
 void
 ao_btm_check_link() __critical
 {
-       if (P2_1) {
+       /* Check the pin and configure the interrupt detector to wait for the
+        * pin to flip the other way
+        */
+       if (BT_LINK_PIN) {
                ao_btm_connected = 0;
-               PICTL |= PICTL_P2ICON;
+               PICTL |= BT_PICTL_ICON;
        } else {
                ao_btm_connected = 1;
-               PICTL &= ~PICTL_P2ICON;
+               PICTL &= ~BT_PICTL_ICON;
        }
 }
 
 void
 ao_btm_isr(void)
+#if BT_LINK_ON_P1
+       __interrupt 15
+#endif
 {
-       if (P2IFG & (1 << 1)) {
+#if BT_LINK_ON_P1
+       P1IF = 0;
+#endif
+       if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
                ao_btm_check_link();
                ao_wakeup(&ao_btm_connected);
        }
-       P2IFG = 0;
+       BT_PIFG = 0;
 }
 
 void
@@ -267,18 +264,39 @@ ao_btm_init (void)
        ao_serial_init();
        ao_serial_set_speed(AO_SERIAL_SPEED_19200);
 
+#if BT_LINK_ON_P1
+       /*
+        * Configure ser reset line
+        */
+
+       P1_6 = 0;
+       P1DIR |= (1 << 6);
+#endif
+
        /*
         * Configure link status line
         */
 
-       /* Set P2_1 to input, pull-down */
-       P2DIR &= ~(1 << 1);
-       P2INP |= P2INP_MDP2_1_TRISTATE;
+       /* Set pin to input */
+       BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
 
-       /* Enable P2 interrupts */
-       IEN2 |= IEN2_P2IE;
+       /* Set pin to tri-state */
+       BT_PINP |= (1 << BT_LINK_PIN_INDEX);
+
+       /* Enable interrupts */
+       IEN2 |= BT_IEN2_PIE;
+
+       /* Check current pin state */
        ao_btm_check_link();
+
+#if BT_LINK_ON_P2
+       /* Eable the pin interrupt */
        PICTL |= PICTL_P2IEN;
+#endif
+#if BT_LINK_ON_P1
+       /* Enable pin interrupt */
+       P1IEN |= (1 << BT_LINK_PIN_INDEX);
+#endif
 
        ao_add_task(&ao_btm_task, ao_btm, "bt");
 }