2016-01-10 09:38 AM
Hello
I am trying configure STM32F4 DISCO to read LIS3DSH using HAL but I have problems on read WHO_AM_I (0x0F) with default value 0011 1111I generate the configuration in MX cube: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_8BIT; hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;//LOW; hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;//1EDGE; hspi1.Init.NSS = SPI_NSS_SOFT; hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;//4; hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB; hspi1.Init.TIMode = SPI_TIMODE_DISABLED; hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLED;//7; HAL_SPI_Init(&hspi1);} main(){ **** MX_GPIO_Init(); MX_SPI1_Init(); HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET); __HAL_SPI_ENABLE(&hspi1); write_accelerator_register(0x20, 0x17, &hspi1); write_accelerator_register(0x24, 0x00, &hspi1); uint8_t id; id=read_accelerator_register(0x0F, &hspi1); ****}void write_accelerator_register(uint8_t piRegister, uint8_t piData, SPI_HandleTypeDef* phSPI){ // Set SS to low to select the accelerator on the SPI interface. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); // Send register index which we want to write. HAL_SPI_Transmit(phSPI, &piRegister, 1, 100); HAL_SPI_Transmit(phSPI, &piData, 1, 100); HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET);}uint8_t read_accelerator_register(uint8_t piRegister, SPI_HandleTypeDef* phSPI){ uint8_t iResult = 0; // Set SS to low to select the accelerator on the SPI interface. HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET); // Set MSB to indicate a read operation. piRegister |= 0x80; HAL_SPI_Transmit(phSPI, &piRegister, 1, 100); HAL_SPI_Receive(phSPI, &iResult, 1, 100); HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_SET); return iResult;}Can you help me?Best regards(Sorry for the english)2016-01-12 05:14 AM
Hipeter.john,
For reading a data from the accelerometer, you can use the function:void ACCELERO_IO_Read(uint8_t *pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead)
It is implemented underSTM32Cube_FW_F4_V1.0\Drivers\BSP\STM32F4-Discovery\stm32f4_discovery.c and it usesSPIx_WriteRead() function which call the functionHAL_SPI_TransmitReceive().
So, as another alternative maybe it will be more efficient if you callHAL_SPI_TransmitReceive() instead ofboth functions HAL_SPI_Transmit() andHAL_SPI_Receive().
-Shahrzad-