From ccc8e7ce4dc0b14d0781e7ef40d26cca40b1798a Mon Sep 17 00:00:00 2001 From: Shicheng Chu <1468559561@qq.com> Date: Mon, 5 Jun 2023 15:52:31 +0800 Subject: [PATCH] [bsp][pico]update: add drv_adc (#7603) --- bsp/raspberry-pico/.config | 34 +++++++-- bsp/raspberry-pico/drivers/SConscript | 3 + bsp/raspberry-pico/drivers/drv_adc.c | 95 +++++++++++++++++++++++++ bsp/raspberry-pico/drivers/drv_adc.h | 60 ++++++++++++++++ bsp/raspberry-pico/libraries/Kconfig | 15 ++++ bsp/raspberry-pico/libraries/SConscript | 2 + bsp/raspberry-pico/rtconfig.h | 11 ++- 7 files changed, 213 insertions(+), 7 deletions(-) create mode 100644 bsp/raspberry-pico/drivers/drv_adc.c create mode 100644 bsp/raspberry-pico/drivers/drv_adc.h diff --git a/bsp/raspberry-pico/.config b/bsp/raspberry-pico/.config index f984d59e89..1f57987a6d 100644 --- a/bsp/raspberry-pico/.config +++ b/bsp/raspberry-pico/.config @@ -57,7 +57,6 @@ CONFIG_RT_USING_MESSAGEQUEUE=y # # Memory Management # -CONFIG_RT_PAGE_MAX_ORDER=11 CONFIG_RT_USING_MEMPOOL=y CONFIG_RT_USING_SMALL_MEM=y # CONFIG_RT_USING_SLAB is not set @@ -81,7 +80,7 @@ CONFIG_RT_USING_DEVICE=y CONFIG_RT_USING_CONSOLE=y CONFIG_RT_CONSOLEBUF_SIZE=128 CONFIG_RT_CONSOLE_DEVICE_NAME="uart0" -CONFIG_RT_VER_NUM=0x50000 +CONFIG_RT_VER_NUM=0x50001 # CONFIG_RT_USING_STDC_ATOMIC is not set # CONFIG_RT_USING_CACHE is not set # CONFIG_RT_USING_HW_ATOMIC is not set @@ -115,6 +114,10 @@ CONFIG_FINSH_USING_DESCRIPTION=y # CONFIG_FINSH_ECHO_DISABLE_DEFAULT is not set # CONFIG_FINSH_USING_AUTH is not set CONFIG_FINSH_ARG_MAX=10 + +# +# DFS: device virtual file system +# # CONFIG_RT_USING_DFS is not set # CONFIG_RT_USING_FAL is not set @@ -436,6 +439,7 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # CONFIG_PKG_USING_HASH_MATCH is not set # CONFIG_PKG_USING_ARMV7M_DWT_TOOL is not set # CONFIG_PKG_USING_VOFA_PLUS is not set +# CONFIG_PKG_USING_RT_TRACE is not set # # system packages @@ -507,6 +511,7 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # CONFIG_PKG_USING_QPC is not set # CONFIG_PKG_USING_AGILE_UPGRADE is not set # CONFIG_PKG_USING_FLASH_BLOB is not set +# CONFIG_PKG_USING_MLIBC is not set # # peripheral libraries and drivers @@ -591,6 +596,7 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # CONFIG_PKG_USING_FT5426 is not set # CONFIG_PKG_USING_FT6236 is not set # CONFIG_PKG_USING_XPT2046_TOUCH is not set +# CONFIG_PKG_USING_CST816X is not set # CONFIG_PKG_USING_REALTEK_AMEBA is not set # CONFIG_PKG_USING_STM32_SDIO is not set # CONFIG_PKG_USING_ESP_IDF is not set @@ -603,7 +609,6 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # CONFIG_PKG_USING_LKDGUI is not set # CONFIG_PKG_USING_NRF5X_SDK is not set # CONFIG_PKG_USING_NRFX is not set -# CONFIG_PKG_USING_WM_LIBRARIES is not set # # Kendryte SDK @@ -661,14 +666,15 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # CONFIG_PKG_USING_MISAKA_AT24CXX is not set # CONFIG_PKG_USING_MISAKA_RGB_BLING is not set # CONFIG_PKG_USING_LORA_MODEM_DRIVER is not set -# CONFIG_PKG_USING_BL_MCU_SDK is not set # CONFIG_PKG_USING_SOFT_SERIAL is not set # CONFIG_PKG_USING_MB85RS16 is not set # CONFIG_PKG_USING_RFM300 is not set # CONFIG_PKG_USING_IO_INPUT_FILTER is not set # CONFIG_PKG_USING_RASPBERRYPI_PICO_SDK is not set # CONFIG_PKG_USING_LRF_NV7LIDAR is not set +# CONFIG_PKG_USING_AIP650 is not set # CONFIG_PKG_USING_FINGERPRINT is not set +# CONFIG_PKG_USING_SPI_TOOLS is not set # # AI packages @@ -687,7 +693,10 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # Signal Processing and Control Algorithm Packages # # CONFIG_PKG_USING_FIRE_PID_CURVE is not set +# CONFIG_PKG_USING_QPID is not set # CONFIG_PKG_USING_UKAL is not set +# CONFIG_PKG_USING_DIGITALCTRL is not set +# CONFIG_PKG_USING_KISSFFT is not set # # miscellaneous packages @@ -734,7 +743,6 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # CONFIG_PKG_USING_DSTR is not set # CONFIG_PKG_USING_TINYFRAME is not set # CONFIG_PKG_USING_KENDRYTE_DEMO is not set -# CONFIG_PKG_USING_DIGITALCTRL is not set # CONFIG_PKG_USING_UPACKER is not set # CONFIG_PKG_USING_UPARAM is not set # CONFIG_PKG_USING_HELLO is not set @@ -761,6 +769,7 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # # Projects # +# CONFIG_PKG_USING_ARDUINO_MSGQ_C_CPP_DEMO is not set # CONFIG_PKG_USING_ARDUINO_ULTRASOUND_RADAR is not set # CONFIG_PKG_USING_ARDUINO_SENSOR_KIT is not set # CONFIG_PKG_USING_ARDUINO_MATLAB_SUPPORT is not set @@ -907,14 +916,20 @@ CONFIG_RT_LIBC_DEFAULT_TIMEZONE=8 # # Display # +# CONFIG_PKG_USING_ARDUINO_TFT_ESPI is not set +# CONFIG_PKG_USING_ARDUINO_ADAFRUIT_GFX_LIBRARY is not set # CONFIG_PKG_USING_ARDUINO_U8G2 is not set # CONFIG_PKG_USING_ARDUINO_U8GLIB_ARDUINO is not set +# CONFIG_PKG_USING_ARDUINO_ADAFRUIT_ST7735 is not set +# CONFIG_PKG_USING_ARDUINO_ADAFRUIT_SSD1306 is not set # CONFIG_PKG_USING_SEEED_TM1637 is not set # # Timing # # CONFIG_PKG_USING_ARDUINO_MSTIMER2 is not set +# CONFIG_PKG_USING_ARDUINO_TICKER is not set +# CONFIG_PKG_USING_ARDUINO_TASKSCHEDULER is not set # # Data Processing @@ -975,3 +990,12 @@ CONFIG_SOC_RP2040=y # Onboard Peripheral Drivers # # CONFIG_BSP_USING_LVGL is not set + +# +# On-chip Peripheral Drivers +# +CONFIG_BSP_USING_PIN=y +CONFIG_BSP_USING_UART=y +# CONFIG_BSP_USING_SOFT_I2C is not set +# CONFIG_BSP_USING_SOFT_SPI is not set +# CONFIG_BSP_USING_ADC is not set diff --git a/bsp/raspberry-pico/drivers/SConscript b/bsp/raspberry-pico/drivers/SConscript index ef3df3b840..0244d8a644 100644 --- a/bsp/raspberry-pico/drivers/SConscript +++ b/bsp/raspberry-pico/drivers/SConscript @@ -16,6 +16,9 @@ if GetDepend('BSP_USING_SOFT_I2C'): if GetDepend('BSP_USING_SOFT_SPI'): src += ['drv_soft_spi.c'] +if GetDepend('BSP_USING_ADC'): + src += ['drv_adc.c'] + group = DefineGroup('Drivers', src, depend = [''], CPPPATH = CPPPATH) Return('group') diff --git a/bsp/raspberry-pico/drivers/drv_adc.c b/bsp/raspberry-pico/drivers/drv_adc.c new file mode 100644 index 0000000000..47e38aef82 --- /dev/null +++ b/bsp/raspberry-pico/drivers/drv_adc.c @@ -0,0 +1,95 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-06-04 Chushicheng first version + */ + +#include "board.h" +#include "drv_adc.h" +#include "hardware/adc.h" + +#ifdef BSP_USING_ADC +#define DBG_TAG "drv.adc" +#define DBG_LVL DBG_INFO +#include + +static struct pico_adc_config adc_config[] = +{ +#ifdef BSP_USING_ADC0 + ADC0_CONFIG, +#endif + +#ifdef BSP_USING_ADC1 + ADC1_CONFIG, +#endif + +#ifdef BSP_USING_ADC2 + ADC2_CONFIG, +#endif +}; + +static struct pico_adc pico_adc_obj[sizeof(adc_config) / sizeof(adc_config[0])]; + +static rt_err_t pico_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled) +{ + struct pico_adc_config *pico_adc_handler; + RT_ASSERT(device != RT_NULL); + pico_adc_handler = device->parent.user_data; + + if (enabled) + { + adc_gpio_init(pico_adc_handler->pin); + adc_select_input(pico_adc_handler->channel); + } + + return RT_EOK; +} + +static rt_err_t pico_adc_get_value(struct rt_adc_device *device, rt_int8_t channel, rt_uint32_t *value) +{ + RT_ASSERT(device != RT_NULL); + RT_ASSERT(value != RT_NULL); + + /* get ADC value */ + *value = (rt_uint32_t)adc_read(); + + return RT_EOK; +} + +static const struct rt_adc_ops pico_adc_ops = +{ + .enabled = pico_adc_enabled, + .convert = pico_adc_get_value, + .get_resolution = RT_NULL, + .get_vref = RT_NULL, +}; + +int rt_hw_adc_init(void) +{ + int result = RT_EOK; + + adc_init(); + + for (rt_size_t i = 0; i < sizeof(pico_adc_obj) / sizeof(struct pico_adc); i++) + { + /* register ADC device */ + if (rt_hw_adc_register(&pico_adc_obj[i].pico_adc_device, adc_config[i].device_name, &pico_adc_ops, &adc_config[i]) == RT_EOK) + { + LOG_D("%s init success", adc_config[i].device_name); + } + else + { + LOG_E("%s register failed", adc_config[i].device_name); + result = -RT_ERROR; + } + } + + return result; +} +INIT_BOARD_EXPORT(rt_hw_adc_init); + +#endif /* BSP_USING_ADC */ diff --git a/bsp/raspberry-pico/drivers/drv_adc.h b/bsp/raspberry-pico/drivers/drv_adc.h new file mode 100644 index 0000000000..a566f688b1 --- /dev/null +++ b/bsp/raspberry-pico/drivers/drv_adc.h @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-06-04 Chushicheng first version + */ + +#ifndef __DRV_ADC_H__ +#define __DRV_ADC_H__ + +#include +#if defined BSP_USING_ADC + +/* pico i2c dirver class */ +static struct pico_adc +{ + struct rt_adc_ops ops; + struct rt_adc_device pico_adc_device; +}; + +/* pico config class */ +static struct pico_adc_config +{ + rt_uint8_t pin; + rt_uint8_t channel; + const char *device_name; +}; + +#ifdef BSP_USING_ADC0 +#define ADC0_CONFIG \ + { \ + .pin = 26, \ + .channel = 0, \ + .device_name = "adc0", \ + } +#endif +#ifdef BSP_USING_ADC1 +#define ADC1_CONFIG \ + { \ + .pin = 27, \ + .channel = 1, \ + .device_name = "adc1", \ + } +#endif +#ifdef BSP_USING_ADC2 +#define ADC1_CONFIG \ + { \ + .pin = 28, \ + .channel = 2, \ + .device_name = "adc2", \ + } +#endif + +int rt_hw_adc_init(void); + +#endif /* BSP_USING_ADC */ +#endif /* __DRV_ADC_H__ */ diff --git a/bsp/raspberry-pico/libraries/Kconfig b/bsp/raspberry-pico/libraries/Kconfig index e1e1e93edd..bd1cd56a9a 100644 --- a/bsp/raspberry-pico/libraries/Kconfig +++ b/bsp/raspberry-pico/libraries/Kconfig @@ -97,7 +97,22 @@ menu "On-chip Peripheral Drivers" range 0 28 default 12 endif + endif + menuconfig BSP_USING_ADC + bool "Enable ADC" + default n + select RT_USING_ADC + if BSP_USING_ADC + config BSP_USING_ADC0 + bool "Enable ADC0 (GP26)" + default n + config BSP_USING_ADC1 + bool "Enable ADC1 (GP27)" + default n + config BSP_USING_ADC2 + bool "Enable ADC2 (GP28)" + default n endif endmenu diff --git a/bsp/raspberry-pico/libraries/SConscript b/bsp/raspberry-pico/libraries/SConscript index f21c6a3b02..d6210e57c1 100644 --- a/bsp/raspberry-pico/libraries/SConscript +++ b/bsp/raspberry-pico/libraries/SConscript @@ -11,6 +11,7 @@ pico-sdk/src/rp2_common/hardware_flash/flash.c pico-sdk/src/rp2_common/pico_multicore/multicore.c pico-sdk/src/rp2_common/pico_stdlib/stdlib.c pico-sdk/src/rp2_common/hardware_gpio/gpio.c +pico-sdk/src/rp2_common/hardware_adc/adc.c pico-sdk/src/rp2_common/hardware_claim/claim.c pico-sdk/src/rp2_common/hardware_sync/sync.c pico-sdk/src/rp2_common/pico_platform/platform.c @@ -65,6 +66,7 @@ path = [ cwd + '/pico-sdk/src/rp2_common/pico_multicore/include', cwd + '/pico-sdk/src/common/pico_stdlib/include', cwd + '/pico-sdk/src/rp2_common/hardware_gpio/include', + cwd + '/pico-sdk/src/rp2_common/hardware_adc/include', cwd + '/pico-sdk/src/common/pico_base/include', cwd + '/pico-sdk/src/boards/include', cwd + '/pico-sdk/src/rp2_common/pico_platform/include', diff --git a/bsp/raspberry-pico/rtconfig.h b/bsp/raspberry-pico/rtconfig.h index 0741641c9b..ee30d0c134 100644 --- a/bsp/raspberry-pico/rtconfig.h +++ b/bsp/raspberry-pico/rtconfig.h @@ -33,7 +33,6 @@ /* Memory Management */ -#define RT_PAGE_MAX_ORDER 11 #define RT_USING_MEMPOOL #define RT_USING_SMALL_MEM #define RT_USING_SMALL_MEM_AS_HEAP @@ -45,7 +44,7 @@ #define RT_USING_CONSOLE #define RT_CONSOLEBUF_SIZE 128 #define RT_CONSOLE_DEVICE_NAME "uart0" -#define RT_VER_NUM 0x50000 +#define RT_VER_NUM 0x50001 #define ARCH_ARM #define ARCH_ARM_CORTEX_M #define ARCH_ARM_CORTEX_M0 @@ -70,6 +69,9 @@ #define FINSH_USING_DESCRIPTION #define FINSH_ARG_MAX 10 +/* DFS: device virtual file system */ + + /* Device Drivers */ #define RT_USING_DEVICE_IPC @@ -225,4 +227,9 @@ /* Onboard Peripheral Drivers */ +/* On-chip Peripheral Drivers */ + +#define BSP_USING_PIN +#define BSP_USING_UART + #endif