1
0
Fork 0

Updates for modern Zephyr

SYS_LOG -> LOG
Updated options for bootloader, &c
This commit is contained in:
Shawn Nock 2019-04-01 10:16:24 -04:00
parent db4235571e
commit 1bc8b9ccb7
3 changed files with 48 additions and 44 deletions

View File

@ -1,8 +1,7 @@
cmake_minimum_required(VERSION 3.13.1)
set(CONF_FILE c3.conf) set(CONF_FILE c3.conf)
include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE)
project(NONE) project(hci_uart_h5)
target_link_libraries(app subsys__bluetooth)
target_sources(app PRIVATE src/main.c) target_sources(app PRIVATE src/main.c)

View File

@ -3,7 +3,6 @@ CONFIG_UART_CONSOLE=n
CONFIG_GPIO=y CONFIG_GPIO=y
CONFIG_SERIAL=y CONFIG_SERIAL=y
CONFIG_UART_INTERRUPT_DRIVEN=y CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_NRF5_BAUD_RATE=115200
CONFIG_MAIN_STACK_SIZE=512 CONFIG_MAIN_STACK_SIZE=512
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512 CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=512
CONFIG_BT=y CONFIG_BT=y
@ -14,12 +13,13 @@ CONFIG_BT_CTLR_DTM_HCI=y
CONFIG_BT_CTLR_ASSERT_HANDLER=y CONFIG_BT_CTLR_ASSERT_HANDLER=y
# Bootloader stuff # Bootloader stuff
CONFIG_TEXT_SECTION_OFFSET=0x200 CONFIG_BOOTLOADER_MCUBOOT=y
CONFIG_REBOOT=y CONFIG_REBOOT=y
# Console # Console
CONFIG_HAS_SEGGER_RTT=y CONFIG_HAS_SEGGER_RTT=y
CONFIG_SYS_LOG=y CONFIG_USE_SEGGER_RTT=y
CONFIG_SYS_LOG_DEFAULT_LEVEL=4 CONFIG_LOG=y
CONFIG_LOG_DEFAULT_LEVEL=4
CONFIG_RTT_CONSOLE=y CONFIG_RTT_CONSOLE=y
CONFIG_CONSOLE=y CONFIG_CONSOLE=y

View File

