NRF51822之GPIOTE介绍

news/2024/7/7 14:32:42 标签: 区块链
为了最大程度的呈现原文的内容。
GPIOTE handling library
Note
This library is obsolete and should not be used in new designs. Instead, you should use GPIOTE driver.

The general purpose Input/Output is organized in the nRF51 Series chips as one port with up to 32 I/Os (dependent on package) enabling access and control of up to 32 pins through one port. In most applications, the state of a GPIO or a change in state of a GPIO may trigger an application to take action or transition to another state.

The GPIO Tasks and Events (GPIOTE) provides functionality for accessing GPIO pins using tasks and events. The GPIO Task & Events (GPIOTE) library referred to as the the 'app_gpiote' in the SDK, facilitates application(s) to register for notification of change in state of one or more pins.

Since an application can consist of multiple independent modules, GPIOTE library allows several components to share the GPIOTE interrupt,each user defining a set of pins able to generate events to the user. When a GPIOTE interrupt occurs, the GPIOTE interrupt handler will identify pin transition(S) and notify each client registered for the transition.

Note
Components that register with the library are henceforth referred to as clients or users.
This library uses GPIOTE driver, which must be enabled and properly configured in nrf_drv_config.h. You must specify GPIOTE_CONFIG_NUM_OF_LOW_POWER_EVENTS, which is the number of pins used for low power EVENTS_PORT including the pins used by this library.
app_gpiote_01.png
Figure 1: Users are being notified of a pin transition event.

The GPIOTE users are responsible for configuring all their corresponding pins, except the SENSE field, which should be initialized to GPIO_PIN_CNF_SENSE_Disabled.

The module specifies on which pins events should be generated if the pin(s) goes from low->high or high->low or both directions.

Note
Even if the application is using the Scheduler, the GPIOTE event handlers will be called directly from the GPIOTE interrupt handler.
尽管应用使用了Scheduler,但是GPTOTE的事件句柄还是直接来自GPTOTE中断句柄。

Initializing GPIOTE module

The initialization procedure must be performed before using any of the other APIs of the module. It is recommended to use initialization macro APP_GPIOTE_INIT instead of the routine app_gpiote_init as the former takes care of reserving needed memory for the each user requested in the MAX_USERS parameter of the macro. This parameter indicates how may users are going to be registering with the library.

// Macro to initialize GPIOTE module and reserving necessary memory for each of user.
APP_GPIOTE_INIT(MAX_USERS);

 

Note
Module initialization should be performed only once. Specifically when using the macro to avoid reserving memory for each module several times.

Registering with GPIOTE

Each user must register itself with the module to be notified of change in state of GPIO. During registry, the user must provide callback handler to notify of a transition event and pin transitions its is interested in. 32-bit bitmask is used to represent 32 GPIO pins as shown in Figure 2 below. User can register for transition from high to low and/or low to high.

app_gpiote_02.png
Figure 2: GPIO Pin representation using 32-bit bitmask.

On successful registration, the user is assigned a user id and the user is expected to remember this identifier for all subsequent requests made to the module. This identifier is provided in the out parameter p_user_id. A sample registration is shown below.

// GPIOTE user identifier for the example module.
static app_gpiote_user_id_t m_example_user_id;
// GPIOTE event handler.
static void example_gpiote_event_handler(uint32_t event_pins_low_to_high, uint32_t event_pins_high_to_low);
.
.
.
uint32_t low_to_high_bitmask = 0x0000000F; // Bitmask to be notified of transition from low to high for GPIO 0-3
uint32_t high_to_low_bitmask = 0x0000000E; // Bitmask to be notified of transition from high to low for GPIO 0-2
uint32_t retval;
retval = app_gpiote_user_register(&m_example_user_id,
low_to_high_bitmask,
high_to_low_bitmask,
example_gpiote_event_handler);
if (retval != NRF_SUCCESS)
{
// Failed to register with user with GPIO module!
}
Note
It is possible for more than one user to register for same set/subset of GPIO pins; each of the concerned users will be notified of pin transition events.

By default, the GPIOTE is disabled on initialization. Therefore the GPIOTE has to be enabled by one of the users to start receiving GPIOTE state events. app_gpiote_user_enable is used to enable GPIOTE.

The following is a sample of registered user callback handling pin transition events.

// GPIOTE event handler.
void example_gpiote_event_handler (uint32_t event_pins_low_to_high, uint32_t event_pins_high_to_low)
{
    .
    .
    .
    if (event_pins_low_to_high & 0x00000001)
    {
         // GPIO pin 0 transitioned from low to high.
         // Take necessary action.
    }
    if (event_pins_high_to_low & 0x00000004)
    {
         // GPIO pin 2 transitioned from high to low.
         // Take necessary action.
    }
    .
    .
    .
}

 

