aboutsummaryrefslogtreecommitdiffstats
path: root/app/usart.c
diff options
context:
space:
mode:
authorroot <root@no.no.james.local>2017-06-13 21:10:37 +0100
committerroot <root@no.no.james.local>2017-06-13 21:10:37 +0100
commiteaf5d4799cc52e9dd1ebcaeafbf8f670658fea98 (patch)
treebbeb1993818c5fc8cbcf5a001080f246facc8de6 /app/usart.c
downloadserial_over_dp-eaf5d4799cc52e9dd1ebcaeafbf8f670658fea98.tar.gz
serial_over_dp-eaf5d4799cc52e9dd1ebcaeafbf8f670658fea98.tar.bz2
serial_over_dp-eaf5d4799cc52e9dd1ebcaeafbf8f670658fea98.zip
inital commit
Diffstat (limited to 'app/usart.c')
-rw-r--r--app/usart.c90
1 files changed, 90 insertions, 0 deletions
diff --git a/app/usart.c b/app/usart.c
new file mode 100644
index 0000000..ecd8e56
--- /dev/null
+++ b/app/usart.c
@@ -0,0 +1,90 @@
+#include "project.h"
+
+#define BUFFER_SIZE 512
+
+ring_t usart_rx_ring;
+static uint8_t usart_rx_ring_buf[BUFFER_SIZE];
+
+ring_t usart_tx_ring;
+static uint8_t usart_tx_ring_buf[BUFFER_SIZE];
+
+void
+usart1_isr (void)
+{
+ uint8_t data;
+
+ /* Check if we were called because of RXNE. */
+ if ( ( (USART_CR1 (USART1) & USART_CR1_RXNEIE) != 0) &&
+ ( (USART_SR (USART1) & USART_SR_RXNE) != 0)) {
+ /* Retrieve the data from the peripheral. */
+ data = usart_recv (USART1);
+ ring_write_byte (&usart_rx_ring, data);
+ }
+
+ /* Check if we were called because of TXE. */
+ if ( ( (USART_CR1 (USART1) & USART_CR1_TXEIE) != 0) &&
+ ( (USART_SR (USART1) & USART_SR_TXE) != 0)) {
+ if (ring_read_byte (&usart_tx_ring, &data)) {
+ /*No more data, Disable the TXE interrupt, it's no longer needed. */
+ USART_CR1 (USART1) &= ~USART_CR1_TXEIE;
+ } else {
+ usart_send (USART1, data);
+ }
+ }
+}
+
+void usart_kick (void)
+{
+ if (!ring_empty (&usart_tx_ring)) {
+ USART_CR1 (USART1) |= USART_CR1_TXEIE;
+ }
+}
+
+
+int
+_write (int file, char *ptr, int len)
+{
+ int ret;
+
+ if (file == 1) {
+ ret = ring_write (&usart_tx_ring, (uint8_t *) ptr, len);
+ usart_kick();
+
+ if (ret < 0) {
+ ret = -ret;
+ }
+
+ return ret;
+ }
+
+ errno = EIO;
+ return -1;
+}
+
+
+
+
+void
+usart_init (void)
+{
+ ring_init (&usart_rx_ring, usart_rx_ring_buf, sizeof (usart_rx_ring_buf));
+ ring_init (&usart_tx_ring, usart_tx_ring_buf, sizeof (usart_tx_ring_buf));
+ /* Enable the USART1 interrupt. */
+ nvic_enable_irq (NVIC_USART1_IRQ);
+ /* Map pins */
+ gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ,
+ GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
+ gpio_set_mode (GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT,
+ GPIO_USART1_RX);
+ /* Setup UART1 parameters. */
+ usart_set_baudrate (USART1, 115200);
+ usart_set_databits (USART1, 8);
+ usart_set_stopbits (USART1, USART_STOPBITS_1);
+ usart_set_parity (USART1, USART_PARITY_NONE);
+ usart_set_flow_control (USART1, USART_FLOWCONTROL_NONE);
+ usart_set_mode (USART1, USART_MODE_TX_RX);
+ /* Enable USART1 Receive interrupt. */
+ USART_CR1 (USART1) |= USART_CR1_RXNEIE;
+ /* Finally enable USART1. */
+ usart_enable (USART1);
+}