Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
rt-thread
提交
eb56b832
R
rt-thread
项目概览
BaiXuePrincess
/
rt-thread
与 Fork 源项目一致
Fork自
RT-Thread / rt-thread
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rt-thread
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
eb56b832
编写于
5月 14, 2015
作者:
A
Aubr.Cool
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add Can Drv
上级
2f5c5b67
变更
6
展开全部
隐藏空白更改
内联
并排
Showing
6 changed file
with
2770 addition
and
0 deletion
+2770
-0
bsp/stm32f10x/applications/canapp.c
bsp/stm32f10x/applications/canapp.c
+141
-0
bsp/stm32f10x/drivers/bxcan.c
bsp/stm32f10x/drivers/bxcan.c
+1411
-0
bsp/stm32f10x/drivers/bxcan.h
bsp/stm32f10x/drivers/bxcan.h
+16
-0
components/drivers/can/can.c
components/drivers/can/can.c
+906
-0
components/drivers/include/drivers/can.h
components/drivers/include/drivers/can.h
+292
-0
components/drivers/include/rtdevice.h
components/drivers/include/rtdevice.h
+4
-0
未找到文件。
bsp/stm32f10x/applications/canapp.c
0 → 100644
浏览文件 @
eb56b832
/*
* 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
*/
#include <board.h>
#include <rtthread.h>
#include <rtdevice.h>
struct
can_app_struct
{
const
char
*
name
;
struct
rt_event
event
;
struct
rt_can_filter_config
*
filter
;
rt_uint8_t
eventopt
;
};
static
struct
can_app_struct
can_data
[
2
];
static
rt_err_t
can1ind
(
rt_device_t
dev
,
void
*
args
,
rt_int32_t
hdr
,
rt_size_t
size
)
{
rt_event_t
pevent
=
(
rt_event_t
)
args
;
rt_event_send
(
pevent
,
1
<<
(
hdr
));
return
RT_EOK
;
}
static
rt_err_t
can2ind
(
rt_device_t
dev
,
void
*
args
,
rt_int32_t
hdr
,
rt_size_t
size
)
{
rt_event_t
pevent
=
(
rt_event_t
)
args
;
rt_event_send
(
pevent
,
1
<<
(
hdr
));
return
RT_EOK
;
}
struct
rt_can_filter_item
filter1item
[
4
]
=
{
RT_CAN_FILTER_STD_INIT
(
1
,
can1ind
,
&
can_data
[
0
].
event
),
RT_CAN_FILTER_STD_INIT
(
2
,
can1ind
,
&
can_data
[
0
].
event
),
RT_CAN_STD_RMT_FILTER_INIT
(
3
,
can1ind
,
&
can_data
[
0
].
event
),
RT_CAN_STD_RMT_DATA_FILTER_INIT
(
4
,
can1ind
,
&
can_data
[
0
].
event
),
};
struct
rt_can_filter_item
filter2item
[
4
]
=
{
RT_CAN_FILTER_STD_INIT
(
1
,
can2ind
,
&
can_data
[
1
].
event
),
RT_CAN_FILTER_STD_INIT
(
2
,
can2ind
,
&
can_data
[
1
].
event
),
RT_CAN_STD_RMT_FILTER_INIT
(
3
,
can2ind
,
&
can_data
[
1
].
event
),
RT_CAN_STD_RMT_DATA_FILTER_INIT
(
4
,
can2ind
,
&
can_data
[
1
].
event
),
};
struct
rt_can_filter_config
filter1
=
{
.
count
=
4
,
.
actived
=
1
,
.
items
=
filter1item
,
};
struct
rt_can_filter_config
filter2
=
{
.
count
=
4
,
.
actived
=
1
,
.
items
=
filter2item
,
};
static
struct
can_app_struct
can_data
[
2
]
=
{
{
.
name
=
"bxcan1"
,
.
filter
=
&
filter1
,
.
eventopt
=
RT_EVENT_FLAG_OR
|
RT_EVENT_FLAG_CLEAR
,
},
{
.
name
=
"bxcan2"
,
.
filter
=
&
filter2
,
.
eventopt
=
RT_EVENT_FLAG_AND
|
RT_EVENT_FLAG_CLEAR
,
},
};
void
rt_can_thread_entry
(
void
*
parameter
)
{
struct
rt_can_msg
msg
;
struct
can_app_struct
*
canpara
=
(
struct
can_app_struct
*
)
parameter
;
rt_device_t
candev
;
rt_uint32_t
e
;
candev
=
rt_device_find
(
canpara
->
name
);
RT_ASSERT
(
candev
);
rt_event_init
(
&
canpara
->
event
,
canpara
->
name
,
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
);
while
(
1
)
{
if
(
rt_event_recv
(
&
canpara
->
event
,
((
1
<<
canpara
->
filter
->
items
[
0
].
hdr
)
|
(
1
<<
canpara
->
filter
->
items
[
1
].
hdr
)
|
(
1
<<
canpara
->
filter
->
items
[
2
].
hdr
)
|
(
1
<<
canpara
->
filter
->
items
[
3
].
hdr
)),
canpara
->
eventopt
,
RT_WAITING_FOREVER
,
&
e
)
!=
RT_EOK
)
{
continue
;
}
if
(
e
&
(
1
<<
canpara
->
filter
->
items
[
0
].
hdr
))
{
msg
.
hdr
=
canpara
->
filter
->
items
[
0
].
hdr
;
while
(
rt_device_read
(
candev
,
0
,
&
msg
,
sizeof
(
msg
))
==
sizeof
(
msg
))
{
rt_device_write
(
candev
,
0
,
&
msg
,
sizeof
(
msg
));
}
}
if
(
e
&
(
1
<<
canpara
->
filter
->
items
[
1
].
hdr
))
{
msg
.
hdr
=
canpara
->
filter
->
items
[
1
].
hdr
;
while
(
rt_device_read
(
candev
,
0
,
&
msg
,
sizeof
(
msg
))
==
sizeof
(
msg
))
{
rt_device_write
(
candev
,
0
,
&
msg
,
sizeof
(
msg
));
}
}
if
(
e
&
(
1
<<
canpara
->
filter
->
items
[
2
].
hdr
))
{
msg
.
hdr
=
canpara
->
filter
->
items
[
2
].
hdr
;
while
(
rt_device_read
(
candev
,
0
,
&
msg
,
sizeof
(
msg
))
==
sizeof
(
msg
))
{
rt_device_write
(
candev
,
0
,
&
msg
,
sizeof
(
msg
));
}
}
if
(
e
&
(
1
<<
canpara
->
filter
->
items
[
3
].
hdr
))
{
msg
.
hdr
=
canpara
->
filter
->
items
[
3
].
hdr
;
while
(
rt_device_read
(
candev
,
0
,
&
msg
,
sizeof
(
msg
))
==
sizeof
(
msg
))
{
rt_device_write
(
candev
,
0
,
&
msg
,
sizeof
(
msg
));
}
}
}
}
int
rt_can_app_init
(
void
)
{
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
);
tid
=
rt_thread_create
(
"canapp2"
,
rt_can_thread_entry
,
&
can_data
[
1
],
512
,
RT_THREAD_PRIORITY_MAX
/
3
-
1
,
20
);
if
(
tid
!=
RT_NULL
)
rt_thread_startup
(
tid
);
return
0
;
}
INIT_APP_EXPORT
(
rt_can_app_init
);
bsp/stm32f10x/drivers/bxcan.c
0 → 100644
浏览文件 @
eb56b832
此差异已折叠。
点击以展开。
bsp/stm32f10x/drivers/bxcan.h
0 → 100644
浏览文件 @
eb56b832
/*
* File : bxcan.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, 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
*/
#ifndef BXCAN_H_
#define BXCAN_H_
#endif
/*BXCAN_H_*/
components/drivers/can/can.c
0 → 100644
浏览文件 @
eb56b832
此差异已折叠。
点击以展开。
components/drivers/include/drivers/can.h
0 → 100644
浏览文件 @
eb56b832
/*
* File : can.h
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2015, 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
*/
#ifndef CAN_H_
#define CAN_H_
#ifndef RT_CANMSG_BOX_SZ
#define RT_CANMSG_BOX_SZ 16
#endif
#ifndef RT_CANSND_BOX_NUM
#define RT_CANSND_BOX_NUM 1
#endif
enum
CANBAUD
{
CAN1MBaud
=
0
,
// 1 MBit/sec
CAN800kBaud
,
// 800 kBit/sec
CAN500kBaud
,
// 500 kBit/sec
CAN250kBaud
,
// 250 kBit/sec
CAN125kBaud
,
// 125 kBit/sec
CAN100kBaud
,
// 100 kBit/sec
CAN50kBaud
,
// 50 kBit/sec
CAN20kBaud
,
// 20 kBit/sec
CAN10kBaud
// 10 kBit/sec
};
#define RT_CAN_MODE_NORMAL 0
#define RT_CAN_MODE_LISEN 1
#define RT_CAN_MODE_LOOPBACK 2
#define RT_CAN_MODE_LOOPBACKANLISEN 3
#define RT_CAN_MODE_PRIV 0x01
#define RT_CAN_MODE_NOPRIV 0x00
#ifdef RT_CAN_USING_LED
struct
rt_can_led
{
rt_uint32_t
pin
,
mode
,
init
;
struct
rt_timer
*
timer
;
const
char
*
timer_name
;
};
#endif
/*RT_CAN_USING_LED*/
struct
rt_can_filter_item
{
rt_uint32_t
id
:
29
;
rt_uint32_t
ide
:
1
;
rt_uint32_t
rtr
:
1
;
rt_uint32_t
mode
:
1
;
rt_uint32_t
mask
;
rt_int32_t
hdr
;
rt_err_t
(
*
ind
)(
rt_device_t
dev
,
void
*
args
,
rt_int32_t
hdr
,
rt_size_t
size
);
void
*
args
;
};
#ifdef RT_CAN_USING_HDR
#define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,ind,args) \
{\
id,\
ide,\
rtr,\
mode,\
mask,\
-1,\
ind,\
args,\
}
#define RT_CAN_FILTER_STD_INIT(id,ind,args) \
RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,ind,args)
#define RT_CAN_FILTER_EXT_INIT(id,ind,args) \
RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,ind,args)
#define RT_CAN_STD_RMT_FILTER_INIT(id,ind,args) \
RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,ind,args)
#define RT_CAN_EXT_RMT_FILTER_INIT(id,ind,args) \
RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,ind,args)
#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,ind,args) \
RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,ind,args)
#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,ind,args) \
RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,ind,args)
#else
#define RT_CAN_FILTER_ITEM_INIT(id,ide,rtr,mode,mask,args) \
{\
id,\
ide,\
rtr,\
mode,\
mask,\
-1,\
args,\
}
#define RT_CAN_FILTER_STD_INIT(id,args) \
RT_CAN_FILTER_ITEM_INIT(id,0,0,0,0xFFFFFFFF,args)
#define RT_CAN_FILTER_EXT_INIT(id,args) \
RT_CAN_FILTER_ITEM_INIT(id,1,0,0,0xFFFFFFFF,args)
#define RT_CAN_STD_RMT_FILTER_INIT(id,args) \
RT_CAN_FILTER_ITEM_INIT(id,0,1,0,0xFFFFFFFF,args)
#define RT_CAN_EXT_RMT_FILTER_INIT(id,args) \
RT_CAN_FILTER_ITEM_INIT(id,1,1,0,0xFFFFFFFF,args)
#define RT_CAN_STD_RMT_DATA_FILTER_INIT(id,args) \
RT_CAN_FILTER_ITEM_INIT(id,0,0,1,0xFFFFFFFF,args)
#define RT_CAN_EXT_RMT_DATA_FILTER_INIT(id,args) \
RT_CAN_FILTER_ITEM_INIT(id,1,0,1,0xFFFFFFFF,args)
#endif
struct
rt_can_filter_config
{
rt_uint32_t
count
;
rt_uint32_t
actived
;
struct
rt_can_filter_item
*
items
;
};
struct
can_configure
{
rt_uint32_t
baud_rate
;
rt_uint32_t
msgboxsz
;
rt_uint32_t
sndboxnumber
;
rt_uint32_t
mode
:
8
;
rt_uint32_t
privmode
:
8
;
rt_uint32_t
reserved
:
16
;
#ifdef RT_CAN_USING_LED
const
struct
rt_can_led
*
rcvled
;
const
struct
rt_can_led
*
sndled
;
const
struct
rt_can_led
*
errled
;
#endif
/*RT_CAN_USING_LED*/
rt_uint32_t
ticks
;
#ifdef RT_CAN_USING_HDR
rt_uint32_t
maxhdr
;
#endif
};
#define CANDEFAULTCONFIG \
{\
CAN1MBaud,\
RT_CANMSG_BOX_SZ,\
RT_CANSND_BOX_NUM,\
RT_CAN_MODE_NORMAL,\
};
struct
rt_can_ops
;
#define RT_CAN_CMD_SET_FILTER 0x13
#define RT_CAN_CMD_SET_BAUD 0x14
#define RT_CAN_CMD_SET_MODE 0x15
#define RT_CAN_CMD_SET_PRIV 0x16
#define RT_CAN_CMD_GET_STATUS 0x17
#define RT_CAN_CMD_SET_STATUS_IND 0x18
#define RT_DEVICE_CAN_INT_ERR 0x1000
enum
RT_CAN_STATUS_MODE
{
NORMAL
=
0
,
ERRWARNING
=
1
,
ERRPASSIVE
=
2
,
BUSOFF
=
4
,
};
enum
RT_CAN_BUS_ERR
{
RT_CAN_BUS_NO_ERR
=
0
,
RT_CAN_BUS_BIT_PAD_ERR
=
1
,
RT_CAN_BUS_FORMAT_ERR
=
2
,
RT_CAN_BUS_ACK_ERR
=
3
,
RT_CAN_BUS_IMPLICIT_BIT_ERR
=
4
,
RT_CAN_BUS_EXPLICIT_BIT_ERR
=
5
,
RT_CAN_BUS_CRC_ERR
=
6
,
};
struct
rt_can_status
{
rt_uint32_t
rcverrcnt
;
rt_uint32_t
snderrcnt
;
rt_uint32_t
errcode
;
rt_uint32_t
rcvpkg
;
rt_uint32_t
dropedrcvpkg
;
rt_uint32_t
sndpkg
;
rt_uint32_t
dropedsndpkg
;
rt_uint32_t
bitpaderrcnt
;
rt_uint32_t
formaterrcnt
;
rt_uint32_t
ackerrcnt
;
rt_uint32_t
biterrcnt
;
rt_uint32_t
crcerrcnt
;
rt_uint32_t
rcvchange
;
rt_uint32_t
sndchange
;
rt_uint32_t
lasterrtype
;
};
#ifdef RT_CAN_USING_HDR
struct
rt_can_hdr
{
rt_uint32_t
connected
;
rt_uint32_t
msgs
;
struct
rt_can_filter_item
filter
;
struct
rt_list_node
list
;
};
#endif
struct
rt_can_device
;
typedef
rt_err_t
(
*
rt_canstatus_ind
)(
struct
rt_can_device
*
,
void
*
);
typedef
struct
rt_can_status_ind_type
{
rt_canstatus_ind
ind
;
void
*
args
;
}
*
rt_can_status_ind_type_t
;
struct
rt_can_device
{
struct
rt_device
parent
;
const
struct
rt_can_ops
*
ops
;
struct
can_configure
config
;
struct
rt_can_status
status
;
rt_uint32_t
timerinitflag
;
struct
rt_timer
timer
;
struct
rt_can_status_ind_type
status_indicate
;
#ifdef RT_CAN_USING_HDR
struct
rt_can_hdr
*
hdr
;
#endif
void
*
can_rx
;
void
*
can_tx
;
};
typedef
struct
rt_can_device
*
rt_can_t
;
#define RT_CAN_STDID 0
#define RT_CAN_EXTID 1
#define RT_CAN_DTR 0
#define RT_CAN_RTR 1
typedef
struct
rt_can_status
*
rt_can_status_t
;
struct
rt_can_msg
{
rt_uint32_t
id
:
29
;
rt_uint32_t
ide
:
1
;
rt_uint32_t
rtr
:
1
;
rt_uint32_t
rsv
:
1
;
rt_uint32_t
len
:
8
;
rt_uint32_t
priv
:
8
;
rt_uint32_t
hdr
:
8
;
rt_uint32_t
reserved
:
8
;
rt_uint8_t
data
[
8
];
};
typedef
struct
rt_can_msg
*
rt_can_msg_t
;
struct
rt_can_msg_list
{
struct
rt_list_node
list
;
#ifdef RT_CAN_USING_HDR
struct
rt_list_node
hdrlist
;
struct
rt_can_hdr
*
owner
;
#endif
struct
rt_can_msg
data
;
};
struct
rt_can_rx_fifo
{
/* software fifo */
struct
rt_can_msg_list
*
buffer
;
rt_uint32_t
freenumbers
;
struct
rt_list_node
freelist
;
struct
rt_list_node
uselist
;
};
#define RT_CAN__SND_RESUTL_OK 0
#define RT_CAN__SND_RESUTL_ERR 1
#define RT_CAN__SND_RESUTL_WAIT 2
#define RT_CAN_EVENT_RX_IND 0x01
/* Rx indication */
#define RT_CAN_EVENT_TX_DONE 0x02
/* Tx complete */
#define RT_CAN_EVENT_TX_FAIL 0x03
/* Tx complete */
#define RT_CAN_EVENT_RX_TIMEOUT 0x05
/* Rx timeout */
#define RT_CAN_EVENT_RXOF_IND 0x06
/* Rx overflow */
struct
rt_can_sndbxinx_list
{
struct
rt_list_node
list
;
struct
rt_completion
completion
;
rt_uint32_t
result
;
};
struct
rt_can_tx_fifo
{
struct
rt_can_sndbxinx_list
*
buffer
;
struct
rt_completion
completion
;
struct
rt_list_node
freelist
;
};
struct
rt_can_ops
{
rt_err_t
(
*
configure
)(
struct
rt_can_device
*
can
,
struct
can_configure
*
cfg
);
rt_err_t
(
*
control
)(
struct
rt_can_device
*
can
,
int
cmd
,
void
*
arg
);
int
(
*
sendmsg
)(
struct
rt_can_device
*
can
,
const
void
*
buf
,
rt_uint32_t
boxno
);
int
(
*
recvmsg
)(
struct
rt_can_device
*
can
,
void
*
buf
,
rt_uint32_t
boxno
);
};
rt_err_t
rt_hw_can_register
(
struct
rt_can_device
*
can
,
const
char
*
name
,
const
struct
rt_can_ops
*
ops
,
void
*
data
);
void
rt_hw_can_isr
(
struct
rt_can_device
*
can
,
int
event
);
#endif
/*_CAN_H*/
components/drivers/include/rtdevice.h
浏览文件 @
eb56b832
...
...
@@ -366,6 +366,10 @@ rt_inline void rt_work_init(struct rt_work* work, void (*work_func)(struct rt_wo
#include "drivers/pin.h"
#endif
#ifdef RT_USING_CAN
#include "drivers/can.h"
#endif
#ifdef __cplusplus
}
#endif
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录