OpenCores
URL https://opencores.org/ocsvn/neorv32/neorv32/trunk

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_uart.c] - Blame information for rev 74

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 zero_gravi
// #################################################################################################
2
// # << NEORV32: neorv32_uart.c - Universal Asynchronous Receiver/Transmitter (UART) HW Driver >>  #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6 72 zero_gravi
// # Copyright (c) 2022, Stephan Nolting. All rights reserved.                                     #
7 2 zero_gravi
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEORV32 Processor - https://github.com/stnolting/neorv32              (c) Stephan Nolting #
33
// #################################################################################################
34
 
35
 
36
/**********************************************************************//**
37
 * @file neorv32_uart.c
38 50 zero_gravi
 * @brief Universal asynchronous receiver/transmitter (UART0/UART1) HW driver source file.
39 2 zero_gravi
 *
40 50 zero_gravi
 * @warning UART0 (primary UART) is used as default user console interface for all NEORV32 software framework/library functions.
41
 *
42
 * @note These functions should only be used if the UART0/UART1 unit was synthesized (IO_UART0_EN = true / IO_UART1_EN = true).
43 2 zero_gravi
 **************************************************************************/
44
 
45
#include "neorv32.h"
46
#include "neorv32_uart.h"
47
#include <string.h>
48 61 zero_gravi
#include <stdarg.h>
49 2 zero_gravi
 
50
/// \cond
51
// Private functions
52 65 zero_gravi
static void __neorv32_uart_itoa(uint32_t x, char *res) __attribute__((unused)); // GCC: do not output a warning when this variable is unused
53
static void __neorv32_uart_tohex(uint32_t x, char *res) __attribute__((unused)); // GCC: do not output a warning when this variable is unused
54 72 zero_gravi
static void __neorv32_uart_touppercase(uint32_t len, char *ptr) __attribute__((unused)); // GCC: do not output a warning when this variable is unused
55 2 zero_gravi
/// \endcond
56
 
57
 
58 50 zero_gravi
// #################################################################################################
59 73 zero_gravi
// Override default STDIO functions
60
// #################################################################################################
61
 
62
/**********************************************************************//**
63
 * Send char via UART0
64
 *
65
 * @param[in] Char to be send.
66
 * @return Char that has been sent.
67
 **************************************************************************/
68
int putchar(int ch) {
69
 
70
  neorv32_uart0_putc((char)ch);
71
  return ch;
72
}
73
 
74
 
75
/**********************************************************************//**
76
 * Read char from UART0.
77
 *
78
 * @return Read char.
79
 **************************************************************************/
80
int getchar(void) {
81
 
82
  return (int)neorv32_uart0_getc();
83
}
84
 
85
 
86
// #################################################################################################
87 50 zero_gravi
// Primary UART (UART0)
88
// #################################################################################################
89
 
90
/**********************************************************************//**
91
 * Check if UART0 unit was synthesized.
92
 *
93
 * @return 0 if UART0 was not synthesized, 1 if UART0 is available.
94
 **************************************************************************/
95
int neorv32_uart0_available(void) {
96
 
97 64 zero_gravi
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_UART0)) {
98 2 zero_gravi
    return 1;
99
  }
100
  else {
101
    return 0;
102
  }
103
}
104
 
105
 
106
/**********************************************************************//**
107 50 zero_gravi
 * Enable and configure primary UART (UART0).
108 2 zero_gravi
 *
109 50 zero_gravi
 * @note The 'UART0_SIM_MODE' compiler flag will configure UART0 for simulation mode: all UART0 TX data will be redirected to simulation output. Use this for simulations only!
110
 * @note To enable simulation mode add <USER_FLAGS+=-DUART0_SIM_MODE> when compiling.
111 30 zero_gravi
 *
112 47 zero_gravi
 * @warning The baud rate is computed using INTEGER operations (truncation errors might occur).
113
 *
114 2 zero_gravi
 * @param[in] baudrate Targeted BAUD rate (e.g. 9600).
115 51 zero_gravi
 * @param[in] parity Parity configuration (00=off, 10=even, 11=odd), see #NEORV32_UART_PARITY_enum.
116
 * @param[in] flow_con Hardware flow control configuration (00=off, 01=RTS, 10=CTS, 11=RTS/CTS), see #NEORV32_UART_FLOW_CONTROL_enum.
117 2 zero_gravi
 **************************************************************************/
