驱动列表里没有添加的驱动

Viewed 32

就是那个开发k230_sdk的驱动示例,然后我按照操作手册上添加

,也在在misc目录下“SConscript”中添加drv_hello.c驱动:
src = src + ['drv_hello.c']了,然后我按照这个重新编译一遍镜像后,再烧录到SD上上版测试,但是驱动列表里面没有那个hello的驱动名字

2 Answers

你好,方便发一下drv_hello.c吗

#include<board.h>
#include<rtthread.h>
#include<rtdevice.h>
#include<string.h>

//#define DRV_DEBUG
#define LOG_TAG             "drv.hello"
//#include <drv_log.h>

static struct rt_device hello_drv;

static rt_size_t hello_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
// 简单地返回一个固定字符串到缓冲区
const char *response = "Hello from read!\n\r";
rt_size_t len = strlen(response);
 
// 确保不会超出缓冲区大小
if (size < len)
{
    len = size;
}
 
// 复制字符串到缓冲区
memcpy(buffer, response, len);
 
return len;
}

static rt_size_t hello_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
const int *tmp = buffer;
if (tmp[0] == 1)
    rt_kprintf("Hello, sam\n\r");
else if (tmp[0] == 0)
    rt_kprintf("Bye, sam\n\r");
else
    rt_kprintf("Invalid\n\r");
return size;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops hello_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
hello_read,
hello_write,
RT_NULL,
};
#endif

static int hello_drv_init(void)
{
int ret;

/* 分配一个 rt_device */
/* 设置 */    
hello_drv.type = RT_Device_Class_Miscellaneous;
hello_drv.rx_indicate = RT_NULL;
hello_drv.tx_complete = RT_NULL;

#ifdef RT_USING_DEVICE_OPS
hello_drv.ops          = &hello_ops;
#else
hello_drv.init     = RT_NULL;
hello_drv.open     = RT_NULL;
hello_drv.close    = RT_NULL;
hello_drv.read     = hello_read;
hello_drv.write    = hello_write;
hello_drv.control  = RT_NULL;
#endif

/* 注册 */
ret = rt_device_register(&hello_drv, "hello", RT_DEVICE_FLAG_RDWR);
rt_kprintf("rt_device_register hello, ret = %d\n", ret);


return ret;
}
INIT_BOARD_EXPORT(hello_drv_init);

#include<board.h>
#include<rtthread.h>
#include<rtdevice.h>
#include<string.h>

//#define DRV_DEBUG
#define LOG_TAG "drv.hello"
//#include <drv_log.h>

static struct rt_device hello_drv;

static rt_size_t hello_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{
// 简单地返回一个固定字符串到缓冲区
const char *response = "Hello from read!\n\r";
rt_size_t len = strlen(response);

// 确保不会超出缓冲区大小
if (size < len)
{
len = size;
}

// 复制字符串到缓冲区
memcpy(buffer, response, len);

return len;
}

static rt_size_t hello_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{
const int *tmp = buffer;
if (tmp[0] == 1)
rt_kprintf("Hello, sam\n\r");
else if (tmp[0] == 0)
rt_kprintf("Bye, sam\n\r");
else
rt_kprintf("Invalid\n\r");
return size;
}

#ifdef RT_USING_DEVICE_OPS
const static struct rt_device_ops hello_ops =
{
RT_NULL,
RT_NULL,
RT_NULL,
hello_read,
hello_write,
RT_NULL,
};
#endif

static int hello_drv_init(void)
{
int ret;

/* 分配一个 rt_device /
/
设置 */
hello_drv.type = RT_Device_Class_Miscellaneous;
hello_drv.rx_indicate = RT_NULL;
hello_drv.tx_complete = RT_NULL;

#ifdef RT_USING_DEVICE_OPS
hello_drv.ops = &hello_ops;
#else
hello_drv.init = RT_NULL;
hello_drv.open = RT_NULL;
hello_drv.close = RT_NULL;
hello_drv.read = hello_read;
hello_drv.write = hello_write;
hello_drv.control = RT_NULL;
#endif

/* 注册 */
ret = rt_device_register(&hello_drv, "hello", RT_DEVICE_FLAG_RDWR);
rt_kprintf("rt_device_register hello, ret = %d\n", ret);

return ret;
}
INIT_BOARD_EXPORT(hello_drv_init);