gpio_config_t .mode not applying?

Lancsrick
Posts: 34
Joined: Mon Apr 10, 2023 5:48 pm

gpio_config_t .mode not applying?

Postby Lancsrick » Tue Jan 14, 2025 5:06 pm

Summary: Appears that despite the

Code: Select all

gpio_config_t
structure being accepted and actioned in the code, I cannot actually output via a GPIO pin without using

Code: Select all

gpio_set_direction
in the body of my code. See below. Intended (undocumented) behaviour, or bug?

Code: Select all

gpio_config_t io_conf5;
				io_conf5.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf5.mode = GPIO_MODE_OUTPUT;
				io_conf5.pin_bit_mask = (1ULL<<5);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf5.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf5.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error5=gpio_config(&io_conf5);//configure GPIO with the given settings
				if(error5!=ESP_OK){
					printf("error configuring GPIO5 \n");
				}
			   #define LIGHT_AMBER1 GPIO_NUM_5

Code: Select all

//below is an extract from the while loop in app_main


gpio_set_direction(LIGHT_AMBER1,GPIO_MODE_OUTPUT); //if this line is not included then output is permanently high. Config block above must be recognised as the LIGHT_AMBER1 alias is correctly handled.

gpio_set_level(LIGHT_AMBER1, 1);

printf("A1 light on\n");
vTaskDelay(pdMS_TO_TICKS(3000));

gpio_set_level(LIGHT_AMBER1, 0);
printf("A1 light off\n");
vTaskDelay(pdMS_TO_TICKS(3000));

MicroController
Posts: 1954
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: gpio_config_t .mode not applying?

Postby MicroController » Tue Jan 14, 2025 6:47 pm

Intended (undocumented) behaviour, or bug?
Bug in your code. Check
https://github.com/espressif/esp-idf/bl ... ple_main.c

Code: Select all

Config block above must be recognised as the LIGHT_AMBER1 alias is correctly handled.
Makes me suspect that either a) gpio_config(&io_conf5) was never executed before you try to output to pin LIGHT_AMBER1, or b) the pin was reconfigured in the meantime, or possibly c) LIGHT_AMBER1 was defined to another value somewhere.

Lancsrick
Posts: 34
Joined: Mon Apr 10, 2023 5:48 pm

Re: gpio_config_t .mode not applying?

Postby Lancsrick » Tue Jan 14, 2025 6:52 pm

I can check again but given the addition of a gpio_set_direction() then works it must be executing the first code block to understand what AMBER_LIGHT1 is. Because it works fine with just the addition of that direction call I'll triple (quadruple?) check I have no conflicting definition for GPIO5 anywhere else but I haven't seen anything so far.

MicroController
Posts: 1954
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: gpio_config_t .mode not applying?

Postby MicroController » Tue Jan 14, 2025 11:18 pm

Lancsrick wrote:
Tue Jan 14, 2025 6:52 pm
it must be executing the first code block to understand what AMBER_LIGHT1 is.
No. For AMBER_LIGHT1 to be a valid expression all that's needed is a line with a corresponding #define somewhere in the compilation unit before the point of use.

Lancsrick
Posts: 34
Joined: Mon Apr 10, 2023 5:48 pm

Re: gpio_config_t .mode not applying?

Postby Lancsrick » Wed Jan 15, 2025 9:13 am

I've commented out the majority of my code but still get the same behaviour - permanent high without the gpio_set_direction(), working as intended with. Copy paste of my code below.

Code: Select all

#include <inttypes.h>
#include <stdio.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <sys/time.h>
//#include <hd44780.h>
#include <pcf8574.h>
#include <string.h>
#include <esp_idf_version.h>
#include <dirent.h>
#include "driver/gpio.h"
#include "freertos/queue.h"
#include "esp_timer.h"
#include <max7219.h>
#include <math.h>
#define LOG_LOCAL_LEVEL ESP_LOG_VERBOSE
#include "esp_log.h"
#ifndef APP_CPU_NUM
#define APP_CPU_NUM PRO_CPU_NUM
#endif

#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0)
#define HOST    HSPI_HOST
#else
#define HOST    SPI2_HOST
#endif

//////////////////////////////////////////////////////////////////////////////////////////////////////////////
//GLOBAL VARIABLES START

QueueHandle_t red_handler_queue;
QueueHandle_t blue_handler_queue;
QueueHandle_t red_box_queue;
QueueHandle_t blue_box_queue;

uint8_t expander_value;
uint8_t stop_flag;
char state[5];

int queue_time_rh;
int queue_time_bh;
int queue_time_rb;
int queue_time_bb;

