7.1.5. 设计说明¶
7.1.5.1. 源码说明¶
CAN模块的源码位于:
- bsp/artinchip/drv/can/drv_can.c,CAN模块driver层源码 
- bsp/artinchip/hal/can/aic_hal_can.c,CAN模块hal层源码 
- bsp/artinchip/include/hal/aic_hal_can.h,CAN模块hal层头文件 
7.1.5.2. 模块架构¶
 
7.1.5.3. 关键流程设计¶
7.1.5.3.2. 中断处理流程¶
在驱动中,利用中断进行数据的接收和总线错误的检测。由于CAN每帧数据量较小,所以发送时采用了轮询的方式,直到发送完所有数据。但是为了统计发送信息,也使能了发送中断,接收到发送中断后,只更新CAN的发送状态信息。
 
7.1.5.4. 数据结构设计¶
typedef struct can_handle can_handle;
struct can_handle {
    unsigned long can_base;
    uint32_t irq_num;
    uint32_t clk_id;
    uint32_t idx;
    void (*callback)(can_handle * phandle, void *arg);
    void *arg;
    uint32_t baudrate;
    can_msg_t msg;
    can_status_t status;
};
7.1.5.5. 接口设计¶
7.1.5.5.1. driver层接口设计¶
7.1.5.5.1.1. aic_can_control¶
| 函数原型 | static rt_err_t aic_can_control(struct rt_can_device *can, int cmd, void *arg) | 
|---|---|
| 功能说明 | CAN模块control接口,用于设置波特率,获取状态等 | 
| 参数定义 | can: 指向rt_can_device设备的指针 cmd: CAN命令 arg: 命令参数 | 
| 返回值 | RT_EOK:执行成功 | 
| 注意事项 | 
7.1.5.5.1.2. aic_can_send¶
| 函数原型 | static int aic_can_send(struct rt_can_device *can, const void *buf, rt_uint32_t boxno) | 
|---|---|
| 功能说明 | CAN发送接口 | 
| 参数定义 | can: 指向rt_can_device设备的指针 buf: 指向要发送的can message的指针 boxno: 发送的邮箱号 | 
| 返回值 | 0:执行成功 | 
| 注意事项 | 
7.1.5.5.1.3. aic_can_recv¶
| 函数原型 | static int aic_can_recv(struct rt_can_device *can, void *buf, rt_uint32_t boxno) | 
|---|---|
| 功能说明 | CAN接收接口 | 
| 参数定义 | can: 指向rt_can_device设备的指针 buf: 指向存储接收的can message的指针 boxno: 接收的邮箱号 | 
| 返回值 | 0:执行成功 | 
| 注意事项 | 
7.1.5.5.2. hal层接口设计¶
7.1.5.5.2.1. hal_can_init¶
| 函数原型 | int hal_can_init(can_handle *phandle, uint32_t can_idx); | 
|---|---|
| 功能说明 | CAN模块初始化 | 
| 参数定义 | phandle: 指向can_handle结构体的指针 can_idx: 需要初始化的CAN模块索引号 | 
| 返回值 | 0:执行成功 -EINVAL:执行失败 | 
| 注意事项 | 
7.1.5.5.2.2. hal_can_uninit¶
| 函数原型 | void hal_can_uninit(can_handle *phandle); | 
|---|---|
| 功能说明 | CAN模块去初始化 | 
| 参数定义 | phandle: 指向can_handle结构体的指针 | 
| 返回值 | 无 | 
| 注意事项 | 
7.1.5.5.2.3. hal_can_tx_frame¶
| 函数原型 | void hal_can_tx_frame(can_handle *phandle, can_msg_t * msg, can_op_req_t req); | 
|---|---|
| 功能说明 | CAN模块去初始化 | 
| 参数定义 | phandle: 指向can_handle结构体的指针 msg: 指向要发送的CAN message的指针 req: 发送请求,用于指示是正常发送/自发自收/终止发送 | 
| 返回值 | 无 | 
| 注意事项 | 
7.1.5.5.2.4. hal_can_ioctl¶
| 函数原型 | int hal_can_ioctl(can_handle *phandle, int cmd, void *arg); | 
|---|---|
| 功能说明 | CAN模块去初始化 | 
| 参数定义 | phandle: 指向can_handle结构体的指针 cmd: CAN命令 arg: 命令参数 | 
| 返回值 | 无 | 
| 注意事项 | 
