2 * Copyright © 2017 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, either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
16 #include <ao_rn4678.h>
20 static uint8_t ao_rn_connected;
25 static void ao_rn_dbg(char *format, ...) {
27 uint32_t irq = ao_arch_irqsave();
28 ao_arch_release_interrupts();
33 ao_arch_irqrestore(irq);
36 static char ao_rn_dir;
39 ao_rn_log_char(char c, char dir)
41 if (dir != ao_rn_dir) {
47 putchar('\\'); putchar('r');
50 putchar('\\'); putchar('n');
59 ao_rn_log_out_char(char c)
61 ao_rn_log_char(c, '}');
65 ao_rn_log_in_char(char c)
67 ao_rn_log_char(c, '{');
73 ao_rn_log_out_char(c);
74 ao_serial_rn_putchar(c);
80 int c = _ao_serial_rn_pollchar();
82 if (c != AO_READ_AGAIN) {
83 ao_arch_release_interrupts();
84 ao_rn_log_in_char((char) c);
85 ao_arch_block_interrupts();
90 #define ao_rn_dbg(fmt, ...)
91 #define ao_rn_putchar(c) ao_serial_rn_putchar(c)
92 #define _ao_rn_pollchar() _ao_serial_rn_pollchar()
95 /* For stdio, this skips all status messages *sigh* */
97 #define STATUS_CHAR '%'
99 static const char *status_strings[] = {
107 #define NUM_STATUS_STRING (sizeof status_strings/sizeof status_strings[0])
110 _ao_wrap_rn_pollchar(void)
112 static char buffer[64];
113 static int buf_cnt, buf_ptr;
115 int c = AO_READ_AGAIN;
119 while (!done && !draining) {
120 c = _ao_serial_rn_pollchar();
122 if (c == AO_READ_AGAIN)
123 return AO_READ_AGAIN;
126 /* buffering chars */
128 if (c == STATUS_CHAR) {
129 /* End of status string, drop it and carry on */
130 buffer[buf_cnt] = '\0';
131 ao_rn_dbg("discard %s\n", buffer);
133 } else if (buf_cnt == sizeof(buffer)) {
134 /* If we filled the buffer, just give up */
137 buffer[buf_cnt++] = c;
138 for (i = 0; i < NUM_STATUS_STRING; i++) {
139 int cmp = strlen(status_strings[i]);
142 if (memcmp(buffer+1, status_strings[i], cmp) == 0)
145 if (i == NUM_STATUS_STRING)
148 } else if (c == STATUS_CHAR) {
156 c = buffer[buf_ptr++] & 0xff;
157 if (buf_ptr == buf_cnt) {
158 buf_ptr = buf_cnt = 0;
163 ao_arch_release_interrupts();
164 ao_usb_putchar(c); ao_usb_flush();
165 ao_arch_block_interrupts();
172 ao_wrap_rn_putchar(char c)
174 ao_usb_putchar(c); ao_usb_flush();
175 ao_serial_rn_putchar(c);
178 #define ao_wrap_rn_putchar ao_serial_rn_putchar
195 ao_rn_dbg("drain...\n");
196 ao_serial_rn_drain();
198 ao_arch_block_interrupts();
199 while (_ao_rn_pollchar() == AO_READ_AGAIN) {
200 if (_ao_serial_rn_sleep_for(AO_MS_TO_TICKS(10))) {
205 ao_arch_release_interrupts();
207 ao_rn_dbg("drain done\n");
211 ao_rn_send_cmd(char *cmd, char *param)
213 ao_rn_dbg("send_cmd %s%s\n", cmd, param ? param : "");
222 ao_rn_wait_char(AO_TICK_TYPE giveup_time)
226 ao_arch_block_interrupts();
227 while ((c = _ao_rn_pollchar()) == AO_READ_AGAIN) {
228 AO_TICK_SIGNED delay = (AO_TICK_SIGNED) (giveup_time - ao_time());
230 ao_arch_release_interrupts();
231 return AO_READ_AGAIN;
233 _ao_serial_rn_sleep_for(delay);
235 ao_arch_release_interrupts();
240 ao_rn_wait_for(int timeout, char *match)
242 char reply[AO_RN_MAX_REPLY_LEN + 1];
243 int match_len = strlen(match);
244 AO_TICK_TYPE giveup_time = ao_time() + timeout;
247 ao_rn_dbg("wait for %d, \"%s\"\n", timeout, match);
248 memset(reply, ' ', sizeof(reply));
249 while (memcmp(reply, match, match_len) != 0) {
250 c = ao_rn_wait_char(giveup_time);
251 if (c == AO_READ_AGAIN) {
252 ao_rn_dbg("\twait for timeout\n");
253 return AO_RN_TIMEOUT;
255 reply[match_len] = (char) c;
256 memmove(reply, reply+1, match_len);
257 reply[match_len] = '\0';
258 ao_rn_dbg("\tmatch now \"%s\"\n", reply);
260 ao_rn_dbg("\twait for ok\n");
265 ao_rn_wait_line(AO_TICK_TYPE giveup_time, char *line, int len)
269 ao_rn_dbg("wait line\n");
271 int c = ao_rn_wait_char(giveup_time);
274 if (c == AO_READ_AGAIN) {
275 ao_rn_dbg("\twait line timeout\n");
276 return AO_RN_TIMEOUT;
282 ao_rn_dbg("\twait line \"%s\"\n", line);
299 ao_rn_wait_status(void)
301 char message[AO_RN_MAX_REPLY_LEN];
302 AO_TICK_TYPE giveup_time = ao_time() + AO_RN_CMD_TIMEOUT;
305 ao_rn_dbg("wait status\n");
306 status = ao_rn_wait_line(giveup_time, message, sizeof (message));
307 if (status == AO_RN_OK)
308 if (strncmp(message, "AOK", 3) != 0)
309 status = AO_RN_ERROR;
320 ao_rn_dbg("set name...\n");
322 n = ao_serial_number;
326 ao_rn_send_cmd(AO_RN_SET_NAME_CMD "TeleBT-", s);
327 return ao_rn_wait_status();
331 ao_rn_get_name(char *name, int len)
333 ao_rn_dbg("get name...\n");
334 ao_rn_send_cmd(AO_RN_GET_NAME_CMD, NULL);
335 return ao_rn_wait_line(ao_time() + AO_RN_CMD_TIMEOUT, name, len);
339 ao_rn_check_link(void)
341 ao_rn_connected = 1 - ao_gpio_get(AO_RN_CONNECTED_PORT, AO_RN_CONNECTED_PIN, foo);
348 ao_wakeup(&ao_rn_connected);
352 ao_bt_panic(int where)
356 for (i = 0; i < 50; i++) {
357 ao_led_toggle(AO_BT_LED);
358 ao_delay(AO_MS_TO_TICKS(10));
360 ao_led_off(AO_BT_LED);
361 ao_delay(AO_MS_TO_TICKS(500));
362 for (i = 0; i < where; i++) {
363 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(200));
364 ao_delay(AO_MS_TO_TICKS(200));
369 static uint8_t ao_rn_stdio;
372 * Set the stdio echo for the bluetooth link
375 ao_rn_echo(uint8_t echo)
377 ao_stdios[ao_rn_stdio].echo = echo;
383 int status = AO_RN_ERROR;
387 ao_rn_dbg("ao_rn top\n");
389 /* Select CMD mode after the device gets out of reset */
390 ao_gpio_set(AO_RN_CMD_PORT, AO_RN_CMD_PIN, foo, AO_RN_CMD_CMD);
392 for (i = 0; i < 3; i++) {
393 ao_rn_dbg("reset device\n");
395 ao_gpio_set(AO_RN_RST_N_PORT, AO_RN_RST_N_PIN, foo, 0);
396 ao_delay(AO_MS_TO_TICKS(100));
398 /* Reboot the RN4678 and wait for it to start talking */
400 ao_gpio_set(AO_RN_RST_N_PORT, AO_RN_RST_N_PIN, foo, 1);
401 status = ao_rn_wait_for(AO_RN_REBOOT_TIMEOUT, AO_RN_REBOOT_MSG);
402 if (status != AO_RN_OK) {
403 ao_rn_dbg("reboot failed\n");
409 /* After it reboots, it can take a moment before it responds
412 (void) ao_rn_wait_for(AO_RN_REBOOT_TIMEOUT, "CMD> ");
414 /* Check to see if the name is already set and assume
415 * that the device is ready to go
417 status = ao_rn_get_name(name, sizeof (name));
418 if (status != AO_RN_OK) {
419 ao_rn_dbg("get name failed\n");
423 if (strncmp(name, "TeleBT-", 7) == 0) {
424 ao_rn_dbg("name is set\n");
429 /* Make the command pin control command/data mode */
430 ao_rn_send_cmd(AO_RN_SET_COMMAND_PIN, NULL);
431 if (ao_rn_wait_status() != AO_RN_OK) {
432 ao_rn_dbg("set command pin failed\n");
436 /* Select 'fast' mode to ignore command sequence (more or less) */
437 ao_rn_send_cmd(AO_RN_SET_FAST_MODE, NULL);
438 if (ao_rn_wait_status() != AO_RN_OK) {
439 ao_rn_dbg("set fast mode failed\n");
443 /* Finally, set the name. Doing this last makes it possible to check
444 * if the whole sequence has been done
446 if (ao_rn_set_name() != AO_RN_OK) {
447 ao_rn_dbg("set name failed\n");
451 /* After we've configured the device, go back around and reboot it
452 * as that's how we get the new configuration to take effect
455 ao_rn_dbg("ao_rn status %d\n", status);
457 if (status != AO_RN_OK)
460 ao_gpio_set(AO_RN_CMD_PORT, AO_RN_CMD_PIN, foo, AO_RN_CMD_DATA);
462 /* Wait for the hardware to finish sending messages, then clear the queue */
463 ao_delay(AO_MS_TO_TICKS(200));
466 ao_exti_enable(AO_RN_CONNECTED_PORT, AO_RN_CONNECTED_PIN);
469 ao_rn_stdio = ao_add_stdio(_ao_wrap_rn_pollchar,
477 ao_rn_dbg("RN running\n");
480 * Now just hang around and flash the blue LED when we've got
484 ao_arch_block_interrupts();
485 while (!ao_rn_connected)
486 ao_sleep(&ao_rn_connected);
487 ao_arch_release_interrupts();
488 while (ao_rn_connected) {
489 ao_led_for(AO_BT_LED, AO_MS_TO_TICKS(20));
490 ao_delay(AO_SEC_TO_TICKS(3));
496 * Separate debug code when things aren't working. Just dump
497 * inbound bluetooth characters to stdout
502 while (rn_cmd_running)
503 ao_delay(AO_MS_TO_TICKS(1000));
505 ao_arch_block_interrupts();
506 while ((c = _ao_wrap_rn_pollchar()) == AO_READ_AGAIN)
507 ao_sleep(&ao_serial_rn_rx_fifo);
508 ao_arch_release_interrupts();
514 static struct ao_task ao_rn_task;
523 * Factory reset. Flip pin P3_1 5 times within the first five
524 * seconds of power-on
527 /* Select our target output pin */
528 ao_enable_output(AO_RN_P3_1_PORT, AO_RN_P3_1_PIN, foo, v);
530 /* Turn off the BT device using the SW_BTN pin */
531 printf("Power down BT\n"); flush();
532 ao_gpio_set(AO_RN_SW_BTN_PORT, AO_RN_SW_BTN_PIN, foo, 0);
533 ao_delay(AO_MS_TO_TICKS(1000));
535 /* And turn it back on */
536 printf("Power up BT\n"); flush();
537 ao_gpio_set(AO_RN_SW_BTN_PORT, AO_RN_SW_BTN_PIN, foo, 1);
539 /* Right after power on, poke P3_1 five times to force a
542 for (i = 0; i < 10; i++) {
544 ao_delay(AO_MS_TO_TICKS(100));
545 ao_gpio_set(AO_RN_P3_1_PORT, AO_RN_P3_1_PIN, foo, v);
546 ao_led_toggle(AO_BT_LED);
549 /* And let P3_1 float again */
550 ao_enable_input(AO_RN_P3_1_PORT, AO_RN_P3_1_PIN, AO_EXTI_MODE_PULL_NONE);
553 static const struct ao_cmds rn_cmds[] = {
554 { ao_rn_factory, "F\0Factory reset rn4678" },
561 (void) ao_rn_set_name;
563 ao_serial_rn_set_speed(AO_SERIAL_SPEED_115200);
566 ao_enable_output(AO_RN_RST_N_PORT, AO_RN_RST_N_PIN, foo, 0);
569 ao_enable_output(AO_RN_SW_BTN_PORT, AO_RN_SW_BTN_PIN, foo, 1);
571 /* P3_7 command/data selector */
572 ao_enable_output(AO_RN_CMD_PORT, AO_RN_CMD_PIN, foo, AO_RN_CMD_CMD);
574 ao_enable_input(AO_RN_CONNECTED_PORT, AO_RN_CONNECTED_PIN, AO_EXTI_MODE_PULL_NONE);
575 ao_exti_setup(AO_RN_CONNECTED_PORT, AO_RN_CONNECTED_PIN,
576 AO_EXTI_MODE_FALLING|AO_EXTI_MODE_RISING|AO_EXTI_PRIORITY_LOW,
579 ao_cmd_register(rn_cmds);
580 ao_add_task(&ao_rn_task, ao_rn, "bluetooth");