cancel
Showing results for 
Search instead for 
Did you mean: 

[SOLVED] STM32F746G-DISCO -- SPI connection to PGA2311---

I have the STM32F746G-DISCO and after some good result with I2C bus I would like to interface a PGA2311 with SPI.

This code hang the STM32 processor, why ?

#include "stm32f7xx_hal_spi.h"
 
SPI_HandleTypeDef spi = { .Instance = SPI2 };
 
void BSP_SPI_Init()
{
    __GPIOB_CLK_ENABLE();
    __GPIOI_CLK_ENABLE();
    __SPI1_CLK_ENABLE();
    
 
    spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
    spi.Init.Direction = SPI_DIRECTION_2LINES;
    spi.Init.CLKPhase = SPI_PHASE_1EDGE;
    spi.Init.CLKPolarity = SPI_POLARITY_LOW;
    spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
    spi.Init.DataSize = SPI_DATASIZE_8BIT;
    spi.Init.FirstBit = SPI_FIRSTBIT_LSB;
    spi.Init.NSS = SPI_NSS_HARD_OUTPUT;
    spi.Init.TIMode = SPI_TIMODE_DISABLED;
    spi.Init.Mode = SPI_MODE_MASTER; 
    if (HAL_SPI_Init(&spi) != HAL_OK)
    {
        __asm("bkpt 255");
    }
    
    GPIO_InitTypeDef  GPIO_InitStruct;
  
    GPIO_InitStruct.Pin       = GPIO_PIN_14 | GPIO_PIN_15;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    //GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; ??
 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
     
    GPIO_InitStruct.Pin       = GPIO_PIN_1;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    //GPIO_InitStruct.Alternate = GPIO_AF5_SPI1; ??
 
    HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); 
 
 
    // CS pin
 
   __HAL_RCC_GPIOA_CLK_ENABLE();
 
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
}
 
void BSP_SPI_Write(uint8_t Value)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
    HAL_SPI_Transmit(&spi, (uint8_t *)&Value, 1, HAL_MAX_DELAY);
    HAL_SPI_Transmit(&spi, (uint8_t *)&Value, 1, HAL_MAX_DELAY);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
}
 

3 REPLIES 3

Solved the hang but still not work

SPI_HandleTypeDef spi = { .Instance = SPI2 };
 
void BSP_SPI_Init()
{
    __GPIOB_CLK_ENABLE();
    __GPIOI_CLK_ENABLE();
    __SPI2_CLK_ENABLE();   <<<<<<< SPI2
    

Bob S
Principal

Define "does not work". What are you seeing, or NOT seeing? And you still have the "alternate" assignment commented out in your GPIO init code, so the SPI port won't be connected to those pins. Instead the pins will be connected to whatever random value is in that structure member. I don't recall who keeps suggesting this, maybe @Community member​  or @Community member​, but for REPEATABLE code, unless you are explicitly assigning values to EVERY structure member, declare your structure variable like this:

GPIO_InitTypeDef  GPIO_InitStruct = {0};

Then at least it will contain the same, known data every time you run the program, which makes it WAAAAAAY easier to debug things.

Solved all ok

SPI2_MOSI is SPI output

SPI2_SCK is SPi clock out

PA8 is the CS pin

void BSP_SPI_Init()
{
    __GPIOB_CLK_ENABLE();
    __GPIOI_CLK_ENABLE();
    __SPI2_CLK_ENABLE();
    
    spi.Instance               = SPI2;
    spi.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
    spi.Init.Direction = SPI_DIRECTION_2LINES;
    spi.Init.CLKPhase = SPI_PHASE_1EDGE;
    spi.Init.CLKPolarity = SPI_POLARITY_LOW;
    spi.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;
    spi.Init.CRCPolynomial     = 7;
    spi.Init.DataSize = SPI_DATASIZE_8BIT;
    //spi.Init.FirstBit = SPI_FIRSTBIT_LSB;
    spi.Init.FirstBit = SPI_FIRSTBIT_MSB;
    //spi.Init.NSS = SPI_NSS_HARD_OUTPUT;
    spi.Init.NSS = SPI_NSS_SOFT;
    spi.Init.TIMode = SPI_TIMODE_DISABLED;
    spi.Init.Mode = SPI_MODE_MASTER; 
 
    if (HAL_SPI_Init(&spi) != HAL_OK)
    {
        __asm("bkpt 255");
    }
    
    GPIO_InitTypeDef  GPIO_InitStruct = {0};
  
    GPIO_InitStruct.Pin       = GPIO_PIN_14 | GPIO_PIN_15;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; 
 
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
     
    GPIO_InitStruct.Pin       = GPIO_PIN_1;
    GPIO_InitStruct.Mode      = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull      = GPIO_PULLUP;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF5_SPI2; 
 
    HAL_GPIO_Init(GPIOI, &GPIO_InitStruct); 
 
 
    // CS pin
 
   __HAL_RCC_GPIOA_CLK_ENABLE();
 
    GPIO_InitStruct.Pin = GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
}
 
void BSP_SPI_Write(uint8_t Value)
{
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_RESET);
    HAL_SPI_Transmit(&spi, (uint8_t *)&Value, 1, HAL_MAX_DELAY);
    HAL_SPI_Transmit(&spi, (uint8_t *)&Value, 1, HAL_MAX_DELAY);
    HAL_GPIO_WritePin(GPIOA, GPIO_PIN_8, GPIO_PIN_SET);
}