[bsp][renesas]Update drv_adc,i2c dirvers.

This commit is contained in:
Rbb666 2024-04-11 14:48:56 +08:00 committed by Meco Man
parent 30c3eb9a56
commit d2555d2169
9 changed files with 197 additions and 161 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -19,18 +19,19 @@ extern "C" {
#endif
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
struct ra_adc_map
struct rt_adc_dev
{
char name;
const adc_cfg_t *g_cfg;
const adc_instance_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
struct rt_adc_ops ops;
struct rt_adc_device adc_device;
};
struct ra_dev
struct ra_adc_map
{
rt_adc_device_t ra_adc_device_t;
struct ra_adc_map *ra_adc_dev;
const char *device_name;
const adc_cfg_t *g_cfg;
const adc_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -19,18 +19,19 @@ extern "C" {
#endif
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
struct ra_adc_map
struct rt_adc_dev
{
char name;
const adc_cfg_t *g_cfg;
const adc_instance_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
struct rt_adc_ops ops;
struct rt_adc_device adc_device;
};
struct ra_dev
struct ra_adc_map
{
rt_adc_device_t ra_adc_device_t;
struct ra_adc_map *ra_adc_dev;
const char *device_name;
const adc_cfg_t *g_cfg;
const adc_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -19,18 +19,19 @@ extern "C" {
#endif
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
struct ra_adc_map
struct rt_adc_dev
{
char name;
const adc_cfg_t *g_cfg;
const adc_instance_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
struct rt_adc_ops ops;
struct rt_adc_device adc_device;
};
struct ra_dev
struct ra_adc_map
{
rt_adc_device_t ra_adc_device_t;
struct ra_adc_map *ra_adc_dev;
const char *device_name;
const adc_cfg_t *g_cfg;
const adc_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -19,18 +19,19 @@ extern "C" {
#endif
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
struct ra_adc_map
struct rt_adc_dev
{
char name;
const adc_cfg_t *g_cfg;
const adc_instance_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
struct rt_adc_ops ops;
struct rt_adc_device adc_device;
};
struct ra_dev
struct ra_adc_map
{
rt_adc_device_t ra_adc_device_t;
struct ra_adc_map *ra_adc_dev;
const char *device_name;
const adc_cfg_t *g_cfg;
const adc_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -19,18 +19,19 @@ extern "C" {
#endif
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
struct ra_adc_map
struct rt_adc_dev
{
char name;
const adc_cfg_t *g_cfg;
const adc_instance_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
struct rt_adc_ops ops;
struct rt_adc_device adc_device;
};
struct ra_dev
struct ra_adc_map
{
rt_adc_device_t ra_adc_device_t;
struct ra_adc_map *ra_adc_dev;
const char *device_name;
const adc_cfg_t *g_cfg;
const adc_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -19,18 +19,19 @@ extern "C" {
#endif
#if defined(BSP_USING_ADC0) || defined(BSP_USING_ADC1)
struct ra_adc_map
struct rt_adc_dev
{
char name;
const adc_cfg_t *g_cfg;
const adc_instance_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
struct rt_adc_ops ops;
struct rt_adc_device adc_device;
};
struct ra_dev
struct ra_adc_map
{
rt_adc_device_t ra_adc_device_t;
struct ra_adc_map *ra_adc_dev;
const char *device_name;
const adc_cfg_t *g_cfg;
const adc_ctrl_t *g_ctrl;
const adc_channel_cfg_t *g_channel_cfg;
};
#endif
#endif

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2023, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2006-2021, RT-Thread Development Team
* Copyright (c) 2006-2024, RT-Thread Development Team
*
* SPDX-License-Identifier: Apache-2.0
*
@ -11,7 +11,7 @@
#include "drv_config.h"
#ifdef RT_USING_ADC
// #define DRV_DEBUG
#define DRV_DEBUG
#define DBG_TAG "drv.adc"
#ifdef DRV_DEBUG
#define DBG_LVL DBG_LOG
@ -22,24 +22,25 @@
struct ra_adc_map ra_adc[] =
{
#if defined(BSP_USING_ADC0)
{'0', &g_adc0_cfg, &g_adc0_ctrl, &g_adc0_channel_cfg},
#ifdef BSP_USING_ADC0
{
.device_name = "adc0",
.g_cfg = &g_adc0_cfg,
.g_ctrl = &g_adc0_ctrl,
.g_channel_cfg = &g_adc0_channel_cfg,
},
#endif
#if defined(BSP_USING_ADC1)
{'1', &g_adc1_cfg, &g_adc1_ctrl, &g_adc1_channel_cfg},
#ifdef BSP_USING_ADC1
{
.device_name = "adc1",
.g_cfg = &g_adc1_cfg,
.g_ctrl = &g_adc1_ctrl,
.g_channel_cfg = &g_adc1_channel_cfg,
},
#endif
};
#if defined(BSP_USING_ADC0)
struct rt_adc_device adc0_device;
struct ra_dev _ra_adc0_device = {.ra_adc_device_t = &adc0_device, .ra_adc_dev = &ra_adc[0]};
#endif
#if defined(BSP_USING_ADC1)
struct rt_adc_device adc1_device;
struct ra_dev _ra_adc1_device = {.ra_adc_device_t = &adc1_device, .ra_adc_dev = &ra_adc[1]};
#endif
static struct rt_adc_dev adc_obj[sizeof(ra_adc) / sizeof(ra_adc[0])] = {0};
static rt_err_t ra_adc_enabled(struct rt_adc_device *device, rt_int8_t channel, rt_bool_t enabled)
{
@ -50,7 +51,7 @@ static rt_err_t ra_adc_enabled(struct rt_adc_device *device, rt_int8_t channel,
{
if (FSP_SUCCESS != R_ADC_ScanStart((adc_ctrl_t *)adc->g_ctrl))
{
LOG_E("start adc%c failed.", adc->name);
LOG_E("start %s failed.", adc->device_name);
return -RT_ERROR;
}
}
@ -59,7 +60,7 @@ static rt_err_t ra_adc_enabled(struct rt_adc_device *device, rt_int8_t channel,
/**< stop adc*/
if (FSP_SUCCESS != R_ADC_ScanStop((adc_ctrl_t *)adc->g_ctrl))
{
LOG_E("stop adc%c failed.", adc->name);
LOG_E("stop %s failed.", adc->device_name);
return -RT_ERROR;
}
}
@ -69,10 +70,10 @@ static rt_err_t ra_adc_enabled(struct rt_adc_device *device, rt_int8_t channel,
rt_err_t ra_adc_close(struct rt_adc_device *device)
{
RT_ASSERT(device != RT_NULL);
struct ra_adc_map *adc = (struct ra_adc_map *)(struct ra_adc_map *)device->parent.user_data;
struct ra_adc_map *adc = (struct ra_adc_map *)device->parent.user_data;
if (FSP_SUCCESS != R_ADC_Close((adc_ctrl_t *)adc->g_ctrl))
{
LOG_E("close adc%c failed.", adc->name);
LOG_E("close %s failed.", adc->device_name);
return -RT_ERROR;
}
return RT_EOK;
@ -98,35 +99,32 @@ static const struct rt_adc_ops ra_adc_ops =
static int ra_adc_init(void)
{
#if defined(BSP_USING_ADC0)
R_ADC_Open((adc_ctrl_t *)_ra_adc0_device.ra_adc_dev->g_ctrl,
(adc_cfg_t const * const)_ra_adc0_device.ra_adc_dev->g_cfg);
rt_err_t result = 0;
rt_size_t obj_num = sizeof(adc_obj) / sizeof(struct rt_adc_dev);
R_ADC_ScanCfg((adc_ctrl_t *)_ra_adc0_device.ra_adc_dev->g_ctrl,
(adc_cfg_t const * const)_ra_adc0_device.ra_adc_dev->g_channel_cfg);
if (RT_EOK != rt_hw_adc_register(_ra_adc0_device.ra_adc_device_t, "adc0", &ra_adc_ops, (void *)_ra_adc0_device.ra_adc_dev))
for (int i = 0; i < obj_num; i++)
{
LOG_E("adc0 register failed");
return -RT_ERROR;
}
#endif
/* init ADC object */
result = R_ADC_Open((adc_ctrl_t *)ra_adc[i].g_ctrl, ra_adc[i].g_cfg);
#if defined(BSP_USING_ADC1)
R_ADC_Open((adc_ctrl_t *)_ra_adc1_device.ra_adc_dev->g_ctrl,
(adc_cfg_t const * const)_ra_adc1_device.ra_adc_dev->g_cfg);
result = R_ADC_ScanCfg((adc_ctrl_t *)ra_adc[i].g_ctrl, ra_adc[i].g_channel_cfg);
R_ADC_ScanCfg((adc_ctrl_t *)_ra_adc1_device.ra_adc_dev->g_ctrl,
(adc_cfg_t const * const)_ra_adc1_device.ra_adc_dev->g_channel_cfg);
if (RT_EOK != rt_hw_adc_register(_ra_adc1_device.ra_adc_device_t, "adc1", &ra_adc_ops, (void *)_ra_adc1_device.ra_adc_dev))
/* register ADC device */
if(rt_hw_adc_register(&adc_obj[i].adc_device,
ra_adc[i].device_name,
&ra_adc_ops,
&ra_adc[i]) == RT_EOK)
{
LOG_E("adc1 register failed");
return -RT_ERROR;
LOG_D("%s init success", ra_adc[i].device_name);
}
else
{
LOG_E("%s register failed", ra_adc[i].device_name);
result = -RT_ERROR;
}
RT_ASSERT(result == RT_EOK);
}
#endif
return RT_EOK;
}
INIT_BOARD_EXPORT(ra_adc_init);
INIT_DEVICE_EXPORT(ra_adc_init);
#endif

View File

@ -16,57 +16,76 @@
#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>
#define DRV_DEBUG
#define LOG_TAG "drv.hwi2c"
#include <drv_log.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;
#define RA_SCI_EVENT_ABORTED 1
#define RA_SCI_EVENT_RX_COMPLETE 2
#define RA_SCI_EVENT_TX_COMPLETE 4
#define RA_SCI_EVENT_ERROR 8
#define RA_SCI_EVENT_ALL 15
struct ra_i2c_handle
{
struct rt_i2c_bus_device bus;
char bus_name[RT_NAME_MAX];
const i2c_master_cfg_t *i2c_cfg;
void *i2c_ctrl;
struct rt_event event;
};
static struct ra_i2c_handle ra_i2cs[] =
{
#ifdef BSP_USING_HW_I2C0
{.bus_name = "i2c0", .i2c_cfg = &g_i2c_master0_cfg, .i2c_ctrl = &g_i2c_master0_ctrl,},
#endif
#ifdef BSP_USING_HW_I2C1
{.bus_name = "i2c1", .i2c_cfg = &g_i2c_master1_cfg, .i2c_ctrl = &g_i2c_master1_ctrl,},
#endif
};
void i2c_master_callback(i2c_master_callback_args_t *p_args)
{
rt_interrupt_enter();
if (NULL != p_args)
{
/* capture callback event for validating the i2c transfer event*/
i2c_event = p_args->event;
struct ra_i2c_handle *obj = (struct ra_i2c_handle *)p_args->p_context;
uint32_t event = 0;
RT_ASSERT(obj != RT_NULL);
switch (p_args->event)
{
case I2C_MASTER_EVENT_ABORTED:
event |= RA_SCI_EVENT_ABORTED;
break;
case I2C_MASTER_EVENT_RX_COMPLETE:
event |= RA_SCI_EVENT_RX_COMPLETE;
break;
case I2C_MASTER_EVENT_TX_COMPLETE:
event |= RA_SCI_EVENT_TX_COMPLETE;
break;
}
rt_event_send(&obj->event, event);
}
rt_interrupt_leave();
}
static fsp_err_t validate_i2c_event(void)
static rt_err_t validate_i2c_event(struct ra_i2c_handle *handle)
{
uint16_t local_time_out = UINT16_MAX;
/* resetting call back event capture variable */
i2c_event = (i2c_master_event_t)0;
do
rt_uint32_t event = 0;
if (RT_EOK != rt_event_recv(&handle->event, RA_SCI_EVENT_ALL, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, (int32_t)rt_tick_from_millisecond(100), &event))
{
/* This is to avoid infinite loop */
--local_time_out;
if(0 == local_time_out)
return -RT_ETIMEOUT;
}
if ((event & (RA_SCI_EVENT_ABORTED | RA_SCI_EVENT_ERROR)) == 0)
{
return FSP_ERR_TRANSFER_ABORTED;
return RT_EOK;
}
}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;
return -RT_ERROR;
}
static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
@ -79,6 +98,9 @@ static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
fsp_err_t err = FSP_SUCCESS;
bool restart = false;
struct ra_i2c_handle *ra_i2c = rt_container_of(bus, struct ra_i2c_handle, bus);
i2c_master_ctrl_t *master_ctrl = ra_i2c->i2c_ctrl;
for (i = 0; i < num; i++)
{
if (msg[i].flags & RT_I2C_NO_START)
@ -87,22 +109,19 @@ static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
}
if (msg[i].flags & RT_I2C_ADDR_10BIT)
{
LOG_E("10Bit not support");
break;
R_IIC_MASTER_SlaveAddressSet(master_ctrl, msg[i].addr, I2C_MASTER_ADDR_MODE_10BIT);
}
else
{
g_i2c_master1_ctrl.slave = msg[i].addr;
R_IIC_MASTER_SlaveAddressSet(master_ctrl, msg[i].addr, I2C_MASTER_ADDR_MODE_7BIT);
}
if (msg[i].flags & RT_I2C_RD)
{
err = R_IIC_MASTER_Read(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart);
err = R_IIC_MASTER_Read(master_ctrl, msg[i].buf, msg[i].len, restart);
if (FSP_SUCCESS == err)
{
err = validate_i2c_event();
/* handle error */
if(FSP_ERR_TRANSFER_ABORTED == err)
if (RT_EOK != validate_i2c_event(ra_i2c))
{
LOG_E("POWER_CTL reg I2C read failed");
break;
@ -112,18 +131,16 @@ static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
else
{
/* Write API returns itself is not successful */
LOG_E("R_IIC_MASTER_Write API failed");
LOG_E("R_I2C_MASTER_Write API failed");
break;
}
}
else
{
err = R_IIC_MASTER_Write(&g_i2c_master1_ctrl, msg[i].buf, msg[i].len, restart);
err = R_IIC_MASTER_Write(master_ctrl, msg[i].buf, msg[i].len, restart);
if (FSP_SUCCESS == err)
{
err = validate_i2c_event();
/* handle error */
if(FSP_ERR_TRANSFER_ABORTED == err)
if (RT_EOK != validate_i2c_event(ra_i2c))
{
LOG_E("POWER_CTL reg I2C write failed");
break;
@ -133,12 +150,12 @@ static rt_ssize_t ra_i2c_mst_xfer(struct rt_i2c_bus_device *bus,
else
{
/* Write API returns itself is not successful */
LOG_E("R_IIC_MASTER_Write API failed");
LOG_E("R_I2C_MASTER_Write API failed");
break;
}
}
}
return i;
return (rt_ssize_t)i;
}
static const struct rt_i2c_bus_device_ops ra_i2c_ops =
@ -151,17 +168,32 @@ static const struct rt_i2c_bus_device_ops ra_i2c_ops =
int ra_hw_i2c_init(void)
{
fsp_err_t err = FSP_SUCCESS;
prv_ra_i2c.ops = &ra_i2c_ops;
prv_ra_i2c.priv = 0;
for (rt_uint32_t i = 0; i < sizeof(ra_i2cs) / sizeof(ra_i2cs[0]); i++)
{
ra_i2cs[i].bus.ops = &ra_i2c_ops;
ra_i2cs[i].bus.priv = 0;
if (RT_EOK != rt_event_init(&ra_i2cs[i].event, ra_i2cs[i].bus_name, RT_IPC_FLAG_FIFO))
{
LOG_E("Init event failed");
continue;
}
/* opening IIC master module */
err = R_IIC_MASTER_Open(&g_i2c_master1_ctrl, &g_i2c_master1_cfg);
err = R_IIC_MASTER_Open(ra_i2cs[i].i2c_ctrl, ra_i2cs[i].i2c_cfg);
if (FSP_SUCCESS != err)
{
LOG_E("R_I2C_MASTER_Open API failed,%d", err);
continue;
}
err = R_IIC_MASTER_CallbackSet(ra_i2cs[i].i2c_ctrl, i2c_master_callback, &ra_i2cs[i], RT_NULL);
/* handle error */
if (FSP_SUCCESS != err)
{
LOG_E("R_IIC_MASTER_Open API failed");
return err;
LOG_E("R_I2C_CallbackSet API failed,%d", err);
continue;
}
rt_i2c_bus_device_register(&ra_i2cs[i].bus, ra_i2cs[i].bus_name);
}
rt_i2c_bus_device_register(&prv_ra_i2c, "i2c1");
return 0;
}