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 __xdata uint8_t ao_btm_connected;
26 __xdata char ao_btm_buffer[256];
31 ao_btm_add_char(char c)
33 if (ao_btm_ptr < sizeof (ao_btm_buffer))
34 ao_btm_buffer[ao_btm_ptr++] = c;
38 ao_btm_log_char(char c, char dir)
40 if (dir != ao_btm_dir) {
48 ao_btm_log_out_char(char c)
50 ao_btm_log_char(c, '>');
54 ao_btm_log_in_char(char c)
56 ao_btm_log_char(c, '<');
60 * Dump everything received from the bluetooth device during startup
68 for (i = 0; i < ao_btm_ptr; i++) {
70 if (c < ' ' && c != '\n')
71 printf("\\%03o", ((int) c) & 0xff);
73 putchar(ao_btm_buffer[i]);
82 if (ao_cmd_lex_u32 == 57600)
83 ao_serial_set_speed(AO_SERIAL_SPEED_57600);
84 else if (ao_cmd_lex_u32 == 19200)
85 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
87 ao_cmd_status = ao_cmd_syntax_error;
90 __code struct ao_cmds ao_btm_cmds[] = {
91 { ao_btm_dump, "d\0Dump btm buffer." },
92 { ao_btm_speed, "s <19200,57600>\0Set btm serial speed." },
96 #define ao_btm_log_init() ao_cmd_register(&ao_btm_cmds[0])
99 #define ao_btm_log_in_char(c)
100 #define ao_btm_log_out_char(c)
101 #define ao_btm_log_init()
104 #define AO_BTM_MAX_REPLY 16
105 __xdata char ao_btm_reply[AO_BTM_MAX_REPLY];
107 extern volatile __xdata struct ao_fifo ao_usart1_rx_fifo;
110 * Read a line of data from the serial port, truncating
111 * it after a few characters.
115 ao_btm_get_line(void)
117 uint8_t ao_btm_reply_len = 0;
122 while ((c = ao_serial_pollchar()) != AO_READ_AGAIN) {
123 ao_btm_log_in_char(c);
124 if (ao_btm_reply_len < sizeof (ao_btm_reply))
125 ao_btm_reply[ao_btm_reply_len++] = c;
126 if (c == '\r' || c == '\n')
129 for (c = 0; c < 10; c++) {
130 ao_delay(AO_MS_TO_TICKS(10));
131 if (!ao_fifo_empty(ao_usart1_rx_fifo))
138 for (c = ao_btm_reply_len; c < sizeof (ao_btm_reply);)
139 ao_btm_reply[c++] = '\0';
140 return ao_btm_reply_len;
144 * Drain the serial port completely
149 while (ao_btm_get_line())
154 * Set the stdio echo for the bluetooth link
157 ao_btm_echo(uint8_t echo)
159 ao_stdios[ao_btm_stdio].echo = echo;
163 * Delay between command charaters; the BT module
164 * can't keep up with 57600 baud
168 ao_btm_putchar(char c)
170 ao_btm_log_out_char(c);
171 ao_serial_putchar(c);
176 * Wait for the bluetooth device to return
177 * status from the previously executed command
180 ao_btm_wait_reply(void)
184 if (!strncmp(ao_btm_reply, "OK", 2))
186 if (!strncmp(ao_btm_reply, "ERROR", 5))
188 if (ao_btm_reply[0] == '\0')
194 ao_btm_string(__code char *cmd)
203 ao_btm_cmd(__code char *cmd)
207 return ao_btm_wait_reply();
211 ao_btm_set_name(void)
217 ao_btm_string("ATN=TeleBT-");
220 n = ao_serial_number;
226 return ao_btm_wait_reply();
230 ao_btm_try_speed(uint8_t speed)
232 ao_serial_set_speed(speed);
234 (void) ao_btm_cmd("\rATE0\rATQ0\r");
235 if (ao_btm_cmd("AT\r") == 1)
241 * A thread to initialize the bluetooth device and
242 * hang around to blink the LED when connected
248 * Wait for the bluetooth device to boot
250 ao_delay(AO_SEC_TO_TICKS(3));
253 ao_beep_for(AO_BEEP_MID, AO_MS_TO_TICKS(200));
257 * The first time we connect, the BTM-180 comes up at 19200 baud.
258 * After that, it will remember and come up at 57600 baud. So, see
259 * if it is already running at 57600 baud, and if that doesn't work
260 * then tell it to switch to 57600 from 19200 baud.
262 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
263 ao_delay(AO_SEC_TO_TICKS(1));
264 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
265 ao_btm_cmd("ATL4\r");
266 ao_delay(AO_SEC_TO_TICKS(1));
270 ao_btm_cmd("ATE0\r");
272 /* Enable flow control */
273 ao_btm_cmd("ATC1\r");
275 /* Set the reported name to something we can find on the host */
278 /* Turn off status reporting */
279 ao_btm_cmd("ATQ1\r");
281 ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
287 while (!ao_btm_connected)
288 ao_sleep(&ao_btm_connected);
289 while (ao_btm_connected) {
290 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
291 ao_delay(AO_SEC_TO_TICKS(3));
296 __xdata struct ao_task ao_btm_task;
299 #define BT_PICTL_ICON PICTL_P2ICON
300 #define BT_PIFG P2IFG
301 #define BT_PDIR P2DIR
302 #define BT_PINP P2INP
303 #define BT_IEN2_PIE IEN2_P2IE
306 #define BT_PICTL_ICON PICTL_P1ICON
307 #define BT_PIFG P1IFG
308 #define BT_PDIR P1DIR
309 #define BT_PINP P1INP
310 #define BT_IEN2_PIE IEN2_P1IE
314 ao_btm_check_link() __critical
316 /* Check the pin and configure the interrupt detector to wait for the
317 * pin to flip the other way
320 ao_btm_connected = 0;
321 PICTL |= BT_PICTL_ICON;
323 ao_btm_connected = 1;
324 PICTL &= ~BT_PICTL_ICON;
337 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
339 ao_wakeup(&ao_btm_connected);
348 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
352 * Configure ser reset line
360 * Configure link status line
363 /* Set pin to input */
364 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
366 /* Set pin to tri-state */
367 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
369 /* Enable interrupts */
372 /* Check current pin state */
376 /* Eable the pin interrupt */
377 PICTL |= PICTL_P2IEN;
380 /* Enable pin interrupt */
381 P1IEN |= (1 << BT_LINK_PIN_INDEX);
384 ao_add_task(&ao_btm_task, ao_btm, "bt");