Skip to content

Commit 3e36267

Browse files
committed
硬件i2c注册修改为静态分配
1 parent d8ed69f commit 3e36267

2 files changed

Lines changed: 103 additions & 28 deletions

File tree

libraries/HAL_Drivers/SConscript

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ if GetDepend(['RT_USING_I2C', 'RT_USING_I2C_BITOPS']):
2727
src += ['drv_soft_i2c.c']
2828

2929
if GetDepend(['RT_USING_I2C']):
30-
if GetDepend('BSP_USING_HW_I2C0') or GetDepend('BSP_USING_HW_I2C4') or GetDepend('BSP_USING_HW_I2C6'):
30+
if GetDepend('BSP_USING_HW_I2C0') or GetDepend('BSP_USING_HW_I2C3') or GetDepend('BSP_USING_HW_I2C4') or GetDepend('BSP_USING_HW_I2C5') or GetDepend('BSP_USING_HW_I2C6') or GetDepend('BSP_USING_HW_I2C8'):
3131
src += ['drv_i2c.c']
3232

3333
if GetDepend(['BSP_USING_SDIO1']):

libraries/HAL_Drivers/drv_i2c.c

Lines changed: 102 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,24 @@
1111
#include "board.h"
1212

1313
#if defined(RT_USING_I2C)
14-
#if defined(BSP_USING_HW_I2C0) || defined(BSP_USING_HW_I2C3) || defined(BSP_USING_HW_I2C4) || defined(BSP_USING_HW_I2C6)
1514
#include <rtdevice.h>
1615

16+
#include "cy_scb_i2c.h"
17+
#include "mtb_hal_i2c.h"
18+
19+
#ifdef BSP_USING_HW_I2C0
20+
extern const cy_stc_scb_i2c_config_t CYBSP_I2C_CONTROLLER_config;
21+
extern const mtb_hal_i2c_configurator_t CYBSP_I2C_CONTROLLER_hal_config;
22+
#endif
23+
#ifdef BSP_USING_HW_I2C5
24+
extern const cy_stc_scb_i2c_config_t CYBSP_I2C5_CONTROLLER_config;
25+
extern const mtb_hal_i2c_configurator_t CYBSP_I2C5_CONTROLLER_hal_config;
26+
#endif
27+
#ifdef BSP_USING_HW_I2C8
28+
extern const cy_stc_scb_i2c_config_t scb_8_config;
29+
extern const mtb_hal_i2c_configurator_t scb_8_hal_config;
30+
#endif
31+
1732
#ifndef I2C0_CONFIG
1833
#define I2C0_CONFIG \
1934
{ \
@@ -24,20 +39,68 @@
2439
}
2540
#endif /* I2C0_CONFIG */
2641

27-
#endif
42+
#ifndef I2C3_CONFIG
43+
#define I2C3_CONFIG \
44+
{ \
45+
.name = "i2c3", \
46+
.base = SCB3, \
47+
.cy_stc_scb_i2c_config = RT_NULL, \
48+
.mtb_hal_i2c_configurator = RT_NULL, \
49+
}
50+
#endif /* I2C3_CONFIG */
51+
52+
#ifndef I2C4_CONFIG
53+
#define I2C4_CONFIG \
54+
{ \
55+
.name = "i2c4", \
56+
.base = SCB4, \
57+
.cy_stc_scb_i2c_config = RT_NULL, \
58+
.mtb_hal_i2c_configurator = RT_NULL, \
59+
}
60+
#endif /* I2C4_CONFIG */
61+
62+
#ifndef I2C5_CONFIG
63+
#define I2C5_CONFIG \
64+
{ \
65+
.name = "i2c5", \
66+
.base = SCB5, \
67+
.cy_stc_scb_i2c_config = &CYBSP_I2C5_CONTROLLER_config, \
68+
.mtb_hal_i2c_configurator = &CYBSP_I2C5_CONTROLLER_hal_config, \
69+
}
70+
#endif /* I2C5_CONFIG */
71+
72+
#ifndef I2C6_CONFIG
73+
#define I2C6_CONFIG \
74+
{ \
75+
.name = "i2c6", \
76+
.base = SCB6, \
77+
.cy_stc_scb_i2c_config = RT_NULL, \
78+
.mtb_hal_i2c_configurator = RT_NULL, \
79+
}
80+
#endif /* I2C6_CONFIG */
81+
82+
#ifndef I2C8_CONFIG
83+
#define I2C8_CONFIG \
84+
{ \
85+
.name = "i2c8", \
86+
.base = SCB8, \
87+
.cy_stc_scb_i2c_config = &scb_8_config, \
88+
.mtb_hal_i2c_configurator = &scb_8_hal_config, \
89+
}
90+
#endif /* I2C8_CONFIG */
2891

