blob: 03d4cdd1c2ea355cc7fb848d3e5ba7c7431fb450 [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 RCC->APB2ENR |= RCC_APB2ENR_UARTEN;
Brian Silverman1b6fbd02013-12-12 18:08:47 -080015
Brian Silvermaned030062013-12-20 21:03:47 -080016 // baud = 60MHz / kMultiplier * (whole_part + fraction / kMultiplier))
17 static const int kMultiplier = 16 /* 8 * (2 - OVER8) */;
18 // The divisor of FPCLK that we want (*2).
19 int divisor = FPCLK * 2 / baud;
20 // The whole-number part of the divisor.
21 int mantissa = divisor / kMultiplier / 2;
22 // The fractional part of the divisor (*2).
23 int fraction = divisor % (kMultiplier * 2);
24 UART->BRR = (mantissa << 4) | ((fraction + 1) / 2);
25 UART->CR1 =
26 //USART_CR1_M /* 9th bit for the parity */ |
27 //USART_CR1_PCE /* enable parity (even by default) */ |
Brian Silverman53f29182013-12-21 15:16:27 -080028 //USART_CR1_OVER8 /* support going faster */ |
29 0;
Brian Silvermaned030062013-12-20 21:03:47 -080030 UART->CR1 |= USART_CR1_UE; // enable it
Brian Silvermanaa9183a2013-12-08 10:33:47 -080031}