Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bsp][ESP32C3] support soft i2c and rt_hw_us_delay #9751

Merged
merged 19 commits into from
Dec 10, 2024
12 changes: 11 additions & 1 deletion bsp/ESP32_C3/.ci/attachconfig/ci.attachconfig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,17 @@ devices.i2c:
kconfig:
- CONFIG_RT_USING_I2C=y
- CONFIG_BSP_USING_I2C=y
- CONFIG_BSP_USING_I2C0=y
devices.hwi2c:
depends:
- devices.i2c
kconfig:
- CONFIG_BSP_USING_HW_I2C=y
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么这里选HWI2c?

Copy link
Contributor Author

@1078249029 1078249029 Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

为什么这里选HWI2c?

之前在 #9643 (comment) 问过模块冲突是否分别写ci,但是一起写是允许的,既然这样那就把软件硬件i2c都测一遍吧

Copy link
Member

@supperthomas supperthomas Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

分开写比较合理一些。检查一下ci是否测试到该swi2c.c和hw_i2c文件

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果确实应用中需要组合场景,可以再加个配置

Copy link
Member

@supperthomas supperthomas Dec 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里缺个CONFIG_BSP_USING_I2C配置。

devices.swi2c:
depends:
- devices.i2c
kconfig:
- CONFIG_BSP_USING_SW_I2C=y
- CONFIG_BSP_USING_SW_I2C0=y
devices.spi:
kconfig:
- CONFIG_RT_USING_SPI=y
Expand Down
23 changes: 21 additions & 2 deletions bsp/ESP32_C3/drivers/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,28 @@ menu "On-chip Peripheral Drivers"
default n
select RT_USING_I2C
if BSP_USING_I2C
config BSP_USING_I2C0
bool "Enable I2C0"
menuconfig BSP_USING_HW_I2C
bool "Enable HardWare I2C"
default n

menuconfig BSP_USING_SW_I2C
bool "Enable SoftWare I2C"
default n
if BSP_USING_SW_I2C
config BSP_USING_SW_I2C0
bool "Enable SoftWare I2C0"
default n
if BSP_USING_SW_I2C0
config BSP_SW_I2C0_SDA_PIN
int "SWI2C0 sda pin number"
range 0 21
default 18
config BSP_SW_I2C0_SCL_PIN
int "SWI2C0 scl pin number"
range 0 21
default 19
endif
endif
endif

menuconfig BSP_USING_SPI
Expand Down
3 changes: 3 additions & 0 deletions bsp/ESP32_C3/drivers/SConscript
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ if GetDepend('BSP_USING_ADC'):

if GetDepend('BSP_USING_I2C'):
Copy link
Member

@supperthomas supperthomas Dec 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里BSP USING I2C有kconfig对应吗?我看到上面BSP_USING_I2C0 已经删掉了。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里BSP USING I2C有kconfig对应吗?我看到上面BSP_USING_I2C0 已经删掉了。

有的

menuconfig BSP_USING_I2C
        bool "Enable I2C"
        default n
        select RT_USING_I2C
        if BSP_USING_I2C
            config BSP_USING_HW_I2C
                bool "Enable HardWare I2C"
                default n

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

那选择BSP_USING_I2C 默认添加drv_hw_i2c.c ?

src += ['drv_hw_i2c.c']

if GetDepend('BSP_USING_SW_I2C'):
src += ['drv_sw_i2c.c']

if GetDepend('BSP_USING_PWM'):
src += ['drv_pwm.c']
Expand Down
34 changes: 33 additions & 1 deletion bsp/ESP32_C3/drivers/board.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
/*
* Copyright (c) 2021-2022, RT-Thread Development Team
* Copyright (c) 2021-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-06-02 supperthomas first version
* 2024-12-08 wumingzi support rt_hw_us_delay
*/

#include <rtthread.h>
#include "rttypes.h"
#include "hal/systimer_hal.h"
#include "hal/systimer_ll.h"
#include "esp_private/panic_internal.h"
#include "esp_private/systimer.h"
#include "esp_private/periph_ctrl.h"
#include "esp_intr_alloc.h"
#include "esp_attr.h"
#include "esp_timer.h"
#include "driver/gptimer.h"