118 51 zero_gravi
void neorv32_uart0_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con) {
119 2 zero_gravi
 
120 64 zero_gravi
  NEORV32_UART0.CTRL = 0; // reset
121 2 zero_gravi
 
122 64 zero_gravi
  uint32_t clock = NEORV32_SYSINFO.CLK;
123 2 zero_gravi
  uint16_t i = 0; // BAUD rate divisor
124 36 zero_gravi
  uint8_t p = 0; // initial prsc = CLK/2
125
 
126
  // raw clock prescaler
127 66 zero_gravi
#ifndef make_bootloader
128 72 zero_gravi
  // use div instructions
129 36 zero_gravi
  i = (uint16_t)(clock / (2*baudrate));
130
#else
131 66 zero_gravi
  // division via repeated subtraction (minimal size, only for bootloader)
132 2 zero_gravi
  while (clock >= 2*baudrate) {
133
    clock -= 2*baudrate;
134
    i++;
135
  }
136 36 zero_gravi
#endif
137 2 zero_gravi
 
138 47 zero_gravi
  // find baud prescaler (12-bit wide))
139 2 zero_gravi
  while (i >= 0x0fff) {
140
    if ((p == 2) || (p == 4))
141
      i >>= 3;
142
    else
143
      i >>= 1;
144
    p++;
145
  }
146
 
147 47 zero_gravi
  uint32_t clk_prsc = (uint32_t)p;
148 64 zero_gravi
  clk_prsc = clk_prsc << UART_CTRL_PRSC0;
149 2 zero_gravi
 
150 47 zero_gravi
  uint32_t baud_prsc = (uint32_t)i;
151
  baud_prsc = baud_prsc - 1;
152 64 zero_gravi
  baud_prsc = baud_prsc << UART_CTRL_BAUD00;
153 2 zero_gravi
 
154
  uint32_t uart_en = 1;
155 64 zero_gravi
  uart_en = uart_en << UART_CTRL_EN;
156 2 zero_gravi
 
157 42 zero_gravi
  uint32_t parity_config = (uint32_t)(parity & 3);
158 64 zero_gravi
  parity_config = parity_config << UART_CTRL_PMODE0;
159 42 zero_gravi
 
160 51 zero_gravi
  uint32_t flow_control = (uint32_t)(flow_con & 3);
161 64 zero_gravi
  flow_control = flow_control << UART_CTRL_RTS_EN;
162 51 zero_gravi
 
163 50 zero_gravi
  /* Enable UART0 for SIM mode. */
164 42 zero_gravi
  /* USE THIS ONLY FOR SIMULATION! */
165 30 zero_gravi
#ifdef UART_SIM_MODE
166 50 zero_gravi
  #warning <UART_SIM_MODE> is obsolete (but still supported for compatibility). Please consider using the new flag <UART0_SIM_MODE>.
167
#endif
168
#if defined UART0_SIM_MODE || defined UART_SIM_MODE
169
  #warning UART0_SIM_MODE (primary UART) enabled! Sending all UART0.TX data to text.io simulation output instead of real UART0 transmitter. Use this for simulations only!
170 64 zero_gravi
  uint32_t sim_mode = 1 << UART_CTRL_SIM_MODE;
171 30 zero_gravi
#else
172
  uint32_t sim_mode = 0;
173
#endif
174
 
175 64 zero_gravi
  NEORV32_UART0.CTRL = clk_prsc | baud_prsc | uart_en | parity_config | sim_mode | flow_control;
176 2 zero_gravi
}
177
 
178
 
179
/**********************************************************************//**
180 50 zero_gravi
 * Disable UART0.
181 2 zero_gravi
 **************************************************************************/
182 50 zero_gravi
void neorv32_uart0_disable(void) {
183 2 zero_gravi
 
184 64 zero_gravi
  NEORV32_UART0.CTRL &= ~((uint32_t)(1 << UART_CTRL_EN));
185 2 zero_gravi
}
186
 
187
 
188
/**********************************************************************//**
189 61 zero_gravi
 * Enable UART0.
190
 **************************************************************************/
191
void neorv32_uart0_enable(void) {
192
 
193 69 zero_gravi
  NEORV32_UART0.CTRL |= ((uint32_t)(1 << UART_CTRL_EN));
194 61 zero_gravi
}
195
 
196
 
197
/**********************************************************************//**
198 50 zero_gravi
 * Send single char via UART0.
199 2 zero_gravi
 *
200
 * @note This function is blocking.
201
 *
202
 * @param[in] c Char to be send.
203
 **************************************************************************/
204 50 zero_gravi
void neorv32_uart0_putc(char c) {
205 2 zero_gravi
 
206
  // wait for previous transfer to finish
207 65 zero_gravi
  while ((NEORV32_UART0.CTRL & (1<<UART_CTRL_TX_FULL)) != 0); // wait for space in TX FIFO
208 64 zero_gravi
  NEORV32_UART0.DATA = ((uint32_t)c) << UART_DATA_LSB;
209 2 zero_gravi
}
210
 
211
 
212
/**********************************************************************//**
213 65 zero_gravi
 * Check if UART0 TX is busy (transmitter busy or data left in TX buffer).
214 23 zero_gravi
 *
215
 * @note This function is blocking.
216
 *
217
 * @return 0 if idle, 1 if busy
218
 **************************************************************************/
