GNU/Linux - U-BOOT的GPIO command

在嵌入式Linux开发中,先运行的是u-boot,然后再加载Linux内核。

启动时如果设置了u-boot等待时间,在等待时间内,按任意键就会进入u-boot命令行环境。

在u-boot命令行环境下,可以使用gpio命令来操作GPIO。

Synopsis

gpio <input|set|clear|toggle> <pin>

gpio read <name> <pin>

gpio status [-a] [<bank>|<pin>]

gpio 命令用于访问通用输入/输出。

The gpio command is used to access General Purpose Inputs/Outputs.

gpio input

将 GPIO 引脚切换为输入模式。

Switch the GPIO pin to input mode.

gpio set

将 GPIO 引脚切换至输出模式,并将信号设置为 1。

Switch the GPIO pin to output mode and set the signal to 1.

gpio clear

将 GPIO 引脚切换至输出模式,并将信号设置为 0。

Switch the GPIO pin to output mode and set the signal to 0.

gpio toggle

将 GPIO 引脚切换至输出模式,并反转信号状态。

Switch the GPIO pin to output mode and reverse the signal state.

gpio read

读取 GPIO 引脚的信号状态,并将其保存在环境变量名称中。

Read the signal state of the GPIO pin and save it in environment variable name.

gpio status

显示一个或多个 GPIO 的状态。默认情况下,只显示声称的 GPIO。 gpio status 命令的输出字段有:

Display the status of one or multiple GPIOs. By default only claimed GPIOs are displayed. gpio status command output fields are:

<name>: <function>: <value> [x] <label>

函数可以取以下值:

function can take the following values:

  • output

pin configured in gpio output, value indicates the pin’s level

  • input

pin configured in gpio input, value indicates the pin’s level

  • func

pin configured in alternate function, followed by label which shows pinmuxing label.

  • unused

pin not configured

[x] or [ ] indicate respectively if the gpio is used or not.

label shows the gpio label.

Parameters

  • -a

Display GPIOs irrespective of being claimed. 显示 GPIO,无论其是否被申领。

  • bank

Name of a bank of GPIOs to be displayed. 显示 GPIO 组的名称。

  • pin

Name of a single GPIO to be displayed or manipulated.要显示或操作的单个 GPIO 的名称。

Examples

Switch the status of a GPIO:

=> gpio set a5

gpio: pin a5 (gpio 133) value is 1

=> gpio clear a5

gpio: pin a5 (gpio 133) value is 0

=> gpio toggle a5

gpio: pin a5 (gpio 133) value is 1

=> gpio read myvar a5

gpio: pin a5 (gpio 133) value is 1

=> echo $myvar

1

=> gpio toggle a5

gpio: pin a5 (gpio 133) value is 0

=> gpio read myvar a5

gpio: pin a5 (gpio 133) value is 0

=> echo $myvar

0

Show the GPIO status:

=> gpio status

Bank GPIOA:

GPIOA1: func rgmii-0

GPIOA2: func rgmii-0

GPIOA7: func rgmii-0

GPIOA10: output: 0 [x] hdmi-transmitter@39.reset-gpios

GPIOA13: output: 1 [x] red.gpios

Bank GPIOB:

GPIOB0: func rgmii-0

GPIOB1: func rgmii-0

GPIOB2: func uart4-0

GPIOB7: input: 0 [x] mmc@58005000.cd-gpios

GPIOB11: func rgmii-0

Configuration

gpio 命令只有在 CONFIG_CMD_GPIO=y 时可用。gpio 读取命令只有在 CONFIG_CMD_GPIO_READ=y 时可用。

The gpio command is only available if CONFIG_CMD_GPIO=y. The gpio read command is only available if CONFIG_CMD_GPIO_READ=y.

Return value

如果命令成功执行,返回值 $? 设置为 0;如果命令出错,返回值 $? 设置为 1。

If the command succeds the return value $? is set to 0. If an error occurs, the return value $? is set to 1.


我的u-boot版本和构建信息:

u-boot=> help

...

version   - print monitor, compiler and linker version

...

u-boot=> version

U-Boot 2023.04-lf_v2023.04+g49b102d988 (Nov 21 2023 - 07:28:53 +0000)

aarch64-poky-linux-gcc (GCC) 12.3.0

