gpio - GPIO Operation#

Adapted Air780E/Air700E Air780EP Air601 Air101/Air103 Air105 ESP32C3 ESP32S3

Note

This page document is automatically generated by this file. If there is any error, please submit issue or help modify pr, thank you!

Tip

This library has its own demo,click this link to view the demo example of gpio

Tip

There are also video tutorials in this library, click this link to view

Constant#

constant

type

explanation

gpio.NONE

number

Invalid pin, generally used to tell the bottom of a function pin is not specified

gpio.LOW

number

Low level

gpio.HIGH

number

High Level

gpio.PULLUP

number

Pull-up

gpio.PULLDOWN

number

Drop-down

gpio.RISING

number

Rising edge trigger

gpio.FALLING

number

Falling edge trigger

gpio.BOTH

number

Two-way trigger, some devices support

gpio.HIGH_IRQ

number

High level trigger, some devices support

gpio.LOW_IRQ

number

Low-level trigger, some devices support

gpio.setup(pin, mode, pull, irq, alt)#

Set Pin Function

Parameters

Incoming Value Type

Explanation

int

pin gpio Number, must be numeric

any

mode Input and output mode:
number 0/1 represents output mode
nil represents input mode
function represents interrupt mode. if gpio.count is filled in, it is interrupt counting function, and there is no callback when interrupting

int

pull pull-up pull-down mode, which can be pull-up mode gpio.PULLUP or pull-down mode gpio.PULLDOWN, or open-leak mode 0. need to be selected according to the actual hardware

int

irq Interrupt trigger mode, default gpio.BOTH. interrupt triggering mode
rising edge gpio.RISING
falling edge gpio.FALLING
both rising and falling are triggered gpio.BOTH

int

alt Multiplexing option, currently only EC618 platform needs this parameter, some GPIO can be multiplexed to different pins, you can select the multiplexing option (0 or 4) to multiplex to the corresponding pin

Return Value

return value type

explanation

any

Output mode returns the closure of the set level, input mode and interrupt mode returns the closure of the acquired level

Examples

-- Set gpio17 as input
gpio.setup(17, nil)

-- Set gpio17 to output, and the initialization level is low, using the hardware default pull-up configuration
gpio.setup(17, 0)

-- Set gpio17 to output, initialize level high, and enable internal pull-up
gpio.setup(17, 1, gpio.PULLUP)

-- Set gpio27 to interrupt, default two-way trigger
gpio.setup(27, function(val)
    print("IRQ_27",val) -- Reminder, val does not represent the trigger direction, only the level at a certain point in time after the interruption
end, gpio.PULLUP)

-- Set gpio27 to interrupt, rising edge only trigger
gpio.setup(27, function(val)
    print("IRQ_27",val) -- Reminder, val does not represent the trigger direction, only the level at a certain point in time after the interruption
end, gpio.PULLUP, gpio.RISING)

-- Interrupt count added in 2024.5.8
-- Set gpio7 to interrupt count. For detailed demo, see gpio/gpio_irq_count
gpio.setup(7, gpio.count)

