Debounce

概述

beetle 芯片中有 8 个 deb 通道

按 GPIO 区别分类:

  • TK 和 AON GPIO 仅可以使用 4-7 通道

  • 其他 GPIO 8 个通道均可使用

按功能区别分类:

  • debounce 复位仅可以使用 6-7 通道

  • 支持 1 个通道 pattern 功能,数据源可选择 4-7 通道

  • 其他功能 8 个通道均可使用

功能特性

  • 支持 4 路独立的 gpi 的 debounce 的控制,可产生 4 个中断,4 个复位信号

  • 支持对 gpi 的输入输出的极性进行配置

  • 支持按键的中断检测,选中的 gpi 作为按键,检测其高电平(或低电平)持续时间并产生相应中断

  • 按键中断检测包括两种中断类型:在按键过程中的中断,以及按键弹起的中断,软件根据需要配置相应的中断类型

  • 支持按键复位,选中的 gpi 在高电平持续时间超过某个阈值时,对于芯片产生复位信号

  • 同一个 channel 既可以支持按键中断,也可以支持按键复位,它们有不通的阈值

  • 支持异步按键 pattern 检测,检测到的 pattern 可以用来产生唤醒中断,也可以用于产生芯片的复位,对应的 pattern 值可配置

工作模式

Debounce 支持按键中断功能,按键复位功能,Pattern 中断和复位功能

按键中断功能

Key debounce IO 模块设置按键对应的 GPIO,以及 debounce 模块的中断方向和防抖的时间 Driver 会根据设定,触发相应的中断(按下或释放)

实现此功能步骤如下:

  1. 判断防抖时长不可为 0,否则直接返回 WQ_RET_INVAL

  2. 根据指定的 GPIO 申请一个 channel

  3. 配置指定的按键 GPIO,指定的防抖时长

  4. 关闭 debounce 复位使能

  5. 使能指定的中断类型

  6. 设置 debounce 为双沿采,配置 debounce 模式,计数时间 counter 清零

  7. 使能某 channel 的 debounce

按键中断共有 2 种中断: 1. 按键过程中,持续时间等于指定的按键时间阈值,则立刻会产生 WQ_DEBOUNCE_INT_PRESS_MID 中断 2. 在按键释放的过程中,如果持续时间超过了(大于等于)指定的按键时间阈值,同样会产生 WQ_DEBOUNCE_INT_PRESS 中断

按键复位功能

Key debounce IO 模块设置按键对应的 GPIO 和防抖的时间 Driver 会根据设定,触发复位(按下达到指定时长)

实现此功能步骤如下:

  1. 判断防抖时长不可为 0,否则直接返回 WQ_RET_INVAL

  2. 根据指定的 GPIO 申请一个 channel

  3. 配置指定的按键 GPIO,指定的防抖时长

  4. 设置 debounce 为双沿采,配置 debounce 模式,计数时间 counter 清零

  5. 使能 debounce 复位

  6. 使能某 channel 的 debounce

注意事项:按键复位检测和按键中断检测,复用的是同一个 24bit 的 counter

Pattern 中断和复位功能

Key debounce IO 模块设置按键对应的 GPIO,指定的波特率,指定的 header,reset,interrupt 控制字 Driver 会根据设定,触发相应的复位或中断(对应 GPIO 收到指定控制字)

实现此功能步骤如下:

  1. 判断指定波特率不可为 0,指定的 GPIO 必须是 PMM GPIO,否则直接返回 WQ_RET_INVAL

  2. 判断没有设置过 pattern 功能,否则直接返回 WQ_RET_EXIST

  3. 根据指定的 GPIO 申请一个 channel

  4. 清除 interrupt,reset 的标志位

  5. 配置指定的按键 GPIO,波特率

  6. 配置指定的 header,reset,intterupt 控制字

  7. 根据指定的 header 控制字配置 debounce 模式

  8. 使能 debounce pattern

  9. 使能某 channel 的 debounce

注意事项:header 默认是"10101010"这 8bit 序列,可以配置为其他值,但一般要求有比较均匀的 0/1

资源依赖

  • GPIO

用法流程

参考示例

examples/debounce_demo

API 介绍

Typedefs

typedef void (*wq_debounce_callback)(uint16_t gpio, WQ_DEBOUNCE_INT int_type)

Enums

enum WQ_DEBOUNCE_INT

DEBOUNCE INT.TYPE button press release |____________________..._______| ______| | |________________ |-20ms-| | | | time 0 20ms x y z int type POS PRESS_MID IO=1 PRESS IO=0.

DEBOUNCE INT.

Values:

enumerator WQ_DEBOUNCE_INT_PRESS
enumerator WQ_DEBOUNCE_INT_PRESS_MID
enumerator WQ_DEBOUNCE_INT_PRESS_POS
enumerator WQ_DEBOUNCE_INT_PATTERN
enumerator WQ_DEBOUNCE_INT_MAX
enum WQ_DEBOUNCE_MODE