219 50 zero_gravi
int neorv32_uart0_tx_busy(void) {
220 23 zero_gravi
 
221 65 zero_gravi
  uint32_t ctrl = NEORV32_UART0.CTRL;
222
 
223
  if (((ctrl & (1<<UART_CTRL_TX_BUSY)) != 0) ||  // TX engine busy
224
      ((ctrl & (1<<UART_CTRL_TX_EMPTY)) == 0)) { // TX buffer not empty
225 23 zero_gravi
    return 1;
226
  }
227
  return 0;
228
}
229
 
230
 
231
/**********************************************************************//**
232 50 zero_gravi
 * Get char from UART0.
233 2 zero_gravi
 *
234 42 zero_gravi
 * @note This function is blocking and does not check for UART frame/parity errors.
235 2 zero_gravi
 *
236
 * @return Received char.
237
 **************************************************************************/
238 50 zero_gravi
char neorv32_uart0_getc(void) {
239 2 zero_gravi
 
240
  uint32_t d = 0;
241
  while (1) {
242 64 zero_gravi
    d = NEORV32_UART0.DATA;
243 2 zero_gravi
    if ((d & (1<<UART_DATA_AVAIL)) != 0) { // char received?
244
      return (char)d;
245
    }
246
  }
247
}
248
 
249
 
250
/**********************************************************************//**
251 50 zero_gravi
 * Get char from UART0 (and check errors).
252 2 zero_gravi
 *
253 42 zero_gravi
 * @note This function is non-blocking and checks for frame and parity errors.
254
 *
255
 * @param[in,out] data Received char.
256 65 zero_gravi
 * @return Status code:
257
 *  0 = char received without errors
258
 * -1 = nothing received
259
 * -2 = char received with frame error
260
 * -3 = char received with parity error
261
 * -4 = char received with overrun error.
262 42 zero_gravi
 **************************************************************************/
263 51 zero_gravi
int neorv32_uart0_getc_safe(char *data) {
264 42 zero_gravi
 
265 64 zero_gravi
  uint32_t uart_rx = NEORV32_UART0.DATA;
266 42 zero_gravi
 
267 65 zero_gravi
  // get received byte (if there is any)
268
  *data = (char)uart_rx;
269 42 zero_gravi
 
270 65 zero_gravi
  // check if no data available at all
271
  if ((uart_rx & (1<<UART_DATA_AVAIL)) == 0) {
272
   return -1;
273
  }
274 42 zero_gravi
 
275 65 zero_gravi
  // check for frame error
276
  if (uart_rx & (1<<UART_DATA_FERR)) {
277
    return -2;
278
  }
279 42 zero_gravi
 
280 65 zero_gravi
  // check for parity error
281
  if (uart_rx & (1<<UART_DATA_PERR)) {
282
    return -3;
283
  }
284 42 zero_gravi
 
285 65 zero_gravi
  // check for overrun error
286
  if (uart_rx & (1<<UART_DATA_OVERR)) {
287
    return -4;
288
  }
289 42 zero_gravi
 
290 65 zero_gravi
  return 0; // all fine
291 42 zero_gravi
}
292
 
293
 
294
/**********************************************************************//**
295 50 zero_gravi
 * Check if UART0 has received a char.
296 42 zero_gravi
 *
297 2 zero_gravi
 * @note This function is non-blocking.
298 50 zero_gravi
 * @note Use neorv32_uart0_char_received_get(void) to get the char.
299 2 zero_gravi
 *
300
 * @return =!0 when a char has been received.
301
 **************************************************************************/
302 50 zero_gravi
int neorv32_uart0_char_received(void) {
303 2 zero_gravi
 
304 64 zero_gravi
  if ((NEORV32_UART0.DATA & (1<<UART_DATA_AVAIL)) != 0) {
305 2 zero_gravi
    return 1;
306
  }
307
  else {
308
    return 0;
309
  }
310
}
311
 
312
 
313
/**********************************************************************//**
314 50 zero_gravi
 * Get a received char from UART0.
315 2 zero_gravi
 *
316
 * @note This function is non-blocking.
317
 * @note Should only be used in combination with neorv32_uart_char_received(void).
318
 *
319
 * @return Received char.
320
 **************************************************************************/
321 50 zero_gravi
char neorv32_uart0_char_received_get(void) {
322 2 zero_gravi
 
323 64 zero_gravi
  return (char)NEORV32_UART0.DATA;
324 2 zero_gravi
}
325
 
326
 
327
/**********************************************************************//**
328 50 zero_gravi
 * Print string (zero-terminated) via UART0. Print full line break "\r\n" for every '\n'.
329 2 zero_gravi
 *
330
 * @note This function is blocking.
331
 *
332
 * @param[in] s Pointer to string.
333
 **************************************************************************/
