blob: 0cb7507fa58d396a82ab446de31bbbd594f6e809 [file] [log] [blame]
Brian Silvermanaa9183a2013-12-08 10:33:47 -08001#include "cape/uart_common.h"
2#include "cape/uart_common_private.h"
3
Brian Silverman1b6fbd02013-12-12 18:08:47 -08004#include "cape/util.h"
5
Brian Silvermaned030062013-12-20 21:03:47 -08006#define RCC_APB2ENR_UARTEN RCC_APB2ENR_USART1EN
7
Brian Silvermanaa9183a2013-12-08 10:33:47 -08008#define FPCLK 60000000
9
Brian Silverman1b6fbd02013-12-12 18:08:47 -080010// The UART is on PA9 and PA10.
Brian Silvermanaa9183a2013-12-08 10:33:47 -080011void uart_common_configure(int baud) {
Brian Silverman1b6fbd02013-12-12 18:08:47 -080012 gpio_setup_alt(GPIOA, 9, 7);
13 gpio_setup_alt(GPIOA, 10, 7);
Brian Silvermaned030062013-12-20 21:03:47 -080014 //GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR9; // we want to go FAST!
15 RCC->APB2ENR |= RCC_APB2ENR_UARTEN;
Brian Silverman1b6fbd02013-12-12 18:08:47 -080016
Brian Silvermaned030062013-12-20 21:03:47 -080017 // baud = 60MHz / kMultiplier * (whole_part + fraction / kMultiplier))
18 static const int kMultiplier = 16 /* 8 * (2 - OVER8) */;
19 // The divisor of FPCLK that we want (*2).
20 int divisor = FPCLK * 2 / baud;
21 // The whole-number part of the divisor.
22 int mantissa = divisor / kMultiplier / 2;
23 // The fractional part of the divisor (*2).
24 int fraction = divisor % (kMultiplier * 2);
25 UART->BRR = (mantissa << 4) | ((fraction + 1) / 2);
26 UART->CR1 =
27 //USART_CR1_M /* 9th bit for the parity */ |
28 //USART_CR1_PCE /* enable parity (even by default) */ |
29 USART_CR1_TE /* enable transmitter */ |
30 USART_CR1_RE /* enable receiver */;
31 UART->CR1 |= USART_CR1_UE; // enable it
Brian Silvermanaa9183a2013-12-08 10:33:47 -080032}