blob: 76297e36a8135b756f8f12c3c893a33a0328bee1 [file] [log] [blame]
Brian Silvermandf49fe32013-12-11 14:21:37 -08001#include "cape/uart_byte.h"
2#include "cape/uart_common_private.h"
3
4#include <STM32F2XX.h>
5
6#define TIMEOUT_TIM TIM7
7
8void uart_byte_configure(void) {
9 TIMEOUT_TIM->CR1 = TIM_CR1_UDIS;
10}
11
12int uart_byte_receive(uint16_t timeout_count, uint16_t timeout_divider) {
13 TIMEOUT_TIM->PSC = timeout_divider;
14 TIMEOUT_TIM->EGR = TIM_EGR_UG;
15 TIMEOUT_TIM->CR1 |= TIM_CR1_CEN;
16
17 while ((UART->SR & USART_SR_RXNE) == 0) {
18 if (TIMEOUT_TIM->CNT >= timeout_count) {
19 TIMEOUT_TIM->CR1 &= ~TIM_CR1_CEN;
20 return -1;
21 }
22 }
23
24 TIMEOUT_TIM->CR1 &= ~TIM_CR1_CEN;
25 return UART->DR;
26}
27
28void uart_byte_send(uint8_t value) {
29 while ((UART->SR & USART_SR_TXE) == 0) {}
30 UART->DR = value;
31}