1. Home
  2. 博客

    nRF54L15 GPIO 特性

nRF54L15 GPIO 特性

Smile Jiang, Nordic Semiconductor

APB总线用于低速且低功耗的外围设备,nRF54L15根据APB总线的标号,对外设进行命名且对GPIO口进行端口分配。AMBIX为AMBA总线互联通道。

nRF54L15 的GPIO口分配

​ APB总线用于低速且低功耗的外围设备,nRF54L15根据APB总线的标号,对外设进行命名且对GPIO口进行端口分配。AMBIX为AMBA总线互联通道。

nrf54l15 gpio

AMBIX0/APB00:

GPIO_P2:P2.00~P2.10

APB/AHB外设如:CCM00,UARTE00,SPIM00,SPIS00...

AMBIX1/APB10:

GPIO:None

外设如:RADIO,TIMER10,PPIB10...

AMBIX2/APB20:

GPIO_P1: P1.00~P1.14(包括P1.00/P1.01 XL1 XL2)

外设如:GPIOTE20,UARTE20~22,TWIM20~22,GRTC,SAADC,PWM20~22...

在nRF54L15DK原理图上 : UART1 即APB20上的 UART20

AMBIX3/APB30:

GPIO_P0: P0.00~P0.04

外设如:GPIOTE30,UARTE30,RESET,WDT30~31...

有些外设对GPIO的引脚选择有要求,如:

Peripheral Description
UARTE20/21 Can use any pin on P1. Can connect across power domains to dedicated pins on P2.
SPIMOO Has dedicated pins on P2. For 32 MHz operation, the pins must be configured usingextra high drive EO/E1 configuration in the DRIVE0/1 fields of the PIN_CNF GPIOregister.
SPIM20/21 Can use any pins on P1; see notes on clock pins. Can be connect across powerdomains to dedicated pins on P2.
SPIS20/21 Can use any pins on P1; see notes on clock pins. Can connect across power domainsto dedicated pins on P2.
TRACE Has dedicated pins that must be configured using extra high drive EO/E1configuration in the DRIVE0/1 fields of the PIN_CNF GPIO register.
GRTC Has dedicated pins for clock and PWM output.
TAMPC Has dedicated pins for active shield input and output.
FLPR Uses dedicated pins on P2 for emulated peripherals such as QSPI.
RADIO Uses dedicated pins on P1 for antenna switch control (DFEGPIO for directionfinding).
NFC Uses dedicated pins as listed in the pin assignments table for the selected device.These pins are configured as NFC antenna pins from reset. To use the pins forDigital 1/0, NFC function must be disabled in the NFCT - Near field communicationtag on page 348 peripheral.

外设管脚的默认配置: ncs/zephyr/boards/nordic/nrf54l15dk/nrf54l15dk_nrf54l15-pinctrl.dtsi

用户可以通过在app工程中添加XXX.overlay文件对外设管脚进行重新分配。比如:

nrf54l15 gpio nrf54l15 gpio

编译完成后可以通过查看生成的zephyr.dts确认.overlay的文件是否生效,并检查GPIO的管脚配置是否正确。比如button工程编译后最终zephyr.dts所在目录:ncs/zephyr/samples/basic/button/build/button/zephyr/zephyr.dts

2. 如何从devicetree中获取GPIO并将其映射到软件中?

​ 可参考文章:

https://www.cnblogs.com/jayant97/articles/18141263

https://github.com/aiminhua/ncs_samples/blob/master/ble_comprehensive/src/io_int_thread.c

​ 我们从两个简单的参考例子开始:

​ ncs/zephyr/samples/basic/blinky/src/main.c

​ ncs/zephyr/samples/basic/button/src/main.c

2.1 devicetree 中的GPIO描述

gpio0: gpio@10a000 {  
  compatible = "nordic,nrf-gpio";
  gpio-controller;
  reg = < 0x10a000 0x300 >;
  #gpio-cells = < 0x2 >;
  ngpios = < 0x5 >;
  status = "okay";
  port = < 0x0 >;
  gpiote-instance = < &gpiote30 >;
};
gpio1: gpio@d8200 {
  compatible = "nordic,nrf-gpio";
  gpio-controller;
  reg = < 0xd8200 0x300 >;
  #gpio-cells = < 0x2 >;
  ngpios = < 0x10 >;
  status = "okay";
  port = < 0x1 >;
  gpiote-instance = < &gpiote20 >;
  phandle = < 0xd >;
};
gpio2: gpio@50400 {
  ...
};

