全志A64 設(shè)備樹里的gpio應(yīng)用開發(fā)
微信公眾號(hào):morixinguan
關(guān)注可了解更多的教程。問(wèn)題或建議,請(qǐng)公眾號(hào)留言;
[如果你覺(jué)得Md2All對(duì)你有幫助,歡迎交流討論]
如需本平臺(tái)推廣以及其它合作,請(qǐng)加微信:morixinguan
通過(guò)A64手冊(cè)可以看出共有兩個(gè)普通gpio控制器:?
?
gpio控制器是由廠商負(fù)責(zé)驅(qū)動(dòng)好的,在設(shè)備樹里關(guān)于gpio控制器的描述:
pio:?pinctrl@01c20800?{
???compatible?=?"allwinner,sun50i-pinctrl";
???reg?=?<0x0?0x01c20800?0x0?0x400>;
???interrupts?=?<0?11?4>,
?????????<0?17?4>,
?????????<0?21?4>;
???device_type?=?"pio";
???clocks?=?<&clk_pio>;
???gpio-controller;
???interrupt-controller;
???#interrupt-cells?=?<2>;
???#size-cells?=?<0>;
???#gpio-cells?=?<6>;???
r_pio:?pinctrl@01f02c00?{
???compatible?=?"allwinner,sun50i-r-pinctrl";
???reg?=?<0x0?0x01f02c00?0x0?0x400>;
???interrupts?=?<0?45?4>;
???clocks?=?<&clk_cpurpio>;
???device_type?=?"r_pio";
???gpio-controller;
???interrupt-controller;
???#interrupt-cells?=?<2>;
???#size-cells?=?<0>;
???#gpio-cells?=?<6>;
關(guān)于設(shè)備樹設(shè)置GPIO的文檔描述:
其中“?#gpio-cells?=?<6>”表示在設(shè)備樹里描述使用一個(gè)gpio口需要提供6個(gè)指定的參數(shù).
通過(guò)文檔,可以得知6個(gè)參數(shù)的分別作用:
gpio?=?<&pio???1???1???1???1???1??0>;
????|??????|????|???|???|???|???|??|-------------------表示有效電平
????|??????|????|???|???|???|???|----------------------上下拉,?0關(guān)閉功能,?1上拉,?2下拉,?3保留
????|??????|????|???|???|???|-------------------------驅(qū)動(dòng)力,電流等級(jí)(0?-?3),級(jí)別越高,輸出電流越大
????|??????|????|???|???|----------------------------gpio功能類型,0輸入,?1輸出,?6和外部中斷,7關(guān)閉功能(具體查手冊(cè))
????|??????|????|???|------------------------------pin?bank?內(nèi)偏移(即組內(nèi)第幾個(gè)io口).
????|??????|????|---------------------------------哪組gpio,?PA(0),PB(1),PC(2),PD(3),PE(4),PF(5),PG(6),PH(7),PI(8),PJ(9),PK(10),PL(11)
????|??????|--------------------------------------指向哪個(gè)gpio控制器,??pio?/?r_pio(PL組)
????|-----------------------------------------------------屬性名字(隨便命名)
那么在Linux驅(qū)動(dòng)里如何獲取設(shè)備樹里設(shè)備節(jié)點(diǎn)的gpio口信息呢?
#include?
//只需一個(gè)函數(shù)即可
int?of_get_named_gpio_flags(struct?device_node?*np,?const?char?*propname,
????int?index,?enum?of_gpio_flags?*flags);
//返回值為int類型的gpio口.
//np為設(shè)備或設(shè)備子節(jié)點(diǎn)對(duì)象,?propname為指定的屬性名字,?index表示獲取屬性里的第幾個(gè)值
//?其中flags一定得注意,按文檔里的說(shuō)明應(yīng)就是一個(gè)int類型的值,但根本就不能為int參數(shù)(會(huì)導(dǎo)致kernel?panic),
//?通過(guò)閱讀內(nèi)核里的代碼得出,?flags的參數(shù)應(yīng)為struct?gpio_config類型.?定義在下面文件:
"include/linux/sys_config.h"
struct?gpio_config?{
?????u32?gpio;???????/*?gpio?global?index,?must?be?unique?*/
?????u32?????mul_sel;????/*?multi?sel?val:?0?-?input,?1?-?output...?*/
?????u32?????pull;???????/*?pull?val:?0?-?pull?up/down?disable,?1?-?pull?up...?*/
?????u32?????drv_level;??/*?driver?level?val:?0?-?level?0,?1?-?level?1...?*/
?????u32?data;???????/*?data?val:?0?-?low,?1?-?high,?only?vaild?when?mul_sel?is?input/output?*/
?};
獲取到int類型的gpio口后,就可以使用linux/gpio.h里的gpio口操作函數(shù):
#include??//里面聲明io口的操作函數(shù)
int?gpio_request(unsigned?gpio,?const?char?*label);//每個(gè)io只能被請(qǐng)求一次,可防止多個(gè)驅(qū)動(dòng)來(lái)控制同一個(gè)IO口
void?gpio_free(unsigned?gpio);?//釋放已請(qǐng)求的io口
int?gpio_direction_input(unsigned?gpio);?//把指定的IO口作輸入功能,?gpio用于指定具體哪個(gè)io口
int?gpio_direction_output(unsigned?gpio,?int?value);?//作輸出功能,并根據(jù)value的值輸出高低電平
int?gpio_get_value(unsigned?gpio);?//獲取指定IO口的電平
void?gpio_set_value(unsigned?gpio,?int?value);?//設(shè)置IO口的電平為value(0/1)
int?gpio_to_irq(unsigned?gpio);??//根據(jù)io口,獲取到它對(duì)應(yīng)的中斷號(hào)(io口大都有外部中斷功能)
應(yīng)用例子,如圖板上有兩個(gè)led,和一個(gè)蜂鳴器:?
設(shè)備樹里添加對(duì)這兩個(gè)LED和蜂鳴器的描述:
jkbuzzer?{
???compatible?=?"jk,buzzer";
???gpios?=?<&pio?3?24?1?1?1?1>;?
?};
?jkleds?{
???compatible?=?"jk,leds";
???gpios?=?<&r_pio?11?10?1?1?1?1>,?<&r_pio?11?12?1?1?1?1>;
?};
在Linux驅(qū)動(dòng)中編寫蜂鳴器的測(cè)試代碼 如下:
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
int?myprobe(struct?platform_device?*pdev)
{
????struct?device_node?*nd?=?pdev->dev.of_node;
????int?gpio;???
????struct?gpio_config?config;
????printk("gpio?count:%d\n",?of_gpio_named_count(nd,?"gpios"));
????gpio?=?of_get_named_gpio_flags(nd,?"gpios",?0,?(enum?of_gpio_flags?*)&config);
????if?(!gpio_is_valid(gpio))
????????printk("gpio?isn't?valid\n");
????if?(gpio_request(gpio,?pdev->name)?0)
????????????printk("gpio?request?failed?%d\n",?gpio);
????gpio_direction_output(gpio,?1);
????msleep(3000);
????gpio_direction_input(gpio);
????gpio_free(gpio);
????return?0;
}
int?myremove(struct?platform_device?*pdev)
{
????printk("in?myremove?...\n");
????return?0;
}
struct?of_device_id?ids[]?=?{
????{.compatible?=?"jk,buzzer"},
????{},
};
struct?platform_driver?mydrv?=?{
????.probe?=?myprobe,
????.remove?=?myremove,
????.driver?=?{
????????.owner?=?THIS_MODULE,
????????.name?=?"mydrv"?,
????????.of_match_table?=?ids,
????},
};
module_platform_driver(mydrv);
MODULE_LICENSE("GPL");
在Linux驅(qū)動(dòng)中編寫兩個(gè)LED的驅(qū)動(dòng)測(cè)試代碼如下:
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#include?
int?myprobe(struct?platform_device?*pdev)
{
????struct?device_node?*nd?=?pdev->dev.of_node;
????int?gpio,?n,?i;?
????struct?gpio_config?config;
????n?=?of_gpio_named_count(nd,?"gpios");
????for?(i?=?0;?i?????{
????????gpio?=?of_get_named_gpio_flags(nd,?"gpios",?i,?(enum?of_gpio_flags?*)&config);
????????if?(!gpio_is_valid(gpio))
????????????printk("gpio?isn't?valid\n");
????????if?(gpio_request(gpio,?pdev->name)?0)
????????????printk("gpio?request?failed?%d\n",?gpio);
????????gpio_direction_output(gpio,?1);
????????msleep(3000);
????????gpio_direction_input(gpio);
????????gpio_free(gpio);
????}
????return?0;
}
int?myremove(struct?platform_device?*pdev)
{
????printk("in?myremove?...\n");
????return?0;
}
struct?of_device_id?ids[]?=?{
????{.compatible?=?"jk,leds"},
????{},
};
struct?platform_driver?mydrv?=?{
????.probe?=?myprobe,
????.remove?=?myremove,
????.driver?=?{
????????.owner?=?THIS_MODULE,
????????.name?=?"mydrv"?,
????????.of_match_table?=?ids,
????},
};
免責(zé)聲明:本文內(nèi)容由21ic獲得授權(quán)后發(fā)布,版權(quán)歸原作者所有,本平臺(tái)僅提供信息存儲(chǔ)服務(wù)。文章僅代表作者個(gè)人觀點(diǎn),不代表本平臺(tái)立場(chǎng),如有問(wèn)題,請(qǐng)聯(lián)系我們,謝謝!