AnsweredAssumed Answered

Able to transmit the data but not receiving the data (USART3)

Question asked by Santosh Ghorpade on Jun 30, 2017

Hello,

 

I am working on the USART3 of NUCLEO-F413ZH. I am able to transmit the data but not receiving the data. Here is the Program

 

#include "stm32f4xx.h"
#include "misc.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"

uint16_t rec_data;

#define MAX_STRLEN 12 // this is the maximum string length of our string in characters
volatile char received_string[MAX_STRLEN+1]; // this will hold the recieved string
void init_USART3();
void USART_puts(USART_TypeDef* USARTx, volatile char *s);
void GPIO_config();
void EXTI_Config();


void main()
{
GPIO_config();
init_USART3();
EXTI_Config();
USART_puts(USART3, "Hello");
while(1)
{

}

}


void GPIO_config()
{
GPIO_InitTypeDef GPIO_InitStruct;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD , ENABLE);

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; // Pins 8 (TX) and 9 (RX) are used
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // the pins are configured as alternate function so the USART peripheral has access to them
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // this defines the IO speed and has nothing to do with the baudrate!
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // this defines the output type as push pull mode (as opposed to open drain)
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; // this activates the pullup resistors on the IO pins
GPIO_Init(GPIOD, &GPIO_InitStruct); // now all the values are passed to the GPIO_Init() function which sets the GPIO registers

GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_USART3);

}

void init_USART3()
{
USART_InitTypeDef USART_InitStruct; // this is for the USART3 initilization

RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);

USART_InitStruct.USART_BaudRate = 115200; // the baudrate is set to the value we passed into this init function
USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)
USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; // we want to enable the transmitter and the receiver
USART_Init(USART3, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting

USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); // enable the USART3 receive interrupt
USART_Cmd(USART3, ENABLE);
}

void USART_puts(USART_TypeDef* USARTx, volatile char *s)
{
while(*s)
{
// wait until data register is empty
while( !(USARTx->SR & 0x00000040) );
USART_SendData(USARTx, *s);
*s++;
}
}


void EXTI_Config()
{
NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)

NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; // we want to configure the USART3 interrupts
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;// this sets the priority group of the USART3 interrupts
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // this sets the subpriority inside the group
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; // the USART3 interrupts are globally enabled
NVIC_Init(&NVIC_InitStructure); // the properties are passed to the NVIC_Init function which takes care of the low level stuff

USART_Cmd(USART3, ENABLE);
}


void USART3_IRQHandler(void){


if( USART_GetITStatus(USART3, USART_IT_RXNE) )
{
static uint8_t cnt = 0;
char t = USART3->DR;

if( (t != '\n') && (cnt < MAX_STRLEN) ){
received_string[cnt] = t;
cnt++;
}
else{
cnt = 0;
USART_puts(USART3, received_string);
}
}
}

Outcomes