参考文件:

​ ncs/zephyr/dts/bindings/gpio/nordic,nrf-gpio.yaml

​ ncs/zephyr/include/zephyr/dt-bindings/gpio/gpio.h

​ ncs/zephyr/include/zephyr/drivers/gpio.h

a.节点名:gpio@10a000;gpio@d8200;gpio@d8200
b.标签:gpio0;gpio1;gpio2
c.gpio-controller: 声明具有域控制器功能
d.reg: 外设在总线上的地址分布,需跟节点名对应
e.phandle:一个节点node1可以通过phandle被另一节点node2引用作为属性值
f.gpiote-instance:指向外部gpiote节点的phandle属性,表明可使用该GPIO port的gpiote实体
g.ngpios:所在GPIO Port(槽位)中最大可用GPIO口的数目。如对于nrf54L15:
    gpio0(port0):   ngpios = < 0xb >;   => P0.00~P0.10
    gpio1(port1):   ngpios = < 0x10 >;  =>P1.00~P1.15
    gpio2(port2):   ngpios = < 0x5 >;   =>P2.00~P2.04
h.gpio-reserved-ranges:为系统保留的gpio口,用户不可以直接使用.比如:
    "gpio-reserved-ranges = [5,3]": 表明GPIO pin脚偏移为5,6,7的IO口为系统保留
    "gpio-reserved-ranges = <0 2>,<10 1>": 表明GPIO pin脚偏移为0, 1,10的IO口为系统保留, 即使定义了ngpios = <0xb>
i.#gpio-cells:gpio节点作为controller时的specifier参数数量。比如#gpio-cells = < 0x2 >; 意味着在其他节点中引用gpio0控制器时,specifier参数数量为2,如:
    led0: led_0 {
        gpios = < &gpio2 0x9 0x0 >; 
        label = "Green LED 0";
    };
     &gpio2:指gpio2控制器, 对应物理上的gpio port2
     0x9: 指pin脚编号是0x9,即P2.09
     0x0: devicetree中定义的GPIO口配置参数,在软件中有时被描述为flag(s)0x0等价于GPIO_ACTIVE_HIGH |GPIO_PUSH_PULL |GPIO_LINE_OPEN_SOURCE

补充说明:

GPIO_ACTIVE_HIGH: GPIO口高有效(有效指逻辑1)

GPIO_PUSH_PULL: 配置GPIO输出为推挽模式

PIO_LINE_OPEN_SOURCE:单端开源模式(线或)

2.2 代码中如何让获取&配置GPIO?

gpio及其配置更多时候是作为某个节点的属性出现,如button节点的GPIO属性,led节点的gpio属性;spi的cs-gpio属性等等

