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

Subversion Repositories neorv32

[/] [neorv32/] [trunk/] [sw/] [lib/] [source/] [neorv32_uart.c] - Diff between revs 64 and 65

Go to most recent revision | Show entire file | Details | Blame | View Log

Rev 64 Rev 65
Line 48... Line 48...
#include <string.h>
#include <string.h>
#include <stdarg.h>
#include <stdarg.h>
 
 
/// \cond
/// \cond
// Private functions
// Private functions
static void __neorv32_uart_itoa(uint32_t x, char *res) __attribute__((unused)); // GCC: do not ouput a warning when this variable is unused
static void __neorv32_uart_itoa(uint32_t x, char *res) __attribute__((unused)); // GCC: do not output a warning when this variable is unused
static void __neorv32_uart_tohex(uint32_t x, char *res) __attribute__((unused)); // GCC: do not ouput a warning when this variable is unused
static void __neorv32_uart_tohex(uint32_t x, char *res) __attribute__((unused)); // GCC: do not output a warning when this variable is unused
/// \endcond
/// \endcond
 
 
 
 
 
 
// #################################################################################################
// #################################################################################################
Line 334... Line 334...
 * @param[in] c Char to be send.
 * @param[in] c Char to be send.
 **************************************************************************/
 **************************************************************************/
void neorv32_uart0_putc(char c) {
void neorv32_uart0_putc(char c) {
 
 
  // wait for previous transfer to finish
  // wait for previous transfer to finish
  while ((NEORV32_UART0.CTRL & (1<<UART_CTRL_TX_BUSY)) != 0);
  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;
  NEORV32_UART0.DATA = ((uint32_t)c) << UART_DATA_LSB;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Check if UART0 TX is busy.
 * Check if UART0 TX is busy (transmitter busy or data left in TX buffer).
 *
 *
 * @note This function is blocking.
 * @note This function is blocking.
 *
 *
 * @return 0 if idle, 1 if busy
 * @return 0 if idle, 1 if busy
 **************************************************************************/
 **************************************************************************/
int neorv32_uart0_tx_busy(void) {
int neorv32_uart0_tx_busy(void) {
 
 
  if ((NEORV32_UART0.CTRL & (1<<UART_CTRL_TX_BUSY)) != 0) {
  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 1;
  }
  }
  return 0;
  return 0;
}
}
 
 
Line 380... Line 383...
 * Get char from UART0 (and check errors).
 * Get char from UART0 (and check errors).
 *
 *
 * @note This function is non-blocking and checks for frame and parity errors.
 * @note This function is non-blocking and checks for frame and parity errors.
 *
 *
 * @param[in,out] data Received char.
 * @param[in,out] data Received char.
 * @return Status code (0=nothing received, 1: char received without errors; -1: char received with frame error; -2: char received with parity error; -3 char received with frame & parity error).
 * @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) {
int neorv32_uart0_getc_safe(char *data) {
 
 
  uint32_t uart_rx = NEORV32_UART0.DATA;
  uint32_t uart_rx = NEORV32_UART0.DATA;
  if (uart_rx & (1<<UART_DATA_AVAIL)) { // char available at all?
 
 
 
    int status = 0;
  // 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
    // check for frame error
    if (uart_rx & (1<<UART_DATA_FERR)) {
    if (uart_rx & (1<<UART_DATA_FERR)) {
      status -= 1;
    return -2;
    }
    }
 
 
    // check for parity error
    // check for parity error
    if (uart_rx & (1<<UART_DATA_PERR)) {
    if (uart_rx & (1<<UART_DATA_PERR)) {
      status -= 2;
    return -3;
    }
    }
 
 
    if (status == 0) {
  // check for overrun error
      status = 1;
  if (uart_rx & (1<<UART_DATA_OVERR)) {
 
    return -4;
    }
    }
 
 
    // get received byte
  return 0; // all fine
    *data =  (char)uart_rx;
 
 
 
    return status;
 
  }
 
  else {
 
    return 0;
 
  }
 
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Check if UART0 has received a char.
 * Check if UART0 has received a char.
Line 694... Line 701...
 * @param[in] c Char to be send.
 * @param[in] c Char to be send.
 **************************************************************************/
 **************************************************************************/
void neorv32_uart1_putc(char c) {
void neorv32_uart1_putc(char c) {
 
 
  // wait for previous transfer to finish
  // wait for previous transfer to finish
  while ((NEORV32_UART1.CTRL & (1<<UART_CTRL_TX_BUSY)) != 0);
  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;
  NEORV32_UART1.DATA = ((uint32_t)c) << UART_DATA_LSB;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Check if UART1 TX is busy.
 * Check if UART1 TX is busy (transmitter busy or data left in TX buffer).
 *
 *
 * @note This function is blocking.
 * @note This function is blocking.
 *
 *
 * @return 0 if idle, 1 if busy
 * @return 0 if idle, 1 if busy
 **************************************************************************/
 **************************************************************************/
int neorv32_uart1_tx_busy(void) {
int neorv32_uart1_tx_busy(void) {
 
 
  if ((NEORV32_UART1.CTRL & (1<<UART_CTRL_TX_BUSY)) != 0) {
  uint32_t ctrl = NEORV32_UART1.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 1;
  }
  }
  return 0;
  return 0;
}
}
 
 
Line 740... Line 750...
 * Get char from UART1 (and check errors).
 * Get char from UART1 (and check errors).
 *
 *
 * @note This function is non-blocking and checks for frame and parity errors.
 * @note This function is non-blocking and checks for frame and parity errors.
 *
 *
 * @param[in,out] data Received char.
 * @param[in,out] data Received char.
 * @return Status code (0=nothing received, 1: char received without errors; -1: char received with frame error; -2: char received with parity error; -3 char received with frame & parity error).
 * @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_uart1_getc_safe(char *data) {
int neorv32_uart1_getc_safe(char *data) {
 
 
  uint32_t uart_rx = NEORV32_UART1.DATA;
  uint32_t uart_rx = NEORV32_UART1.DATA;
  if (uart_rx & (1<<UART_DATA_AVAIL)) { // char available at all?
 
 
 
    int status = 0;
  // 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
    // check for frame error
    if (uart_rx & (1<<UART_DATA_FERR)) {
    if (uart_rx & (1<<UART_DATA_FERR)) {
      status -= 1;
    return -2;
    }
    }
 
 
    // check for parity error
    // check for parity error
    if (uart_rx & (1<<UART_DATA_PERR)) {
    if (uart_rx & (1<<UART_DATA_PERR)) {
      status -= 2;
    return -3;
    }
    }
 
 
    if (status == 0) {
  // check for overrun error
      status = 1;
  if (uart_rx & (1<<UART_DATA_OVERR)) {
 
    return -4;
    }
    }
 
 
    // get received byte
  return 0; // all fine
    *data =  (char)uart_rx;
 
 
 
    return status;
 
  }
 
  else {
 
    return 0;
 
  }
 
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Check if UART1 has received a char.
 * Check if UART1 has received a char.

powered by: WebSVN 2.1.0

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