Brian Silverman | aa9183a | 2013-12-08 10:33:47 -0800 | [diff] [blame] | 1 | #include "cape/uart_common.h" |
| 2 | #include "cape/uart_common_private.h" |
| 3 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 4 | #include "cape/util.h" |
| 5 | |
Brian Silverman | aa9183a | 2013-12-08 10:33:47 -0800 | [diff] [blame] | 6 | #define FPCLK 60000000 |
| 7 | |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 8 | // The UART is on PA9 and PA10. |
Brian Silverman | aa9183a | 2013-12-08 10:33:47 -0800 | [diff] [blame] | 9 | void uart_common_configure(int baud) { |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 10 | 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 Silverman | 2df8441 | 2013-12-10 14:00:40 -0800 | [diff] [blame] | 13 | RCC->APB2ENR |= RCC_APB2ENR_USART1EN; |
Brian Silverman | 1b6fbd0 | 2013-12-12 18:08:47 -0800 | [diff] [blame] | 14 | |
Brian Silverman | aa9183a | 2013-12-08 10:33:47 -0800 | [diff] [blame] | 15 | // 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 | } |