Remove mutex from GPIO driver code. Replace uint8_t/uint16_t with uint32_t

This commit is contained in:
Wangjialin 2016-09-21 09:51:37 +08:00
parent 948be5c0c4
commit f7b10745be
2 changed files with 178 additions and 235 deletions

View file

@ -16,7 +16,6 @@
#include "esp_err.h" #include "esp_err.h"
#include "esp_intr.h" #include "esp_intr.h"
#include "freertos/FreeRTOS.h" #include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
#include "freertos/xtensa_api.h" #include "freertos/xtensa_api.h"
#include "soc/soc.h" #include "soc/soc.h"
#include "driver/gpio.h" #include "driver/gpio.h"
@ -49,7 +48,7 @@
#define GPIO_ERROR(...) #define GPIO_ERROR(...)
#endif #endif
const uint32_t GPIO_PIN_MUX_REG[40] = { const uint32_t GPIO_PIN_MUX_REG[GPIO_PIN_COUNT] = {
GPIO_PIN_REG_0, GPIO_PIN_REG_0,
GPIO_PIN_REG_1, GPIO_PIN_REG_1,
GPIO_PIN_REG_2, GPIO_PIN_REG_2,
@ -92,8 +91,7 @@ const uint32_t GPIO_PIN_MUX_REG[40] = {
GPIO_PIN_REG_39 GPIO_PIN_REG_39
}; };
static SemaphoreHandle_t gpio_mutex = NULL; #define IS_VALID_GPIO(gpio_num) ( (gpio_num < GPIO_PIN_COUNT && GPIO_PIN_MUX_REG[gpio_num] != 0))
static int is_valid_gpio(int gpio_num) static int is_valid_gpio(int gpio_num)
{ {
if(gpio_num >= GPIO_PIN_COUNT || GPIO_PIN_MUX_REG[gpio_num] == 0) { if(gpio_num >= GPIO_PIN_COUNT || GPIO_PIN_MUX_REG[gpio_num] == 0) {
@ -103,17 +101,7 @@ static int is_valid_gpio(int gpio_num)
return 1; return 1;
} }
static esp_err_t gpio_mutex_init() esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type)
{
if(gpio_mutex == NULL) {
gpio_mutex = xSemaphoreCreateRecursiveMutex();
if(gpio_mutex == NULL)
return ESP_FAIL;
}
return ESP_OK;
}
static esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, uint8_t intr_type)
{ {
if(!is_valid_gpio(gpio_num)) if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
@ -121,79 +109,28 @@ static esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, uint8_t intr_type)
GPIO_ERROR("Unknown GPIO intr:%u\n",intr_type); GPIO_ERROR("Unknown GPIO intr:%u\n",intr_type);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if(gpio_mutex != NULL) {
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
GPIO.pin[gpio_num].int_type = intr_type; GPIO.pin[gpio_num].int_type = intr_type;
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK; return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }
static esp_err_t gpio_intr_enable(gpio_num_t gpio_num) esp_err_t gpio_intr_enable(gpio_num_t gpio_num)
{ {
if(!is_valid_gpio(gpio_num)) if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
if(gpio_mutex != NULL) {
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
if(xPortGetCoreID() == 0) { if(xPortGetCoreID() == 0) {
GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr GPIO.pin[gpio_num].int_ena = GPIO_PRO_CPU_INTR_ENA; //enable pro cpu intr
} else { } else {
GPIO.pin[gpio_num].int_ena = GPIO_APP_CPU_INTR_ENA; //enable pro cpu intr GPIO.pin[gpio_num].int_ena = GPIO_APP_CPU_INTR_ENA; //enable pro cpu intr
} }
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK; return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }
static esp_err_t gpio_intr_disable(gpio_num_t gpio_num) esp_err_t gpio_intr_disable(gpio_num_t gpio_num)
{ {
if(!is_valid_gpio(gpio_num)) if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
if(gpio_mutex != NULL) {
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr GPIO.pin[gpio_num].int_ena = 0; //disable GPIO intr
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK; return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
}
static esp_err_t gpio_input_enable(gpio_num_t gpio_num)
{
if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG;
if(gpio_mutex != NULL) {
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_num]);
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
}
static esp_err_t gpio_input_disable(gpio_num_t gpio_num)
{
if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG;
if(gpio_mutex != NULL) {
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_num]);
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }
static esp_err_t gpio_output_disable(gpio_num_t gpio_num) static esp_err_t gpio_output_disable(gpio_num_t gpio_num)
@ -214,10 +151,8 @@ static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
GPIO_ERROR("io_num=%d can only be input\n",gpio_num); GPIO_ERROR("io_num=%d can only be input\n",gpio_num);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if(gpio_num >= GPIO_PIN_COUNT || GPIO_PIN_MUX_REG[gpio_num] == 0) { if(!is_valid_gpio(gpio_num))
GPIO_ERROR("io_num=%d does not exist\n",gpio_num);
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
}
if(gpio_num < 32) { if(gpio_num < 32) {
GPIO.enable_w1ts = (0x1 << gpio_num); GPIO.enable_w1ts = (0x1 << gpio_num);
} else { } else {
@ -228,7 +163,7 @@ static esp_err_t gpio_output_enable(gpio_num_t gpio_num)
esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level) esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level)
{ {
if(!is_valid_gpio(gpio_num)) if(!IS_VALID_GPIO(gpio_num))
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
if(level) { if(level) {
if(gpio_num < 32) if(gpio_num < 32)
@ -255,11 +190,8 @@ int gpio_get_level(gpio_num_t gpio_num)
esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull) esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
{ {
if(gpio_mutex != NULL) {
if(!is_valid_gpio(gpio_num)) if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
switch(pull) { switch(pull) {
case GPIO_PULLUP_ONLY: case GPIO_PULLUP_ONLY:
@ -283,99 +215,83 @@ esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull)
ret = ESP_ERR_INVALID_ARG; ret = ESP_ERR_INVALID_ARG;
break; break;
} }
xSemaphoreGiveRecursive(gpio_mutex);
return ret; return ret;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }
esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_direction_t direction) esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode)
{ {
if(gpio_mutex != NULL) {
if(!is_valid_gpio(gpio_num)) if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY); if(gpio_num >= 34 && (mode & (GPIO_MODE_DEF_OUTPUT))) {
GPIO_ERROR("io_num=%d can only be input\n",gpio_num);
return ESP_ERR_INVALID_ARG;
}
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
switch(direction) { if(mode & GPIO_MODE_DEF_INPUT) {
case GPIO_DIR_OUTPUT_ONLY: PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[gpio_num]);
gpio_input_disable(gpio_num);
gpio_output_enable(gpio_num);
break;
case GPIO_DIR_INPUT_ONLY:
gpio_input_enable(gpio_num);
gpio_output_disable(gpio_num);
break;
case GPIO_DIR_INPUT_AND_OUTPUT:
gpio_input_enable(gpio_num);
gpio_output_enable(gpio_num);
break;
case GPIO_DIR_DISABLE_IO:
gpio_input_disable(gpio_num);
gpio_output_disable(gpio_num);
break;
default:
GPIO_ERROR("Unknown direction type,gpio_num=%u,direction=%u\n",gpio_num,direction);
ret = ESP_ERR_INVALID_ARG;
break;
}
xSemaphoreGiveRecursive(gpio_mutex);
return ret;
} else { } else {
GPIO_ERROR("Mutex null\n"); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[gpio_num]);
return ESP_FAIL;
} }
if(mode & GPIO_MODE_DEF_OUTPUT) {
if(gpio_num < 32) {
GPIO.enable_w1ts = (0x1 << gpio_num);
} else {
GPIO.enable1_w1ts.data = (0x1 << (gpio_num - 32));
}
} else {
if(gpio_num < 32) {
GPIO.enable_w1tc = (0x1 << gpio_num);
} else {
GPIO.enable1_w1tc.data = (0x1 << (gpio_num - 32));
}
}
if(mode & GPIO_MODE_DEF_OD) {
GPIO.pin[gpio_num].pad_driver = 1;
} else {
GPIO.pin[gpio_num].pad_driver = 0;
}
return ret;
} }
esp_err_t gpio_config(gpio_config_t *pGPIOConfig) esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
{ {
if(gpio_mutex == NULL) {
gpio_mutex = xSemaphoreCreateRecursiveMutex();
if(gpio_mutex == NULL) {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
}
uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask); uint64_t gpio_pin_mask = (pGPIOConfig->pin_bit_mask);
uint32_t io_reg = 0; uint32_t io_reg = 0;
uint8_t io_num = 0; uint32_t io_num = 0;
uint64_t bit_valid = 0; uint64_t bit_valid = 0;
if(pGPIOConfig->pin_bit_mask == 0) { if(pGPIOConfig->pin_bit_mask == 0 || pGPIOConfig->pin_bit_mask >= (((uint64_t) 1) << GPIO_PIN_COUNT)) {
GPIO_ERROR("GPIO_PIN = 0 \n"); GPIO_ERROR("GPIO_PIN mask error \n");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
if((pGPIOConfig->mode) & (BIT1)) { if((pGPIOConfig->mode) & (GPIO_MODE_DEF_OUTPUT)) {
//GPIO 34/35/36/37/38/39 can only be used as input mode; //GPIO 34/35/36/37/38/39 can only be used as input mode;
if((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) { if((gpio_pin_mask & ( GPIO_SEL_34 | GPIO_SEL_35 | GPIO_SEL_36 | GPIO_SEL_37 | GPIO_SEL_38 | GPIO_SEL_39))) {
GPIO_ERROR("GPIO34-39 can only be used as input mode\n"); GPIO_ERROR("GPIO34-39 can only be used as input mode\n");
return ESP_ERR_INVALID_ARG; return ESP_ERR_INVALID_ARG;
} }
} }
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
do { do {
io_reg = GPIO_PIN_MUX_REG[io_num]; io_reg = GPIO_PIN_MUX_REG[io_num];
if(((gpio_pin_mask >> io_num) & BIT(0)) && io_reg) { if(((gpio_pin_mask >> io_num) & BIT(0)) && io_reg) {
GPIO_INFO("Gpio%02d |Mode:",io_num); GPIO_INFO("Gpio%02d |Mode:",io_num);
if((pGPIOConfig->mode) & GPIO_MODE_INPUT) { if((pGPIOConfig->mode) & GPIO_MODE_DEF_INPUT) {
GPIO_INFO("INPUT "); GPIO_INFO("INPUT ");
gpio_input_enable(io_num); PIN_INPUT_ENABLE(GPIO_PIN_MUX_REG[io_num]);
} else { } else {
gpio_input_disable(io_num); PIN_INPUT_DISABLE(GPIO_PIN_MUX_REG[io_num]);
} }
if((pGPIOConfig->mode) & BIT2) { if((pGPIOConfig->mode) & GPIO_MODE_DEF_OD) {
GPIO_INFO("OD "); GPIO_INFO("OD ");
GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */ GPIO.pin[io_num].pad_driver = 1; /*0x01 Open-drain */
} else { } else {
GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */ GPIO.pin[io_num].pad_driver = 0; /*0x00 Normal gpio output */
} }
if((pGPIOConfig->mode) & GPIO_MODE_OUTPUT) { if((pGPIOConfig->mode) & GPIO_MODE_DEF_OUTPUT) {
GPIO_INFO("OUTPUT "); GPIO_INFO("OUTPUT ");
gpio_output_enable(io_num); gpio_output_enable(io_num);
} else { } else {
gpio_output_disable(io_num); gpio_output_disable(io_num);
} }GPIO_INFO("|");
GPIO_INFO("|");
if(pGPIOConfig->pull_up_en) { if(pGPIOConfig->pull_up_en) {
GPIO_INFO("PU "); GPIO_INFO("PU ");
PIN_PULLUP_EN(io_reg); PIN_PULLUP_EN(io_reg);
@ -401,32 +317,26 @@ esp_err_t gpio_config(gpio_config_t *pGPIOConfig)
} }
io_num++; io_num++;
} while(io_num < GPIO_PIN_COUNT); } while(io_num < GPIO_PIN_COUNT);
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK; return ESP_OK;
} }
esp_err_t gpio_intr_handler_register(uint8_t gpio_intr_num, void (*fn)(void*), void * arg) esp_err_t gpio_intr_handler_register(uint32_t gpio_intr_num, void (*fn)(void*), void * arg)
{ {
if(gpio_mutex != NULL) { if(fn == NULL)
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY); return ESP_ERR_INVALID_ARG;
ESP_INTR_DISABLE(gpio_intr_num); ESP_INTR_DISABLE(gpio_intr_num);
intr_matrix_set(xPortGetCoreID(), ETS_GPIO_INTR_SOURCE, gpio_intr_num); intr_matrix_set(xPortGetCoreID(), ETS_GPIO_INTR_SOURCE, gpio_intr_num);
xt_set_interrupt_handler(gpio_intr_num, fn, arg); xt_set_interrupt_handler(gpio_intr_num, fn, arg);
ESP_INTR_ENABLE(gpio_intr_num); ESP_INTR_ENABLE(gpio_intr_num);
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK; return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }
/*only level interrupt can be used for wake-up function*/ /*only level interrupt can be used for wake-up function*/
esp_err_t gpio_pin_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type) esp_err_t gpio_pin_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
{ {
if(gpio_mutex != NULL) { if(!is_valid_gpio(gpio_num))
return ESP_ERR_INVALID_ARG;
esp_err_t ret = ESP_OK; esp_err_t ret = ESP_OK;
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY);
if((intr_type == GPIO_PIN_INTR_LOW_LEVEL) || (intr_type == GPIO_PIN_INTR_HIGH_LEVEL)) { if((intr_type == GPIO_PIN_INTR_LOW_LEVEL) || (intr_type == GPIO_PIN_INTR_HIGH_LEVEL)) {
GPIO.pin[gpio_num].int_type = intr_type; GPIO.pin[gpio_num].int_type = intr_type;
GPIO.pin[gpio_num].wakeup_enable = 0x1; GPIO.pin[gpio_num].wakeup_enable = 0x1;
@ -434,24 +344,13 @@ esp_err_t gpio_pin_wakeup_enable(gpio_num_t gpio_num, gpio_int_type_t intr_type)
GPIO_ERROR("GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u\n",gpio_num); GPIO_ERROR("GPIO wakeup only support Level mode,but edge mode set. gpio_num:%u\n",gpio_num);
ret = ESP_ERR_INVALID_ARG; ret = ESP_ERR_INVALID_ARG;
} }
xSemaphoreGiveRecursive(gpio_mutex);
return ret; return ret;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }
esp_err_t gpio_pin_wakeup_disable(gpio_num_t gpio_num) esp_err_t gpio_pin_wakeup_disable(gpio_num_t gpio_num)
{ {
if(gpio_mutex != NULL) { if(!is_valid_gpio(gpio_num))
xSemaphoreTakeRecursive(gpio_mutex, portMAX_DELAY); return ESP_ERR_INVALID_ARG;
GPIO.pin[gpio_num].wakeup_enable = 0; GPIO.pin[gpio_num].wakeup_enable = 0;
xSemaphoreGiveRecursive(gpio_mutex);
return ESP_OK; return ESP_OK;
} else {
GPIO_ERROR("Mutex null\n");
return ESP_FAIL;
}
} }

View file

@ -26,8 +26,6 @@
extern "C" { extern "C" {
#endif #endif
extern const uint32_t GPIO_PIN_MUX_REG[40];
#define GPIO_SEL_0 (BIT(0)) /* Pin 0 selected */ #define GPIO_SEL_0 (BIT(0)) /* Pin 0 selected */
#define GPIO_SEL_1 (BIT(1)) /* Pin 1 selected */ #define GPIO_SEL_1 (BIT(1)) /* Pin 1 selected */
#define GPIO_SEL_2 (BIT(2)) /* Pin 2 selected */ #define GPIO_SEL_2 (BIT(2)) /* Pin 2 selected */
@ -115,6 +113,12 @@ extern const uint32_t GPIO_PIN_MUX_REG[40];
#define GPIO_ID_PIN(n) (GPIO_ID_PIN0 + (n)) #define GPIO_ID_PIN(n) (GPIO_ID_PIN0 + (n))
#define GPIO_PIN_ADDR(i) (GPIO_PIN0_REG + i * 4) #define GPIO_PIN_ADDR(i) (GPIO_PIN0_REG + i * 4)
#define GPIO_MODE_DEF_INPUT (BIT0)
#define GPIO_MODE_DEF_OUTPUT (BIT1)
#define GPIO_MODE_DEF_OD (BIT2)
extern const uint32_t GPIO_PIN_MUX_REG[GPIO_PIN_COUNT];
typedef enum { typedef enum {
GPIO_NUM_0 = 0, GPIO_NUM_0 = 0,
GPIO_NUM_1 = 1, GPIO_NUM_1 = 1,
@ -166,11 +170,11 @@ typedef enum {
} gpio_int_type_t; } gpio_int_type_t;
typedef enum { typedef enum {
GPIO_MODE_INPUT = (BIT0), /* GPIO mode : input only */ GPIO_MODE_INPUT = GPIO_MODE_DEF_INPUT, /* GPIO mode : input only */
GPIO_MODE_OUTPUT = (BIT1), /* GPIO mode : output only mode */ GPIO_MODE_OUTPUT = GPIO_MODE_DEF_OUTPUT, /* GPIO mode : output only mode */
GPIO_MODE_OUTPUT_OD = ((BIT1)|(BIT2)), /* GPIO mode : output only with open-drain mode */ GPIO_MODE_OUTPUT_OD = ((GPIO_MODE_DEF_OUTPUT)|(GPIO_MODE_DEF_OD)), /* GPIO mode : output only with open-drain mode */
GPIO_MODE_INPUT_OUTPUT_OD = ((BIT0)|(BIT1)|(BIT2)), /* GPIO mode : output and input with open-drain mode*/ GPIO_MODE_INPUT_OUTPUT_OD = ((GPIO_MODE_DEF_INPUT)|(GPIO_MODE_DEF_OUTPUT)|(GPIO_MODE_DEF_OD)), /* GPIO mode : output and input with open-drain mode*/
GPIO_MODE_INPUT_OUTPUT = ((BIT0)|(BIT1)), /* GPIO mode : output and input mode */ GPIO_MODE_INPUT_OUTPUT = ((GPIO_MODE_DEF_INPUT)|(GPIO_MODE_DEF_OUTPUT)), /* GPIO mode : output and input mode */
} gpio_mode_t; } gpio_mode_t;
typedef enum { typedef enum {
@ -191,13 +195,6 @@ typedef struct {
gpio_int_type_t intr_type; /* GPIO interrupt type */ gpio_int_type_t intr_type; /* GPIO interrupt type */
} gpio_config_t; } gpio_config_t;
typedef enum {
GPIO_DIR_OUTPUT_ONLY, /* GPIO output */
GPIO_DIR_INPUT_ONLY, /* GPIO input */
GPIO_DIR_INPUT_AND_OUTPUT, /* GPIO input + output */
GPIO_DIR_DISABLE_IO, /* GPIO disable */
} gpio_direction_t;
typedef enum { typedef enum {
GPIO_LOW_LEVEL = 0, GPIO_LOW_LEVEL = 0,
GPIO_HIGH_LEVEL = 1, GPIO_HIGH_LEVEL = 1,
@ -242,12 +239,50 @@ typedef void (*gpio_event_callback)(gpio_num_t gpio_intr_num);
* pGPIOConfig.pull_down_en : Enable or Disable pull-down * pGPIOConfig.pull_down_en : Enable or Disable pull-down
* pGPIOConfig.intr_type : Configure GPIO interrupt trigger type * pGPIOConfig.intr_type : Configure GPIO interrupt trigger type
* @return ESP_OK: success ; * @return ESP_OK: success ;
* ESP_ERR_INVALID_ARG: parameters error * ESP_ERR_INVALID_ARG: parameter error
* ESP_FAIL : GPIO mutex error * ESP_FAIL : GPIO error
* *
*/ */
esp_err_t gpio_config(gpio_config_t *pGPIOConfig); esp_err_t gpio_config(gpio_config_t *pGPIOConfig);
/**
* @brief GPIO set interrupt trigger type
*
* @parameter[in] gpio_num : GPIO number.
* If you want to set output level of GPIO16, gpio_num should be GPIO_NUM_16 (16);
* @parameter[in] intr_type: interrupt type, select from gpio_int_type_t
*
* @return ESP_OK : success
* ESP_ERR_INVALID_ARG: parameter error
*
*/
esp_err_t gpio_set_intr_type(gpio_num_t gpio_num, gpio_int_type_t intr_type);
/**
* @brief enable GPIO module interrupt signal
*
* @parameter[in] gpio_num : GPIO number.
* If you want to set output level of GPIO16, gpio_num should be GPIO_NUM_16 (16);
*
* @return ESP_OK : success
* ESP_ERR_INVALID_ARG: parameter error
*
*/
esp_err_t gpio_intr_enable(gpio_num_t gpio_num);
/**
* @brief disable GPIO module interrupt signal
*
* @parameter[in] gpio_num : GPIO number.
* If you want to set output level of GPIO16, gpio_num should be GPIO_NUM_16 (16);
*
* @return ESP_OK : success
* ESP_ERR_INVALID_ARG: parameter error
*
*/
esp_err_t gpio_intr_disable(gpio_num_t gpio_num);
/** /**
* @brief GPIO set output level * @brief GPIO set output level
* *
@ -256,7 +291,7 @@ esp_err_t gpio_config(gpio_config_t *pGPIOConfig);
* @parameter[in] level : Output level. 0: low ; 1: high * @parameter[in] level : Output level. 0: low ; 1: high
* *
* @return ESP_OK : success * @return ESP_OK : success
* ESP_FAIL : GPIO mutex error * ESP_FAIL : GPIO error
* *
*/ */
esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level); esp_err_t gpio_set_level(gpio_num_t gpio_num, uint32_t level);
@ -278,29 +313,29 @@ int gpio_get_level(gpio_num_t gpio_num);
* *
* Configure GPIO direction,such as output_only,input_only,output_and_input * Configure GPIO direction,such as output_only,input_only,output_and_input
* *
* @parameter[in] gpio_num : Configure GPIO pins number,it should GPIO number. * @parameter[in] gpio_num : Configure GPIO pins number,it should be GPIO number.
* If you want to set direction of GPIO16, gpio_num should be GPIO_NUM_16 (16); * If you want to set direction of GPIO16, gpio_num should be GPIO_NUM_16 (16);
* @parameter[in] direction: Configure GPIO direction,such as output_only,input_only,... * @parameter[in] mode : Configure GPIO direction,such as output_only,input_only,...
* *
* @return ESP_OK : success * @return ESP_OK : success
* ESP_ERR_INVALID_ARG : fail * ESP_ERR_INVALID_ARG : fail
* ESP_FAIL : GPIO mutex error * ESP_FAIL : GPIO error
* *
*/ */
esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_direction_t direction); esp_err_t gpio_set_direction(gpio_num_t gpio_num, gpio_mode_t mode);
/** /**
* @brief GPIO set pull * @brief GPIO set pull
* *
* User this Function,configure GPIO pull mode,such as pull-up,pull-down * User this Function,configure GPIO pull mode,such as pull-up,pull-down
* *
* @parameter[in] gpio_num : Configure GPIO pins number,it should GPIO number. * @parameter[in] gpio_num : Configure GPIO pins number,it should be GPIO number.
* If you want to set pull up or down mode for GPIO16,gpio_num should be GPIO_NUM_16 (16); * If you want to set pull up or down mode for GPIO16,gpio_num should be GPIO_NUM_16 (16);
* @parameter[in] pull : Configure GPIO pull up/down mode,such as pullup_only,pulldown_only,pullup_and_pulldown,... * @parameter[in] pull : Configure GPIO pull up/down mode,such as pullup_only,pulldown_only,pullup_and_pulldown,...
* *
* @return ESP_OK : success * @return ESP_OK : success
* ESP_ERR_INVALID_ARG : fail * ESP_ERR_INVALID_ARG : fail
* ESP_FAIL : GPIO mutex error * ESP_FAIL : GPIO error
* *
*/ */
esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull); esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull);
@ -313,7 +348,7 @@ esp_err_t gpio_set_pull_mode(gpio_num_t gpio_num, gpio_pull_mode_t pull);
* Second , enable the GPIO input,if you use pin_func_as_gpio,you can use gpio_set_direction function * Second , enable the GPIO input,if you use pin_func_as_gpio,you can use gpio_set_direction function
* Third , call gpio_matrix_in function * Third , call gpio_matrix_in function
* *
* @parameter[in] GPIO : Configure GPIO pins number,it should GPIO number. * @parameter[in] GPIO : Configure GPIO pins number,it should be GPIO number.
* If you want to configure GPIO16, gpio_num should be GPIO_NUM_16 (16); * If you want to configure GPIO16, gpio_num should be GPIO_NUM_16 (16);
* @parameter[in] signal_idx : the signal_idx,find the signal index from gpio_sig_map.h * @parameter[in] signal_idx : the signal_idx,find the signal index from gpio_sig_map.h
* *
@ -351,19 +386,30 @@ void gpio_matrix_out(uint32_t GPIO, uint32_t signal_idx, bool out_inv, bool oen_
* Users should know that which CPU is running and then pick a INUM that is not used by system. * Users should know that which CPU is running and then pick a INUM that is not used by system.
* We can find the information of INUM and interrupt level in soc.h. * We can find the information of INUM and interrupt level in soc.h.
* TODO: to move INUM options to menu_config * TODO: to move INUM options to menu_config
* @parameter uint8_t gpio_intr_num : GPIO interrupt number,check the info in soc.h, and please see the core-isa.h for more details * @parameter uint32_t gpio_intr_num : GPIO interrupt number,check the info in soc.h, and please see the core-isa.h for more details
* @parameter void (* fn)(void* ) : interrupt handler function. * @parameter void (* fn)(void* ) : interrupt handler function.
* Note that the handler function MUST be defined with attribution of "IRAM_ATTR". * Note that the handler function MUST be defined with attribution of "IRAM_ATTR".
* @parameter void * arg : parameter for handler function * @parameter void * arg : parameter for handler function
* *
* @return ESP_OK : success ; * @return ESP_OK : success ;
* ESP_FAIL: gpio_mutex error * ESP_FAIL: gpio_ error
*/ */
esp_err_t gpio_intr_handler_register(uint8_t gpio_intr_num, void (*fn)(void*), void * arg); esp_err_t gpio_intr_handler_register(uint32_t gpio_intr_num, void (*fn)(void*), void * arg);
/**
* *************** ATTENTION ********************/
/**
*
* Each GPIO have their separated registers, so we don't have to use
* lock for multi-task issues.
* Please make sure that there would be no such situation, in which,
* different tasks read-then-write the same GPIO register.
*/
/*----------EXAMPLE TO CONIFGURE GPIO AS OUTPUT ------------ */ /*----------EXAMPLE TO CONIFGURE GPIO AS OUTPUT ------------ */
/* gpio_config_t io_conf; /* gpio_config_t io_conf;
* io_conf.intr_type = GPIO_PIN_INTR_DISABLE; //disable interrupt * io_conf.intr_type = GPIO_PIN_INTR_DISABLE; //disable interrupt
@ -425,8 +471,6 @@ esp_err_t gpio_intr_handler_register(uint8_t gpio_intr_num, void (*fn)(void*), v
* *
*/ */
/** /**
* @} * @}
*/ */