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.
19 #ifdef AO_BTM_INT_PORT
23 #ifndef ao_serial_btm_getchar
24 #define ao_serial_btm_putchar ao_serial1_putchar
25 #define _ao_serial_btm_pollchar _ao_serial1_pollchar
26 #define _ao_serial_btm_sleep_for(timeout) ao_sleep_for((void *) &ao_serial1_rx_fifo, timeout)
27 #define ao_serial_btm_set_speed ao_serial1_set_speed
28 #define ao_serial_btm_drain ao_serial1_drain
32 __xdata uint8_t ao_btm_connected;
37 __xdata char ao_btm_buffer[256];
42 ao_btm_add_char(char c)
44 if (ao_btm_ptr < sizeof (ao_btm_buffer))
45 ao_btm_buffer[ao_btm_ptr++] = c;
49 ao_btm_log_char(char c, char dir)
51 if (dir != ao_btm_dir) {
59 ao_btm_log_out_char(char c)
61 ao_btm_log_char(c, '>');
65 ao_btm_log_in_char(char c)
67 ao_btm_log_char(c, '<');
71 * Dump everything received from the bluetooth device during startup
79 for (i = 0; i < ao_btm_ptr; i++) {
81 if (c < ' ' && c != '\n')
82 printf("\\%03o", ((int) c) & 0xff);
84 putchar(ao_btm_buffer[i]);
88 if (ao_cmd_status == ao_cmd_success && ao_cmd_lex_i)
90 ao_cmd_status = ao_cmd_success;
97 if (ao_cmd_lex_u32 == 57600)
98 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
99 else if (ao_cmd_lex_u32 == 19200)
100 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
102 ao_cmd_status = ao_cmd_syntax_error;
105 static uint8_t ao_btm_enable;
111 while (ao_btm_enable) {
112 ao_arch_block_interrupts();
113 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN && ao_btm_enable)
114 _ao_serial_btm_sleep_for(0);
115 ao_arch_release_interrupts();
116 if (c != AO_READ_AGAIN) {
124 static struct ao_task ao_btm_echo_task;
131 ao_add_task(&ao_btm_echo_task, ao_btm_do_echo, "btm-echo");
132 while ((c = getchar()) != '~') {
133 ao_serial_btm_putchar(c);
136 ao_wakeup((void *) &ao_serial_btm_rx_fifo);
139 __code struct ao_cmds ao_btm_cmds[] = {
140 { ao_btm_dump, "d\0Dump btm buffer." },
141 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
142 { ao_btm_send, "S\0BTM interactive mode. ~ to exit." },
146 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
149 #define ao_btm_log_in_char(c)
150 #define ao_btm_log_out_char(c)
151 #define ao_btm_log_init()
154 #define AO_BTM_MAX_REPLY 16
155 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
158 * Read one bluetooth character.
159 * Returns AO_READ_AGAIN if no character arrives within 10ms
167 ao_arch_block_interrupts();
168 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
169 c = _ao_serial_btm_sleep_for(AO_MS_TO_TICKS(10));
175 ao_arch_release_interrupts();
180 * Read a line of data from the serial port, truncating
181 * it after a few characters.
185 ao_btm_get_line(void)
187 uint8_t ao_btm_reply_len = 0;
191 while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
192 ao_btm_log_in_char(c);
193 if (ao_btm_reply_len < sizeof (ao_btm_reply))
194 ao_btm_reply[ao_btm_reply_len++] = c;
195 if (c == '\r' || c == '\n')
198 for (l = ao_btm_reply_len; l < sizeof (ao_btm_reply);)
199 ao_btm_reply[l++] = '\0';
200 return ao_btm_reply_len;
204 * Drain the serial port completely
209 while (ao_btm_get_line())
214 * Set the stdio echo for the bluetooth link
217 ao_btm_echo(uint8_t echo)
219 ao_stdios[ao_btm_stdio].echo = echo;
223 * Delay between command charaters; the BT module
224 * can't keep up with 57600 baud
228 ao_btm_putchar(char c)
230 ao_btm_log_out_char(c);
231 ao_serial_btm_putchar(c);
236 * Wait for the bluetooth device to return
237 * status from the previously executed command
240 ao_btm_wait_reply(void)
244 if (!strncmp(ao_btm_reply, "OK", 2))
246 if (!strncmp(ao_btm_reply, "ERROR", 5))
248 if (ao_btm_reply[0] == '\0')
254 ao_btm_string(__code char *cmd)
258 while ((c = *cmd++) != '\0')
263 ao_btm_cmd(__code char *cmd)
267 #ifdef AO_BTM_INT_PORT
268 /* Trust that AltosDroid will eventually disconnect and let us
269 * get things set up. The BTM module doesn't appear to listen
270 * for +++, so we have no way to force a disconnect.
272 while (ao_btm_connected)
273 ao_sleep(&ao_btm_connected);
276 return ao_btm_wait_reply();
280 ao_btm_set_name(void)
286 ao_btm_string("ATN=TeleBT-");
289 n = ao_serial_number;
295 return ao_btm_wait_reply();
299 ao_btm_try_speed(uint8_t speed)
301 ao_serial_btm_set_speed(speed);
302 ao_serial_btm_drain();
303 (void) ao_btm_cmd("\rATE0\rATQ0\r");
304 if (ao_btm_cmd("AT\r") == 1)
310 #define BT_PICTL_ICON PICTL_P2ICON
311 #define BT_PIFG P2IFG
312 #define BT_PDIR P2DIR
313 #define BT_PINP P2INP
314 #define BT_IEN2_PIE IEN2_P2IE
318 #define BT_PICTL_ICON PICTL_P1ICON
319 #define BT_PIFG P1IFG
320 #define BT_PDIR P1DIR
321 #define BT_PINP P1INP
322 #define BT_IEN2_PIE IEN2_P1IE
331 /* Check the pin and configure the interrupt detector to wait for the
332 * pin to flip the other way
335 ao_btm_connected = 0;
336 PICTL |= BT_PICTL_ICON;
338 ao_btm_connected = 1;
339 PICTL &= ~BT_PICTL_ICON;
343 ao_arch_block_interrupts();
344 if (ao_gpio_get(AO_BTM_INT_PORT, AO_BTM_INT_PIN, AO_BTM_INT) == 0) {
345 ao_btm_connected = 1;
347 ao_btm_connected = 0;
349 ao_arch_release_interrupts();
353 __xdata struct ao_task ao_btm_task;
356 * A thread to initialize the bluetooth device and
357 * hang around to blink the LED when connected
362 #ifdef AO_BTM_INT_PORT
363 ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
367 * Wait for the bluetooth device to boot
369 ao_delay(AO_SEC_TO_TICKS(3));
372 * The first time we connect, the BTM-180 comes up at 19200 baud.
373 * After that, it will remember and come up at 57600 baud. So, see
374 * if it is already running at 57600 baud, and if that doesn't work
375 * then tell it to switch to 57600 from 19200 baud.
377 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
378 ao_delay(AO_SEC_TO_TICKS(1));
379 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
380 ao_btm_cmd("ATL4\r");
381 ao_delay(AO_SEC_TO_TICKS(1));
385 ao_btm_cmd("ATE0\r");
387 /* Enable flow control */
388 ao_btm_cmd("ATC1\r");
390 /* Set the reported name to something we can find on the host */
393 /* Turn off status reporting */
394 ao_btm_cmd("ATQ1\r");
398 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
399 ao_serial_btm_putchar,
403 /* Check current pin state */
407 while (!ao_btm_connected)
408 ao_sleep(&ao_btm_connected);
409 while (ao_btm_connected) {
410 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
411 ao_delay(AO_SEC_TO_TICKS(3));
427 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
429 ao_wakeup(&ao_btm_connected);
435 #ifdef AO_BTM_INT_PORT
440 ao_wakeup(&ao_btm_connected);
449 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
451 #ifdef AO_BTM_RESET_PORT
452 ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,AO_BTM_RESET,0);
455 #ifdef AO_BTM_INT_PORT
456 ao_enable_port(AO_BTM_INT_PORT);
457 ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
458 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
465 * Configure ser reset line
473 * Configure link status line
476 /* Set pin to input */
477 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
479 /* Set pin to tri-state */
480 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
482 /* Enable interrupts */
487 /* Eable the pin interrupt */
488 PICTL |= PICTL_P2IEN;
491 /* Enable pin interrupt */
492 P1IEN |= (1 << BT_LINK_PIN_INDEX);
495 ao_add_task(&ao_btm_task, ao_btm, "bt");