mtk-GPIO設置與應用
阿新 • • 發佈:2018-06-14
引腳 ima bsp mon #define i2c ini UNC 方式
一.block Dragram
一般來說,GPIO引腳功能復用,可作為EINT、GPIO、或I2C、SPI等,通過GPIO_MODE進行選擇
GPIO_DIR:控制輸入輸出的方向,為0作為input,為1作為output,默認狀態為0
GPIO_DIN:通過訪問寄存器可以得知輸入的是高電平還是低電平
GPIO_OUT:控制輸出高低電平
GPIO_PULLEN:是否支持上下拉
mtk的文檔給的block dragram中並沒有標全,還有設置上下拉,SMT,slew rate這些屬性
二.dws中的配置
依次來看下GPIO的配置項:
- EintMode:中斷模式
- M0~M7:GPIO支持的模式選擇
- Def.Mode:默認的模式,根據後面勾選的M0~M7中選擇默認模式
- InPull En:是否支持上下拉
- InPull Sel High:選擇上拉還是下拉,勾選表示上拉
- Def.Dir:input還是output
- In:input
- Out:output
- OutHigh:輸出為高電平還是低電平
- SMT#:SMT group
- SMT:是否支持SMT(schmitt trigger)功能,波形過濾,輸出方波或者脈沖波,延遲滯後具有消噪的功能。一般不用勾選
最終生成cust_gpio_boot.h和cust_gpio_usage.h兩個頭文件,取GPIO8看下實際內容,還是比較一目了然的
cust_gpio_boot.h//Configureation for Pin 8 #define GPIO8_MODE GPIO_MODE_00 #define GPIO8_DIR GPIO_DIR_OUT #define GPIO8_PULLEN GPIO_PULL_DISABLE #define GPIO8_PULL GPIO_PULL_DOWN #define GPIO8_DATAOUT GPIO_OUT_ZERO #define GPIO8_SMT GPIO_SMT_DISABLE
cust_gpio_usage.h #define GPIO_FLASH_LED_EN (GPIO8 | 0x80000000) #defineGPIO_FLASH_LED_EN_M_GPIO GPIO_MODE_00 #define GPIO_FLASH_LED_EN_M_PWM GPIO_MODE_01 #define GPIO_FLASH_LED_EN_M_PCC_PPC_IO GPIO_MODE_02 #define GPIO_FLASH_LED_EN_M_CONN_MCU_TRST_B GPIO_MODE_04 #define GPIO_FLASH_LED_EN_M_C2K_DM_JTINTP GPIO_MODE_05 #define GPIO_FLASH_LED_EN_M_IO_JTAG_TRSTN GPIO_MODE_06 #define GPIO_FLASH_LED_EN_M_DBG_MON_A GPIO_MODE_07
lk階段中對gpio進行初始化
void mt_gpio_set_default(void) { //mt_gpio_set_avoid_leakage(); mt_gpio_set_default_chip(); //mt_gpio_set_driving(); //mt_gpio_set_power(); mutex_init(&gpio_mutex); return; } void mt_gpio_set_default_chip(void) { unsigned pin = 0; for (pin = 0; pin < MT_GPIO_BASE_MAX; pin++) { /* set GPIOx_MODE*/ mt_set_gpio_mode(0x80000000+pin ,gpio_array[pin].mode); /* set GPIOx_DIR*/ mt_set_gpio_dir(0x80000000+pin ,gpio_array[pin].dir); //GPIOVER("fwq2..........\n"); /* set GPIOx_PULLEN*/ mt_set_gpio_pull_enable(0x80000000+pin ,gpio_array[pin].pullen); //GPIOVER("fwq3..........\n"); /* set GPIOx_PULL*/ mt_set_gpio_pull_select(0x80000000+pin ,gpio_array[pin].pull); /* set GPIOx_DATAOUT*/ mt_set_gpio_out(0x80000000+pin ,gpio_array[pin].dataout); //GPIOVER("fwq5..........\n"); /* set GPIO0_SMT */ mt_set_gpio_smt(0x80000000+pin ,gpio_array[pin].smt); }
三.MTK 舊時API(MTK不推薦這種方法)
設置mode >> 設置dir >>設置上下拉使能 >>設置上下拉
GPIO設置為中斷:mode0 >> input >> 根據實際連接設置上下拉
mt-plat/mt-gpio.h /*direction*/ int mt_set_gpio_dir(unsigned long pin, unsigned long dir); int mt_get_gpio_dir(unsigned long pin); /*pull enable*/ int mt_set_gpio_pull_enable(unsigned long pin, unsigned long enable); int mt_get_gpio_pull_enable(unsigned long pin); /*schmitt trigger*/ int mt_set_gpio_smt(unsigned long pin, unsigned long enable); int mt_get_gpio_smt(unsigned long pin); /*IES*/ int mt_set_gpio_ies(unsigned long pin, unsigned long enable); int mt_get_gpio_ies(unsigned long pin); /*pull select*/ int mt_set_gpio_pull_select(unsigned long pin, unsigned long select); int mt_get_gpio_pull_select(unsigned long pin); /*data inversion*/ int mt_set_gpio_inversion(unsigned long pin, unsigned long enable); int mt_get_gpio_inversion(unsigned long pin); /*input/output*/ int mt_set_gpio_out(unsigned long pin, unsigned long output); int mt_get_gpio_out(unsigned long pin); int mt_get_gpio_in(unsigned long pin); /*mode control*/ int mt_set_gpio_mode(unsigned long pin, unsigned long mode); int mt_get_gpio_mode(unsigned long pin);
四.pinctrl的方法
dts中設置pinctrl的方式如下
&device{ ...... pinctrl-names = "aaa","bbb","ccc"; pinctrl-0 = <&xxx>; pinctrl-1 = <&yyy>; pinctrl-2 = <&zzz>; ...... }; &pio { xxx: config0 { pins_cmd_dat { pins = <PINMUX_GPIO1__FUNC_GPIO1>; ----設置GPIO mode,在boot/dts/中mtxxxx-pinfunc.h裏有定義 slew-rate = <0>; ----設置GPIO dir,0為input,1為output bias-pull-down = <11>; ----設置pull enable,下拉,後面的11並無影響,寫00也可以 input-schmitt-enable = <0>; ----設置SMT enable }; }; yyy: config1 { pins_cmd_dat { pins = <PINMUX_GPIO1__FUNC_GPIO1>; slew-rate = <1>; bias-pull-up = <11>; output-low; ----設置低電平輸出,只有在slew-rate設置為1時才有效,output-high就是設置為高電平輸出 input-schmitt-enable = <0>; }; }; zzz: config2 { pins_cmd_dat { pins = <PINMUX_GPIO1__FUNC_GPIO1>; slew-rate = <1>; bias-disable; ----無上下拉設置,不能與bias-pull-down/up共存 output-low; input-schmitt-enable = <0>; }; }; };
驅動中獲取pinctrl並進行配置
struct pinctrl* pinctrl0; struct pinctrl_state* config0, *config1, *config2; //獲取pinctrl句柄 pinctrl0 = devm_pinctrl_get(&pdev->dev); //根據句柄得到gpio的配置,根據pinctrl-name屬性進行匹配 config0 = pinctrl_lookup_state(pinctrl0, "aaa"); config1 = pinctrl_lookup_state(pinctrl0, "bbb"); config2 = pinctrl_lookup_state(pinctrl0, "ccc"); //配置生效 pinctrl_lookup_state(pinctrl0, config0);
mtk-GPIO設置與應用