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 uint8_t ao_btm_connected;
38 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
81 for (i = 0; i < ao_btm_ptr; i++) {
83 if (c < ' ' && c != '\n')
84 printf("\\%03o", ((int) c) & 0xff);
86 putchar(ao_btm_buffer[i]);
90 if (ao_cmd_status == ao_cmd_success && r)
92 ao_cmd_status = ao_cmd_success;
98 switch (ao_cmd_decimal()) {
100 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
103 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
106 ao_cmd_status = ao_cmd_syntax_error;
111 static uint8_t ao_btm_enable;
117 while (ao_btm_enable) {
118 ao_arch_block_interrupts();
119 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN && ao_btm_enable)
120 _ao_serial_btm_sleep_for(0);
121 ao_arch_release_interrupts();
122 if (c != AO_READ_AGAIN) {
130 static struct ao_task ao_btm_echo_task;
137 ao_add_task(&ao_btm_echo_task, ao_btm_do_echo, "btm-echo");
138 while ((c = getchar()) != '~') {
139 ao_serial_btm_putchar(c);
142 ao_wakeup((void *) &ao_serial_btm_rx_fifo);
145 const struct ao_cmds ao_btm_cmds[] = {
146 { ao_btm_dump, "d\0Dump btm buffer." },
147 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
148 { ao_btm_send, "S\0BTM interactive mode. ~ to exit." },
152 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
155 #define ao_btm_log_in_char(c)
156 #define ao_btm_log_out_char(c)
157 #define ao_btm_log_init()
160 #define AO_BTM_MAX_REPLY 16
161 char ao_btm_reply[AO_BTM_MAX_REPLY];
164 * Read one bluetooth character.
165 * Returns AO_READ_AGAIN if no character arrives within 10ms
173 ao_arch_block_interrupts();
174 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
175 c = _ao_serial_btm_sleep_for(AO_MS_TO_TICKS(10));
181 ao_arch_release_interrupts();
186 * Read a line of data from the serial port, truncating
187 * it after a few characters.
191 ao_btm_get_line(void)
193 uint8_t ao_btm_reply_len = 0;
197 while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
198 ao_btm_log_in_char(c);
199 if (ao_btm_reply_len < sizeof (ao_btm_reply))
200 ao_btm_reply[ao_btm_reply_len++] = (char) c;
201 if (c == '\r' || c == '\n')
204 for (l = ao_btm_reply_len; l < sizeof (ao_btm_reply);)
205 ao_btm_reply[l++] = '\0';
206 return ao_btm_reply_len;
210 * Drain the serial port completely
215 while (ao_btm_get_line())
220 * Set the stdio echo for the bluetooth link
223 ao_btm_echo(uint8_t echo)
225 ao_stdios[ao_btm_stdio].echo = echo;
229 * Delay between command charaters; the BT module
230 * can't keep up with 57600 baud
234 ao_btm_putchar(char c)
236 ao_btm_log_out_char(c);
237 ao_serial_btm_putchar(c);
242 * Wait for the bluetooth device to return
243 * status from the previously executed command
246 ao_btm_wait_reply(void)
250 if (!strncmp(ao_btm_reply, "OK", 2))
252 if (!strncmp(ao_btm_reply, "ERROR", 5))
254 if (ao_btm_reply[0] == '\0')
260 ao_btm_string(const char *cmd)
264 while ((c = *cmd++) != '\0')
269 ao_btm_cmd(const char *cmd)
273 #ifdef AO_BTM_INT_PORT
274 /* Trust that AltosDroid will eventually disconnect and let us
275 * get things set up. The BTM module doesn't appear to listen
276 * for +++, so we have no way to force a disconnect.
278 while (ao_btm_connected)
279 ao_sleep(&ao_btm_connected);
282 return ao_btm_wait_reply();
286 ao_btm_set_name(void)
292 ao_btm_string("ATN=TeleBT-");
295 n = ao_serial_number;
297 *--s = (uint8_t) ('0' + n % 10);
301 return ao_btm_wait_reply();
305 ao_btm_try_speed(uint8_t speed)
307 ao_serial_btm_set_speed(speed);
308 ao_serial_btm_drain();
309 (void) ao_btm_cmd("\rATE0\rATQ0\r");
310 if (ao_btm_cmd("AT\r") == 1)
316 #define BT_PICTL_ICON PICTL_P2ICON
317 #define BT_PIFG P2IFG
318 #define BT_PDIR P2DIR
319 #define BT_PINP P2INP
320 #define BT_IEN2_PIE IEN2_P2IE
324 #define BT_PICTL_ICON PICTL_P1ICON
325 #define BT_PIFG P1IFG
326 #define BT_PDIR P1DIR
327 #define BT_PINP P1INP
328 #define BT_IEN2_PIE IEN2_P1IE
333 ao_btm_check_link(void)
337 /* Check the pin and configure the interrupt detector to wait for the
338 * pin to flip the other way
341 ao_btm_connected = 0;
342 PICTL |= BT_PICTL_ICON;
344 ao_btm_connected = 1;
345 PICTL &= ~BT_PICTL_ICON;
349 ao_arch_block_interrupts();
350 if (ao_gpio_get(AO_BTM_INT_PORT, AO_BTM_INT_PIN) == 0) {
351 ao_btm_connected = 1;
353 ao_btm_connected = 0;
355 ao_arch_release_interrupts();
359 struct ao_task ao_btm_task;
362 * A thread to initialize the bluetooth device and
363 * hang around to blink the LED when connected
368 #ifdef AO_BTM_INT_PORT
369 ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
373 * Wait for the bluetooth device to boot
375 ao_delay(AO_SEC_TO_TICKS(3));
378 * The first time we connect, the BTM-180 comes up at 19200 baud.
379 * After that, it will remember and come up at 57600 baud. So, see
380 * if it is already running at 57600 baud, and if that doesn't work
381 * then tell it to switch to 57600 from 19200 baud.
383 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
384 ao_delay(AO_SEC_TO_TICKS(1));
385 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
386 ao_btm_cmd("ATL4\r");
387 ao_delay(AO_SEC_TO_TICKS(1));
391 ao_btm_cmd("ATE0\r");
393 /* Enable flow control */
394 ao_btm_cmd("ATC1\r");
396 /* Set the reported name to something we can find on the host */
399 /* Turn off status reporting */
400 ao_btm_cmd("ATQ1\r");
404 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
405 ao_serial_btm_putchar,
409 /* Check current pin state */
413 while (!ao_btm_connected)
414 ao_sleep(&ao_btm_connected);
415 while (ao_btm_connected) {
416 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
417 ao_delay(AO_SEC_TO_TICKS(3));
433 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
435 ao_wakeup(&ao_btm_connected);
441 #ifdef AO_BTM_INT_PORT
446 ao_wakeup(&ao_btm_connected);
455 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
457 #ifdef AO_BTM_RESET_PORT
458 ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,0);
461 #ifdef AO_BTM_INT_PORT
462 ao_enable_port(AO_BTM_INT_PORT);
463 ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
464 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
471 * Configure ser reset line
479 * Configure link status line
482 /* Set pin to input */
483 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
485 /* Set pin to tri-state */
486 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
488 /* Enable interrupts */
493 /* Eable the pin interrupt */
494 PICTL |= PICTL_P2IEN;
497 /* Enable pin interrupt */
498 P1IEN |= (1 << BT_LINK_PIN_INDEX);
501 ao_add_task(&ao_btm_task, ao_btm, "bt");