2 * Copyright © 2011 Keith Packard <keithp@keithp.com>
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.
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.
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.
20 uint8_t ao_btm_running;
22 __xdata uint8_t ao_btm_connected;
25 __xdata char ao_btm_buffer[1024];
29 uint8_t ao_btm_send_chars = 0;
32 ao_btm_putchar(char c);
35 ao_btm_add_char(char c)
37 if (ao_btm_ptr < sizeof (ao_btm_buffer))
38 ao_btm_buffer[ao_btm_ptr++] = c;
42 ao_btm_log_char(char c, char dir)
44 if (dir != ao_btm_dir) {
52 ao_btm_log_out_char(char c)
54 ao_btm_log_char(c, '>');
58 ao_btm_log_in_char(char c)
60 ao_btm_log_char(c, '<');
63 #define AO_BTM_MAX_REPLY 16
64 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
66 extern volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
69 * Read a line of data from the serial port, truncating
70 * it after a few characters.
76 uint8_t ao_btm_reply_len = 0;
81 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
82 ao_btm_log_in_char(c);
83 if (ao_btm_reply_len < sizeof (ao_btm_reply))
84 ao_btm_reply[ao_btm_reply_len++] = c;
85 if (c == '\r' || c == '\n')
88 for (c = 0; c < 10; c++) {
89 ao_delay(AO_MS_TO_TICKS(10));
90 if (!ao_fifo_empty(ao_usart1_rx_fifo))
97 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
98 ao_btm_reply[c++] = '\0';
99 return ao_btm_reply_len;
103 * Drain the serial port completely
108 while (ao_btm_get_line())
113 ao_btm_echo(uint8_t echo)
115 ao_stdios[ao_btm_stdio].echo = echo;
119 * A command line pre-processor to detect connect/disconnect messages
120 * and update the internal state
126 if (ao_cur_stdio != ao_btm_stdio)
129 while (ao_cmd_lex_c != '\n') {
130 if (ao_match_word("CONNECT"))
132 if (ao_match_word("DISCONNECT"))
134 if (ao_match_word("ERROR"))
136 if (ao_match_word("OK"))
141 return !ao_btm_connected;
145 * A wrapper for ao_serial_pollchar that
146 * doesn't return any characters while we're
147 * initializing the bluetooth device
150 ao_btm_pollchar(void)
154 return AO_READ_AGAIN;
155 c = ao_serial_pollchar();
156 if (c != AO_READ_AGAIN)
157 ao_btm_log_in_char(c);
162 ao_btm_putchar(char c)
164 if (!ao_btm_send_chars) {
165 ao_btm_log_out_char(c);
166 ao_serial_putchar(c);
171 ao_btm_stdio_putchar(char c) {
172 if (ao_btm_connected)
177 * Wait for the bluetooth device to return
178 * status from the previously executed command
181 ao_btm_wait_reply(void)
185 if (!strncmp(ao_btm_reply, "OK", 2))
187 if (!strncmp(ao_btm_reply, "ERROR", 5))
189 if (ao_btm_reply[0] == '\0')
195 ao_btm_string(__code char *cmd)
204 ao_btm_cmd(__code char *cmd)
208 return ao_btm_wait_reply();
212 ao_btm_set_name(void)
218 ao_btm_string("ATN=TeleBT-");
220 n = ao_serial_number;
226 return ao_btm_wait_reply();
230 ao_btm_try_speed(uint8_t speed)
232 ao_serial_set_speed(speed);
234 (void) ao_btm_cmd("\rATE0\rATQ0\r");
235 if (ao_btm_cmd("AT\r") == 1)
240 * A thread to initialize the bluetooth device and
241 * hang around to blink the LED when connected
246 ao_add_stdio(ao_btm_pollchar,
247 ao_btm_stdio_putchar,
249 ao_btm_stdio = ao_num_stdios - 1;
252 ao_delay(AO_SEC_TO_TICKS(3));
255 * The first time we connect, the BTM-180 comes up at 19200 baud.
256 * After that, it will remember and come up at 57600 baud. So, see
257 * if it is already running at 57600 baud, and if that doesn't work
258 * then tell it to switch to 57600 from 19200 baud.
260 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
261 ao_delay(AO_SEC_TO_TICKS(1));
262 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
263 ao_btm_cmd("ATL4\r");
264 ao_delay(AO_SEC_TO_TICKS(1));
268 ao_btm_cmd("ATE0\r");
270 /* Enable flow control */
271 ao_btm_cmd("ATC1\r");
273 /* Set the reported name to something we can find on the host */
276 /* Turn off status reporting */
277 ao_btm_cmd("ATQ1\r");
281 while (!ao_btm_connected && !ao_btm_chat)
282 ao_sleep(&ao_btm_connected);
285 while (ao_btm_chat) {
287 c = ao_serial_pollchar();
288 if (c != AO_READ_AGAIN) {
289 ao_btm_log_in_char(c);
293 ao_sleep(&ao_usart1_rx_fifo);
298 while (ao_btm_connected) {
299 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
300 ao_delay(AO_SEC_TO_TICKS(3));
305 __xdata struct ao_task ao_btm_task;
308 * Connect directly to the bluetooth device, mostly
317 ao_wakeup(&ao_btm_connected);
319 while ((c = ao_usb_getchar()) != '~') {
320 if (c == '\n') c = '\r';
324 while (!ao_btm_running) {
325 ao_wakeup(&ao_usart1_rx_fifo);
326 ao_delay(AO_MS_TO_TICKS(10));
331 * Dump everything received from the bluetooth device during startup
339 for (i = 0; i < ao_btm_ptr; i++) {
340 c = ao_btm_buffer[i];
341 if (c < ' ' && c != '\n')
342 printf("\\%03o", ((int) c) & 0xff);
344 putchar(ao_btm_buffer[i]);
353 if (ao_cmd_lex_u32 == 57600)
354 ao_serial_set_speed(AO_SERIAL_SPEED_57600);
355 else if (ao_cmd_lex_u32 == 19200)
356 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
358 ao_cmd_status = ao_cmd_syntax_error;
362 ao_btm_check_link() __critical
365 ao_btm_connected = 0;
366 PICTL |= PICTL_P2ICON;
368 ao_btm_connected = 1;
369 PICTL &= ~PICTL_P2ICON;
376 if (P2IFG & (1 << 1)) {
378 ao_wakeup(&ao_btm_connected);
383 __code struct ao_cmds ao_btm_cmds[] = {
384 { ao_btm_forward, "B\0BTM serial link." },
385 { ao_btm_dump, "d\0Dump btm buffer." },
386 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
394 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
397 * Configure link status line
400 /* Set P2_1 to input, pull-down */
402 P2INP |= P2INP_MDP2_1_TRISTATE;
404 /* Enable P2 interrupts */
407 PICTL |= PICTL_P2IEN;
409 ao_add_task(&ao_btm_task, ao_btm, "bt");
410 ao_cmd_register(&ao_btm_cmds[0]);