spi@abcd0001 {
    cs-gpios = <&gpio0 1 GPIO_ACTIVE_LOW>;
    spidev: spi-device@0 { ... };
};
buttons {
    compatible = "gpio-keys";
    button0: button_0 {
    gpios = < &gpio1 0xd 0x11 >;
    label = "Push button 0";
    zephyr,code = < 0xb >;
};

button1: button_1 { ...};
nrf52840_reset: gpio-reset {
    compatible = "nordic,nrf9160dk-nrf52840-reset";
    status = "disabled";
    gpios = < &interface_to_nrf52840 0x9 0x1 >;
  };
aliases {
    led00 = &led0;
    pwm-led0 = &pwm_led1;
    sw0 = &button0;
};

spi@abcd0001, buttons, button_0, button_1, gpio-reset为devicetree中的节点名(node name)

button0,button1,nrf52840_reset,为节点的标签(LABEL)

led00, pwm-led0,sw0为节点的别名(ALIAS)

对于GPIO的应用,也可以系统预留给用户自定义的节点(zephyr,user)中将其定义为属性:

/{
    zephyr,user{
        my-gpios = <&gpio0 12    (GPIO_ACTIVE_HIGH|GPIO_PUSH_PULL|GPIO_PULL_DOWN)>;
    };
};

2.2.1.获取节点描述符node specifier,即node_id

​ 参考文件:ncs/zephyr/doc/build/dts/api-usage.rst

​ 通过标签获取:DT_NODELABEL()

​ 通过别名获取: DT_ALIAS(sw0);DT_ALIAS(led00) //sw0,led00分别为button0,led0的别名

​ 通过绝对路径获取:DT_PATH(zephyr_user);DT_PATH(leds);DT_PATH(soc,gpio_50400)

​ 通过实体编号获取:DT_INST()

​ 通过被选取的节点chosen node获取:DT_CHOSEN()

​ 通过父节点/子节点获取:DT_PARENT(); DT_CHILD() ;

​ 通过node2及其phandle属性获取node1的节点描述符: node1=DT_PHANDLE(node2, prop)

\- DT_PATH(zephyr_user, i2c_40002000)
\- DT_NODELABEL(i2c1)
\- DT_ALIAS(sensor_controller)
\- DT_INST(x, vnd_soc_i2c)

2.2.2通过节点将gpio配置(属性)读取至特定结构体中

gpio_dt_sepc my_gpio=GPIO_DT_SPEC_GET(DT_PATH(zephyr_user), my_gpios)
gpio_dt_spec myled = GPIO_DT_SPEC_GET(DT_ALIAS(led0), gpios); //节点id,属性名
gpio_dt_spec mybutton = GPIO_DT_SPEC_GET_OR(DT_ALIAS(sw0), gpios,{0}); // 节点id,属性名,默认值

以上是gpio-button作为特殊设备的直接获取方式,对于一般的外设如uart/spi等,可以用DEVICE_DT_GET()获取设备。

2.2.3.重新配置gpio参数

GPIO的默认配置来源于devicetree,我们可以用以下方式覆盖默认配置:

​ 参考文档:2.8.0/zephyr/include/zephyr/drivers/gpio.h

a.配置输入输出:

gpio_pin_configure_dt(&mybutton, GPIO_INPUT);
gpio_pin_configure_dt(&myled, GPIO_OUTPUT);

b.设置/获取IO口状态

gpio_pin_toggle_dt(&myled);
gpio_pin_get_dt(&mybutton);
gpio_pin_set_dt(&myled, 1);

c.配置中断

gpio_pin_interrupt_configure_dt()
gpio_init_callback(&button_cb_data, button_pressed, BIT(button.pin));
gpio_add_callback_dt() => gpio_add_callback(spec->port, callback);
gpio_add_callback(button.port, &button_cb_data);

d.其他配置

gpio_remove_callback_dt()
gpio_get_pending_int()

3. GPIO & GPIOTE Event

gpio作为通用输入输出端口,用来对IO口进行读写和清空等操作,能通过配置+检测管脚的电平产生中断,也可以被其他外设配置和使用。GPIO模块的重要功能:

​ sense:系统进入sleep模式后(也称system OFF模式),只能通过IO口等特殊唤醒源来唤醒并产生复位。

​ Detect:sense除了可以唤醒sleep模式,还可以用来产生中断,即detect功能。DETECT类似一个中断标志位,是每个端口所有IO口进行或操作的结果,所以DETECT信号状态会受同一port下所有IO口的影响,只要有一个外部IO口有效,那么DETECT信号就一直为true,只有所有IO口状态都clear时,DETECT信号才会重新变成低。

​ ncs底层提供了将GPIO配置成一个特殊gpio device(如button,led) ),以便而=对其进行中断配置,通过gpio_dt_spec获取其GPIO配置。

gpiote则是具有task/event 的外设,作用就是让每个GPIO也具有传统意义上的task和event的功能,gpiote外设使用GPIO后,将会覆盖对GPIO直接的操作和配置。根据GPIO的特性,我们可以将GPIOTE的事件配置为<u>PORT EVENT 和Pin EVENT</u>。

<u>PORT EVNET</u> 是基于GPIO DETECT原理,是GPIO的sense特性,当所有外设和CPU都处于空闲状态时,此功能可用于在System ON模式下将CPU从WFI或WFE类型睡眠中唤醒,这意味着在System ON模式下功耗最低.但可能检测不到宽度仅为1us的脉冲(同一port上的任意pin脚都有可能使DETECT信号为高,产生PORT EVENT,,之后第二个pin上即便有SETECT信号变化也不再产生event,再次使用需要清空所有DETECT);

