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

Subversion Repositories neorv32

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

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

Rev 36 Rev 42
Line 73... Line 73...
 *
 *
 * @warning The 'UART_SIM_MODE' compiler flag will configure UART for simulation mode: all UART TX data will be redirected to simulation output. Use this for simulations only!
 * @warning The 'UART_SIM_MODE' compiler flag will configure UART for simulation mode: all UART TX data will be redirected to simulation output. Use this for simulations only!
 * @warning To enable simulation mode add <USER_FLAGS+=-DUART_SIM_MODE> when compiling.
 * @warning To enable simulation mode add <USER_FLAGS+=-DUART_SIM_MODE> when compiling.
 *
 *
 * @param[in] baudrate Targeted BAUD rate (e.g. 9600).
 * @param[in] baudrate Targeted BAUD rate (e.g. 9600).
 
 * @param[in] parity PArity configuration (00=off, 10=even, 11=odd).
 * @param[in] rx_irq Enable RX interrupt (data received) when 1.
 * @param[in] rx_irq Enable RX interrupt (data received) when 1.
 * @param[in] tx_irq Enable TX interrupt (transmission done) when 1.
 * @param[in] tx_irq Enable TX interrupt (transmission done) when 1.
 **************************************************************************/
 **************************************************************************/
void neorv32_uart_setup(uint32_t baudrate, uint8_t rx_irq, uint8_t tx_irq) {
void neorv32_uart_setup(uint32_t baudrate, uint8_t parity, uint8_t rx_irq, uint8_t tx_irq) {
 
 
  UART_CT = 0; // reset
  UART_CT = 0; // reset
 
 
  uint32_t clock = SYSINFO_CLK;
  uint32_t clock = SYSINFO_CLK;
  uint16_t i = 0; // BAUD rate divisor
  uint16_t i = 0; // BAUD rate divisor
Line 114... Line 115...
  baud = baud << UART_CT_BAUD00;
  baud = baud << UART_CT_BAUD00;
 
 
  uint32_t uart_en = 1;
  uint32_t uart_en = 1;
  uart_en = uart_en << UART_CT_EN;
  uart_en = uart_en << UART_CT_EN;
 
 
 
  uint32_t parity_config = (uint32_t)(parity & 3);
 
  parity_config = parity_config << UART_CT_PMODE0;
 
 
  uint32_t rx_irq_en = (uint32_t)(rx_irq & 1);
  uint32_t rx_irq_en = (uint32_t)(rx_irq & 1);
  rx_irq_en = rx_irq_en << UART_CT_RX_IRQ;
  rx_irq_en = rx_irq_en << UART_CT_RX_IRQ;
 
 
  uint32_t tx_irq_en = (uint32_t)(tx_irq & 1);
  uint32_t tx_irq_en = (uint32_t)(tx_irq & 1);
  tx_irq_en = tx_irq_en << UART_CT_TX_IRQ;
  tx_irq_en = tx_irq_en << UART_CT_TX_IRQ;
 
 
  /* Enable the UART for SIM mode. */
  /* Enable the UART for SIM mode. */
  /* Only use this for simulation! */
  /* USE THIS ONLY FOR SIMULATION! */
#ifdef UART_SIM_MODE
#ifdef UART_SIM_MODE
  #warning UART_SIM_MODE enabled! Sending all UART.TX data to text.io simulation output instead of real UART transmitter. Use this for simulations only!
  #warning UART_SIM_MODE enabled! Sending all UART.TX data to text.io simulation output instead of real UART transmitter. Use this for simulations only!
  uint32_t sim_mode = 1 << UART_CT_SIM_MODE;
  uint32_t sim_mode = 1 << UART_CT_SIM_MODE;
#else
#else
  uint32_t sim_mode = 0;
  uint32_t sim_mode = 0;
#endif
#endif
 
 
  UART_CT = prsc | baud | uart_en | rx_irq_en | tx_irq_en | sim_mode;
  UART_CT = prsc | baud | uart_en | parity_config | rx_irq_en | tx_irq_en | sim_mode;
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Disable UART.
 * Disable UART.
Line 180... Line 184...
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Get char from UART.
 * Get char from UART.
 *
 *
 * @note This function is blocking.
 * @note This function is blocking and does not check for UART frame/parity errors.
 *
 *
 * @return Received char.
 * @return Received char.
 **************************************************************************/
 **************************************************************************/
char neorv32_uart_getc(void) {
char neorv32_uart_getc(void) {
 
 
Line 197... Line 201...
  }
  }
}
}
 
 
 
 
/**********************************************************************//**
/**********************************************************************//**
 * Check if UARt has received a char.
 * Get char from UART (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=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).
 
 **************************************************************************/
 
int neorv32_uart_getc_secure(char *data) {
 
 
 
  uint32_t uart_rx = UART_DATA;
 
  if (uart_rx & (1<<UART_DATA_AVAIL)) { // char available at all?
 
 
 
    int status = 0;
 
 
 
    // check for frame error
 
    if (uart_rx & (1<<UART_DATA_FERR)) {
 
      status -= 1;
 
    }
 
 
 
    // check for parity error
 
    if (uart_rx & (1<<UART_DATA_PERR)) {
 
      status -= 2;
 
    }
 
 
 
    if (status == 0) {
 
      status = 1;
 
    }
 
 
 
    // get received byte
 
    *data =  (char)uart_rx;
 
 
 
    return status;
 
  }
 
  else {
 
    return 0;
 
  }
 
}
 
 
 
 
 
/**********************************************************************//**
 
 * Check if UART has received a char.
 *
 *
 * @note This function is non-blocking.
 * @note This function is non-blocking.
 * @note Use neorv32_uart_char_received_get(void) to get the char.
 * @note Use neorv32_uart_char_received_get(void) to get the char.
 *
 *
 * @return =!0 when a char has been received.
 * @return =!0 when a char has been received.

powered by: WebSVN 2.1.0

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