AnsweredAssumed Answered

Don't working USART in STM32F051K8U6

Question asked by sklyarov.sergey on May 30, 2016
Latest reply on Jun 2, 2016 by Clive One
Hello. I'm use MCU STM32F051K8U6 for simple devices.
I'm had a problem when using the UART.
If i'm initialize USART thus (with SPL):
It freezes the microcontroller, and the program is not executed.

void GPIO_Initialization (void)
{
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);

  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_StructInit(&GPIO_InitStructure);

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  GPIO_InitStructure.GPIO_Pin = LCD44780_DATA | LCD44780_PIN_E | LCD44780_PIN_RS | GPIO_Pin_6 | GPIO_Pin_7;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

  GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_1);
  GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_1);

  /** PA2, PA9 -> TX UART */
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOD, &GPIO_InitStructure);

  /** PA3, PA10  -> RX UART. */
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_10;
  GPIO_Init(GPIOD, &GPIO_InitStructure);
}

void USART_Initialization(void)
{
  USART_InitTypeDef USART_InitStructure;
  USART_ClockInitTypeDef USART_ClockInitStructure;

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

  USART_InitStructure.USART_BaudRate = 115200;
  USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  USART_InitStructure.USART_StopBits = USART_StopBits_1;
  USART_InitStructure.USART_Parity = USART_Parity_No;
  USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  USART_Init(USART1, &USART_InitStructure);
  USART_Init(USART2, &USART_InitStructure);

  USART_ClockInitStructure.USART_Clock = USART_Clock_Enable;
  USART_ClockInitStructure.USART_CPHA = USART_CPHA_1Edge;
  USART_ClockInitStructure.USART_CPOL = USART_CPOL_High;
  USART_ClockInitStructure.USART_LastBit = USART_LastBit_Enable;
  USART_ClockInit(USART1, &USART_ClockInitStructure);
  USART_ClockInit(USART2, &USART_ClockInitStructure);

  USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
  USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);

  USART_Cmd(USART1, ENABLE);
  USART_Cmd(USART2, ENABLE);
}

void NVIC_Initialization(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;

  NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 4;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);

  NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPriority = 4;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

If initialize USART via direct written in the registers, the program works (at least do not crash).
void USART_Register_Initializiation(void)
{
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);

  USART1->BRR=0x1A0; //BaudRate 115200
  USART1->CR1 |= USART_CR1_UE; //Разрешаем работу USART1
  USART1->CR1 |= USART_CR1_TE; //Включаем передатчик

  USART2->BRR=0x1A0; //BaudRate 115200
  USART2->CR1 |= USART_CR1_UE; //Разрешаем работу USART2
  USART2->CR1 |= USART_CR1_TE; //Включаем передатчик
}

This bug SPL, or am I doing something wrong?

All source code this project (for Eclipse) is on GitHub.
https://github.com/kirra-cat/pwm_control/tree/master/firmware/control_driver/source

To one USART1 connected on One-Wire temperature sensor DS18B20, and to second USART2 connected USB-UART bridge on CP2102.
P.S. Оn the STM32F030F4P6 also don't worked USART (same problem).
--
With best regards, Sergey S. Sklyarov.
Saratov, Russian Federation


Outcomes