summaryrefslogtreecommitdiffstats
path: root/stm32/app/usart.c
diff options
context:
space:
mode:
Diffstat (limited to 'stm32/app/usart.c')
-rw-r--r--stm32/app/usart.c83
1 files changed, 71 insertions, 12 deletions
diff --git a/stm32/app/usart.c b/stm32/app/usart.c
index 6b44656..e48eff5 100644
--- a/stm32/app/usart.c
+++ b/stm32/app/usart.c
@@ -2,11 +2,11 @@
#define BUFFER_SIZE 512
-ring_t usart_rx_ring;
-static uint8_t usart_rx_ring_buf[BUFFER_SIZE];
+ring_t usart1_rx_ring;
+static uint8_t usart1_rx_ring_buf[BUFFER_SIZE];
-ring_t usart_tx_ring;
-static uint8_t usart_tx_ring_buf[BUFFER_SIZE];
+ring_t usart1_tx_ring;
+static uint8_t usart1_tx_ring_buf[BUFFER_SIZE];
void
usart1_isr (void)
@@ -18,13 +18,13 @@ usart1_isr (void)
((USART_SR (USART1) & USART_SR_RXNE) != 0)) {
/* Retrieve the data from the peripheral. */
data = usart_recv (USART1);
- ring_write_byte (&usart_rx_ring, data);
+ ring_write_byte (&usart1_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)) {
+ if (ring_read_byte (&usart1_tx_ring, &data)) {
/*No more data, Disable the TXE interrupt, it's no longer needed. */
USART_CR1 (USART1) &= ~USART_CR1_TXEIE;
} else
@@ -32,9 +32,10 @@ usart1_isr (void)
}
}
-void usart_kick (void)
+
+void usart1_kick (void)
{
- if (!ring_empty (&usart_tx_ring))
+ if (!ring_empty (&usart1_tx_ring))
USART_CR1 (USART1) |= USART_CR1_TXEIE;
}
@@ -45,8 +46,8 @@ _write (int file, char *ptr, int len)
int ret;
if (file == 1) {
- ret = ring_write (&usart_tx_ring, (uint8_t *) ptr, len);
- usart_kick();
+ ret = ring_write (&usart1_tx_ring, (uint8_t *) ptr, len);
+ usart1_kick();
ring_write (&cdcacm_tx_ring, (uint8_t *) ptr, len);
if (ret < 0)
@@ -59,14 +60,44 @@ _write (int file, char *ptr, int len)
return -1;
}
+#define TRANSACT_TIMEOUT (HZ/2)
+
+int usart_transact (uint32_t u, void *_b, size_t txl, size_t rxl)
+{
+ uint8_t *b = _b;
+ uint32_t then = DWT_CYCCNT;
+
+ while (USART_SR (u) & USART_SR_RXNE)
+ (void) usart_recv (u);
+
+
+ while (txl--) {
+ while (! (USART_SR (USART1) & USART_SR_TXE))
+ if ((DWT_CYCCNT - then) > TRANSACT_TIMEOUT) return -1;
+
+ usart_send (u, * (b++));
+ }
+
+ b = _b;
+
+ while (rxl--) {
+ while (! (USART_SR (USART1) & USART_SR_RXNE))
+ if ((DWT_CYCCNT - then) > TRANSACT_TIMEOUT) return -1;
+
+ * (b++) = usart_recv (u);
+ }
+
+ return 0;
+}
+
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));
+ ring_init (&usart1_rx_ring, usart1_rx_ring_buf, sizeof (usart1_rx_ring_buf));
+ ring_init (&usart1_tx_ring, usart1_tx_ring_buf, sizeof (usart1_tx_ring_buf));
/* Enable the USART1 interrupt. */
nvic_enable_irq (NVIC_USART1_IRQ);
@@ -85,4 +116,32 @@ usart_init (void)
USART_CR1 (USART1) |= USART_CR1_RXNEIE;
/* Finally enable USART1. */
usart_enable (USART1);
+
+
+ MAP_AF (USART2_TX);
+ MAP_AF_PU (USART2_RX);
+
+ /* Setup UART1 parameters. */
+ usart_set_baudrate (USART2, 115200);
+ usart_set_databits (USART2, 8);
+ usart_set_stopbits (USART2, USART_STOPBITS_1);
+ usart_set_parity (USART2, USART_PARITY_NONE);
+ usart_set_flow_control (USART2, USART_FLOWCONTROL_NONE);
+ usart_set_mode (USART2, USART_MODE_TX_RX);
+ usart_enable (USART2);
+
+ MAP_AF (USART3_TX);
+ MAP_AF_PU (USART3_RX);
+
+ /* Setup UART1 parameters. */
+ usart_set_baudrate (USART3, 115200);
+ usart_set_databits (USART3, 8);
+ usart_set_stopbits (USART3, USART_STOPBITS_1);
+ usart_set_parity (USART3, USART_PARITY_NONE);
+ usart_set_flow_control (USART3, USART_FLOWCONTROL_NONE);
+ usart_set_mode (USART3, USART_MODE_TX_RX);
+ usart_enable (USART3);
+
+
+
}