2015-09-28 12:10 PM
I have been trying to get my code to work but I cannot get any output on PA9 (USART1_Tx) on my STM32f0discovery board. Any help would be greatly appreciated.
CODE ========================================================== void InitUART() { // RCC Setup RCC->CFGR = 0; RCC->CFGR3 &= 0xFFFFFFFC; RCC->APB2ENR |= (1<<14); RCC->AHBENR |= (1<<17); // GPIO Setup GPIOA->MODER &= 0xFFC3FFFF; GPIOA->MODER |= 0x00280000L; GPIOA->OSPEEDR = 0x003c0000; GPIOA->OTYPER = 0; GPIOA->PUPDR &= 0xFFC3FFFF; GPIOA->PUPDR|=0x00140000; // Pull up on both lines // USART Setup USART1->CR2 = 0; USART1->CR3 = 0; USART1->BRR = 69; // 8MHz clock, 115200 rate USART1->GTPR = 1; USART1->RTOR = 0; USART1->RQR = 0; USART1->CR1 = USART_CR1_RE | USART_CR1_TE | USART_CR1_UE; } void SendByte(unsigned char b) { while ((USART1->ISR & USART_ISR_TXE) == 0); USART1->TDR=b; while ((USART1->ISR & USART_ISR_TXE) == 0); } int main() { InitUART(); for(;;) { SendByte(0xF0); SendByte(0x55); for(i=0;i<50;i++) ; } } ============================================================== #usart-stm32f0512015-09-28 03:35 PM
The joys of register level coding, tried stepping through with a debugger?
You might want to switch the AHBENR writing around so you avoid the hazard of touching GPIOA immediately after you've enabled the clock rather than dwelling a few cycles.2015-09-29 06:51 AM
I stepped through slowly with the debugger (Keil) and there is no change. Both the Tx and Rx lines remain high permanently without any serial data.
2015-09-29 08:40 AM
And does the Peripheral view within Keil lead you to believe that the registers are correctly configured? And the other clocks are all in order? Do you need to configure the GPIO AF mux to USART1?
I'd wade into the register level settings, but I've better things to do. I've posted SPL examples for the F0 that should work.2015-09-29 10:11 AM
That was it! I omitted to specify the alternate function number:
GPIOA->AFR[1] &= 0xFFFFF00F; GPIOA->AFR[1] |= 0x00000110; Works fine now, thank you for pointing it out.