Pin驅(qū)動(dòng)框架

以NXP MCXA153為例
PIN設(shè)備驅(qū)動(dòng)層
單純的提供接口給應(yīng)用層用,其中PIN設(shè)備驅(qū)動(dòng)框架接口包含rt_pin_read等,具體在pin.c 文件中查看
pin.c是提供應(yīng)用接口
drv_gpio.c是具體實(shí)現(xiàn)
實(shí)現(xiàn)操作方法原理
struct rt_pin_ops{ void (*pin_mode)(struct rt_device *device, rt_base_t pin, rt_uint8_t mode); void (*pin_write)(struct rt_device *device, rt_base_t pin, rt_uint8_t value); rt_ssize_t (*pin_read)(struct rt_device *device, rt_base_t pin); rt_err_t (*pin_attach_irq)(struct rt_device *device, rt_base_t pin, rt_uint8_t mode, void (*hdr)(void *args), void *args); rt_err_t (*pin_detach_irq)(struct rt_device *device, rt_base_t pin); rt_err_t (*pin_irq_enable)(struct rt_device *device, rt_base_t pin, rt_uint8_t enabled); rt_base_t (*pin_get)(const char *name);#ifdef RT_USING_DM rt_err_t (*pin_irq_mode)(struct rt_device *device, rt_base_t pin, rt_uint8_t mode); rt_ssize_t (*pin_parse)(struct rt_device *device, struct rt_ofw_cell_args *args, rt_uint32_t *flags);#endif#ifdef RT_USING_PINCTRL rt_err_t (*pin_ctrl_confs_apply)(struct rt_device *device, void *fw_conf_np);#endif /* RT_USING_PINCTRL */};
rt_pin_ops 成員介紹
pin_mode
引腳初始化
pin_write
引腳寫
pin_read
引腳讀
pin_attach_irq
中斷操作 為某個(gè)引腳綁定一個(gè)中斷回調(diào)函數(shù),使能中斷,當(dāng)中斷來(lái)時(shí)調(diào)用該函數(shù)
pin_detach_irq
中斷操作 脫離某個(gè)引腳的中斷回調(diào)函數(shù)
pin_irq_enable
中斷操作 開(kāi)啟或關(guān)閉中斷
pin_get
獲取某個(gè)pin腳編號(hào)
pin_irq_mode
pin_parse
pin_ctrl_confs_apply
引腳編號(hào)
#define GET_GPIO_PORT(x) ((x) / 32)#defineGET_GPIO_PIN(x)((x)%32)
rt_pin_ops 賦值
rt_hw_pin_init(){ int ret = RT_EOK; mcx_pin_ops.pin_mode = mcx_pin_mode; mcx_pin_ops.pin_read = mcx_pin_read; mcx_pin_ops.pin_write = mcx_pin_write; mcx_pin_ops.pin_attach_irq = mcx_pin_attach_irq; mcx_pin_ops.pin_detach_irq = mcx_pin_detach_irq; mcx_pin_ops.pin_irq_enable = mcx_pin_irq_enable; mcx_pin_ops.pin_get = RT_NULL, ret = rt_device_pin_register("pin", &mcx_pin_ops, RT_NULL);// return ret;}INIT_BOARD_EXPORT(rt_hw_pin_init);
注意:這里的操作很奇怪INIT_BOARD_EXPORT ,根據(jù)老師的講解是在預(yù)編譯的時(shí)候就處理了,具體后面再學(xué)習(xí)。。。
drv_pin.c 的 rt_hw_pin_init 將底層驅(qū)動(dòng)和驅(qū)動(dòng)框架進(jìn)行鏈接起來(lái),此文件實(shí)現(xiàn)gpio的初始化
添加Pin驅(qū)動(dòng)代碼流程
編寫drv_pin.c文件
實(shí)現(xiàn) rt_pin_ops 的各種操作接口函數(shù)
然后利用 rt_hw_pin_init 進(jìn)行鏈接驅(qū)動(dòng)層
實(shí)際上就是指針的賦值
實(shí)驗(yàn)代碼
rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT); /* Set GPIO as Output */rt_pin_mode(KEY_BAND, PIN_MODE_INPUT);rt_kprintf("MCXA153 HelloWorld\r\n");while (1){ rt_thread_mdelay(1000); if(rt_pin_read(KEY_BAND)) rt_pin_write(LED_PIN, PIN_HIGH); /* Set GPIO output 1 */ else rt_pin_write(LED_PIN, PIN_LOW); /* Set GPIO output 0 */ #if 0 rt_pin_write(LED_PIN, PIN_HIGH); /* Set GPIO output 1 */ rt_thread_mdelay(500); /* Delay 500mS */ rt_pin_write(LED_PIN, PIN_LOW); /* Set GPIO output 0 */ rt_thread_mdelay(500); /* Delay 500mS */ #endif}
注意:這里延遲1s,目的是為了有msh 功能
總結(jié)
學(xué)習(xí)了PIN設(shè)備驅(qū)動(dòng)框架的添加
-
驅(qū)動(dòng)
+關(guān)注
關(guān)注
12文章
1878瀏覽量
86346 -
PIN
+關(guān)注
關(guān)注
1文章
311瀏覽量
25036 - RT-Thread
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
RT-Thread NUC97x 移植 LVGL
RT-Thread編程指南
RT-Thread用戶手冊(cè)
RT-Thread Studio驅(qū)動(dòng)SD卡

【RT-Thread開(kāi)源作品秀】基于RT-Thread的星務(wù)平臺(tái)研究

RT-Thread全球技術(shù)大會(huì):Kconfig在RT-Thread中的工作機(jī)制

RT-Thread全球技術(shù)大會(huì):RT-Thread測(cè)試用例集合案例

RT-Thread學(xué)習(xí)筆記 RT-Thread的架構(gòu)概述

RT-Thread文檔_RT-Thread 潘多拉 STM32L475 上手指南

基于RT-Thread Studio學(xué)習(xí)

《RT-Thread設(shè)備驅(qū)動(dòng)開(kāi)發(fā)指南》基礎(chǔ)篇--以先楫bsp的hwtimer設(shè)備為例

評(píng)論