Skip to content

Commit fde369f

Browse files
[[renesas/drivers] Fix error code issues 6183.Add hardware i2c driver. (#6279)
* [renesas/ra2l1-cpk] add Captouch板载触摸按键配置说明.md * Update Captouch板载触摸按键配置说明.md * [renesas/drv_wdt.c] Fix error code issues [renesas/drv_i2c.c] add hardware i2c driver * [update] drv_wdt.c,drv_i2c.c Co-authored-by: Man, Jianting (Meco) <[email protected]>
1 parent eedb4e1 commit fde369f

File tree

6 files changed

+263
-44
lines changed

6 files changed

+263
-44
lines changed

bsp/renesas/libraries/HAL_Drivers/SConscript

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@ if GetDepend(['BSP_USING_I2C', 'RT_USING_I2C_BITOPS']):
2929
if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'):
3030
src += ['drv_soft_i2c.c']
3131

32+
if GetDepend(['BSP_USING_I2C', 'BSP_USING_HW_I2C']):
33+
src += ['drv_i2c.c']
34+
3235
if GetDepend(['BSP_USING_SPI']):
3336
src += ['drv_spi.c']
3437

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* Copyright (c) 2006-2022, RT-Thread Development Team
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*
6+
* Change Logs:
7+
* Date Author Notes
8+
* 2022-02-22 airm2m first version
9+
*/
10+
11+
#include <rtdevice.h>
12+
#include <rtthread.h>
13+
#include "board.h"
14+
15+
#include <stdlib.h>
16+
17+
#ifdef BSP_USING_HW_I2C
18+
19+
#define DBG_TAG "drv.hwi2c"
20+
#ifdef DRV_DEBUG
21+
#define DBG_LVL DBG_LOG
22+
#else
23+
#define DBG_LVL DBG_INFO
24+
#endif /* DRV_DEBUG */
25+
#include <rtdbg.h>
26+
27+
#include <hal_data.h>
28+
29+
static struct rt_i2c_bus_device prv_ra_i2c;
30+
static volatile i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
31+
32+
void i2c_master_callback(i2c_master_callback_args_t *p_args)
33+
{
34+
if (NULL != p_args)
35+
{
36+
/* capture callback event for validating the i2c transfer event*/
37+
i2c_event = p_args->event;
38+
}
39+
}
40+
41+
static fsp_err_t validate_i2c_event(void)
42+
{
43+
uint16_t local_time_out = UINT16_MAX;
44+
45+
/* resetting call back event capture variable */
46+
i2c_event = (i2c_master_event_t)0;
47+
48+
do
49+
{
50+
/* This is to avoid infinite loop */
51+
--local_time_out;
52+
53+
if(0 == local_time_out)
54+
{
55+
return FSP_ERR_TRANSFER_ABORTED;
56+
}
57+
58+
}while(i2c_event == 0);
59+
60+
if(i2c_event != I2C_MASTER_EVENT_ABORTED)
61+
{
62+
/* Make sure this is always Reset before return*/
63+
i2c_event = (i2c_master_event_t)0;
64+
return FSP_SUCCESS;
65+
}
66+
67+
/* Make sure this is always Reset before return */
68+
i2c_event = (i2c_master_event_t)0;
69+
return FSP_ERR_TRANSFER_ABORTED;
70+
}
71+
72+
static rt_size_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
73+
struct rt_i2c_msg msgs[],
74+
rt_uint32_t num)
75+
{
76+
rt_size_t i;
77+
struct rt_i2c_msg *msg = msgs;
78+
RT_ASSERT(bus != RT_NULL);
79+
fsp_err_t err = FSP_SUCCESS;
80+
bool restart = false;
81+
82+
for (i = 0; i < num; i++)
83+
{
84+
if (msg[i].flags & RT_I2C_NO_START)
85+
{
86+
restart = true;
87+
}
88+
if (msg[i].flags & RT_I2C_ADDR_10BIT)
89+
{
90+
LOG_E("10Bit not support");
91+
break;
92+
}
93+
else
94+
{
95+
g_i2c_master1_ctrl.slave = msg[i].addr;
96+
}
97+
98+
if (msg[i].flags & RT_I2C_RD)
99+
{
100+
err = R_IIC_MASTER_Read(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart);
101+
if (FSP_SUCCESS == err)
102+
{
103+
err = validate_i2c_event();
104+
/* handle error */
105+
if(FSP_ERR_TRANSFER_ABORTED == err)
106+
{
107+
LOG_E("POWER_CTL reg I2C read failed");
108+
break;
109+
}
110+
}
111+
/* handle error */
112+
else
113+
{
114+
/* Write API returns itself is not successful */
115+
LOG_E("R_IIC_MASTER_Write API failed");
116+
break;
117+
}
118+
}
119+
else
120+
{
121+
err = R_IIC_MASTER_Write(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart);
122+
if (FSP_SUCCESS == err)
123+
{
124+
err = validate_i2c_event();
125+
/* handle error */
126+
if(FSP_ERR_TRANSFER_ABORTED == err)
127+
{
128+
LOG_E("POWER_CTL reg I2C write failed");
129+
break;
130+
}
131+
}
132+
/* handle error */
133+
else
134+
{
135+
/* Write API returns itself is not successful */
136+
LOG_E("R_IIC_MASTER_Write API failed");
137+
break;
138+
}
139+
}
140+
}
141+
return i;
142+
}
143+
144+
static const struct rt_i2c_bus_device_ops ra_i2c_ops =
145+
{
146+
.master_xfer = ra_i2c_mst_xfer,
147+
.slave_xfer = RT_NULL,
148+
.i2c_bus_control = RT_NULL
149+
};
150+
151+
int ra_hw_i2c_init(void)
152+
{
153+
fsp_err_t err = FSP_SUCCESS;
154+
prv_ra_i2c.ops = &ra_i2c_ops;
155+
prv_ra_i2c.priv = 0;
156+
/* opening IIC master module */
157+
err = R_IIC_MASTER_Open(&g_i2c_master1_ctrl, &g_i2c_master1_cfg);
158+
/* handle error */
159+
if (FSP_SUCCESS != err)
160+
{
161+
LOG_E("R_IIC_MASTER_Open API failed");
162+
return err;
163+
}
164+
rt_i2c_bus_device_register(&prv_ra_i2c, "i2c1");
165+
166+
return 0;
167+
}
168+
INIT_DEVICE_EXPORT(ra_hw_i2c_init);
169+
170+
#endif /* BSP_USING_I2C */

bsp/renesas/libraries/HAL_Drivers/drv_wdt.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static rt_err_t wdt_init(rt_watchdog_t *wdt)
3131

3232
static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
3333
{
34+
rt_err_t ret = -RT_ERROR;
3435
struct st_wdt_timeout_values *wdt_value = {0};
3536
switch (cmd)
3637
{
@@ -39,18 +40,29 @@ static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
3940
if (R_WDT_Refresh(&g_wdt_ctrl) != FSP_SUCCESS)
4041
{
4142
LOG_E("watch dog keepalive fail.");
43+
ret = -RT_ERROR;
44+
}
45+
else
46+
{
47+
ret = RT_EOK;
4248
}
4349
break;
4450
/* set watchdog timeout */
4551
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
4652
/**< set*/
53+
LOG_W("Use the FSP tool to modify the configuration parameters!");
54+
ret = -RT_EINVAL;
4755
break;
4856
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
4957
wdt_value = (struct st_wdt_timeout_values *)arg;
5058
if (R_WDT_TimeoutGet(&g_wdt_ctrl, wdt_value) != FSP_SUCCESS)
5159
{
5260
LOG_E("wdt get timeout failed.");
53-
return -RT_ERROR;
61+
ret = -RT_ERROR;
62+
}
63+
else
64+
{
65+
ret = RT_EOK;
5466
}
5567
break;
5668
case RT_DEVICE_CTRL_WDT_START:
@@ -59,20 +71,24 @@ static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
5971
if (R_WDT_Refresh(&g_wdt_ctrl) != FSP_SUCCESS)
6072
{
6173
LOG_E("wdt start failed.");
62-
return -RT_ERROR;
74+
ret = -RT_ERROR;
75+
}
76+
else
77+
{
78+
ret = RT_EOK;
6379
}
6480
}
6581
else
6682
{
6783
LOG_E("wdt start failed.");
68-
return -RT_ERROR;
84+
ret = -RT_ERROR;
6985
}
7086
break;
7187
default:
7288
LOG_W("This command is not supported.");
73-
return -RT_ERROR;
89+
ret = -RT_ERROR;
7490
}
75-
return RT_EOK;
91+
return ret;
7692
}
7793

