491e4be3b6a367078d19213d4c9daf8521ceebd9
[fw/altos] / src / ao_btm.c
1 /*
2  * Copyright © 2011 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 #include "ao.h"
19
20 uint8_t                 ao_btm_running;
21 int8_t                  ao_btm_stdio;
22 __xdata uint8_t         ao_btm_connected;
23
24 void
25 ao_btm_putchar(char c);
26
27 #define AO_BTM_MAX_REPLY        16
28 __xdata char            ao_btm_reply[AO_BTM_MAX_REPLY];
29
30 extern volatile __xdata struct ao_fifo  ao_usart1_rx_fifo;
31
32 /*
33  * Read a line of data from the serial port, truncating
34  * it after a few characters.
35  */
36
37 uint8_t
38 ao_btm_get_line(void)
39 {
40         uint8_t ao_btm_reply_len = 0;
41         char c;
42
43         for (;;) {
44
45                 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
46                         if (ao_btm_reply_len < sizeof (ao_btm_reply))
47                                 ao_btm_reply[ao_btm_reply_len++] = c;
48                         if (c == '\r' || c == '\n')
49                                 goto done;
50                 }
51                 for (c = 0; c < 10; c++) {
52                         ao_delay(AO_MS_TO_TICKS(10));
53                         if (!ao_fifo_empty(ao_usart1_rx_fifo))
54                                 break;
55                 }
56                 if (c == 10)
57                         goto done;
58         }
59 done:
60         for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
61                 ao_btm_reply[c++] = '\0';
62         return ao_btm_reply_len;
63 }
64
65 /*
66  * Drain the serial port completely
67  */
68 void
69 ao_btm_drain()
70 {
71         while (ao_btm_get_line())
72                 ;
73 }
74
75 /*
76  * Set the stdio echo for the bluetooth link
77  */
78 void
79 ao_btm_echo(uint8_t echo)
80 {
81         ao_stdios[ao_btm_stdio].echo = echo;
82 }
83
84 /*
85  * A command line pre-processor to detect connect/disconnect messages
86  * and update the internal state
87  */
88
89 uint8_t
90 ao_cmd_filter(void)
91 {
92         if (ao_cur_stdio != ao_btm_stdio)
93                 return 0;
94         ao_cmd_lex();
95         while (ao_cmd_lex_c != '\n') {
96                 if (ao_match_word("CONNECT"))
97                         return 1;
98                 if (ao_match_word("DISCONNECT"))
99                         return 1;
100                 if (ao_match_word("ERROR"))
101                         return 1;
102                 if (ao_match_word("OK"))
103                         return 1;
104                 ao_cmd_lex();
105         }
106         ao_cmd_status = 0;
107         return 0;
108 }
109
110 /*
111  * Delay between command charaters; the BT module
112  * can't keep up with 57600 baud
113  */
114
115 void
116 ao_btm_putchar(char c)
117 {
118         ao_serial_putchar(c);
119         ao_delay(1);
120 }
121
122 /*
123  * Wait for the bluetooth device to return
124  * status from the previously executed command
125  */
126 uint8_t
127 ao_btm_wait_reply(void)
128 {
129         for (;;) {
130                 ao_btm_get_line();
131                 if (!strncmp(ao_btm_reply, "OK", 2))
132                         return 1;
133                 if (!strncmp(ao_btm_reply, "ERROR", 5))
134                         return -1;
135                 if (ao_btm_reply[0] == '\0')
136                         return 0;
137         }
138 }
139
140 void
141 ao_btm_string(__code char *cmd)
142 {
143         char    c;
144
145         while (c = *cmd++)
146                 ao_btm_putchar(c);
147 }
148
149 uint8_t
150 ao_btm_cmd(__code char *cmd)
151 {
152         ao_btm_drain();
153         ao_btm_string(cmd);
154         return ao_btm_wait_reply();
155 }
156
157 uint8_t
158 ao_btm_set_name(void)
159 {
160         char    sn[8];
161         char    *s = sn + 8;
162         char    c;
163         int     n;
164         ao_btm_string("ATN=TeleBT-");
165         *--s = '\0';
166         *--s = '\r';
167         n = ao_serial_number;
168         do {
169                 *--s = '0' + n % 10;
170         } while (n /= 10);
171         while ((c = *s++))
172                 ao_btm_putchar(c);
173         return ao_btm_wait_reply();
174 }
175
176 uint8_t
177 ao_btm_try_speed(uint8_t speed)
178 {
179         ao_serial_set_speed(speed);
180         ao_btm_drain();
181         (void) ao_btm_cmd("\rATE0\rATQ0\r");
182         if (ao_btm_cmd("AT\r") == 1)
183                 return 1;
184         return 0;
185 }
186
187 /*
188  * A thread to initialize the bluetooth device and
189  * hang around to blink the LED when connected
190  */
191 void
192 ao_btm(void)
193 {
194         /*
195          * Wait for the bluetooth device to boot
196          */
197         ao_delay(AO_SEC_TO_TICKS(3));
198
199         /*
200          * The first time we connect, the BTM-180 comes up at 19200 baud.
201          * After that, it will remember and come up at 57600 baud. So, see
202          * if it is already running at 57600 baud, and if that doesn't work
203          * then tell it to switch to 57600 from 19200 baud.
204          */
205         while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
206                 ao_delay(AO_SEC_TO_TICKS(1));
207                 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
208                         ao_btm_cmd("ATL4\r");
209                 ao_delay(AO_SEC_TO_TICKS(1));
210         }
211
212         /* Disable echo */
213         ao_btm_cmd("ATE0\r");
214
215         /* Enable flow control */
216         ao_btm_cmd("ATC1\r");
217
218         /* Set the reported name to something we can find on the host */
219         ao_btm_set_name();
220
221         /* Turn off status reporting */
222         ao_btm_cmd("ATQ1\r");
223
224         ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
225                                     ao_serial_putchar,
226                                     NULL);
227         ao_btm_echo(0);
228
229         ao_btm_running = 1;
230         for (;;) {
231                 while (!ao_btm_connected)
232                         ao_sleep(&ao_btm_connected);
233                 while (ao_btm_connected) {
234                         ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
235                         ao_delay(AO_SEC_TO_TICKS(3));
236                 }
237         }
238 }
239
240 __xdata struct ao_task ao_btm_task;
241
242 void
243 ao_btm_check_link() __critical
244 {
245         if (P2_1) {
246                 ao_btm_connected = 0;
247                 PICTL |= PICTL_P2ICON;
248         } else {
249                 ao_btm_connected = 1;
250                 PICTL &= ~PICTL_P2ICON;
251         }
252 }
253
254 void
255 ao_btm_isr(void)
256 {
257         if (P2IFG & (1 << 1)) {
258                 ao_btm_check_link();
259                 ao_wakeup(&ao_btm_connected);
260         }
261         P2IFG = 0;
262 }
263
264 void
265 ao_btm_init (void)
266 {
267         ao_serial_init();
268         ao_serial_set_speed(AO_SERIAL_SPEED_19200);
269
270         /*
271          * Configure link status line
272          */
273
274         /* Set P2_1 to input, pull-down */
275         P2DIR &= ~(1 << 1);
276         P2INP |= P2INP_MDP2_1_TRISTATE;
277
278         /* Enable P2 interrupts */
279         IEN2 |= IEN2_P2IE;
280         ao_btm_check_link();
281         PICTL |= PICTL_P2IEN;
282
283         ao_add_task(&ao_btm_task, ao_btm, "bt");
284 }