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 |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 7 | #define RCC_APB1ENR_TIMEOUT_TIMEN RCC_APB1ENR_TIM7EN |
Brian Silverman | df49fe3 | 2013-12-11 14:21:37 -0800 | [diff] [blame] | 8 | |
| 9 | void uart_byte_configure(void) { |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 10 | RCC->APB1ENR |= RCC_APB1ENR_TIMEOUT_TIMEN; |
| 11 | |
Brian Silverman | df49fe3 | 2013-12-11 14:21:37 -0800 | [diff] [blame] | 12 | TIMEOUT_TIM->CR1 = TIM_CR1_UDIS; |
| 13 | } |
| 14 | |
| 15 | int uart_byte_receive(uint16_t timeout_count, uint16_t timeout_divider) { |
| 16 | TIMEOUT_TIM->PSC = timeout_divider; |
| 17 | TIMEOUT_TIM->EGR = TIM_EGR_UG; |
| 18 | TIMEOUT_TIM->CR1 |= TIM_CR1_CEN; |
| 19 | |
| 20 | while ((UART->SR & USART_SR_RXNE) == 0) { |
| 21 | if (TIMEOUT_TIM->CNT >= timeout_count) { |
| 22 | TIMEOUT_TIM->CR1 &= ~TIM_CR1_CEN; |
| 23 | return -1; |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | TIMEOUT_TIM->CR1 &= ~TIM_CR1_CEN; |
| 28 | return UART->DR; |
| 29 | } |
| 30 | |
| 31 | void uart_byte_send(uint8_t value) { |
| 32 | while ((UART->SR & USART_SR_TXE) == 0) {} |
| 33 | UART->DR = value; |
| 34 | } |