Skip to content

Commit 6f9ce1f

Browse files
committed
[feat] 1.modified gcc startup file;
2.add soft i2c; 3.support keil/iar;
1 parent a4c277c commit 6f9ce1f

File tree

18 files changed

+7785
-617
lines changed

18 files changed

+7785
-617
lines changed

bsp/mm32f526x/.config

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,11 @@ CONFIG_RT_SERIAL_RB_BUFSZ=64
232232
# CONFIG_RT_USING_SERIAL_BYPASS is not set
233233
# CONFIG_RT_USING_CAN is not set
234234
# CONFIG_RT_USING_CPUTIME is not set
235-
# CONFIG_RT_USING_I2C is not set
235+
CONFIG_RT_USING_I2C=y
236+
# CONFIG_RT_I2C_DEBUG is not set
237+
CONFIG_RT_USING_I2C_BITOPS=y
238+
# CONFIG_RT_I2C_BITOPS_DEBUG is not set
239+
# CONFIG_RT_USING_SOFT_I2C is not set
236240
# CONFIG_RT_USING_PHY is not set
237241
# CONFIG_RT_USING_PHY_V2 is not set
238242
# CONFIG_RT_USING_ADC is not set
@@ -1274,6 +1278,10 @@ CONFIG_BSP_USING_UART3=y
12741278
# end of UART Drivers
12751279

12761280
# CONFIG_BSP_USING_ADC is not set
1281+
CONFIG_BSP_USING_I2C1=y
1282+
CONFIG_BSP_I2C1_SCL_PIN=40
1283+
CONFIG_BSP_I2C1_SDA_PIN=39
1284+
# CONFIG_BSP_USING_I2C2 is not set
12771285

12781286
#
12791287
# Flash Drivers

bsp/mm32f526x/Libraries/MM32F526x/Source/GCC_StartAsm/startup_mm32f5260_gcc.s

Lines changed: 339 additions & 486 deletions
Large diffs are not rendered by default.

bsp/mm32f526x/drivers/Kconfig

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,36 @@ menu "Hardware Drivers Config"
3737
default n
3838
endif
3939

40+
menuconfig BSP_USING_I2C1
41+
bool "Enable I2C1 BUS (software simulation)"
42+
default n
43+
select RT_USING_I2C
44+
select RT_USING_I2C_BITOPS
45+
select RT_USING_PIN
46+
if BSP_USING_I2C1
47+
config BSP_I2C1_SCL_PIN
48+
int "i2c1 scl pin number"
49+
default 40
50+
config BSP_I2C1_SDA_PIN
51+
int "I2C1 sda pin number"
52+
default 39
53+
endif
54+
55+
menuconfig BSP_USING_I2C2
56+
bool "Enable I2C2 BUS (software simulation)"
57+
default n
58+
select RT_USING_I2C
59+
select RT_USING_I2C_BITOPS
60+
select RT_USING_PIN
61+
if BSP_USING_I2C2
62+
config BSP_I2C2_SCL_PIN
63+
int "i2c2 scl pin number"
64+
default 10
65+
config BSP_I2C2_SDA_PIN
66+
int "I2C2 sda pin number"
67+
default 12
68+
endif
69+
4070
menu "Flash Drivers"
4171
config BSP_USING_OCFLASH
4272
bool "Enable On Chip Flash"

bsp/mm32f526x/drivers/SConscript

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ if GetDepend(['BSP_USING_GPIO']):
2424
if GetDepend(['BSP_USING_ADC']):
2525
src += ['drv_adc.c']
2626

27+
# add soft i2c driver code
28+
if GetDepend(['RT_USING_I2C']) or GetDepend(['RT_USING_I2C_BITOPS']):
29+
src += ['drv_soft_i2c.c']
30+
2731
# add flash driver code
2832
if GetDepend(['BSP_USING_OCFLASH']):
2933
src += ['drv_flash.c']

bsp/mm32f526x/drivers/board.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
#include "hal_device.h"
1515
#include "mm32_device.h"
1616

