summaryrefslogtreecommitdiff
path: root/code/fe310/eos/uart.c
diff options
context:
space:
mode:
authorUros Majstorovic <majstor@majstor.org>2019-12-07 17:18:37 +0100
committerUros Majstorovic <majstor@majstor.org>2019-12-07 17:18:37 +0100
commit256db0fba03232ab4ad9a151487677f8d538e69a (patch)
tree790e2e2c104b3fa6c56a15952923bdd4fa1dfb24 /code/fe310/eos/uart.c
parent2e6facf66ee3d6aa37f2fb41096b1763261506a7 (diff)
addded uart driver and cell stub driver
Diffstat (limited to 'code/fe310/eos/uart.c')
-rw-r--r--code/fe310/eos/uart.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/code/fe310/eos/uart.c b/code/fe310/eos/uart.c
new file mode 100644
index 0000000..88931ab
--- /dev/null
+++ b/code/fe310/eos/uart.c
@@ -0,0 +1,100 @@
+#include <stdlib.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "encoding.h"
+#include "platform.h"
+
+#include "eos.h"
+#include "interrupt.h"
+#include "event.h"
+
+#include "uart.h"
+#include "irq_def.h"
+
+static eos_uart_fptr_t uart_handler[EOS_UART_MAX_ETYPE];
+
+static void uart_handler_evt(unsigned char type, unsigned char *buffer, uint16_t len) {
+ unsigned char idx = (type & ~EOS_EVT_MASK) - 1;
+
+ if ((idx < EOS_UART_MAX_ETYPE) && uart_handler[idx]) {
+ uart_handler[idx](type);
+ } else {
+ eos_evtq_bad_handler(type, buffer, len);
+ }
+}
+
+static void uart_handler_intr(void) {
+ if (UART0_REG(UART_REG_IP) & UART_IP_TXWM) {
+ UART0_REG(UART_REG_IE) &= ~UART_IP_TXWM;
+ eos_evtq_push_isr(EOS_EVT_UART | EOS_UART_ETYPE_TX, NULL, 0);
+ }
+ if (UART0_REG(UART_REG_IP) & UART_IP_RXWM) {
+ UART0_REG(UART_REG_IE) &= ~UART_IP_RXWM;
+ eos_evtq_push_isr(EOS_EVT_UART | EOS_UART_ETYPE_RX, NULL, 0);
+ }
+}
+
+void eos_uart_init(void) {
+ int i;
+
+ for (i=0; i<EOS_UART_MAX_ETYPE; i++) {
+ uart_handler[i] = NULL;
+ }
+ eos_evtq_set_handler(EOS_EVT_UART, uart_handler_evt);
+ eos_intr_set(INT_UART0_BASE, IRQ_PRIORITY_UART, uart_handler_intr);
+}
+
+void eos_uart_set_handler(unsigned char type, eos_uart_fptr_t handler, uint8_t flags) {
+ if (type && (type <= EOS_UART_MAX_ETYPE)) {
+ type--;
+ } else {
+ return;
+ }
+ uart_handler[type] = handler;
+
+ if (flags) eos_evtq_set_flags(EOS_EVT_UART | type + 1, flags);
+}
+
+void eos_uart_txwm_set(uint8_t wm) {
+ UART0_REG(UART_REG_TXCTRL) &= ~UART_TXWM(0xFFFF);
+ UART0_REG(UART_REG_TXCTRL) |= UART_TXWM(wm);
+ UART0_REG(UART_REG_IE) |= UART_IP_TXWM;
+}
+
+void eos_uart_txwm_clear(void) {
+ UART0_REG(UART_REG_IE) &= ~UART_IP_TXWM;
+}
+
+void eos_uart_rxwm_set(uint8_t wm) {
+ UART0_REG(UART_REG_RXCTRL) &= ~UART_RXWM(0xFFFF);
+ UART0_REG(UART_REG_RXCTRL) |= UART_RXWM(wm);
+ UART0_REG(UART_REG_IE) |= UART_IP_RXWM;
+}
+
+void eos_uart_rxwm_clear(void) {
+ UART0_REG(UART_REG_IE) &= ~UART_IP_RXWM;
+}
+
+int eos_uart_putc(int c, char b) {
+ if (b) {
+ while (UART0_REG(UART_REG_TXFIFO) & 0x80000000);
+ UART0_REG(UART_REG_TXFIFO) = c & 0xff;
+ } else {
+ if (UART0_REG(UART_REG_TXFIFO) & 0x80000000) return EOS_ERR_FULL;
+ UART0_REG(UART_REG_TXFIFO) = c & 0xff;
+ }
+ return EOS_OK;
+}
+
+int eos_uart_getc(char b) {
+ volatile uint32_t r;
+
+ if (b) {
+ while ((r = UART0_REG(UART_REG_RXFIFO)) & 0x80000000);
+ } else {
+ r = UART0_REG(UART_REG_RXFIFO);
+ if (r & 0x80000000) return EOS_ERR_EMPTY;
+ }
+ return r & 0xff;
+} \ No newline at end of file