Issues with SPI using HAL
Hi,
I'm new to using HAL, and I'm having several issues with setting up the SPI.
I'm using SPI 1 on an STM32F429ZGT6, and using KEIL as my IDE.
I've just tried initializing SPI 1 by configuring GPIOA Pins 5, 6 and 7 for their SPI Alternate functions.
I set up GPIOE Pins 0, 1, 2 and 3 for a manual chip select, and I'm writing to them for an idle high (initializing them as pullup doesn't seem to work).
My project builds and loads onto the board, but the MOSI pin is silent, and putting the scope on the clock pin, the clock line is pulled up once for a few milliseconds, comes back down, and remains that way.
I will also note that the clock idles low, even though I've set the clock polarity to high on my SPI initiailization.
Is there something I'm missing? I don't understand why the SPI lines are acting this way.
SPI_HandleTypeDef SPI_1;
void SPI_INIT(void)
{
__HAL_RCC_SPI1_CLK_ENABLE();
SPI_1.Instance = SPI1;
SPI_1.Init.Mode = SPI_MODE_MASTER;
SPI_1.Init.Direction = SPI_DIRECTION_2LINES;
SPI_1.Init.DataSize = SPI_DATASIZE_8BIT;
SPI_1.Init.CLKPolarity = SPI_POLARITY_HIGH;
SPI_1.Init.CLKPhase = SPI_PHASE_2EDGE;
SPI_1.Init.NSS = SPI_NSS_SOFT;
SPI_1.Init.FirstBit = SPI_FIRSTBIT_MSB;
SPI_1.Init.TIMode = SPI_TIMODE_DISABLE;
SPI_1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
SPI_1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_4;
HAL_SPI_Init(&SPI_1);
}
static void ConfigGPIOA(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitStruct.Pin = GPIO_PIN_5;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
}
static void ConfigGPIOE(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
__HAL_RCC_GPIOE_CLK_ENABLE();
GPIO_InitStruct.Pin = SPI_CS02_Pin|SPI_CS03_Pin|SPI_CS00_Pin|SPI_CS01_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLUP;
GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
//Manually raise all SPI Chip select pins
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_0, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_2, GPIO_PIN_SET);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);
}
int main(void)
{
HAL_Init();
SystemClock_Config();
// GPIO Init does the initialization for both GPIOA and GPIOE using the functions above
GPIOInit();
SPI_INIT();
for(spibyte=0; spibyte < 4; spibyte++)
{
// Macro for activating Chip Select Line
CS_LOW();
if(spibyte == 0)
{
HAL_SPI_Transmit(&SPI_1, (uint8_t *)0x51, 2, 10);
}
if(spibyte >= 1)
{
HAL_SPI_Transmit(&SPI_1, (uint8_t *)0xFF, 2, 10);
}
_DELAY(TDISCS);
//Macro for deactivating Chip Select Line
CS_HIGH();
}Update:
Changed the main loop
for(spibyte=0; spibyte < 4; spibyte++)
{
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_RESET);
if(spibyte == 0)
{
txBuffer[0] = 0x51;
HAL_SPI_Transmit(&SPI_1, (uint8_t *)txBuffer, 1, 1000);
}
if(spibyte >= 1)
{
txBuffer[0] = 0xFF;
HAL_SPI_Transmit(&SPI_1, (uint8_t *)txBuffer, 1, 1000);
}
_DELAY(TDISCS);
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_1, GPIO_PIN_SET);
}And here's the RCC Config
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
HAL_RCC_OscConfig(&RCC_OscInitStruct);
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
return;
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
return;
}
}Using 8MHz external oscillator
