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