Common neorv32 uart functions (#509)

This commit is contained in:
Stephan 2023-03-04 15:05:14 +01:00 committed by GitHub
commit efb94ac25c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 181 additions and 502 deletions

View file

@ -133,7 +133,7 @@ int main() {
// setup UARTs at default baud rate, no parity bits, no HW flow control
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
NEORV32_UART1.CTRL = NEORV32_UART0.CTRL; // copy configuration to initialize UART1
NEORV32_UART1->CTRL = NEORV32_UART0->CTRL; // copy configuration to initialize UART1
#ifdef SUPPRESS_OPTIONAL_UART_PRINT
neorv32_uart0_disable(); // do not generate any UART0 output
@ -975,12 +975,12 @@ int main() {
while(neorv32_uart0_tx_busy());
// backup current UART0 configuration
tmp_a = NEORV32_UART0.CTRL;
tmp_a = NEORV32_UART0->CTRL;
// make sure UART is enabled
NEORV32_UART0.CTRL |= (1 << UART_CTRL_EN);
NEORV32_UART0->CTRL |= (1 << UART_CTRL_EN);
// make sure sim mode is disabled
NEORV32_UART0.CTRL &= ~(1 << UART_CTRL_SIM_MODE);
NEORV32_UART0->CTRL &= ~(1 << UART_CTRL_SIM_MODE);
// enable fast interrupt
neorv32_cpu_csr_write(CSR_MIE, 1 << UART0_RX_FIRQ_ENABLE);
@ -997,7 +997,7 @@ int main() {
neorv32_cpu_csr_write(CSR_MIE, 0);
// restore original configuration
NEORV32_UART0.CTRL = tmp_a;
NEORV32_UART0->CTRL = tmp_a;
if (neorv32_cpu_csr_read(CSR_MCAUSE) == UART0_RX_TRAP_CODE) {
test_ok();
@ -1018,12 +1018,12 @@ int main() {
while(neorv32_uart0_tx_busy());
// backup current UART0 configuration
tmp_a = NEORV32_UART0.CTRL;
tmp_a = NEORV32_UART0->CTRL;
// make sure UART is enabled
NEORV32_UART0.CTRL |= (1 << UART_CTRL_EN);
NEORV32_UART0->CTRL |= (1 << UART_CTRL_EN);
// make sure sim mode is disabled
NEORV32_UART0.CTRL &= ~(1 << UART_CTRL_SIM_MODE);
NEORV32_UART0->CTRL &= ~(1 << UART_CTRL_SIM_MODE);
// UART0 TX interrupt enable
neorv32_cpu_csr_write(CSR_MIE, 1 << UART0_TX_FIRQ_ENABLE);
@ -1040,7 +1040,7 @@ int main() {
neorv32_cpu_csr_write(CSR_MIE, 0);
// restore original configuration
NEORV32_UART0.CTRL = tmp_a;
NEORV32_UART0->CTRL = tmp_a;
if (neorv32_cpu_csr_read(CSR_MCAUSE) == UART0_TX_TRAP_CODE) {
test_ok();
@ -1058,12 +1058,12 @@ int main() {
cnt_test++;
// backup current UART1 configuration
tmp_a = NEORV32_UART1.CTRL;
tmp_a = NEORV32_UART1->CTRL;
// make sure UART is enabled
NEORV32_UART1.CTRL |= (1 << UART_CTRL_EN);
NEORV32_UART1->CTRL |= (1 << UART_CTRL_EN);
// make sure sim mode is disabled
NEORV32_UART1.CTRL &= ~(1 << UART_CTRL_SIM_MODE);
NEORV32_UART1->CTRL &= ~(1 << UART_CTRL_SIM_MODE);
// UART1 RX interrupt enable
neorv32_cpu_csr_write(CSR_MIE, 1 << UART1_RX_FIRQ_ENABLE);
@ -1080,7 +1080,7 @@ int main() {
neorv32_cpu_csr_write(CSR_MIE, 0);
// restore original configuration
NEORV32_UART1.CTRL = tmp_a;
NEORV32_UART1->CTRL = tmp_a;
if (neorv32_cpu_csr_read(CSR_MCAUSE) == UART1_RX_TRAP_CODE) {
test_ok();
@ -1098,12 +1098,12 @@ int main() {
cnt_test++;
// backup current UART1 configuration
tmp_a = NEORV32_UART1.CTRL;
tmp_a = NEORV32_UART1->CTRL;
// make sure UART is enabled
NEORV32_UART1.CTRL |= (1 << UART_CTRL_EN);
NEORV32_UART1->CTRL |= (1 << UART_CTRL_EN);
// make sure sim mode is disabled
NEORV32_UART1.CTRL &= ~(1 << UART_CTRL_SIM_MODE);
NEORV32_UART1->CTRL &= ~(1 << UART_CTRL_SIM_MODE);
// UART1 RX interrupt enable
neorv32_cpu_csr_write(CSR_MIE, 1 << UART1_TX_FIRQ_ENABLE);
@ -1120,7 +1120,7 @@ int main() {
neorv32_cpu_csr_write(CSR_MIE, 0);
// restore original configuration
NEORV32_UART1.CTRL = tmp_a;
NEORV32_UART1->CTRL = tmp_a;
if (neorv32_cpu_csr_read(CSR_MCAUSE) == UART1_TX_TRAP_CODE) {
test_ok();
@ -1732,9 +1732,9 @@ void test_fail(void) {
void __neorv32_crt0_after_main(int32_t return_code) {
// make sure sim mode is disabled and UARTs are actually enabled
NEORV32_UART0.CTRL |= (1 << UART_CTRL_EN);
NEORV32_UART0.CTRL &= ~(1 << UART_CTRL_SIM_MODE);
NEORV32_UART1.CTRL = NEORV32_UART0.CTRL;
NEORV32_UART0->CTRL |= (1 << UART_CTRL_EN);
NEORV32_UART0->CTRL &= ~(1 << UART_CTRL_SIM_MODE);
NEORV32_UART1->CTRL = NEORV32_UART0->CTRL;
// minimal result report
PRINT_CRITICAL("%u/%u\n", (uint32_t)return_code, (uint32_t)cnt_test);

View file

@ -47,6 +47,44 @@
// UART0 & UART1
// ================================================================================================
/**********************************************************************//**
* @name UART0: Backward compatibility Wrapper, #neorv32_uart_h
**************************************************************************/
/**@{*/
#define neorv32_uart0_available() neorv32_uart_available(NEORV32_UART0)
#define neorv32_uart0_setup(baudrate, parity, flow_con) neorv32_uart_setup(NEORV32_UART0, baudrate, parity, flow_con)
#define neorv32_uart0_disable() neorv32_uart_disable(NEORV32_UART0)
#define neorv32_uart0_enable() neorv32_uart_enable(NEORV32_UART0)
#define neorv32_uart0_putc(c) neorv32_uart_putc(NEORV32_UART0, c)
#define neorv32_uart0_tx_busy() neorv32_uart_tx_busy(NEORV32_UART0)
#define neorv32_uart0_getc() neorv32_uart_getc(NEORV32_UART0)
#define neorv32_uart0_char_received() neorv32_uart_char_received(NEORV32_UART0)
#define neorv32_uart0_getc_safe(data) neorv32_uart_getc_safe(NEORV32_UART0, *data)
#define neorv32_uart0_char_received_get() neorv32_uart_char_received_get(NEORV32_UART0)
#define neorv32_uart0_puts(s) neorv32_uart_puts(NEORV32_UART0, s)
#define neorv32_uart0_printf(...) neorv32_uart_printf(NEORV32_UART0, __VA_ARGS__)
#define neorv32_uart0_scan(buffer, max_size, echo) neorv32_uart_scan(NEORV32_UART0, buffer, max_size, echo)
/**@}*/
/**********************************************************************//**
* @name UART1: Backward compatibility Wrapper, #neorv32_uart_h
**************************************************************************/
/**@{*/
#define neorv32_uart1_available() neorv32_uart_available(NEORV32_UART1)
#define neorv32_uart1_setup(baudrate, parity, flow_con) neorv32_uart_setup(NEORV32_UART1, baudrate, parity, flow_con)
#define neorv32_uart1_disable() neorv32_uart_disable(NEORV32_UART1)
#define neorv32_uart1_enable() neorv32_uart_enable(NEORV32_UART1)
#define neorv32_uart1_putc(c) neorv32_uart_putc(NEORV32_UART1, c)
#define neorv32_uart1_tx_busy() neorv32_uart_tx_busy(NEORV32_UART1)
#define neorv32_uart1_getc() neorv32_uart_getc(NEORV32_UART1)
#define neorv32_uart1_char_received() neorv32_uart_char_received(NEORV32_UART1)
#define neorv32_uart1_getc_safe(data) neorv32_uart_getc_safe(NEORV32_UART1, *data)
#define neorv32_uart1_char_received_get() neorv32_uart_char_received_get(NEORV32_UART1)
#define neorv32_uart1_puts(s) neorv32_uart_puts(NEORV32_UART1, s)
#define neorv32_uart1_printf(...) neorv32_uart_printf(NEORV32_UART1, __VA_ARGS__)
#define neorv32_uart1_scan(buffer, max_size, echo) neorv32_uart_scan(NEORV32_UART1, buffer, max_size, echo)
/**@}*/
/**********************************************************************//**
* Print string (zero-terminated) via UART0. Print full line break "\r\n" for every '\n'.
* @note This function is blocking.

View file

@ -968,7 +968,7 @@ typedef volatile struct __attribute__((packed,aligned(4))) {
**************************************************************************/
/**@{*/
/** UART module prototype */
typedef struct __attribute__((packed,aligned(4))) {
typedef volatile struct __attribute__((packed,aligned(4))) {
uint32_t CTRL; /**< offset 0: control register (#NEORV32_UART_CTRL_enum) */
uint32_t DATA; /**< offset 4: data register (#NEORV32_UART_DATA_enum) */
} neorv32_uart_t;
@ -977,13 +977,13 @@ typedef struct __attribute__((packed,aligned(4))) {
#define NEORV32_UART0_BASE (0xFFFFFFA0U)
/** UART0 module hardware access (#neorv32_uart_t) */
#define NEORV32_UART0 (*((volatile neorv32_uart_t*) (NEORV32_UART0_BASE)))
#define NEORV32_UART0 ((neorv32_uart_t*) (NEORV32_UART0_BASE))
/** UART1 module base address */
#define NEORV32_UART1_BASE (0xFFFFFFD0U)
/** UART1 module hardware access (#neorv32_uart_t) */
#define NEORV32_UART1 (*((volatile neorv32_uart_t*) (NEORV32_UART1_BASE)))
#define NEORV32_UART1 ((neorv32_uart_t*) (NEORV32_UART1_BASE))
/** UART0/UART1 control register bits */
enum NEORV32_UART_CTRL_enum {

View file

@ -3,7 +3,7 @@
// # ********************************************************************************************* #
// # BSD 3-Clause License #
// # #
// # Copyright (c) 2021, Stephan Nolting. All rights reserved. #
// # Copyright (c) 2023, Stephan Nolting. All rights reserved. #
// # #
// # Redistribution and use in source and binary forms, with or without modification, are #
// # permitted provided that the following conditions are met: #
@ -48,34 +48,19 @@
// Libs required by functions
#include <stdarg.h>
// prototypes for UART0 (primary UART)
int neorv32_uart0_available(void);
void neorv32_uart0_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con);
void neorv32_uart0_disable(void);
void neorv32_uart0_enable(void);
void neorv32_uart0_putc(char c);
int neorv32_uart0_tx_busy(void);
char neorv32_uart0_getc(void);
int neorv32_uart0_char_received(void);
int neorv32_uart0_getc_safe(char *data);
char neorv32_uart0_char_received_get(void);
void neorv32_uart0_puts(const char *s);
void neorv32_uart0_printf(const char *format, ...);
int neorv32_uart0_scan(char *buffer, int max_size, int echo);
// prototypes for UART1 (secondary UART)
int neorv32_uart1_available(void);
void neorv32_uart1_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con);
void neorv32_uart1_disable(void);
void neorv32_uart1_enable(void);
void neorv32_uart1_putc(char c);
int neorv32_uart1_tx_busy(void);
char neorv32_uart1_getc(void);
int neorv32_uart1_char_received(void);
int neorv32_uart1_getc_safe(char *data);
char neorv32_uart1_char_received_get(void);
void neorv32_uart1_puts(const char *s);
void neorv32_uart1_printf(const char *format, ...);
int neorv32_uart1_scan(char *buffer, int max_size, int echo);
// prototypes for common used UART functions, applicable to UART0 and UART1
int neorv32_uart_available(neorv32_uart_t *UARTx);
void neorv32_uart_setup(neorv32_uart_t *UARTx, uint32_t baudrate, uint8_t parity, uint8_t flow_con);
void neorv32_uart_enable(neorv32_uart_t *UARTx);
void neorv32_uart_disable(neorv32_uart_t *UARTx);
void neorv32_uart_putc(neorv32_uart_t *UARTx, char c);
int neorv32_uart_tx_busy(neorv32_uart_t *UARTx);
char neorv32_uart_getc(neorv32_uart_t *UARTx);
int neorv32_uart_getc_safe(neorv32_uart_t *UARTx, char *data);
int neorv32_uart_char_received(neorv32_uart_t *UARTx);
char neorv32_uart_char_received_get(neorv32_uart_t *UARTx);
void neorv32_uart_puts(neorv32_uart_t *UARTx, const char *s);
void neorv32_uart_printf(neorv32_uart_t *UARTx, const char *format, ...);
int neorv32_uart_scan(neorv32_uart_t *UARTx, char *buffer, int max_size, int echo);
#endif // neorv32_uart_h

View file

@ -3,7 +3,7 @@
// # ********************************************************************************************* #
// # BSD 3-Clause License #
// # #
// # Copyright (c) 2022, Stephan Nolting. All rights reserved. #
// # Copyright (c) 2023, Stephan Nolting. All rights reserved. #
// # #
// # Redistribution and use in source and binary forms, with or without modification, are #
// # permitted provided that the following conditions are met: #
@ -84,422 +84,74 @@ int getchar(void) {
// #################################################################################################
// Primary UART (UART0)
// Common used UART, assigned to UART0/1 in legacy.h
// #################################################################################################
/**********************************************************************//**
* Check if UART0 unit was synthesized.
* Check if UART0/1 unit was synthesized.
*
* @return 0 if UART0 was not synthesized, 1 if UART0 is available.
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @return 0 if UART0/1 was not synthesized, 1 if UART0/1 is available.
**************************************************************************/
int neorv32_uart0_available(void) {
int neorv32_uart_available (neorv32_uart_t *UARTx) {
if (NEORV32_SYSINFO->SOC & (1 << SYSINFO_SOC_IO_UART0)) {
return 1;
int available = 0;
if ( ((int)UARTx == NEORV32_UART0_BASE) && (NEORV32_SYSINFO->SOC & (1 << SYSINFO_SOC_IO_UART0)) ) {
available = 1;
}
else {
return 0;
if ( ((int)UARTx == NEORV32_UART1_BASE) && (NEORV32_SYSINFO->SOC & (1 << SYSINFO_SOC_IO_UART1)) ) {
available = 1;
}
return(available);
}
/**********************************************************************//**
* Enable and configure primary UART (UART0).
*
* @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!
* @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!
* @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!
* @note To enable simulation mode add <USER_FLAGS+=-DUART0_SIM_MODE> when compiling.
*
* @warning The baud rate is computed using INTEGER operations (truncation errors might occur).
*
* @param[in] baudrate Targeted BAUD rate (e.g. 9600).
* @param[in] parity Parity configuration (00=off, 10=even, 11=odd), see #NEORV32_UART_PARITY_enum.
* @param[in] flow_con Hardware flow control configuration (00=off, 01=RTS, 10=CTS, 11=RTS/CTS), see #NEORV32_UART_FLOW_CONTROL_enum.
**************************************************************************/
void neorv32_uart0_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con) {
NEORV32_UART0.CTRL = 0; // reset
uint32_t clock = NEORV32_SYSINFO->CLK;
uint16_t i = 0; // BAUD rate divisor
uint8_t p = 0; // initial prsc = CLK/2
// raw clock prescaler
#ifndef make_bootloader
// use div instructions
i = (uint16_t)(clock / (2*baudrate));
#else
// division via repeated subtraction (minimal size, only for bootloader)
while (clock >= 2*baudrate) {
clock -= 2*baudrate;
i++;
}
#endif
// find baud prescaler (12-bit wide))
while (i >= 0x0fff) {
if ((p == 2) || (p == 4))
i >>= 3;
else
i >>= 1;
p++;
}
uint32_t clk_prsc = (uint32_t)p;
clk_prsc = clk_prsc << UART_CTRL_PRSC0;
uint32_t baud_prsc = (uint32_t)i;
baud_prsc = baud_prsc - 1;
baud_prsc = baud_prsc << UART_CTRL_BAUD00;
uint32_t uart_en = 1;
uart_en = uart_en << UART_CTRL_EN;
uint32_t parity_config = (uint32_t)(parity & 3);
parity_config = parity_config << UART_CTRL_PMODE0;
uint32_t flow_control = (uint32_t)(flow_con & 3);
flow_control = flow_control << UART_CTRL_RTS_EN;
/* Enable UART0 for SIM mode. */
/* USE THIS ONLY FOR SIMULATION! */
#ifdef UART_SIM_MODE
#warning <UART_SIM_MODE> is obsolete (but still supported for compatibility). Please consider using the new flag <UART0_SIM_MODE>.
#endif
#if defined UART0_SIM_MODE || defined UART_SIM_MODE
#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!
uint32_t sim_mode = 1 << UART_CTRL_SIM_MODE;
#else
uint32_t sim_mode = 0;
#endif
NEORV32_UART0.CTRL = clk_prsc | baud_prsc | uart_en | parity_config | sim_mode | flow_control;
}
/**********************************************************************//**
* Disable UART0.
**************************************************************************/
void neorv32_uart0_disable(void) {
NEORV32_UART0.CTRL &= ~((uint32_t)(1 << UART_CTRL_EN));
}
/**********************************************************************//**
* Enable UART0.
**************************************************************************/
void neorv32_uart0_enable(void) {
NEORV32_UART0.CTRL |= ((uint32_t)(1 << UART_CTRL_EN));
}
/**********************************************************************//**
* Send single char via UART0.
*
* @note This function is blocking.
*
* @param[in] c Char to be send.
**************************************************************************/
void neorv32_uart0_putc(char c) {
// wait for previous transfer to finish
while ((NEORV32_UART0.CTRL & (1<<UART_CTRL_TX_FULL)) != 0); // wait for space in TX FIFO
NEORV32_UART0.DATA = ((uint32_t)c) << UART_DATA_LSB;
}
/**********************************************************************//**
* Check if UART0 TX is busy (transmitter busy or data left in TX buffer).
*
* @note This function is blocking.
*
* @return 0 if idle, 1 if busy
**************************************************************************/
int neorv32_uart0_tx_busy(void) {
uint32_t ctrl = NEORV32_UART0.CTRL;
if (((ctrl & (1<<UART_CTRL_TX_BUSY)) != 0) || // TX engine busy
((ctrl & (1<<UART_CTRL_TX_EMPTY)) == 0)) { // TX buffer not empty
return 1;
}
return 0;
}
/**********************************************************************//**
* Get char from UART0.
*
* @note This function is blocking and does not check for UART frame/parity errors.
*
* @return Received char.
**************************************************************************/
char neorv32_uart0_getc(void) {
uint32_t d = 0;
while (1) {
d = NEORV32_UART0.DATA;
if ((d & (1<<UART_DATA_AVAIL)) != 0) { // char received?
return (char)d;
}
}
}
/**********************************************************************//**
* Get char from UART0 (and check errors).
*
* @note This function is non-blocking and checks for frame and parity errors.
*
* @param[in,out] data Received char.
* @return Status code:
* 0 = char received without errors
* -1 = nothing received
* -2 = char received with frame error
* -3 = char received with parity error
* -4 = char received with overrun error.
**************************************************************************/
int neorv32_uart0_getc_safe(char *data) {
uint32_t uart_rx = NEORV32_UART0.DATA;
// get received byte (if there is any)
*data = (char)uart_rx;
// check if no data available at all
if ((uart_rx & (1<<UART_DATA_AVAIL)) == 0) {
return -1;
}
// check for frame error
if (uart_rx & (1<<UART_DATA_FERR)) {
return -2;
}
// check for parity error
if (uart_rx & (1<<UART_DATA_PERR)) {
return -3;
}
// check for overrun error
if (uart_rx & (1<<UART_DATA_OVERR)) {
return -4;
}
return 0; // all fine
}
/**********************************************************************//**
* Check if UART0 has received a char.
*
* @note This function is non-blocking.
* @note Use neorv32_uart0_char_received_get(void) to get the char.
*
* @return =!0 when a char has been received.
**************************************************************************/
int neorv32_uart0_char_received(void) {
if (NEORV32_UART0.CTRL & (1<<UART_CTRL_RX_EMPTY)) {
return 0;
}
else {
return 1;
}
}
/**********************************************************************//**
* Get a received char from UART0.
*
* @note This function is non-blocking.
* @note Should only be used in combination with neorv32_uart_char_received(void).
*
* @return Received char.
**************************************************************************/
char neorv32_uart0_char_received_get(void) {
return (char)NEORV32_UART0.DATA;
}
/**********************************************************************//**
* Print string (zero-terminated) via UART0. Print full line break "\r\n" for every '\n'.
*
* @note This function is blocking.
*
* @param[in] s Pointer to string.
**************************************************************************/
void neorv32_uart0_puts(const char *s) {
char c = 0;
while ((c = *s++)) {
if (c == '\n') {
neorv32_uart0_putc('\r');
}
neorv32_uart0_putc(c);
}
}
/**********************************************************************//**
* Custom version of 'printf' function using UART0.
*
* @note This function is blocking.
*
* @param[in] format Pointer to format string.
*
* <TABLE>
* <TR><TD>%s</TD><TD>String (array of chars, zero-terminated)</TD></TR>
* <TR><TD>%c</TD><TD>Single char</TD></TR>
* <TR><TD>%d/%i</TD><TD>32-bit signed number, printed as decimal</TD></TR>
* <TR><TD>%u</TD><TD>32-bit unsigned number, printed as decimal</TD></TR>
* <TR><TD>%x</TD><TD>32-bit number, printed as 8-char hexadecimal - lower-case</TD></TR>
* <TR><TD>%X</TD><TD>32-bit number, printed as 8-char hexadecimal - upper-case</TD></TR>
* <TR><TD>%p</TD><TD>32-bit pointer, printed as 8-char hexadecimal - lower-case</TD></TR>
* </TABLE>
**************************************************************************/
void neorv32_uart0_printf(const char *format, ...) {
char c, string_buf[11];
int32_t n;
va_list a;
va_start(a, format);
while ((c = *format++)) {
if (c == '%') {
c = *format++;
switch (c) {
case 's': // string
neorv32_uart0_puts(va_arg(a, char*));
break;
case 'c': // char
neorv32_uart0_putc((char)va_arg(a, int));
break;
case 'i': // 32-bit signed
case 'd':
n = (int32_t)va_arg(a, int32_t);
if (n < 0) {
n = -n;
neorv32_uart0_putc('-');
}
__neorv32_uart_itoa((uint32_t)n, string_buf);
neorv32_uart0_puts(string_buf);
break;
case 'u': // 32-bit unsigned
__neorv32_uart_itoa(va_arg(a, uint32_t), string_buf);
neorv32_uart0_puts(string_buf);
break;
case 'x': // 32-bit hexadecimal
case 'p':
case 'X':
__neorv32_uart_tohex(va_arg(a, uint32_t), string_buf);
if (c == 'X') {
__neorv32_uart_touppercase(11, string_buf);
}
neorv32_uart0_puts(string_buf);
break;
default: // unsupported format
neorv32_uart0_putc('%');
neorv32_uart0_putc(c);
break;
}
}
else {
if (c == '\n') {
neorv32_uart0_putc('\r');
}
neorv32_uart0_putc(c);
}
}
va_end(a);
}
/**********************************************************************//**
* Simplified custom version of 'scanf' function for UART0.
*
* @note This function is blocking.
*
* @param[in,out] buffer Pointer to array of chars to store string.
* @param[in] max_size Maximum number of chars to sample.
* @param[in] echo Echo UART input when 1.
* @return Number of chars read.
**************************************************************************/
int neorv32_uart0_scan(char *buffer, int max_size, int echo) {
char c = 0;
int length = 0;
while (1) {
c = neorv32_uart0_getc();
if (c == '\b') { // BACKSPACE
if (length != 0) {
if (echo) {
neorv32_uart0_puts("\b \b"); // delete last char in console
}
buffer--;
length--;
}
}
else if (c == '\r') // carriage return
break;
else if ((c >= ' ') && (c <= '~') && (length < (max_size-1))) {
if (echo) {
neorv32_uart0_putc(c); // echo
}
*buffer++ = c;
length++;
}
}
*buffer = '\0'; // terminate string
return length;
}
// #################################################################################################
// Secondary UART (UART1)
// #################################################################################################
/**********************************************************************//**
* Check if UART1 unit was synthesized.
*
* @return 0 if UART1 was not synthesized, 1 if UART1 is available.
**************************************************************************/
int neorv32_uart1_available(void) {
if (NEORV32_SYSINFO->SOC & (1 << SYSINFO_SOC_IO_UART1)) {
return 1;
}
else {
return 0;
}
}
/**********************************************************************//**
* Enable and configure secondary UART (UART1).
*
* @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!
* @note To enable simulation mode add <USER_FLAGS+=-DUART1_SIM_MODE> when compiling.
*
* @warning The baud rate is computed using INTEGER operations (truncation errors might occur).
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @param[in] baudrate Targeted BAUD rate (e.g. 9600).
* @param[in] parity Parity configuration (00=off, 10=even, 11=odd), see #NEORV32_UART_PARITY_enum.
* @param[in] flow_con Hardware flow control configuration (00=off, 01=RTS, 10=CTS, 11=RTS/CTS), see #NEORV32_UART_FLOW_CONTROL_enum.
**************************************************************************/
void neorv32_uart1_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con) {
void neorv32_uart_setup(neorv32_uart_t *UARTx, uint32_t baudrate, uint8_t parity, uint8_t flow_con) {
NEORV32_UART1.CTRL = 0; // reset
uint32_t clock = NEORV32_SYSINFO->CLK; // get system clock
uint16_t i = 0; // BAUD rate divisor
uint8_t p = 0; // initial prsc = CLK/2
uint32_t sim_mode = 0; // redirect output to stdio
uint32_t clock = NEORV32_SYSINFO->CLK;
uint16_t i = 0; // BAUD rate divisor
uint8_t p = 0; // initial prsc = CLK/2
// Sim mode requested?
/* Enable UART0/1 for SIM mode. */
/* USE THIS ONLY FOR SIMULATION! */
#ifdef UART_SIM_MODE
#warning <UART_SIM_MODE> is obsolete (but still supported for compatibility). Please consider using the new flag <UART0_SIM_MODE>.
#endif
#ifdef UART0_SIM_MODE
#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!
if (((uint32_t)UARTx) == NEORV32_UART0_BASE) {
sim_mode = 1 << UART_CTRL_SIM_MODE;
}
#endif
#ifdef UART1_SIM_MODE
#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!
if (((uint32_t)UARTx) == NEORV32_UART1_BASE) {
sim_mode = 1 << UART_CTRL_SIM_MODE;
}
#endif
// reset
UARTx->CTRL = 0;
// raw clock prescaler
#ifndef make_bootloader
@ -538,62 +190,60 @@ void neorv32_uart1_setup(uint32_t baudrate, uint8_t parity, uint8_t flow_con) {
uint32_t flow_control = (uint32_t)(flow_con & 3);
flow_control = flow_control << UART_CTRL_RTS_EN;
/* Enable UART1 for SIM mode. */
/* USE THIS ONLY FOR SIMULATION! */
#ifdef UART1_SIM_MODE
#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!
uint32_t sim_mode = 1 << UART_CTRL_SIM_MODE;
#else
uint32_t sim_mode = 0;
#endif
NEORV32_UART1.CTRL = clk_prsc | baud_prsc | uart_en | parity_config | sim_mode | flow_control;
UARTx->CTRL = clk_prsc | baud_prsc | uart_en | parity_config | sim_mode | flow_control;
}
/**********************************************************************//**
* Disable UART1.
* Enable UART
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
**************************************************************************/
void neorv32_uart1_disable(void) {
void neorv32_uart_enable(neorv32_uart_t *UARTx) {
NEORV32_UART1.CTRL &= ~((uint32_t)(1 << UART_CTRL_EN));
UARTx->CTRL |= ((uint32_t)(1 << UART_CTRL_EN));
}
/**********************************************************************//**
* Enable UART1.
* Disable UART
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
**************************************************************************/
void neorv32_uart1_enable(void) {
void neorv32_uart_disable(neorv32_uart_t *UARTx) {
NEORV32_UART1.CTRL |= ((uint32_t)(1 << UART_CTRL_EN));
UARTx->CTRL &= ~((uint32_t)(1 << UART_CTRL_EN));
}
/**********************************************************************//**
* Send single char via UART1.
* Send single char via UART
*
* @note This function is blocking.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @param[in] c Char to be send.
**************************************************************************/
void neorv32_uart1_putc(char c) {
void neorv32_uart_putc(neorv32_uart_t *UARTx, char c) {
// wait for previous transfer to finish
while ((NEORV32_UART1.CTRL & (1<<UART_CTRL_TX_FULL)) != 0); // wait for space in TX FIFO
NEORV32_UART1.DATA = ((uint32_t)c) << UART_DATA_LSB;
while ((UARTx->CTRL & (1<<UART_CTRL_TX_FULL)) != 0); // wait for space in TX FIFO
UARTx->DATA = ((uint32_t)c) << UART_DATA_LSB;
}
/**********************************************************************//**
* Check if UART1 TX is busy (transmitter busy or data left in TX buffer).
* Check if UART TX is busy (transmitter busy or data left in TX buffer).
*
* @note This function is blocking.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @return 0 if idle, 1 if busy
**************************************************************************/
int neorv32_uart1_tx_busy(void) {
int neorv32_uart_tx_busy(neorv32_uart_t *UARTx) {
uint32_t ctrl = NEORV32_UART1.CTRL;
uint32_t ctrl = UARTx->CTRL;
if (((ctrl & (1<<UART_CTRL_TX_BUSY)) != 0) || // TX engine busy
((ctrl & (1<<UART_CTRL_TX_EMPTY)) == 0)) { // TX buffer not empty
@ -604,17 +254,18 @@ int neorv32_uart1_tx_busy(void) {
/**********************************************************************//**
* Get char from UART1.
* Get char from UART.
*
* @note This function is blocking and does not check for UART frame/parity errors.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @return Received char.
**************************************************************************/
char neorv32_uart1_getc(void) {
char neorv32_uart_getc(neorv32_uart_t *UARTx) {
uint32_t d = 0;
while (1) {
d = NEORV32_UART1.DATA;
d = UARTx->DATA;
if ((d & (1<<UART_DATA_AVAIL)) != 0) { // char received?
return (char)d;
}
@ -623,10 +274,11 @@ char neorv32_uart1_getc(void) {
/**********************************************************************//**
* Get char from UART1 (and check errors).
* Get char from UART (and check errors).
*
* @note This function is non-blocking and checks for frame and parity errors.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @param[in,out] data Received char.
* @return Status code:
* 0 = char received without errors
@ -635,9 +287,9 @@ char neorv32_uart1_getc(void) {
* -3 = char received with parity error
* -4 = char received with overrun error.
**************************************************************************/
int neorv32_uart1_getc_safe(char *data) {
int neorv32_uart_getc_safe(neorv32_uart_t *UARTx, char *data) {
uint32_t uart_rx = NEORV32_UART1.DATA;
uint32_t uart_rx = UARTx->DATA;
// get received byte (if there is any)
*data = (char)uart_rx;
@ -667,16 +319,17 @@ int neorv32_uart1_getc_safe(char *data) {
/**********************************************************************//**
* Check if UART1 has received a char.
* Check if UART has received a char.
*
* @note This function is non-blocking.
* @note Use neorv32_uart0_char_received_get(void) to get the char.
* @note Use neorv32_uart_char_received_get(void) to get the char.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @return =!0 when a char has been received.
**************************************************************************/
int neorv32_uart1_char_received(void) {
int neorv32_uart_char_received(neorv32_uart_t *UARTx) {
if (NEORV32_UART1.CTRL & (1<<UART_CTRL_RX_EMPTY)) {
if (UARTx->CTRL & (1<<UART_CTRL_RX_EMPTY)) {
return 0;
}
else {
@ -686,43 +339,46 @@ int neorv32_uart1_char_received(void) {
/**********************************************************************//**
* Get a received char from UART1.
* Get a received char from UART.
*
* @note This function is non-blocking.
* @note Should only be used in combination with neorv32_uart_char_received(void).
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @return Received char.
**************************************************************************/
char neorv32_uart1_char_received_get(void) {
char neorv32_uart_char_received_get(neorv32_uart_t *UARTx) {
return (char)NEORV32_UART1.DATA;
return (char)(UARTx->DATA);
}
/**********************************************************************//**
* Print string (zero-terminated) via UART1. Print full line break "\r\n" for every '\n'.
* Print string (zero-terminated) via UART. Print full line break "\r\n" for every '\n'.
*
* @note This function is blocking.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @param[in] s Pointer to string.
**************************************************************************/
void neorv32_uart1_puts(const char *s) {
void neorv32_uart_puts(neorv32_uart_t *UARTx, const char *s) {
char c = 0;
while ((c = *s++)) {
if (c == '\n') {
neorv32_uart1_putc('\r');
neorv32_uart_putc(UARTx, '\r');
}
neorv32_uart1_putc(c);
neorv32_uart_putc(UARTx, c);
}
}
/**********************************************************************//**
* Custom version of 'printf' function using UART1.
* Custom version of 'printf' function using UART.
*
* @note This function is blocking.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @param[in] format Pointer to format string.
*
* <TABLE>
@ -735,7 +391,7 @@ void neorv32_uart1_puts(const char *s) {
* <TR><TD>%p</TD><TD>32-bit pointer, printed as 8-char hexadecimal - lower-case</TD></TR>
* </TABLE>
**************************************************************************/
void neorv32_uart1_printf(const char *format, ...) {
void neorv32_uart_printf(neorv32_uart_t *UARTx, const char *format, ...) {
char c, string_buf[11];
int32_t n;
@ -748,24 +404,24 @@ void neorv32_uart1_printf(const char *format, ...) {
c = *format++;
switch (c) {
case 's': // string
neorv32_uart1_puts(va_arg(a, char*));
neorv32_uart_puts(UARTx, va_arg(a, char*));
break;
case 'c': // char
neorv32_uart1_putc((char)va_arg(a, int));
neorv32_uart_putc(UARTx, (char)va_arg(a, int));
break;
case 'i': // 32-bit signed
case 'd':
n = (int32_t)va_arg(a, int32_t);
if (n < 0) {
n = -n;
neorv32_uart1_putc('-');
neorv32_uart_putc(UARTx, '-');
}
__neorv32_uart_itoa((uint32_t)n, string_buf);
neorv32_uart1_puts(string_buf);
neorv32_uart_puts(UARTx, string_buf);
break;
case 'u': // 32-bit unsigned
__neorv32_uart_itoa(va_arg(a, uint32_t), string_buf);
neorv32_uart1_puts(string_buf);
neorv32_uart_puts(UARTx, string_buf);
break;
case 'x': // 32-bit hexadecimal
case 'p':
@ -774,19 +430,19 @@ void neorv32_uart1_printf(const char *format, ...) {
if (c == 'X') {
__neorv32_uart_touppercase(11, string_buf);
}
neorv32_uart1_puts(string_buf);
neorv32_uart_puts(UARTx, string_buf);
break;
default: // unsupported format
neorv32_uart1_putc('%');
neorv32_uart1_putc(c);
neorv32_uart_putc(UARTx, '%');
neorv32_uart_putc(UARTx, c);
break;
}
}
else {
if (c == '\n') {
neorv32_uart1_putc('\r');
neorv32_uart_putc(UARTx, '\r');
}
neorv32_uart1_putc(c);
neorv32_uart_putc(UARTx, c);
}
}
va_end(a);
@ -794,26 +450,27 @@ void neorv32_uart1_printf(const char *format, ...) {
/**********************************************************************//**
* Simplified custom version of 'scanf' function for UART1.
* Simplified custom version of 'scanf' function for UART.
*
* @note This function is blocking.
*
* @param[in,out] hardware handle to UART register, #neorv32_uart_t.
* @param[in,out] buffer Pointer to array of chars to store string.
* @param[in] max_size Maximum number of chars to sample.
* @param[in] echo Echo UART input when 1.
* @return Number of chars read.
**************************************************************************/
int neorv32_uart1_scan(char *buffer, int max_size, int echo) {
int neorv32_uart_scan(neorv32_uart_t *UARTx, char *buffer, int max_size, int echo) {
char c = 0;
int length = 0;
while (1) {
c = neorv32_uart1_getc();
c = neorv32_uart_getc(UARTx);
if (c == '\b') { // BACKSPACE
if (length != 0) {
if (echo) {
neorv32_uart1_puts("\b \b"); // delete last char in console
neorv32_uart_puts(UARTx, "\b \b"); // delete last char in console
}
buffer--;
length--;
@ -823,7 +480,7 @@ int neorv32_uart1_scan(char *buffer, int max_size, int echo) {
break;
else if ((c >= ' ') && (c <= '~') && (length < (max_size-1))) {
if (echo) {
neorv32_uart1_putc(c); // echo
neorv32_uart_putc(UARTx, c); // echo
}
*buffer++ = c;
length++;
@ -835,7 +492,6 @@ int neorv32_uart1_scan(char *buffer, int max_size, int echo) {
}
// #################################################################################################
// Shared functions
// #################################################################################################