From 581e9ce80b4f6b9a207c9c30713c0d7d602a0a3f Mon Sep 17 00:00:00 2001 From: AuroraRAS Date: Fri, 10 Mar 2023 02:32:21 +0800 Subject: [PATCH] Add MPU60x0 IMU sensors support for ESP32C3 Add MPU60x0 IMU sensors support for ESP32C3 Signed-off-by: AuroraRAS --- .../include/esp32c3_board_mpu60x0_i2c.h | 74 ++++++++++++++ boards/risc-v/esp32c3/common/src/Make.defs | 4 + .../common/src/esp32c3_board_mpu60x0_i2c.c | 99 +++++++++++++++++++ .../esp32c3-devkit/src/esp32c3_bringup.c | 13 +++ 4 files changed, 190 insertions(+) create mode 100644 boards/risc-v/esp32c3/common/include/esp32c3_board_mpu60x0_i2c.h create mode 100644 boards/risc-v/esp32c3/common/src/esp32c3_board_mpu60x0_i2c.c diff --git a/boards/risc-v/esp32c3/common/include/esp32c3_board_mpu60x0_i2c.h b/boards/risc-v/esp32c3/common/include/esp32c3_board_mpu60x0_i2c.h new file mode 100644 index 0000000000..ff7059bf4a --- /dev/null +++ b/boards/risc-v/esp32c3/common/include/esp32c3_board_mpu60x0_i2c.h @@ -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 + +/**************************************************************************** + * 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 */ diff --git a/boards/risc-v/esp32c3/common/src/Make.defs b/boards/risc-v/esp32c3/common/src/Make.defs index 78ca51f6fd..25fe787a03 100644 --- a/boards/risc-v/esp32c3/common/src/Make.defs +++ b/boards/risc-v/esp32c3/common/src/Make.defs @@ -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) diff --git a/boards/risc-v/esp32c3/common/src/esp32c3_board_mpu60x0_i2c.c b/boards/risc-v/esp32c3/common/src/esp32c3_board_mpu60x0_i2c.c new file mode 100644 index 0000000000..bf4f5534d1 --- /dev/null +++ b/boards/risc-v/esp32c3/common/src/esp32c3_board_mpu60x0_i2c.c @@ -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 + +#include +#include + +#include +#include +#include +#include + +#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 */ + + 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; +} + diff --git a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c index 3a6206cb24..dbb5708cf2 100644 --- a/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c +++ b/boards/risc-v/esp32c3/esp32c3-devkit/src/esp32c3_bringup.c @@ -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.