Serial MIDI output using HAL and a Nucleo F4
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