334 50 zero_gravi
void neorv32_uart0_print(const char *s) {
335 2 zero_gravi
 
336
  char c = 0;
337
  while ((c = *s++)) {
338
    if (c == '\n') {
339 50 zero_gravi
      neorv32_uart0_putc('\r');
340 2 zero_gravi
    }
341 50 zero_gravi
    neorv32_uart0_putc(c);
342 2 zero_gravi
  }
343
}
344
 
345
 
346
/**********************************************************************//**
347 50 zero_gravi
 * Custom version of 'printf' function using UART0.
348 2 zero_gravi
 *
349 50 zero_gravi
 * @note This function is blocking.
350
 *
351
 * @param[in] format Pointer to format string.
352
 *
353
 * <TABLE>
354
 * <TR><TD>%s</TD><TD>String (array of chars, zero-terminated)</TD></TR>
355
 * <TR><TD>%c</TD><TD>Single char</TD></TR>
356 72 zero_gravi
 * <TR><TD>%d/%i</TD><TD>32-bit signed number, printed as decimal</TD></TR>
357 50 zero_gravi
 * <TR><TD>%u</TD><TD>32-bit unsigned number, printed as decimal</TD></TR>
358 72 zero_gravi
 * <TR><TD>%x</TD><TD>32-bit number, printed as 8-char hexadecimal - lower-case</TD></TR>
359
 * <TR><TD>%X</TD><TD>32-bit number, printed as 8-char hexadecimal - upper-case</TD></TR>
360
 * <TR><TD>%p</TD><TD>32-bit pointer, printed as 8-char hexadecimal - lower-case</TD></TR>
361 50 zero_gravi
 * </TABLE>
362 2 zero_gravi
 **************************************************************************/
363 50 zero_gravi
void neorv32_uart0_printf(const char *format, ...) {
364 2 zero_gravi
 
365 50 zero_gravi
  char c, string_buf[11];
366
  int32_t n;
367 2 zero_gravi
 
368 50 zero_gravi
  va_list a;
369
  va_start(a, format);
370 2 zero_gravi
 
371 50 zero_gravi
  while ((c = *format++)) {
372
    if (c == '%') {
373
      c = *format++;
374
      switch (c) {
375
        case 's': // string
376
          neorv32_uart0_print(va_arg(a, char*));
377
          break;
378
        case 'c': // char
379
          neorv32_uart0_putc((char)va_arg(a, int));
380
          break;
381
        case 'i': // 32-bit signed
382 72 zero_gravi
        case 'd':
383 50 zero_gravi
          n = (int32_t)va_arg(a, int32_t);
384
          if (n < 0) {
385
            n = -n;
386
            neorv32_uart0_putc('-');
387
          }
388
          __neorv32_uart_itoa((uint32_t)n, string_buf);
389
          neorv32_uart0_print(string_buf);
390
          break;
391
        case 'u': // 32-bit unsigned
392
          __neorv32_uart_itoa(va_arg(a, uint32_t), string_buf);
393
          neorv32_uart0_print(string_buf);
394
          break;
395
        case 'x': // 32-bit hexadecimal
396 72 zero_gravi
        case 'p':
397
        case 'X':
398 50 zero_gravi
          __neorv32_uart_tohex(va_arg(a, uint32_t), string_buf);
399 72 zero_gravi
          if (c == 'X') {
400
            __neorv32_uart_touppercase(11, string_buf);
401
          }
402 50 zero_gravi
          neorv32_uart0_print(string_buf);
403
          break;
404 55 zero_gravi
        default: // unsupported format
405
          neorv32_uart0_putc('%');
406
          neorv32_uart0_putc(c);
407
          break;
408 50 zero_gravi
      }
409
    }
410
    else {
411
      if (c == '\n') {
412
        neorv32_uart0_putc('\r');
413
      }
414
      neorv32_uart0_putc(c);
415
    }
416 2 zero_gravi
  }
417 50 zero_gravi
  va_end(a);
418
}
419 2 zero_gravi
 
420 50 zero_gravi
 
421
/**********************************************************************//**
422
 * Simplified custom version of 'scanf' function for UART0.
423
 *
424
 * @note This function is blocking.
425
 *
426
 * @param[in,out] buffer Pointer to array of chars to store string.
427
 * @param[in] max_size Maximum number of chars to sample.
428
 * @param[in] echo Echo UART input when 1.
429
 * @return Number of chars read.
430
 **************************************************************************/
431
int neorv32_uart0_scan(char *buffer, int max_size, int echo) {
432
 
433
  char c = 0;
434
  int length = 0;
435
 
436
  while (1) {
437
    c = neorv32_uart0_getc();
438
    if (c == '\b') { // BACKSPACE
439
      if (length != 0) {
440
        if (echo) {
441
          neorv32_uart0_print("\b \b"); // delete last char in console
442
        }
443
        buffer--;
444
        length--;
445
      }
446
    }
447
    else if (c == '\r') // carriage return
448 2 zero_gravi
      break;
449 50 zero_gravi
    else if ((c >= ' ') && (c <= '~') && (length < (max_size-1))) {
450
      if (echo) {
451
        neorv32_uart0_putc(c); // echo
452
      }
453
      *buffer++ = c;
454
      length++;
455
    }
456 2 zero_gravi
  }
457 50 zero_gravi
  *buffer = '\0'; // terminate string
458 2 zero_gravi
 
459 50 zero_gravi
  return length;
460
}
461 2 zero_gravi
 
