cancel
Showing results for 
Search instead for 
Did you mean: 

Serial MIDI output using HAL and a Nucleo F4

Charlie Lesoine
Associate II
Posted on January 29, 2018 at 21:44

I'm trying to get a simple MIDI output over DIN (standard midi connector, not usb) example working using a Nucleo-F401RE and a Sparkfun MIDI shield (

https://www.sparkfun.com/products/12898

) to send a MIDI note to a MIDI/USB adapter but so far have not had any luck. No idea what part of it is wrong. I'm guessing some part of the configuration? Any help would be greatly appreciated. I've confirmed both the MIDI shield and MIDI/USB adapter is working correctly with an old Arduino UNO.

Here is my code: 

main.c

unsigned char

tx[] = {

0x90,

0x3C,

0x45

};

int

main(

void

)

{

 

HAL_Init();

 

SystemClock_Config();

  

MX_GPIO_Init();

 

MX_USART2_UART_Init();

 

while

(1)

 

{

 

HAL_UART_Transmit(&huart2, (

uint8_t

*)&tx, 3, 0);

 

HAL_Delay(500);

 

}

Generated by CubeMX:

main.c

/* USART2

init

function */

static

void

MX_USART2_UART_Init(

void

)

{

 

huart2.

Instance

= USART2;

 

huart2.

Init

.

BaudRate

= 31250;

 

huart2.

Init

.

WordLength

= UART_WORDLENGTH_8B;

 

huart2.

Init

.

StopBits

= UART_STOPBITS_1;

 

huart2.

Init

.

Parity

= UART_PARITY_NONE;

 

huart2.

Init

.

Mode

= UART_MODE_TX_RX;

 

huart2.

Init

.

HwFlowCtl

= UART_HWCONTROL_NONE;

 

huart2.

Init

.

OverSampling

= UART_OVERSAMPLING_16;

 

if

(HAL_UART_Init(&huart2) !=

HAL_OK

)

 

{

   

_Error_Handler(__FILE__, __LINE__);

 

}

}

stm32f4xx_hal_msp.c

void

HAL_UART_MspInit(

UART_HandleTypeDef

* huart)

{

 

GPIO_InitTypeDef

GPIO_InitStruct;

 

if

(huart->

Instance

==USART2)

 

{

 

/* USER CODE BEGIN USART2_MspInit 0 */

 

/* USER CODE END USART2_MspInit 0 */

   

/* Peripheral clock enable */

   

__HAL_RCC_USART2_CLK_ENABLE();

  

   

/**USART2 GPIO Configuration

   

   

PA2

   

------> USART2_TX

   

PA3

   

------> USART2_RX

   

*/

   

GPIO_InitStruct.

Pin

= USART_TX_Pin|USART_RX_Pin;

   

GPIO_InitStruct.

Mode

= GPIO_MODE_AF_PP;

   

GPIO_InitStruct.

Pull

= GPIO_NOPULL;

   

GPIO_InitStruct.

Speed

= GPIO_SPEED_FREQ_LOW;

   

GPIO_InitStruct.

Alternate

= GPIO_AF7_USART2;

   

HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 

/* USER CODE BEGIN USART2_MspInit 1 */

 

/* USER CODE END USART2_MspInit 1 */

 

}

}

#usart #nucleo #midi #music #synthesizer #cube-hal
6 REPLIES 6
T J
Lead
Posted on January 30, 2018 at 00:48

I use this to transmit by DMA, but the code should show you what you need to know;

    if (TxDMA1BufHasData) {

        char uartState = HAL_UART_GetState(&huart1);

        if ((uartState == HAL_UART_STATE_READY) || (uartState == HAL_UART_STATE_BUSY_RX)) {

            TxDMA1BufHasData = false;        // sending now

            if(HAL_UART_Transmit_DMA(&huart1, (uint8_t *)Usart1TxDMABuffer + U1TxBufferPtrOUT, U1TxBufferPtrIN - U1TxBufferPtrOUT) == HAL_OK) {

                U1TxBufferPtrOUT = U1TxBufferPtrIN;

                Usart1TxDMABuffer[U1TxBufferPtrIN] = 0 ;    // null not needed

            }

            else {

                _Error_Handler(__FILE__, __LINE__); /* Transfer error in transmission process */

            }

        }

    }
Posted on January 30, 2018 at 05:18

I see for the transmission data you are casting '(uint8_t *)' instead of to the address '(uint8_t *)&' which I tried but it still is not working. Other than that I'm not sure what part of your code can help me understand what I'm doing wrong.

Posted on January 30, 2018 at 06:53

it doesnt look like you need DMA, its all I have to offer..

Thats the runtime use of the code that works for me, other than that I have the same init code from the cube.

but how did you initialise the port inside the cube ?

To make it work like this, you need to configure the Usart to use the DMA, inside the cube.

0690X00000604K4QAI.jpg
Posted on January 31, 2018 at 00:31

Thanks. I will try this out. I do want to eventually transmit in DMA mode.

Posted on February 07, 2018 at 01:15

Why is the baud rate 23040 and not 31250 for MIDI?

Posted on February 07, 2018 at 01:22

I don't use MIDI , it started in the 80s, more than 35 years ago.