GNU ld (GNU Binutils) 2.40.0.20230703

u-boot=> gpio status -a

Bank GPIO2_:

GPIO2_0: input: 1 [ ]

GPIO2_1: output: 0 [ ]

GPIO2_2: input: 0 [ ]

GPIO2_3: input: 0 [ ]

GPIO2_4: input: 0 [ ]

GPIO2_5: input: 0 [ ]

GPIO2_6: input: 0 [ ]

GPIO2_7: input: 0 [ ]

GPIO2_8: input: 0 [ ]

GPIO2_9: input: 0 [ ]

GPIO2_10: input: 0 [ ]

GPIO2_11: output: 0 [ ]

GPIO2_12: input: 0 [ ]

GPIO2_13: input: 1 [ ]

GPIO2_14: input: 0 [ ]

GPIO2_15: input: 0 [ ]

GPIO2_16: input: 0 [ ]

GPIO2_17: input: 0 [ ]

GPIO2_18: input: 0 [ ]

GPIO2_19: input: 0 [ ]

GPIO2_20: input: 0 [ ]

GPIO2_21: input: 0 [ ]

GPIO2_22: input: 1 [ ]

GPIO2_23: input: 0 [ ]

GPIO2_24: input: 0 [ ]

GPIO2_25: input: 0 [ ]

GPIO2_26: input: 0 [ ]

GPIO2_27: input: 0 [ ]

GPIO2_28: input: 1 [ ]

GPIO2_29: input: 0 [ ]

GPIO2_30: input: 0 [ ]

GPIO2_31: input: 0 [ ]

Bank GPIO3_:

GPIO3_0: input: 1 [x] mmc@42860000.cd-gpios

GPIO3_1: input: 0 [ ]

GPIO3_2: input: 0 [ ]

GPIO3_3: input: 0 [ ]

GPIO3_4: input: 0 [ ]

GPIO3_5: input: 0 [ ]

GPIO3_6: input: 0 [ ]

GPIO3_7: input: 0 [ ]

GPIO3_8: input: 0 [ ]

GPIO3_9: input: 0 [ ]

GPIO3_10: input: 0 [ ]

GPIO3_11: input: 0 [ ]

GPIO3_12: input: 0 [ ]

GPIO3_13: input: 0 [ ]

GPIO3_14: input: 0 [ ]

GPIO3_15: input: 0 [ ]

GPIO3_16: input: 0 [ ]

GPIO3_17: input: 0 [ ]

GPIO3_18: input: 0 [ ]

GPIO3_19: input: 0 [ ]

GPIO3_20: input: 0 [ ]

GPIO3_21: input: 0 [ ]

GPIO3_22: input: 0 [ ]

GPIO3_23: input: 0 [ ]

GPIO3_24: input: 0 [ ]

GPIO3_25: input: 0 [ ]

GPIO3_26: input: 0 [ ]

GPIO3_27: input: 0 [ ]

GPIO3_28: input: 0 [ ]

GPIO3_29: input: 0 [ ]

GPIO3_30: input: 0 [ ]

GPIO3_31: input: 0 [ ]

Bank GPIO4_:

GPIO4_0: input: 0 [ ]

GPIO4_1: input: 0 [ ]

GPIO4_2: input: 0 [ ]

GPIO4_3: input: 0 [ ]

GPIO4_4: input: 0 [ ]

GPIO4_5: input: 0 [ ]

GPIO4_6: input: 0 [ ]

GPIO4_7: input: 0 [ ]

GPIO4_8: input: 0 [ ]

GPIO4_9: input: 0 [ ]

GPIO4_10: input: 0 [ ]

GPIO4_11: input: 0 [ ]

GPIO4_12: input: 0 [ ]

GPIO4_13: input: 0 [ ]

GPIO4_14: input: 0 [ ]

GPIO4_15: input: 0 [ ]

GPIO4_16: input: 0 [ ]

GPIO4_17: input: 1 [ ]

GPIO4_18: input: 0 [ ]

GPIO4_19: input: 0 [ ]

GPIO4_20: input: 0 [ ]

GPIO4_21: input: 0 [ ]

GPIO4_22: input: 0 [ ]

GPIO4_23: input: 0 [ ]

GPIO4_24: input: 1 [ ]

