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

Initial Support for RP2040 platform #3284

Merged
merged 49 commits into from
Oct 20, 2022
Merged

Initial Support for RP2040 platform #3284

merged 49 commits into from
Oct 20, 2022

Conversation

jesserockz
Copy link
Member

@jesserockz jesserockz commented Mar 11, 2022

What does this implement/fix?

Basic support for RP2040

esphome/feature-requests#1924

This code is also waiting on platformio/platform-raspberrypi#36

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Other

Related issue or feature (if applicable): fixes

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#

Test Environment

  • ESP32
  • ESP32 IDF
  • ESP8266
  • RP2040 😏

Example entry for A Raspberry Pi Pico:

esphome:
  name: rpi-pico

rp2040:
  board: rpipico
  framework:
    platform_version: https://github.com/maxgerhardt/platform-raspberrypi.git

logger:

Example entry for A Raspberry Pi Pico W:

esphome:
  name: rpi-pico-w

rp2040:
  board: rpipicow
  framework:
    platform_version: https://github.com/maxgerhardt/platform-raspberrypi.git

wifi:
  ssid: REPLACEME
  password: REPLACEME

logger:
api: 
ota:

Checklist:

  • The code change is tested and works locally.
  • Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

  • Documentation added/updated in esphome-docs.
    Fix preferences crashing

@probot-esphome
Copy link

Hey there @jesserockz,
Thanks for submitting this pull request! Can you add yourself as a codeowner for this integration? This way we can notify you if a bug report for this integration is reported.
In __init__.py of the integration, please add:

CODEOWNERS = ["@jesserockz"]

And run script/build_codeowners.py

(message by NeedsCodeownersLabel)

tries = 0
while tries < 5:
try:
with ser:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Afaik this should call .close() on the serial object if an exception happens (what the with clause is meant to do). If this code retries the connection, it should not wrap it in this with at least at this level.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The with line was throwing the exception because the port did not (yet) exist which is why I put the try and retry around it, would it still need to close a port that did not open? The inner code catches the exception in the case when it tries to actually read from the port...

esphome/__main__.py Outdated Show resolved Hide resolved
@@ -76,6 +76,8 @@ uint32_t random_uint32() {
return esp_random();
#elif defined(USE_ESP8266)
return os_random();
#elif defined(USE_RP2040)
return rand();
Copy link
Member

@OttoWinter OttoWinter Mar 11, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note this returns values from 0 to RAND_MAX, which iirc usually is 2**16. To return a random 32 bit value it should run rand multiple times to get 0-2**32 range

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure about this part at all, thanks.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this suffice?

Suggested change
return rand();
return ((uint32_t) rand()) + ((uint32_t) rand());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that only doubles the range from 0..216 to 0..217. You want rand()<<16 + rand(). Not sure if the casts are needed on that or not, but it's certianly safe to include them: ((uint32_t) rand())<<16 + ((uint32_t) rand());

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh duh, how did I miss that..haha

Comment on lines +22 to +28
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_RP2040,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not that I really want a change but aren't we calling platform other things already? what about CHIPSET or something else?

Copy link
Member Author

@jesserockz jesserockz Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point 😅
But it was esphome -> platform before which is why I guess

esphome/core/helpers.cpp Outdated Show resolved Hide resolved
@geerlingguy
Copy link

I'm trying to test this on my Mac (running on an M1 Mac Studio), but when I try esphome run, the install fails:

$ esphome run config/config.yml
INFO Reading configuration config/config.yml...
INFO Generating C++ source...
INFO Core config or version changed, cleaning build files...
INFO Compiling app...
...
Tool Manager: Installing platformio/framework-arduino-mbed @ ~2.7.2
Error: Could not find the package with 'platformio/framework-arduino-mbed @ ~2.7.2' requirements for your system 'darwin_arm64'

It looks like platformio/framework-arduino-mbed is available for darwin_arm64, but only with later versions (3.x). And maybe that will require bumping to a newer version of platformio in the rp2040 requirements.txt since 6.x is available now? https://github.com/jesserockz/esphome/blob/rp2040/requirements.txt#L9

@geerlingguy
Copy link

After the versions were bumped, compilation works great on my M1 Mac Studio. With the following config, I was able to get the LED flashing on my Pico via ESPHome—nice!

esphome:
  name: rpi-pico

rp2040:
  board: pico

output:
  - platform: gpio
    pin:
      number: 25
      mode: output
    id: LED

interval:
  - interval: 500ms
    then:
      - output.turn_on: LED
      - delay: 250ms
      - output.turn_off: LED

To install this branch, I ran pip3 install git+https://github.com/jesserockz/esphome.git@rp2040 (making sure I had uninstalled esphome prior to that).

@cpyarger
Copy link

cpyarger commented Jul 1, 2022

Tried installing using windows. Got the following error

INFO Installing earlephilhower/framework-arduinopico @ ~1.20101.0
Error: Could not find the package with 'earlephilhower/framework-arduinopico @ ~1.20101.0' requirements for your system 'windows_amd64'

@jesserockz
Copy link
Member Author

For those following this PR, I ave pushed another round of changes.
These implement the Wifi code for the pico-w as they were added to arduino-pico after I made my original changes in a fork.

There are still things missing, but Home Assistant can talk to the pico-w via the ESPHome API and ESPHome can update the pico-w via OTA updates.

I call that progress =)

