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