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

Usermod settings SCL and SDA not showing platformio values #2931

Closed
1 task done
ewoudwijma opened this issue Dec 7, 2022 · 10 comments
Closed
1 task done

Usermod settings SCL and SDA not showing platformio values #2931

ewoudwijma opened this issue Dec 7, 2022 · 10 comments
Labels
workaround The issue contains a workaround

Comments

@ewoudwijma
Copy link
Contributor

What happened?

Usermod settings do not show values for build flags HW_PIN_SCL and HW_PIN_SDA on new install. Show -1 instead:

image

To Reproduce Bug

erase flash: pio run --target erase

Set in platformio environment xyz
-D HW_PIN_SCL=22 -D HW_PIN_SDA=21

build xyz

go to settings / usermod

Expected Behavior

image

Install Method

Self-Compiled

What version of WLED?

0.14.0 latest

Which microcontroller/board are you seeing the problem on?

ESP32

Relevant log/trace output

No response

Anything else?

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@ewoudwijma ewoudwijma added the bug label Dec 7, 2022
@ewoudwijma
Copy link
Contributor Author

Same applies for

image

(HW_PIN_DATASPI, HW_PIN_MISOSPI, HW_PIN_CLOCKSPI)

@blazoncek
Copy link
Collaborator

They do, as a placeholder (remove -1 or any other input by deleting digits). This is intentional as to not allocate GPIO during boot.
HW_PIN_... macros are just informational which GPIO is reserved (if used) for hardware accelerated IO.

Not a bug but intentional behaviour.

@blazoncek blazoncek added workaround The issue contains a workaround and removed bug labels Dec 7, 2022
@ewoudwijma
Copy link
Contributor Author

ewoudwijma commented Dec 7, 2022

I saw the placeholder functionality. It's info only, not assigned to anything

I would say setting hw_pin_sda/scl (or not setting) determines if gpio should be allocated or not.
Might be confusing for end users as build flags normally are shown as default in the ui.

We are currently reviewing the sda/scl functionality. After review will let you know our advice.

@blazoncek
Copy link
Collaborator

I would say setting hw_pin_sda/scl (or not setting) determines if gpio should be allocated or not. Might be confusing for end users as build flags normally are shown as default in the ui.

Well, it is not intended in the way you think. Confusing or not these setting are meant for advanced users who know how are they compiling and for what.
If you want to pre-allocate I2C and/or SPI GPIO pins, do so by introducing new compile time options. These may have nothing in common with HW_PIN_...

We are currently reviewing the sda/scl functionality. After review will let you know our advice.

I wish you luck. I stumbled to a roadblock when trying to use 2 I2C devices on the same HW pins. Looks like a bug in Espressif/Arduino implementation. Since I2C is not used for LEDs and global SPI may conflict if using APS102 they are secondary in priority so I dropped any further development.

@ewoudwijma
Copy link
Contributor Author

ewoudwijma commented Dec 8, 2022

Well, it is not intended in the way you think. Confusing or not these setting are meant for advanced users who know how are they compiling and for what.

Is it documented anywhere? Discord links also okay.
Any names of advanced users to check ?

Not to criticize your approach but get a general understanding how it is supposed to be used in different scenarios, improve documentation if needed and also test together multiple i2c devices

@ewoudwijma
Copy link
Contributor Author

ewoudwijma commented Dec 8, 2022

I2C SPI Scrapbook:

PART1:

highest level declarations are in pins_arduino.h:

image

If no HW_PIN values set in platformio and above variables commented out we see where these vars are used due to errors:

SCK and MOSI