Enable/Disable GPIOTE

The GPIOTE module can be enabled or disabled by a registered user at any point of time. No state transition events are received when the GPIOTE is disabled. On initialization, by default, the GPIOTE is disabled.

The following code snippet disables and enables the GPIOTE.

uint32_t retval;
// Enable notifications for example user module which is already registered.
retval = app_gpiote_user_disable(m_example_user_id);
if (retval != NRF_SUCCESS)
{
// Enabling notifications failed. Take corrective/needed action.
.
.
}
.
.
.

 


// Enable notifications for example user module which is already registered.
retval = app_gpiote_user_enable(m_example_user_id);
if (retval != NRF_SUCCESS)
{
// Enabling notifications failed. Take corrective/needed action.
.
.
}

 

Reading GPIOTE State

A registered user can read the current state of GPIOs by reading the state information. The following code snippets demonstrates a module reading the state information.

uint32_t retval;
uint32_t gpio_pin_state;
retval = app_gpiote_pins_state_get(m_example_user_id,&gpio_pin_state);
if (retval != NRF_SUCCESS)
{
// Failed to read state information. Take corrective action.
}
else
{
.
.
if (gpio_pins_state & 0x00000006) // Checks if pin one and two are set
{
// Take necessary action
}
.
.
}

 


转载于:https://www.cnblogs.com/libra13179/p/5336580.html


http://www.niftyadmin.cn/n/1829217.html

相关文章

zabbix安装及过程中遇到的问题总结

zabbix需要lnmp或lamp环境,在这里不介绍了,其他文章有介绍lnmp的搭建,直接进入zabbix服务端和agent端的安装。一、下载及编译安装。cd /usr/local/src/wgethttp://sourceforge.net/projects/zabbix/files/ZABBIX%20Latest%20Stable/2.2.9/zab…

JAVA高级应用之序列化与反序列化与IO流总结

缓冲流 序列化 反序列化 缓冲流 字节缓冲流 缓冲流(高效率的流) BufferedOutputStream 缓冲输出字节流 构造方法: BufferedOutPutStream(OutputStream out) 参数:字节输出流的父类 FileOutputStream 你想对哪个流高效 就把该流装进去BufferedInputStream 缓冲输入字节流 代…

文件上传绕过方法

绕过 前端验证绕过 对于前端验证的校验,可以直接修改前端 js ,添加允许PHP扩展名。 或者上传.gif 文件然后用burp抓包,修改文件修改成PHP。 mime 限制扩展名绕过 修改concent-type 服务端黑名单校验后缀名的绕过 黑名单的意思就是服务端不允许哪些…

Windows、Linux 应急响应

Windows 系统账号安全 1、查看服务器是否有弱口令,远程管理端口是否对公网开放。 检查方法:据实际情况咨询相关服务器管理员。 2、查看服务器是否存在可疑账号、新增账号。 检查方法:打开 cmd 窗口,输入 lusrmgr.msc 查看是否有…

Windows、Linux、web 日志分析

Windows 日志分析是应急响应中的重要环节。很多问题通过日志分析就可以直接找到解决方法。 日志分析对象主要是系统日志,web中间件日志,数据库日志,以及其他安全设备收集到的日志。 日志分析 Windows系统日志是记录系统中硬件、软件和系统…

JAVA高级应用之装饰者模式 LineNumberReader 打印流

装饰者模式 思想:可以通过装饰 对原来的功能进行升级BufferedOutputStream 需要OutputStream参数进行构造 可以是FileOutputStream 相当于装饰后 功能得到提升 写入效率提高ObjectOutputStream 需要OutputStream参数进行构造 可以是FileOutputStream 装饰后 功能提升了,可以写…

游戏引擎网络开发者的 64 做与不做(一):客户端方面

2019独角兽企业重金招聘Python工程师标准>>> 【编者按】时下,游戏网络化已势不可逆,因此,对于游戏开发者来说,掌握网络引擎的打造技巧同样不可避免。近日,Research Industrial Systems Engineering GmbH 安…

PHP -- SQL 注入讲解

概念 SQL 注入漏洞,本质是语句的混淆,对用户输入的语句过滤不严,导致语句拼接成新的语句,达到注入的目的。 原理 参数用户可控:前端传给后端的参数内容是用户可控的。 参数带入数据库查询:传入的参数拼接…