Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
CAN总线
drv_can.c_CAN设备驱动
RTthread 使用CAN通讯硬件端口怎么配置的?
发布于 2021-06-02 22:42:38 浏览:2103
订阅该版
之前一直用裸机,最近才开始用rtthread,看了drv_can.c的程序,关于设置硬件端口的地方不是太明白,请各位大神指导一下。 rt_err_t rt_hw_can_register(struct rt_can_device *can, const char *name, const struct rt_can_ops *ops, void *data) { struct rt_device *device; RT_ASSERT(can != RT_NULL); device = &(can->parent); device->type = RT_Device_Class_CAN; device->rx_indicate = RT_NULL; device->tx_complete = RT_NULL; #ifdef RT_CAN_USING_HDR can->hdr = RT_NULL; #endif can->can_rx = RT_NULL;//这地方是配置什么的? can->can_tx = RT_NULL;// rt_mutex_init(&(can->lock), "can", RT_IPC_FLAG_PRIO); #ifdef RT_CAN_USING_BUS_HOOK can->bus_hook = RT_NULL; #endif /*RT_CAN_USING_BUS_HOOK*/ #ifdef RT_USING_DEVICE_OPS device->ops = &can_device_ops; #else device->init = rt_can_init; device->open = rt_can_open; device->close = rt_can_close; device->read = rt_can_read; device->write = rt_can_write; device->control = rt_can_control; #endif can->ops = ops; can->status_indicate.ind = RT_NULL; can->status_indicate.args = RT_NULL; rt_memset(&can->status, 0, sizeof(can->status)); device->user_data = data; can->timerinitflag = 0; rt_timer_init(&can->timer, name, cantimeout, (void *)can, can->config.ticks, RT_TIMER_FLAG_PERIODIC); /* register a character device */ return rt_device_register(device, name, RT_DEVICE_FLAG_RDWR); } 有没有配置好的代码上来学习一下。参考着代码理解的更清楚一下。谢谢大家!
查看更多
3
个回答
默认排序
按发布时间排序
小小李sunny
2021-06-02
这家伙很懒,什么也没写!
可以看一下官方文档,写的比较清楚,也有例程可以参考。[CAN 设备](https://www.rt-thread.org/document/site/#/rt-thread-version/rt-thread-standard/programming-manual/device/can/can)
zhkag
2021-06-04
这家伙很懒,什么也没写!
建议你先用其他RTT支持CAN的板子实验一下,看他是怎么实现的,再去改自己的,会比现在方便的多
redoc
2021-06-04
这家伙很懒,什么也没写!
```c /* * Copyright (c) 2019-2020, redoc * * SPDX-License-Identifier: Apache-2.0 * * Change Logs: * Date Author Notes * 2020-03-30 redoc the first version */ #define LOG_TAG "can" #define LOG_LVL LOG_LVL_DBG #include
#include "rtdevice.h" #include "thread_can.h" #include "ulog.h" #define CAN_DEV_NAME "can1" #define CAN1_CONFIG \ { \ CAN500kBaud, \ RT_CANMSG_BOX_SZ, \ RT_CANSND_BOX_NUM, \ RT_CAN_MODE_NORMAL, \ }; static rt_sem_t sem_can_rx; static rt_sem_t sem_can_tx; static rt_device_t can_dev; static rt_err_t can_rx_call(rt_device_t dev, rt_size_t size) { rt_sem_release(sem_can_rx); return RT_EOK; } static void can_rx_thread(void *parameter) { rt_err_t res; struct rt_can_msg rxmsg = {0}; #ifdef RT_CAN_USING_HDR struct rt_can_filter_item items[2] = { // id, ide, rtr, mode, mask, hdr {0x30 << 18, 0, 0, 0, (((rt_uint32_t)0x7f8 << 21)| 6), 0}, {0x40 << 18, 0, 0, 1, (((rt_uint32_t)0x7ff << 21)| 6), 1}, }; struct rt_can_filter_config cfg = {2, 1, items}; res = rt_device_control(can_dev, RT_CAN_CMD_SET_FILTER, &cfg); RT_ASSERT(res == RT_EOK); #endif rt_thread_mdelay(1000); while (1) { rxmsg.hdr = -1; if(RT_EOK == rt_sem_take(sem_can_rx, 20)) { rt_device_read(can_dev, 0, &rxmsg, sizeof(rxmsg)); // rt_kprintf("ID:%x Len:%d Data:", rxmsg.id, rxmsg.len); // for (int i = 0; i < 8; i++) // { // rt_kprintf("%2x ", rxmsg.data[i]); // } // rt_kprintf("\r\n"); // 解析数据 id data len //can_process_unpack(rxmsg.id, rxmsg.data, rxmsg.len); } } } int can_send(uint16_t can_id, uint8_t *buf, uint8_t len) { struct rt_can_msg msg = {0}; uint16_t size; rt_sem_take(sem_can_tx, RT_WAITING_FOREVER); msg.id = can_id; msg.ide = RT_CAN_STDID; msg.rtr = RT_CAN_DTR; msg.len = len; rt_memcpy(msg.data, buf, len); size = rt_device_write(can_dev, 0, &msg, sizeof(msg)); if (size == 0) { rt_kprintf("can dev write data failed!\n"); } rt_sem_release(sem_can_tx); return size; } int thread_can_init(void) { rt_err_t res; rt_thread_t thread; struct can_configure cfg = CAN1_CONFIG; can_dev = rt_device_find(CAN_DEV_NAME); if (!can_dev) { rt_kprintf("find %s failed!\n", CAN_DEV_NAME); return -RT_ERROR; } sem_can_rx = rt_sem_create("can_rx", 0, RT_IPC_FLAG_FIFO); sem_can_tx = rt_sem_create("can_tx", 1, RT_IPC_FLAG_FIFO); res = rt_device_open(can_dev, RT_DEVICE_OFLAG_RDWR | RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_INT_RX); RT_ASSERT(res == RT_EOK); rt_device_control(can_dev, RT_DEVICE_CTRL_CONFIG, &cfg); rt_device_set_rx_indicate(can_dev, can_rx_call); thread = rt_thread_create("can_rx", can_rx_thread, RT_NULL, 2048, 16, 100); if (thread != RT_NULL) { rt_thread_startup(thread); } else { log_e("create can_rx thread failed!"); } return res; } ```
撰写答案
登录
注册新账号
关注者
0
被浏览
2.1k
关于作者
冰棒棍儿
这家伙很懒,什么也没写!
提问
4
回答
1
被采纳
0
关注TA
发私信
相关问题
1
我也来传一个CANOpen移植,RTT+STM32F107+CanOpenNode
2
谁有STM32裸跑的CANopen程序啊???
3
CAN驱动程序框架
4
CAN驱动接口如何规范一下
5
RTT无法进入线程.Cannot access Memory
6
编译提示arm-none-eabi/bin/ld: cannot find crt0.o: No such file o
7
rtt 2.1.0 正式版 mdk4 bsp/stm32 编译canapp.c错误
8
STM32F10XCAN驱动使用的问题
9
2.1版本stm32f10x分支bxcan驱动波特率设置的bug
10
rtthread2.1.0下,找不到can1设备
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
使用百度AI助手辅助编写一个rt-thread下的ONVIF设备发现功能的功能代码
2
RT-Thread 发布 EtherKit开源以太网硬件!
3
rt-thread使用cherryusb实现虚拟串口
4
《C++20 图形界面程序:速度与渲染效率的双重优化秘籍》
5
《原子操作:程序世界里的“最小魔法单位”解析》
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
USB
DMA
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
FAL
rt-smart
ESP8266
I2C_IIC
WIZnet_W5500
ota在线升级
UART
PWM
cubemx
freemodbus
flash
packages_软件包
BSP
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
中断
Debug
编译报错
msh
SFUD
keil_MDK
rt_mq_消息队列_msg_queue
at_device
ulog
C++_cpp
本月问答贡献
踩姑娘的小蘑菇
7
个答案
3
次被采纳
张世争
8
个答案
2
次被采纳
rv666
5
个答案
2
次被采纳
用户名由3_15位
11
个答案
1
次被采纳
KunYi
6
个答案
1
次被采纳
本月文章贡献
程序员阿伟
6
篇文章
2
次点赞
hhart
3
篇文章
4
次点赞
大龄码农
1
篇文章
2
次点赞
ThinkCode
1
篇文章
1
次点赞
Betrayer
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部