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

相关推荐
zhouwy1132 小时前
ARM汇编指令集详解
汇编·arm开发
山后太阳17 小时前
Keil5(MDK-ARM)完整下载安装教程+入门教程:从零搭建STM32开发环境
arm开发·stm32·嵌入式硬件
zz_lzh17 小时前
arm版AI牛马:armbian(rk3588)设备部署openclaw
arm开发·人工智能·arm
lanxiao88882 天前
F1C100S 内核
arm开发
杰杰桀桀桀2 天前
基于stm32ARM库函数的IIR二阶巴特沃斯低通滤波器--附完整代码
arm开发·stm32·嵌入式硬件·数字滤波器·巴特沃斯低通滤波
TBrL7UtdTELTTdut4BAL2 天前
ARM Cortex-A53 (无AES)平台加密网络转发性能测试与对比分析
arm开发·集成测试
AI服务老曹2 天前
架构实战:如何基于 GB28181 与异构计算构建跨平台(X86/ARM)AI 视频管理系统?源码交付深度解析
arm开发·人工智能·架构
CinzWS3 天前
A53 FPGA原型验证:从RTL到可运行系统的挑战
arm开发·嵌入式·芯片验证·原型验证·a53
AI服务老曹3 天前
深度解析:支持 GB28181/RTSP 及异构计算(X86/ARM+GPU/NPU)的 AI 视频管理平台架构方案(附源码交付与 Docker 部署)
arm开发·人工智能·音视频
2302_813806223 天前
基础环境篇 – 交叉编译环境搭建与NFS服务配置
arm开发