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