Switch from GPLv2 to GPLv2+
[fw/altos] / src / drivers / 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; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include "ao.h"
20 #ifdef AO_BTM_INT_PORT
21 #include <ao_exti.h>
22 #endif
23
24 #ifndef ao_serial_btm_getchar
25 #define ao_serial_btm_putchar   ao_serial1_putchar
26 #define _ao_serial_btm_pollchar _ao_serial1_pollchar
27 #define _ao_serial_btm_sleep_for(timeout)       ao_sleep_for((void *) &ao_serial1_rx_fifo, timeout)
28 #define ao_serial_btm_set_speed ao_serial1_set_speed
29 #define ao_serial_btm_drain     ao_serial1_drain
30 #endif
31
32 int8_t                  ao_btm_stdio;
33 __xdata uint8_t         ao_btm_connected;
34
35 #define BT_DEBUG 0
36
37 #if BT_DEBUG
38 __xdata char            ao_btm_buffer[256];
39 uint16_t                ao_btm_ptr;
40 char                    ao_btm_dir;
41
42 static void
43 ao_btm_add_char(char c)
44 {
45         if (ao_btm_ptr < sizeof (ao_btm_buffer))
46                 ao_btm_buffer[ao_btm_ptr++] = c;
47 }
48
49 static void
50 ao_btm_log_char(char c, char dir)
51 {
52         if (dir != ao_btm_dir) {
53                 ao_btm_add_char(dir);
54                 ao_btm_dir = dir;
55         }
56         ao_btm_add_char(c);
57 }
58
59 static void
60 ao_btm_log_out_char(char c)
61 {
62         ao_btm_log_char(c, '>');
63 }
64
65 static void
66 ao_btm_log_in_char(char c)
67 {
68         ao_btm_log_char(c, '<');
69 }
70
71 /*
72  * Dump everything received from the bluetooth device during startup
73  */
74 static void
75 ao_btm_dump(void)
76 {
77         int i;
78         char c;
79
80         for (i = 0; i < ao_btm_ptr; i++) {
81                 c = ao_btm_buffer[i];
82                 if (c < ' ' && c != '\n')
83                         printf("\\%03o", ((int) c) & 0xff);
84                 else
85                         putchar(ao_btm_buffer[i]);
86         }
87         putchar('\n');
88         ao_cmd_decimal();
89         if (ao_cmd_status == ao_cmd_success && ao_cmd_lex_i)
90                 ao_btm_ptr = 0;
91         ao_cmd_status = ao_cmd_success;
92 }
93
94 static void
95 ao_btm_speed(void)
96 {
97         ao_cmd_decimal();
98         if (ao_cmd_lex_u32 == 57600)
99                 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
100         else if (ao_cmd_lex_u32 == 19200)
101                 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
102         else
103                 ao_cmd_status = ao_cmd_syntax_error;
104 }
105
106 static uint8_t  ao_btm_enable;
107
108 static void
109 ao_btm_do_echo(void)
110 {
111         int     c;
112         while (ao_btm_enable) {
113                 ao_arch_block_interrupts();
114                 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN && ao_btm_enable)
115                         _ao_serial_btm_sleep_for(0);
116                 ao_arch_release_interrupts();
117                 if (c != AO_READ_AGAIN) {
118                         putchar(c);
119                         flush();
120                 }
121         }
122         ao_exit();
123 }
124
125 static struct ao_task ao_btm_echo_task;
126
127 static void
128 ao_btm_send(void)
129 {
130         int c;
131         ao_btm_enable = 1;
132         ao_add_task(&ao_btm_echo_task, ao_btm_do_echo, "btm-echo");
133         while ((c = getchar()) != '~') {
134                 ao_serial_btm_putchar(c);
135         }
136         ao_btm_enable = 0;
137         ao_wakeup((void *) &ao_serial_btm_rx_fifo);
138 }
139
140 __code struct ao_cmds ao_btm_cmds[] = {
141         { ao_btm_dump,          "d\0Dump btm buffer." },
142         { ao_btm_speed,         "s <19200,57600>\0Set btm serial speed." },
143         { ao_btm_send,          "S\0BTM interactive mode. ~ to exit." },
144         { 0, NULL },
145 };
146
147 #define ao_btm_log_init()       ao_cmd_register(&ao_btm_cmds[0])
148
149 #else
150 #define ao_btm_log_in_char(c)
151 #define ao_btm_log_out_char(c)
152 #define ao_btm_log_init()
153 #endif
154
155 #define AO_BTM_MAX_REPLY        16
156 __xdata char            ao_btm_reply[AO_BTM_MAX_REPLY];
157
158 /*
159  * Read one bluetooth character.
160  * Returns AO_READ_AGAIN if no character arrives within 10ms
161  */
162
163 static int
164 ao_btm_getchar(void)
165 {
166         int     c;
167
168         ao_arch_block_interrupts();
169         while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
170                 c = _ao_serial_btm_sleep_for(AO_MS_TO_TICKS(10));
171                 if (c) {
172                         c = AO_READ_AGAIN;
173                         break;
174                 }
175         }
176         ao_arch_release_interrupts();
177         return c;
178 }
179
180 /*
181  * Read a line of data from the serial port, truncating
182  * it after a few characters.
183  */
184
185 uint8_t
186 ao_btm_get_line(void)
187 {
188         uint8_t ao_btm_reply_len = 0;
189         int c;
190         uint8_t l;
191
192         while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
193                 ao_btm_log_in_char(c);
194                 if (ao_btm_reply_len < sizeof (ao_btm_reply))
195                         ao_btm_reply[ao_btm_reply_len++] = c;
196                 if (c == '\r' || c == '\n')
197                         break;
198         }
199         for (l = ao_btm_reply_len; l < sizeof (ao_btm_reply);)
200                 ao_btm_reply[l++] = '\0';
201         return ao_btm_reply_len;
202 }
203
204 /*
205  * Drain the serial port completely
206  */
207 void
208 ao_btm_drain()
209 {
210         while (ao_btm_get_line())
211                 ;
212 }
213
214 /*
215  * Set the stdio echo for the bluetooth link
216  */
217 void
218 ao_btm_echo(uint8_t echo)
219 {
220         ao_stdios[ao_btm_stdio].echo = echo;
221 }
222
223 /*
224  * Delay between command charaters; the BT module
225  * can't keep up with 57600 baud
226  */
227
228 void
229 ao_btm_putchar(char c)
230 {
231         ao_btm_log_out_char(c);
232         ao_serial_btm_putchar(c);
233         ao_delay(1);
234 }
235
236 /*
237  * Wait for the bluetooth device to return
238  * status from the previously executed command
239  */
240 uint8_t
241 ao_btm_wait_reply(void)
242 {
243         for (;;) {
244                 ao_btm_get_line();
245                 if (!strncmp(ao_btm_reply, "OK", 2))
246                         return 1;
247                 if (!strncmp(ao_btm_reply, "ERROR", 5))
248                         return -1;
249                 if (ao_btm_reply[0] == '\0')
250                         return 0;
251         }
252 }
253
254 void
255 ao_btm_string(__code char *cmd)
256 {
257         char    c;
258
259         while ((c = *cmd++) != '\0')
260                 ao_btm_putchar(c);
261 }
262
263 uint8_t
264 ao_btm_cmd(__code char *cmd)
265 {
266         ao_btm_drain();
267
268 #ifdef AO_BTM_INT_PORT
269         /* Trust that AltosDroid will eventually disconnect and let us
270          * get things set up. The BTM module doesn't appear to listen
271          * for +++, so we have no way to force a disconnect.
272          */
273         while (ao_btm_connected)
274                 ao_sleep(&ao_btm_connected);
275 #endif
276         ao_btm_string(cmd);
277         return ao_btm_wait_reply();
278 }
279
280 uint8_t
281 ao_btm_set_name(void)
282 {
283         char    sn[8];
284         char    *s = sn + 8;
285         char    c;
286         int     n;
287         ao_btm_string("ATN=TeleBT-");
288         *--s = '\0';
289         *--s = '\r';
290         n = ao_serial_number;
291         do {
292                 *--s = '0' + n % 10;
293         } while (n /= 10);
294         while ((c = *s++))
295                 ao_btm_putchar(c);
296         return ao_btm_wait_reply();
297 }
298
299 uint8_t
300 ao_btm_try_speed(uint8_t speed)
301 {
302         ao_serial_btm_set_speed(speed);
303         ao_serial_btm_drain();
304         (void) ao_btm_cmd("\rATE0\rATQ0\r");
305         if (ao_btm_cmd("AT\r") == 1)
306                 return 1;
307         return 0;
308 }
309
310 #if BT_LINK_ON_P2
311 #define BT_PICTL_ICON   PICTL_P2ICON
312 #define BT_PIFG         P2IFG
313 #define BT_PDIR         P2DIR
314 #define BT_PINP         P2INP
315 #define BT_IEN2_PIE     IEN2_P2IE
316 #define BT_CC1111       1
317 #endif
318 #if BT_LINK_ON_P1
319 #define BT_PICTL_ICON   PICTL_P1ICON
320 #define BT_PIFG         P1IFG
321 #define BT_PDIR         P1DIR
322 #define BT_PINP         P1INP
323 #define BT_IEN2_PIE     IEN2_P1IE
324 #define BT_CC1111       1
325 #endif
326
327 void
328 ao_btm_check_link()
329 {
330 #if BT_CC1111
331         ao_arch_critical(
332                 /* Check the pin and configure the interrupt detector to wait for the
333                  * pin to flip the other way
334                  */
335                 if (BT_LINK_PIN) {
336                         ao_btm_connected = 0;
337                         PICTL |= BT_PICTL_ICON;
338                 } else {
339                         ao_btm_connected = 1;
340                         PICTL &= ~BT_PICTL_ICON;
341                 }
342                 );
343 #else
344         ao_arch_block_interrupts();
345         if (ao_gpio_get(AO_BTM_INT_PORT, AO_BTM_INT_PIN, AO_BTM_INT) == 0) {
346                 ao_btm_connected = 1;
347         } else {
348                 ao_btm_connected = 0;
349         }
350         ao_arch_release_interrupts();
351 #endif
352 }
353
354 __xdata struct ao_task ao_btm_task;
355
356 /*
357  * A thread to initialize the bluetooth device and
358  * hang around to blink the LED when connected
359  */
360 void
361 ao_btm(void)
362 {
363 #ifdef AO_BTM_INT_PORT
364         ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
365 #endif
366
367         /*
368          * Wait for the bluetooth device to boot
369          */
370         ao_delay(AO_SEC_TO_TICKS(3));
371
372         /*
373          * The first time we connect, the BTM-180 comes up at 19200 baud.
374          * After that, it will remember and come up at 57600 baud. So, see
375          * if it is already running at 57600 baud, and if that doesn't work
376          * then tell it to switch to 57600 from 19200 baud.
377          */
378         while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
379                 ao_delay(AO_SEC_TO_TICKS(1));
380                 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
381                         ao_btm_cmd("ATL4\r");
382                 ao_delay(AO_SEC_TO_TICKS(1));
383         }
384
385         /* Disable echo */
386         ao_btm_cmd("ATE0\r");
387
388         /* Enable flow control */
389         ao_btm_cmd("ATC1\r");
390
391         /* Set the reported name to something we can find on the host */
392         ao_btm_set_name();
393
394         /* Turn off status reporting */
395         ao_btm_cmd("ATQ1\r");
396
397         ao_btm_drain();
398
399         ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
400                                     ao_serial_btm_putchar,
401                                     NULL);
402         ao_btm_echo(0);
403
404         /* Check current pin state */
405         ao_btm_check_link();
406
407         for (;;) {
408                 while (!ao_btm_connected)
409                         ao_sleep(&ao_btm_connected);
410                 while (ao_btm_connected) {
411                         ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
412                         ao_delay(AO_SEC_TO_TICKS(3));
413                 }
414         }
415 }
416
417
418 #if BT_CC1111
419 void
420 ao_btm_isr(void)
421 #if BT_LINK_ON_P1
422         __interrupt 15
423 #endif
424 {
425 #if BT_LINK_ON_P1
426         P1IF = 0;
427 #endif
428         if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
429                 ao_btm_check_link();
430                 ao_wakeup(&ao_btm_connected);
431         }
432         BT_PIFG = 0;
433 }
434 #endif
435
436 #ifdef AO_BTM_INT_PORT
437 void
438 ao_btm_isr(void)
439 {
440         ao_btm_check_link();
441         ao_wakeup(&ao_btm_connected);
442 }
443 #endif
444
445 void
446 ao_btm_init (void)
447 {
448         ao_serial_init();
449
450         ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
451
452 #ifdef AO_BTM_RESET_PORT
453         ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,AO_BTM_RESET,0);
454 #endif
455
456 #ifdef AO_BTM_INT_PORT
457         ao_enable_port(AO_BTM_INT_PORT);
458         ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
459                       AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
460                       ao_btm_isr);
461 #endif
462
463 #if BT_CC1111
464 #if BT_LINK_ON_P1
465         /*
466          * Configure ser reset line
467          */
468
469         P1_6 = 0;
470         P1DIR |= (1 << 6);
471 #endif
472
473         /*
474          * Configure link status line
475          */
476
477         /* Set pin to input */
478         BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
479
480         /* Set pin to tri-state */
481         BT_PINP |= (1 << BT_LINK_PIN_INDEX);
482
483         /* Enable interrupts */
484         IEN2 |= BT_IEN2_PIE;
485 #endif
486
487 #if BT_LINK_ON_P2
488         /* Eable the pin interrupt */
489         PICTL |= PICTL_P2IEN;
490 #endif
491 #if BT_LINK_ON_P1
492         /* Enable pin interrupt */
493         P1IEN |= (1 << BT_LINK_PIN_INDEX);
494 #endif
495
496         ao_add_task(&ao_btm_task, ao_btm, "bt");
497         ao_btm_log_init();
498 }