2024-09-26 10:14 AM
I'm having difficulty writing to STGAP1BS configuration registers.
I'm using a Nucleo -G474RE to communicate with STGAP1BS chip on a custom board through SPI bus.
Below I'm trying to configure CFG2 with the value 0xA0 but when I read back the register I get the default value of 0x04.
The STGAP1BS uses a 16-bit SPI, below is the setup for 8-bit SPI on the Nucleo board but I did try 16-bit data size as well although I might not be implementing it correctly.
SPI Init: (generated by STM32CubeIDE)
/* SPI2 parameter configuration*/
hspi2.Instance = SPI2;
hspi2.Init.Mode = SPI_MODE_MASTER;
hspi2.Init.Direction = SPI_DIRECTION_2LINES;
hspi2.Init.DataSize = SPI_DATASIZE_8BIT;
hspi2.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi2.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi2.Init.NSS = SPI_NSS_SOFT;
hspi2.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi2.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi2.Init.TIMode = SPI_TIMODE_DISABLE;
hspi2.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi2.Init.CRCPolynomial = 7;
hspi2.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi2.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
if (HAL_SPI_Init(&hspi2) != HAL_OK)
{
Error_Handler();
}
Commands:
uint8_t StartCFG[2] = {0x2A, 0xDA};
uint8_t StopCFG[2] = {0x3A, 0xAA};
uint8_t ResetStatus[2] = {0xD0, 0x32};
uint8_t wCFG1[4] = {0x8C, 0xA1, 0x00, 0x00};
uint8_t CFG1[2] = {0xAC, 0x41};
uint8_t wCFG2[4] = {0x9D, 0x00, 0xA0, 0x00};
uint8_t CFG2[2] = {0xBD, 0x36};
Write to registers: This is a snippet, I configure all registers before the exit config command.
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_2, GPIO_PIN_RESET); //set SD pin low
delay_us(5);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET); //set CS pin low
SPIerror = HAL_SPI_Transmit(&hspi2, (uint8_t *)&ResetStatus, 2, 100); //Clear status registers
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); //set CS pin high
delay_us(80); //min required delay for reset status = 50us
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET); //set CS pin low
SPIerror = HAL_SPI_Transmit(&hspi2, (uint8_t *)&StartCFG, 2, 100); //Enter configuration mode
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); //set CS pin high
delay_us(40); //min required delay for start config = 22us
// ***** Write to configuration registers *****
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET); //set CS pin low
SPIerror = HAL_SPI_Transmit(&hspi2, (uint8_t *)&wCFG1, 4, 100); //Configure register 1
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); //set CS pin high
delay_us(1); //min required delay = 700ns
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET); //set CS pin low
SPIerror = HAL_SPI_Transmit(&hspi2, (uint8_t *)&wCFG2, 4, 100); //Configure register 2
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); //set CS pin high
delay_us(1); //min required delay = 700ns
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET); //set CS pin low
HAL_SPI_Transmit(&hspi2, (uint8_t *)&StopCFG, 2, 100); //Exit configuration mode
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET); //set CS pin high
delay_us(10); //min required delay for stop config = 5us
Read registers:
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
HAL_SPI_Transmit(&hspi2, (uint8_t *)&CFG2, 2, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
delay_us(50); //min required delay for reading remote register = 30us
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_RESET);
SPIerror = HAL_SPI_TransmitReceive(&hspi2, (uint8_t *)&spi_buf, (uint8_t *)&spi_buf, 8, 100);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_11, GPIO_PIN_SET);
STGAP1BS datasheet:
https://www.st.com/resource/en/datasheet/stgap1bs.pdf
Another issue is I'm getting the register value as the 1st byte of the reply but according to the datasheet it should be the 3rd byte in the reply. It's consistent when I tried reading all the other registers.
Below is an oscilloscope screenshot. It shows the CLK(yellow), MISO(blue), and MOSI(purple). I did confirm both CS and SD lines are being set correctly.