Skip to content

Commit 876f2be

Browse files
authored
[bsp][ESP32C3] support soft i2c and rt_hw_us_delay
1 parent 5b75e1c commit 876f2be

File tree

10 files changed

+320
-18
lines changed

10 files changed

+320
-18
lines changed

bsp/ESP32_C3/.ci/attachconfig/ci.attachconfig.yml

+11-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,17 @@ devices.i2c:
1515
kconfig:
1616
- CONFIG_RT_USING_I2C=y
1717
- CONFIG_BSP_USING_I2C=y
18-
- CONFIG_BSP_USING_I2C0=y
18+
devices.hwi2c:
19+
depends:
20+
- devices.i2c
21+
kconfig:
22+
- CONFIG_BSP_USING_HW_I2C=y
23+
devices.swi2c:
24+
depends:
25+
- devices.i2c
26+
kconfig:
27+
- CONFIG_BSP_USING_SW_I2C=y
28+
- CONFIG_BSP_USING_SW_I2C0=y
1929
devices.spi:
2030
kconfig:
2131
- CONFIG_RT_USING_SPI=y

bsp/ESP32_C3/drivers/Kconfig

+21-2
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,28 @@ menu "On-chip Peripheral Drivers"
7272
default n
7373
select RT_USING_I2C
7474
if BSP_USING_I2C
75-
config BSP_USING_I2C0
76-
bool "Enable I2C0"
75+
menuconfig BSP_USING_HW_I2C
76+
bool "Enable HardWare I2C"
7777
default n
78+
79+
menuconfig BSP_USING_SW_I2C
80+
bool "Enable SoftWare I2C"
81+
default n
82+
if BSP_USING_SW_I2C
83+
config BSP_USING_SW_I2C0
84+
bool "Enable SoftWare I2C0"
85+
default n
86+
if BSP_USING_SW_I2C0
87+
config BSP_SW_I2C0_SDA_PIN
88+
int "SWI2C0 sda pin number"
89+
range 0 21
90+
default 18
91+
config BSP_SW_I2C0_SCL_PIN
92+
int "SWI2C0 scl pin number"
93+
range 0 21
94+
default 19
95+
endif
96+
endif
7897
endif
7998

8099
menuconfig BSP_USING_SPI

bsp/ESP32_C3/drivers/SConscript

+3
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ if GetDepend('BSP_USING_ADC'):
1717

1818
if GetDepend('BSP_USING_I2C'):
1919
src += ['drv_hw_i2c.c']
20+
21+
if GetDepend('BSP_USING_SW_I2C'):
22+
src += ['drv_sw_i2c.c']
2023

2124
if GetDepend('BSP_USING_PWM'):
2225
src += ['drv_pwm.c']

bsp/ESP32_C3/drivers/board.c

+33-1
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,25 @@
11
/*
2-
* Copyright (c) 2021-2022, RT-Thread Development Team
2+
* Copyright (c) 2021-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
88
* 2022-06-02 supperthomas first version
9+
* 2024-12-08 wumingzi support rt_hw_us_delay
910
*/
1011

1112
#include <rtthread.h>
13+
#include "rttypes.h"
1214
#include "hal/systimer_hal.h"
1315
#include "hal/systimer_ll.h"
1416
#include "esp_private/panic_internal.h"
1517
#include "esp_private/systimer.h"
1618
#include "esp_private/periph_ctrl.h"
1719
#include "esp_intr_alloc.h"
1820
#include "esp_attr.h"
21+
#include "esp_timer.h"
22+
#include "driver/gptimer.h"
1923

2024
static systimer_hal_context_t systimer_hal;
2125
IRAM_ATTR void rt_SysTickIsrHandler(void *arg)
@@ -57,3 +61,31 @@ void rt_hw_board_init(void)
5761
rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
5862
#endif
5963
}
64+
65+
static gptimer_handle_t gptimer_hw_us = NULL;
66+
67+
static int delay_us_init(void)
68+
{
69+
gptimer_config_t timer_config = {
70+
.clk_src = GPTIMER_CLK_SRC_DEFAULT,
71+
.direction = GPTIMER_COUNT_UP,
72+
.resolution_hz = 1 * 1000 * 1000, /* 1MHz, 1 tick = 1us*/
73+
};
74+
ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &gptimer_hw_us));
75+
ESP_ERROR_CHECK(gptimer_enable(gptimer_hw_us));
76+
return RT_EOK;
77+
}
78+
INIT_DEVICE_EXPORT(delay_us_init);
79+
80+
void rt_hw_us_delay(rt_uint32_t us)
81+
{
82+
uint64_t count = 0;
83+
ESP_ERROR_CHECK(gptimer_start(gptimer_hw_us));
84+
ESP_ERROR_CHECK(gptimer_set_raw_count(gptimer_hw_us, 0));
85+
/* Retrieve the timestamp at anytime*/
86+
while(count < (uint64_t)us)
87+
{
88+
ESP_ERROR_CHECK(gptimer_get_raw_count(gptimer_hw_us, &count));
89+
}
90+
ESP_ERROR_CHECK(gptimer_stop(gptimer_hw_us));
91+
}

