Merge remote-tracking branch 'uniarch/master' into multiarch
[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; 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 int8_t                  ao_btm_stdio;
21 __xdata uint8_t         ao_btm_connected;
22
23 #define BT_DEBUG 0
24
25 #if BT_DEBUG
26 __xdata char            ao_btm_buffer[256];
27 int                     ao_btm_ptr;
28 char                    ao_btm_dir;
29
30 static void
31 ao_btm_add_char(char c)
32 {
33         if (ao_btm_ptr < sizeof (ao_btm_buffer))
34                 ao_btm_buffer[ao_btm_ptr++] = c;
35 }
36
37 static void
38 ao_btm_log_char(char c, char dir)
39 {
40         if (dir != ao_btm_dir) {
41                 ao_btm_add_char(dir);
42                 ao_btm_dir = dir;
43         }
44         ao_btm_add_char(c);
45 }
46
47 static void
48 ao_btm_log_out_char(char c)
49 {
50         ao_btm_log_char(c, '>');
51 }
52
53 static void
54 ao_btm_log_in_char(char c)
55 {
56         ao_btm_log_char(c, '<');
57 }
58
59 /*
60  * Dump everything received from the bluetooth device during startup
61  */
62 static void
63 ao_btm_dump(void)
64 {
65         int i;
66         char c;
67
68         for (i = 0; i < ao_btm_ptr; i++) {
69                 c = ao_btm_buffer[i];
70                 if (c < ' ' && c != '\n')
71                         printf("\\%03o", ((int) c) & 0xff);
72                 else
73                         putchar(ao_btm_buffer[i]);
74         }
75         putchar('\n');
76 }
77
78 static void
79 ao_btm_speed(void)
80 {
81         ao_cmd_decimal();
82         if (ao_cmd_lex_u32 == 57600)
83                 ao_serial_set_speed(AO_SERIAL_SPEED_57600);
84         else if (ao_cmd_lex_u32 == 19200)
85                 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
86         else
87                 ao_cmd_status = ao_cmd_syntax_error;
88 }
89
90 __code struct ao_cmds ao_btm_cmds[] = {
91         { ao_btm_dump,          "d\0Dump btm buffer." },
92         { ao_btm_speed,         "s <19200,57600>\0Set btm serial speed." },
93         { 0, NULL },
94 };
95
96 #define ao_btm_log_init()       ao_cmd_register(&ao_btm_cmds[0])
97
98 #else
99 #define ao_btm_log_in_char(c)
100 #define ao_btm_log_out_char(c)
101 #define ao_btm_log_init()
102 #endif
103
104 #define AO_BTM_MAX_REPLY        16
105 __xdata char            ao_btm_reply[AO_BTM_MAX_REPLY];
106
107 extern volatile __xdata struct ao_fifo  ao_usart1_rx_fifo;
108
109 /*
110  * Read a line of data from the serial port, truncating
111  * it after a few characters.
112  */
113
114 uint8_t
115 ao_btm_get_line(void)
116 {
117         uint8_t ao_btm_reply_len = 0;
118         char c;
119
120         for (;;) {
121
122                 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
123                         ao_btm_log_in_char(c);
124                         if (ao_btm_reply_len < sizeof (ao_btm_reply))
125                                 ao_btm_reply[ao_btm_reply_len++] = c;
126                         if (c == '\r' || c == '\n')
127                                 goto done;
128                 }
129                 for (c = 0; c < 10; c++) {
130                         ao_delay(AO_MS_TO_TICKS(10));
131                         if (!ao_fifo_empty(ao_usart1_rx_fifo))
132                                 break;
133                 }
134                 if (c == 10)
135                         goto done;
136         }
137 done:
138         for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
139                 ao_btm_reply[c++] = '\0';
140         return ao_btm_reply_len;
141 }
142
143 /*
144  * Drain the serial port completely
145  */
146 void
147 ao_btm_drain()
148 {
149         while (ao_btm_get_line())
150                 ;
151 }
152
153 /*
154  * Set the stdio echo for the bluetooth link
155  */
156 void
157 ao_btm_echo(uint8_t echo)
158 {
159         ao_stdios[ao_btm_stdio].echo = echo;
160 }
161
162 /*
163  * Delay between command charaters; the BT module
164  * can't keep up with 57600 baud
165  */
166
167 void
168 ao_btm_putchar(char c)
169 {
170         ao_btm_log_out_char(c);
171         ao_serial_putchar(c);
172         ao_delay(1);
173 }
174
175 /*
176  * Wait for the bluetooth device to return
177  * status from the previously executed command
178  */
179 uint8_t
180 ao_btm_wait_reply(void)
181 {
182         for (;;) {
183                 ao_btm_get_line();
184                 if (!strncmp(ao_btm_reply, "OK", 2))
185                         return 1;
186                 if (!strncmp(ao_btm_reply, "ERROR", 5))
187                         return -1;
188                 if (ao_btm_reply[0] == '\0')
189                         return 0;
190         }
191 }
192
193 void
194 ao_btm_string(__code char *cmd)
195 {
196         char    c;
197
198         while (c = *cmd++)
199                 ao_btm_putchar(c);
200 }
201
202 uint8_t
203 ao_btm_cmd(__code char *cmd)
204 {
205         ao_btm_drain();
206         ao_btm_string(cmd);
207         return ao_btm_wait_reply();
208 }
209
210 uint8_t
211 ao_btm_set_name(void)
212 {
213         char    sn[8];
214         char    *s = sn + 8;
215         char    c;
216         int     n;
217         ao_btm_string("ATN=TeleBT-");
218         *--s = '\0';
219         *--s = '\r';
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 /*
241  * A thread to initialize the bluetooth device and
242  * hang around to blink the LED when connected
243  */
244 void
245 ao_btm(void)
246 {
247         /*
248          * Wait for the bluetooth device to boot
249          */
250         ao_delay(AO_SEC_TO_TICKS(3));
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                 ao_delay(AO_SEC_TO_TICKS(1));
260                 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
261                         ao_btm_cmd("ATL4\r");
262                 ao_delay(AO_SEC_TO_TICKS(1));
263         }
264
265         /* Disable echo */
266         ao_btm_cmd("ATE0\r");
267
268         /* Enable flow control */
269         ao_btm_cmd("ATC1\r");
270
271         /* Set the reported name to something we can find on the host */
272         ao_btm_set_name();
273
274         /* Turn off status reporting */
275         ao_btm_cmd("ATQ1\r");
276
277         ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
278                                     ao_serial_putchar,
279                                     NULL);
280         ao_btm_echo(0);
281
282         for (;;) {
283                 while (!ao_btm_connected)
284                         ao_sleep(&ao_btm_connected);
285                 while (ao_btm_connected) {
286                         ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
287                         ao_delay(AO_SEC_TO_TICKS(3));
288                 }
289         }
290 }
291
292 __xdata struct ao_task ao_btm_task;
293
294 #if BT_LINK_ON_P2
295 #define BT_PICTL_ICON   PICTL_P2ICON
296 #define BT_PIFG         P2IFG
297 #define BT_PDIR         P2DIR
298 #define BT_PINP         P2INP
299 #define BT_IEN2_PIE     IEN2_P2IE
300 #endif
301 #if BT_LINK_ON_P1
302 #define BT_PICTL_ICON   PICTL_P1ICON
303 #define BT_PIFG         P1IFG
304 #define BT_PDIR         P1DIR
305 #define BT_PINP         P1INP
306 #define BT_IEN2_PIE     IEN2_P1IE
307 #endif
308
309 void
310 ao_btm_check_link() __critical
311 {
312         /* Check the pin and configure the interrupt detector to wait for the
313          * pin to flip the other way
314          */
315         if (BT_LINK_PIN) {
316                 ao_btm_connected = 0;
317                 PICTL |= BT_PICTL_ICON;
318         } else {
319                 ao_btm_connected = 1;
320                 PICTL &= ~BT_PICTL_ICON;
321         }
322 }
323
324 void
325 ao_btm_isr(void)
326 #if BT_LINK_ON_P1
327         __interrupt 15
328 #endif
329 {
330 #if BT_LINK_ON_P1
331         P1IF = 0;
332 #endif
333         if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
334                 ao_btm_check_link();
335                 ao_wakeup(&ao_btm_connected);
336         }
337         BT_PIFG = 0;
338 }
339
340 void
341 ao_btm_init (void)
342 {
343         ao_serial_init();
344         ao_serial_set_speed(AO_SERIAL_SPEED_19200);
345
346 #if BT_LINK_ON_P1
347         /*
348          * Configure ser reset line
349          */
350
351         P1_6 = 0;
352         P1DIR |= (1 << 6);
353 #endif
354
355         /*
356          * Configure link status line
357          */
358
359         /* Set pin to input */
360         BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
361
362         /* Set pin to tri-state */
363         BT_PINP |= (1 << BT_LINK_PIN_INDEX);
364
365         /* Enable interrupts */
366         IEN2 |= BT_IEN2_PIE;
367
368         /* Check current pin state */
369         ao_btm_check_link();
370
371 #if BT_LINK_ON_P2
372         /* Eable the pin interrupt */
373         PICTL |= PICTL_P2IEN;
374 #endif
375 #if BT_LINK_ON_P1
376         /* Enable pin interrupt */
377         P1IEN |= (1 << BT_LINK_PIN_INDEX);
378 #endif
379
380         ao_add_task(&ao_btm_task, ao_btm, "bt");
381         ao_btm_log_init();
382 }