blob: f51dc94d908434a96a3296aa1957fbe98aefc3c9 [file] [log] [blame]
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +02001/*
2 * Copyright (c) 2017 Nordic Semiconductor ASA
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <stdio.h>
18#include <uart.h>
19#include <assert.h>
20#include <string.h>
21#include <zephyr.h>
22
23#ifdef CONFIG_UART_CONSOLE
24#error Zephyr UART console must been disabled if serial_adapter module is used.
25#endif
26
27/** @brief Console input representation
28 *
29 * This struct is used to represent an input line from a serial interface.
30 */
31struct line_input {
Carles Cufi5ceeddb2018-06-15 12:43:22 +020032 /** Required to use sys_slist */
33 sys_snode_t node;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020034
Carles Cufi5ceeddb2018-06-15 12:43:22 +020035 int len;
36 /** Buffer where the input line is recorded */
37 char line[CONFIG_BOOT_MAX_LINE_INPUT_LEN];
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020038};
39
40static struct device *uart_dev;
41static struct line_input line_bufs[2];
42
Carles Cufi5ceeddb2018-06-15 12:43:22 +020043static sys_slist_t free_queue;
44static sys_slist_t used_queue;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020045
Carles Cufi5ceeddb2018-06-15 12:43:22 +020046static sys_slist_t *avail_queue;
47static sys_slist_t *lines_queue;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020048static u16_t cur;
49
50static int boot_uart_fifo_getline(char **line);
51static int boot_uart_fifo_init(void);
52
53int
54console_out(int c)
55{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020056 uart_poll_out(uart_dev, c);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020057
Carles Cufi5ceeddb2018-06-15 12:43:22 +020058 return c;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020059}
60
61void
62console_write(const char *str, int cnt)
63{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020064 int i;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020065
Carles Cufi5ceeddb2018-06-15 12:43:22 +020066 for (i = 0; i < cnt; i++) {
67 if (console_out((int)str[i]) == EOF) {
68 break;
69 }
70 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020071}
72
73int
74console_read(char *str, int str_size, int *newline)
75{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020076 char *line;
77 int len;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020078
Carles Cufi5ceeddb2018-06-15 12:43:22 +020079 len = boot_uart_fifo_getline(&line);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020080
Carles Cufi5ceeddb2018-06-15 12:43:22 +020081 if (line == NULL) {
82 *newline = 0;
83 return 0;
84 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020085
Carles Cufi5ceeddb2018-06-15 12:43:22 +020086 if (len > str_size - 1) {
87 len = str_size - 1;
88 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020089
Carles Cufi5ceeddb2018-06-15 12:43:22 +020090 memcpy(str, line, len);
91 str[len] = '\0';
92 *newline = 1;
93 return len + 1;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020094}
95
96int
97boot_console_init(void)
98{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020099 int i;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200100
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200101 sys_slist_init(&free_queue);
102 sys_slist_init(&used_queue);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200103
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200104 for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
105 sys_slist_append(&free_queue, &line_bufs[i].node);
106 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200107
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200108 /* Zephyr UART handler takes an empty buffer from free_queue,
109 * stores UART input in it until EOL, and then puts it into
110 * used_queue.
111 */
112 avail_queue = &free_queue;
113 lines_queue = &used_queue;
114
115 return boot_uart_fifo_init();
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200116}
117
118static void
119boot_uart_fifo_callback(struct device *dev)
120{
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200121 static struct line_input *cmd;
122 u8_t byte;
123 int rx;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200124
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200125 while (uart_irq_update(uart_dev) &&
126 uart_irq_rx_ready(uart_dev)) {
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200127
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200128 rx = uart_fifo_read(uart_dev, &byte, 1);
129 if (rx != 1) {
130 continue;
131 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200132
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200133 if (!cmd) {
134 sys_snode_t *node;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200135
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200136 node = sys_slist_get(avail_queue);
137 if (!node) {
138 return;
139 }
140 cmd = CONTAINER_OF(node, struct line_input, node);
141 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200142
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200143 if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) {
144 cmd->line[cur++] = byte;
145 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200146
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200147 if (byte == '\n') {
148 cmd->len = cur;
149 sys_slist_append(lines_queue, &cmd->node);
150 cur = 0;
151 }
152 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200153}
154
155static int
156boot_uart_fifo_getline(char **line)
157{
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200158 static struct line_input *cmd;
159 sys_snode_t *node;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200160
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200161 /* Recycle cmd buffer returned previous time */
162 if (cmd != NULL) {
163 sys_slist_append(&free_queue, &cmd->node);
164 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200165
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200166 node = sys_slist_get(&used_queue);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200167
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200168 if (node == NULL) {
169 cmd = NULL;
170 *line = NULL;
171 return 0;
172 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200173
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200174 cmd = CONTAINER_OF(node, struct line_input, node);
175 *line = cmd->line;
176 return cmd->len;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200177}
178
179static int
180boot_uart_fifo_init(void)
181{
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200182 uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
183 u8_t c;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200184
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200185 if (!uart_dev) {
186 return (-1);
187 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200188
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200189 uart_irq_callback_set(uart_dev, boot_uart_fifo_callback);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200190
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200191 /* Drain the fifo */
192 while (uart_irq_rx_ready(uart_dev)) {
193 uart_fifo_read(uart_dev, &c, 1);
194 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200195
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200196 cur = 0;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200197
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200198 uart_irq_rx_enable(uart_dev);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200199
Carles Cufi5e48c552018-06-15 12:58:08 +0200200 /* Enable all interrupts unconditionally. Note that this is due
201 * to Zephyr issue #8393. This should be removed once the
202 * issue is fixed in upstream Zephyr. */
203 irq_unlock(0);
204
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200205 return 0;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200206}