462 50 zero_gravi
 
463
 
464
// #################################################################################################
465
// Secondary UART (UART1)
466
// #################################################################################################
467
 
468
/**********************************************************************//**
469
 * Check if UART1 unit was synthesized.
470
 *
471
 * @return 0 if UART1 was not synthesized, 1 if UART1 is available.
472
 **************************************************************************/
473
int neorv32_uart1_available(void) {
474
 
475 64 zero_gravi
  if (NEORV32_SYSINFO.SOC & (1 << SYSINFO_SOC_IO_UART1)) {
476 50 zero_gravi
    return 1;
477
  }
478
  else {
479
    return 0;
480
  }
481 2 zero_gravi
}
482
 
483
 
484
/**********************************************************************//**
485 50 zero_gravi
 * Enable and configure secondary UART (UART1).
486 2 zero_gravi
 *
487 50 zero_gravi
 * @note The 'UART1_SIM_MODE' compiler flag will configure UART1 for simulation mode: all UART1 TX data will be redirected to simulation output. Use this for simulations only!
488
 * @note To enable simulation mode add <USER_FLAGS+=-DUART1_SIM_MODE> when compiling.
489
 *
490
 * @warning The baud rate is computed using INTEGER operations (truncation errors might occur).
491
 *
492
 * @param[in] baudrate Targeted BAUD rate (e.g. 9600).
493 51 zero_gravi
 * @param[in] parity Parity configuration (00=off, 10=even, 11=odd), see #NEORV32_UART_PARITY_enum.
494
 * @param[in] flow_con Hardware flow control configuration (00=off, 01=RTS, 10=CTS, 11=RTS/CTS), see #NEORV32_UART_FLOW_CONTROL_enum.
495 2 zero_gravi
 **************************************************************************/
496 51 zero_gravi
void neorv32_uart1_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con) {
497 2 zero_gravi
 
498 64 zero_gravi
  NEORV32_UART1.CTRL = 0; // reset
499 2 zero_gravi
 
500 64 zero_gravi
  uint32_t clock = NEORV32_SYSINFO.CLK;
501 50 zero_gravi
  uint16_t i = 0; // BAUD rate divisor
502
  uint8_t p = 0; // initial prsc = CLK/2
503
 
504
  // raw clock prescaler
505 72 zero_gravi
#ifndef make_bootloader
506 50 zero_gravi
  // use div instructions
507
  i = (uint16_t)(clock / (2*baudrate));
508
#else
509 66 zero_gravi
  // division via repeated subtraction (minimal size, only for bootloader)
510 50 zero_gravi
  while (clock >= 2*baudrate) {
511
    clock -= 2*baudrate;
512
    i++;
513 2 zero_gravi
  }
514 50 zero_gravi
#endif
515 2 zero_gravi
 
516 50 zero_gravi
  // find baud prescaler (12-bit wide))
517
  while (i >= 0x0fff) {
518
    if ((p == 2) || (p == 4))
519
      i >>= 3;
520
    else
521
      i >>= 1;
522
    p++;
523
  }
524
 
525
  uint32_t clk_prsc = (uint32_t)p;
526 64 zero_gravi
  clk_prsc = clk_prsc << UART_CTRL_PRSC0;
527 50 zero_gravi
 
528
  uint32_t baud_prsc = (uint32_t)i;
529
  baud_prsc = baud_prsc - 1;
530 64 zero_gravi
  baud_prsc = baud_prsc << UART_CTRL_BAUD00;
531 50 zero_gravi
 
532
  uint32_t uart_en = 1;
533 64 zero_gravi
  uart_en = uart_en << UART_CTRL_EN;
534 50 zero_gravi
 
535
  uint32_t parity_config = (uint32_t)(parity & 3);
536 64 zero_gravi
  parity_config = parity_config << UART_CTRL_PMODE0;
537 50 zero_gravi
 
538 51 zero_gravi
  uint32_t flow_control = (uint32_t)(flow_con & 3);
539 64 zero_gravi
  flow_control = flow_control << UART_CTRL_RTS_EN;
540 51 zero_gravi
 
541 50 zero_gravi
  /* Enable UART1 for SIM mode. */
542
  /* USE THIS ONLY FOR SIMULATION! */
543
#ifdef UART1_SIM_MODE
544
  #warning UART1_SIM_MODE (secondary UART) enabled! Sending all UART1.TX data to text.io simulation output instead of real UART1 transmitter. Use this for simulations only!
545 64 zero_gravi
  uint32_t sim_mode = 1 << UART_CTRL_SIM_MODE;
546 50 zero_gravi
#else
547
  uint32_t sim_mode = 0;
548
#endif
549
 
550 64 zero_gravi
  NEORV32_UART1.CTRL = clk_prsc | baud_prsc | uart_en | parity_config | sim_mode | flow_control;
551 2 zero_gravi
}
552
 