7894
int rt_wdt_init(void)

bsp/renesas/ra2l1-cpk/board/Kconfig

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,29 @@ menu "Hardware Drivers Config"
168168
select RT_USING_I2C_BITOPS
169169
select RT_USING_PIN
170170
if BSP_USING_I2C
171-
menuconfig BSP_USING_I2C1
172-
bool "Enable I2C1 BUS (software simulation)"
173-
default y
174-
if BSP_USING_I2C1
175-
config BSP_I2C1_SCL_PIN
176-
hex "i2c1 scl pin number"
177-
range 0x0000 0x0B0F
178-
default 0x0512
179-
config BSP_I2C1_SDA_PIN
180-
hex "I2C1 sda pin number"
181-
range 0x0000 0x0B0F
182-
default 0x0511
183-
endif
171+
config BSP_USING_HW_I2C
172+
bool "Enable Hardware I2C BUS"
173+
default n
174+
if BSP_USING_HW_I2C
175+
config BSP_USING_HW_I2C1
176+
bool "Enable Hardware I2C1 BUS"
177+
default n
178+
endif
179+
if !BSP_USING_HW_I2C
180+
menuconfig BSP_USING_I2C1
181+
bool "Enable I2C1 BUS (software simulation)"
182+
default y
183+
if BSP_USING_I2C1
184+
config BSP_I2C1_SCL_PIN
185+
hex "i2c1 scl pin number"
186+
range 0x0000 0x0B0F
187+
default 0x050C
188+
config BSP_I2C1_SDA_PIN
189+
hex "I2C1 sda pin number"
190+
range 0x0000 0x0B0F
191+
default 0x050B
192+
endif
193+
endif
184194
endif
185195