2992
struct ifx_i2c
3093
{
31-
mtb_hal_i2c_t *mtb_hal_i2c;
32-
char *name;
94+
const char *name;
3395
CySCB_Type *base;
3496
const cy_stc_scb_i2c_config_t *cy_stc_scb_i2c_config;
3597
const mtb_hal_i2c_configurator_t *mtb_hal_i2c_configurator;
36-
cy_stc_scb_i2c_context_t *context;
98+
mtb_hal_i2c_t hal_obj;
99+
cy_stc_scb_i2c_context_t context;
37100
struct rt_i2c_bus_device i2c_bus;
38101
};
39102

40-
static struct ifx_i2c ifx_i2c[] =
103+
static struct ifx_i2c i2c_objs[] =
41104
{
42105
#ifdef BSP_USING_HW_I2C0
43106
I2C0_CONFIG,
@@ -51,16 +114,24 @@ static struct ifx_i2c ifx_i2c[] =
51114
I2C4_CONFIG,
52115
#endif
53116

117+
#ifdef BSP_USING_HW_I2C5
118+
I2C5_CONFIG,
119+
#endif
120+
54121
#ifdef BSP_USING_HW_I2C6
55122
I2C6_CONFIG,
56123
#endif
124+
125+
#ifdef BSP_USING_HW_I2C8
126+
I2C8_CONFIG,
127+
#endif
57128
};
58129

59-
static struct ifx_i2c i2c_objs[sizeof(ifx_i2c) / sizeof(struct ifx_i2c)] = {0};
130+
#define I2C_BUS_NUM (sizeof(i2c_objs) / sizeof(i2c_objs[0]))
60131

