blob: 8aa50ba6084580a5dc154a5b3115dfce62c4d856 [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 Cufi6400f0b2018-07-17 17:32:12 +020043static sys_slist_t avail_queue;
44static sys_slist_t lines_queue;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020045
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020046static u16_t cur;
47
48static int boot_uart_fifo_getline(char **line);
49static int boot_uart_fifo_init(void);
50
51int
52console_out(int c)
53{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020054 uart_poll_out(uart_dev, c);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020055
Carles Cufi5ceeddb2018-06-15 12:43:22 +020056 return c;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020057}
58
59void
60console_write(const char *str, int cnt)
61{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020062 int i;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020063
Carles Cufi5ceeddb2018-06-15 12:43:22 +020064 for (i = 0; i < cnt; i++) {
65 if (console_out((int)str[i]) == EOF) {
66 break;
67 }
68 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020069}
70
71int
72console_read(char *str, int str_size, int *newline)
73{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020074 char *line;
75 int len;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020076
Carles Cufi5ceeddb2018-06-15 12:43:22 +020077 len = boot_uart_fifo_getline(&line);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020078
Carles Cufi5ceeddb2018-06-15 12:43:22 +020079 if (line == NULL) {
80 *newline = 0;
81 return 0;
82 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020083
Carles Cufi5ceeddb2018-06-15 12:43:22 +020084 if (len > str_size - 1) {
85 len = str_size - 1;
86 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020087
Carles Cufi5ceeddb2018-06-15 12:43:22 +020088 memcpy(str, line, len);
89 str[len] = '\0';
90 *newline = 1;
91 return len + 1;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020092}
93
94int
95boot_console_init(void)
96{
Carles Cufi5ceeddb2018-06-15 12:43:22 +020097 int i;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020098
Carles Cufi6400f0b2018-07-17 17:32:12 +020099 /* Zephyr UART handler takes an empty buffer from avail_queue,
100 * stores UART input in it until EOL, and then puts it into
101 * lines_queue.
102 */
103 sys_slist_init(&avail_queue);
104 sys_slist_init(&lines_queue);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200105
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200106 for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
Carles Cufi6400f0b2018-07-17 17:32:12 +0200107 sys_slist_append(&avail_queue, &line_bufs[i].node);
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200108 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200109
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200110 return boot_uart_fifo_init();
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200111}
112
113static void
114boot_uart_fifo_callback(struct device *dev)
115{
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200116 static struct line_input *cmd;
117 u8_t byte;
118 int rx;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200119
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200120 while (uart_irq_update(uart_dev) &&
121 uart_irq_rx_ready(uart_dev)) {
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200122
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200123 rx = uart_fifo_read(uart_dev, &byte, 1);
124 if (rx != 1) {
125 continue;
126 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200127
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200128 if (!cmd) {
129 sys_snode_t *node;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200130
Carles Cufi6400f0b2018-07-17 17:32:12 +0200131 node = sys_slist_get(&avail_queue);
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200132 if (!node) {
133 return;
134 }
135 cmd = CONTAINER_OF(node, struct line_input, node);
136 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200137
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200138 if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) {
139 cmd->line[cur++] = byte;
140 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200141
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200142 if (byte == '\n') {
143 cmd->len = cur;
Carles Cufi6400f0b2018-07-17 17:32:12 +0200144 sys_slist_append(&lines_queue, &cmd->node);
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200145 cur = 0;
Carles Cufib124e392018-07-17 17:27:50 +0200146 cmd = NULL;
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200147 }
148 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200149}
150
151static int
152boot_uart_fifo_getline(char **line)
153{
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200154 static struct line_input *cmd;
155 sys_snode_t *node;
Andrzej Puzdrowski30117142018-06-18 14:43:14 +0200156 int key;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200157
Andrzej Puzdrowski30117142018-06-18 14:43:14 +0200158 key = irq_lock();
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200159 /* Recycle cmd buffer returned previous time */
160 if (cmd != NULL) {
Carles Cufi6400f0b2018-07-17 17:32:12 +0200161 if (sys_slist_peek_tail(&avail_queue) != &cmd->node) {
162 sys_slist_append(&avail_queue, &cmd->node);
Andrzej Puzdrowski30117142018-06-18 14:43:14 +0200163 }
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200164 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200165
Carles Cufi6400f0b2018-07-17 17:32:12 +0200166 node = sys_slist_get(&lines_queue);
Andrzej Puzdrowski30117142018-06-18 14:43:14 +0200167 irq_unlock(key);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200168
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200169 if (node == NULL) {
170 cmd = NULL;
171 *line = NULL;
Andrzej Puzdrowski30117142018-06-18 14:43:14 +0200172
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200173 return 0;
174 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200175
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200176 cmd = CONTAINER_OF(node, struct line_input, node);
177 *line = cmd->line;
178 return cmd->len;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200179}
180
181static int
182boot_uart_fifo_init(void)
183{
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200184 uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
185 u8_t c;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200186
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200187 if (!uart_dev) {
188 return (-1);
189 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200190
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200191 uart_irq_callback_set(uart_dev, boot_uart_fifo_callback);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200192
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200193 /* Drain the fifo */
194 while (uart_irq_rx_ready(uart_dev)) {
195 uart_fifo_read(uart_dev, &c, 1);
196 }
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200197
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200198 cur = 0;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200199
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200200 uart_irq_rx_enable(uart_dev);
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200201
Carles Cufi5e48c552018-06-15 12:58:08 +0200202 /* Enable all interrupts unconditionally. Note that this is due
203 * to Zephyr issue #8393. This should be removed once the
204 * issue is fixed in upstream Zephyr. */
205 irq_unlock(0);
206
Carles Cufi5ceeddb2018-06-15 12:43:22 +0200207 return 0;
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +0200208}