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.
21 #define BT_PICTL_ICON PICTL_P2ICON
25 #define BT_IEN2_PIE IEN2_P2IE
28 #define BT_PICTL_ICON PICTL_P1ICON
32 #define BT_IEN2_PIE IEN2_P1IE
36 __xdata uint8_t ao_btm_connected;
38 #define AO_BTM_MAX_REPLY 16
39 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
41 extern volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
44 * Read a line of data from the serial port, truncating
45 * it after a few characters.
51 uint8_t ao_btm_reply_len = 0;
56 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
57 if (ao_btm_reply_len < sizeof (ao_btm_reply))
58 ao_btm_reply[ao_btm_reply_len++] = c;
59 if (c == '\r' || c == '\n')
62 for (c = 0; c < 10; c++) {
63 ao_delay(AO_MS_TO_TICKS(10));
64 if (!ao_fifo_empty(ao_usart1_rx_fifo))
71 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
72 ao_btm_reply[c++] = '\0';
73 return ao_btm_reply_len;
77 * Drain the serial port completely
82 while (ao_btm_get_line())
87 * Set the stdio echo for the bluetooth link
90 ao_btm_echo(uint8_t echo)
92 ao_stdios[ao_btm_stdio].echo = echo;
96 * Delay between command charaters; the BT module
97 * can't keep up with 57600 baud
101 ao_btm_putchar(char c)
103 ao_serial_putchar(c);
108 * Wait for the bluetooth device to return
109 * status from the previously executed command
112 ao_btm_wait_reply(void)
116 if (!strncmp(ao_btm_reply, "OK", 2))
118 if (!strncmp(ao_btm_reply, "ERROR", 5))
120 if (ao_btm_reply[0] == '\0')
126 ao_btm_string(__code char *cmd)
135 ao_btm_cmd(__code char *cmd)
139 return ao_btm_wait_reply();
143 ao_btm_set_name(void)
149 ao_btm_string("ATN=TeleBT-");
152 n = ao_serial_number;
158 return ao_btm_wait_reply();
162 ao_btm_try_speed(uint8_t speed)
164 ao_serial_set_speed(speed);
166 (void) ao_btm_cmd("\rATE0\rATQ0\r");
167 if (ao_btm_cmd("AT\r") == 1)
173 * A thread to initialize the bluetooth device and
174 * hang around to blink the LED when connected
180 * Wait for the bluetooth device to boot
182 ao_delay(AO_SEC_TO_TICKS(3));
185 if ((P1DIR & (1 << 6)) == 0)
186 ao_panic(AO_PANIC_BT);
187 if ((P1DIR & (1 << 7)) != 0)
188 ao_panic(AO_PANIC_BT);
189 if ((P0SEL & ((1 << 5) | (1 << 4) | (1 << 3) | (1 << 2))) !=
190 ((1 << 5) | (1 << 4) | (1 << 3) | (1 << 2)))
191 ao_panic(AO_PANIC_BT);
194 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
198 * The first time we connect, the BTM-180 comes up at 19200 baud.
199 * After that, it will remember and come up at 57600 baud. So, see
200 * if it is already running at 57600 baud, and if that doesn't work
201 * then tell it to switch to 57600 from 19200 baud.
203 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
204 ao_delay(AO_SEC_TO_TICKS(1));
205 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
206 ao_btm_cmd("ATL4\r");
207 ao_delay(AO_SEC_TO_TICKS(1));
211 ao_btm_cmd("ATE0\r");
213 /* Enable flow control */
214 ao_btm_cmd("ATC1\r");
216 /* Set the reported name to something we can find on the host */
219 /* Turn off status reporting */
220 ao_btm_cmd("ATQ1\r");
222 ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
228 while (!ao_btm_connected)
229 ao_sleep(&ao_btm_connected);
230 while (ao_btm_connected) {
231 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
232 ao_delay(AO_SEC_TO_TICKS(3));
237 __xdata struct ao_task ao_btm_task;
240 ao_btm_check_link() __critical
242 /* Check the pin and configure the interrupt detector to wait for the
243 * pin to flip the other way
246 ao_btm_connected = 0;
247 PICTL |= BT_PICTL_ICON;
249 ao_btm_connected = 1;
250 PICTL &= ~BT_PICTL_ICON;
257 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
259 ao_wakeup(&ao_btm_connected);
268 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
272 * Configure ser reset line
280 * Configure link status line
283 /* Set pin to input */
284 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
286 /* Set pin to tri-state */
287 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
289 /* Enable interrupts */
292 /* Check current pin state */
296 /* Eable the pin interrupt */
297 PICTL |= PICTL_P2IEN;
300 /* Enable pin interrupt */
301 P1IEN |= (1 << BT_LINK_PIN_INDEX);
304 ao_add_task(&ao_btm_task, ao_btm, "bt");