GPIO4_25: input: 0 [ ]

GPIO4_26: input: 0 [ ]

GPIO4_27: input: 0 [ ]

GPIO4_28: input: 0 [ ]

GPIO4_29: input: 0 [ ]

GPIO4_30: input: 0 [ ]

GPIO4_31: input: 0 [ ]

Bank GPIO1_:

GPIO1_0: input: 1 [ ]

GPIO1_1: input: 1 [ ]

GPIO1_2: input: 0 [ ]

GPIO1_3: input: 0 [ ]

GPIO1_4: input: 0 [ ]

GPIO1_5: input: 0 [ ]

GPIO1_6: input: 0 [ ]

GPIO1_7: input: 1 [ ]

GPIO1_8: input: 0 [ ]

GPIO1_9: input: 0 [ ]

GPIO1_10: input: 0 [ ]

GPIO1_11: input: 0 [ ]

GPIO1_12: input: 0 [ ]

GPIO1_13: input: 0 [ ]

GPIO1_14: input: 0 [ ]

GPIO1_15: input: 0 [ ]

GPIO1_16: input: 0 [ ]

GPIO1_17: input: 0 [ ]

GPIO1_18: input: 0 [ ]

GPIO1_19: input: 0 [ ]

GPIO1_20: input: 0 [ ]

GPIO1_21: input: 0 [ ]

GPIO1_22: input: 0 [ ]

GPIO1_23: input: 0 [ ]

GPIO1_24: input: 0 [ ]

GPIO1_25: input: 0 [ ]

GPIO1_26: input: 0 [ ]

GPIO1_27: input: 0 [ ]

GPIO1_28: input: 0 [ ]

GPIO1_29: input: 0 [ ]

GPIO1_30: input: 0 [ ]

GPIO1_31: input: 0 [ ]

GPIO bank和pin name可以参照status的结果,然后运行gpio命令:

u-boot=> gpio status GPIO3_

Bank GPIO3_:

GPIO3_0: input: 1 [x] mmc@42860000.cd-gpios

u-boot=> gpio status GPIO3_0

Bank GPIO3_:

GPIO3_0: input: 1 [x] mmc@42860000.cd-gpios

u-boot=> gpio status GPIO3_1

Bank GPIO3_:

GPIO3_1: input: 0 [ ]

u-boot=> gpio set GPIO2_11

gpio: pin GPIO2_11 (gpio 11) value is 1

u-boot=> gpio clear GPIO2_11

gpio: pin GPIO2_11 (gpio 11) value is 0

参考:

gpio command — Das U-Boot unknown version documentation

