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-13 06:19 AM
Unfortunately, the markings for that sheet are not specified in the datasheet.
I would guess it's actually a L3G4200D chip based on the register value. Perhaps there are other differences in the registers which you could examine to find out.
2024-02-12 05:21 AM
Perhaps the chip is not what you think it is.
#define WHO_I_AM_L3G4200D 0xD3 /* for L3G4200D */
What hardware is this? Post a high resolution in focus picture of the chip.
2024-02-12 09:08 PM
Hi,
I am using STM32F411VET6 and also attaching Images of the controller.
Thanks,
2024-02-13 04:00 AM
Hi,
is the "CS" pin configured as an output pin? With PullUp?
Your Prescaler for SPI is 2 so you get a >16 MHz on the SPI.
Datasheet says max 10Mhz.
HTH
padawan
2024-02-13 04:18 AM
Hi,
Yes, i configured CS pin as an output with no PullUp,
here is my code snippet
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOE, GPIO_PIN_3, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_14, GPIO_PIN_RESET);
/*Configure GPIO pin : PE3 */
GPIO_InitStruct.Pin = GPIO_PIN_3;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
/*Configure GPIO pin : PD14 */
GPIO_InitStruct.Pin = GPIO_PIN_14;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
}
For SPI Configuration
static void MX_SPI1_Init(void)
{
/* 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_16;
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();
}
}
2024-02-13 06:19 AM
Unfortunately, the markings for that sheet are not specified in the datasheet.
I would guess it's actually a L3G4200D chip based on the register value. Perhaps there are other differences in the registers which you could examine to find out.
2024-02-13 06:47 AM
Use this button to properly post source code:
2024-02-13 07:17 AM - edited 2024-02-13 07:18 AM
Hi
if i look at
https://qyriad.github.io/arduino-l3g4200d/html/group__registers.html
is at the bottom of the page
"The chip ID constant value of REG_WHO_AM_I: 0xd3".
so the reading of your code is correct.
<quote of your code>
"Sensor is not connected Received data from register 0x0F: 0xD3"
</quote of your code>
hth
padawan
2024-02-13 07:59 PM
Thanks for guiding me, i will make sure to use this format in future.
2024-02-23 03:04 AM
Hi
If you feel a post has answered your question, please click "Accept as Solution".
padawan