Brian Silverman | df49fe3 | 2013-12-11 14:21:37 -0800 | [diff] [blame^] | 1 | #include "cape/uart_byte.h" |
| 2 | #include "cape/uart_common_private.h" |
| 3 | |
| 4 | #include <STM32F2XX.h> |
| 5 | |
| 6 | #define TIMEOUT_TIM TIM7 |
| 7 | |
| 8 | void uart_byte_configure(void) { |
| 9 | TIMEOUT_TIM->CR1 = TIM_CR1_UDIS; |
| 10 | } |
| 11 | |
| 12 | int 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 | |
| 28 | void uart_byte_send(uint8_t value) { |
| 29 | while ((UART->SR & USART_SR_TXE) == 0) {} |
| 30 | UART->DR = value; |
| 31 | } |