cancel
Showing results for 
Search instead for 
Did you mean: 

Midi over SPI on STM32F030

deep_rune
Associate III

I started a project with MIDI, using the SPI peripheral of a STM32F030. I'm no longer sure I can get this peripheral to do what I need it to. I'm using STM32cubeIDE with the setup through the pinout configuration.

Midi signals are split into three bytes with a lead bit(0) and a trailing bit (1). Ordinarily the output is meant to sit high, followed by a 0,<byte>1, sit high. I am operating the SPI with only the MOSI line at a baud rate of 31.25kHz.

The SPI doesn't seem to have any option about what level it stays at between transmitted bytes of data. As I change things, sometimes it stays high and sometimes it stays low. Plus it seems that the first bit of data is elongated to two bits - I have no idea why this is. I can only observe it when the first bit is a 1, as otherwise I can't see it with my oscilloscope, I'm assuming it's the same if it's 0. I can't find any reference to this anywhere.

Furthermore, the data length doesn't seem to work how I would expect. As the midi message needs a 10 bit message including the trailing bits, I would have expected that i need to set the data length to 10 bits and store the values as a uint16_t array, but the HAL command HAL_SPI_Transmit (SPI_HandleTypeDef * hspi, uint8_t * pData, uint16_t Size, uint32_t Timeout) seems to indicate that the data needs to be uint8_t (uint8_t * pData) and when I try to change it it doesn't do what I expect.

Can anyone give me advice / answers? I'm very confused

static void MX_SPI1_Init(void)
{
  hspi1.Instance = SPI1;
  hspi1.Init.Mode = SPI_MODE_MASTER;
  hspi1.Init.Direction = SPI_DIRECTION_2LINES;
  hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
  hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
  hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
  hspi1.Init.NSS = SPI_NSS_SOFT;
  hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
  hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
  hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
  hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
  hspi1.Init.CRCPolynomial = 7;
  hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
  hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
  if (HAL_SPI_Init(&hspi1) != HAL_OK)
  {
    Error_Handler();
  }
}

2 REPLIES 2

MIDI is UART.

JW

deep_rune
Associate III

oh nuts