bool InitUart(int baudRate) { if (baudRate != 38400) return false; if ((RCC->APB1ENR & RCC_APB1ENR_USART2EN) == 0) { // not running yet ? RCC->APB1ENR |= RCC_APB1ENR_USART2EN; RCC->APB1RSTR |= RCC_APB1RSTR_USART2RST; RCC->APB1RSTR &= ~RCC_APB1RSTR_USART2RST; } USART2->CR1 = USART_CR1_RE | USART_CR1_TE; USART2->CR2 = 0; USART2->CR3 = 0; { //TODO - calculate BRR from apb1clock USART2->BRR = 0x03AA; } USART2->CR1 |= USART_CR1_UE; InitIOPort(GPIOD, 5, portALTOUT); // TX = PD5 InitIOPort(GPIOD, 6, portINPUT); // RX = PD6 { // AFIO->REMAP if ((RCC->APB2ENR & RCC_APB2ENR_AFIOEN) == 0) { // not running yet ? RCC->APB2ENR |= RCC_APB2ENR_AFIOEN; RCC->APB2RSTR |= RCC_APB2RSTR_AFIORST; RCC->APB2RSTR &= ~RCC_APB2RSTR_AFIORST; } AFIO->MAPR |= AFIO_MAPR_USART2_REMAP; } return true; }