553
 
554
/**********************************************************************//**
555 50 zero_gravi
 * Disable UART1.
556
 **************************************************************************/
557
void neorv32_uart1_disable(void) {
558
 
559 64 zero_gravi
  NEORV32_UART1.CTRL &= ~((uint32_t)(1 << UART_CTRL_EN));
560 50 zero_gravi
}
561
 
562
 
563
/**********************************************************************//**
564 61 zero_gravi
 * Enable UART1.
565
 **************************************************************************/
566
void neorv32_uart1_enable(void) {
567
 
568 64 zero_gravi
  NEORV32_UART1.CTRL |= ((uint32_t)(1 << UART_CTRL_EN));
569 61 zero_gravi
}
570
 
571
 
572
/**********************************************************************//**
573 50 zero_gravi
 * Send single char via UART1.
574 2 zero_gravi
 *
575
 * @note This function is blocking.
576
 *
577 50 zero_gravi
 * @param[in] c Char to be send.
578
 **************************************************************************/
579
void neorv32_uart1_putc(char c) {
580
 
581
  // wait for previous transfer to finish
582 65 zero_gravi
  while ((NEORV32_UART1.CTRL & (1<<UART_CTRL_TX_FULL)) != 0); // wait for space in TX FIFO
583 64 zero_gravi
  NEORV32_UART1.DATA = ((uint32_t)c) << UART_DATA_LSB;
584 50 zero_gravi
}
585
 
586
 
587
/**********************************************************************//**
588 65 zero_gravi
 * Check if UART1 TX is busy (transmitter busy or data left in TX buffer).
589 50 zero_gravi
 *
590
 * @note This function is blocking.
591
 *
592
 * @return 0 if idle, 1 if busy
593
 **************************************************************************/
594
int neorv32_uart1_tx_busy(void) {
595
 
596 65 zero_gravi
  uint32_t ctrl = NEORV32_UART1.CTRL;
597
 
598
  if (((ctrl & (1<<UART_CTRL_TX_BUSY)) != 0) ||  // TX engine busy
599
      ((ctrl & (1<<UART_CTRL_TX_EMPTY)) == 0)) { // TX buffer not empty
600 50 zero_gravi
    return 1;
601
  }
602
  return 0;
603
}
604
 
605
 
606
/**********************************************************************//**
607
 * Get char from UART1.
608
 *
609
 * @note This function is blocking and does not check for UART frame/parity errors.
610
 *
611
 * @return Received char.
612
 **************************************************************************/
613
char neorv32_uart1_getc(void) {
614
 
615
  uint32_t d = 0;
616
  while (1) {
617 64 zero_gravi
    d = NEORV32_UART1.DATA;
618 50 zero_gravi
    if ((d & (1<<UART_DATA_AVAIL)) != 0) { // char received?
619
      return (char)d;
620
    }
621
  }
622
}
623
 
624
 
625
/**********************************************************************//**
626
 * Get char from UART1 (and check errors).
627
 *
628
 * @note This function is non-blocking and checks for frame and parity errors.
629
 *
630
 * @param[in,out] data Received char.
631 65 zero_gravi
 * @return Status code:
632
 *  0 = char received without errors
633
 * -1 = nothing received
634
 * -2 = char received with frame error
635
 * -3 = char received with parity error
636
 * -4 = char received with overrun error.
637 50 zero_gravi
 **************************************************************************/
638 51 zero_gravi
int neorv32_uart1_getc_safe(char *data) {
639 50 zero_gravi
 
640 64 zero_gravi
  uint32_t uart_rx = NEORV32_UART1.DATA;
641 50 zero_gravi
 
642 65 zero_gravi
  // get received byte (if there is any)
643
  *data = (char)uart_rx;
644 50 zero_gravi
 
645 65 zero_gravi
  // check if no data available at all
646
  if ((uart_rx & (1<<UART_DATA_AVAIL)) == 0) {
647
   return -1;
648
  }
649 50 zero_gravi
 
650 65 zero_gravi
  // check for frame error
651
  if (uart_rx & (1<<UART_DATA_FERR)) {
652
    return -2;
653
  }
654 50 zero_gravi
 
655 65 zero_gravi
  // check for parity error
656
  if (uart_rx & (1<<UART_DATA_PERR)) {
657
    return -3;
658
  }
659 50 zero_gravi
 
660 65 zero_gravi
  // check for overrun error
661
  if (uart_rx & (1<<UART_DATA_OVERR)) {
662
    return -4;
663
  }
664 50 zero_gravi
 
665 65 zero_gravi
  return 0; // all fine
666 50 zero_gravi
}
667
 
