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 return ao_btm_wait_reply();
271 ao_btm_set_name(void)
277 ao_btm_string("ATN=TeleBT-");
280 n = ao_serial_number;
286 return ao_btm_wait_reply();
290 ao_btm_try_speed(uint8_t speed)
292 ao_serial_btm_set_speed(speed);
293 ao_serial_btm_drain();
294 (void) ao_btm_cmd("\rATE0\rATQ0\r");
295 if (ao_btm_cmd("AT\r") == 1)
301 #define BT_PICTL_ICON PICTL_P2ICON
302 #define BT_PIFG P2IFG
303 #define BT_PDIR P2DIR
304 #define BT_PINP P2INP
305 #define BT_IEN2_PIE IEN2_P2IE
309 #define BT_PICTL_ICON PICTL_P1ICON
310 #define BT_PIFG P1IFG
311 #define BT_PDIR P1DIR
312 #define BT_PINP P1INP
313 #define BT_IEN2_PIE IEN2_P1IE
322 /* Check the pin and configure the interrupt detector to wait for the
323 * pin to flip the other way
326 ao_btm_connected = 0;
327 PICTL |= BT_PICTL_ICON;
329 ao_btm_connected = 1;
330 PICTL &= ~BT_PICTL_ICON;
334 ao_arch_block_interrupts();
335 if (ao_gpio_get(AO_BTM_INT_PORT, AO_BTM_INT_PIN, AO_BTM_INT) == 0) {
336 ao_btm_connected = 1;
338 ao_btm_connected = 0;
340 ao_arch_release_interrupts();
344 __xdata struct ao_task ao_btm_task;
347 * A thread to initialize the bluetooth device and
348 * hang around to blink the LED when connected
354 * Wait for the bluetooth device to boot
356 ao_delay(AO_SEC_TO_TICKS(3));
359 * The first time we connect, the BTM-180 comes up at 19200 baud.
360 * After that, it will remember and come up at 57600 baud. So, see
361 * if it is already running at 57600 baud, and if that doesn't work
362 * then tell it to switch to 57600 from 19200 baud.
364 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
365 ao_delay(AO_SEC_TO_TICKS(1));
366 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
367 ao_btm_cmd("ATL4\r");
368 ao_delay(AO_SEC_TO_TICKS(1));
372 ao_btm_cmd("ATE0\r");
374 /* Enable flow control */
375 ao_btm_cmd("ATC1\r");
377 /* Set the reported name to something we can find on the host */
380 /* Turn off status reporting */
381 ao_btm_cmd("ATQ1\r");
383 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
384 ao_serial_btm_putchar,
388 /* Check current pin state */
391 #ifdef AO_BTM_INT_PORT
392 ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
396 while (!ao_btm_connected)
397 ao_sleep(&ao_btm_connected);
398 while (ao_btm_connected) {
399 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
400 ao_delay(AO_SEC_TO_TICKS(3));
416 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
418 ao_wakeup(&ao_btm_connected);
424 #ifdef AO_BTM_INT_PORT
429 ao_wakeup(&ao_btm_connected);
438 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
440 #ifdef AO_BTM_RESET_PORT
441 ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,AO_BTM_RESET,0);
444 #ifdef AO_BTM_INT_PORT
445 ao_enable_port(AO_BTM_INT_PORT);
446 ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
447 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
454 * Configure ser reset line
462 * Configure link status line
465 /* Set pin to input */
466 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
468 /* Set pin to tri-state */
469 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
471 /* Enable interrupts */
476 /* Eable the pin interrupt */
477 PICTL |= PICTL_P2IEN;
480 /* Enable pin interrupt */
481 P1IEN |= (1 << BT_LINK_PIN_INDEX);
484 ao_add_task(&ao_btm_task, ao_btm, "bt");