This is an automated email from the ASF dual-hosted git repository. acassis pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 2f715c491802757cbf1d2fe59000b80e4157fdb1 Author: Marcio Ribeiro <[email protected]> AuthorDate: Wed Sep 9 17:49:21 2026 -0300 boards/risc-v/esp32c2/common: add on-chip peripheral board helpers Add ADC, I2C, LEDC, SPI, SPI flash, MMCSD and RNG helpers used by the ESP32-C2 board bring-up. Assisted-by: Claude:claude-opus-5 Signed-off-by: Marcio Ribeiro <[email protected]> --- .../risc-v/esp32c2/common/include/esp_board_adc.h | 79 ++++ .../risc-v/esp32c2/common/include/esp_board_i2c.h | 76 ++++ .../risc-v/esp32c2/common/include/esp_board_ledc.h | 76 ++++ .../esp32c2/common/include/esp_board_mmcsd.h | 76 ++++ .../esp32c2/common/include/esp_board_rng90.h | 74 +++ .../esp32c2/common/include/esp_board_spidev.h | 80 ++++ .../esp32c2/common/include/esp_board_spiflash.h | 76 ++++ .../esp32c2/common/include/esp_board_spislavedev.h | 76 ++++ boards/risc-v/esp32c2/common/src/CMakeLists.txt | 40 ++ boards/risc-v/esp32c2/common/src/Make.defs | 36 ++ boards/risc-v/esp32c2/common/src/esp_board_adc.c | 138 ++++++ boards/risc-v/esp32c2/common/src/esp_board_i2c.c | 170 +++++++ boards/risc-v/esp32c2/common/src/esp_board_ledc.c | 149 ++++++ boards/risc-v/esp32c2/common/src/esp_board_mmcsd.c | 97 ++++ boards/risc-v/esp32c2/common/src/esp_board_rng90.c | 95 ++++ boards/risc-v/esp32c2/common/src/esp_board_spi.c | 90 ++++ .../risc-v/esp32c2/common/src/esp_board_spidev.c | 197 ++++++++ .../risc-v/esp32c2/common/src/esp_board_spiflash.c | 499 +++++++++++++++++++++ .../esp32c2/common/src/esp_board_spislavedev.c | 85 ++++ 19 files changed, 2209 insertions(+) diff --git a/boards/risc-v/esp32c2/common/include/esp_board_adc.h b/boards/risc-v/esp32c2/common/include/esp_board_adc.h new file mode 100644 index 00000000000..c5f7845b03c --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_adc.h @@ -0,0 +1,79 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_adc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP32C2_BOARD_ADC_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP32C2_BOARD_ADC_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_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_ADC +int board_adc_init(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP32C2_BOARD_ADC_H */ diff --git a/boards/risc-v/esp32c2/common/include/esp_board_i2c.h b/boards/risc-v/esp32c2/common/include/esp_board_i2c.h new file mode 100644 index 00000000000..d15cca9a27d --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_i2c.h @@ -0,0 +1,76 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_i2c.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP_BOARD_I2C_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_I2C_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_i2c_init + * + * Description: + * Configure the I2C driver. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_I2C +int board_i2c_init(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_I2C_H */ diff --git a/boards/risc-v/esp32c2/common/include/esp_board_ledc.h b/boards/risc-v/esp32c2/common/include/esp_board_ledc.h new file mode 100644 index 00000000000..1702e022b3c --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_ledc.h @@ -0,0 +1,76 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_ledc.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP_BOARD_LEDC_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_LEDC_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_ledc_setup + * + * Description: + * Initialize LEDC PWM and register the PWM device. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_LEDC +int board_ledc_setup(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_LEDC_H */ diff --git a/boards/risc-v/esp32c2/common/include/esp_board_mmcsd.h b/boards/risc-v/esp32c2/common/include/esp_board_mmcsd.h new file mode 100644 index 00000000000..86eddbca271 --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_mmcsd.h @@ -0,0 +1,76 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_mmcsd.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP_BOARD_MMCSD_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_MMCSD_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: esp_mmcsd_spi_initialize + * + * Description: + * Initialize SPI-based SD card. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_MMCSD_SPI +int esp_mmcsd_spi_initialize(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_MMCSD_H */ diff --git a/boards/risc-v/esp32c2/common/include/esp_board_rng90.h b/boards/risc-v/esp32c2/common/include/esp_board_rng90.h new file mode 100644 index 00000000000..31864c2b230 --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_rng90.h @@ -0,0 +1,74 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_rng90.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP_BOARD_RNG90_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_RNG90_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_rng90_initialize + * + * Description: + * Initialize and register the RNG90 True Random Number Generator driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/rngN + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + ****************************************************************************/ + +#ifdef CONFIG_DEV_RNG90 +int board_rng90_initialize(int devno); +#endif /* CONFIG_DEV_RNG90 */ + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_RNG90_H */ \ No newline at end of file diff --git a/boards/risc-v/esp32c2/common/include/esp_board_spidev.h b/boards/risc-v/esp32c2/common/include/esp_board_spidev.h new file mode 100644 index 00000000000..3bebd6bd1e8 --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_spidev.h @@ -0,0 +1,80 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_spidev.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_RISC_V_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPIDEV_H +#define __BOARDS_RISC_V_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPIDEV_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include <nuttx/config.h> + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef __ASSEMBLY__ + +/**************************************************************************** + * Public Data + ****************************************************************************/ + +#undef EXTERN +#if defined(__cplusplus) +#define EXTERN extern "C" +extern "C" +{ +#else +#define EXTERN extern +#endif + +/**************************************************************************** + * Public Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Name: board_spidev_initialize + * + * Description: + * Initialize SPI driver and register the /dev/spi device. + * + * Input Parameters: + * bus - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_SPI +int board_spidev_initialize(int bus); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISC_V_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPIDEV_H */ diff --git a/boards/risc-v/esp32c2/common/include/esp_board_spiflash.h b/boards/risc-v/esp32c2/common/include/esp_board_spiflash.h new file mode 100644 index 00000000000..805278602c6 --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_spiflash.h @@ -0,0 +1,76 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_spiflash.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPIFLASH_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPIFLASH_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_spiflash_init + * + * Description: + * Initialize the SPIFLASH and register the MTD device. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_SPIFLASH +int board_spiflash_init(void); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPIFLASH_H */ diff --git a/boards/risc-v/esp32c2/common/include/esp_board_spislavedev.h b/boards/risc-v/esp32c2/common/include/esp_board_spislavedev.h new file mode 100644 index 00000000000..1f9c34efc44 --- /dev/null +++ b/boards/risc-v/esp32c2/common/include/esp_board_spislavedev.h @@ -0,0 +1,76 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/include/esp_board_spislavedev.h + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPISLAVEDEV_H +#define __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPISLAVEDEV_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_spislavedev_initialize + * + * Description: + * Initialize SPI Slave driver and register the /dev/spislv device. + * + * Input Parameters: + * bus - The SPI bus number, used to build the device path as /dev/spislvN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_SPI_SLAVE +int board_spislavedev_initialize(int bus); +#endif + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* __BOARDS_RISCV_ESP32C2_COMMON_INCLUDE_ESP_BOARD_SPISLAVEDEV_H */ diff --git a/boards/risc-v/esp32c2/common/src/CMakeLists.txt b/boards/risc-v/esp32c2/common/src/CMakeLists.txt index b7b2785374d..ba812a0a202 100644 --- a/boards/risc-v/esp32c2/common/src/CMakeLists.txt +++ b/boards/risc-v/esp32c2/common/src/CMakeLists.txt @@ -22,4 +22,44 @@ set(SRCS) +if(CONFIG_ARCH_BOARD_COMMON) + + if(CONFIG_ESPRESSIF_ADC) + list(APPEND SRCS esp_board_adc.c) + endif() + + if(CONFIG_ESPRESSIF_LEDC) + list(APPEND SRCS esp_board_ledc.c) + endif() + + if(CONFIG_ESPRESSIF_SPI) + list(APPEND SRCS esp_board_spi.c) + endif() + + if(CONFIG_SPI) + list(APPEND SRCS esp_board_spidev.c) + endif() + + if(CONFIG_SPI_SLAVE) + list(APPEND SRCS esp_board_spislavedev.c) + endif() + + if(CONFIG_I2C) + list(APPEND SRCS esp_board_i2c.c) + endif() + + if(CONFIG_ESPRESSIF_SPIFLASH) + list(APPEND SRCS esp_board_spiflash.c) + endif() + + if(CONFIG_DEV_RNG90) + list(APPEND SRCS esp_board_rng90.c) + endif() + + if(CONFIG_MMCSD_SPI) + list(APPEND SRCS esp_board_mmcsd.c) + endif() + +endif() + target_sources(board PRIVATE ${SRCS}) diff --git a/boards/risc-v/esp32c2/common/src/Make.defs b/boards/risc-v/esp32c2/common/src/Make.defs index 74821f85cfe..313eb74f283 100644 --- a/boards/risc-v/esp32c2/common/src/Make.defs +++ b/boards/risc-v/esp32c2/common/src/Make.defs @@ -22,6 +22,42 @@ ifeq ($(CONFIG_ARCH_BOARD_COMMON),y) +ifeq ($(CONFIG_ESPRESSIF_ADC),y) + CSRCS += esp_board_adc.c +endif + +ifeq ($(CONFIG_ESPRESSIF_LEDC),y) + CSRCS += esp_board_ledc.c +endif + +ifeq ($(CONFIG_ESPRESSIF_SPI),y) + CSRCS += esp_board_spi.c +endif + +ifeq ($(CONFIG_SPI),y) + CSRCS += esp_board_spidev.c +endif + +ifeq ($(CONFIG_SPI_SLAVE),y) + CSRCS += esp_board_spislavedev.c +endif + +ifeq ($(CONFIG_I2C),y) + CSRCS += esp_board_i2c.c +endif + +ifeq ($(CONFIG_ESPRESSIF_SPIFLASH),y) + CSRCS += esp_board_spiflash.c +endif + +ifeq ($(CONFIG_DEV_RNG90),y) + CSRCS += esp_board_rng90.c +endif + +ifeq ($(CONFIG_MMCSD_SPI),y) + CSRCS += esp_board_mmcsd.c +endif + DEPPATH += --dep-path src VPATH += :src CFLAGS += ${INCDIR_PREFIX}$(TOPDIR)$(DELIM)arch$(DELIM)$(CONFIG_ARCH)$(DELIM)src$(DELIM)board$(DELIM)src diff --git a/boards/risc-v/esp32c2/common/src/esp_board_adc.c b/boards/risc-v/esp32c2/common/src/esp_board_adc.c new file mode 100644 index 00000000000..aaf61adedc0 --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_adc.c @@ -0,0 +1,138 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_adc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <syslog.h> + +#include <nuttx/analog/adc.h> + +#include "espressif/esp_adc.h" + +#include "esp_board_adc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/* The number of channels for each ADC */ + +#define ADC_MAX_CHANNELS 5 + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +/* Select channels to be used for each ADC. + * + * GPIOs are fixed for each channel and configured in the lower-half driver. + * + * ADC 1 + * Channel: 0 1 2 3 4 5 + * GPIO: 0 1 2 3 4 5 + * + * On the chanlist arrays below, channels are added +1. Do not change. + */ + +#ifdef CONFIG_ESPRESSIF_ADC_1 +static const uint8_t g_chanlist_adc1[ADC_MAX_CHANNELS] = +{ +#ifdef CONFIG_ESPRESSIF_ADC_1_CH0 + 1, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH1 + 2, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH2 + 3, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH3 + 4, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH4 + 5, +#endif +#ifdef CONFIG_ESPRESSIF_ADC_1_CH5 + 6, +#endif +}; +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_adc_init + * + * Description: + * This function configures and initializes the ADC driver for the board. + * It allocates memory for the ADC device structure, sets up the ADC + * hardware, and registers the ADC device with the system. + * + * Input Parameters: + * None. + * + * Returned Value: + * Returns zero (OK) on successful initialization and registration of the + * ADC device; a negated errno value is returned to indicate the nature + * of any failure. + * + ****************************************************************************/ + +int board_adc_init(void) +{ + int ret; + struct adc_dev_s *adcdev; + + adcdev = kmm_malloc(sizeof(struct adc_dev_s)); + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to allocate adc_dev_s instance\n"); + return -ENOMEM; + } + + memset(adcdev, 0, sizeof(struct adc_dev_s)); + + adcdev = esp_adc_initialize(1, g_chanlist_adc1); + + if (adcdev == NULL) + { + syslog(LOG_ERR, "ERROR: Failed to initialize ADC1\n"); + return -ENODEV; + } + + /* Register the ADC driver at "/dev/adcx" */ + + ret = adc_register("/dev/adc0", adcdev); + if (ret < 0) + { + kmm_free(adcdev); + syslog(LOG_ERR, "ERROR: adc_register /dev/adc0 failed: %d\n", ret); + } + + return ret; +} diff --git a/boards/risc-v/esp32c2/common/src/esp_board_i2c.c b/boards/risc-v/esp32c2/common/src/esp_board_i2c.c new file mode 100644 index 00000000000..5977b07a5bf --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_i2c.c @@ -0,0 +1,170 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_i2c.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <syslog.h> +#include <errno.h> +#include <sys/types.h> + +#include <nuttx/i2c/i2c_master.h> + +#ifdef CONFIG_ESPRESSIF_I2C_BITBANG +#include "espressif/esp_i2c_bitbang.h" +#endif +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE +#include "espressif/esp_i2c.h" +#endif +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE +#include "espressif/esp_i2c_slave.h" +#endif + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE +#define I2C0_SLAVE_ADDR 0x28 +#define I2C0_SLAVE_NBITS 7 +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_I2C_BITBANG +static int i2c_bitbang_driver_init(int bus) +{ + struct i2c_master_s *i2c; + int ret = OK; + + i2c = esp_i2cbus_bitbang_initialize(); + if (i2c == NULL) + { + syslog(LOG_ERR, "Failed to get I2C%d interface\n", bus); + return -ENODEV; + } + +#ifdef CONFIG_I2C_DRIVER + ret = i2c_register(i2c, bus); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to register I2C%d driver: %d\n", bus, ret); + } +#endif + + return ret; +} +#endif + +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE +static int i2c_driver_init(int bus) +{ + struct i2c_master_s *i2c; + int ret = OK; + + i2c = esp_i2cbus_initialize(bus); + if (i2c == NULL) + { + syslog(LOG_ERR, "Failed to get I2C%d interface\n", bus); + return -ENODEV; + } + +#ifdef CONFIG_I2C_DRIVER + ret = i2c_register(i2c, bus); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to register I2C%d driver: %d\n", bus, ret); + esp_i2cbus_uninitialize(i2c); + } +#endif + + return ret; +} +#endif + +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE +static int i2c_slave_driver_init(int bus, int addr) +{ + struct i2c_slave_s *i2c; + int ret = OK; + + i2c = esp_i2cbus_slave_initialize(bus, addr); + if (i2c == NULL) + { + syslog(LOG_ERR, "Failed to get I2C%d interface\n", bus); + return -ENODEV; + } + +#ifdef CONFIG_I2C_DRIVER + ret = i2c_slave_register(i2c, bus, addr, I2C0_SLAVE_NBITS); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to register I2C%d driver: %d\n", bus, ret); + esp_i2cbus_slave_uninitialize(i2c); + } +#endif + + return ret; +} +#endif + +/**************************************************************************** + * Name: board_i2c_init + * + * Description: + * Configure the I2C driver. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_i2c_init(void) +{ + int ret = OK; + +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_MASTER_MODE +# ifdef CONFIG_ESPRESSIF_I2C0_MASTER_MODE + ret = i2c_driver_init(ESPRESSIF_I2C0); +# endif +#endif + +#ifdef CONFIG_ESPRESSIF_I2C_BITBANG +# ifdef CONFIG_ESPRESSIF_I2C_BITBANG_MASTER_MODE + ret = i2c_bitbang_driver_init(ESPRESSIF_I2C_BITBANG); +# endif +#endif + +#ifdef CONFIG_ESPRESSIF_I2C_PERIPH_SLAVE_MODE +# ifdef CONFIG_ESPRESSIF_I2C0_SLAVE_MODE + ret = i2c_slave_driver_init(ESPRESSIF_I2C0_SLAVE, I2C0_SLAVE_ADDR); +# endif +#endif + + return ret; +} diff --git a/boards/risc-v/esp32c2/common/src/esp_board_ledc.c b/boards/risc-v/esp32c2/common/src/esp_board_ledc.c new file mode 100644 index 00000000000..b0fff5228dd --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_ledc.c @@ -0,0 +1,149 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_ledc.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <sys/types.h> +#include <errno.h> +#include <nuttx/debug.h> + +#include <nuttx/board.h> +#include <nuttx/timers/pwm.h> + +#include <arch/board/board.h> + +#include "espressif/esp_ledc.h" + +#include "esp_board_ledc.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#define LEDC_TIMER0 0 +#define LEDC_TIMER1 1 +#define LEDC_TIMER2 2 +#define LEDC_TIMER3 3 + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_ledc_setup + * + * Description: + * Initialize LEDC PWM and register the PWM device. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_ledc_setup(void) +{ + int ret = OK; + struct pwm_lowerhalf_s *pwm; + +#ifdef CONFIG_ESPRESSIF_LEDC_TIMER0 + pwm = esp_ledc_init(LEDC_TIMER0); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 0 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm0" */ + + ret = pwm_register("/dev/pwm0", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESPRESSIF_LEDC_TIMER1 + pwm = esp_ledc_init(LEDC_TIMER1); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 1 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm1" */ + + ret = pwm_register("/dev/pwm1", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESPRESSIF_LEDC_TIMER2 + pwm = esp_ledc_init(LEDC_TIMER2); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 2 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm2" */ + + ret = pwm_register("/dev/pwm2", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + +#ifdef CONFIG_ESPRESSIF_LEDC_TIMER3 + pwm = esp_ledc_init(LEDC_TIMER3); + if (!pwm) + { + syslog(LOG_ERR, "ERROR: Failed to get the LEDC PWM 3 lower half\n"); + return -ENODEV; + } + + /* Register the PWM driver at "/dev/pwm3" */ + + ret = pwm_register("/dev/pwm3", pwm); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: pwm_register failed: %d\n", ret); + return ret; + } +#endif + + return ret; +} + diff --git a/boards/risc-v/esp32c2/common/src/esp_board_mmcsd.c b/boards/risc-v/esp32c2/common/src/esp_board_mmcsd.c new file mode 100644 index 00000000000..57d8f2c9275 --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_mmcsd.c @@ -0,0 +1,97 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_mmcsd.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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/debug.h> +#include <nuttx/config.h> +#include <nuttx/mmcsd.h> +#include <nuttx/spi/spi.h> + +#include "espressif/esp_spi.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_mmcsd_spi_initialize + * + * Description: + * Initialize SPI-based SD card. + * + * Input Parameters: + * minor - The MMC/SD minor device number. The MMC/SD device will be + * registered as /dev/mmcsdN where N is the minor number + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int esp_mmcsd_spi_initialize(void) +{ + struct spi_dev_s *spi; + int ret; + + syslog(LOG_INFO, "INFO: init MMC/SD slot %d on SPI%d: /dev/mmcsd%d\n", + CONFIG_NSH_MMCSDSLOTNO, CONFIG_NSH_MMCSDSPIPORTNO, + CONFIG_NSH_MMCSDMINOR); + + spi = esp_spibus_initialize(CONFIG_NSH_MMCSDSPIPORTNO); + + if (spi == NULL) + { + syslog(LOG_ERR, "ERROR: failed to initialize SPI%d.\n", + CONFIG_NSH_MMCSDSPIPORTNO); + return -ENODEV; + } + + /* Mounts to /dev/mmcsdN where N in the minor number */ + + ret = mmcsd_spislotinitialize(CONFIG_NSH_MMCSDMINOR, + CONFIG_NSH_MMCSDSLOTNO, spi); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: failed to bind SPI%d to SD slot %d\n", + CONFIG_NSH_MMCSDSPIPORTNO, CONFIG_NSH_MMCSDSLOTNO); + return ret; + } + + syslog(LOG_INFO, "INFO: MMCSD initialized\n"); + return OK; +} diff --git a/boards/risc-v/esp32c2/common/src/esp_board_rng90.c b/boards/risc-v/esp32c2/common/src/esp_board_rng90.c new file mode 100644 index 00000000000..d82af86f201 --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_rng90.c @@ -0,0 +1,95 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_rng90.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <nuttx/arch.h> +#include <nuttx/crypto/rng90.h> +#include <nuttx/debug.h> +#include <nuttx/i2c/i2c_master.h> + +#ifndef CONFIG_ESPRESSIF_I2C_BITBANG +# include "espressif/esp_i2c.h" +#else +# include "espressif/esp_i2c_bitbang.h" +#endif + +#include "esp_board_rng90.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_rng90_initialize + * + * Description: + * Initialize and register the RNG90 True Random Number Generator driver. + * + * Input Parameters: + * devno - The device number, used to build the device path as /dev/rngN + * + * Returned Value: + * Zero (OK) on success; a negated errno value on failure. + ****************************************************************************/ + +int board_rng90_initialize(int devno) +{ + struct i2c_master_s *i2c; + char devpath[16]; + int ret; + + sninfo("Initializing RNG90\n"); + + /* Initialize I2C */ + +#ifndef CONFIG_ESPRESSIF_I2C_BITBANG + i2c = esp_i2cbus_initialize(ESPRESSIF_I2C0); +#else + i2c = esp_i2cbus_bitbang_initialize(); +#endif + + if (i2c == NULL) + { + snerr("ERROR: Failed to initialize I2C for RNG90\n"); + return -ENODEV; + } + + /* Register the RNG90 driver at "/dev/rngN" */ + + snprintf(devpath, sizeof(devpath), "/dev/rng%d", devno); + ret = rng90_register(devpath, i2c, RNG90_I2C_ADDR); + if (ret < 0) + { + snerr("ERROR: Failed to register RNG90 driver: %d\n", ret); + return ret; + } + + return OK; +} + diff --git a/boards/risc-v/esp32c2/common/src/esp_board_spi.c b/boards/risc-v/esp32c2/common/src/esp_board_spi.c new file mode 100644 index 00000000000..6446329be28 --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_spi.c @@ -0,0 +1,90 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_spi.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <stdint.h> +#include <stdbool.h> +#include <nuttx/debug.h> + +#include <nuttx/spi/spi.h> + +#include "espressif/esp_gpio.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: esp_spi2_status + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_SPI2 + +uint8_t esp_spi2_status(struct spi_dev_s *dev, uint32_t devid) +{ + uint8_t status = 0; + + if (devid == SPIDEV_MMCSD(0)) + { + return SPI_STATUS_PRESENT; + } + + return status; +} + +#endif + +/**************************************************************************** + * Name: esp_spi2_cmddata + ****************************************************************************/ + +#if defined(CONFIG_ESPRESSIF_SPI2) && defined(CONFIG_SPI_CMDDATA) + +int esp_spi2_cmddata(struct spi_dev_s *dev, uint32_t devid, bool cmd) +{ + if (devid == SPIDEV_DISPLAY(0)) + { + /* This is the Data/Command control pad which determines whether the + * data bits are data or a command. + */ + + esp_gpiowrite(CONFIG_ESPRESSIF_SPI2_MISOPIN, !cmd); + + return OK; + } + + spiinfo("devid: %" PRIu32 " CMD: %s\n", devid, cmd ? "command" : + "data"); + + return -ENODEV; +} + +#endif diff --git a/boards/risc-v/esp32c2/common/src/esp_board_spidev.c b/boards/risc-v/esp32c2/common/src/esp_board_spidev.c new file mode 100644 index 00000000000..b1cbbcfa235 --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_spidev.c @@ -0,0 +1,197 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_spidev.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <syslog.h> +#include <errno.h> + +#include <nuttx/spi/spi_transfer.h> + +#ifdef CONFIG_ESPRESSIF_SPI_PERIPH +#include "espressif/esp_spi.h" +#endif +#ifdef CONFIG_ESPRESSIF_SPI_BITBANG +#include "espressif/esp_spi_bitbang.h" +#endif + +#include "esp_board_spidev.h" + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: spi_bitbang_driver_init + * + * Description: + * Initialize SPI bitbang driver and register the /dev/spi device. + * + * Input Parameters: + * port - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_SPI_BITBANG +static int spi_bitbang_driver_init(int port) +{ + struct spi_dev_s *spi; + int ret = OK; + + /* Initialize SPI device */ + + spi = esp_spi_bitbang_init(); + + if (spi == NULL) + { + syslog(LOG_ERR, "Failed to initialize SPI%d.\n", port); + return -ENODEV; + } + +#ifdef CONFIG_SPI_DRIVER + syslog(LOG_INFO, "Initializing /dev/spi%d...\n", port); + + ret = spi_register(spi, port); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to register /dev/spi%d: %d\n", port, ret); + esp_spi_bitbang_uninitialize(spi); + } +#endif + + return ret; +} +#endif + +/**************************************************************************** + * Name: spi_driver_init + * + * Description: + * Initialize SPI driver and register the /dev/spi device. + * + * Input Parameters: + * port - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_SPI_PERIPH +static int spi_driver_init(int port) +{ + int ret = OK; + struct spi_dev_s *spi; + + /* Initialize SPI device */ + + spi = esp_spibus_initialize(port); + + if (spi == NULL) + { + syslog(LOG_ERR, "Failed to initialize SPI%d.\n", port); + return -ENODEV; + } + +#ifdef CONFIG_SPI_DRIVER + syslog(LOG_INFO, "Initializing /dev/spi%d...\n", port); + + ret = spi_register(spi, port); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to register /dev/spi%d: %d\n", port, ret); + esp_spibus_uninitialize(spi); + } +#endif + + return ret; +} +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_spidev_initialize + * + * Description: + * Configure the SPI drivers. + * + * Input Parameters: + * port - The SPI bus number, used to build the device path as /dev/spiN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_spidev_initialize(int port) +{ + int ret = OK; + + switch (port) + { +#ifdef CONFIG_ESPRESSIF_SPI2 + case ESPRESSIF_SPI2: + { + ret = spi_driver_init(ESPRESSIF_SPI2); + if (ret != OK) + { + return ret; + } + break; + } +#endif + +#ifdef CONFIG_ESPRESSIF_SPI_BITBANG + case ESPRESSIF_SPI_BITBANG: + { + ret = spi_bitbang_driver_init(ESPRESSIF_SPI_BITBANG); + if (ret != OK) + { + return ret; + } + break; + } +#endif + + default: + { + syslog(LOG_ERR, "ERROR: unsupported SPI %d\n", port); + return ERROR; + } + } + + return ret; +} diff --git a/boards/risc-v/esp32c2/common/src/esp_board_spiflash.c b/boards/risc-v/esp32c2/common/src/esp_board_spiflash.c new file mode 100644 index 00000000000..d3e22e0a174 --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_spiflash.c @@ -0,0 +1,499 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_spiflash.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <sys/mount.h> + +#include "inttypes.h" +#include <stdbool.h> +#include <stdlib.h> +#include <stdio.h> +#include <assert.h> +#include <errno.h> +#include <nuttx/debug.h> + +#include <sys/param.h> + +#include <nuttx/kmalloc.h> +#include <nuttx/spi/spi.h> +#include <nuttx/mtd/mtd.h> +#include <nuttx/mtd/configdata.h> +#include <nuttx/fs/nxffs.h> +#include <nuttx/fs/partition.h> + +#include "espressif/esp_spiflash.h" +#include "espressif/esp_spiflash_mtd.h" + +#include "esp_board_spiflash.h" + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Types + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void); +#endif + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static const struct partition_s g_ota_partition_table[] = +{ + { + .name = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_DEVPATH, + .index = 0, + .firstblock = CONFIG_ESPRESSIF_OTA_PRIMARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_DEVPATH, + .index = 1, + .firstblock = CONFIG_ESPRESSIF_OTA_SECONDARY_SLOT_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SLOT_SIZE, + }, + { + .name = CONFIG_ESPRESSIF_OTA_SCRATCH_DEVPATH, + .index = 2, + .firstblock = CONFIG_ESPRESSIF_OTA_SCRATCH_OFFSET, + .blocksize = CONFIG_ESPRESSIF_OTA_SCRATCH_SIZE, + } +}; +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: init_ota_partitions + * + * Description: + * Initialize partitions that are dedicated to firmware OTA update. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION +static int init_ota_partitions(void) +{ + struct mtd_dev_s *mtd; + int ret = OK; + int i; + + for (i = 0; i < nitems(g_ota_partition_table); ++i) + { + const struct partition_s *part = &g_ota_partition_table[i]; + + mtd = esp_spiflash_alloc_mtdpart(part->firstblock, part->blocksize); + + ret = register_mtddriver(part->name, mtd, 0755, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: register_mtddriver %s failed: %d\n", + part->name, ret); + return ret; + } + } + + return ret; +} + +#endif + +/**************************************************************************** + * Name: setup_smartfs + * + * Description: + * Provide a block driver wrapper around MTD partition and mount a + * SMART FS over it. + * + * Parameters: + * smartn - Number used to register the mtd partition: /dev/smartx, where + * x = smartn. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESPRESSIF_SPIFLASH_SMARTFS) +static int setup_smartfs(int smartn, struct mtd_dev_s *mtd, + const char *mnt_pt) +{ + int ret = OK; + char path[22]; + + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + syslog(LOG_INFO, "smart_initialize failed, " + "Trying to erase first...\n"); + ret = mtd->ioctl(mtd, MTDIOC_BULKERASE, 0); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: ioctl(BULKERASE) failed: %d\n", ret); + return ret; + } + + syslog(LOG_INFO, "Erase successful, initializing it again.\n"); + ret = smart_initialize(smartn, mtd, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: smart_initialize failed: %d\n", ret); + return ret; + } + } + + if (mnt_pt != NULL) + { + snprintf(path, sizeof(path), "/dev/smart%d", smartn); + + ret = nx_mount(path, mnt_pt, "smartfs", 0, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); + if (ret == -ENODEV) + { + syslog(LOG_WARNING, "Smartfs seems unformatted. " + "Did you run 'mksmartfs /dev/smart%d'?\n", smartn); + } + + return ret; + } + } + + return ret; +} +#endif + +/**************************************************************************** + * Name: setup_littlefs + * + * Description: + * Register a mtd driver and mount a Little FS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESPRESSIF_SPIFLASH_LITTLEFS) +static int setup_littlefs(const char *path, struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, NULL); + if (ret < 0) + { + ret = nx_mount(path, mnt_pt, "littlefs", 0, "forceformat"); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); + return ret; + } + } + } + + return OK; +} +#endif + +/**************************************************************************** + * Name: setup_spiffs + * + * Description: + * Register a mtd driver and mount a SPIFFS over it. + * + * Parameters: + * path - Path name used to register the mtd driver. + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * priv - Privileges + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESPRESSIF_SPIFLASH_SPIFFS) +static int setup_spiffs(const char *path, struct mtd_dev_s *mtd, + const char *mnt_pt, int priv) +{ + int ret = OK; + + ret = register_mtddriver(path, mtd, priv, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to register MTD: %d\n", ret); + return ERROR; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(path, mnt_pt, "spiffs", 0, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); + return ret; + } + } + + return ret; +} +#endif + +/**************************************************************************** + * Name: setup_nxffs + * + * Description: + * Register a mtd driver and mount a NXFFS over it. + * + * Parameters: + * mtd - Pointer to a pre-allocated mtd partition. + * mnt_pt - Mount point + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +#if defined (CONFIG_ESPRESSIF_SPIFLASH_NXFFS) +static int setup_nxffs(struct mtd_dev_s *mtd, const char *mnt_pt) +{ + int ret = OK; + + ret = nxffs_initialize(mtd); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: NXFFS init failed: %d\n", ret); + return ret; + } + + if (mnt_pt != NULL) + { + ret = nx_mount(NULL, mnt_pt, "nxffs", 0, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to mount the FS volume: %d\n", + ret); + return ret; + } + } + + return ret; +} +#endif + +/**************************************************************************** + * Name: init_storage_partition + * + * Description: + * Initialize partition that is dedicated to general use. + * + * Input Parameters: + * None + * + * Returned Value: + * Zero on success; a negated errno value on failure. + * + ****************************************************************************/ + +static int init_storage_partition(void) +{ + int ret = OK; + struct mtd_dev_s *mtd; + + mtd = esp_spiflash_alloc_mtdpart(CONFIG_ESPRESSIF_STORAGE_MTD_OFFSET, + CONFIG_ESPRESSIF_STORAGE_MTD_SIZE); + if (!mtd) + { + syslog(LOG_ERR, "ERROR: Fail to alloc MTD partition of SPI Flash\n"); + return ERROR; + } + +#if defined (CONFIG_ESPRESSIF_SPIFLASH_SMARTFS) + + ret = setup_smartfs(0, mtd, CONFIG_ESPRESSIF_SPIFLASH_FS_MOUNT_PT); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to setup smartfs\n"); + return ret; + } + +#elif defined (CONFIG_ESPRESSIF_SPIFLASH_NXFFS) + + ret = setup_nxffs(mtd, CONFIG_ESPRESSIF_SPIFLASH_FS_MOUNT_PT); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to setup nxffs\n"); + return ret; + } + +#elif defined (CONFIG_ESPRESSIF_SPIFLASH_LITTLEFS) + + const char *path = "/dev/espflash"; + + ret = setup_littlefs(path, mtd, CONFIG_ESPRESSIF_SPIFLASH_FS_MOUNT_PT, + 0755); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to setup littlefs\n"); + return ret; + } + +#elif defined (CONFIG_ESPRESSIF_SPIFLASH_SPIFFS) + + const char *path = "/dev/espflash"; + + ret = setup_spiffs(path, mtd, CONFIG_ESPRESSIF_SPIFLASH_FS_MOUNT_PT, + 0755); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to setup spiffs\n"); + return ret; + } + +#elif defined (CONFIG_ESPRESSIF_SPIFLASH_MTD_CONFIG) + +# if defined (CONFIG_TESTING_MTD_CONFIG_NVS) + + /* To test power-loss resilient kv system, + * we write possible power-loss flash layout into flash + * then start kv system to see if it recovers. + * To do so, we need a mtd driver so that test code can + * write into flash. + */ + + const char *path = CONFIG_TESTING_MTD_CONFIG_MOUNTPT_NAME; + + ret = register_mtddriver(path, mtd, 0777, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to register MTD: %d\n", ret); + return ret; + } + +# else + + ret = mtdconfig_register(mtd); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to setup mtd config\n"); + return ret; + } + +# endif + +#else + + ret = register_mtddriver("/dev/mtdblock0", mtd, 0755, NULL); + if (ret < 0) + { + syslog(LOG_ERR, "ERROR: Failed to register MTD mtdblock0: %d\n", ret); + return ret; + } + +#endif + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_spiflash_init + * + * Description: + * Initialize the SPI Flash and register the MTD device. + * + * Input Parameters: + * None. + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_spiflash_init(void) +{ + int ret = OK; + +#ifdef CONFIG_ESPRESSIF_HAVE_OTA_PARTITION + ret = init_ota_partitions(); + if (ret < 0) + { + return ret; + } +#endif + + ret = init_storage_partition(); + if (ret < 0) + { + return ret; + } + + return ret; +} diff --git a/boards/risc-v/esp32c2/common/src/esp_board_spislavedev.c b/boards/risc-v/esp32c2/common/src/esp_board_spislavedev.c new file mode 100644 index 00000000000..1bdd25bbbcd --- /dev/null +++ b/boards/risc-v/esp32c2/common/src/esp_board_spislavedev.c @@ -0,0 +1,85 @@ +/**************************************************************************** + * boards/risc-v/esp32c2/common/src/esp_board_spislavedev.c + * + * SPDX-License-Identifier: Apache-2.0 + * + * 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 <syslog.h> +#include <errno.h> + +#include <nuttx/spi/slave.h> + +#include "espressif/esp_spi.h" + +#include "esp_board_spislavedev.h" + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: board_spislavedev_initialize + * + * Description: + * Initialize SPI Slave driver and register the /dev/spislv device. + * + * Input Parameters: + * bus - The SPI bus number, used to build the device path as /dev/spislvN + * + * Returned Value: + * Zero (OK) is returned on success; A negated errno value is returned + * to indicate the nature of any failure. + * + ****************************************************************************/ + +int board_spislavedev_initialize(int bus) +{ + struct spi_slave_ctrlr_s *ctrlr; + int ret = OK; + + /* Initialize SPI Slave controller device */ + + ctrlr = esp_spislave_ctrlr_initialize(bus); + if (ctrlr == NULL) + { + syslog(LOG_ERR, "Failed to initialize SPI%d as slave.\n", bus); + return -ENODEV; + } + +#ifdef CONFIG_SPI_SLAVE_DRIVER + syslog(LOG_INFO, "Initializing /dev/spislv%d...\n", bus); + + ret = spi_slave_register(ctrlr, bus); + if (ret < 0) + { + syslog(LOG_ERR, "Failed to register /dev/spislv%d: %d\n", bus, ret); + + esp_spislave_ctrlr_uninitialize(ctrlr); + } +#endif + + return ret; +}
