Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GPIO numbering wrong (GIT8266O-717) #1139

Closed
bertmelis opened this issue Oct 18, 2021 · 4 comments
Closed

GPIO numbering wrong (GIT8266O-717) #1139

bertmelis opened this issue Oct 18, 2021 · 4 comments

Comments

@bertmelis
Copy link

I've installed the latest SDK on Windows 10. Firmware is running on a Wemos D1 Mini.
I tried to run a simple blink but the pin numbering is completely wrong.

With

#define GPIO_OUTPUT_IO GPIO_Pin_4

a LED blinks on D0 which imho should map to GPIO16.
Output is

pin: 16
pin bitmask: 65536

With

#define GPIO_OUTPUT_IO GPIO_Pin_2

a LED blinks on D2 which should map to GPIO4
Output is

pin: 4
pin bitmask: 16

It becomes worse when using GPIO_Pin_5:

C:/msys32/home/Bert/esp/pmsensor02/main/main.c:15:21: warning: conversion from 'long long unsigned int' to 'unsigned int' changes value from '4294967296' to '0' [-Woverflow]
     .pin_bit_mask = (1ULL<<GPIO_OUTPUT_IO),
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/gpio.h"

#define GPIO_OUTPUT_IO GPIO_Pin_4

void blink() {
  gpio_config_t io_conf = {
    .intr_type = GPIO_INTR_DISABLE,
    .mode = GPIO_MODE_OUTPUT,
    .pin_bit_mask = (1ULL<<GPIO_OUTPUT_IO),
    .pull_down_en = 0,
    .pull_up_en = 0,
  };
  gpio_config(&io_conf);

  printf("pin: %lu\n", GPIO_OUTPUT_IO);
  printf("pin bitmask: %u\n", io_conf.pin_bit_mask);

  int cnt = 0;
  while (true) {
    vTaskDelay(1000 / portTICK_RATE_MS);
    gpio_set_level(GPIO_OUTPUT_IO, cnt % 2);
  }
}

TaskHandle_t xHandle = NULL;

void app_main() {
  printf("Starting blink task\n");
  BaseType_t xReturned = xTaskCreate(blink,
                                     "BLINK",
                                     2048,
                                     (void*)1,
                                     1,
                                     &xHandle);
  if (xReturned != pdPASS) {
    printf("Error creating BLINK task\n");
  }
}
@github-actions github-actions bot changed the title GPIO numbering wrong GPIO numbering wrong (GIT8266O-717) Oct 18, 2021
@dsptech
Copy link

dsptech commented Oct 19, 2021

Hi,
D0, D1 ... etc is the Arduino numbering. In this sdk you have to use directly the chip GPIO numbering instead. Google for "Wemos D1 Mini pinout" to see the mapping.

It also should be noted that GPIO_Pin_4 is already a mask :
from gpio.h: #define GPIO_Pin_4 (BIT(4))
from eagle_soc.h #define BIT(nr) (1UL << (nr))

so you must use it "as is".
Example: ".... .pin_bit_mask = GPIO_Pin_4 | GPIO_Pin_5" etc.

You might also be interested in this sdk bug (still open):
#1071

Regards.

@bertmelis
Copy link
Author

bertmelis commented Oct 19, 2021

I know the Dx labels are used in Arduino. But they are on the silkscreen so I defined them, mapping to the GPIO number as per pinout. I excluded this in the given example.

#define D0 GPIO_Pin_16
#define D1 GPIO_Pin_5
...

Now, suppose I attach a led to GPIO13 (or D7):

Starting blink task
E (239) gpio: GPIO_PIN mask error
pin: 16384
pin bitmask: 0
E (1241) gpio: gpio_set_level(188): GPIO number error

PS: It is indeed already a mask, so somebody should change the example:

#define GPIO_OUTPUT_PIN_SEL ((1ULL<<GPIO_OUTPUT_IO_0) | (1ULL<<GPIO_OUTPUT_IO_1))

@bertmelis
Copy link
Author

My bad. Confused GPIO_Pin_x with plain GPIO number

@dsptech
Copy link

dsptech commented Oct 19, 2021

Hi,
the example is right because GPIO_OUTPUT_IO_0 and GPIO_OUTPUT_IO_1 are not masks.

#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))

it is the same of
#define GPIO_OUTPUT_PIN_SEL (GPIO_Pin_15|GPIO_Pin_16)

by the way, I think the the proper definitions of D0 and D1 are:
#define D0 16
#define D1 5

because the gpio functions (other than the configuration) require a "pure" pin number instead.
Regards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants