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]);
93 if (ao_cmd_lex_u32 == 57600)
94 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
95 else if (ao_cmd_lex_u32 == 19200)
96 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
98 ao_cmd_status = ao_cmd_syntax_error;
101 __code struct ao_cmds ao_btm_cmds[] = {
102 { ao_btm_dump, "d\0Dump btm buffer." },
103 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
107 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
110 #define ao_btm_log_in_char(c)
111 #define ao_btm_log_out_char(c)
112 #define ao_btm_log_init()
115 #define AO_BTM_MAX_REPLY 16
116 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
119 * Read one bluetooth character.
120 * Returns AO_READ_AGAIN if no character arrives within 10ms
128 ao_arch_block_interrupts();
129 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
130 ao_alarm(AO_MS_TO_TICKS(10));
131 c = _ao_serial_btm_sleep();
138 ao_arch_release_interrupts();
143 * Read a line of data from the serial port, truncating
144 * it after a few characters.
148 ao_btm_get_line(void)
150 uint8_t ao_btm_reply_len = 0;
154 while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
155 ao_btm_log_in_char(c);
156 if (ao_btm_reply_len < sizeof (ao_btm_reply))
157 ao_btm_reply[ao_btm_reply_len++] = c;
158 if (c == '\r' || c == '\n')
161 for (l = ao_btm_reply_len; l < sizeof (ao_btm_reply);)
162 ao_btm_reply[l++] = '\0';
163 return ao_btm_reply_len;
167 * Drain the serial port completely
172 while (ao_btm_get_line())
177 * Set the stdio echo for the bluetooth link
180 ao_btm_echo(uint8_t echo)
182 ao_stdios[ao_btm_stdio].echo = echo;
186 * Delay between command charaters; the BT module
187 * can't keep up with 57600 baud
191 ao_btm_putchar(char c)
193 ao_btm_log_out_char(c);
194 ao_serial_btm_putchar(c);
199 * Wait for the bluetooth device to return
200 * status from the previously executed command
203 ao_btm_wait_reply(void)
207 if (!strncmp(ao_btm_reply, "OK", 2))
209 if (!strncmp(ao_btm_reply, "ERROR", 5))
211 if (ao_btm_reply[0] == '\0')
217 ao_btm_string(__code char *cmd)
221 while ((c = *cmd++) != '\0')
226 ao_btm_cmd(__code char *cmd)
230 return ao_btm_wait_reply();
234 ao_btm_set_name(void)
240 ao_btm_string("ATN=TeleBT-");
243 n = ao_serial_number;
249 return ao_btm_wait_reply();
253 ao_btm_try_speed(uint8_t speed)
255 ao_serial_btm_set_speed(speed);
256 ao_serial_btm_drain();
257 (void) ao_btm_cmd("\rATE0\rATQ0\r");
258 if (ao_btm_cmd("AT\r") == 1)
264 * A thread to initialize the bluetooth device and
265 * hang around to blink the LED when connected
270 #ifdef AO_BTM_RESET_PORT
271 ao_gpio_set(AO_BTM_RESET_PORT, AO_BTM_RESET_PIN, AO_BTM_RESET, 0);
272 ao_delay(AO_MS_TO_TICKS(20));
273 ao_gpio_set(AO_BTM_RESET_PORT, AO_BTM_RESET_PIN, AO_BTM_RESET, 1);
277 * Wait for the bluetooth device to boot
279 ao_delay(AO_SEC_TO_TICKS(3));
282 * The first time we connect, the BTM-180 comes up at 19200 baud.
283 * After that, it will remember and come up at 57600 baud. So, see
284 * if it is already running at 57600 baud, and if that doesn't work
285 * then tell it to switch to 57600 from 19200 baud.
287 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
288 ao_delay(AO_SEC_TO_TICKS(1));
289 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
290 ao_btm_cmd("ATL4\r");
291 ao_delay(AO_SEC_TO_TICKS(1));
295 ao_btm_cmd("ATE0\r");
297 /* Enable flow control */
298 ao_btm_cmd("ATC1\r");
300 /* Set the reported name to something we can find on the host */
303 /* Turn off status reporting */
304 ao_btm_cmd("ATQ1\r");
306 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
307 ao_serial_btm_putchar,
312 while (!ao_btm_connected)
313 ao_sleep(&ao_btm_connected);
314 while (ao_btm_connected) {
315 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
316 ao_delay(AO_SEC_TO_TICKS(3));
321 __xdata struct ao_task ao_btm_task;
324 #define BT_PICTL_ICON PICTL_P2ICON
325 #define BT_PIFG P2IFG
326 #define BT_PDIR P2DIR
327 #define BT_PINP P2INP
328 #define BT_IEN2_PIE IEN2_P2IE
332 #define BT_PICTL_ICON PICTL_P1ICON
333 #define BT_PIFG P1IFG
334 #define BT_PDIR P1DIR
335 #define BT_PINP P1INP
336 #define BT_IEN2_PIE IEN2_P1IE
345 /* Check the pin and configure the interrupt detector to wait for the
346 * pin to flip the other way
349 ao_btm_connected = 0;
350 PICTL |= BT_PICTL_ICON;
352 ao_btm_connected = 1;
353 PICTL &= ~BT_PICTL_ICON;
369 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
371 ao_wakeup(&ao_btm_connected);
377 #ifdef AO_BTM_INT_PORT
382 ao_wakeup(&ao_btm_connected);
391 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
393 #ifdef AO_BTM_RESET_PORT
394 ao_enable_output(AO_BTM_RESET_PORT,AO_BTM_RESET_PIN,AO_BTM_RESET,0);
397 #ifdef AO_BTM_INT_PORT
398 ao_enable_port(AO_BTM_INT_PORT);
399 ao_exti_setup(AO_BTM_INT_PORT, AO_BTM_INT_PIN,
400 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
407 * Configure ser reset line
415 * Configure link status line
418 /* Set pin to input */
419 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
421 /* Set pin to tri-state */
422 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
424 /* Enable interrupts */
428 /* Check current pin state */
431 #ifdef AO_BTM_INT_PORT
432 ao_exti_enable(AO_BTM_INT_PORT, AO_BTM_INT_PIN);
436 /* Eable the pin interrupt */
437 PICTL |= PICTL_P2IEN;
440 /* Enable pin interrupt */
441 P1IEN |= (1 << BT_LINK_PIN_INDEX);
444 ao_add_task(&ao_btm_task, ao_btm, "bt");