<u>Pin EVENT</u>是基于event中断(电流相对于Port Event高),是将每个GPIO口的沿变化配置到不同的GPIOTE通道,各自触发相应的IN EVENT,具有更高的事件精度。

GPIOTE输入中断,既可以是Pin Event,也可以用Port Event。FOR NRF54L15:

• GPIOTE20: 8 个channels 和 2 种中断, 使用 GPIO port P1 • GPIOTE30: 4 个channels 和 2 种中断, 使用 GPIO port P0 • 安全的GPIOTE通道可以配置安全和非安全的GPIO引脚 • 非安全GPIOTE通道只能配置非安全GPIO引脚

3.1 GPIOTE Pin Event

ncs/zephyr/samples/basic/button展示的是GPIO Pin Event的配置方法

注意,不推荐拿这个代码去处理button。因为这个是最底层的GPIO中断,并没有按键消抖功能,有button的应用需求可以用lbs里的button模块来做,一般只需要简单的引脚替换和硬件上细微改动。

3.2 GPIOTE PORT EVENT

Port Event的响应速度稍慢,但功耗会低15uA。如果要使用更低功耗的Port Event,可以作如下择一选择

3.2.1.在DeviceTree Overlay文件中,在对应的GPIO Port的节点中,设置一个`sense-edge-mask`属性,把所有需要使用Port Event的引脚的对应bit设为1即可,例如:

如果是nRF54L15中button0和button1分别使用P1.13(0xd)和P1.09(0x9),可以在app.overlay中加如下配置将这两个引脚配置为port event,其余gpio button为Pin EVENT:

&gpio1 {
    sense-edge-mask = <(BIT(13)|BIT(9))>;
};

二进制展开为0010 0010 0000 0000,十六进制为0x2200,因此也可以表达为:

&gpio1 {
    sense-edge-mask = <0x2200>;
};

*BIT(13)的意思就是`0x00000001<<13`。

3.2.2.参考lbs的例程中app_button的用法对GPIO口进行配置

app_button/lbs检测button信号分为两个阶段

​ a.扫描模式

​ Module starts in scanning mode and will switch to callback mode if no button is pressed.

​ 模块以扫描模式启动,如果扫描模式期间没约发现button GPIO口电平变化(即按下按钮),软件将切换到事件中断(回调)模式。

​ b.事件中断模式,

​ app_button进入事件中断模式后,功耗也会随之降下来,并低于15uA。

​ 参考app_button/lbs的配置,即使我们不修改devicetree, 也能将GPIO button功能模块配置成port event状态,如:

gpio_flags_t flags = button.dt_flags & GPIO_ACTIVE_LOW ? GPIO_PULL_UP : GPIO_PULL_DOWN;
ret = gpio_pin_configure_dt(&button, GPIO_INPUT | flags);
ret = gpio_pin_interrupt_configure_dt(&button,GPIO_INT_LEVEL_ACTIVE);

3.3 nRF54L15 GPIO休眠功耗 (Power Monitor for nRF54L15 GPIO event Wake UP)

 

3.3.1.Pin Event ppk2波形图

nrf54l15 gpio

3.3.2.Port Event ppk2波形图

nrf54l15 gpio

3.3.3.System OFF ppk2波形图

nrf54l15 gpio

nRF54L15 进入system off前需要关闭不使用的RAM

#ifdef CONFIG_SOC_NRF54L15_CPUAPP
  /* Disable RAM retention in System OFF as it is not utilized by this sample. */
  uint32_t ram_sections = 8;
  nrf_memconf_ramblock_ret_mask_enable_set(NRF_MEMCONF, 0, BIT_MASK(ram_sections), false);
  nrf_memconf_ramblock_ret2_mask_enable_set(NRF_MEMCONF, 0,   BIT_MASK(ram_sections), false);
#endif 
sys_poweroff();

4. 将特殊GPIOs 配置为通用 GPIOs

4.1 NFC pin 作为通用 GPIO

在NCSv2.8.0,在device tree overlay中修改UICR配置:

&uicr {
      nfct-pins-as-gpios;
};

添加后,系统启动时会自动擦写、配置UICR.

4.2 RESET PIN 作为通用 GPIO

对于nRF54L15,nRESET是Pin Reset专用pin. 查datasheet上可知,芯片并未为其分配作为通用GPIO口的Port和Pin number,nrf5340也是如此.