668
 
669
/**********************************************************************//**
670
 * Check if UART1 has received a char.
671
 *
672
 * @note This function is non-blocking.
673
 * @note Use neorv32_uart0_char_received_get(void) to get the char.
674
 *
675
 * @return =!0 when a char has been received.
676
 **************************************************************************/
677
int neorv32_uart1_char_received(void) {
678
 
679 64 zero_gravi
  if ((NEORV32_UART1.DATA & (1<<UART_DATA_AVAIL)) != 0) {
680 50 zero_gravi
    return 1;
681
  }
682
  else {
683
    return 0;
684
  }
685
}
686
 
687
 
688
/**********************************************************************//**
689
 * Get a received char from UART1.
690
 *
691
 * @note This function is non-blocking.
692
 * @note Should only be used in combination with neorv32_uart_char_received(void).
693
 *
694
 * @return Received char.
695
 **************************************************************************/
696
char neorv32_uart1_char_received_get(void) {
697
 
698 64 zero_gravi
  return (char)NEORV32_UART1.DATA;
699 50 zero_gravi
}
700
 
701
 
702
/**********************************************************************//**
703
 * Print string (zero-terminated) via UART1. Print full line break "\r\n" for every '\n'.
704
 *
705
 * @note This function is blocking.
706
 *
707
 * @param[in] s Pointer to string.
708
 **************************************************************************/
709
void neorv32_uart1_print(const char *s) {
710
 
711
  char c = 0;
712
  while ((c = *s++)) {
713
    if (c == '\n') {
714
      neorv32_uart1_putc('\r');
715
    }
716
    neorv32_uart1_putc(c);
717
  }
718
}
719
 
720
 
721
/**********************************************************************//**
722
 * Custom version of 'printf' function using UART1.
723
 *
724
 * @note This function is blocking.
725
 *
726 2 zero_gravi
 * @param[in] format Pointer to format string.
727
 *
728
 * <TABLE>
729
 * <TR><TD>%s</TD><TD>String (array of chars, zero-terminated)</TD></TR>
730
 * <TR><TD>%c</TD><TD>Single char</TD></TR>
731 72 zero_gravi
 * <TR><TD>%d/%i</TD><TD>32-bit signed number, printed as decimal</TD></TR>
732 2 zero_gravi
 * <TR><TD>%u</TD><TD>32-bit unsigned number, printed as decimal</TD></TR>
733 72 zero_gravi
 * <TR><TD>%x</TD><TD>32-bit number, printed as 8-char hexadecimal - lower-case</TD></TR>
734
 * <TR><TD>%X</TD><TD>32-bit number, printed as 8-char hexadecimal - upper-case</TD></TR>
735
 * <TR><TD>%p</TD><TD>32-bit pointer, printed as 8-char hexadecimal - lower-case</TD></TR>
736 2 zero_gravi
 * </TABLE>
737
 **************************************************************************/
738 50 zero_gravi
void neorv32_uart1_printf(const char *format, ...) {
739 2 zero_gravi
 
740
  char c, string_buf[11];
741
  int32_t n;
742
 
743
  va_list a;
744
  va_start(a, format);
745
 
746
  while ((c = *format++)) {
747
    if (c == '%') {
748
      c = *format++;
749
      switch (c) {
750
        case 's': // string
751 50 zero_gravi
          neorv32_uart1_print(va_arg(a, char*));
752 2 zero_gravi
          break;
753
        case 'c': // char
754 50 zero_gravi
          neorv32_uart1_putc((char)va_arg(a, int));
755 2 zero_gravi
          break;
756
        case 'i': // 32-bit signed
757 72 zero_gravi
        case 'd':
758 2 zero_gravi
          n = (int32_t)va_arg(a, int32_t);
759
          if (n < 0) {
760
            n = -n;
761 50 zero_gravi
            neorv32_uart1_putc('-');
762 2 zero_gravi
          }
763
          __neorv32_uart_itoa((uint32_t)n, string_buf);
764 50 zero_gravi
          neorv32_uart1_print(string_buf);
765 2 zero_gravi
          break;
766
        case 'u': // 32-bit unsigned
767
          __neorv32_uart_itoa(va_arg(a, uint32_t), string_buf);
768 50 zero_gravi
          neorv32_uart1_print(string_buf);
769 2 zero_gravi
          break;
770
        case 'x': // 32-bit hexadecimal
771 72 zero_gravi
        case 'p':
772
        case 'X':
773 2 zero_gravi
          __neorv32_uart_tohex(va_arg(a, uint32_t), string_buf);
774 72 zero_gravi
          if (c == 'X') {
775
            __neorv32_uart_touppercase(11, string_buf);
776
          }
777 50 zero_gravi
          neorv32_uart1_print(string_buf);
778 2 zero_gravi
          break;
779 55 zero_gravi
        default: // unsupported format
780
          neorv32_uart1_putc('%');
781
          neorv32_uart1_putc(c);
782
          break;
783 2 zero_gravi
      }
784
    }
785
    else {
786
      if (c == '\n') {
787 50 zero_gravi
        neorv32_uart1_putc('\r');
788 2 zero_gravi
      }
789 50 zero_gravi
      neorv32_uart1_putc(c);
790 2 zero_gravi
    }
791
  }
792
  va_end(a);
793
}
794
 