17-
#define SRAM_SIZE 0x20000
17+
#define SRAM_SIZE 0x1C000
1818

1919
#define SRAM_END (SRAM_BASE + SRAM_SIZE)
2020
#ifdef __CC_ARM
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
/*
2+
* Copyright (c) 2024, Chasel
3+
*
4+
* Change Logs:
5+
* Date Author Notes
6+
* 2025-01-06 Chasel first commit
7+
*/
8+
9+
#include "drv_soft_i2c.h"
10+
11+
#if defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2)
12+
13+
14+
static const struct mm32_soft_i2c_config soft_i2c_config[] =
15+
{
16+
#ifdef BSP_USING_I2C1
17+
I2C1_BUS_CONFIG,
18+
#endif
19+
#ifdef BSP_USING_I2C2
20+
I2C2_BUS_CONFIG,
21+
#endif
22+
};
23+
24+
static struct mm32_i2c i2c_obj[sizeof(soft_i2c_config) / sizeof(soft_i2c_config[0])];
25+
26+
/**
27+
* This function initializes the i2c pin.
28+
*
29+
* @param mm32 i2c dirver class.
30+
*/
31+
static void mm32_i2c_gpio_init(struct mm32_i2c *i2c)
32+
{
33+
struct mm32_soft_i2c_config* cfg = (struct mm32_soft_i2c_config*)i2c->ops.data;
34+
35+
rt_pin_mode(cfg->scl, PIN_MODE_OUTPUT_OD);
36+
rt_pin_mode(cfg->sda, PIN_MODE_OUTPUT_OD);
37+
38+
rt_pin_write(cfg->scl, PIN_HIGH);
39+
rt_pin_write(cfg->sda, PIN_HIGH);
40+
}
41+
42+
static void mm32_i2c_pin_init(void)
43+
{
44+
rt_size_t obj_num = sizeof(i2c_obj) / sizeof(struct mm32_i2c);
45+
46+
for(rt_size_t i = 0; i < obj_num; i++)
47+
{
48+
mm32_i2c_gpio_init(&i2c_obj[i]);
49+
}
50+
}
51+
52+
/**
53+
* This function sets the sda pin.
54+
*
55+
* @param mm32 config class.
56+
* @param The sda pin state.
57+
*/
58+
static void mm32_set_sda(void *data, rt_int32_t state)
59+
{
60+
struct mm32_soft_i2c_config* cfg = (struct mm32_soft_i2c_config*)data;
61+
if (state)
62+
{
63+
rt_pin_write(cfg->sda, PIN_HIGH);
64+
}
65+
else
66+
{
67+
rt_pin_write(cfg->sda, PIN_LOW);
68+
}
69+
}
70+
71+
/**
72+
* This function sets the scl pin.
73+
*
74+
* @param mm32 config class.
75+
* @param The scl pin state.
76+
*/
77+
static void mm32_set_scl(void *data, rt_int32_t state)
78+
{
79+
struct mm32_soft_i2c_config* cfg = (struct mm32_soft_i2c_config*)data;
80+
if (state)
81+
{
82+
rt_pin_write(cfg->scl, PIN_HIGH);
83+
}
84+
else
85+
{
86+
rt_pin_write(cfg->scl, PIN_LOW);
87+
}
88+
}
89+
90+
/**
91+
* This function gets the sda pin state.
92+
*
93+
* @param The sda pin state.
94+
*/
95+
static rt_int32_t mm32_get_sda(void *data)
96+
{
97+
struct mm32_soft_i2c_config* cfg = (struct mm32_soft_i2c_config*)data;
98+
return rt_pin_read(cfg->sda);
99+
}
100+
101+
/**
102+
* This function gets the scl pin state.
103+
*
104+
* @param The scl pin state.
105+
*/
106+
static rt_int32_t mm32_get_scl(void *data)
107+
{
108+
struct mm32_soft_i2c_config* cfg = (struct mm32_soft_i2c_config*)data;
109+
return rt_pin_read(cfg->scl);
110+
}
111+
112+
static const struct rt_i2c_bit_ops mm32_bit_ops_default =
113+
{
114+
.data = RT_NULL,
115+
.pin_init = mm32_i2c_pin_init,
116+
.set_sda = mm32_set_sda,
117+
.set_scl = mm32_set_scl,
118+
.get_sda = mm32_get_sda,
119+
.get_scl = mm32_get_scl,
120+
.udelay = rt_hw_us_delay,
121+
.delay_us = 1,
122+
.timeout = 100,
123+
.i2c_pin_init_flag = RT_FALSE
124+
};
125+
126+
/**
127+
* if i2c is locked, this function will unlock it
128+
*
129+
* @param mm32 config class
130+
*
131+
* @return RT_EOK indicates successful unlock.
132+
*/
133+
static rt_err_t mm32_i2c_bus_unlock(const struct mm32_soft_i2c_config *cfg)
134+
{
135+
rt_int32_t i = 0;
136+
137+
if (PIN_LOW == rt_pin_read(cfg->sda))
138+
{
139+
while (i++ < 9)
140+
{
141+
rt_pin_write(cfg->scl, PIN_HIGH);
142+
rt_hw_us_delay(100);
143+
rt_pin_write(cfg->scl, PIN_LOW);
144+
rt_hw_us_delay(100);
145+
}
146+
}
147+
if (PIN_LOW == rt_pin_read(cfg->sda))
148+
{
149+
return -RT_ERROR;
150+
}
151+
152+
return RT_EOK;
153+
}
154+
155+
/* I2C initialization function */
156+
int rt_hw_i2c_init(void)
157+
{
158+
rt_err_t result;
159+
160+
for (rt_size_t i = 0; i < sizeof(i2c_obj) / sizeof(struct mm32_i2c); i++)
161+
{
162+
i2c_obj[i].ops = mm32_bit_ops_default;
163+
i2c_obj[i].ops.data = (void*)&soft_i2c_config[i];
164+
i2c_obj[i].i2c_bus.priv = &i2c_obj[i].ops;
165+
result = rt_i2c_bit_add_bus(&i2c_obj[i].i2c_bus, soft_i2c_config[i].bus_name);
166+
RT_ASSERT(result == RT_EOK);
167+
mm32_i2c_bus_unlock(&soft_i2c_config[i]);
168+
}
169+
170+
return RT_EOK;
171+
}
172+
INIT_BOARD_EXPORT(rt_hw_i2c_init);
173+
174+
#endif /* defined(BSP_USING_I2C1) || defined(BSP_USING_I2C2) */
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright (c) 2025, Chasel
3+
*
4+
* Change Logs:
5+
* Date Author Notes
6+
* 2025-01-06 Chasel first version
7+
*/
8+
9+
#ifndef __DRV_I2C__
10+
#define __DRV_I2C__
11+
12+
#include <rtthread.h>
13+
#include <rthw.h>
14+
#include <rtdevice.h>
15+
16+
/* mm32 config class */
17+
struct mm32_soft_i2c_config
18+
{
19+
rt_uint8_t scl;
20+
rt_uint8_t sda;
21+
const char *bus_name;
22+
};
23+
/* mm32 i2c dirver class */
24+
struct mm32_i2c
25+
{
26+
struct rt_i2c_bit_ops ops;
27+
struct rt_i2c_bus_device i2c_bus;
28+
};
29+
30+
#ifdef BSP_USING_I2C1
31+
#define I2C1_BUS_CONFIG \
32+
{ \
33+
.scl = BSP_I2C1_SCL_PIN, \
34+
.sda = BSP_I2C1_SDA_PIN, \
35+
.bus_name = "i2c1", \
36+
}
37+
#endif
38+
39+
#ifdef BSP_USING_I2C2
40+
#define I2C2_BUS_CONFIG \
41+
{ \
42+
.scl = BSP_I2C2_SCL_PIN, \
43+
.sda = BSP_I2C2_SDA_PIN, \
44+
.bus_name = "i2c2", \
45+
}
46+
#endif
47+
48+
int rt_hw_i2c_init(void);
49+
50+
#endif

0 commit comments

Comments
 (0)