NeoPixelBus/src/internal/*DotStarGenericMethod.h
(T_TWOWIRE)

image

NeoPixelBus/src/internal/DotStarEsp32DmaSpiMethod.h

image

const.h

image

image

pinmanager.cpp

image

.platformio/packages/framework-arduinoespressif32@src-e9b1fbd6563a55e19ddae15e1fc09589/libraries/SPI/src/SPI.cpp

image

.platformio/packages/framework-arduinoespressif32@src-e9b1fbd6563a55e19ddae15e1fc09589/libraries/Wire/src/Wire.cpp

image

U8g2/src/U8x8lib.cpp

image

image

libraries/SD/src/SD.h

image

Conclusion from above:

  • SCK, MOSI, MISO and SS is used in 'system' libraries and not in WLED code for SPI and have fixed values 18, 23, 19 and 5 respectively (in SPI.cpp 12,13,14,15 used...? only if -1...)
  • SCL and SDA is used in Wire.cpp (therefore wire.cpp will only use pins 22 and 21 ?) and in const.h to set HW_PIN_SCL and HW_PIN_SDA if they do not get a value in platformio, not used anywhere else in code
  • HW_PIN_SCL and HW_PIN_SDA is used in WLED code...

PART 2

wled.h defines global variables which are used to set and get persistent values

image

platformio has currently only one entry where standard values for an environment are defined (lolin_s2_mini):

image

SPI values (HW_PIN_CLOCKSPI, HW_PIN_DATASPI, HW_PIN_MISOSPI) used in usermod_v2_four_line_display_ALT.h in case the global variables are -1

setup()
image
image

addToConfig()
image

readFromConfig()
image

and used in pin_manager.cpp and xml.cpp for display purposes
in set.cpp to overwrite in case of esp8266 (cannot change pins on 8266)
but they are also undeffed in const.h for esp8266???

SPI values (HW_PIN_SCL, HW_PIN_SDA) used in above SPI cases in case the global variables are -1

    • more platformio entries (esp32_4MB_V4_max_base.build_flags, wemos_shield_esp32_4MB_max_base, esp32_pico_4MB_max)
    • USERMOD_BH1750 (redefined!!! and fixed to 21,22)
    • usermod_mpu6050_imu.h: in case global var is -1
    • also display purposes and unset 8266 as for spi

U8X8_PIN_I2C_DATA and U8X8_PIN_I2C_DATA

u8x8.h
image

U8x8lib.cpp
image
image

Conclusions:

  • global variables are initialised with -1
  • HW_PIN values are used in case global vars are initialised with -1 (only visible as placeholder text in ui)
  • if HW_PIN values are not set in platformio, the values of pinsarduino.h are used
  • u8x8 12 en 13?
  • =>
  • Wire and usermod_bh1750.h are always using 21,22 for i2c
  • usermod_mpu6050_imu.h and usermod_v2_four_line_display_ALT.h use pinsarduino values unless redefined in platformio

Part 3

Use of FLD_PIN_* values in usermod_v2_four_line_display_ALT.h
setup of ion array using FLD_PIN_ and if -1 use global variables and if they are -1 use HW_PIN values

Conclusion:

  • complicated
  • Not sure if gpio is not allocated in case HW_PIN is not defined: global defaults are assumed... Therefore not sure if the -1 system is needed

Part 4

  • u8x8 has its own pin values...

image

* Wire.begin() assume -1 and internally assign SDA (21) and SCL (22) in that case

Screenshot 2022-12-09 at 17 55 23

* SPI.begin() assume -1 and internally assignSCK (14), MISO(12), MOSI(13) and SS(15) in that case

image

Proposal

  • Find out if we can only use HW_PIN* and all usermods use the global vars (instead if the if <0 formula)
  • The global vars are initialised by HW_PIN if defined, otherwise pinsarduino values are used
  • Normal way of dealing with platformio values are used, not placemarkers
  • Check if we can deviate anyway from pinsarduino values as wire and spi and Wire 'system' software is assuming these values

... to be continued ...

@blazoncek
Copy link
Collaborator

blazoncek commented Dec 8, 2022

Search in #dev-general for the "I2C" keyword on Discord.
There was no documentation but a lot of discussion. Happened somewhere in September.

The advanced users in question were @softhack007 and @Matoran ( #2740 )

As for documentation: If you want clarifications or complaint about missing information/documentation open different kind of issue. Or ask on Discord.

As noted above the implementation of shareable I2C and SPI failed and since it is not crucial for workings of WLED, further development stopped beyond the entry fields in usermod settings. Which work as intended.
As shareable pins are exclusively used by usermods (except SPI which in case of APA102 cannot be shared ATM) it is up to usermods to implement proper use of I2C and SPI buses. They do get the information about which GPIO ports are intended as HW accelerated via global variables. HW_PIN_... constants are informational and only meant for intended override of platform default values. You do not need to define them to have HW accelerated I2C or SPI.

I do admit that this information (how & why) needs to be added somewhere. Perhaps in WLED-docs by someone that has time and will to do that and has played with usermods that use them.

In current implementation you have global variables i2c_sda, i2c_scl, spi_mosi, spi_miso and spi_sclk which are filled at boot with values entered in usermod settings page. Their intention is to represent HW accelerated GPIO pins. If they are set to anything other than -1 the appropriate interface is initialised (assuming no other code did that prior) with Wire.begin() and SPI.begin(). In such case pins are also pre-allocated to prevent unintended use in other patrs of WLED. They do not do anything beyond that.
Usermods that use I2C or SPI and use the same GPIO as global pre-allocated ones do not need to call Wire.begin() or SPI.begin() as it has been done at the boot time.

@softhack007
Copy link
Collaborator

softhack007 commented Dec 8, 2022

Hi, thanks @blazoncek and @ewoudwijma, this explains a lot how WLED behaves today.

Proposal: give me some time to digest & numbercrunch this in my head. I'll need liquid cooling (beer) for this, so can do it on the weekend. Then i'll make a proposal in discord (SR-to-WLED team), about how I2C sharing could/should behave (kind of one-line requirements) including user perspective. We discuss in discord, then I try to improve the current implementation.
Agreed?

@softhack007
Copy link
Collaborator

SPI sharing seems to be a different beast:

  • NeoPixelBus DotStar (and maybe other) use SPI pins
  • SD_card used SPI
  • There is vspi and hspi - sometimes need to use one but not the other
  • SPI sharing works by using the same MISO/MOSI/CLK, but usually a dedicates "SS" (chip select) signal per device is needed.

So we could first try to improve I2C only, and then see what can be done for SPI.

@blazoncek
Copy link
Collaborator

resolved by #2955

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

No branches or pull requests

3 participants