bsp/ESP32_C3/drivers/board.h

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,27 @@
11
/*
2-
* Copyright (c) 2021-2022, RT-Thread Development Team
2+
* Copyright (c) 2021-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
88
* 2022-06-02 supperthomas first version
9+
* 2024-12-08 wumingzi support rt_hw_us_delay
910
*/
1011

1112
#ifndef __BOARD_H__
1213
#define __BOARD_H__
1314

1415
#include <rtconfig.h>
16+
#include "rttypes.h"
1517

1618
#ifdef __cplusplus
1719
extern "C" {
1820
#endif
19-
2021
void rt_hw_board_init(void);
22+
void rt_hw_us_delay(rt_uint32_t us);
2123

2224
#ifdef __cplusplus
2325
}
2426
#endif
25-
26-
#endif
27-
27+
#endif

bsp/ESP32_C3/drivers/drv_gpio.c

+10-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
/*
2-
* Copyright (c) 2021-2022, RT-Thread Development Team
2+
* Copyright (c) 2021-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
66
* Change Logs:
77
* Date Author Notes
88
* 2022-06-03 supperthomas first version
9-
*
9+
* 2024-12-08 wumingzi support open drain mode for soft i2c
1010
*/
1111

1212
#include <rtthread.h>
@@ -37,9 +37,14 @@ static void mcu_pin_mode(rt_device_t dev, rt_base_t pin, rt_uint8_t mode)
3737
io_conf.pull_down_en = 0;
3838
io_conf.pull_up_en = 0;
3939
gpio_config(&io_conf);
40-
if (mode == PIN_MODE_OUTPUT)
40+
switch (mode)
4141
{
42-
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
42+
case PIN_MODE_INPUT:
43+
gpio_set_direction(pin, GPIO_MODE_INPUT);
44+
case PIN_MODE_OUTPUT:
45+
gpio_set_direction(pin, GPIO_MODE_OUTPUT);
46+
case PIN_MODE_OUTPUT_OD:
47+
gpio_set_direction(pin, GPIO_MODE_INPUT_OUTPUT_OD);
4348
}
4449
/*TODO:set gpio out put mode */
4550
}
@@ -84,4 +89,4 @@ int rt_hw_pin_init(void)
8489
}
8590
INIT_BOARD_EXPORT(rt_hw_pin_init);
8691

87-
#endif /* RT_USING_PIN */
92+
#endif /* RT_USING_PIN */

bsp/ESP32_C3/drivers/drv_gpio.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021-2022, RT-Thread Development Team
2+
* Copyright (c) 2021-2024 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -18,4 +18,4 @@
1818
int rt_hw_pin_init(void);
1919
#endif
2020

21-
#endif /* __DRV_GPIO_H__ */
21+
#endif /* __DRV_GPIO_H__ */

bsp/ESP32_C3/drivers/drv_hw_i2c.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77
* Date Author Notes
88
*2023-03-28 Zxy first version
99
*/
10-
#ifdef RT_USING_I2C
10+
1111
#include "drv_hw_i2c.h"
1212
#include "driver/i2c.h"//bsp/ESP32_C3/packages/ESP-IDF-latest/components/driver/include/driver/i2c.h
1313
#include "hal/i2c_types.h"//bsp/ESP32_C3/packages/ESP-IDF-latest/tools/mocks/hal/include/hal/i2c_types.h
1414
#include "esp_err.h"
1515
//#include "portmacro.h"//bsp/ESP32_C3/packages/FreeRTOS_Wrapper-latest/FreeRTOS/portable/rt-thread/portmacro.h
16-
16+
#ifdef BSP_USING_HW_I2C
1717
struct esp32_i2c
1818
{
1919
struct rt_i2c_bus_device bus;

0 commit comments

Comments
 (0)