From 241e7ff0837ab372bfbb7c36f3792a985693dbdf Mon Sep 17 00:00:00 2001 From: BetMul <98676202+bettermultiply@users.noreply.github.com> Date: Fri, 17 Nov 2023 16:11:37 +0800 Subject: [PATCH] [bsp] [ESP32_C3]add hwtimer support for ESP32C3 (#8263) --- bsp/ESP32_C3/README.md | 2 +- bsp/ESP32_C3/README_ZH.md | 4 +- bsp/ESP32_C3/drivers/Kconfig | 10 ++ bsp/ESP32_C3/drivers/drv_hwtimer.c | 166 +++++++++++++++++++++++++++++ bsp/ESP32_C3/drivers/drv_hwtimer.h | 20 ++++ bsp/ESP32_C3/rtconfig.h | 2 +- 6 files changed, 200 insertions(+), 4 deletions(-) create mode 100644 bsp/ESP32_C3/drivers/drv_hwtimer.c create mode 100644 bsp/ESP32_C3/drivers/drv_hwtimer.h diff --git a/bsp/ESP32_C3/README.md b/bsp/ESP32_C3/README.md index 7789cee56d..78d5f8e4be 100644 --- a/bsp/ESP32_C3/README.md +++ b/bsp/ESP32_C3/README.md @@ -45,7 +45,7 @@ Each peripheral supporting condition for this BSP is as follows: | WIFI | Partial support | There are currently some problems, such as `rt_mq_recive` cannot be used in ISR, etc. | | BLE | Partially supported | There are currently some problems, such as `NimBLE` running errors after starting for a while | | GDBStub | Support | You can use the GDB provided by ESP-IDF by turning on the `BSP_ENABLE_GDBSTUB` switch, which will enter GDB mode after a chip error | - +| HWTIMER | Support | Note: 1. WIFI and BLE cannot be enabled at the same time. When using the BLE driver, be sure to turn off the `RT_USING_WIFI` and `LWIP` switches in `menuconfig`. In addition, due to limited capabilities and lack of debugging equipment, there are problems with WIFI and BLE driver operation. If it can be solved, please contact [timwcx@qq.com](mailto:timwcx@qq.com). diff --git a/bsp/ESP32_C3/README_ZH.md b/bsp/ESP32_C3/README_ZH.md index 5784329f6d..c4c8d4625d 100644 --- a/bsp/ESP32_C3/README_ZH.md +++ b/bsp/ESP32_C3/README_ZH.md @@ -52,7 +52,7 @@ | WIFI | 部分支持 | 目前存在一些问题,例如不能在ISR中使用`rt_mq_recive`等 | | BLE | 部分支持 | 目前存在一些问题,例如`NimBLE`启动一段时间后运行错误 | | GDBStub | 支持 | 通过开启`BSP_ENABLE_GDBSTUB`开关即可使用ESP-IDF所提供的GDB,其会在芯片出错后进入GDB模式 | - +| HWTIMER | 支持 | 注: 1、WIFI和BLE不能同时启用,在使用BLE驱动时注意在`menuconfig`中关闭`RT_USING_WIFI`和`LWIP`开关。另外由于能力有限且缺乏调试设备,WIFI和BLE驱动运行都有问题,如果可以解决联系[timwcx@qq.com](mailto:timwcx@qq.com)。 @@ -169,7 +169,7 @@ Linux 下可以使用先前下载的 esptool 进行烧录 ![flash_download_tools](images/flash_download_tools.png) -### Linux 下进行烧录 +### Linux ```sh esptool.py -b 115200 --before default_reset --after hard_reset write_flash --flash_mode dio --flash_size detect --flash_freq 80m 0x0 path/to/your/bootloader.bin 0x08000 path/to/your/partition-table.bin 0x010000 path/to/your/rtthread.bin diff --git a/bsp/ESP32_C3/drivers/Kconfig b/bsp/ESP32_C3/drivers/Kconfig index d50ff9f7bb..e345ebeafd 100644 --- a/bsp/ESP32_C3/drivers/Kconfig +++ b/bsp/ESP32_C3/drivers/Kconfig @@ -76,6 +76,16 @@ menu "On-chip Peripheral Drivers" bool "Enable BLE" default n + config BSP_USING_HWTIMER + bool "Enable HWTIMER" + select RT_USING_HWTIMER + default n + if BSP_USING_HWTIMER + config BSP_USING_TIMER0 + bool "Enable HWTIMER0" + default n + endif + endmenu config BSP_ENABLE_GDBSTUB diff --git a/bsp/ESP32_C3/drivers/drv_hwtimer.c b/bsp/ESP32_C3/drivers/drv_hwtimer.c new file mode 100644 index 0000000000..63d33d9a91 --- /dev/null +++ b/bsp/ESP32_C3/drivers/drv_hwtimer.c @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-11-15 BetMul first version + */ + +#include "drv_hwtimer.h" + +#include +#include +#include "driver/gptimer.h" +#include "sdkconfig.h" + +#ifdef RT_USING_HWTIMER + +/** + * handle interrupt for hwtimer. + */ +static bool mcu_hwtimer_intr_handler(gptimer_handle_t gptimer, const gptimer_alarm_event_data_t *edata, void *user_ctx) +{ + rt_interrupt_enter(); + + rt_hwtimer_t *hwtimer = (rt_hwtimer_t *)user_ctx; + rt_device_hwtimer_isr(hwtimer); + + rt_interrupt_leave(); + + return 0; +} + +/** + * init the hwtimer +*/ +static void mcu_hwtimer_init(rt_hwtimer_t *timer, rt_uint32_t state) +{ + gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data; + + // let the gptimer into enable status + ESP_ERROR_CHECK(gptimer_enable(gptimer)); +} + +/** + * start the hwtimer, change status into running +*/ +static rt_err_t mcu_hwtimer_start(rt_hwtimer_t *timer, rt_uint32_t cnt, rt_hwtimer_mode_t mode) +{ + gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data; + gptimer_alarm_config_t alarm_config = { + .alarm_count = cnt, + }; + if (mode == HWTIMER_MODE_ONESHOT) + { + + } + else + { + alarm_config.reload_count = 0; + alarm_config.flags.auto_reload_on_alarm = true; + } + ESP_ERROR_CHECK(gptimer_set_alarm_action(gptimer, &alarm_config)); + ESP_ERROR_CHECK(gptimer_start(gptimer)); + + return RT_EOK; +} + +/** + * stop the hwtimer, change the status from running into enable +*/ +static void mcu_hwtimer_stop(rt_hwtimer_t *timer) +{ + gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data; + + ESP_ERROR_CHECK(gptimer_stop(gptimer)); +} + +/** + * get count +*/ +static rt_uint32_t mcu_hwtimer_count_get(rt_hwtimer_t *timer) +{ + gptimer_handle_t gptimer = (gptimer_handle_t)timer->parent.user_data; + // get count number + uint64_t value; + ESP_ERROR_CHECK(gptimer_get_raw_count(gptimer, &value)); + return (rt_uint32_t)value; +} + +/** + * control the hwtimer +*/ +static rt_err_t mcu_hwtimer_control(rt_hwtimer_t *timer, rt_uint32_t cmd, void *args) +{ + + rt_err_t err = RT_EOK; + + switch (cmd) + { + case HWTIMER_CTRL_FREQ_SET: + err = -RT_ERROR; + break; + + case HWTIMER_CTRL_INFO_GET: + err = -RT_ERROR; + break; + + case HWTIMER_CTRL_MODE_SET: + timer->mode = *(rt_uint32_t *)args; + break; + + case HWTIMER_CTRL_STOP: + mcu_hwtimer_stop(timer); + break; + } + + return err; +} + +static struct rt_hwtimer_device _hwtimer; +static const struct rt_hwtimer_ops _hwtimer_ops = + { + .init = mcu_hwtimer_init, + .start = mcu_hwtimer_start, + .stop = mcu_hwtimer_stop, + .count_get = mcu_hwtimer_count_get, + .control = mcu_hwtimer_control}; + +static const struct rt_hwtimer_info _hwtimer_info = + { + // TODO:what is the true max and min? + .maxfreq = 1000000UL, + .minfreq = 1000000UL, + .maxcnt = 0xFFFF, + .cntmode = HWTIMER_MODE_ONESHOT}; + +int rt_hw_hwtimer_init(void) +{ + + char *name = "timer0"; + + gptimer_handle_t gptimer = NULL; + gptimer_config_t timer_config = { + .clk_src = GPTIMER_CLK_SRC_DEFAULT, + .direction = GPTIMER_COUNT_UP, + .resolution_hz = 1 * 1000 * 1000, + }; + + gptimer_event_callbacks_t cbs = { + .on_alarm = mcu_hwtimer_intr_handler, + }; + + ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer)); + ESP_ERROR_CHECK(gptimer_register_event_callbacks(gptimer, &cbs, &_hwtimer)); + + _hwtimer.info = &_hwtimer_info; + _hwtimer.ops = &_hwtimer_ops; + + return rt_device_hwtimer_register(&_hwtimer, name, (void *)gptimer); + +} + +INIT_DEVICE_EXPORT(rt_hw_hwtimer_init); +#endif /* RT_USING_HWTIMER */ diff --git a/bsp/ESP32_C3/drivers/drv_hwtimer.h b/bsp/ESP32_C3/drivers/drv_hwtimer.h new file mode 100644 index 0000000000..a5a62fe003 --- /dev/null +++ b/bsp/ESP32_C3/drivers/drv_hwtimer.h @@ -0,0 +1,20 @@ +/* + * Copyright (c) 2006-2023, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-11-15 BetMul first version + */ +#ifndef __DRV_HWTIMER_H__ +#define __DRV_HWTIMER_H__ + +#include + +#ifdef RT_USING_HWTIMER +int rt_hw_hwtimer_init(void); +#endif + + +#endif /* __DRV_HWTIMER_H__ */ diff --git a/bsp/ESP32_C3/rtconfig.h b/bsp/ESP32_C3/rtconfig.h index cbeb07d78e..aa61bc4544 100644 --- a/bsp/ESP32_C3/rtconfig.h +++ b/bsp/ESP32_C3/rtconfig.h @@ -254,4 +254,4 @@ #define BSP_USING_GPIO #define BSP_USING_UART -#endif +#endif \ No newline at end of file