DEBOUNCE MODE.

Values:

enumerator WQ_DEBOUNCE_EDGE_RAISING
enumerator WQ_DEBOUNCE_EDGE_FALLING
enumerator WQ_DEBOUNCE_MODE_MAX
enum WQ_DEBOUNCE_RESET_CAUSE

DEBOUNCE MODE.

Values:

enumerator WQ_DEBOUNCE_RESET_CAUSE_DIG_PRESS
enumerator WQ_DEBOUNCE_RESET_CAUSE_PMM_PRESS
enumerator WQ_DEBOUNCE_RESET_CAUSE_PMM_PATTERN
enumerator WQ_DEBOUNCE_RESET_CAUSE_MAX

Functions

void wq_debounce_init(void)

This function is to init gpio debounce.

void wq_debounce_deinit(void)

This function is to deinit gpio debounce.

WQ_RET wq_debounce_gpio_open(const wq_debounce_gpio_cfg_t *cfg)

This function is to set debounce int or reset for gpio.

参数:

cfg -- is the gpio config

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_gpio_reset_open(const wq_debounce_reset_cfg_t *cfg)

This function is to set debounce reset for gpio.

参数:

cfg -- is the reset config

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_ocl_open(const wq_debounce_ocl_cfg_t *cfg)

This function is to set debounce int or reset for ocl.

参数:

cfg -- is the ocl debounce config

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_ocl_close(void)

This function is to close debounce for ocl.

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_gpio_close(WQ_GPIO_ID gpio)

This function is to close gpio`s debounce int and reset function.

参数:

gpio -- is the gpio to close.

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_set_mode(WQ_GPIO_ID gpio, WQ_DEBOUNCE_MODE mode)

This function is to change gpio mode.

参数:
  • gpio -- is the gpio num.

  • mode -- is the gpio mode.

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_set_int_time(WQ_GPIO_ID gpio, uint32_t time)

This function is to change int time.

参数:
  • gpio -- is the gpio num.

  • time -- is the time, unit of int time is ms, unit of reset time is s.

返回:

WQ_RET WQ_RET_OK for success, others failed.

WQ_RET wq_debounce_set_reset_time(WQ_GPIO_ID gpio, uint32_t time)

This function is to change reset time.

参数:
  • gpio -- is the gpio num.

  • time -- is the time, unit of int time is ms, unit of reset time is s.

返回:

WQ_RET WQ_RET_OK for success, others failed.

void wq_debounce_clear_counter(WQ_GPIO_ID gpio)

This function clear the debounce counter to avoid unexpected reset or int.

参数:

gpio -- is the gpio num.

WQ_RET wq_debounce_pattern_dect(const wq_debounce_pattern_cfg_t *cfg)

This function is to set debounce pattern for gpio.

参数:

cfg -- is the pattern config

返回:

WQ_RET WQ_RET_OK for success, others failed.

uint8_t wq_debounce_get_reset_flag(void)

This function is to get the flag of debounce reset.

返回:

uint8_t is the value of flag.

void wq_debounce_clear_reset_flag(void)

This function is to clear the flag of debounce reset.

struct wq_debounce_gpio_cfg_t
#include <wq_debounce.h>

debounce gpio interrupt config

Public Members

wq_debounce_callback cb

interrupt callback

uint32_t time_ms

debounce time microsecond

WQ_GPIO_ID gpio

GPIO ID

WQ_DEBOUNCE_MODE mode

GPIO EDGE mode

bool int_press

int type PRESS

bool int_press_mid

int type PRESS MID

bool int_press_pos

int type PRESS POS

struct wq_debounce_reset_cfg_t
#include <wq_debounce.h>

debounce hardware reset config

Public Members

uint32_t time_ms

reset time microsecond

WQ_GPIO_ID gpio

reset trigger GPIO

WQ_DEBOUNCE_MODE mode

GPIO EDGE mode

struct wq_debounce_ocl_cfg_t
#include <wq_debounce.h>

debounce ocl config

Public Members

uint32_t int_time_ms

interrupt time microsecond

uint32_t rst_time_ms

reset time microsecond

wq_debounce_callback cb

interrupt callback

struct wq_debounce_pattern_cfg_t
#include <wq_debounce.h>

debounce pattern config

Public Members

wq_debounce_callback cb

interrupt callback

WQ_GPIO_ID gpio

GPIO ID

uint32_t baud_rate

baud_rate

uint8_t header

pattern header

uint8_t rst_code

reset code

uint8_t int_code

interrupt code

uint8_t is_reset

pattern reset

uint8_t is_interrupt

pattern interrupt

uint8_t reserved

unused