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; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
20 #ifdef AO_BTM_INT_PORT
24 #ifndef ao_serial_btm_getchar
25 #define ao_serial_btm_putchar ao_serial1_putchar
26 #define _ao_serial_btm_pollchar _ao_serial1_pollchar
27 #define _ao_serial_btm_sleep_for(timeout) ao_sleep_for((void *) &ao_serial1_rx_fifo, timeout)
28 #define ao_serial_btm_set_speed ao_serial1_set_speed
29 #define ao_serial_btm_drain ao_serial1_drain
33 __xdata uint8_t ao_btm_connected;
38 __xdata char ao_btm_buffer[256];
43 ao_btm_add_char(char c)
45 if (ao_btm_ptr < sizeof (ao_btm_buffer))
46 ao_btm_buffer[ao_btm_ptr++] = c;
50 ao_btm_log_char(char c, char dir)
52 if (dir != ao_btm_dir) {
60 ao_btm_log_out_char(char c)
62 ao_btm_log_char(c, '>');
66 ao_btm_log_in_char(char c)
68 ao_btm_log_char(c, '<');
72 * Dump everything received from the bluetooth device during startup
80 for (i = 0; i < ao_btm_ptr; i++) {
82 if (c < ' ' && c != '\n')
83 printf("\\%03o", ((int) c) & 0xff);
85 putchar(ao_btm_buffer[i]);
89 if (ao_cmd_status == ao_cmd_success && ao_cmd_lex_i)
91 ao_cmd_status = ao_cmd_success;
98 if (ao_cmd_lex_u32 == 57600)
99 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
100 else if (ao_cmd_lex_u32 == 19200)
101 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
103 ao_cmd_status = ao_cmd_syntax_error;
106 static uint8_t ao_btm_enable;
112 while (ao_btm_enable) {
113 ao_arch_block_interrupts();
114 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN && ao_btm_enable)
115 _ao_serial_btm_sleep_for(0);
116 ao_arch_release_interrupts();
117 if (c != AO_READ_AGAIN) {
125 static struct ao_task ao_btm_echo_task;
132 ao_add_task(&ao_btm_echo_task, ao_btm_do_echo, "btm-echo");
133 while ((c = getchar()) != '~') {
134 ao_serial_btm_putchar(c);
137 ao_wakeup((void *) &ao_serial_btm_rx_fifo);
140 __code struct ao_cmds ao_btm_cmds[] = {
141 { ao_btm_dump, "d\0Dump btm buffer." },
142 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
143 { ao_btm_send, "S\0BTM interactive mode. ~ to exit." },
147 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
150 #define ao_btm_log_in_char(c)
151 #define ao_btm_log_out_char(c)
152 #define ao_btm_log_init()
155 #define AO_BTM_MAX_REPLY 16
156 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
159 * Read one bluetooth character.
160 * Returns AO_READ_AGAIN if no character arrives within 10ms
168 ao_arch_block_interrupts();
169 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
170 c = _ao_serial_btm_sleep_for(AO_MS_TO_TICKS(10));
176 ao_arch_release_interrupts();
181 * Read a line of data from the serial port, truncating
182 * it after a few characters.
186 ao_btm_get_line(void)
188 uint8_t ao_btm_reply_len = 0;
192 while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
193 ao_btm_log_in_char(c);
194 if (ao_btm_reply_len < sizeof (ao_btm_reply))
195 ao_btm_reply[ao_btm_reply_len++] = c;
196 if (c == '\r' || c == '\n')
199 for (l = ao_btm_reply_len; l < sizeof (ao_btm_reply);)
200 ao_btm_reply[l++] = '\0';
201 return ao_btm_reply_len;
205 * Drain the serial port completely
210 while (ao_btm_get_line())
215 * Set the stdio echo for the bluetooth link
218 ao_btm_echo(uint8_t echo)
220 ao_stdios[ao_btm_stdio].echo = echo;
224 * Delay between command charaters; the BT module
225 * can't keep up with 57600 baud
229 ao_btm_putchar(char c)
231 ao_btm_log_out_char(c);
232 ao_serial_btm_putchar(c);
237 * Wait for the bluetooth device to return
238 * status from the previously executed command
241 ao_btm_wait_reply(void)
245 if (!strncmp(ao_btm_reply, "OK", 2))
247 if (!strncmp(ao_btm_reply, "ERROR", 5))
249 if (ao_btm_reply[0] == '\0')
255 ao_btm_string(__code char *cmd)
259 while ((c = *cmd++) != '\0')
264 ao_btm_cmd(__code char *cmd)
268 #ifdef AO_BTM_INT_PORT
269 /* Trust that AltosDroid will eventually disconnect and let us
270 * get things set up. The BTM module doesn't appear to listen
271 * for +++, so we have no way to force a disconnect.
273 while (ao_btm_connected)
274 ao_sleep(&ao_btm_connected);
277 return ao_btm_wait_reply();
281 ao_btm_set_name(void)
287 ao_btm_string("ATN=TeleBT-");
290 n = ao_serial_number;
296 return ao_btm_wait_reply();
300 ao_btm_try_speed(uint8_t speed)
302 ao_serial_btm_set_speed(speed);
303 ao_serial_btm_drain();
304 (void) ao_btm_cmd("\rATE0\rATQ0\r");
305 if (ao_btm_cmd("AT\r") == 1)
311 #define BT_PICTL_ICON PICTL_P2ICON
312 #define BT_PIFG P2IFG
313 #define BT_PDIR P2DIR
314 #define BT_PINP P2INP
315 #define BT_IEN2_PIE IEN2_P2IE
319 #define BT_PICTL_ICON PICTL_P1ICON
320 #define BT_PIFG P1IFG
321 #define BT_PDIR P1DIR
322 #define BT_PINP P1INP
323 #define BT_IEN2_PIE IEN2_P1IE
332 /* Check the pin and configure the interrupt detector to wait for the
333 * pin to flip the other way
336 ao_btm_connected = 0;
337 PICTL |= BT_PICTL_ICON;
339 ao_btm_connected = 1;
340 PICTL &= ~BT_PICTL_ICON;
344 ao_arch_block_interrupts();
345 if (ao_gpio_get(AO_BTM_INT_PORT, AO_BTM_INT_PIN, AO_BTM_INT) == 0) {
346 ao_btm_connected = 1;
348 ao_btm_connected = 0;
350 ao_arch_release_interrupts();
354 __xdata struct ao_task ao_btm_task;
357 * A thread to initialize the bluetooth device and
358 * hang around to blink the LED when connected
363 #ifdef AO_BTM_INT_PORT
364 ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
368 * Wait for the bluetooth device to boot
370 ao_delay(AO_SEC_TO_TICKS(3));
373 * The first time we connect, the BTM-180 comes up at 19200 baud.
374 * After that, it will remember and come up at 57600 baud. So, see
375 * if it is already running at 57600 baud, and if that doesn't work
376 * then tell it to switch to 57600 from 19200 baud.
378 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
379 ao_delay(AO_SEC_TO_TICKS(1));
380 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
381 ao_btm_cmd("ATL4\r");
382 ao_delay(AO_SEC_TO_TICKS(1));
386 ao_btm_cmd("ATE0\r");
388 /* Enable flow control */
389 ao_btm_cmd("ATC1\r");
391 /* Set the reported name to something we can find on the host */
394 /* Turn off status reporting */
395 ao_btm_cmd("ATQ1\r");
399 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
400 ao_serial_btm_putchar,
404 /* Check current pin state */
408 while (!ao_btm_connected)
409 ao_sleep(&ao_btm_connected);
410 while (ao_btm_connected) {
411 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
412 ao_delay(AO_SEC_TO_TICKS(3));
428 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
430 ao_wakeup(&ao_btm_connected);
436 #ifdef AO_BTM_INT_PORT
441 ao_wakeup(&ao_btm_connected);
450 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
452 #ifdef AO_BTM_RESET_PORT
453 ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,AO_BTM_RESET,0);
456 #ifdef AO_BTM_INT_PORT
457 ao_enable_port(AO_BTM_INT_PORT);
458 ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
459 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
466 * Configure ser reset line
474 * Configure link status line
477 /* Set pin to input */
478 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
480 /* Set pin to tri-state */
481 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
483 /* Enable interrupts */
488 /* Eable the pin interrupt */
489 PICTL |= PICTL_P2IEN;
492 /* Enable pin interrupt */
493 P1IEN |= (1 << BT_LINK_PIN_INDEX);
496 ao_add_task(&ao_btm_task, ao_btm, "bt");