ARM DIY(十)LRADC 按键

前言

ARM SOC 有别于单片机 MCU 的一点就是,ARM SOC 的 GPIO 比较少,基本上引脚都有专用的功能,因为它很少去接矩阵键盘、众多继电器、众多 LED。

但有时 ARM SOC 又需要三五个按键,这时候 LRADC 就是一个不错的选择,它使用一个引脚,就可以扩展几个到几十个按键。

原理

设计上述电路,

  • 当没有按键按下时,KEYADC0 引脚电压约等于 AVCC(3V)
  • S1 按下时,KEYADC0 引脚电压等于 6.8/(6.8+100)*3V=0.19V
  • S2 按下时,KEYADC0 引脚电压等于 (6.8+8.2)/(6.8+8.2+100)*3V=0.39V
  • S3 按下时,0.6V
  • S4 按下时,0.8V

这样,根据 KEYADC0 引脚电压值,就可知道哪个键被按下了。

继续介绍下 LRADC 内部原理

  • 当 ADC_IN 从 3.0V 降到 2.0V 以下,比较器 24 会发送第一个中断给 Control Logic;
  • 当 ADC_IN 从 2.0V 降到某一指定电压以下,比较器 25 会发送第二个中断给 Control Logic;
  • 如果 Control Logic 收到了第一个中断,在指定的时间内没有收到第二个中断,它会向 host 发送 HOLD_KEY_IRQ;
  • 如果 Control Logic 收到了第一个中断,并且在指定的时间内又收到第二个中断,它会向 host 发送 KEY_DOWN_IRQ;
  • 如果 Control Logic 只收到了第二个中断,没有收到第一个中断,它会向 host 发送 ALREADY_HOLD_IRQ。

硬件

焊接按键和分压电阻

设备树

arch/arm/boot/dts/sun8i-v3s.dtsi

c 复制代码
	soc {
		lradc: lradc@1c22800 {
			compatible = "allwinner,sun4i-a10-lradc-keys";
			reg = <0x01c22800 0x400>;
			interrupts = <GIC_SPI 30 IRQ_TYPE_LEVEL_HIGH>;
			status = "disabled";
		};
	};

arch/arm/boot/dts/sun8i-v3s-licheepi-zero-dock.dts

c 复制代码
&lradc {
	vref-supply = <&reg_vcc3v0>;
	status = "okay";

	button-200 {
		label = "Volume Up";
		linux,code = <KEY_VOLUMEUP>;
		channel = <0>;
		voltage = <200000>;
	};

	button-400 {
		label = "Volume Down";
		linux,code = <KEY_VOLUMEDOWN>;
		channel = <0>;
		voltage = <400000>;
	};

	button-600 {
		label = "Select";
		linux,code = <KEY_SELECT>;
		channel = <0>;
		voltage = <600000>;
	};

	button-800 {
		label = "Start";
		linux,code = <KEY_OK>;
		channel = <0>;
		voltage = <800000>;
	};
};

内核编译选项

测试

LRADC 中断已注册成功

bash 复制代码
root@v3s-diy:~# cat /proc/interrupts 
           CPU0       
 17:          0     GIC-0  29 Level     arch_timer
 18:     267730     GIC-0  30 Level     arch_timer
 21:          0     GIC-0  50 Level     timer@1c20c00
 22:      24551     GIC-0  92 Level     sunxi-mmc
 23:     832950     GIC-0  93 Level     sunxi-mmc
 24:          0     GIC-0 103 Level     musb-hdrc.1.auto
 25:          0     GIC-0  72 Level     1c20400.rtc
 31:          0     GIC-0  62 Level     sun4i-a10-lradc-keys // LRADC 中断
 32:       2037     GIC-0  32 Level     ttyS0
 34:        224     GIC-0  38 Level     mv64xxx_i2c
 35:         10     GIC-0  39 Level     mv64xxx_i2c
 36:    3589553     GIC-0  97 Level     sun6i-spi
 37:          0     GIC-0  82 Level     1c02000.dma-controller
 39:          0     GIC-0 116 Level     sun6i-csi
 40:          0     GIC-0 104 Level     ehci_hcd:usb1
 41:          2     GIC-0 105 Level     ohci_hcd:usb2
IPI0:          0  CPU wakeup interrupts
IPI1:          0  Timer broadcast interrupts
IPI2:          0  Rescheduling interrupts
IPI3:          0  Function call interrupts
IPI4:          0  CPU stop interrupts
IPI5:          0  IRQ work interrupts
IPI6:          0  completion interrupts
Err:          0

/dev/input 目录下已产生对应设备

bash 复制代码
# ls /dev/input/
event0

检测按键

bash 复制代码
# hexdump /dev/input/event0
0000000 005c 0000 a87a 000c 0001 0160 0001 0000
0000010 005c 0000 a87a 000c 0000 0000 0000 0000
0000020 005d 0000 8dfc 0006 0001 0160 0000 0000
0000030 005d 0000 8dfc 0006 0000 0000 0000 0000
0000040 005e 0000 fa30 000e 0001 0160 0001 0000
0000050 005e 0000 fa30 000e 0000 0000 0000 0000
0000060 005f 0000 a730 0007 0001 0160 0000 0000
0000070 005f 0000 a730 0007 0000 0000 0000 0000
0000080 0060 0000 664f 000a 0001 0160 0001 0000
0000090 0060 0000 664f 000a 0000 0000 0000 0000
00000a0 0061 0000 dad1 0001 0001 0160 0000 0000
00000b0 0061 0000 dad1 0001 0000 0000 0000 0000
# 
# cat /proc/interrupts | grep lradc
 31:         6     GIC-0  62 Level     sun4i-a10-lradc-keys

可以检测到按键事件,并且中断数量也相应增加。

至此 LRADC KEY 调试 OK

相关推荐
暮云星影2 小时前
十二、buildroot系统 adb登录权限设置
linux·arm开发·adb
钡铼技术物联网关9 小时前
ARM分拣机vs传统PLC:实测数据揭示的4倍效率差
大数据·linux·arm开发·人工智能·边缘计算
阿超爱嵌入式15 小时前
STM32学习之ARM内核自带的中断
arm开发·stm32·学习
程序员JerrySUN1 天前
深入解析ARM与RISC-V架构的Bring-up核心流程
arm开发·架构·risc-v
ZzxtEcho2 天前
ARM 汇编启动代码详解:从中断向量表到中断处理
汇编·arm开发
钡铼技术物联网关2 天前
工业4.0时代:RK3588边缘工业计算机助力AGV导航升级
linux·arm开发·人工智能·5g·边缘计算
逆鱼_042 天前
ARM-UART
linux·运维·arm开发
钡铼技术物联网关3 天前
ARM架构+CODESYS:解锁嵌入式边缘计算的实时控制新范式
大数据·linux·arm开发·人工智能·边缘计算
感觉画质不如…原神3 天前
使用Qemu模拟32位ARM系统
arm开发·qemu
矿渣渣4 天前
ARM 协处理器(Coprocessor)功能及用法详解
arm开发