2024-02-11 10:22 PM
Hi Everyone,
I am attempting to communicate with the L3GD20 sensor via SPI communication using the STM32F411VET Board.
Here is my code:
#include "main.h"
#include <stdio.h>
#include <string.h>
/* USER CODE BEGIN PD */
#define L3GD20_CS_PIN GPIO_PIN_3
#define L3GD20_CS_PORT GPIOE
#define L3GD20_WHO_AM_I 0x0F
#define L3GD20_I_AM 0xD4
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
static uint8_t TxData[2] = {0};
static uint8_t RxData[2] = {0};
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_SPI1_Init(void);
static void MX_USART2_UART_Init(void);
uint8_t L3GD20_Init(uint8_t regAddr);
int __io_putchar(int ch) {
// Transmit one character to the UART bit by bit
// Transmit one character to the UART
HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, HAL_MAX_DELAY);
return ch;
}
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_SPI1_Init();
MX_USART2_UART_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
HAL_GPIO_TogglePin(GPIOD, GPIO_PIN_13);
HAL_Delay(500);
uint8_t whoAmIValue = L3GD20_Init(L3GD20_WHO_AM_I); // Assuming WHO_AM_I register is at address 0x0F
if (whoAmIValue == L3GD20_I_AM) // Replace with the correct expected value for L2GD20
{
printf("Sensor is connected");
}
else
{
// WHO_AM_I value does not match, there might be a communication issue
// Handle the error, log a message, or take appropriate action
printf("Sensor is not connected");
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
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_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
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_0) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief SPI1 Initialization Function
* @param None
* @retval None
*/
static void MX_SPI1_Init(void)
{
/* USER CODE BEGIN SPI1_Init 0 */
/* USER CODE END SPI1_Init 0 */
/* USER CODE BEGIN SPI1_Init 1 */
/* USER CODE END SPI1_Init 1 */
/* SPI1 parameter configuration*/
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_LOW;
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_2;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN SPI1_Init 2 */
/* USER CODE END SPI1_Init 2 */
}
/* USER CODE BEGIN 4 */
uint8_t L3GD20_Init(uint8_t regAddr) {
uint8_t readCommand = regAddr | 0x80;
uint8_t receivedData;
// Select the L3GD20 device (if using hardware NSS)
HAL_GPIO_WritePin(L3GD20_CS_PORT, L3GD20_CS_PIN, GPIO_PIN_RESET);
HAL_Delay(50);
// Send command to read WHO_AM_I register
if (HAL_SPI_Transmit(&hspi1, &readCommand, 1, HAL_MAX_DELAY) != HAL_OK) {
Error_Handler();
}
// Receive data from WHO_AM_I register
if (HAL_SPI_Receive(&hspi1, &receivedData, 1, HAL_MAX_DELAY) != HAL_OK) {
Error_Handler();
}
// Deselect the L3GD20 device (if using hardware NSS)
HAL_GPIO_WritePin(L3GD20_CS_PORT, L3GD20_CS_PIN, GPIO_PIN_SET);
// Print received data
printf("Received data from register 0x%02X: 0x%02X\n", regAddr, receivedData);
return receivedData;
}
I want to start with this sensor but i am receiving
Sensor is not connected Received data from register 0x0F: 0xD3
Sensor is not connected Received data from register 0x0F: 0xD3
Sensor is not connected Received data from register 0x0F: 0xD3
Sensor is not connected Received data from register 0x0F: 0xD3
Can anyone help me identify where I may be wrong?
Solved! Go to Solution.
2024-02-23 03:16 AM
Hi,
Yes your suggestion helps me, as my sensor chip is different.