mirror of
https://github.com/espressif/ESP8266_RTOS_SDK.git
synced 2025-05-28 21:48:05 +08:00
refactor(gpio): Refactor gpio driver for esp8266 idf
This commit is contained in:
9
examples/peripherals/gpio/Makefile
Normal file
9
examples/peripherals/gpio/Makefile
Normal file
@ -0,0 +1,9 @@
|
||||
#
|
||||
# This is a project Makefile. It is assumed the directory this Makefile resides in is a
|
||||
# project subdirectory.
|
||||
#
|
||||
|
||||
PROJECT_NAME := gpio
|
||||
|
||||
include $(IDF_PATH)/make/project.mk
|
||||
|
80
examples/peripherals/gpio/README.md
Normal file
80
examples/peripherals/gpio/README.md
Normal file
@ -0,0 +1,80 @@
|
||||
# _GPIO Example_
|
||||
|
||||
_This test code shows how to configure gpio and how to use gpio interrupt._
|
||||
|
||||
## GPIO functions
|
||||
|
||||
* GPIO15: output
|
||||
* GPIO16: output
|
||||
* GPIO4: input, pulled up, interrupt from rising edge and falling edge
|
||||
* GPIO5: input, pulled up, interrupt from rising edge.
|
||||
|
||||
## How to use example
|
||||
|
||||
### Hardware Required
|
||||
|
||||
* Connect GPIO15 with GPIO4
|
||||
* Connect GPIO16 with GPIO5
|
||||
|
||||
### Configure the project
|
||||
|
||||
```
|
||||
make menuconfig
|
||||
```
|
||||
|
||||
* Set serial port under Serial Flasher Options.
|
||||
|
||||
|
||||
### Build and Flash
|
||||
|
||||
Build the project and flash it to the board, then run monitor tool to view serial output:
|
||||
|
||||
```
|
||||
make -j4 flash monitor
|
||||
```
|
||||
|
||||
(To exit the serial monitor, type ``Ctrl-]``.)
|
||||
|
||||
See the Getting Started Guide for full steps to configure and use ESP-IDF to build projects.
|
||||
|
||||
## Example Output
|
||||
|
||||
* Generate pulses on GPIO15/16, that triggers interrupt on GPIO4/5
|
||||
|
||||
```
|
||||
I (0) gpio: GPIO[15]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
|
||||
I (0) gpio: GPIO[16]| InputEn: 0| OutputEn: 1| OpenDrain: 0| Pullup: 0| Pulldown: 0| Intr:0
|
||||
I (0) gpio: GPIO[4]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:1
|
||||
I (0) gpio: GPIO[5]| InputEn: 1| OutputEn: 0| OpenDrain: 0| Pullup: 1| Pulldown: 0| Intr:1
|
||||
I (0) main: cnt: 0
|
||||
|
||||
I (1) main: cnt: 1
|
||||
|
||||
I (1) main: GPIO[4] intr, val: 1
|
||||
|
||||
I (1) main: GPIO[5] intr, val: 1
|
||||
|
||||
I (2) main: cnt: 2
|
||||
|
||||
I (2) main: GPIO[4] intr, val: 0
|
||||
|
||||
I (3) main: cnt: 3
|
||||
|
||||
I (3) main: GPIO[4] intr, val: 1
|
||||
|
||||
I (3) main: GPIO[5] intr, val: 1
|
||||
|
||||
I (4) main: cnt: 4
|
||||
|
||||
I (4) main: GPIO[4] intr, val: 0
|
||||
|
||||
I (5) main: cnt: 5
|
||||
|
||||
I (5) main: GPIO[4] intr, val: 1
|
||||
|
||||
I (5) main: GPIO[5] intr, val: 1
|
||||
|
||||
I (6) main: cnt: 6
|
||||
|
||||
I (6) main: GPIO[4] intr, val: 0
|
||||
```
|
3
examples/peripherals/gpio/main/component.mk
Normal file
3
examples/peripherals/gpio/main/component.mk
Normal file
@ -0,0 +1,3 @@
|
||||
#
|
||||
# Main Makefile. This is basically the same as a component makefile.
|
||||
#
|
129
examples/peripherals/gpio/main/user_main.c
Normal file
129
examples/peripherals/gpio/main/user_main.c
Normal file
@ -0,0 +1,129 @@
|
||||
// Copyright 2018-2025 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/queue.h"
|
||||
|
||||
#include "driver/gpio.h"
|
||||
|
||||
#include "esp_log.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
static const char *TAG = "main";
|
||||
|
||||
/**
|
||||
* Brief:
|
||||
* This test code shows how to configure gpio and how to use gpio interrupt.
|
||||
*
|
||||
* GPIO status:
|
||||
* GPIO15: output
|
||||
* GPIO16: output
|
||||
* GPIO4: input, pulled up, interrupt from rising edge and falling edge
|
||||
* GPIO5: input, pulled up, interrupt from rising edge.
|
||||
*
|
||||
* Test:
|
||||
* Connect GPIO15 with GPIO4
|
||||
* Connect GPIO16 with GPIO5
|
||||
* Generate pulses on GPIO15/16, that triggers interrupt on GPIO4/5
|
||||
*
|
||||
*/
|
||||
|
||||
#define GPIO_OUTPUT_IO_0 15
|
||||
#define GPIO_OUTPUT_IO_1 16
|
||||
#define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))
|
||||
#define GPIO_INPUT_IO_0 4
|
||||
#define GPIO_INPUT_IO_1 5
|
||||
#define GPIO_INPUT_PIN_SEL ((1ULL<<GPIO_INPUT_IO_0) | (1ULL<<GPIO_INPUT_IO_1))
|
||||
|
||||
static xQueueHandle gpio_evt_queue = NULL;
|
||||
|
||||
static void gpio_isr_handler(void *arg)
|
||||
{
|
||||
uint32_t gpio_num = (uint32_t) arg;
|
||||
xQueueSendFromISR(gpio_evt_queue, &gpio_num, NULL);
|
||||
}
|
||||
|
||||
static void gpio_task_example(void *arg)
|
||||
{
|
||||
uint32_t io_num;
|
||||
|
||||
for (;;) {
|
||||
if (xQueueReceive(gpio_evt_queue, &io_num, portMAX_DELAY)) {
|
||||
ESP_LOGI(TAG, "GPIO[%d] intr, val: %d\n", io_num, gpio_get_level(io_num));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
gpio_config_t io_conf;
|
||||
//disable interrupt
|
||||
io_conf.intr_type = GPIO_INTR_DISABLE;
|
||||
//set as output mode
|
||||
io_conf.mode = GPIO_MODE_OUTPUT;
|
||||
//bit mask of the pins that you want to set,e.g.GPIO18/19
|
||||
io_conf.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
|
||||
//disable pull-down mode
|
||||
io_conf.pull_down_en = 0;
|
||||
//disable pull-up mode
|
||||
io_conf.pull_up_en = 0;
|
||||
//configure GPIO with the given settings
|
||||
gpio_config(&io_conf);
|
||||
|
||||
//interrupt of rising edge
|
||||
io_conf.intr_type = GPIO_INTR_POSEDGE;
|
||||
//bit mask of the pins, use GPIO4/5 here
|
||||
io_conf.pin_bit_mask = GPIO_INPUT_PIN_SEL;
|
||||
//set as input mode
|
||||
io_conf.mode = GPIO_MODE_INPUT;
|
||||
//enable pull-up mode
|
||||
io_conf.pull_up_en = 1;
|
||||
gpio_config(&io_conf);
|
||||
|
||||
//change gpio intrrupt type for one pin
|
||||
gpio_set_intr_type(GPIO_INPUT_IO_0, GPIO_INTR_ANYEDGE);
|
||||
|
||||
//create a queue to handle gpio event from isr
|
||||
gpio_evt_queue = xQueueCreate(10, sizeof(uint32_t));
|
||||
//start gpio task
|
||||
xTaskCreate(gpio_task_example, "gpio_task_example", 2048, NULL, 10, NULL);
|
||||
|
||||
//install gpio isr service
|
||||
gpio_install_isr_service(0);
|
||||
//hook isr handler for specific gpio pin
|
||||
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *) GPIO_INPUT_IO_0);
|
||||
//hook isr handler for specific gpio pin
|
||||
gpio_isr_handler_add(GPIO_INPUT_IO_1, gpio_isr_handler, (void *) GPIO_INPUT_IO_1);
|
||||
|
||||
//remove isr handler for gpio number.
|
||||
gpio_isr_handler_remove(GPIO_INPUT_IO_0);
|
||||
//hook isr handler for specific gpio pin again
|
||||
gpio_isr_handler_add(GPIO_INPUT_IO_0, gpio_isr_handler, (void *) GPIO_INPUT_IO_0);
|
||||
|
||||
int cnt = 0;
|
||||
|
||||
while (1) {
|
||||
ESP_LOGI(TAG, "cnt: %d\n", cnt++);
|
||||
vTaskDelay(1000 / portTICK_RATE_MS);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_0, cnt % 2);
|
||||
gpio_set_level(GPIO_OUTPUT_IO_1, cnt % 2);
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user