2023-04-12 12:32 PM
Hi. I'm new to st programming so I want to ask some questions. I'm trying to initialize an angle sensor TLE5012b on stm32g431 MCU but it seems that it has problems with writing and reeding registers. What I'm doing wrong?
//spi initialization
static void MX_SPI1_Init(void)
{
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_16BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_32;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
//hspi1.Init.CRCLength = SPI_CRC_LENGTH_16BIT;
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
//read function
uint16_t Read_TLI5012B (uint16_t Dataw)
{
uint16_t Data_tmp_1;
uint16_t Datar, Data_tmp;
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port, SPI_CSB_Pin, GPIO_PIN_RESET); // CS=0
/* COMMAND in Dataw*/
HAL_SPI_Transmit(&hspi1, (uint8_t *)(&Dataw ), sizeof(Dataw) / sizeof(uint16_t), 0xFF);
/* READ in Data 1 */
HAL_SPI_Receive(&hspi1, (uint8_t *)(&Data_tmp), 1, 0xFF);
/*SAFETY-WORD*/
HAL_SPI_Receive(&hspi1, (uint8_t *)(&Data_tmp_1), 1, 0xFF);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port, SPI_CSB_Pin, GPIO_PIN_SET); //CSQ = 1
return Data_tmp; //return Data 1
}
//write function
uint16_t Write_TLI5012B (uint16_t Dataw, uint16_t Register)
{
uint16_t Datar;
uint16_t Data_tmp;
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port, SPI_CSB_Pin, GPIO_PIN_RESET); //CSQ = 0
/* COMMAND in Dataw */
HAL_SPI_Transmit(&hspi1, (uint8_t *)(&Dataw ), sizeof(Dataw) / sizeof(uint16_t), 0xFF);
HAL_SPI_Receive(&hspi1, (uint8_t *)(&Datar), 1, 0xFF);
/* WRITE Data 1 in Register*/
HAL_SPI_Transmit(&hspi1, (uint8_t *)(&Register), sizeof(Register) / sizeof(uint16_t), 0xFF);
/*SAFETY-WORD*/
HAL_SPI_Receive(&hspi1, (uint8_t *)(&Data_tmp), 1, 0xFF);
HAL_GPIO_WritePin(SPI_CSB_GPIO_Port, SPI_CSB_Pin, GPIO_PIN_SET); //CSQ = 1
return Data_tmp; //return safeword
}