对于nrf52系列,可以通过ncs代码将nReset配置为通用GPIO口。其中:

​ nRF52805/52810/52811/52832对应GPIO口P0.21

​ nRF52820/52833/52840对应GPIO口P0.18

拿nrf52840 nreset举例,PSELRESET[0]和PSELREET[1]的值都是PIN=18,PORT=0,CONNECT=0的情况下,P0.18才会作为Reset引脚使用。否则,P0.18作为普通GPIO使用。Reset信号无法映射到其他GPIO。将nrf52的NReset pin配置为通用GPIO只需在devicetree中加如下代码:

&uicr{
          /delete-property/ gpio-as-nreset;
};

VS Code默认的烧录(west flash 烧录)会将pin reset打开导致程序里面删掉的UICR pin reset失效(相当于写了值到UICR)

4.3 低频晶振引脚XL1 XL2 作为通用 GPIO

4.3.1 对于 nrf52840/nrf53,P0.00/P0.01为XL1 XL2

​ a. CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y

​ b.devicetree 修改

&gpio1 {
    gpio-reserved-ranges = < 0x6 0x1 >, < 0x8 0x3 >, < 0x11 0x7 >;
    /*去除XL1,XL2描述,可选项*/
    gpio-line-names = "AREF", "A0", "A1", "RTS", "TXD", "CTS", "RXD", "NFC1", "NFC2", "BUTTON1", "BUTTON2", "LED1", "LED2", "LED3", "LED4", "QSPI CS", "RESET", "QSPI CLK", "QSPI DIO0", "QSPI DIO1", "QSPI DIO2", "QSPI DIO3", "BUTTON3", "BUTTON4", "SDA", "SCL", "A2", "A3", "A4", "A5";
};

4.3.2 对于nRF54L15,P1.00/P1.01 为XL1 XL2

​ CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y

5. 其他外设对GPIO的复用

A. Nordic nRF54L15给外设配置GPIO时,原则上是使用自己域内的IO口,同时需参考datasheet和nRF54L15DK的配置。外设的gpio配置可以自动覆盖(Override)GPIO的原来的输入输出方向、输出值等配置。

Peripheral Description
UARTE20/21 Can use any pin on P1. Can connect across power domains to dedicated pins on P2.
SPIMOO Has dedicated pins on P2. For 32 MHz operation, the pins must be configured usingextra high drive EO/E1 configuration in the DRIVE0/1 fields of the PIN_CNF GPIOregister.
SPIM20/21 Can use any pins on P1; see notes on clock pins. Can be connect across powerdomains to dedicated pins on P2.
SPIS20/21 Can use any pins on P1; see notes on clock pins. Can connect across power domainsto dedicated pins on P2.
TRACE Has dedicated pins that must be configured using extra high drive EO/E1configuration in the DRIVE0/1 fields of the PIN_CNF GPIO register.
GRTC Has dedicated pins for clock and PWM output.
TAMPC Has dedicated pins for active shield input and output.
FLPR Uses dedicated pins on P2 for emulated peripherals such as QSPI.
RADIO Uses dedicated pins on P1 for antenna switch control (DFEGPIO for directionfinding).
NFC Uses dedicated pins as listed in the pin assignments table for the selected device.These pins are configured as NFC antenna pins from reset. To use the pins forDigital 1/0, NFC function must be disabled in the NFCT - Near field communicationtag on page 348 peripheral.

B.peripheral 域(APB10,APB20,APB30)内的串行数据接口外设可以自由使用自己域内通用GPIO口

C. Port2(APB00)上的dedicated pins可以被peripheral 域内的串行数据接口如SPIM/SPIS/UARTE使用,但是仅限于P2内的dedicated Pin(参考Pin Assignment Table),比如:

​ a.Peripheral域内的SPI PSEL.MOSI可以使用P2 MOSI,但不能使用其他P2的其他GPIO

​ b.由于P2上没有TWI 的dedicated pin,所以periperal 区域的TWI不能跨域使用P2 GPIO

D. Nordic54L给外设配置GPIO时,外设时钟有专用GPIO口:

nrf54l15 gpio

下图中红色GPIO口可用于时钟.

nrf54l15 gpio

 

订阅Nordic新闻简报

了解最新信息!订阅后即可获取最新Nordic及物联网资讯

立即订阅