186196
menuconfig BSP_USING_SPI

bsp/renesas/ra6m4-cpk/board/Kconfig

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,29 @@ menu "Hardware Drivers Config"
300300
select RT_USING_I2C_BITOPS
301301
select RT_USING_PIN
302302
if BSP_USING_I2C
303-
menuconfig BSP_USING_I2C1
304-
bool "Enable I2C1 BUS (software simulation)"
305-
default y
306-
if BSP_USING_I2C1
307-
config BSP_I2C1_SCL_PIN
308-
hex "i2c1 scl pin number"
309-
range 0x0000 0x0B0F
310-
default 0x0512
311-
config BSP_I2C1_SDA_PIN
312-
hex "I2C1 sda pin number"
313-
range 0x0000 0x0B0F
314-
default 0x0511
315-
endif
303+
config BSP_USING_HW_I2C
304+
bool "Enable Hardware I2C BUS"
305+
default n
306+
if BSP_USING_HW_I2C
307+
config BSP_USING_HW_I2C1
308+
bool "Enable Hardware I2C1 BUS"
309+
default n
310+
endif
311+
if !BSP_USING_HW_I2C
312+
menuconfig BSP_USING_I2C1
313+
bool "Enable I2C1 BUS (software simulation)"
314+
default y
315+
if BSP_USING_I2C1
316+
config BSP_I2C1_SCL_PIN
317+
hex "i2c1 scl pin number"
318+
range 0x0000 0x0B0F
319+
default 0x050C
320+
config BSP_I2C1_SDA_PIN
321+
hex "I2C1 sda pin number"
322+
range 0x0000 0x0B0F
323+
default 0x050B
324+
endif
325+
endif
316326
endif
317327

318328
menuconfig BSP_USING_SPI

bsp/renesas/ra6m4-iot/board/Kconfig

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -300,19 +300,29 @@ menu "Hardware Drivers Config"
300300
select RT_USING_I2C_BITOPS
301301
select RT_USING_PIN
302302
if BSP_USING_I2C
303-
menuconfig BSP_USING_I2C1
304-
bool "Enable I2C1 BUS (software simulation)"
305-
default y
306-
if BSP_USING_I2C1
307-
config BSP_I2C1_SCL_PIN
308-
hex "i2c1 scl pin number"
309-
range 0x0000 0x0B0F
310-
default 0x0512
311-
config BSP_I2C1_SDA_PIN
312-
hex "I2C1 sda pin number"
313-
range 0x0000 0x0B0F
314-
default 0x0511
315-
endif
303+
config BSP_USING_HW_I2C
304+
bool "Enable Hardware I2C BUS"
305+
default n
306+
if BSP_USING_HW_I2C
307+
config BSP_USING_HW_I2C1
308+
bool "Enable Hardware I2C1 BUS"
309+
default n
310+
endif
311+
if !BSP_USING_HW_I2C
312+
menuconfig BSP_USING_I2C1
313+
bool "Enable I2C1 BUS (software simulation)"
314+
default y
315+
if BSP_USING_I2C1
316+
config BSP_I2C1_SCL_PIN
317+
hex "i2c1 scl pin number"
318+
range 0x0000 0x0B0F
319+
default 0x050C
320+
config BSP_I2C1_SDA_PIN
321+
hex "I2C1 sda pin number"
322+
range 0x0000 0x0B0F
323+
default 0x050B
324+
endif
325+
endif
316326
endif
317327

318328
menuconfig BSP_USING_SPI

0 commit comments

Comments
 (0)