blob: 88937f33312663cbbd28d62ee106b5dd30936786 [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 {
32 /** FIFO uses first 4 bytes itself, reserve space */
33 int _unused;
34
35 int len;
36 /** Buffer where the input line is recorded */
37 char line[CONFIG_BOOT_MAX_LINE_INPUT_LEN];
38};
39
40static struct device *uart_dev;
41static struct line_input line_bufs[2];
42
43static K_FIFO_DEFINE(free_queue);
44static K_FIFO_DEFINE(used_queue);
45
46static struct k_fifo *avail_queue;
47static struct k_fifo *lines_queue;
48static 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{
Andrzej Puzdrowskif6f652e2017-10-19 11:21:52 +020056 uart_poll_out(uart_dev, c);
57
58 return c;
59}
60
61void
62console_write(const char *str, int cnt)
63{
64 int i;
65
66 for (i = 0; i < cnt; i++) {
67 if (console_out((int)str[i]) == EOF) {
68 break;
69 }
70 }
71}
72
73int
74console_read(char *str, int str_size, int *newline)
75{
76 char * line;
77 int len;
78
79 len = boot_uart_fifo_getline(&line);
80
81 if (line == NULL) {
82 *newline = 0;
83 return 0;
84 }
85
86 if (len > str_size - 1) {
87 len = str_size - 1;
88 }
89
90 memcpy(str, line, len);
91 str[len] = '\0';
92 *newline = 1;
93 return len + 1;
94}
95
96int
97boot_console_init(void)
98{
99 int i;
100
101 for (i = 0; i < ARRAY_SIZE(line_bufs); i++) {
102 k_fifo_put(&free_queue, &line_bufs[i]);
103 }
104
105 /* Zephyr UART handler takes an empty buffer from free_queue,
106 * stores UART input in it until EOL, and then puts it into
107 * used_queue.
108 */
109 avail_queue = &free_queue;
110 lines_queue = &used_queue;
111
112 return boot_uart_fifo_init();
113}
114
115static void
116boot_uart_fifo_callback(struct device *dev)
117{
118 static struct line_input *cmd;
119 u8_t byte;
120 int rx;
121
122 while (uart_irq_update(uart_dev) &&
123 uart_irq_rx_ready(uart_dev)) {
124
125 rx = uart_fifo_read(uart_dev, &byte, 1);
126
127 if (!cmd) {
128 cmd = k_fifo_get(avail_queue, K_NO_WAIT);
129 if (!cmd) {
130 return;
131 }
132 }
133
134 if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) {
135 cmd->line[cur++] = byte;
136 }
137
138 if (byte == '\n') {
139 cmd->len = cur;
140 k_fifo_put(lines_queue, cmd);
141 cur = 0;
142 }
143
144 }
145}
146
147static int
148boot_uart_fifo_getline(char **line)
149{
150 static struct line_input *cmd;
151
152 /* Recycle cmd buffer returned previous time */
153 if (cmd != NULL) {
154 k_fifo_put(&free_queue, cmd);
155 }
156
157 cmd = k_fifo_get(&used_queue, K_NO_WAIT);
158
159 if (cmd == NULL) {
160 *line = NULL;
161 return 0;
162 }
163
164 *line = cmd->line;
165 return cmd->len;
166}
167
168static int
169boot_uart_fifo_init(void)
170{
171 uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
172 u8_t c;
173
174 if (!uart_dev) {
175 return (-1);
176 }
177
178 uart_irq_callback_set(uart_dev, boot_uart_fifo_callback);
179
180 /* Drain the fifo */
181 while (uart_irq_rx_ready(uart_dev)) {
182 uart_fifo_read(uart_dev, &c, 1);
183 }
184
185 cur = 0;
186
187 uart_irq_rx_enable(uart_dev);
188
189 return 0;
190}