altos: Remove bt debug command
[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 __xdata char            ao_btm_buffer[1024];
25 int                     ao_btm_ptr;
26 char                    ao_btm_dir;
27
28 void
29 ao_btm_putchar(char c);
30
31 static void
32 ao_btm_add_char(char c)
33 {
34         if (ao_btm_ptr < sizeof (ao_btm_buffer))
35                 ao_btm_buffer[ao_btm_ptr++] = c;
36 }
37
38 static void
39 ao_btm_log_char(char c, char dir)
40 {
41         if (dir != ao_btm_dir) {
42                 ao_btm_add_char(dir);
43                 ao_btm_dir = dir;
44         }
45         ao_btm_add_char(c);
46 }
47
48 static void
49 ao_btm_log_out_char(char c)
50 {
51         ao_btm_log_char(c, '>');
52 }
53
54 static void
55 ao_btm_log_in_char(char c)
56 {
57         ao_btm_log_char(c, '<');
58 }
59
60 #define AO_BTM_MAX_REPLY        16
61 __xdata char            ao_btm_reply[AO_BTM_MAX_REPLY];
62
63 extern volatile __xdata struct ao_fifo  ao_usart1_rx_fifo;
64
65 /*
66  * Read a line of data from the serial port, truncating
67  * it after a few characters.
68  */
69
70 uint8_t
71 ao_btm_get_line(void)
72 {
73         uint8_t ao_btm_reply_len = 0;
74         char c;
75
76         for (;;) {
77
78                 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
79                         ao_btm_log_in_char(c);
80                         if (ao_btm_reply_len < sizeof (ao_btm_reply))
81                                 ao_btm_reply[ao_btm_reply_len++] = c;
82                         if (c == '\r' || c == '\n')
83                                 goto done;
84                 }
85                 for (c = 0; c < 10; c++) {
86                         ao_delay(AO_MS_TO_TICKS(10));
87                         if (!ao_fifo_empty(ao_usart1_rx_fifo))
88                                 break;
89                 }
90                 if (c == 10)
91                         goto done;
92         }
93 done:
94         for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
95                 ao_btm_reply[c++] = '\0';
96         return ao_btm_reply_len;
97 }
98
99 /*
100  * Drain the serial port completely
101  */
102 void
103 ao_btm_drain()
104 {
105         while (ao_btm_get_line())
106                 ;
107 }
108
109 /*
110  * Set the stdio echo for the bluetooth link
111  */
112 void
113 ao_btm_echo(uint8_t echo)
114 {
115         ao_stdios[ao_btm_stdio].echo = echo;
116 }
117
118 /*
119  * A command line pre-processor to detect connect/disconnect messages
120  * and update the internal state
121  */
122
123 uint8_t
124 ao_cmd_filter(void)
125 {
126         if (ao_cur_stdio != ao_btm_stdio)
127                 return 0;
128         ao_cmd_lex();
129         while (ao_cmd_lex_c != '\n') {
130                 if (ao_match_word("CONNECT"))
131                         return 1;
132                 if (ao_match_word("DISCONNECT"))
133                         return 1;
134                 if (ao_match_word("ERROR"))
135                         return 1;
136                 if (ao_match_word("OK"))
137                         return 1;
138                 ao_cmd_lex();
139         }
140         ao_cmd_status = 0;
141         return 0;
142 }
143
144 /*
145  * Delay between command charaters; the BT module
146  * can't keep up with 57600 baud
147  */
148
149 void
150 ao_btm_putchar(char c)
151 {
152         ao_btm_log_out_char(c);
153         ao_serial_putchar(c);
154         ao_delay(1);
155 }
156
157 /*
158  * Wait for the bluetooth device to return
159  * status from the previously executed command
160  */
161 uint8_t
162 ao_btm_wait_reply(void)
163 {
164         for (;;) {
165                 ao_btm_get_line();
166                 if (!strncmp(ao_btm_reply, "OK", 2))
167                         return 1;
168                 if (!strncmp(ao_btm_reply, "ERROR", 5))
169                         return -1;
170                 if (ao_btm_reply[0] == '\0')
171                         return 0;
172         }
173 }
174
175 void
176 ao_btm_string(__code char *cmd)
177 {
178         char    c;
179
180         while (c = *cmd++)
181                 ao_btm_putchar(c);
182 }
183
184 uint8_t
185 ao_btm_cmd(__code char *cmd)
186 {
187         ao_btm_drain();
188         ao_btm_string(cmd);
189         return ao_btm_wait_reply();
190 }
191
192 uint8_t
193 ao_btm_set_name(void)
194 {
195         char    sn[8];
196         char    *s = sn + 8;
197         char    c;
198         int     n;
199         ao_btm_string("ATN=TeleBT-");
200         *--s = '\0';
201         *--s = '\r';
202         n = ao_serial_number;
203         do {
204                 *--s = '0' + n % 10;
205         } while (n /= 10);
206         while ((c = *s++))
207                 ao_btm_putchar(c);
208         return ao_btm_wait_reply();
209 }
210
211 uint8_t
212 ao_btm_try_speed(uint8_t speed)
213 {
214         ao_serial_set_speed(speed);
215         ao_btm_drain();
216         (void) ao_btm_cmd("\rATE0\rATQ0\r");
217         if (ao_btm_cmd("AT\r") == 1)
218                 return 1;
219         return 0;
220 }
221
222 /*
223  * A thread to initialize the bluetooth device and
224  * hang around to blink the LED when connected
225  */
226 void
227 ao_btm(void)
228 {
229         /*
230          * Wait for the bluetooth device to boot
231          */
232         ao_delay(AO_SEC_TO_TICKS(3));
233
234         /*
235          * The first time we connect, the BTM-180 comes up at 19200 baud.
236          * After that, it will remember and come up at 57600 baud. So, see
237          * if it is already running at 57600 baud, and if that doesn't work
238          * then tell it to switch to 57600 from 19200 baud.
239          */
240         while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
241                 ao_delay(AO_SEC_TO_TICKS(1));
242                 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
243                         ao_btm_cmd("ATL4\r");
244                 ao_delay(AO_SEC_TO_TICKS(1));
245         }
246
247         /* Disable echo */
248         ao_btm_cmd("ATE0\r");
249
250         /* Enable flow control */
251         ao_btm_cmd("ATC1\r");
252
253         /* Set the reported name to something we can find on the host */
254         ao_btm_set_name();
255
256         /* Turn off status reporting */
257         ao_btm_cmd("ATQ1\r");
258
259         ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
260                                     ao_serial_putchar,
261                                     NULL);
262         ao_btm_echo(0);
263
264         ao_btm_running = 1;
265         for (;;) {
266                 while (!ao_btm_connected)
267                         ao_sleep(&ao_btm_connected);
268                 while (ao_btm_connected) {
269                         ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
270                         ao_delay(AO_SEC_TO_TICKS(3));
271                 }
272         }
273 }
274
275 __xdata struct ao_task ao_btm_task;
276
277 /*
278  * Dump everything received from the bluetooth device during startup
279  */
280 static void
281 ao_btm_dump(void)
282 {
283         int i;
284         char c;
285
286         for (i = 0; i < ao_btm_ptr; i++) {
287                 c = ao_btm_buffer[i];
288                 if (c < ' ' && c != '\n')
289                         printf("\\%03o", ((int) c) & 0xff);
290                 else
291                         putchar(ao_btm_buffer[i]);
292         }
293         putchar('\n');
294 }
295
296 static void
297 ao_btm_speed(void)
298 {
299         ao_cmd_decimal();
300         if (ao_cmd_lex_u32 == 57600)
301                 ao_serial_set_speed(AO_SERIAL_SPEED_57600);
302         else if (ao_cmd_lex_u32 == 19200)
303                 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
304         else
305                 ao_cmd_status = ao_cmd_syntax_error;
306 }
307
308 void
309 ao_btm_check_link() __critical
310 {
311         if (P2_1) {
312                 ao_btm_connected = 0;
313                 PICTL |= PICTL_P2ICON;
314         } else {
315                 ao_btm_connected = 1;
316                 PICTL &= ~PICTL_P2ICON;
317         }
318 }
319
320 void
321 ao_btm_isr(void)
322 {
323         if (P2IFG & (1 << 1)) {
324                 ao_btm_check_link();
325                 ao_wakeup(&ao_btm_connected);
326         }
327         P2IFG = 0;
328 }
329
330 __code struct ao_cmds ao_btm_cmds[] = {
331         { ao_btm_dump,          "d\0Dump btm buffer." },
332         { ao_btm_speed,         "s <19200,57600>\0Set btm serial speed." },
333         { 0, NULL },
334 };
335
336 void
337 ao_btm_init (void)
338 {
339         ao_serial_init();
340         ao_serial_set_speed(AO_SERIAL_SPEED_19200);
341
342         /*
343          * Configure link status line
344          */
345
346         /* Set P2_1 to input, pull-down */
347         P2DIR &= ~(1 << 1);
348         P2INP |= P2INP_MDP2_1_TRISTATE;
349
350         /* Enable P2 interrupts */
351         IEN2 |= IEN2_P2IE;
352         ao_btm_check_link();
353         PICTL |= PICTL_P2IEN;
354
355         ao_add_task(&ao_btm_task, ao_btm, "bt");
356         ao_cmd_register(&ao_btm_cmds[0]);
357 }