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