[[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) <920369182@qq.com>
This commit is contained in:
parent
eedb4e1981
commit
fde369f011
|
@ -29,6 +29,9 @@ if GetDepend(['BSP_USING_I2C', 'RT_USING_I2C_BITOPS']):
|
|||
if GetDepend('BSP_USING_I2C0') or GetDepend('BSP_USING_I2C1'):
|
||||
src += ['drv_soft_i2c.c']
|
||||
|
||||
if GetDepend(['BSP_USING_I2C', 'BSP_USING_HW_I2C']):
|
||||
src += ['drv_i2c.c']
|
||||
|
||||
if GetDepend(['BSP_USING_SPI']):
|
||||
src += ['drv_spi.c']
|
||||
|
||||
|
|
|
@ -0,0 +1,170 @@
|
|||
/*
|
||||
* Copyright (c) 2006-2022, RT-Thread Development Team
|
||||
*
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Change Logs:
|
||||
* Date Author Notes
|
||||
* 2022-02-22 airm2m first version
|
||||
*/
|
||||
|
||||
#include <rtdevice.h>
|
||||
#include <rtthread.h>
|
||||
#include "board.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef BSP_USING_HW_I2C
|
||||
|
||||
#define DBG_TAG "drv.hwi2c"
|
||||
#ifdef DRV_DEBUG
|
||||
#define DBG_LVL DBG_LOG
|
||||
#else
|
||||
#define DBG_LVL DBG_INFO
|
||||
#endif /* DRV_DEBUG */
|
||||
#include <rtdbg.h>
|
||||
|
||||
#include <hal_data.h>
|
||||
|
||||
static struct rt_i2c_bus_device prv_ra_i2c;
|
||||
static volatile i2c_master_event_t i2c_event = I2C_MASTER_EVENT_ABORTED;
|
||||
|
||||
void i2c_master_callback(i2c_master_callback_args_t *p_args)
|
||||
{
|
||||
if (NULL != p_args)
|
||||
{
|
||||
/* capture callback event for validating the i2c transfer event*/
|
||||
i2c_event = p_args->event;
|
||||
}
|
||||
}
|
||||
|
||||
static fsp_err_t validate_i2c_event(void)
|
||||
{
|
||||
uint16_t local_time_out = UINT16_MAX;
|
||||
|
||||
/* resetting call back event capture variable */
|
||||
i2c_event = (i2c_master_event_t)0;
|
||||
|
||||
do
|
||||
{
|
||||
/* This is to avoid infinite loop */
|
||||
--local_time_out;
|
||||
|
||||
if(0 == local_time_out)
|
||||
{
|
||||
return FSP_ERR_TRANSFER_ABORTED;
|
||||
}
|
||||
|
||||
}while(i2c_event == 0);
|
||||
|
||||
if(i2c_event != I2C_MASTER_EVENT_ABORTED)
|
||||
{
|
||||
/* Make sure this is always Reset before return*/
|
||||
i2c_event = (i2c_master_event_t)0;
|
||||
return FSP_SUCCESS;
|
||||
}
|
||||
|
||||
/* Make sure this is always Reset before return */
|
||||
i2c_event = (i2c_master_event_t)0;
|
||||
return FSP_ERR_TRANSFER_ABORTED;
|
||||
}
|
||||
|
||||
static rt_size_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
|
||||
struct rt_i2c_msg msgs[],
|
||||
rt_uint32_t num)
|
||||
{
|
||||
rt_size_t i;
|
||||
struct rt_i2c_msg *msg = msgs;
|
||||
RT_ASSERT(bus != RT_NULL);
|
||||
fsp_err_t err = FSP_SUCCESS;
|
||||
bool restart = false;
|
||||
|
||||
for (i = 0; i < num; i++)
|
||||
{
|
||||
if (msg[i].flags & RT_I2C_NO_START)
|
||||
{
|
||||
restart = true;
|
||||
}
|
||||
if (msg[i].flags & RT_I2C_ADDR_10BIT)
|
||||
{
|
||||
LOG_E("10Bit not support");
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
g_i2c_master1_ctrl.slave = msg[i].addr;
|
||||
}
|
||||
|
||||
if (msg[i].flags & RT_I2C_RD)
|
||||
{
|
||||
err = R_IIC_MASTER_Read(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart);
|
||||
if (FSP_SUCCESS == err)
|
||||
{
|
||||
err = validate_i2c_event();
|
||||
/* handle error */
|
||||
if(FSP_ERR_TRANSFER_ABORTED == err)
|
||||
{
|
||||
LOG_E("POWER_CTL reg I2C read failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* handle error */
|
||||
else
|
||||
{
|
||||
/* Write API returns itself is not successful */
|
||||
LOG_E("R_IIC_MASTER_Write API failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
err = R_IIC_MASTER_Write(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart);
|
||||
if (FSP_SUCCESS == err)
|
||||
{
|
||||
err = validate_i2c_event();
|
||||
/* handle error */
|
||||
if(FSP_ERR_TRANSFER_ABORTED == err)
|
||||
{
|
||||
LOG_E("POWER_CTL reg I2C write failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
/* handle error */
|
||||
else
|
||||
{
|
||||
/* Write API returns itself is not successful */
|
||||
LOG_E("R_IIC_MASTER_Write API failed");
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return i;
|
||||
}
|
||||
|
||||
static const struct rt_i2c_bus_device_ops ra_i2c_ops =
|
||||
{
|
||||
.master_xfer = ra_i2c_mst_xfer,
|
||||
.slave_xfer = RT_NULL,
|
||||
.i2c_bus_control = RT_NULL
|
||||
};
|
||||
|
||||
int ra_hw_i2c_init(void)
|
||||
{
|
||||
fsp_err_t err = FSP_SUCCESS;
|
||||
prv_ra_i2c.ops = &ra_i2c_ops;
|
||||
prv_ra_i2c.priv = 0;
|
||||
/* opening IIC master module */
|
||||
err = R_IIC_MASTER_Open(&g_i2c_master1_ctrl, &g_i2c_master1_cfg);
|
||||
/* handle error */
|
||||
if (FSP_SUCCESS != err)
|
||||
{
|
||||
LOG_E("R_IIC_MASTER_Open API failed");
|
||||
return err;
|
||||
}
|
||||
rt_i2c_bus_device_register(&prv_ra_i2c, "i2c1");
|
||||
|
||||
return 0;
|
||||
}
|
||||
INIT_DEVICE_EXPORT(ra_hw_i2c_init);
|
||||
|
||||
#endif /* BSP_USING_I2C */
|
|
@ -31,6 +31,7 @@ static rt_err_t wdt_init(rt_watchdog_t *wdt)
|
|||
|
||||
static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
|
||||
{
|
||||
rt_err_t ret = -RT_ERROR;
|
||||
struct st_wdt_timeout_values *wdt_value = {0};
|
||||
switch (cmd)
|
||||
{
|
||||
|
@ -39,18 +40,29 @@ static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
|
|||
if (R_WDT_Refresh(&g_wdt_ctrl) != FSP_SUCCESS)
|
||||
{
|
||||
LOG_E("watch dog keepalive fail.");
|
||||
ret = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = RT_EOK;
|
||||
}
|
||||
break;
|
||||
/* set watchdog timeout */
|
||||
case RT_DEVICE_CTRL_WDT_SET_TIMEOUT:
|
||||
/**< set*/
|
||||
LOG_W("Use the FSP tool to modify the configuration parameters!");
|
||||
ret = -RT_EINVAL;
|
||||
break;
|
||||
case RT_DEVICE_CTRL_WDT_GET_TIMEOUT:
|
||||
wdt_value = (struct st_wdt_timeout_values *)arg;
|
||||
if (R_WDT_TimeoutGet(&g_wdt_ctrl, wdt_value) != FSP_SUCCESS)
|
||||
{
|
||||
LOG_E("wdt get timeout failed.");
|
||||
return -RT_ERROR;
|
||||
ret = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = RT_EOK;
|
||||
}
|
||||
break;
|
||||
case RT_DEVICE_CTRL_WDT_START:
|
||||
|
@ -59,20 +71,24 @@ static rt_err_t wdt_control(rt_watchdog_t *wdt, int cmd, void *arg)
|
|||
if (R_WDT_Refresh(&g_wdt_ctrl) != FSP_SUCCESS)
|
||||
{
|
||||
LOG_E("wdt start failed.");
|
||||
return -RT_ERROR;
|
||||
ret = -RT_ERROR;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = RT_EOK;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
LOG_E("wdt start failed.");
|
||||
return -RT_ERROR;
|
||||
ret = -RT_ERROR;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
LOG_W("This command is not supported.");
|
||||
return -RT_ERROR;
|
||||
ret = -RT_ERROR;
|
||||
}
|
||||
return RT_EOK;
|
||||
return ret;
|
||||
}
|
||||
|
||||
int rt_wdt_init(void)
|
||||
|
|
|
@ -168,19 +168,29 @@ menu "Hardware Drivers Config"
|
|||
select RT_USING_I2C_BITOPS
|
||||
select RT_USING_PIN
|
||||
if BSP_USING_I2C
|
||||
menuconfig BSP_USING_I2C1
|
||||
bool "Enable I2C1 BUS (software simulation)"
|
||||
default y
|
||||
if BSP_USING_I2C1
|
||||
config BSP_I2C1_SCL_PIN
|
||||
hex "i2c1 scl pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x0512
|
||||
config BSP_I2C1_SDA_PIN
|
||||
hex "I2C1 sda pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x0511
|
||||
endif
|
||||
config BSP_USING_HW_I2C
|
||||
bool "Enable Hardware I2C BUS"
|
||||
default n
|
||||
if BSP_USING_HW_I2C
|
||||
config BSP_USING_HW_I2C1
|
||||
bool "Enable Hardware I2C1 BUS"
|
||||
default n
|
||||
endif
|
||||
if !BSP_USING_HW_I2C
|
||||
menuconfig BSP_USING_I2C1
|
||||
bool "Enable I2C1 BUS (software simulation)"
|
||||
default y
|
||||
if BSP_USING_I2C1
|
||||
config BSP_I2C1_SCL_PIN
|
||||
hex "i2c1 scl pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x050C
|
||||
config BSP_I2C1_SDA_PIN
|
||||
hex "I2C1 sda pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x050B
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_SPI
|
||||
|
|
|
@ -300,19 +300,29 @@ menu "Hardware Drivers Config"
|
|||
select RT_USING_I2C_BITOPS
|
||||
select RT_USING_PIN
|
||||
if BSP_USING_I2C
|
||||
menuconfig BSP_USING_I2C1
|
||||
bool "Enable I2C1 BUS (software simulation)"
|
||||
default y
|
||||
if BSP_USING_I2C1
|
||||
config BSP_I2C1_SCL_PIN
|
||||
hex "i2c1 scl pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x0512
|
||||
config BSP_I2C1_SDA_PIN
|
||||
hex "I2C1 sda pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x0511
|
||||
endif
|
||||
config BSP_USING_HW_I2C
|
||||
bool "Enable Hardware I2C BUS"
|
||||
default n
|
||||
if BSP_USING_HW_I2C
|
||||
config BSP_USING_HW_I2C1
|
||||
bool "Enable Hardware I2C1 BUS"
|
||||
default n
|
||||
endif
|
||||
if !BSP_USING_HW_I2C
|
||||
menuconfig BSP_USING_I2C1
|
||||
bool "Enable I2C1 BUS (software simulation)"
|
||||
default y
|
||||
if BSP_USING_I2C1
|
||||
config BSP_I2C1_SCL_PIN
|
||||
hex "i2c1 scl pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x050C
|
||||
config BSP_I2C1_SDA_PIN
|
||||
hex "I2C1 sda pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x050B
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_SPI
|
||||
|
|
|
@ -300,19 +300,29 @@ menu "Hardware Drivers Config"
|
|||
select RT_USING_I2C_BITOPS
|
||||
select RT_USING_PIN
|
||||
if BSP_USING_I2C
|
||||
menuconfig BSP_USING_I2C1
|
||||
bool "Enable I2C1 BUS (software simulation)"
|
||||
default y
|
||||
if BSP_USING_I2C1
|
||||
config BSP_I2C1_SCL_PIN
|
||||
hex "i2c1 scl pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x0512
|
||||
config BSP_I2C1_SDA_PIN
|
||||
hex "I2C1 sda pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x0511
|
||||
endif
|
||||
config BSP_USING_HW_I2C
|
||||
bool "Enable Hardware I2C BUS"
|
||||
default n
|
||||
if BSP_USING_HW_I2C
|
||||
config BSP_USING_HW_I2C1
|
||||
bool "Enable Hardware I2C1 BUS"
|
||||
default n
|
||||
endif
|
||||
if !BSP_USING_HW_I2C
|
||||
menuconfig BSP_USING_I2C1
|
||||
bool "Enable I2C1 BUS (software simulation)"
|
||||
default y
|
||||
if BSP_USING_I2C1
|
||||
config BSP_I2C1_SCL_PIN
|
||||
hex "i2c1 scl pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x050C
|
||||
config BSP_I2C1_SDA_PIN
|
||||
hex "I2C1 sda pin number"
|
||||
range 0x0000 0x0B0F
|
||||
default 0x050B
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
menuconfig BSP_USING_SPI
|
||||
|
|
Loading…
Reference in New Issue