AnsweredAssumed Answered

USART RX - HOW IT WORKS ?

Question asked by prieur.jean on Apr 19, 2013
Latest reply on Apr 29, 2013 by lary.thomas
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 at 31250 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"
void USART3_rx(void);
 
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 (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);
}
 
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);
}

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...

Outcomes