static systimer_hal_context_t systimer_hal;
IRAM_ATTR void rt_SysTickIsrHandler(void *arg)
Expand Down Expand Up @@ -57,3 +61,31 @@ void rt_hw_board_init(void)
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
#endif
}

static gptimer_handle_t gptimer_hw_us = NULL;

static int delay_us_init(void)
{
gptimer_config_t timer_config = {
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
.direction = GPTIMER_COUNT_UP,
.resolution_hz = 1 * 1000 * 1000, /* 1MHz, 1 tick = 1us*/
};
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer_hw_us));
ESP_ERROR_CHECK(gptimer_enable(gptimer_hw_us));
return RT_EOK;
}
INIT_DEVICE_EXPORT(delay_us_init);

void rt_hw_us_delay(rt_uint32_t us)
{
uint64_t count = 0;
ESP_ERROR_CHECK(gptimer_start(gptimer_hw_us));
ESP_ERROR_CHECK(gptimer_set_raw_count(gptimer_hw_us, 0));
/* Retrieve the timestamp at anytime*/
while(count < (uint64_t)us)
{
ESP_ERROR_CHECK(gptimer_get_raw_count(gptimer_hw_us, &count));
}
ESP_ERROR_CHECK(gptimer_stop(gptimer_hw_us));
}
10 changes: 5 additions & 5 deletions bsp/ESP32_C3/drivers/board.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
/*
* Copyright (c) 2021-2022, RT-Thread Development Team
* Copyright (c) 2021-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-06-02 supperthomas first version
* 2024-12-08 wumingzi support rt_hw_us_delay
*/

#ifndef __BOARD_H__
#define __BOARD_H__

#include <rtconfig.h>
#include "rttypes.h"

#ifdef __cplusplus
extern "C" {
#endif

void rt_hw_board_init(void);
void rt_hw_us_delay(rt_uint32_t us);

#ifdef __cplusplus
}
#endif

#endif

#endif
15 changes: 10 additions & 5 deletions bsp/ESP32_C3/drivers/drv_gpio.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/*
* Copyright (c) 2021-2022, RT-Thread Development Team
* Copyright (c) 2021-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2022-06-03 supperthomas first version
*
* 2024-12-08 wumingzi support open drain mode for soft i2c
*/

#include <rtthread.h>
Expand Down Expand Up @@ -37,9 +37,14 @@ static void mcu_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
io_conf.pull_down_en = 0;
io_conf.pull_up_en = 0;
gpio_config(&io_conf);
if (mode == PIN_MODE_OUTPUT)
switch (mode)
{
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
case PIN_MODE_INPUT:
gpio_set_direction(pin, GPIO_MODE_INPUT);
case PIN_MODE_OUTPUT:
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
case PIN_MODE_OUTPUT_OD:
gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
}
/*TODO:set gpio out put mode */
}
Expand Down Expand Up @@ -84,4 +89,4 @@ int rt_hw_pin_init(void)
}
INIT_BOARD_EXPORT(rt_hw_pin_init);

#endif /* RT_USING_PIN */
#endif /* RT_USING_PIN */
4 changes: 2 additions & 2 deletions bsp/ESP32_C3/drivers/drv_gpio.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, RT-Thread Development Team
* Copyright (c) 2021-2024 RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
Expand All @@ -18,4 +18,4 @@
int rt_hw_pin_init(void);
#endif

#endif /* __DRV_GPIO_H__ */
#endif /* __DRV_GPIO_H__ */
4 changes: 2 additions & 2 deletions bsp/ESP32_C3/drivers/drv_hw_i2c.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
* Date Author Notes
*2023-03-28 Zxy first version
*/
#ifdef RT_USING_I2C

#include "drv_hw_i2c.h"
#include "driver/i2c.h"//bsp/ESP32_C3/packages/ESP-IDF-latest/components/driver/include/driver/i2c.h
#include "hal/i2c_types.h"//bsp/ESP32_C3/packages/ESP-IDF-latest/tools/mocks/hal/include/hal/i2c_types.h
#include "esp_err.h"
//#include "portmacro.h"//bsp/ESP32_C3/packages/FreeRTOS_Wrapper-latest/FreeRTOS/portable/rt-thread/portmacro.h

#ifdef BSP_USING_HW_I2C
struct esp32_i2c
{
struct rt_i2c_bus_device bus;
Expand Down
Loading
Loading