Skip to content

Commit

Permalink
zephyr: Use sys_slist instead of k_fifo in serial adapter
Browse files Browse the repository at this point in the history
The k_fifo_* primitives are not available when multithreading is
disabled. Use sys_slist_* instead.

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
  • Loading branch information
carlescufi committed Jun 16, 2018
1 parent eae6313 commit 13a9f25
Showing 1 changed file with 105 additions and 94 deletions.
199 changes: 105 additions & 94 deletions boot/zephyr/serial_adapter.c
Expand Up @@ -29,22 +29,22 @@
* This struct is used to represent an input line from a serial interface.
*/
struct line_input {
/** FIFO uses first 4 bytes itself, reserve space */
int _unused;
/** Required to use sys_slist */
sys_snode_t node;

int len;
/** Buffer where the input line is recorded */
char line[CONFIG_BOOT_MAX_LINE_INPUT_LEN];
int len;
/** Buffer where the input line is recorded */
char line[CONFIG_BOOT_MAX_LINE_INPUT_LEN];
};

static struct device *uart_dev;
static struct line_input line_bufs[2];

static K_FIFO_DEFINE(free_queue);
static K_FIFO_DEFINE(used_queue);
static sys_slist_t free_queue;
static sys_slist_t used_queue;

static struct k_fifo *avail_queue;
static struct k_fifo *lines_queue;
static sys_slist_t *avail_queue;
static sys_slist_t *lines_queue;
static u16_t cur;

static int boot_uart_fifo_getline(char **line);
Expand All @@ -53,138 +53,149 @@ static int boot_uart_fifo_init(void);
int
console_out(int c)
{
uart_poll_out(uart_dev, c);
uart_poll_out(uart_dev, c);

return c;
return c;
}

void
console_write(const char *str, int cnt)
{
int i;
int i;

for (i = 0; i < cnt; i++) {
if (console_out((int)str[i]) == EOF) {
break;
}
}
for (i = 0; i < cnt; i++) {
if (console_out((int)str[i]) == EOF) {
break;
}
}
}

int
console_read(char *str, int str_size, int *newline)
{
char * line;
int len;
char *line;
int len;

len = boot_uart_fifo_getline(&line);
len = boot_uart_fifo_getline(&line);

if (line == NULL) {
*newline = 0;
return 0;
}
if (line == NULL) {
*newline = 0;
return 0;
}

if (len > str_size - 1) {
len = str_size - 1;
}
if (len > str_size - 1) {
len = str_size - 1;
}

memcpy(str, line, len);
str[len] = '\0';
*newline = 1;
return len + 1;
memcpy(str, line, len);
str[len] = '\0';
*newline = 1;
return len + 1;
}

int
boot_console_init(void)
{
int i;
int i;

for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
k_fifo_put(&free_queue, &line_bufs[i]);
}
sys_slist_init(&free_queue);
sys_slist_init(&used_queue);

/* Zephyr UART handler takes an empty buffer from free_queue,
* stores UART input in it until EOL, and then puts it into
* used_queue.
*/
avail_queue = &free_queue;
lines_queue = &used_queue;
for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
sys_slist_append(&free_queue, &line_bufs[i].node);
}

return boot_uart_fifo_init();
/* Zephyr UART handler takes an empty buffer from free_queue,
* stores UART input in it until EOL, and then puts it into
* used_queue.
*/
avail_queue = &free_queue;
lines_queue = &used_queue;

return boot_uart_fifo_init();
}

static void
boot_uart_fifo_callback(struct device *dev)
{
static struct line_input *cmd;
u8_t byte;
int rx;

while (uart_irq_update(uart_dev) &&
uart_irq_rx_ready(uart_dev)) {

rx = uart_fifo_read(uart_dev, &byte, 1);

if (!cmd) {
cmd = k_fifo_get(avail_queue, K_NO_WAIT);
if (!cmd) {
return;
}
}

if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) {
cmd->line[cur++] = byte;
}

if (byte == '\n') {
cmd->len = cur;
k_fifo_put(lines_queue, cmd);
cur = 0;
}

}
static struct line_input *cmd;
u8_t byte;
int rx;

while (uart_irq_update(uart_dev) &&
uart_irq_rx_ready(uart_dev)) {

rx = uart_fifo_read(uart_dev, &byte, 1);
if (rx != 1) {
continue;
}

if (!cmd) {
sys_snode_t *node;

node = sys_slist_get(avail_queue);
if (!node) {
return;
}
cmd = CONTAINER_OF(node, struct line_input, node);
}

if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) {
cmd->line[cur++] = byte;
}

if (byte == '\n') {
cmd->len = cur;
sys_slist_append(lines_queue, &cmd->node);
cur = 0;
}
}
}

static int
boot_uart_fifo_getline(char **line)
{
static struct line_input *cmd;
static struct line_input *cmd;
sys_snode_t *node;

/* Recycle cmd buffer returned previous time */
if (cmd != NULL) {
k_fifo_put(&free_queue, cmd);
}
/* Recycle cmd buffer returned previous time */
if (cmd != NULL) {
sys_slist_append(&free_queue, &cmd->node);
}

cmd = k_fifo_get(&used_queue, K_NO_WAIT);
node = sys_slist_get(&used_queue);

if (cmd == NULL) {
*line = NULL;
return 0;
}
if (node == NULL) {
cmd = NULL;
*line = NULL;
return 0;
}

*line = cmd->line;
return cmd->len;
cmd = CONTAINER_OF(node, struct line_input, node);
*line = cmd->line;
return cmd->len;
}

static int
boot_uart_fifo_init(void)
{
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
u8_t c;
uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
u8_t c;

if (!uart_dev) {
return (-1);
}
if (!uart_dev) {
return (-1);
}

uart_irq_callback_set(uart_dev, boot_uart_fifo_callback);
uart_irq_callback_set(uart_dev, boot_uart_fifo_callback);

/* Drain the fifo */
while (uart_irq_rx_ready(uart_dev)) {
uart_fifo_read(uart_dev, &c, 1);
}
/* Drain the fifo */
while (uart_irq_rx_ready(uart_dev)) {
uart_fifo_read(uart_dev, &c, 1);
}

cur = 0;
cur = 0;

uart_irq_rx_enable(uart_dev);
uart_irq_rx_enable(uart_dev);

return 0;
return 0;
}

0 comments on commit 13a9f25

Please sign in to comment.