diff options
author | root <root@lab2.panaceas.james.local> | 2014-11-02 18:17:44 +0000 |
---|---|---|
committer | root <root@lab2.panaceas.james.local> | 2014-11-02 18:17:44 +0000 |
commit | 12287ff0a55f929bf840dcb4780d3f77b862c434 (patch) | |
tree | 46632674f393249e7cd74eacd7a4da00ccec540f /app/usart.c | |
parent | 479e719a64d75374f00438498cf91ba2601a63f1 (diff) | |
download | stm32_usb_kvm-12287ff0a55f929bf840dcb4780d3f77b862c434.tar.gz stm32_usb_kvm-12287ff0a55f929bf840dcb4780d3f77b862c434.tar.bz2 stm32_usb_kvm-12287ff0a55f929bf840dcb4780d3f77b862c434.zip |
fish
Diffstat (limited to 'app/usart.c')
-rw-r--r-- | app/usart.c | 104 |
1 files changed, 104 insertions, 0 deletions
diff --git a/app/usart.c b/app/usart.c new file mode 100644 index 0000000..f510682 --- /dev/null +++ b/app/usart.c @@ -0,0 +1,104 @@ +#include "project.h" + +#define BUFFER_SIZE 256 + +static ring_t output_ring; +static uint8_t output_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); + + kvm_recv (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 (&output_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); + } + } + +} + +int +_write (int file, char *ptr, int len) +{ + int ret; + + if (file == 1) + { + ret = ring_write (&output_ring, (uint8_t *) ptr, len); + + if (ret < 0) + ret = -ret; + + USART_CR1 (USART1) |= USART_CR1_TXEIE; + return ret; + } + + errno = EIO; + return -1; +} + +void +usart_queue (uint8_t d) +{ + ring_write_byte (&output_ring, d); + USART_CR1 (USART1) |= USART_CR1_TXEIE; + +#if 0 + printf ("0x%02x ", d); + fflush (stdout); +#endif +} + + +void +usart_init (void) +{ + ring_init (&output_ring, output_ring_buf, sizeof (output_ring_buf)); + + /* Enable the USART1 interrupt. */ + nvic_enable_irq (NVIC_USART1_IRQ); + + /* Setup GPIO pin GPIO_USART1_RE_TX on GPIO port B for transmit. */ + gpio_set_mode (GPIOA, GPIO_MODE_OUTPUT_50_MHZ, + GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX); + + /* Setup GPIO pin GPIO_USART1_RE_RX on GPIO port B for receive. */ + gpio_set_mode (GPIOA, GPIO_MODE_INPUT, + GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX); + + /* Setup UART parameters. */ + usart_set_baudrate (USART1, 38400); + 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 the USART. */ + usart_enable (USART1); +} |