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 uint8_t ao_btm_running;
22 __xdata uint8_t ao_btm_connected;
25 ao_btm_putchar(char c);
27 #define AO_BTM_MAX_REPLY 16
28 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
30 extern volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
33 * Read a line of data from the serial port, truncating
34 * it after a few characters.
40 uint8_t ao_btm_reply_len = 0;
45 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
46 if (ao_btm_reply_len < sizeof (ao_btm_reply))
47 ao_btm_reply[ao_btm_reply_len++] = c;
48 if (c == '\r' || c == '\n')
51 for (c = 0; c < 10; c++) {
52 ao_delay(AO_MS_TO_TICKS(10));
53 if (!ao_fifo_empty(ao_usart1_rx_fifo))
60 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
61 ao_btm_reply[c++] = '\0';
62 return ao_btm_reply_len;
66 * Drain the serial port completely
71 while (ao_btm_get_line())
76 * Set the stdio echo for the bluetooth link
79 ao_btm_echo(uint8_t echo)
81 ao_stdios[ao_btm_stdio].echo = echo;
85 * A command line pre-processor to detect connect/disconnect messages
86 * and update the internal state
92 if (ao_cur_stdio != ao_btm_stdio)
95 while (ao_cmd_lex_c != '\n') {
96 if (ao_match_word("CONNECT"))
98 if (ao_match_word("DISCONNECT"))
100 if (ao_match_word("ERROR"))
102 if (ao_match_word("OK"))
111 * Delay between command charaters; the BT module
112 * can't keep up with 57600 baud
116 ao_btm_putchar(char c)
118 ao_serial_putchar(c);
123 * Wait for the bluetooth device to return
124 * status from the previously executed command
127 ao_btm_wait_reply(void)
131 if (!strncmp(ao_btm_reply, "OK", 2))
133 if (!strncmp(ao_btm_reply, "ERROR", 5))
135 if (ao_btm_reply[0] == '\0')
141 ao_btm_string(__code char *cmd)
150 ao_btm_cmd(__code char *cmd)
154 return ao_btm_wait_reply();
158 ao_btm_set_name(void)
164 ao_btm_string("ATN=TeleBT-");
167 n = ao_serial_number;
173 return ao_btm_wait_reply();
177 ao_btm_try_speed(uint8_t speed)
179 ao_serial_set_speed(speed);
181 (void) ao_btm_cmd("\rATE0\rATQ0\r");
182 if (ao_btm_cmd("AT\r") == 1)
188 * A thread to initialize the bluetooth device and
189 * hang around to blink the LED when connected
195 * Wait for the bluetooth device to boot
197 ao_delay(AO_SEC_TO_TICKS(3));
200 * The first time we connect, the BTM-180 comes up at 19200 baud.
201 * After that, it will remember and come up at 57600 baud. So, see
202 * if it is already running at 57600 baud, and if that doesn't work
203 * then tell it to switch to 57600 from 19200 baud.
205 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
206 ao_delay(AO_SEC_TO_TICKS(1));
207 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
208 ao_btm_cmd("ATL4\r");
209 ao_delay(AO_SEC_TO_TICKS(1));
213 ao_btm_cmd("ATE0\r");
215 /* Enable flow control */
216 ao_btm_cmd("ATC1\r");
218 /* Set the reported name to something we can find on the host */
221 /* Turn off status reporting */
222 ao_btm_cmd("ATQ1\r");
224 ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
231 while (!ao_btm_connected)
232 ao_sleep(&ao_btm_connected);
233 while (ao_btm_connected) {
234 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
235 ao_delay(AO_SEC_TO_TICKS(3));
240 __xdata struct ao_task ao_btm_task;
243 ao_btm_check_link() __critical
246 ao_btm_connected = 0;
247 PICTL |= PICTL_P2ICON;
249 ao_btm_connected = 1;
250 PICTL &= ~PICTL_P2ICON;
257 if (P2IFG & (1 << 1)) {
259 ao_wakeup(&ao_btm_connected);
268 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
271 * Configure link status line
274 /* Set P2_1 to input, pull-down */
276 P2INP |= P2INP_MDP2_1_TRISTATE;
278 /* Enable P2 interrupts */
281 PICTL |= PICTL_P2IEN;
283 ao_add_task(&ao_btm_task, ao_btm, "bt");