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
28 __xdata uint8_t ao_btm_connected;
33 __xdata char ao_btm_buffer[256];
38 ao_btm_add_char(char c)
40 if (ao_btm_ptr < sizeof (ao_btm_buffer))
41 ao_btm_buffer[ao_btm_ptr++] = c;
45 ao_btm_log_char(char c, char dir)
47 if (dir != ao_btm_dir) {
55 ao_btm_log_out_char(char c)
57 ao_btm_log_char(c, '>');
61 ao_btm_log_in_char(char c)
63 ao_btm_log_char(c, '<');
67 * Dump everything received from the bluetooth device during startup
75 for (i = 0; i < ao_btm_ptr; i++) {
77 if (c < ' ' && c != '\n')
78 printf("\\%03o", ((int) c) & 0xff);
80 putchar(ao_btm_buffer[i]);
89 if (ao_cmd_lex_u32 == 57600)
90 ao_serial_btm_set_speed(AO_SERIAL_SPEED_57600);
91 else if (ao_cmd_lex_u32 == 19200)
92 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
94 ao_cmd_status = ao_cmd_syntax_error;
97 __code struct ao_cmds ao_btm_cmds[] = {
98 { ao_btm_dump, "d\0Dump btm buffer." },
99 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
103 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
106 #define ao_btm_log_in_char(c)
107 #define ao_btm_log_out_char(c)
108 #define ao_btm_log_init()
111 #define AO_BTM_MAX_REPLY 16
112 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
115 * Read a line of data from the serial port, truncating
116 * it after a few characters.
120 ao_btm_get_line(void)
122 uint8_t ao_btm_reply_len = 0;
127 while ((c = ao_serial_btm_pollchar()) != AO_READ_AGAIN) {
128 ao_btm_log_in_char(c);
129 if (ao_btm_reply_len < sizeof (ao_btm_reply))
130 ao_btm_reply[ao_btm_reply_len++] = c;
131 if (c == '\r' || c == '\n')
134 for (c = 0; c < 10; c++) {
135 ao_delay(AO_MS_TO_TICKS(10));
136 if (!ao_fifo_empty(ao_serial1_rx_fifo))
143 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
144 ao_btm_reply[c++] = '\0';
145 return ao_btm_reply_len;
149 * Drain the serial port completely
154 while (ao_btm_get_line())
159 * Set the stdio echo for the bluetooth link
162 ao_btm_echo(uint8_t echo)
164 ao_stdios[ao_btm_stdio].echo = echo;
168 * Delay between command charaters; the BT module
169 * can't keep up with 57600 baud
173 ao_btm_putchar(char c)
175 ao_btm_log_out_char(c);
176 ao_serial_btm_putchar(c);
181 * Wait for the bluetooth device to return
182 * status from the previously executed command
185 ao_btm_wait_reply(void)
189 if (!strncmp(ao_btm_reply, "OK", 2))
191 if (!strncmp(ao_btm_reply, "ERROR", 5))
193 if (ao_btm_reply[0] == '\0')
199 ao_btm_string(__code char *cmd)
208 ao_btm_cmd(__code char *cmd)
212 return ao_btm_wait_reply();
216 ao_btm_set_name(void)
222 ao_btm_string("ATN=TeleBT-");
225 n = ao_serial_number;
231 return ao_btm_wait_reply();
235 ao_btm_try_speed(uint8_t speed)
237 ao_serial_btm_set_speed(speed);
238 ao_serial_btm_drain();
239 (void) ao_btm_cmd("\rATE0\rATQ0\r");
240 if (ao_btm_cmd("AT\r") == 1)
246 * A thread to initialize the bluetooth device and
247 * hang around to blink the LED when connected
253 * Wait for the bluetooth device to boot
255 ao_delay(AO_SEC_TO_TICKS(3));
258 * The first time we connect, the BTM-180 comes up at 19200 baud.
259 * After that, it will remember and come up at 57600 baud. So, see
260 * if it is already running at 57600 baud, and if that doesn't work
261 * then tell it to switch to 57600 from 19200 baud.
263 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
264 ao_delay(AO_SEC_TO_TICKS(1));
265 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
266 ao_btm_cmd("ATL4\r");
267 ao_delay(AO_SEC_TO_TICKS(1));
271 ao_btm_cmd("ATE0\r");
273 /* Enable flow control */
274 ao_btm_cmd("ATC1\r");
276 /* Set the reported name to something we can find on the host */
279 /* Turn off status reporting */
280 ao_btm_cmd("ATQ1\r");
282 ao_btm_stdio = ao_add_stdio(ao_serial_btm_pollchar,
283 ao_serial_btm_putchar,
288 while (!ao_btm_connected)
289 ao_sleep(&ao_btm_connected);
290 while (ao_btm_connected) {
291 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
292 ao_delay(AO_SEC_TO_TICKS(3));
297 __xdata struct ao_task ao_btm_task;
300 #define BT_PICTL_ICON PICTL_P2ICON
301 #define BT_PIFG P2IFG
302 #define BT_PDIR P2DIR
303 #define BT_PINP P2INP
304 #define BT_IEN2_PIE IEN2_P2IE
307 #define BT_PICTL_ICON PICTL_P1ICON
308 #define BT_PIFG P1IFG
309 #define BT_PDIR P1DIR
310 #define BT_PINP P1INP
311 #define BT_IEN2_PIE IEN2_P1IE
318 /* Check the pin and configure the interrupt detector to wait for the
319 * pin to flip the other way
322 ao_btm_connected = 0;
323 PICTL |= BT_PICTL_ICON;
325 ao_btm_connected = 1;
326 PICTL &= ~BT_PICTL_ICON;
340 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
342 ao_wakeup(&ao_btm_connected);
352 ao_serial_btm_set_speed(AO_SERIAL_SPEED_19200);
356 * Configure ser reset line
364 * Configure link status line
367 /* Set pin to input */
368 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
370 /* Set pin to tri-state */
371 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
373 /* Enable interrupts */
376 /* Check current pin state */
380 /* Eable the pin interrupt */
381 PICTL |= PICTL_P2IEN;
384 /* Enable pin interrupt */
385 P1IEN |= (1 << BT_LINK_PIN_INDEX);
388 ao_add_task(&ao_btm_task, ao_btm, "bt");