Would be good to know if specific sensors/configurations do not work so I can look into them so please leave a comment if you find something.

Thanks,
Jesse

@p4block
Copy link

p4block commented Oct 19, 2022

Great to see this closer to merging!

I am unable to test even a minimal config and haven't been able to find what's wrong with my setup, must be missing something very obvious.

esphome run pico.yaml
[normal build output]
|   |   |-- lwIP_CYW43 @ 1
|   |   |   |-- SPI @ 1.0
|   |   |-- SPI @ 1.0
ModuleNotFoundError: No module named 'platformio.device.list.util'; 'platformio.device.list' is not a package:
  File "/home/hal/.local/lib/python3.10/site-packages/platformio/builder/main.py", line 184:
    env.SConscript("$BUILD_SCRIPT")
  File "/home/hal/.platformio/packages/tool-scons/scons-local-4.3.0/SCons/Script/SConscript.py", line 597:
    return _SConscript(self.fs, *files, **subst_kw)
  File "/home/hal/.platformio/packages/tool-scons/scons-local-4.3.0/SCons/Script/SConscript.py", line 285:
    exec(compile(scriptdata, scriptname, 'exec'), call_stack[-1].globals)
  File "/home/hal/.platformio/platforms/raspberrypi/builder/main.py", line 335:
    from platformio.device.list.util import list_logical_devices

@maxgerhardt
Copy link

ModuleNotFoundError: No module named 'platformio.device.list.util'; 'platformio.device.list' is not a package:

Did you try a pio upgrade --dev in the CLI (or anywhere where you have the pio command available)?

@p4block
Copy link

p4block commented Oct 19, 2022

My lack of pio-fu is strong. Yes, that worked, I can toy around with rp2040home now 😃

@balloob
Copy link
Member

balloob commented Oct 19, 2022

pip3 install platformio works too. It should probably be bumped in requirements.txt or preferably in a separate PR.

@BenSisk
Copy link

BenSisk commented Oct 19, 2022

Might be the wrong place and I might just be dumb so feel free to delete, but I can't seem to find the right device path when uploading a firmware.

The .yaml file compiles successfully on both windows 11 and raspbian bullseye, but I can't seem to find the right device path for my pico w on either windows or from a pi 4. I expected the device to show up under /dev/ttyUSB*, but it doesn't, only /dev/sda and did not work.

Jeef Geerling has a short blog about this on his site, but he is using mac OS and says the path for the USB volume will be different.

@jesserockz
Copy link
Member Author

@BenSisk Initially you need to put the device into bootloader mode by holding the button when powering it on. Then you can drag and drop the uf2 firmware file into the storage device that will be mounted on the computer. It will then automatically boot ESPHome.

@jesserockz jesserockz marked this pull request as ready for review October 20, 2022 03:46
@probot-esphome
Copy link

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (logger) you are listed as a code owner for? Thanks!
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (ota) you are listed as a code owner for? Thanks!
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (socket) you are listed as a code owner for? Thanks!
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (mdns) you are listed as a code owner for? Thanks!
Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (md5) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@jesserockz
Copy link
Member Author

jesserockz commented Oct 20, 2022

I am going to merge this PR so its easier for people to test and make PRs to improve support.
I have created a new feature-request issue to keep track of the items remaining to get this working the best we can.
esphome/feature-requests#1924

@jesserockz jesserockz changed the title Support for RP2040 platform Initial Support for RP2040 platform Oct 20, 2022
@jesserockz jesserockz merged commit 6153bcc into esphome:dev Oct 20, 2022
@github-actions github-actions bot locked and limited conversation to collaborators Oct 22, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.