设计说明
源代码位于 bsp/artinchip/
:
-
bsp/artinchip/drv/mtop/drv_mtop.c,MTOP Driver 层实现
-
bsp/artinchip/include/drv/drv_mtop.h,MTOP Drive 层头文件
-
bsp/artinchip/hal/mtop/hal_mtop.c,MTOP HAL 层实现
-
bsp/artinchip/include/hal/hal_mtop.h,MTOP HAL 层接口头文件
RTOS 系统中没有对应的设备框架,所以 MTOP 作为一个 Miscellaneous device 注册到 RTOS 上。如果只是使用 HAL 层也可以支持 baremetal 方式的应用场景。
关键流程设计
初始化流程
MTOP 驱动的初始化接口通过 INIT_BOARD_EXPORT(drv_mtop_init)
完成注册,其中主要步骤有:
-
初始化模块的 clk
-
注册接收回调函数
-
注册中断
数据结构设计
- mtop_dev:属于 Driver 层数据结构,记录 MTOP
控制器配置信息
struct mtop_dev { struct rt_device dev; struct aic_mtop_dev mtop_handle; char *name; };
- aic_mtop_dev:属于 HAL 层数据结构,记录 MTOP 的通道分组和端口配置信息
接口设计
函数原型 | rt_err_t mtop_ops_init(rt_device_t dev) |
---|---|
功能说明 | MTOP 控制器的初始化 |
参数定义 | dev - 指向 MTOP 设备 |
返回值 | 0,成功 |
注意事项 | - |
函数原型 | rt_err_t mtop_ops_open(rt_device_t dev) |
---|---|
功能说明 | 打开 MTOP 设备 |
参数定义 | dev - 指向 MTOP 设备 |
返回值 | 0,成功 |
注意事项 | - |
函数原型 | rt_err_t mtop_ops_close(rt_device_t dev) |
---|---|
功能说明 | 关闭 MTOP 设备 |
参数定义 | dev - 指向 MTOP 设备 |
返回值 | 0,成功 |
注意事项 | - |
函数原型 | rt_err_t mtop_ops_control(rt_device_t *dev, int cmd, void *args) |
---|---|
功能说明 | MTOP 驱动的 ioctl 接口 |
参数定义 | dev - 指向 MTOP 设备 cmd - ioctl 命令码 ioctl 命令相应的参数 |
返回值 | 0,成功;<0,失败 |
注意事项 | - |
MTOP 的 cmd 命令码的定义(详见
bsp/arcinchip/include/drv/aic_drv_mtop.h):
enum { MTOP_SET_PERIOD_MODE, MTOP_ENABLE, };
HAL 层接口设计
HAL 层的函数接口声明存放在 aic_hal_mtop.h
中,主要接口有:
int hal_mtop_init(struct aic_mtop_dev *phandle); int hal_mtop_deinit(struct aic_mtop_dev *phandle); void hal_mtop_enable(struct aic_mtop_dev *phandle); void hal_mtop_irq_enable(struct aic_mtop_dev *phandle); irqreturn_t hal_mtop_irq_handler(int irq_num, void *can_handle); void hal_mtop_set_period_cnt(struct aic_mtop_dev *phandle, uint32_t period_cnt); void hal_mtop_attach_callback(struct aic_mtop_dev *phandle, void *callback, void *arg); void hal_mtop_detach_callback(struct aic_mtop_dev *phandle);