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.
20 #ifndef ao_serial_btm_getchar
21 #define ao_serial_btm_putchar ao_serial1_putchar
22 #define _ao_serial_btm_pollchar _ao_serial1_pollchar
23 #define ao_serial_btm_set_speed ao_serial1_set_speed
24 #define ao_serial_btm_drain ao_serial1_drain
25 #define ao_serial_btm_rx_fifo ao_serial1_rx_fifo
29 __xdata uint8_t ao_btm_connected;
34 __xdata char ao_btm_buffer[256];
39 ao_btm_add_char(char c)
41 if (ao_btm_ptr < sizeof (ao_btm_buffer))
42 ao_btm_buffer[ao_btm_ptr++] = c;
46 ao_btm_log_char(char c, char dir)
48 if (dir != ao_btm_dir) {
56 ao_btm_log_out_char(char c)
58 ao_btm_log_char(c, '>');
62 ao_btm_log_in_char(char c)
64 ao_btm_log_char(c, '<');
68 * Dump everything received from the bluetooth device during startup
76 for (i = 0; i < ao_btm_ptr; i++) {
78 if (c < ' ' && c != '\n')
79 printf("\\%03o", ((int) c) & 0xff);
81 putchar(ao_btm_buffer[i]);
90 if (ao_cmd_lex_u32 == 57600)
91 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
92 else if (ao_cmd_lex_u32 == 19200)
93 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
95 ao_cmd_status = ao_cmd_syntax_error;
98 __code struct ao_cmds ao_btm_cmds[] = {
99 { ao_btm_dump, "d\0Dump btm buffer." },
100 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
104 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
107 #define ao_btm_log_in_char(c)
108 #define ao_btm_log_out_char(c)
109 #define ao_btm_log_init()
112 #define AO_BTM_MAX_REPLY 16
113 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
116 * Read one bluetooth character.
117 * Returns AO_READ_AGAIN if no character arrives within 10ms
125 ao_arch_block_interrupts();
126 while ((c = _ao_serial_btm_pollchar()) == AO_READ_AGAIN) {
127 ao_alarm(AO_MS_TO_TICKS(10));
128 c = ao_sleep(&ao_serial_btm_rx_fifo);
135 ao_arch_release_interrupts();
140 * Read a line of data from the serial port, truncating
141 * it after a few characters.
145 ao_btm_get_line(void)
147 uint8_t ao_btm_reply_len = 0;
150 while ((c = ao_btm_getchar()) != AO_READ_AGAIN) {
151 ao_btm_log_in_char(c);
152 if (ao_btm_reply_len < sizeof (ao_btm_reply))
153 ao_btm_reply[ao_btm_reply_len++] = c;
154 if (c == '\r' || c == '\n')
157 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
158 ao_btm_reply[c++] = '\0';
159 return ao_btm_reply_len;
163 * Drain the serial port completely
168 while (ao_btm_get_line())
173 * Set the stdio echo for the bluetooth link
176 ao_btm_echo(uint8_t echo)
178 ao_stdios[ao_btm_stdio].echo = echo;
182 * Delay between command charaters; the BT module
183 * can't keep up with 57600 baud
187 ao_btm_putchar(char c)
189 ao_btm_log_out_char(c);
190 ao_serial_btm_putchar(c);
195 * Wait for the bluetooth device to return
196 * status from the previously executed command
199 ao_btm_wait_reply(void)
203 if (!strncmp(ao_btm_reply, "OK", 2))
205 if (!strncmp(ao_btm_reply, "ERROR", 5))
207 if (ao_btm_reply[0] == '\0')
213 ao_btm_string(__code char *cmd)
222 ao_btm_cmd(__code char *cmd)
226 return ao_btm_wait_reply();
230 ao_btm_set_name(void)
236 ao_btm_string("ATN=TeleBT-");
239 n = ao_serial_number;
245 return ao_btm_wait_reply();
249 ao_btm_try_speed(uint8_t speed)
251 ao_serial_btm_set_speed(speed);
252 ao_serial_btm_drain();
253 (void) ao_btm_cmd("\rATE0\rATQ0\r");
254 if (ao_btm_cmd("AT\r") == 1)
260 * A thread to initialize the bluetooth device and
261 * hang around to blink the LED when connected
267 * Wait for the bluetooth device to boot
269 ao_delay(AO_SEC_TO_TICKS(3));
272 * The first time we connect, the BTM-180 comes up at 19200 baud.
273 * After that, it will remember and come up at 57600 baud. So, see
274 * if it is already running at 57600 baud, and if that doesn't work
275 * then tell it to switch to 57600 from 19200 baud.
277 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
278 ao_delay(AO_SEC_TO_TICKS(1));
279 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
280 ao_btm_cmd("ATL4\r");
281 ao_delay(AO_SEC_TO_TICKS(1));
285 ao_btm_cmd("ATE0\r");
287 /* Enable flow control */
288 ao_btm_cmd("ATC1\r");
290 /* Set the reported name to something we can find on the host */
293 /* Turn off status reporting */
294 ao_btm_cmd("ATQ1\r");
296 ao_btm_stdio = ao_add_stdio(_ao_serial_btm_pollchar,
297 ao_serial_btm_putchar,
302 while (!ao_btm_connected)
303 ao_sleep(&ao_btm_connected);
304 while (ao_btm_connected) {
305 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
306 ao_delay(AO_SEC_TO_TICKS(3));
311 __xdata struct ao_task ao_btm_task;
314 #define BT_PICTL_ICON PICTL_P2ICON
315 #define BT_PIFG P2IFG
316 #define BT_PDIR P2DIR
317 #define BT_PINP P2INP
318 #define BT_IEN2_PIE IEN2_P2IE
321 #define BT_PICTL_ICON PICTL_P1ICON
322 #define BT_PIFG P1IFG
323 #define BT_PDIR P1DIR
324 #define BT_PINP P1INP
325 #define BT_IEN2_PIE IEN2_P1IE
332 /* Check the pin and configure the interrupt detector to wait for the
333 * pin to flip the other way
336 ao_btm_connected = 0;
337 PICTL |= BT_PICTL_ICON;
339 ao_btm_connected = 1;
340 PICTL &= ~BT_PICTL_ICON;
354 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
356 ao_wakeup(&ao_btm_connected);
366 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
370 * Configure ser reset line
378 * Configure link status line
381 /* Set pin to input */
382 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
384 /* Set pin to tri-state */
385 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
387 /* Enable interrupts */
390 /* Check current pin state */
394 /* Eable the pin interrupt */
395 PICTL |= PICTL_P2IEN;
398 /* Enable pin interrupt */
399 P1IEN |= (1 << BT_LINK_PIN_INDEX);
402 ao_add_task(&ao_btm_task, ao_btm, "bt");