2012-02-03 06:08:17 -05:00
|
|
|
#include <uart.h>
|
|
|
|
#include <irq.h>
|
2013-11-24 13:50:17 -05:00
|
|
|
#include <generated/csr.h>
|
2013-03-25 09:42:48 -04:00
|
|
|
#include <hw/flags.h>
|
2012-02-03 06:08:17 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Buffer sizes must be a power of 2 so that modulos can be computed
|
|
|
|
* with logical AND.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define UART_RINGBUFFER_SIZE_RX 128
|
|
|
|
#define UART_RINGBUFFER_MASK_RX (UART_RINGBUFFER_SIZE_RX-1)
|
|
|
|
|
|
|
|
static char rx_buf[UART_RINGBUFFER_SIZE_RX];
|
|
|
|
static volatile unsigned int rx_produce;
|
2015-07-19 03:23:34 -04:00
|
|
|
static unsigned int rx_consume;
|
2012-02-03 06:08:17 -05:00
|
|
|
|
|
|
|
#define UART_RINGBUFFER_SIZE_TX 128
|
|
|
|
#define UART_RINGBUFFER_MASK_TX (UART_RINGBUFFER_SIZE_TX-1)
|
|
|
|
|
|
|
|
static char tx_buf[UART_RINGBUFFER_SIZE_TX];
|
|
|
|
static unsigned int tx_produce;
|
2015-07-19 03:23:34 -04:00
|
|
|
static volatile unsigned int tx_consume;
|
2012-02-03 06:08:17 -05:00
|
|
|
|
|
|
|
void uart_isr(void)
|
|
|
|
{
|
2015-07-19 03:23:34 -04:00
|
|
|
unsigned int stat, rx_produce_next;
|
|
|
|
|
2013-03-25 09:42:48 -04:00
|
|
|
stat = uart_ev_pending_read();
|
2012-02-03 06:08:17 -05:00
|
|
|
|
2012-02-06 11:53:41 -05:00
|
|
|
if(stat & UART_EV_RX) {
|
2015-05-01 09:58:10 -04:00
|
|
|
while(!uart_rxempty_read()) {
|
2015-07-19 03:23:34 -04:00
|
|
|
rx_produce_next = (rx_produce + 1) & UART_RINGBUFFER_MASK_RX;
|
|
|
|
if(rx_produce_next != rx_consume) {
|
|
|
|
rx_buf[rx_produce] = uart_rxtx_read();
|
|
|
|
rx_produce = rx_produce_next;
|
|
|
|
}
|
2015-05-01 09:58:10 -04:00
|
|
|
uart_ev_pending_write(UART_EV_RX);
|
|
|
|
}
|
2012-02-03 06:08:17 -05:00
|
|
|
}
|
|
|
|
|
2012-02-06 11:53:41 -05:00
|
|
|
if(stat & UART_EV_TX) {
|
2013-10-22 13:44:58 -04:00
|
|
|
uart_ev_pending_write(UART_EV_TX);
|
2015-07-19 03:23:34 -04:00
|
|
|
while((tx_consume != tx_produce) && !uart_txfull_read()) {
|
2013-03-25 09:42:48 -04:00
|
|
|
uart_rxtx_write(tx_buf[tx_consume]);
|
2012-02-03 06:08:17 -05:00
|
|
|
tx_consume = (tx_consume + 1) & UART_RINGBUFFER_MASK_TX;
|
2015-05-01 09:58:10 -04:00
|
|
|
}
|
2012-02-03 06:08:17 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Do not use in interrupt handlers! */
|
|
|
|
char uart_read(void)
|
|
|
|
{
|
|
|
|
char c;
|
2015-07-19 03:23:34 -04:00
|
|
|
|
|
|
|
if(irq_getie()) {
|
|
|
|
while(rx_consume == rx_produce);
|
|
|
|
} else if (rx_consume == rx_produce) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-03 06:08:17 -05:00
|
|
|
c = rx_buf[rx_consume];
|
|
|
|
rx_consume = (rx_consume + 1) & UART_RINGBUFFER_MASK_RX;
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
|
|
|
int uart_read_nonblock(void)
|
|
|
|
{
|
|
|
|
return (rx_consume != rx_produce);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_write(char c)
|
|
|
|
{
|
|
|
|
unsigned int oldmask;
|
2015-07-19 03:23:34 -04:00
|
|
|
unsigned int tx_produce_next = (tx_produce + 1) & UART_RINGBUFFER_MASK_TX;
|
|
|
|
|
2012-02-07 09:12:27 -05:00
|
|
|
if(irq_getie()) {
|
2015-07-19 03:23:34 -04:00
|
|
|
while(tx_produce_next == tx_consume);
|
|
|
|
} else if(tx_produce_next == tx_consume) {
|
|
|
|
return;
|
2012-02-07 09:12:27 -05:00
|
|
|
}
|
2012-02-03 06:08:17 -05:00
|
|
|
|
2015-07-19 03:23:34 -04:00
|
|
|
oldmask = irq_getmask();
|
|
|
|
irq_setmask(oldmask & ~(1 << UART_INTERRUPT));
|
|
|
|
if((tx_consume != tx_produce) || uart_txfull_read()) {
|
2012-02-03 06:08:17 -05:00
|
|
|
tx_buf[tx_produce] = c;
|
2015-07-19 03:23:34 -04:00
|
|
|
tx_produce = tx_produce_next;
|
|
|
|
} else {
|
|
|
|
uart_rxtx_write(c);
|
2012-02-03 06:08:17 -05:00
|
|
|
}
|
|
|
|
irq_setmask(oldmask);
|
|
|
|
}
|
|
|
|
|
|
|
|
void uart_init(void)
|
|
|
|
{
|
|
|
|
rx_produce = 0;
|
|
|
|
rx_consume = 0;
|
2015-07-19 03:23:34 -04:00
|
|
|
|
2012-02-03 06:08:17 -05:00
|
|
|
tx_produce = 0;
|
|
|
|
tx_consume = 0;
|
|
|
|
|
2013-03-25 09:42:48 -04:00
|
|
|
uart_ev_pending_write(uart_ev_pending_read());
|
|
|
|
uart_ev_enable_write(UART_EV_TX | UART_EV_RX);
|
2015-07-19 03:23:34 -04:00
|
|
|
irq_setmask(irq_getmask() | (1 << UART_INTERRUPT));
|
2012-02-03 06:08:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void uart_sync(void)
|
|
|
|
{
|
2015-07-19 03:23:34 -04:00
|
|
|
while(tx_consume != tx_produce);
|
2012-02-03 06:08:17 -05:00
|
|
|
}
|