@ -13,7 +13,7 @@
#include <zephyr.h> #include <zephyr.h>
#include <arch/cpu.h> #include <arch/cpu.h>
#include <misc/byteorder.h> #include <misc/byteorder.h>
#include <logging/sys_log.h> #include <logging/log.h>
#include <misc/util.h> #include <misc/util.h>
#include <device.h> #include <device.h>
@ -28,7 +28,7 @@
#include <bluetooth/hci_raw.h> #include <bluetooth/hci_raw.h>
#include <gpio.h> #include <gpio.h>
#include <board.h> //#include <board.h>
#include <misc/reboot.h> #include <misc/reboot.h>
#include "common/log.h" #include "common/log.h"
@ -54,10 +54,10 @@ NET_BUF_POOL_DEFINE(cmd_tx_pool, CONFIG_BT_HCI_CMD_COUNT, CMD_BUF_SIZE,
NET_BUF_POOL_DEFINE(acl_tx_pool, TX_BUF_COUNT, BT_BUF_ACL_SIZE, NET_BUF_POOL_DEFINE(acl_tx_pool, TX_BUF_COUNT, BT_BUF_ACL_SIZE,
BT_BUF_USER_DATA_MIN, NULL); BT_BUF_USER_DATA_MIN, NULL);
static BT_STACK_NOINIT(tx_stack, 256); static K_THREAD_STACK_DEFINE(tx_stack, 256);
static struct k_thread tx_thread_data; static struct k_thread tx_thread_data;
static BT_STACK_NOINIT(unproc_stack, 256); static K_THREAD_STACK_DEFINE(unproc_stack, 256);
static struct k_thread unproc_thread_data; static struct k_thread unproc_thread_data;
static struct k_delayed_work ack_work; static struct k_delayed_work ack_work;
@ -167,6 +167,7 @@ static inline void bt_uart_drain(struct device *dev)
} }
} }
/*
static void process_unack(void) static void process_unack(void)
{ {
u8_t next_seq = h5.tx_seq; u8_t next_seq = h5.tx_seq;
@ -176,39 +177,39 @@ static void process_unack(void)
return; return;
} }
SYS_LOG_DBG("rx_ack %u tx_ack %u tx_seq %u unack_queue_len %u", LOG_DBG("rx_ack %u tx_ack %u tx_seq %u unack_queue_len %u",
h5.rx_ack, h5.tx_ack, h5.tx_seq, unack_queue_len); h5.rx_ack, h5.tx_ack, h5.tx_seq, unack_queue_len);
while (unack_queue_len > 0) { while (unack_queue_len > 0) {
if (next_seq == h5.rx_ack) { if (next_seq == h5.rx_ack) {
/* Next sequence number is the same as last received // Next sequence number is the same as last received
* ack number // ack number
*/ //
break; break;
} }
number_removed--; number_removed--;
/* Similar to (n - 1) % 8 with unsigned conversion */ // Similar to (n - 1) % 8 with unsigned conversion
next_seq = (next_seq - 1) & 0x07; next_seq = (next_seq - 1) & 0x07;
} }
if (next_seq != h5.rx_ack) { if (next_seq != h5.rx_ack) {
SYS_LOG_ERR("Wrong sequence: rx_ack %u tx_seq %u next_seq %u", LOG_ERR("Wrong sequence: rx_ack %u tx_seq %u next_seq %u",
h5.rx_ack, h5.tx_seq, next_seq); h5.rx_ack, h5.tx_seq, next_seq);
} }
SYS_LOG_DBG("Need to remove %u packet from the queue", number_removed); LOG_DBG("Need to remove %u packet from the queue", number_removed);
while (number_removed) { while (number_removed) {
struct net_buf *buf = net_buf_get(&h5.unack_queue, K_NO_WAIT); struct net_buf *buf = net_buf_get(&h5.unack_queue, K_NO_WAIT);
if (!buf) { if (!buf) {
SYS_LOG_ERR("Unack queue is empty"); LOG_ERR("Unack queue is empty");
break; break;
} }
/* TODO: print or do something with packet */ // TODO: print or do something with packet
SYS_LOG_DBG("Remove buf from the unack_queue"); LOG_DBG("Remove buf from the unack_queue");
//net_buf_unref(buf); //net_buf_unref(buf);
unack_queue_len--; unack_queue_len--;
@ -219,16 +220,17 @@ static void process_unack(void)
static void h5_print_header(const u8_t *hdr, const char *str) static void h5_print_header(const u8_t *hdr, const char *str)
{ {
if (H5_HDR_RELIABLE(hdr)) { if (H5_HDR_RELIABLE(hdr)) {
SYS_LOG_DBG("%s REL: seq %u ack %u crc %u type %u len %u", LOG_DBG("%s REL: seq %u ack %u crc %u type %u len %u",
str, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr), str, H5_HDR_SEQ(hdr), H5_HDR_ACK(hdr),
H5_HDR_CRC(hdr), H5_HDR_PKT_TYPE(hdr), H5_HDR_CRC(hdr), H5_HDR_PKT_TYPE(hdr),
H5_HDR_LEN(hdr)); H5_HDR_LEN(hdr));
} else { } else {
SYS_LOG_DBG("%s UNREL: ack %u crc %u type %u len %u", LOG_DBG("%s UNREL: ack %u crc %u type %u len %u",
str, H5_HDR_ACK(hdr), H5_HDR_CRC(hdr), str, H5_HDR_ACK(hdr), H5_HDR_CRC(hdr),
H5_HDR_PKT_TYPE(hdr), H5_HDR_LEN(hdr)); H5_HDR_PKT_TYPE(hdr), H5_HDR_LEN(hdr));
} }
} }
*/
static void hexdump(const char *str, const u8_t *packet, size_t length) static void hexdump(const char *str, const u8_t *packet, size_t length)
{ {
@ -323,7 +325,7 @@ static void retx_timeout(struct k_work *work)
{ {
ARG_UNUSED(work); ARG_UNUSED(work);
SYS_LOG_DBG("unack_queue_len %u", unack_queue_len); LOG_DBG("unack_queue_len %u", unack_queue_len);
if (unack_queue_len) { if (unack_queue_len) {
struct k_fifo tmp_queue; struct k_fifo tmp_queue;
@ -355,7 +357,7 @@ static void ack_timeout(struct k_work *work)
{ {
ARG_UNUSED(work); ARG_UNUSED(work);
SYS_LOG_DBG(""); LOG_DBG("");
h5_send(NULL, HCI_3WIRE_ACK_PKT, 0); h5_send(NULL, HCI_3WIRE_ACK_PKT, 0);
@ -382,7 +384,7 @@ int unslip_next_byte(struct net_buf *buf) {
if (next == SLIP_ESC_DELIM) { if (next == SLIP_ESC_DELIM) {
return SLIP_DELIMITER; return SLIP_DELIMITER;
} }
SYS_LOG_WRN("Bad Escape Seqence: %02X %02X", SLIP_ESC, next); LOG_WRN("Bad Escape Seqence: %02X %02X", SLIP_ESC, next);
return -2; return -2;
} }
@ -440,7 +442,7 @@ int pull_header(struct net_buf *buf, u8_t *hdr) {
// Checksum // Checksum
if (((hdr[3] + hdr[0] + hdr[1] + hdr[2]) & 0xff) != 0xff) { if (((hdr[3] + hdr[0] + hdr[1] + hdr[2]) & 0xff) != 0xff) {
SYS_LOG_WRN("Invalid Header Checksum\n"); LOG_WRN("Invalid Header Checksum\n");
} }
return 0; return 0;
@ -465,7 +467,7 @@ static void unproc_thread(void) {
case HCI_ACLDATA_PKT: case HCI_ACLDATA_PKT:
rx_buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT); rx_buf = net_buf_alloc(&acl_tx_pool, K_NO_WAIT);
if (!rx_buf) { if (!rx_buf) {
SYS_LOG_WRN("No available data buffers"); LOG_WRN("No available data buffers");
sys_reboot(SYS_REBOOT_COLD); sys_reboot(SYS_REBOOT_COLD);
goto next; goto next;
} }
@ -474,28 +476,28 @@ static void unproc_thread(void) {
case HCI_COMMAND_PKT: case HCI_COMMAND_PKT:
rx_buf = net_buf_alloc(&cmd_tx_pool, K_NO_WAIT); rx_buf = net_buf_alloc(&cmd_tx_pool, K_NO_WAIT);
if (!rx_buf) { if (!rx_buf) {
SYS_LOG_WRN("No available data buffers"); LOG_WRN("No available data buffers");
sys_reboot(SYS_REBOOT_COLD); sys_reboot(SYS_REBOOT_COLD);
goto next; goto next;
} }
bt_buf_set_type(rx_buf, BT_BUF_CMD); bt_buf_set_type(rx_buf, BT_BUF_CMD);
break; break;
case HCI_3WIRE_ACK_PKT: case HCI_3WIRE_ACK_PKT:
SYS_LOG_DBG("ACK PACKET"); LOG_DBG("ACK PACKET");
h5.rx_ack = H5_HDR_ACK(hdr); h5.rx_ack = H5_HDR_ACK(hdr);
goto next; goto next;
break; break;
case HCI_3WIRE_LINK_PKT: case HCI_3WIRE_LINK_PKT:
rx_buf = net_buf_alloc(&h5_pool, K_NO_WAIT); rx_buf = net_buf_alloc(&h5_pool, K_NO_WAIT);
if (!rx_buf) { if (!rx_buf) {
SYS_LOG_WRN("No available signal buffers"); LOG_WRN("No available signal buffers");
sys_reboot(SYS_REBOOT_COLD); sys_reboot(SYS_REBOOT_COLD);
goto next; goto next;
} }
SYS_LOG_DBG("ALLOC %p", rx_buf); LOG_DBG("ALLOC %p", rx_buf);
break; break;
default: default:
SYS_LOG_ERR("Wrong packet type from host: %u", H5_HDR_PKT_TYPE(hdr)); LOG_ERR("Wrong packet type from host: %u", H5_HDR_PKT_TYPE(hdr));
goto next; goto next;
} }
@ -505,7 +507,7 @@ static void unproc_thread(void) {
} }
if (H5_HDR_LEN(hdr) != rx_buf->len) { if (H5_HDR_LEN(hdr) != rx_buf->len) {
SYS_LOG_ERR("Payload too short\n"); LOG_ERR("Payload too short\n");
goto next; goto next;
} }
@ -517,7 +519,7 @@ static void unproc_thread(void) {
*/ */
if (H5_HDR_RELIABLE(hdr) && if (H5_HDR_RELIABLE(hdr) &&
H5_HDR_SEQ(hdr) != h5.tx_ack) { H5_HDR_SEQ(hdr) != h5.tx_ack) {
SYS_LOG_ERR("Seq expected %u got %u. Drop packet", h5.tx_ack, LOG_ERR("Seq expected %u got %u. Drop packet", h5.tx_ack,
H5_HDR_SEQ(hdr)); H5_HDR_SEQ(hdr));
goto next; goto next;
} }
@ -542,11 +544,11 @@ static void unproc_thread(void) {
break; break;
case HCI_COMMAND_PKT: case HCI_COMMAND_PKT:
case HCI_ACLDATA_PKT: case HCI_ACLDATA_PKT:
//SYS_LOG_DBG("Adding to controller queue\n"); //LOG_DBG("Adding to controller queue\n");
net_buf_put(&h5.controller_queue, rx_buf); net_buf_put(&h5.controller_queue, rx_buf);
break; break;
default: default:
SYS_LOG_WRN("Unknown packet type %u\n", H5_HDR_PKT_TYPE(hdr)); LOG_WRN("Unknown packet type %u\n", H5_HDR_PKT_TYPE(hdr));
break; break;
} }
next: next:
@ -559,7 +561,7 @@ static void h5_send_config(void);
static void tx_thread(void) static void tx_thread(void)
{ {
SYS_LOG_DBG("TX Thread is alive."); LOG_DBG("TX Thread is alive.");
while (true) { while (true) {
struct net_buf *buf; struct net_buf *buf;
@ -596,7 +598,7 @@ static void tx_thread(void)
static void h5_init(void) static void h5_init(void)
{ {
SYS_LOG_DBG(""); LOG_DBG("");
h5.link_state = UNINIT; h5.link_state = UNINIT;
h5.rx_state = START; h5.rx_state = START;
@ -635,7 +637,7 @@ static void h5_init(void)
static int h5_open(struct device *unused) static int h5_open(struct device *unused)
{ {
SYS_LOG_DBG("Open"); LOG_DBG("Open");
hci_uart_dev = device_get_binding(CONFIG_BT_CTLR_TO_HOST_UART_DEV_NAME); hci_uart_dev = device_get_binding(CONFIG_BT_CTLR_TO_HOST_UART_DEV_NAME);
if (!hci_uart_dev) { if (!hci_uart_dev) {
@ -713,6 +715,9 @@ void gpio_callback(struct device *port,
sys_reboot(SYS_REBOOT_COLD); sys_reboot(SYS_REBOOT_COLD);
} }
#define BOOTLOADER_REQ_GPIO_PIN 9
#define BOOTLOADER_STATUS_GPIO_PIN 9
void gpio_init() { void gpio_init() {
int ret; int ret;
gpio_dev = device_get_binding("GPIO_0"); gpio_dev = device_get_binding("GPIO_0");
@ -739,7 +744,7 @@ void gpio_init() {
void main(void) void main(void)
{ {
SYS_LOG_DBG("Start"); LOG_DBG("Start");
gpio_init(); gpio_init();
// Adds controller output to host output queue // Adds controller output to host output queue
bt_enable_raw(&h5.host_queue); bt_enable_raw(&h5.host_queue);
@ -771,7 +776,7 @@ void main(void)
h5.tx_win = conf_rsp[2] & 0x7; h5.tx_win = conf_rsp[2] & 0x7;
h5.tx_seq = 0; h5.tx_seq = 0;
h5.tx_ack = 0; h5.tx_ack = 0;
SYS_LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win); LOG_DBG("Finished H5 configuration, tx_win %u", h5.tx_win);
} }
} else if (h5.link_state == ACTIVE) { } else if (h5.link_state == ACTIVE) {
if (packet_is_config(buf)) { if (packet_is_config(buf)) {
@ -788,7 +793,7 @@ void main(void)
//hexdump("From CTRL To HOST => ", buf->data, buf->len); //hexdump("From CTRL To HOST => ", buf->data, buf->len);
h5_send(buf->data, HCI_EVENT_PKT, buf->len); h5_send(buf->data, HCI_EVENT_PKT, buf->len);
} else { } else {
SYS_LOG_DBG("WTF is this (%u): ", bt_buf_get_type(buf)); LOG_DBG("WTF is this (%u): ", bt_buf_get_type(buf));
hexdump("CTRL WTF: ", buf->data, buf->len); hexdump("CTRL WTF: ", buf->data, buf->len);
} }
} }