61132
static int ifx_i2c_read(struct ifx_i2c *hi2c, rt_uint16_t slave_address, rt_uint8_t *p_buffer, rt_uint16_t data_byte)
62133
{
63-
if (mtb_hal_i2c_controller_read(hi2c->mtb_hal_i2c, slave_address, p_buffer, data_byte, 10, true) != RT_EOK)
134+
if (mtb_hal_i2c_controller_read(&hi2c->hal_obj, slave_address, p_buffer, data_byte, 10, true) != CY_RSLT_SUCCESS)
64135
{
65136
return -RT_ERROR;
66137
}
@@ -70,7 +141,7 @@ static int ifx_i2c_read(struct ifx_i2c *hi2c, rt_uint16_t slave_address, rt_uint
70141

71142
static int ifx_i2c_write(struct ifx_i2c *hi2c, uint16_t slave_address, uint8_t *p_buffer, uint16_t data_byte)
72143
{
73-
if (mtb_hal_i2c_controller_write(hi2c->mtb_hal_i2c, slave_address, p_buffer, data_byte, 10, true) != RT_EOK)
144+
if (mtb_hal_i2c_controller_write(&hi2c->hal_obj, slave_address, p_buffer, data_byte, 10, true) != CY_RSLT_SUCCESS)
74145
{
75146
return -RT_ERROR;
76147
}
@@ -121,33 +192,35 @@ static const struct rt_i2c_bus_device_ops i2c_ops =
121192
RT_NULL
122193
};
123194

124-
void HAL_I2C_Init(struct ifx_i2c *obj)
195+
static rt_err_t ifx_i2c_hw_init(struct ifx_i2c *obj)
125196
{
126197
RT_ASSERT(obj != RT_NULL);
127-
RT_ASSERT(obj->mtb_hal_i2c != RT_NULL);
128-
RT_ASSERT(obj->mtb_hal_i2c_configurator != RT_NULL);
129-
RT_ASSERT(obj->context != RT_NULL);
198+
199+
if ((obj->mtb_hal_i2c_configurator == RT_NULL) || (obj->cy_stc_scb_i2c_config == RT_NULL))
200+
{
201+
rt_kprintf("I2C %s config is missing, skip register\n", obj->name);
202+
return -RT_ERROR;
203+
}
130204

131205
cy_rslt_t rslt;
132206
cy_en_scb_i2c_status_t result;
133207

134-
result = Cy_SCB_I2C_Init(obj->base, obj->cy_stc_scb_i2c_config, obj->context);
208+
result = Cy_SCB_I2C_Init(obj->base, obj->cy_stc_scb_i2c_config, &obj->context);
135209
if (result != CY_SCB_I2C_SUCCESS)
136210
{
137211
rt_kprintf("Cy_SCB_I2C_Init failed for %s, code: 0x%08x\n", obj->name, result);
138-
return;
212+
return -RT_ERROR;
139213
}
140214

141215
Cy_SCB_I2C_Enable(obj->base);
142216

143-
rslt = mtb_hal_i2c_setup(obj->mtb_hal_i2c, obj->mtb_hal_i2c_configurator, obj->context, NULL);
217+
rslt = mtb_hal_i2c_setup(&obj->hal_obj, obj->mtb_hal_i2c_configurator, &obj->context, NULL);
144218
if (rslt != CY_RSLT_SUCCESS)
145219
{
146220
rt_kprintf("I2C setup failed for %s, code: 0x%08x\n", obj->name, rslt);
147-
return;
221+
return -RT_ERROR;
148222
}
149223

150-
// Define the I2C controller dev_config_struct structure
151224
mtb_hal_i2c_cfg_t i2c_controller_config =
152225
{
153226
MTB_HAL_I2C_MODE_CONTROLLER,
@@ -157,28 +230,30 @@ void HAL_I2C_Init(struct ifx_i2c *obj)
157230
false,
158231
};
159232

160-
rslt = mtb_hal_i2c_configure(obj->mtb_hal_i2c, &i2c_controller_config);
233+
rslt = mtb_hal_i2c_configure(&obj->hal_obj, &i2c_controller_config);
161234
if (rslt != CY_RSLT_SUCCESS)
162235
{
163236
rt_kprintf("I2C configure failed for %s, code: 0x%08x\n", obj->name, rslt);
164-
return;
237+
return -RT_ERROR;
165238
}
239+
240+
return RT_EOK;
166241
}
167242

168243
int rt_hw_i2c_init(void)
169244
{
170245
rt_err_t result = RT_EOK;
171-
size_t i2c_num = sizeof(ifx_i2c) / sizeof(struct ifx_i2c);
246+
size_t i2c_num = I2C_BUS_NUM;
172247

173248
for (size_t i = 0; i < i2c_num; i++)
174249
{
175-
i2c_objs[i] = ifx_i2c[i];
176250
i2c_objs[i].i2c_bus.ops = &i2c_ops;
177-
i2c_objs[i].context = rt_malloc(sizeof(cy_stc_scb_i2c_context_t));
178-
RT_ASSERT(i2c_objs[i].context != RT_NULL);
179-
i2c_objs[i].mtb_hal_i2c = rt_malloc(sizeof(mtb_hal_i2c_t));
180-
RT_ASSERT(i2c_objs[i].mtb_hal_i2c != RT_NULL);
181-
HAL_I2C_Init(&i2c_objs[i]);
251+
252+
if (ifx_i2c_hw_init(&i2c_objs[i]) != RT_EOK)
253+
{
254+
continue;
255+
}
256+
182257
result = rt_i2c_bus_device_register(&i2c_objs[i].i2c_bus, i2c_objs[i].name);
183258
RT_ASSERT(result == RT_EOK);
184259
}

0 commit comments

Comments
 (0)