Add MPU60x0 IMU sensors support for ESP32C3

Add MPU60x0 IMU sensors support for ESP32C3

Signed-off-by: AuroraRAS <chplee@gmail.com>
This commit is contained in:
AuroraRAS 2023-03-10 02:32:21 +08:00 committed by Xiang Xiao
parent d8da8dcc44
commit 581e9ce80b
4 changed files with 190 additions and 0 deletions

View File

@ -0,0 +1,74 @@
/****************************************************************************
* boards/risc-v/esp32c3/common/include/esp32c3_board_mpu60x0_i2c.h
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
#ifndef __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_MPU60X0_H
#define __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_MPU60X0_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C"
{
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
/****************************************************************************
* Name: board_mpu60x0_initialize
*
* Description:
* Initialize and register the MPU60X0 IMU Sensor driver.
*
* Input Parameters:
* devno - The device number, used to build the device path as /dev/imuN
* busno - The I2C bus number
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
#ifdef CONFIG_SENSORS_MPU60X0
int board_mpu60x0_initialize(int devno, int busno);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* __BOARDS_RISCV_ESP32C3_COMMON_INCLUDE_ESP32C3_BOARD_MPU60X0_H */

View File

@ -88,6 +88,10 @@ ifeq ($(CONFIG_LCD_APA102),y)
CSRCS += esp32c3_board_apa102.c
endif
ifeq ($(CONFIG_MPU60X0_I2C),y)
CSRCS += esp32c3_board_mpu60x0_i2c.c
endif
DEPPATH += --dep-path src
VPATH += :src
CFLAGS += $(shell $(INCDIR) "$(CC)" $(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src)

View File

@ -0,0 +1,99 @@
/****************************************************************************
* boards/risc-v/esp32c3/common/src/esp32c3_board_mpu60x0_i2c.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <stdio.h>
#include <debug.h>
#include <nuttx/arch.h>
#include <nuttx/kmalloc.h>
#include <nuttx/sensors/mpu60x0.h>
#include <nuttx/i2c/i2c_master.h>
#include "esp32c3_i2c.h"
#include "esp32c3_board_mpu60x0_i2c.h"
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: board_mpu60x0_initialize
*
* Description:
* Initialize and register the MPU60x0 Sensor driver.
*
* Input Parameters:
* devno - The device number, used to build the device path as /dev/imuN
* busno - The I2C bus number
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure.
*
****************************************************************************/
int board_mpu60x0_initialize(int devno, int busno)
{
struct i2c_master_s *i2c;
char devpath[10];
FAR struct mpu_config_s *mpu_config;
int ret;
sninfo("Initializing MPU60X0!\n");
/* Initialize MPU60X0 */
i2c = esp32c3_i2cbus_initialize(busno);
if (i2c)
{
/* Then try to register the IMU sensor in I2C<busno> */
mpu_config = kmm_zalloc(sizeof(struct mpu_config_s));
if (mpu_config == NULL)
{
snerr("ERROR: Failed to allocate mpu60x0 driver\n");
ret = -ENODEV;
}
else
{
mpu_config->i2c = i2c;
mpu_config->addr = 0x68;
snprintf(devpath, sizeof(devpath), "/dev/imu%d", devno);
ret = mpu60x0_register(devpath, mpu_config);
if (ret < 0)
{
snerr("ERROR: Error registering MPU60X0 in I2C%d\n", busno);
}
}
}
else
{
ret = -ENODEV;
}
return ret;
}

View File

@ -53,6 +53,7 @@
#include "esp32c3_board_twai.h"
#include "esp32c3_board_wdt.h"
#include "esp32c3_board_wlan.h"
#include "esp32c3_board_mpu60x0_i2c.h"
#ifdef CONFIG_SPI
# include "esp32c3_spi.h"
@ -373,6 +374,18 @@ int esp32c3_bringup(void)
}
#endif
#ifdef CONFIG_MPU60X0_I2C
/* Try to register MPU60x0 device in I2C0 */
ret = board_mpu60x0_initialize(0, 0);
if (ret < 0)
{
syslog(LOG_ERR, "Failed to initialize MPU60x0 "
"Driver for I2C0: %d\n", ret);
}
#endif
/* If we got here then perhaps not all initialization was successful, but
* at least enough succeeded to bring-up NSH with perhaps reduced
* capabilities.