将如下的markdown翻译为中文 # TMF8806 Linux Reference Driver README.md ## Description This file describes the installation of the tmf8806 linux reference driver, and how to use it with the Sysfs. ### Compilation The compilition was done with the ams-Osram provided image bookworm_1v0. Linux version 6.1.0-rpi7-rpi-v6 (debian-kernel@lists.debian.org) (gcc-12 (Raspbian 12.2.0-14+rpi1) 12.2.0, GNU ld (GNU Binutils for Raspbian) 2.40) #1 Raspbian 1:6.1.63-1+rpt1 (2023-11-24) ## Files Overlay Files The device tree overlay files for the tmf8806 prototyped on the Raspberry Pi zero. - tmf8806-overlay.dts pinheader configuration, EN gpio and INT gpio - tmf8806-overlay-fpc.dts evm configuration with flex printed circuit, EN gpio and INT gpio - tmf8806-overlay-polled.dts pinheader configuration, EN gpio and INT register polled - tmf8806-overlay-polled-fpc.dts evm configuration with flex printed circuit, EN gpio and INT register polled Module File - tmf8806.ko ## Installation Configuration of Raspberry Pi zero: 1. Use the right image bookworm_1v0 (provided by ams-Osram) 2. connect to Raspberry Pi ssh ams@169.254.0.2 Pw: ams 3. copy the right files on the Raspberry Pi - Module tmf8806.ko - tmf8806_install.sh for fpc(EVM Demo), tmf8806_install_pinheader.sh for pinheader - Overlay file tmf8806-overlay.dtbo for pinheader or tmf8806-overlay-fpc.dtbo for fpc (EVM Demo) 4. chmod 777 tmf8806_install_pinheader.sh 5. sudo su (Super User mode) 6. ./tmf8806_install_pinheader.sh for pinheader or ./tmf8806_install.sh for fpc 7. sync 8. reboot (reboot system) ## General commands on the Raspberry Pi 1. Connect to Raspberry Pi - ssh ams@169.254.0.2 - Pw: ams 2. See pinout on Raspberry Pi - pinout 3. Debug messages - list: dmesg - list and clear: sudo dmesg -c 4. I2C commands on command line - Detect i2c devices: i2cdetect -y 1 - I2C Read: i2cget -y 1 0x41 0xE0 - I2C Write: i2cset -y 1 0x41 0xE0 0x01 6. Change Raspberry I2C frequency - in file: /boot/config.txt - reboot 5. Module Functions - list all modules: lsmod - insert module: sudo insmod tmf8806.ko - remove module: sudo rmmod tmf8806.ko - module info : modinfo tmf8806.ko 6. See running tmf8806 thread for interrupt handling - ps -ef 7. Makefile command for TMF8806 - make CONFIG_SENSORS_TMF8806=m 8. The overlay file could be changed - copy overlay file to boot directory - if overlay files are generated - cp /home/ams/arch/arm/boot/dts/tmf8806-overlay-polled.dtbo /boot/overlays - cp /home/ams/arch/arm/boot/dts/tmf8806-overlay.dtbo /boot/overlays - cp /home/ams/arch/arm/boot/dts/tmf8806-overlay-polled-fpc.dtbo /boot/overlays - cp /home/ams/arch/arm/boot/dts/tmf8806-overlay-fpc.dtbo /boot/overlays - in /boot/config.txt add the desired overlay file: - dtoverlay=tmf8806-overlay-polled - dtoverlay=tmf8806-overlay - dtoverlay=tmf8806-overlay-polled-fpc - dtoverlay=tmf8806-overlay-fpc - echo "dtoverlay=tmf8806-overlay" >> /boot/config.txt ## System File System - I2C Bus 1 (pinheader): /sys/class/i2c-adapter/i2c-1/1-0041/ - I2C Bus 0 (fpc): /sys/class/i2c-adapter/i2c-0/0-0041/ A value with 0x prepended means the attribute interpretation is done in hexadecimal. ### Common Attributes (tmf8806_common) 1. chip_enable (R/W) - See chip enabled status: cat chip_enable - Enable the device: echo 1 > chip_enable - Disable the device: echo 0 > chip_enable 2. driver_debug (R/W) - Debugging statement for driver - 0x0 ... no logging - 0x1 ... only error logging - 0x8 ... this is a bit-mask check for clock correction logging - 0x10 ... some information - 0x20 ... very chatty firmware - 0x80 ... this is a bit-mask check for i2c logging - 0xFF ... dump everything - Show debug state (hex): cat driver_debug - Enable debugging: echo 0x1 > driver_debug - Disable debugging: echo 0x0 > driver_debug 3. program (R/W) - See running program (hex): cat program - Switch to Bootloader: echo 0x80 > program - Switch to App0: echo 0xC0 > program 4. program_version (R) - See program, major/minor/patch version, chip id and chip revision (hex): cat program_version 5. registers (R) - Dump Registers 0x00-0xFF (hex): cat registers 6. register_write (W) - Write to device register: echo "0xE0:0x41" > register_write - example stop measurement: echo "0x10:0xFF" > register_write - example irq enabling: echo "0xe2:0x01" > register_write 7. request_ram_patch(W) The firmware mainapp_PATCH_Maxwell.hex located in /lib/firmware will be downloaded - Download Firmware patch: echo 1 > request_ram_patch ### Application Attributes (tmf8806_app0) See datasheet for the measurement command parameters: - spreadSpecSpad - spreadSpecVcsel - data_setting - alg_setting - gpio_setting - capture_delay - snr Attributes: 1. alg_setting (R/W) - Show algorithm setting of app0_command (hex): cat alg_setting - Set algorithm setting of app0_command: echo 0x2 > alg_setting 2. app0_apply_fac_calib (W) - Perform a factory calibration: echo 1 > app0_apply_fac_calib 3. app0_clk_correction (W) - Disable Clock Correction: echo 0 > app0_clk_correction - Enable Clock Correction: echo 1 > app0_clk_correction 4. app0_command (R/W) - See the registers from 0x06 to 0x10. Show app0_command: cat app0_command - Write a commando last value is the commando at register 0x10. DO NOT USE!! I2C command only, no logic in driver is used. Set app0_command: echo 0x0 0x0 0x3 0x2 0x0 0x0 0x6 0x21 0x90 0x1 0x2 > app0_command 5. app0_ctrl_reg( R ) - Show registers 0x00 - 0x20 (hex): cat app0_ctrl_reg 6. app0_fac_calib (R/W) - Show factory calibration data (hex): cat app0_fac_calib - Set factory calibration data: echo 0x3 0x0 0x0 0xf3 0xaf 0x7f 0xc1 0x3 0xf6 0xea 0x27 0x18 0x0 0x4 > app0_fac_calib 7. app0_histogram_readout (W) - Modes - TMF8806_DUMP_HIST_ELECTRICAL_CAL 0x1 - TMF8806_DUMP_HIST_PROXIMITY 0x2 - TMF8806_DUMP_HIST_DISTANCE 0x4 - TMF8806_DUMP_HIST_ALG_PILEUP 0x8 - TMF8806_DUMP_HIST_ALG_PU_TDC_SUM 0x10 - Configuration of histogram readout: echo 0x1 > app0_histogram_readout 8. app0_state_data (W) - Set the state data: echo 0x2 0x3c 0x0 0x0 0x7f 0x7f 0x0 0x0 0x0 0x0 0x0 > app0_state_data 9. capture (R/W) - Show app0_command (hex): cat capture - Stop capture of app0_command: echo 0 > capture - Start capture of app0_command: echo 1 > capture 10. capture_delay (R/W) - Show capture delay of app0_command (dec): cat capture_delay - Show capture delay of app0_command (dec): echo 1 > capture_delay 11. data_setting (R/W) - Show data setting of app0_command (hex): cat data_setting - Set data setting of app0_command: echo 0x2 > data_setting 12. gpio_setting (R/W) - Show gpio setting of app0_command (hex): cat gpio_setting - Set gpio setting of app0_command: echo 0x1 > gpio_setting 13. iterations (R/W) - Show iterations of app0_command (dec): cat iterations - Set iterations of app0_command (dec): echo 500 > iterations 14. period (R/W) - Show period of app0_command (dec): cat period - Set period of app0_command (dec): echo 33 > period 15. snr (R/W) - Show snr of app0_command (hex): cat snr - Set snr of app0_command: echo 0x6 > snr 16. spreadSpecSpad (R/W) - Show spreadSpecSpad of app0_command (hex): cat spreadSpecSpad - Set spreadSpecSpad of app0_command: echo 0x0 > spreadSpecSpad 17. spreadSpecVcsel (R/W) - Show spreadSpecVcsel of app0_command (hex): cat spreadSpecVcsel - Set spreadSpecVcsel of app0_command: echo 0x0 > spreadSpecVcsel 18. distance_thresholds (R/W) - Show persistance, low and high threshold (dec): cat distance_thresholds - Set persistance, low and high threshold: echo 0 0 2710 > distance_thresholds 19. app0_osc_trim (R/W) - Show oscillator trim value (dec): cat app0_osc_trim - Set oscillator trim value (dec): echo 100 > app0_osc_trim ### Output data 1. app0_tof_output (R) Every data frame has 4 four byte header. Byte[0] = FrameID (Histogram-Type = PileUp, EC, Raw, Long, Short, Result=0x55) Byte[1] = frameNumber (free-running number, increments with each frame) Byte[2] = payload_size_LSB Byte[3] = payload_size_MSB Result Data from I2C readout starting from register 0x1C. For content see datasheet. If histogram readout is enabled, the raw histograms are also dumped. Frame ID: TMF8806_DIAG_HIST_ELECTRICAL_CAL 1 TMF8806_DIAG_HIST_PROXIMITY 4 TMF8806_DIAG_HIST_DISTANCE 7 TMF8806_DIAG_HIST_ALG_PILEUP 16 TMF8806_DIAG_HIST_ALG_PU_TDC_SUM 17 - Output data (binary) could be read: cat app0_tof_output Note: The output buffer is a fifo buffer with 32k.
11-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夜流冰

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值