795
 
796
/**********************************************************************//**
797 50 zero_gravi
 * Simplified custom version of 'scanf' function for UART1.
798 2 zero_gravi
 *
799
 * @note This function is blocking.
800
 *
801
 * @param[in,out] buffer Pointer to array of chars to store string.
802
 * @param[in] max_size Maximum number of chars to sample.
803
 * @param[in] echo Echo UART input when 1.
804
 * @return Number of chars read.
805
 **************************************************************************/
806 50 zero_gravi
int neorv32_uart1_scan(char *buffer, int max_size, int echo) {
807 2 zero_gravi
 
808
  char c = 0;
809
  int length = 0;
810
 
811
  while (1) {
812 50 zero_gravi
    c = neorv32_uart1_getc();
813 2 zero_gravi
    if (c == '\b') { // BACKSPACE
814
      if (length != 0) {
815
        if (echo) {
816 50 zero_gravi
          neorv32_uart1_print("\b \b"); // delete last char in console
817 2 zero_gravi
        }
818
        buffer--;
819
        length--;
820
      }
821
    }
822
    else if (c == '\r') // carriage return
823
      break;
824
    else if ((c >= ' ') && (c <= '~') && (length < (max_size-1))) {
825
      if (echo) {
826 50 zero_gravi
        neorv32_uart1_putc(c); // echo
827 2 zero_gravi
      }
828
      *buffer++ = c;
829
      length++;
830
    }
831
  }
832
  *buffer = '\0'; // terminate string
833
 
834
  return length;
835
}
836
 
837 50 zero_gravi
 
838
 
839
// #################################################################################################
840
// Shared functions
841
// #################################################################################################
842
 
843
/**********************************************************************//**
844
 * Private function for 'neorv32_printf' to convert into decimal.
845
 *
846
 * @param[in] x Unsigned input number.
847
 * @param[in,out] res Pointer for storing the reuslting number string (11 chars).
848
 **************************************************************************/
849
static void __neorv32_uart_itoa(uint32_t x, char *res) {
850
 
851
  static const char numbers[] = "0123456789";
852
  char buffer1[11];
853
  uint16_t i, j;
854
 
855
  buffer1[10] = '\0';
856
  res[10] = '\0';
857
 
858
  // convert
859
  for (i=0; i<10; i++) {
860
    buffer1[i] = numbers[x%10];
861
    x /= 10;
862
  }
863
 
864
  // delete 'leading' zeros
865
  for (i=9; i!=0; i--) {
866
    if (buffer1[i] == '0')
867
      buffer1[i] = '\0';
868
    else
869
      break;
870
  }
871
 
872
  // reverse
873
  j = 0;
874
  do {
875
    if (buffer1[i] != '\0')
876
      res[j++] = buffer1[i];
877
  } while (i--);
878
 
879
  res[j] = '\0'; // terminate result string
880
}
881
 
882
 
883
/**********************************************************************//**
884
 * Private function for 'neorv32_printf' to convert into hexadecimal.
885
 *
886
 * @param[in] x Unsigned input number.
887 55 zero_gravi
 * @param[in,out] res Pointer for storing the resulting number string (9 chars).
888 50 zero_gravi
 **************************************************************************/
889
static void __neorv32_uart_tohex(uint32_t x, char *res) {
890
 
891
  static const char symbols[] = "0123456789abcdef";
892
 
893
  int i;
894 55 zero_gravi
  for (i=0; i<8; i++) { // nibble by nibble
895 50 zero_gravi
    uint32_t num_tmp = x >> (4*i);
896
    res[7-i] = (char)symbols[num_tmp & 0x0f];
897
  }
898
 
899
  res[8] = '\0'; // terminate result string
900
}
901 72 zero_gravi
 
902
 
903
/**********************************************************************//**
904
 * Private function to cast a string to UPPERCASE.
905
 *
906
 * @param[in] len Total length of input string.
907
 * @param[in,out] ptr Pointer for input/output string.
908
 **************************************************************************/
909
static void __neorv32_uart_touppercase(uint32_t len, char *ptr) {
910
 
911
  char tmp;
912
 
913
  while (len > 0) {
914
    tmp = *ptr;
915
    if ((tmp >= 'a') && (tmp <= 'z')) {
916
      *ptr = tmp - 32;
917
    }
918
    ptr++;
919
    len--;
920
  }
921
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.