Question
[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);
}