CAN driver api.
#include <rtthread.h>
#include "rtdevice.h"
#define CAN_DEV_NAME "can1"
static rt_err_t can_rx_call(
rt_device_t dev, rt_size_t size)
{
return RT_EOK;
}
static void can_rx_thread(void *parameter)
{
int i;
rt_err_t res;
struct rt_can_msg rxmsg = {0};
rt_device_set_rx_indicate(can_dev, can_rx_call);
#ifdef RT_CAN_USING_HDR
{
RT_CAN_FILTER_ITEM_INIT(0x100, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
RT_CAN_FILTER_ITEM_INIT(0x300, 0, 0, 0, 0x700, RT_NULL, RT_NULL),
RT_CAN_FILTER_ITEM_INIT(0x211, 0, 0, 0, 0x7ff, RT_NULL, RT_NULL),
RT_CAN_FILTER_STD_INIT(0x486, RT_NULL, RT_NULL),
{0x555, 0, 0, 0, 0x7ff, 7,}
};
res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg);
RT_ASSERT(res == RT_EOK);
#endif
res = RT_TRUE;
res = rt_device_control(can_dev, RT_CAN_CMD_START, &res);
while (1)
{
rxmsg.hdr = -1;
rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg));
rt_kprintf("ID:%x", rxmsg.id);
for (i = 0; i < 8; i++)
{
rt_kprintf("%2x", rxmsg.data[i]);
}
rt_kprintf("\n");
}
}
int can_sample(int argc, char *argv[])
{
struct rt_can_msg msg = {0};
rt_err_t res;
rt_size_t size;
char can_name[RT_NAME_MAX];
if (argc == 2)
{
rt_strncpy(can_name, argv[1], RT_NAME_MAX);
}
else
{
rt_strncpy(can_name, CAN_DEV_NAME, RT_NAME_MAX);
}
can_dev = rt_device_find(can_name);
if (!can_dev)
{
rt_kprintf("find %s failed!\n", can_name);
return -RT_ERROR;
}
RT_ASSERT(res == RT_EOK);
if (thread != RT_NULL)
{
}
else
{
rt_kprintf("create can_rx thread failed!\n");
}
msg.id = 0x78;
msg.ide = RT_CAN_STDID;
msg.rtr = RT_CAN_DTR;
msg.len = 8;
msg.data[0] = 0x00;
msg.data[1] = 0x11;
msg.data[2] = 0x22;
msg.data[3] = 0x33;
msg.data[4] = 0x44;
msg.data[5] = 0x55;
msg.data[6] = 0x66;
msg.data[7] = 0x77;
size = rt_device_write(can_dev, 0, &msg, sizeof(msg));
if (size == 0)
{
rt_kprintf("can dev write data failed!\n");
}
return res;
}
#define RT_DEVICE_FLAG_INT_RX
Definition: rtdef.h:1297
#define RT_DEVICE_FLAG_INT_TX
Definition: rtdef.h:1299
#define RT_IPC_FLAG_FIFO
Definition: rtdef.h:972
#define RT_WAITING_FOREVER
Definition: rtdef.h:980
rt_err_t rt_thread_startup(rt_thread_t thread)
This function will start a thread and put it to system ready queue.
Definition: thread.c:393
rt_thread_t rt_thread_create(const char *name, void(*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick)
This function will create a thread object and allocate thread object memory. and stack.
Definition: thread.c:523
#define MSH_CMD_EXPORT(...)
Definition: finsh.h:142
rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
This function will initialize a static semaphore object.
Definition: ipc.c:376
rt_err_t rt_sem_release(rt_sem_t sem)
This function will release a semaphore. If there is thread suspended on the semaphore,...
Definition: ipc.c:695
CAN filter configuration.
Definition: dev_can.h:271
CAN filter item.
Definition: dev_can.h:218