12. GPIO
GPIO objects provide GPIO control functions.
Functional overview:
- Provides a function to set and control the input / output of the GPIO port.
- Provides PWM output function.
Abstracts
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
new GPIO() | Creates a GPIO instance. | 01.00.00+ |
{GPIO} Instance
Methods()/Properties | Summary | Version | Note |
---|---|---|---|
.setMode() | Sets the GPIO operation mode. | 01.00.00+ | |
.getValue() | Gets the IO port state. | 01.00.00+ | |
.setValue() | Sets the IO port output. | 01.00.00+ | |
.setIrq() | Sets the IO port input interrupt event. | 01.00.00+ | |
.disableIrq() | Invalidates the IO port input interrupt event set by .setIrq(). | 01.00.00+ | |
.setPwm() | Sets the PWM output. | 01.00.00+ | |
.releasePwm() | Stops and invalidates the PWM output set by .setPwm() | 01.00.00+ | |
.configureWakeup() | Sets the wakeup trigger. | 01.00.00+ | |
.setOnePulse() | Outputs one arbitrary pulse. | 01.00.00+ |
Details
new GPIO(gpioNo,mode)
Creates a GPIO instance.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
gpioNo | number | mandatory | Specify the GPIO number. Please refer to Pinout. | |
mode | number | mandatory | Specify the GPIO operation mode. Please refer to GPIO operation mode details. | |
return | {GPIO} | - | {GPIO} : Generated {GPIO} |
GPIO operation mode details
mode | I/O | Summary | Note |
---|---|---|---|
0 | IN | PULL-UP | - |
1 | IN | PULL-DOWN | - |
2 | IN | - | |
3 | OUT | PUSH-PULL PULL-UP | - |
4 | OUT | PUSH-PULL PULL-DOWN | - |
5 | OUT | PUSH-PULL | - |
6 | OUT | OPEN-DRAIN PULL-UP | - |
7 | OUT | OPEN-DRAIN PULL-DOWN | - |
8 | OUT | OPEN-DRAIN | - |
9 | OUT | PWM | Please refer to Pinout for the ports that can use the PWM function. |
.setMode(mode)
Sets the GPIO operation mode.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
mode | number | mandatory | GPIO operation mode Please refer to GPIO operation mode details. | |
return | boolean | - | true: Success false: Failure |
.getValue()
Gets the IO port state.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | number | - | IO port status 0: Low 1: High | If the status cannot be obtained due to the IO port setting, -1 is returned. |
.setValue(value)
Sets the IO port output.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
value | number | mandatory | IO port output value 0: Low 1: High | |
return | boolean | - | true: Success false: Failure |
.setIrq(edge,callback)
Sets the IO port input interrupt event.
When the specified interrupt condition occurs, the hardware interrupt is triggered and the callback is executed.
It is also used as a resume trigger in stop mode.
In addition, it is not possible to re-set the interrupt event within the interrupt event callback process execution.
For the ports that can use the interrupt function, refer to Pinout.
This function can be used for up to 4 ports.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
edge | number | mandatory | Interrupt condition 1: Rising edge 2: Falling edge 3: Rising / Falling edge | |
callback(value) | function | mandatory | Executes callback processing when an interrupt event occurs. value : {number} IO port status at event occurrence 0: Low 1: High | |
return | boolean | - | true: Success false: Failure |
.disableIrq()
Invalidates the IO port input interrupt event set by .setIrq().
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | boolean | - | true: Success false: Failure |
.setPwm(period,pulse)
Sets the PWM (Pulse Width Modulation) output.
Outputs a periodic pulse specified by the period and width.
Specify GPIO operation mode 9 in advance.
The generated PWM signal starts with a Low output.
The Low output is maintained until the duration specified by the pulse width, and then becomes High output until the start of the next pulse period.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
period | number | mandatory | Pulse period [ x1us | x100us ]Range: 1 - 1000000 | 1 - 10000 | Some ports have restrictions on the range. Please refer to Pinout and check the notes. |
pulse | number | mandatory | Pulse width [ x1us | x100us ]Range: 0 - 1000000 | 0 - 10000 | Pulse must be less than the period. |
return | boolean | - | true: Success false: Failure |
.releasePwm()
Stops and invalidates the PWM output set by .setPwm().
To enable the PWM control again, specify GPIO operation mode 9 again.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
return | boolean | - | true: Success false: Failure |
.configureWakeup(edge)
Sets the wakeup trigger.
During standby mode, when the wakeup trigger is valid and the specified edge change has occurred, the system will resume.
For the ports that can use the wakeup function, refer to Pinout.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
edge | number | mandatory | Set the wakeup trigger. 0: Invalid 1: Rising edge 2: Falling edge | Disabled by default. |
return | boolean | - | true: Success false: Failure |
.setOnePulse(value1,width1,value2,width2)
Outputs one arbitrary pulse.
This function outputs an arbitrary pulse generated by block type processing by software.
The cycle is not strictly guaranteed like PWM, so please use it after evaluating it.
Name | Type | M/O | Summary | Note |
---|---|---|---|---|
value1 | number | mandatory | 1st signal; Specify the value to be output to the IO port. 0: Low 1: High | |
width1 | number | mandatory | 1st signal; Specify the output time [ms] of the IO port. Range: 0 - 100 | If 0 is specified, it will be about 1.2us. |
value2 | number | mandatory | 2nd signal; Specify the value to be output to the IO port. 0: Low 1: High | |
width2 | number | mandatory | 2nd signal; Specify the output time [ms] of the IO port. Range: 0 - 100 | If 0 is specified, it will be about 1.2us. |
return | boolean | - | true: Success false: Failure |
Object Usage Examples
Sample 1
This is a sample that uses interrupts.
When the level of GPIO13 changes from H to L within 10 seconds, the callback is executed.
var gp = new GPIO(13,2);
gp.setIrq(2, function(sts) {
print('status fall.');
});
setTimeout(function() {
gp.disableIrq();
print('timeout!');
}, 10000).wait();
Sample 2
This is a sample that uses PWM control.
Using GPIO11, PWM output of 10ms period and 5ms pulse width is executed for 10 seconds.
var gp = new GPIO(11,9);
gp.setPwm(100, 50);
setTimeout(function() {
gp.releasePwm();
print('timeout!');
}, 10000).wait();