Andrzej Puzdrowski | f6f652e | 2017-10-19 11:21:52 +0200 | [diff] [blame^] | 1 | /* |
| 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 | */ |
| 31 | struct 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 | |
| 40 | static struct device *uart_dev; |
| 41 | static struct line_input line_bufs[2]; |
| 42 | |
| 43 | static K_FIFO_DEFINE(free_queue); |
| 44 | static K_FIFO_DEFINE(used_queue); |
| 45 | |
| 46 | static struct k_fifo *avail_queue; |
| 47 | static struct k_fifo *lines_queue; |
| 48 | static u16_t cur; |
| 49 | |
| 50 | static int boot_uart_fifo_getline(char **line); |
| 51 | static int boot_uart_fifo_init(void); |
| 52 | |
| 53 | int |
| 54 | console_out(int c) |
| 55 | { |
| 56 | if ('\n' == c) { |
| 57 | uart_poll_out(uart_dev, '\r'); |
| 58 | } |
| 59 | uart_poll_out(uart_dev, c); |
| 60 | |
| 61 | return c; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | console_write(const char *str, int cnt) |
| 66 | { |
| 67 | int i; |
| 68 | |
| 69 | for (i = 0; i < cnt; i++) { |
| 70 | if (console_out((int)str[i]) == EOF) { |
| 71 | break; |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | int |
| 77 | console_read(char *str, int str_size, int *newline) |
| 78 | { |
| 79 | char * line; |
| 80 | int len; |
| 81 | |
| 82 | len = boot_uart_fifo_getline(&line); |
| 83 | |
| 84 | if (line == NULL) { |
| 85 | *newline = 0; |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | if (len > str_size - 1) { |
| 90 | len = str_size - 1; |
| 91 | } |
| 92 | |
| 93 | memcpy(str, line, len); |
| 94 | str[len] = '\0'; |
| 95 | *newline = 1; |
| 96 | return len + 1; |
| 97 | } |
| 98 | |
| 99 | int |
| 100 | boot_console_init(void) |
| 101 | { |
| 102 | int i; |
| 103 | |
| 104 | for (i = 0; i < ARRAY_SIZE(line_bufs); i++) { |
| 105 | k_fifo_put(&free_queue, &line_bufs[i]); |
| 106 | } |
| 107 | |
| 108 | /* 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(); |
| 116 | } |
| 117 | |
| 118 | static void |
| 119 | boot_uart_fifo_callback(struct device *dev) |
| 120 | { |
| 121 | static struct line_input *cmd; |
| 122 | u8_t byte; |
| 123 | int rx; |
| 124 | |
| 125 | while (uart_irq_update(uart_dev) && |
| 126 | uart_irq_rx_ready(uart_dev)) { |
| 127 | |
| 128 | rx = uart_fifo_read(uart_dev, &byte, 1); |
| 129 | |
| 130 | if (!cmd) { |
| 131 | cmd = k_fifo_get(avail_queue, K_NO_WAIT); |
| 132 | if (!cmd) { |
| 133 | return; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | if (cur < CONFIG_BOOT_MAX_LINE_INPUT_LEN) { |
| 138 | cmd->line[cur++] = byte; |
| 139 | } |
| 140 | |
| 141 | if (byte == '\n') { |
| 142 | cmd->len = cur; |
| 143 | k_fifo_put(lines_queue, cmd); |
| 144 | cur = 0; |
| 145 | } |
| 146 | |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | static int |
| 151 | boot_uart_fifo_getline(char **line) |
| 152 | { |
| 153 | static struct line_input *cmd; |
| 154 | |
| 155 | /* Recycle cmd buffer returned previous time */ |
| 156 | if (cmd != NULL) { |
| 157 | k_fifo_put(&free_queue, cmd); |
| 158 | } |
| 159 | |
| 160 | cmd = k_fifo_get(&used_queue, K_NO_WAIT); |
| 161 | |
| 162 | if (cmd == NULL) { |
| 163 | *line = NULL; |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | *line = cmd->line; |
| 168 | return cmd->len; |
| 169 | } |
| 170 | |
| 171 | static int |
| 172 | boot_uart_fifo_init(void) |
| 173 | { |
| 174 | uart_dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME); |
| 175 | u8_t c; |
| 176 | |
| 177 | if (!uart_dev) { |
| 178 | return (-1); |
| 179 | } |
| 180 | |
| 181 | uart_irq_callback_set(uart_dev, boot_uart_fifo_callback); |
| 182 | |
| 183 | /* Drain the fifo */ |
| 184 | while (uart_irq_rx_ready(uart_dev)) { |
| 185 | uart_fifo_read(uart_dev, &c, 1); |
| 186 | } |
| 187 | |
| 188 | cur = 0; |
| 189 | |
| 190 | uart_irq_rx_enable(uart_dev); |
| 191 | |
| 192 | return 0; |
| 193 | } |