canapp.c 2.1 KB
Newer Older
A
Aubr.Cool 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
 * File      : canapp.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       		Notes
 * 2015-05-14     aubrcool@qq.com 	first version
 */

#include <board.h>
#include <rtthread.h>
#include <rtdevice.h>
#ifdef RT_USING_CAN
#include "drv_lpccan.h"
struct can_app_struct
{
A
Aubr.Cool 已提交
22 23 24 25
	const char* name;
	struct rt_can_filter_config * filter;
	rt_uint8_t eventopt;
	struct rt_semaphore sem;
A
Aubr.Cool 已提交
26 27 28 29
};
static struct can_app_struct can_data[1];
struct rt_can_filter_item filter1item[4] =
{
A
Aubr.Cool 已提交
30
	LPC_CAN_AF_STD_INIT(1),
A
Aubr.Cool 已提交
31 32 33 34 35 36
	LPC_CAN_AF_STD_GRP_INIT(3,5),
	LPC_CAN_AF_EXT_INIT(2),
	LPC_CAN_AF_EXT_GRP_INIT(4,6),
};
struct rt_can_filter_config filter1 =
{
A
Aubr.Cool 已提交
37 38 39
	.count = 4,
	.actived = 1,
	.items = filter1item,
A
Aubr.Cool 已提交
40 41
};
static struct can_app_struct can_data[1] = {
A
Aubr.Cool 已提交
42 43 44 45 46
	{
		.name = "lpccan1",
		.filter = &filter1,
		.eventopt = RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
	},
A
Aubr.Cool 已提交
47 48 49
};
static rt_err_t lpccanind(rt_device_t dev, rt_size_t size)
{
A
Aubr.Cool 已提交
50
	rt_sem_release(&can_data[0].sem);
A
Aubr.Cool 已提交
51 52 53
}
void rt_can_thread_entry(void* parameter)
{
A
Aubr.Cool 已提交
54 55 56
	struct rt_can_msg msg;
	struct can_app_struct* canpara = (struct can_app_struct*) parameter;
	rt_device_t candev;
A
Aubr.Cool 已提交
57

A
Aubr.Cool 已提交
58 59 60 61 62 63 64 65 66 67 68 69
	candev = rt_device_find(canpara->name);
	RT_ASSERT(candev);
	rt_sem_init(&canpara->sem, canpara->name, 0, RT_IPC_FLAG_FIFO);
	rt_device_open(candev, (RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_INT_TX));
	rt_device_control(candev,RT_CAN_CMD_SET_FILTER,canpara->filter);
	rt_device_set_rx_indicate(candev, lpccanind);
	while(1) {
		rt_sem_take(&canpara->sem, RT_WAITING_FOREVER);
		while (rt_device_read(candev, 0, &msg, sizeof(msg)) == sizeof(msg)) {
			rt_device_write(candev, 0, &msg, sizeof(msg));
		}
	}
A
Aubr.Cool 已提交
70 71 72
}
int rt_can_app_init(void)
{
A
Aubr.Cool 已提交
73 74 75 76 77 78 79 80
	rt_thread_t tid;

	tid = rt_thread_create("canapp1",
			rt_can_thread_entry, &can_data[0],
			512, RT_THREAD_PRIORITY_MAX /3 - 1, 20);
	if (tid != RT_NULL) rt_thread_startup(tid);

	return 0;
A
Aubr.Cool 已提交
81 82 83 84
}

INIT_APP_EXPORT(rt_can_app_init);
#endif /*RT_USING_CAN*/