1
0
Fork 0

Simplifies mainloop and renames conf files for automation

This commit is contained in:
Shawn Nock 2019-07-16 12:33:43 -04:00
parent cae7b49c35
commit 9ee8b2dd22
4 changed files with 100 additions and 49 deletions

View File

@ -0,0 +1,25 @@
CONFIG_STDOUT_CONSOLE=y
CONFIG_UART_CONSOLE=n
CONFIG_GPIO=y
CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_MAIN_STACK_SIZE=512
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512
CONFIG_BT=y
CONFIG_BT_HCI_RAW=y
CONFIG_BT_MAX_CONN=16
CONFIG_BT_TINYCRYPT_ECC=n
CONFIG_BT_CTLR_DTM_HCI=y
CONFIG_BT_CTLR_ASSERT_HANDLER=y
# Bootloader stuff
CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_REBOOT=y
# Console
CONFIG_HAS_SEGGER_RTT=y
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_RTT_CONSOLE=y
CONFIG_CONSOLE=y

View File

@ -113,6 +113,12 @@ static bool reliable_packet(u8_t type)
#define H5_SET_LEN(hdr, len) (((hdr)[1] |= ((len) & 0x0f) << 4), \
((hdr)[2] |= (len) >> 4))
typedef enum {
UNINIT,
INIT,
ACTIVE,
} linkstate_t;
static struct h5 {
//struct net_buf *rx_buf;
@ -127,11 +133,7 @@ static struct h5 {
u8_t rx_ack;
enum {
UNINIT,
INIT,
ACTIVE,
} link_state;
linkstate_t link_state;
enum {
START,
@ -554,7 +556,7 @@ static void tx_thread(void)
static void h5_init(void)
{
LOG_DBG("");
LOG_DBG("h5_init");
h5.link_state = UNINIT;
h5.rx_state = START;
@ -628,23 +630,23 @@ void bt_ctlr_assert_handle(char *file, u32_t line)
DEVICE_INIT(hci_uart, "hci_uart", &h5_open, NULL, NULL,
APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEVICE);
bool _link_ctrl_memcmp(struct net_buf const * const buf, u8_t const * const ref) {
bool _link_ctrl_memcmp(struct net_buf const * buf, u8_t const * const ref) {
return !memcmp(buf->data, ref, 2);
}
bool packet_is_sync(struct net_buf *buf) {
bool packet_is_sync(struct net_buf const *buf) {
return _link_ctrl_memcmp(buf, sync_req);
}
bool packet_is_sync_response(struct net_buf *buf) {
bool packet_is_sync_response(struct net_buf const *buf) {
return _link_ctrl_memcmp(buf, sync_rsp);
}
bool packet_is_config(struct net_buf *buf) {
bool packet_is_config(struct net_buf const *buf) {
return _link_ctrl_memcmp(buf, conf_req);
}
static bool packet_is_config_response(struct net_buf *buf) {
static bool packet_is_config_response(struct net_buf const *buf) {
return _link_ctrl_memcmp(buf, conf_rsp);
}
@ -699,6 +701,57 @@ void gpio_init() {
if (ret) {
printk("Error enabling callback!\n");
}
}
static linkstate_t do_uninit(struct net_buf const *buf) {
if (packet_is_sync_response(buf)) {
h5_send_config();
return INIT;
} else {
/* SYNC is the answer to any non-SYNC_RESP packets in UNINIT state */
h5_send_sync();
return UNINIT;
}
}
static linkstate_t do_init(struct net_buf const *buf) {
if (packet_is_config(buf)) {
h5_send_config_response();
return INIT;
} else if (packet_is_config_response(buf)) {
h5.tx_win = conf_rsp[2] & 0x7;
h5.tx_seq = 0;
h5.tx_ack = 0;
LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win);
return ACTIVE;
}
}
static linkstate_t do_active(struct net_buf const *buf) {
if (packet_is_config(buf)) {
h5_send_config_response();
return ACTIVE;
}
if (packet_is_config_response(buf)) {
return ACTIVE;
}
if (packet_is_sync_response(buf) || packet_is_config(buf)) {
h5_send_sync();
return UNINIT;
}
// Presumably something from the controller
u8_t type = bt_buf_get_type(buf);
if (type == BT_BUF_EVT) {
LOG_HEXDUMP_DBG(buf->data, buf->len, "CTRL -> HOST");
h5_send(buf->data, HCI_EVENT_PKT, buf->len);
} else {
LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue");
}
}
void main(void)
@ -718,43 +771,16 @@ void main(void)
goto next;
}
if (h5.link_state == UNINIT) {
if (packet_is_sync_response(buf)) {
h5.link_state = INIT;
h5_send_config();
} else {
/* SYNC is the answer to any non-SYNC_RESP packets in UNINIT
state */
h5_send_sync();
}
} else if (h5.link_state == INIT) {
if (packet_is_config(buf)) {
h5_send_config_response();
} else if (packet_is_config_response(buf)) {
h5.link_state = ACTIVE;
h5.tx_win = conf_rsp[2] & 0x7;
h5.tx_seq = 0;
h5.tx_ack = 0;
LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win);
}
} else if (h5.link_state == ACTIVE) {
if (packet_is_config(buf)) {
h5_send_config_response();
} else if (packet_is_config_response(buf)) {
goto next;
} else if (packet_is_sync_response(buf) || packet_is_config(buf)) {
h5.link_state = UNINIT;
h5_send_sync();
} else {
// Presumably something from the controller
u8_t type = bt_buf_get_type(buf);
if (type == BT_BUF_EVT) {
LOG_HEXDUMP_DBG(buf->data, buf->len, "CTRL -> HOST");
h5_send(buf->data, HCI_EVENT_PKT, buf->len);
} else {
LOG_HEXDUMP_ERR(buf->data, buf->len, "Unexpected buffer in host_queue");
}
}
switch (h5.link_state) {
case UNINIT:
h5.link_state = do_uninit(buf);
break;
case INIT:
h5.link_state = do_init(buf);
break;
case ACTIVE:
h5.link_state = do_active(buf);
break;
}
next:
net_buf_unref(buf);