int start_button_status;

uint64_t race_start_time = 5000000; //set the initial value to 5 seconds, means when we check in the interrupt later compare to race_start_time it will automatically flag an early start if it hasn't been updated with an accurate value

static i2c_dev_t pcf8574_expander;

//GLOBAL VARIABLES END

Code: Select all

void pin_setup()
{
/////////////////////////////////////////////////////////////////////

//Pin definition on ESP DOIT Devkit V1 and pin config

//D1/TXD0 - Red lane Dog 1 Fault button TX2
	gpio_config_t io_conf1;
				io_conf1.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf1.mode = GPIO_MODE_INPUT;
				io_conf1.pin_bit_mask = (1ULL<<1);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf1.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf1.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error1=gpio_config(&io_conf1);//configure GPIO with the given settings
				if(error1!=ESP_OK){
					printf("error configuring GPIO1 \n");
				}
			   #define BUTTON_RED_DOG1 GPIO_NUM_1
//D2 - Red lane Dog 2 Fault button RX2
	gpio_config_t io_conf2;
				io_conf2.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf2.mode = GPIO_MODE_INPUT;
				io_conf2.pin_bit_mask = (1ULL<<2);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf2.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf2.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error2=gpio_config(&io_conf2);//configure GPIO with the given settings
				if(error2!=ESP_OK){
					printf("error configuring GPIO2 \n");
				}
			   #define BUTTON_RED_DOG2 GPIO_NUM_2
//D3/RXD0 - Red lane Dog 3 Fault button
	gpio_config_t io_conf3;
				io_conf3.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf3.mode = GPIO_MODE_INPUT;
				io_conf3.pin_bit_mask = (1ULL<<3);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf3.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf3.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error3=gpio_config(&io_conf3);//configure GPIO with the given settings
				if(error3!=ESP_OK){
					printf("error configuring GPIO3 \n");
				}
			   #define BUTTON_RED_DOG3 GPIO_NUM_3
//D4 - Red lane Dog 4 Fault button
	gpio_config_t io_conf4;
				io_conf4.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf4.mode = GPIO_MODE_INPUT;
				io_conf4.pin_bit_mask = (1ULL<<4);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf4.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf4.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error4=gpio_config(&io_conf4);//configure GPIO with the given settings
				if(error4!=ESP_OK){
					printf("error configuring GPIO4 \n");
				}
			   #define BUTTON_RED_DOG4 GPIO_NUM_4
//D5 - Amber start light 1
	gpio_config_t io_conf5;
				io_conf5.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf5.mode = GPIO_MODE_OUTPUT;
				io_conf5.pin_bit_mask = (1ULL<<5);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf5.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf5.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error5=gpio_config(&io_conf5);//configure GPIO with the given settings
				if(error5!=ESP_OK){
					printf("error configuring GPIO5 \n");
				}
			   #define LIGHT_AMBER1 GPIO_NUM_5
//D12 - Amber start light 2
	gpio_config_t io_conf12;
					io_conf12.intr_type = GPIO_INTR_DISABLE;//disable interrupt
					io_conf12.mode = GPIO_MODE_OUTPUT;
					io_conf12.pin_bit_mask = (1ULL<<12);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf12.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
					io_conf12.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
					esp_err_t error12=gpio_config(&io_conf12);//configure GPIO with the given settings
					if(error12!=ESP_OK){
						printf("error configuring GPIO12 \n");
					}
				   #define LIGHT_AMBER2 GPIO_NUM_12
//D13 - Amber start light 3
	gpio_config_t io_conf13;
						io_conf13.intr_type = GPIO_INTR_DISABLE;//disable interrupt
						io_conf13.mode = GPIO_MODE_OUTPUT;
						io_conf13.pin_bit_mask = (1ULL<<13);//bit mask of the pins that you want to set,e.g. GPIO 1
						io_conf13.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
						io_conf13.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
						esp_err_t error13=gpio_config(&io_conf13);//configure GPIO with the given settings
						if(error13!=ESP_OK){
							printf("error configuring GPIO13 \n");
						}
					   #define LIGHT_AMBER3 GPIO_NUM_13
//D14 - Green start light
	gpio_config_t io_conf14;
						io_conf14.intr_type = GPIO_INTR_DISABLE;//disable interrupt
						io_conf14.mode = GPIO_MODE_OUTPUT;
						io_conf14.pin_bit_mask = (1ULL<<14);//bit mask of the pins that you want to set,e.g. GPIO 1
						io_conf14.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
						io_conf14.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
						esp_err_t error14=gpio_config(&io_conf14);//configure GPIO with the given settings
						if(error14!=ESP_OK){
							printf("error configuring GPIO14 \n");
						}
					   #define LIGHT_GREENSTART GPIO_NUM_14
//D15 - CS Pin for MAX7219 interface
//D16/RXD2 - 2min timer button
	gpio_config_t io_conf16;
			io_conf16.intr_type = GPIO_INTR_DISABLE;//disable interrupt
			io_conf16.mode = GPIO_MODE_INPUT;
			io_conf16.pin_bit_mask = (1ULL<<16);//bit mask of the pins that you want to set,e.g. GPIO 1
			io_conf16.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
			io_conf16.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
			esp_err_t error16=gpio_config(&io_conf16);//configure GPIO with the given settings
			if(error16!=ESP_OK){
				printf("error configuring GPIO16 \n");
			}
		   #define BUTTON_2MIN GPIO_NUM_16
//D17/TXD2 - START button
		gpio_config_t io_conf17;
				io_conf17.intr_type = GPIO_INTR_DISABLE;//no interrupt
				io_conf17.mode = GPIO_MODE_INPUT;//set as input mode
				io_conf17.pin_bit_mask = (1ULL<<17);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf17.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf17.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error17=gpio_config(&io_conf17);//configure GPIO with the given settings
				if(error17!=ESP_OK){
					printf("error configuring GPIO17 \n");
				}
			   #define BUTTON_START GPIO_NUM_17
//D21 - SDA Pin
//D19 - red fault light flash
		gpio_config_t io_conf19;
				io_conf19.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf19.mode = GPIO_MODE_OUTPUT;
				io_conf19.pin_bit_mask = (1ULL<<19);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf19.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf19.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error19=gpio_config(&io_conf19);//configure GPIO with the given settings
				if(error19!=ESP_OK){
					printf("error configuring GPIO19 \n");
				}
			   #define LIGHT_RED_FAULT GPIO_NUM_19
//D18 - CLK
//D22 - SDL
//D23 - DIN to MAX7219


//D25 - Blue lane Dog 3 Fault button
		gpio_config_t io_conf25;
				io_conf25.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf25.mode = GPIO_MODE_INPUT;
				io_conf25.pin_bit_mask = (1ULL<<25);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf25.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf25.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error25=gpio_config(&io_conf25);//configure GPIO with the given settings
				if(error25!=ESP_OK){
					printf("error configuring GPIO25 \n");
				}
			   #define BUTTON_BLUE_DOG3 GPIO_NUM_25
//D26 - Blue lane Dog 4 Fault button
		gpio_config_t io_conf26;
				io_conf26.intr_type = GPIO_INTR_DISABLE;//disable interrupt
				io_conf26.mode = GPIO_MODE_INPUT;
				io_conf26.pin_bit_mask = (1ULL<<26);//bit mask of the pins that you want to set,e.g. GPIO 1
				io_conf26.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
				io_conf26.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
				esp_err_t error26=gpio_config(&io_conf26);//configure GPIO with the given settingsw
				if(error26!=ESP_OK){
					printf("error configuring GPIO26 \n");
				}
			   #define BUTTON_BLUE_DOG4 GPIO_NUM_26
//D27 - Blue lane Fault light (white flash)
		 gpio_config_t io_conf27;
					io_conf27.intr_type = GPIO_INTR_DISABLE;//disable interrupt
					io_conf27.mode = GPIO_MODE_OUTPUT;//set as output mode
					io_conf27.pin_bit_mask = (1ULL<<27);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf27.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-dwwown mode
					io_conf27.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
					esp_err_t error27=gpio_config(&io_conf27);//configure GPIO with the given settings
					if(error27!=ESP_OK){
						printf("error configuring GPIO27 \n");
					}
				   #define LIGHT_BLUE_FAULT GPIO_NUM_27
//D32 -Blue lane Dog 1 Fault button
		gpio_config_t io_conf32;
					io_conf32.intr_type = GPIO_INTR_DISABLE;//disable interrupt
					io_conf32.mode = GPIO_MODE_INPUT;
					io_conf32.pin_bit_mask = (1ULL<<32);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf32.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
					io_conf32.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
					esp_err_t error32=gpio_config(&io_conf32);//configure GPIO with the given settings
					if(error32!=ESP_OK){
						printf("error configuring GPIO32 \n");
					}
				   #define BUTTON_BLUE_DOG1 GPIO_NUM_32
//D33 -Blue lane Dog 2 Fault button
		gpio_config_t io_conf33;
					io_conf33.intr_type = GPIO_INTR_DISABLE;//disable interrupt
					io_conf33.mode = GPIO_MODE_INPUT;
					io_conf33.pin_bit_mask = (1ULL<<33);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf33.pull_down_en = GPIO_PULLDOWN_ENABLE;//enable pull-down mode
					io_conf33.pull_up_en = GPIO_PULLUP_DISABLE;//disable pull-up mode
					esp_err_t error33=gpio_config(&io_conf33);//configure GPIO with the given settings
					if(error33!=ESP_OK){
						printf("error configuring GPIO33 \n");
					}
				   #define BUTTON_BLUE_DOG2 GPIO_NUM_33
//D34 - Red Handler Sensor
		 gpio_config_t io_conf34;
					io_conf34.intr_type = GPIO_INTR_NEGEDGE;//falling edge interrupt
					io_conf34.mode = GPIO_MODE_INPUT;//set as output mode
					io_conf34.pin_bit_mask = (1ULL<<34);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf34.pull_down_en = GPIO_PULLDOWN_DISABLE;//disable pull-down mode
					io_conf34.pull_up_en = GPIO_PULLUP_ENABLE;//enable pull-up mode
					esp_err_t error34=gpio_config(&io_conf34);//configure GPIO with the given settings
					if(error34!=ESP_OK){
						printf("error configuring GPIO34 \n");
					}
				   #define SENSOR_RED_HANDLER GPIO_NUM_34
//D35 - Red Box Sensor
		 gpio_config_t io_conf35;
					io_conf35.intr_type = GPIO_INTR_NEGEDGE;//falling edge interrupt
					io_conf35.mode = GPIO_MODE_INPUT;//set as output mode
					io_conf35.pin_bit_mask = (1ULL<<35);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf35.pull_down_en = GPIO_PULLDOWN_DISABLE;//disable pull-down mode
					io_conf35.pull_up_en = GPIO_PULLUP_ENABLE;//enable pull-up mode
					esp_err_t error35=gpio_config(&io_conf35);//configure GPIO with the given settings
					if(error35!=ESP_OK){
						printf("error configuring GPIO35 \n");
					}
				   #define SENSOR_RED_BOX GPIO_NUM_35
//D36/VP - Blue Handler Sensor
		 gpio_config_t io_conf36;
					io_conf36.intr_type = GPIO_INTR_NEGEDGE;//falling edge interrupt
					io_conf36.mode = GPIO_MODE_INPUT;//set as output mode
					io_conf36.pin_bit_mask = (1ULL<<36);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf36.pull_down_en = GPIO_PULLDOWN_DISABLE;//disable pull-down mode
					io_conf36.pull_up_en = GPIO_PULLUP_ENABLE;//enable pull-up modew
					esp_err_t error36=gpio_config(&io_conf36);//configure GPIO with the given settings
					if(error36!=ESP_OK){
						printf("error configuring GPIO36 \n");
					}
				   #define SENSOR_BLUE_HANDLER GPIO_NUM_36
//D39/VN - Blue Box Sensor
		 gpio_config_t io_conf39;
					io_conf39.intr_type = GPIO_INTR_NEGEDGE;//falling edge interrupt
					io_conf39.mode = GPIO_MODE_INPUT;//set as output mode
					io_conf39.pin_bit_mask = (1ULL<<39);//bit mask of the pins that you want to set,e.g. GPIO 1
					io_conf39.pull_down_en = GPIO_PULLDOWN_DISABLE;//disable pull-down mode
					io_conf39.pull_up_en = GPIO_PULLUP_ENABLE;//enable pull-up mode
					esp_err_t error39=gpio_config(&io_conf39);//configure GPIO with the given settings
					if(error39!=ESP_OK){
						printf("error configuring GPIO39 \n");
					}
				   #define SENSOR_BLUE_BOX GPIO_NUM_39

}

Code: Select all

void app_main()
{
   printf("about to enter while loop");
 while (1)
    {
    gpio_set_level(LIGHT_AMBER1, 1);

printf("A1 light on\n");
vTaskDelay(pdMS_TO_TICKS(3000));

gpio_set_level(LIGHT_AMBER1, 0);
printf("A1 light off\n");
vTaskDelay(pdMS_TO_TICKS(3000));
    }

}
    

Lancsrick
Posts: 34
Joined: Mon Apr 10, 2023 5:48 pm

Re: gpio_config_t .mode not applying?

Postby Lancsrick » Wed Jan 15, 2025 10:13 am

I'm an idiot, I never called pin_setup() within main. Thanks for driving me to look closer at my code, the fact that the #define was still actioned threw me off. Cheers.

Who is online

Users browsing this forum: No registered users and 68 guests