2020-04-18 09:32 AM
I've a problem with UART on Bluepill (STM32F103C8T6). Let's say that I need to send some string via UART periodically using Std Peripheral Library . Below I post simple code which 'propably' should work. What I know for sure is that devices (bluepill and usb-uart converter) work fine (I've written similiar code but using HAL library and it worked in the same setting). Maybe the case is Clock Configuration? Thank You for any help...
#include "stm32f10x.h"
void USART_PutChar(char c)
{
while (!USART_GetFlagStatus(USART2, USART_FLAG_TXE));
USART_SendData(USART2, c);
}
void USART_PutString(char *s)
{
while (*s)
{
USART_PutChar(*s++);
}
}
int main(void)
{
GPIO_InitTypeDef GPIOInit;
USART_InitTypeDef USART_InitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
//LED
GPIO_InitTypeDef GPIO_InitDef;
GPIO_InitDef.GPIO_Pin = GPIO_Pin_13;
GPIO_InitDef.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitDef);
//PA2 Tx
GPIO_StructInit(&GPIOInit);
GPIOInit.GPIO_Pin = GPIO_Pin_2;
GPIOInit.GPIO_Speed = GPIO_Speed_50MHz;
GPIOInit.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIOInit);
//PA3 Rx
GPIOInit.GPIO_Pin = GPIO_Pin_3;
GPIOInit.GPIO_Speed = GPIO_Speed_50MHz;
GPIOInit.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIOInit);
// USART2
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStruct.USART_BaudRate = 9600;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStruct);
USART_Cmd(USART2, ENABLE);
while (1)
{
USART_PutString("Test\n");
}
}