2013-04-19 07:43 AM
Hello,
I'm quite a newbie with USART RX, I read a lot of code examples but I don't exactly understand how it works. I want to receive MIDI data from a musical instrument. MIDI is a protocol using USART communication at31250 bauds. I already connected the MIDI connector to the USART RX pin PD9 (USART3) via an optocoupler and I can see with an oscilloscope on PD9 that the frame is OK. This is the code I wrote:#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h'' #include ''stm32f4xx_usart.h'' voidUSART3_rx(
void);
intmain(
void)
{ // INIT THE GREEN LED OF THE STM32F4DISCOVERY GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); USART3_rx(); while(1)
{ // Waiting for a data in USART3 peripheral while(USART_ReceiveData(USART3) != 0xFF);
// Enable the green led GPIO_SetBits(GPIOD, GPIO_Pin_12); } return(1);
} voidUSART3_rx(
void)
{ USART_InitTypeDef USART_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOD, &GPIO_InitStructure); /* Connect USART pins to AF */ GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); USART_InitStructure.USART_BaudRate = 31250; // BaudRate of the MIDI protocolUSART_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_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); } The green led should turn as soon as a MIDI frame is received. But the program stay at while (USART_ReceiveData(USART3) != 0xFF); Do you have any idea of what append ? I'm quite sure that the problem does not came from the MIDI frame send by my instrument...2013-04-28 04:49 PM
Hi Prieur Jean.
here is your corrected code..: cheers Lary.#include ''stm32f4xx_rcc.h''
#include ''stm32f4xx_gpio.h''
#include ''stm32f4xx_usart.h''
void USART3_rx(void);
uint8_t Usart3Get(void)
{
while ( USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET);
return (uint8_t)USART_ReceiveData(USART3);
}
int main(void)
{
/* INIT THE GREEN LED OF THE STM32F4DISCOVERY*/
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
USART3_rx();
while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE)== RESET);
if( USART_ReceiveData(USART3)!= 0xFF)
{
GPIO_SetBits(GPIOD,GPIO_Pin_12);
}
while(1)
{
}
}
void USART3_rx(void)
{
USART_InitTypeDef USART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* Connect USART pins to AF */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3);
USART_InitStructure.USART_BaudRate = 31250; // BaudRate of the MIDI protocol
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_Init(USART3, &USART_InitStructure);
USART_Cmd(USART3, ENABLE);
}