2019-03-07 08:42 PM
Hello everyone.
I am beginner in programming, and I was not able to find working example for USART 3,4,5, and 6 for STM32F030CC. Until now I was just using USART1 and 2 to send messages and receive message but while i tried the same configuration for the usart 3,4,5,and 6 i cant able to receive any message at the tx end in.This is my configuration and and interrupt handler program. Can someone please help? me to solve the problem
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
/* Configure USART1 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9 | GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART1 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
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_Cmd(USART1,ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
}
void USART2_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
/* Configure USART2 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART2 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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(USART2, &USART_InitStructure);
USART_Cmd(USART2,ENABLE);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
}
void USART3_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource10, GPIO_AF_4);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource11, GPIO_AF_4);
/* Configure USART3 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable USART3 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART3_6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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(USART3, &USART_InitStructure);
USART_Cmd(USART3,ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC);
USART_ITConfig(USART3, USART_IT_TXE, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
}
void USART4_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART4,ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource0, GPIO_AF_0);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource1, GPIO_AF_0);
/* Configure USART4 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Enable USART4 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART3_6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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(USART4, &USART_InitStructure);
USART_Cmd(USART4,ENABLE);
USART_ITConfig(USART4, USART_IT_TXE, ENABLE);
USART_ITConfig(USART4, USART_IT_RXNE, ENABLE);
}
void USART5_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART5,ENABLE);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource3, GPIO_AF_1);
GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_1);
/* Configure USART5 pins: Rx and Tx ----------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* Enable USART5 IRQ */
NVIC_InitStructure.NVIC_IRQChannel = USART3_6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
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(USART5, &USART_InitStructure);
USART_Cmd(USART5,ENABLE);
USART_ITConfig(USART5, USART_IT_TXE, ENABLE);
USART_ITConfig(USART5, USART_IT_RXNE, ENABLE);
}
void USART1_IRQHandler(void)
{
uint8_t ch;
if (USART_GetITStatus(USART1, USART_IT_RXNE))
{
ch = USART1->RDR;
rxbuf1[rxptrh1++] = ch;
}
if (USART_GetITStatus(USART1, USART_IT_TXE)) {
if (txlen1) {
txlen1--;
USART1->TDR = *txptr1++;
}
if (!txlen1) {
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}
void USART2_IRQHandler(void)
{
uint8_t ch;
if (USART_GetITStatus(USART2, USART_IT_RXNE)) {
ch = USART2->RDR;
rxbuf2[rxptrh2++] = ch;
}
if (USART_GetITStatus(USART2, USART_IT_TXE)) {
if (txlen2) {
txlen2--;
USART2->TDR = *txptr2++;
}
if (!txlen2) {
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
}
void USART3_6_IRQHandler(void)
{
uint8_t ch;
if (USART_GetITStatus(USART3, USART_IT_RXNE))
{
ch = USART3->RDR;
rxbuf3[rxptrh3++] = ch;
}
if (USART_GetITStatus(USART3, USART_IT_TXE)) {
if (txlen3) {
txlen3--;
USART3->TDR = *txptr3++;
}
if (!txlen3) {
USART_ITConfig(USART3, USART_IT_TXE, DISABLE);
}
}
///////////
if (USART_GetITStatus(USART4, USART_IT_RXNE)) {
ch = USART4->RDR;
rxbuf4[rxptrh4++] = ch;
}
if (USART_GetITStatus(USART4, USART_IT_TXE)) {
if (txlen4) {
txlen4--;
USART4->TDR = *txptr4++;
}
if (!txlen4) {
USART_ITConfig(USART4, USART_IT_TXE, DISABLE);
}
}
if (USART_GetITStatus(USART6, USART_IT_RXNE)) {
ch = USART6->RDR;
rxbuf6[rxptrh6++] = ch;
}
}
2019-03-07 11:22 PM
I didn't work much with F030, to be honest.
Adding another UART is a bit more than copy/paste/edit, unfortunately.
There are some things you need to check, probably in the reference manual:
> RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
> RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
Is USART 3 really on APB1 ?
> GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
I always used the push-pull setting, which worked fine.
Even if you configured no error interrupts, check the regarding flags in your handler, and clear if necessary. This error conditions prevent normal reception/transmission.
And check the schematics of your board if the pins you intent to use are really free/usable for UART purposes.
2019-03-08 01:56 AM
Thanks for your response
I have checked the clock configuration in reference manual it uses APB1 only
and in this GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
using the same only
Using the same configuration and setup USART 1 and 2 is working only issue is usart 3,4,5 and 6 is not working(not working in the sense no data is transmitting and receiving ).