-- alt_func Added in 2023.7.2
-- This function is only valid for some platforms and is only used to adjust GPIO multiplexing. For other multiplexing methods, please use the mu c.iomux function
-- The following sample code reuses the I2S_DOUT gpio18
-- AIR780E PIN33 (module pin number), corresponding to paddr 38, the default function is I2S_DOUT, multiplexed gpio18
-- Direction output, and initialization level is low, using hardware default pull-down configuration
-- Air780E(EC618 For GPIO multiplexing of the series, please refer to the hardware data table on the https://air780e.cn homepage.Air780E&Air780EG&Air780EX&Air700E_GPIO_table_20231227.pdf)
-- Air780EP(EC718P For GPIO multiplexing of the series, please refer to the hardware data table on the https://air780ep.cn homepage.Air780E&Air780EG&Air780EX&Air700E_GPIO_table_20231227.pdf)
gpio.setup(18, 0, nil, nil, 4)

-- Reminder :
-- When the pin is in input mode or interrupt, the level can be obtained through gpio.get()
-- When the pin is in output mode, the level can be set through gpio.set()
-- When the pin is in output mode, you will always get it through gpio.get().0
-- The val parameter of the interrupt callback does not represent the trigger direction, but only represents the level at a certain point in time after the interrupt.
-- Yeah, Cat. 1 module, EC618 series can only be triggered in both directions by AONGPIO, and all GPIO in other series can be triggered in both directions. Please refer to the hardware manual for details.
-- By default, there is no anti-shake time for interruption, and the anti-shake time can be set through gpio.set_debounce(pin, 50)

-- pull Additional description of parameters, pull-up/pull-down configuration
-- For some BSPs, only gpio.PULLUP or gpio.PULLDOWN is supported, but some BSPs support open-drain mode.
-- For bsp that supports open drain, the pull parameter must be passed to 0 to open drain mode, not pass nil
-- For example:
-- EC618 Series (Air780E/Air780EG/Air780EX/Air700E, etc.)
-- EC718 Series (Air780EP/Air780EPV, etc.)
-- XT804 Series(Air101/Air103/Air601)

gpio.caplevel(pin, level,func)#

Capture Pin Level Duration, Unit us

Parameters

Incoming Value Type

Explanation

int

pin GPIO Number, must be numeric

int

level The level to be captured can be high level gpio.HIGH, low level gpio.LOW, or directly write the value 1 or 0, I .e. the normal time on the pin is opposite to level, capturing the set level duration

function

func The callback function after the capture is completed. There is only one parameter. The parameter is the number-type value of the captured time length, in units.us

Return Value

return value type

explanation

any

Returns the closure of the acquisition level

Examples

-- Capture the duration when pin.PA07 is high
gpio.caplevel(pin.PA07,1,function(val) print(val) end)

gpio.set(pin, value)#

Set Pin Level

Parameters

Incoming Value Type

Explanation

int

pin GPIO Number, must be numeric

int

value Level, which can be high level gpio.HIGH, low level gpio.LOW, or directly write the value 1 or 0

Return Value

return value type

explanation

nil

No return value

Examples

-- Set gpio17 to low
gpio.set(17, 0)

gpio.get(pin)#

Get Pin Level

Parameters

Incoming Value Type

Explanation

int

pin GPIO Number, must be numeric

Return Value

return value type

explanation

value

Level, high level gpio.HIGH, low level gpio.LOW, corresponding to value 1 and 0

Examples

-- Get the current level of gpio17.
gpio.get(17)

gpio.close(pin)#

Turn off the pin function (high impedance input state), turn off the interrupt

Parameters

Incoming Value Type

Explanation

int

pin GPIO Number, must be numeric

Return Value

return value type

explanation

nil

No return value, always successful execution

Examples

-- Close gpio17
gpio.close(17)

gpio.setDefaultPull(val)#

Set the default pull-up/pull-down setting of GPIO pin, the default is platform customization (generally open and leak).

Parameters

Incoming Value Type

Explanation

int

val 0 Platform customization, 1 pull-up, 2 pull-down

Return Value

return value type

explanation

boolean

If the pass value is correct, return true, otherwise return false

Examples

-- Set the pull default value of gpio.setup to pull up
gpio.setDefaultPull(1)

gpio.toggle(pin)#

Transform GPIO pin output level, only output mode available

Parameters

Incoming Value Type

Explanation

int

GPIO number of the pin

Return Value

return value type

explanation

nil

No return value

Examples

-- This API was added on 2022.05.17
-- Assuming there are LEDs on GPIO16, switch every 500ms
gpio.setup(16, 0)
sys.timerLoopStart(function()
    gpio.toggle(16)
end, 500)

gpio.pulse(pin,level,len,delay)#

Output a set of pulses at the same GPIO. Note that len’s unit is bit, with the high bit first..

Parameters

Incoming Value Type

Explanation

int

gpio No.

int/string

Value or string.

int

len The unit of length is bit, with the high bit in front..

int

delay Delay, currently no fixed time unit

Return Value

return value type

explanation

nil

No return value

Examples

-- Through the PB06 pin output output 8 level changes.
gpio.pulse(pin.PB06,0xA9, 8, 0)

gpio.debounce(pin, ms, mode)#

Anti-shake settings, anti-shake according to hardware ticks

Parameters

Incoming Value Type

Explanation

int

gpio Number, 0~127, related to hardware

int

Anti-shake duration, in milliseconds, with a maximum of 65555 ms. If it is set to 0, it is turned off.

int

mode, 0 cooling mode, 1 delay mode. Default is 0

Return Value

return value type

explanation

nil

No return value

Examples

-- Shake elimination mode, currently supports 2 types, starting on 2022.12.16 mode=1
-- 0 After the interrupt is triggered, report it once immediately, and then accept the interrupt again after cooling for N milliseconds.
-- 1 After triggering the interrupt, delay N milliseconds, during which there is no new interrupt and no level change, report once

-- Turn on anti-shake, mode 0-cooling, report immediately after interruption, but report only once within 100ms
gpio.debounce(7, 100) -- If the chip supports pin library, pin.PA7 can be used instead of numbers.7
-- Turn on anti-shake, mode 1-delay, wait for 100ms after interruption, if the level is maintained during the period, report once after the time is up
-- Correspondingly, if the input is a 50hz square wave, then no report will be triggered.
gpio.debounce(7, 100, 1)

-- Turn off anti-shake, and turn off when the time is set to 0.
gpio.debounce(7, 0)

gpio.count(pin)#

Obtain the number of gpio interrupts and clear the cumulative value, similar to the pulse count of air724.

Parameters

Incoming Value Type

Explanation

int

gpio Number, 0~127, related to hardware

Return Value

return value type

explanation

int

Returns the count of interrupts from the last time the number of interrupts was obtained to the current

Examples

log.info("irq cnt", gpio.count(10))