Skip to content

device->user_data在rt-thread驱动层被使用,导致hardfult #1

Description

@kylongmu

在官方rt-thread源bsp\wch\risc-v\ch32v307v-r1开发板工程下测试直接死机
msh > hardfult
mepc:00013a5c
mcause:00000004
mtval:50504251
跟踪后在small_modbus_port_rtthread.c文件中

`
static int _modbus_rtdevice_open(small_modbus_t *smb)
{
small_modbus_port_rtdevice_t *smb_port_device = (small_modbus_port_rtdevice_t *)smb->port;
if (smb_port_device->device)
{
//屏蔽下面一行可以执行无硬件报错,应该是这里引起问题
//smb_port_device->device->user_data = smb_port_device;

	rt_device_set_rx_indicate(smb_port_device->device, _modbus_rtdevice_rx_indicate);
	rt_device_open(smb_port_device->device, smb_port_device->oflag);

`


把smb_port_device->device->user_data = smb_port_device;
放到rt_device_open后执行,能通过,但是一旦接收到数据,接收数据引用user_data就直接hardfult。看来是user_data的赋值有问题,再引用它的指针时发生了内存越界错误。

进一步检查后发现user_data在Libraries\ch32_drivers\drv_usart.c中被使用了
`
static rt_err_t ch32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
{
struct ch32_uart *uart;
GPIO_InitTypeDef GPIO_InitStructure={0};

RT_ASSERT(serial != RT_NULL);
RT_ASSERT(cfg != RT_NULL);

uart = (struct ch32_uart *) serial->parent.user_data;//此处被驱动使用

`
这个变量已经被驱动层代码占用了,应用层再用当然指针出错了,device->user_data不能用。

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions