cancel
Showing results for 
Search instead for 
Did you mean: 

using SPI, do we need to control the pins?

ulao
Associate III

I'm trying to set up the SPI5 for a test but see a lot of references online indicating the pins modes need to also be set. I figured the driver would do that. The init seem pretty straight forward.

but the transmit does not. Do I need to set my pin mode or is this enough?

 HAL_SPI_Transmit_DMA(&hspi5,data_to_send, sizeof(data_to_send));

     while(HAL_SPI_GetState(&hspi5) != HAL_SPI_STATE_READY);

 I do not see any activity on the wires.

init code generate is

static void MX_SPI5_Init(void)

{

 /* USER CODE BEGIN SPI5_Init 0 */

 /* USER CODE END SPI5_Init 0 */

 /* USER CODE BEGIN SPI5_Init 1 */

 /* USER CODE END SPI5_Init 1 */

 /* SPI5 parameter configuration*/

 hspi5.Instance = SPI5;

 hspi5.Init.Mode = SPI_MODE_MASTER;

 hspi5.Init.Direction = SPI_DIRECTION_2LINES;

 hspi5.Init.DataSize = SPI_DATASIZE_4BIT;

 hspi5.Init.CLKPolarity = SPI_POLARITY_LOW;

 hspi5.Init.CLKPhase = SPI_PHASE_1EDGE;

 hspi5.Init.NSS = SPI_NSS_SOFT;

 hspi5.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;

 hspi5.Init.FirstBit = SPI_FIRSTBIT_MSB;

 hspi5.Init.TIMode = SPI_TIMODE_DISABLE;

 hspi5.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;

 hspi5.Init.CRCPolynomial = 0x0;

 hspi5.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;

 hspi5.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;

 hspi5.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;

 hspi5.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;

 hspi5.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;

 hspi5.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;

 hspi5.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;

 hspi5.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;

 hspi5.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;

 hspi5.Init.IOSwap = SPI_IO_SWAP_DISABLE;

 if (HAL_SPI_Init(&hspi5) != HAL_OK)

 {

   Error_Handler();

 }

 /* USER CODE BEGIN SPI5_Init 2 */

 /* USER CODE END SPI5_Init 2 */

}

and MX_SPI5_Init was called at the end of the init list.

8 REPLIES 8
MasterT
Senior III

Yes, you do need to initialize GPIO.

Here is the code generated by STM32Cube:

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(hspi->Instance==SPI3)
  {
  /* USER CODE BEGIN SPI3_MspInit 0 */
 
  /* USER CODE END SPI3_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_SPI3_CLK_ENABLE();
  
    __HAL_RCC_GPIOA_CLK_ENABLE();
    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**SPI3 GPIO Configuration    
    PA15     ------> SPI3_NSS
    PC10     ------> SPI3_SCK
    PC11     ------> SPI3_MISO
    PC12     ------> SPI3_MOSI 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_15;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
 
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF6_SPI3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
 
    /* SPI3 DMA Init */
    /* SPI3_RX Init */
    hdma_spi3_rx.Instance = DMA1_Stream0;
    hdma_spi3_rx.Init.Channel = DMA_CHANNEL_0;
    hdma_spi3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    hdma_spi3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi3_rx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_spi3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_spi3_rx.Init.Mode = DMA_CIRCULAR;
    hdma_spi3_rx.Init.Priority = DMA_PRIORITY_HIGH;
    hdma_spi3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_spi3_rx) != HAL_OK)
    {
      Error_Handler();
    }
 
    __HAL_LINKDMA(hspi,hdmarx,hdma_spi3_rx);
 
    /* SPI3_TX Init */
    hdma_spi3_tx.Instance = DMA1_Stream5;
    hdma_spi3_tx.Init.Channel = DMA_CHANNEL_0;
    hdma_spi3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_spi3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_spi3_tx.Init.MemInc = DMA_MINC_ENABLE;
    hdma_spi3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_spi3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_spi3_tx.Init.Mode = DMA_CIRCULAR;
    hdma_spi3_tx.Init.Priority = DMA_PRIORITY_VERY_HIGH;
    hdma_spi3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&hdma_spi3_tx) != HAL_OK)
    {
      Error_Handler();
    }
 
    __HAL_LINKDMA(hspi,hdmatx,hdma_spi3_tx);
 
  /* USER CODE BEGIN SPI3_MspInit 1 */
 
  /* USER CODE END SPI3_MspInit 1 */
  }
 
}

S.Ma
Principal

This codd configure the peripheral IP, it does not tell where thr SPI internal signals are routed to which pins, as there are various options. Both SPI and GPIO registere should be cpnfigured. You can run your code, put a breakpoint in your while trnasmitting loop, and manually edit the gpio registers until the main loop sends data through the chosen pin, for example.

looks like that code is being hit, 5 in my case.

void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)

{

 GPIO_InitTypeDef GPIO_InitStruct = {0};

 RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

 if(hspi->Instance==SPI5)

 {

...

I assume I do not need to edit any of this code.

ulao
Associate III

not fully absorbing this. I most certainly can break my code at HAL_SPI_Transmit

I can see all of this

0693W00000WJsctQAD.png

ulao
Associate III

I have all the SPI wires on a scope and only the SPI5_NSS / USART7_CTS (PF6/PF9) shows a signal. 0693W00000WJsjaQAD.png

ulao
Associate III

It is also possible its my board I'm using. stm32h735ig

the manual shows

Figure 13. P2 STMod+ connector to hold the SPI5 but looking at the pins on the chip, they do not match up right. I find this board document really hard to follow. How can MOSI of SPI 5 be either PF9, PF7, or PE11 ?

I do not care what SPI I use but I need the BGA pins exposed on the board somewhere. anyone know of a page that shows where the pins go?

Also Pmod shows the same.

1 SPI5_NSS / USART7_CTS (PF6/PF9)

2 SPI5_MOSI / USART7_TX (PF9/PF7)

3 SPI5_MISO / USART7_RX (PF8/PF6)

4 SPI5_SCK / USART7_RTS (PF7/PF8)

this is the manual I use but its not very helpful in some respects.

file:///C:/Users/sean_32bvxh7/Downloads/um2679-discovery-kit-with-stm32h735ig-mcu-stmicroelectronics.pdf

ulao
Associate III

oh boy, starting to maybe get this...

So the line I see working is my out data, I know this because changing my data out matches (hard to read without a clock). and my MOSI is configure as PF9 and that happens to be the Pmod 1 (1 SPI5_NSS / USART7_CTS (PF6/PF9) ) Where I see data on the wires

The IOC says SPI5 clock is on PH6 but the board says it can ( or should be ) on PF7/8

So there must be a way to set SCK and MISO to PF pins, but how?

I also see uart7 is in use in my project, maybe this is a conflict?

ulao
Associate III

ah I was able to get it from the LCD connector.