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 * The first time we connect, the BTM-180 comes up at 19200 baud.
254 * After that, it will remember and come up at 57600 baud. So, see
255 * if it is already running at 57600 baud, and if that doesn't work
256 * then tell it to switch to 57600 from 19200 baud.
258 while (!ao_btm_try_speed(AO_SERIAL_SPEED_57600)) {
259 ao_delay(AO_SEC_TO_TICKS(1));
260 if (ao_btm_try_speed(AO_SERIAL_SPEED_19200))
261 ao_btm_cmd("ATL4\r");
262 ao_delay(AO_SEC_TO_TICKS(1));
266 ao_btm_cmd("ATE0\r");
268 /* Enable flow control */
269 ao_btm_cmd("ATC1\r");
271 /* Set the reported name to something we can find on the host */
274 /* Turn off status reporting */
275 ao_btm_cmd("ATQ1\r");
277 ao_btm_stdio = ao_add_stdio(ao_serial_pollchar,
283 while (!ao_btm_connected)
284 ao_sleep(&ao_btm_connected);
285 while (ao_btm_connected) {
286 ao_led_for(AO_LED_GREEN, AO_MS_TO_TICKS(20));
287 ao_delay(AO_SEC_TO_TICKS(3));
292 __xdata struct ao_task ao_btm_task;
295 #define BT_PICTL_ICON PICTL_P2ICON
296 #define BT_PIFG P2IFG
297 #define BT_PDIR P2DIR
298 #define BT_PINP P2INP
299 #define BT_IEN2_PIE IEN2_P2IE
302 #define BT_PICTL_ICON PICTL_P1ICON
303 #define BT_PIFG P1IFG
304 #define BT_PDIR P1DIR
305 #define BT_PINP P1INP
306 #define BT_IEN2_PIE IEN2_P1IE
310 ao_btm_check_link() __critical
312 /* Check the pin and configure the interrupt detector to wait for the
313 * pin to flip the other way
316 ao_btm_connected = 0;
317 PICTL |= BT_PICTL_ICON;
319 ao_btm_connected = 1;
320 PICTL &= ~BT_PICTL_ICON;
333 if (BT_PIFG & (1 << BT_LINK_PIN_INDEX)) {
335 ao_wakeup(&ao_btm_connected);
344 ao_serial_set_speed(AO_SERIAL_SPEED_19200);
348 * Configure ser reset line
356 * Configure link status line
359 /* Set pin to input */
360 BT_PDIR &= ~(1 << BT_LINK_PIN_INDEX);
362 /* Set pin to tri-state */
363 BT_PINP |= (1 << BT_LINK_PIN_INDEX);
365 /* Enable interrupts */
368 /* Check current pin state */
372 /* Eable the pin interrupt */
373 PICTL |= PICTL_P2IEN;
376 /* Enable pin interrupt */
377 P1IEN |= (1 << BT_LINK_PIN_INDEX);
380 ao_add_task(&ao_btm_task, ao_btm, "bt");