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() ao_sleep((void *) &ao_serial1_rx_fifo)
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();
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 ao_alarm(AO_MS_TO_TICKS(10));
170 c = _ao_serial_btm_sleep();
177 ao_arch_release_interrupts();
182 * Read a line of data from the serial port, truncating
183 * it after a few characters.
187 ao_btm_get_line(void)
189 uint8_t ao_btm_reply_len = 0;
193 while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
194 ao_btm_log_in_char(c);
195 if (ao_btm_reply_len < sizeof (ao_btm_reply))
196 ao_btm_reply[ao_btm_reply_len++] = c;
197 if (c == '\r' || c == '\n')
200 for (l = ao_btm_reply_len; l < sizeof (ao_btm_reply);)
201 ao_btm_reply[l++] = '\0';
202 return ao_btm_reply_len;
206 * Drain the serial port completely
211 while (ao_btm_get_line())
216 * Set the stdio echo for the bluetooth link
219 ao_btm_echo(uint8_t echo)
221 ao_stdios[ao_btm_stdio].echo = echo;
225 * Delay between command charaters; the BT module
226 * can't keep up with 57600 baud
230 ao_btm_putchar(char c)
232 ao_btm_log_out_char(c);
233 ao_serial_btm_putchar(c);
238 * Wait for the bluetooth device to return
239 * status from the previously executed command
242 ao_btm_wait_reply(void)
246 if (!strncmp(ao_btm_reply, "OK", 2))
248 if (!strncmp(ao_btm_reply, "ERROR", 5))
250 if (ao_btm_reply[0] == '\0')
256 ao_btm_string(__code char *cmd)
260 while ((c = *cmd++) != '\0')
265 ao_btm_cmd(__code char *cmd)
269 return ao_btm_wait_reply();
273 ao_btm_set_name(void)
279 ao_btm_string("ATN=TeleBT-");
282 n = ao_serial_number;
288 return ao_btm_wait_reply();
292 ao_btm_try_speed(uint8_t speed)
294 ao_serial_btm_set_speed(speed);
295 ao_serial_btm_drain();
296 (void) ao_btm_cmd("\rATE0\rATQ0\r");
297 if (ao_btm_cmd("AT\r") == 1)
303 #define BT_PICTL_ICON PICTL_P2ICON
304 #define BT_PIFG P2IFG
305 #define BT_PDIR P2DIR
306 #define BT_PINP P2INP
307 #define BT_IEN2_PIE IEN2_P2IE
311 #define BT_PICTL_ICON PICTL_P1ICON
312 #define BT_PIFG P1IFG
313 #define BT_PDIR P1DIR
314 #define BT_PINP P1INP
315 #define BT_IEN2_PIE IEN2_P1IE
324 /* Check the pin and configure the interrupt detector to wait for the
325 * pin to flip the other way
328 ao_btm_connected = 0;
329 PICTL |= BT_PICTL_ICON;
331 ao_btm_connected = 1;
332 PICTL &= ~BT_PICTL_ICON;
336 ao_arch_block_interrupts();
337 if (ao_gpio_get(AO_BTM_INT_PORT, AO_BTM_INT_PIN, AO_BTM_INT) == 0) {
338 ao_btm_connected = 1;
340 ao_btm_connected = 0;
342 ao_arch_release_interrupts();
346 __xdata struct ao_task ao_btm_task;
349 * A thread to initialize the bluetooth device and
350 * hang around to blink the LED when connected
356 * Wait for the bluetooth device to boot
358 ao_delay(AO_SEC_TO_TICKS(3));
361 * The first time we connect, the BTM-180 comes up at 19200 baud.
362 * After that, it will remember and come up at 57600 baud. So, see
363 * if it is already running at 57600 baud, and if that doesn't work
364 * then tell it to switch to 57600 from 19200 baud.
366 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
367 ao_delay(AO_SEC_TO_TICKS(1));
368 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
369 ao_btm_cmd("ATL4\r");
370 ao_delay(AO_SEC_TO_TICKS(1));
374 ao_btm_cmd("ATE0\r");
376 /* Enable flow control */
377 ao_btm_cmd("ATC1\r");
379 /* Set the reported name to something we can find on the host */
382 /* Turn off status reporting */
383 ao_btm_cmd("ATQ1\r");
385 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
386 ao_serial_btm_putchar,
390 /* Check current pin state */
393 #ifdef AO_BTM_INT_PORT
394 ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
398 while (!ao_btm_connected)
399 ao_sleep(&ao_btm_connected);
400 while (ao_btm_connected) {
401 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
402 ao_delay(AO_SEC_TO_TICKS(3));
418 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
420 ao_wakeup(&ao_btm_connected);
426 #ifdef AO_BTM_INT_PORT
431 ao_wakeup(&ao_btm_connected);
440 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
442 #ifdef AO_BTM_RESET_PORT
443 ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,AO_BTM_RESET,0);
446 #ifdef AO_BTM_INT_PORT
447 ao_enable_port(AO_BTM_INT_PORT);
448 ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
449 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
456 * Configure ser reset line
464 * Configure link status line
467 /* Set pin to input */
468 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
470 /* Set pin to tri-state */
471 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
473 /* Enable interrupts */
478 /* Eable the pin interrupt */
479 PICTL |= PICTL_P2IEN;
482 /* Enable pin interrupt */
483 P1IEN |= (1 << BT_LINK_PIN_INDEX);
486 ao_add_task(&ao_btm_task, ao_btm, "bt");