summaryrefslogtreecommitdiffstats
path: root/app/usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'app/usart.c')
-rw-r--r--app/usart.c100
1 files changed, 100 insertions, 0 deletions
diff --git a/app/usart.c b/app/usart.c
new file mode 100644
index 0000000..9b2a454
--- /dev/null
+++ b/app/usart.c
@@ -0,0 +1,100 @@
+#include "project.h"
+
+#define BUFFER_SIZE 256
+#define BIG_BUFFER_SIZE 600
+
+volatile ring_t rx6_ring;
+static uint8_t rx6_ring_buf[BUFFER_SIZE];
+
+volatile ring_t tx6_ring;
+static uint8_t tx6_ring_buf[BUFFER_SIZE];
+
+
+#define TX6 GPIO6
+#define TX6_PORT GPIOC
+
+#define RX6 GPIO7
+#define RX6_PORT GPIOC
+
+
+void usart6_isr (void)
+{
+ uint8_t data;
+
+ /* Check if we were called because of RXNE. */
+ if (((USART_CR1 (USART6) & USART_CR1_RXNEIE) != 0) &&
+ ((USART_SR (USART6) & USART_SR_RXNE) != 0)) {
+
+ /* Retrieve the data from the peripheral. */
+ data = usart_recv (USART6);
+
+ ring_write_byte (&rx6_ring, data);
+ }
+
+ /* Check if we were called because of TXE. */
+ if (((USART_CR1 (USART6) & USART_CR1_TXEIE) != 0) &&
+ ((USART_SR (USART6) & USART_SR_TXE) != 0)) {
+
+ if (ring_read_byte (&tx6_ring, &data)) {
+ /*No more data, Disable the TXE interrupt, it's no longer needed. */
+ usart_disable_tx_interrupt (USART6);
+ } else
+ usart_send_blocking (USART6, data);
+ }
+
+}
+
+void
+usart6_queue (uint8_t d)
+{
+ ring_write_byte (&tx6_ring, d);
+ usart_enable_tx_interrupt (USART6);
+}
+
+void
+usart6_drain (void)
+{
+ while (!ring_empty (&tx6_ring));
+}
+
+
+int
+usart6_write (char *ptr, int len, int blocking)
+{
+ int ret;
+
+ ret = ring_write (&tx6_ring, (uint8_t *) ptr, len, blocking);
+ usart_enable_tx_interrupt (USART6);
+ return ret;
+}
+
+
+void
+usart_init (void)
+{
+
+ ring_init (&rx6_ring, rx6_ring_buf, sizeof (rx6_ring_buf));
+ ring_init (&tx6_ring, tx6_ring_buf, sizeof (tx6_ring_buf));
+
+ /* Map pins, and set usart2 to have pull ups */
+
+ MAP_AF (TX6, GPIO_AF8);
+ MAP_AF_PU (RX6, GPIO_AF8);
+
+ usart_set_baudrate (USART6, 38400);
+ usart_set_databits (USART6, 8);
+ usart_set_stopbits (USART6, USART_STOPBITS_1);
+ usart_set_parity (USART6, USART_PARITY_NONE);
+ usart_set_flow_control (USART6, USART_FLOWCONTROL_NONE);
+ usart_set_mode (USART6, USART_MODE_TX_RX);
+
+
+ usart_enable_rx_interrupt (USART6);
+
+ /* Finally enable the USARTs. */
+ usart_enable (USART6);
+
+ nvic_enable_irq (NVIC_USART6_IRQ);
+
+
+}