blob: 6bafdb6aeae7a6850e905dc9b5cddc5f5ad75df3 [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
Brian Silverman1b6fbd02013-12-12 18:08:47 -08007#define RCC_APB1ENR_TIMEOUT_TIMEN RCC_APB1ENR_TIM7EN
Brian Silvermandf49fe32013-12-11 14:21:37 -08008
9void uart_byte_configure(void) {
Brian Silverman1b6fbd02013-12-12 18:08:47 -080010 RCC->APB1ENR |= RCC_APB1ENR_TIMEOUT_TIMEN;
11
Brian Silvermandf49fe32013-12-11 14:21:37 -080012 TIMEOUT_TIM->CR1 = TIM_CR1_UDIS;
13}
14
15int 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
31void uart_byte_send(uint8_t value) {
32 while ((UART->SR & USART_SR_TXE) == 0) {}
33 UART->DR = value;
34}