blob: 783c1543f2e75904746a6ba26454db9a08268d81 [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 Silvermanaa9183a2013-12-08 10:33:47 -08006#define FPCLK 60000000
7
Brian Silverman1b6fbd02013-12-12 18:08:47 -08008// The UART is on PA9 and PA10.
Brian Silvermanaa9183a2013-12-08 10:33:47 -08009void uart_common_configure(int baud) {
Brian Silverman1b6fbd02013-12-12 18:08:47 -080010 gpio_setup_alt(GPIOA, 9, 7);
11 gpio_setup_alt(GPIOA, 10, 7);
12 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR9; // we want to go FAST!
Brian Silverman2df84412013-12-10 14:00:40 -080013 RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
Brian Silverman1b6fbd02013-12-12 18:08:47 -080014
Brian Silvermanaa9183a2013-12-08 10:33:47 -080015 // baud = 60MHz / (8 * (2 - OVER8) * (mantissa / fraction))
16 int fraction = 8; // the biggest it can be with OVER8=0
17 int mantissa = FPCLK * (16 /* 8 * (2 - OVER8) */ / fraction) / baud;
18 UART->BRR = (mantissa << 4) | fraction;
19 UART->CR1 = USART_CR1_UE /* enable it */ |
20 USART_CR1_M /* 9th bit for the parity */ |
21 USART_CR1_PCE /* enable parity (even by default) */;
22}