altos: Add the BT serial debug code back in, disabled
[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 #if HAS_BEEP
253         ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
254 #endif
255
256         /*
257          * The first time we connect, the BTM-180 comes up at 19200 baud.
258          * After that, it will remember and come up at 57600 baud. So, see
259          * if it is already running at 57600 baud, and if that doesn't work
260          * then tell it to switch to 57600 from 19200 baud.
261          */
262         while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
263                 ao_delay(AO_SEC_TO_TICKS(1));
264                 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
265                         ao_btm_cmd("ATL4\r");
266                 ao_delay(AO_SEC_TO_TICKS(1));
267         }
268
269         /* Disable echo */
270         ao_btm_cmd("ATE0\r");
271
272         /* Enable flow control */
273         ao_btm_cmd("ATC1\r");
274
275         /* Set the reported name to something we can find on the host */
276         ao_btm_set_name();
277
278         /* Turn off status reporting */
279         ao_btm_cmd("ATQ1\r");
280
281         ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
282                                     ao_serial_putchar,
283                                     NULL);
284         ao_btm_echo(0);
285
286         for (;;) {
287                 while (!ao_btm_connected)
288                         ao_sleep(&ao_btm_connected);
289                 while (ao_btm_connected) {
290                         ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
291                         ao_delay(AO_SEC_TO_TICKS(3));
292                 }
293         }
294 }
295
296 __xdata struct ao_task ao_btm_task;
297
298 #if BT_LINK_ON_P2
299 #define BT_PICTL_ICON   PICTL_P2ICON
300 #define BT_PIFG         P2IFG
301 #define BT_PDIR         P2DIR
302 #define BT_PINP         P2INP
303 #define BT_IEN2_PIE     IEN2_P2IE
304 #endif
305 #if BT_LINK_ON_P1
306 #define BT_PICTL_ICON   PICTL_P1ICON
307 #define BT_PIFG         P1IFG
308 #define BT_PDIR         P1DIR
309 #define BT_PINP         P1INP
310 #define BT_IEN2_PIE     IEN2_P1IE
311 #endif
312
313 void
314 ao_btm_check_link() __critical
315 {
316         /* Check the pin and configure the interrupt detector to wait for the
317          * pin to flip the other way
318          */
319         if (BT_LINK_PIN) {
320                 ao_btm_connected = 0;
321                 PICTL |= BT_PICTL_ICON;
322         } else {
323                 ao_btm_connected = 1;
324                 PICTL &= ~BT_PICTL_ICON;
325         }
326 }
327
328 void
329 ao_btm_isr(void)
330 #if BT_LINK_ON_P1
331         __interrupt 15
332 #endif
333 {
334 #if BT_LINK_ON_P1
335         P1IF = 0;
336 #endif
337         if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
338                 ao_btm_check_link();
339                 ao_wakeup(&ao_btm_connected);
340         }
341         BT_PIFG = 0;
342 }
343
344 void
345 ao_btm_init (void)
346 {
347         ao_serial_init();
348         ao_serial_set_speed(AO_SERIAL_SPEED_19200);
349
350 #if BT_LINK_ON_P1
351         /*
352          * Configure ser reset line
353          */
354
355         P1_6 = 0;
356         P1DIR |= (1 << 6);
357 #endif
358
359         /*
360          * Configure link status line
361          */
362
363         /* Set pin to input */
364         BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
365
366         /* Set pin to tri-state */
367         BT_PINP |= (1 << BT_LINK_PIN_INDEX);
368
369         /* Enable interrupts */
370         IEN2 |= BT_IEN2_PIE;
371
372         /* Check current pin state */
373         ao_btm_check_link();
374
375 #if BT_LINK_ON_P2
376         /* Eable the pin interrupt */
377         PICTL |= PICTL_P2IEN;
378 #endif
379 #if BT_LINK_ON_P1
380         /* Enable pin interrupt */
381         P1IEN |= (1 << BT_LINK_PIN_INDEX);
382 #endif
383
384         ao_add_task(&ao_btm_task, ao_btm, "bt");
385         ao_btm_log_init();
386 }