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

basic support for nrf52840 #6075

Open
wants to merge 243 commits into
base: dev
Choose a base branch
from
Open

basic support for nrf52840 #6075

wants to merge 243 commits into from

Conversation

tomaszduda23
Copy link
Contributor

@tomaszduda23 tomaszduda23 commented Jan 10, 2024

What does this implement/fix?

It is possible to buy cheap nrf52840 module. It would be very convenience to program it using yaml files.
It is proof of concept for support NRF52 on esphome.

How to upload

There are few options:

  1. with stlink python -m esphome upload tests/test12.2.yaml --device PYOCD
  2. if board have adafruit bootloader image has to build with
nrf52:
  board: adafruit_itsybitsy_nrf52840

to upload python -m esphome upload tests/test12.3.yaml

  1. if board support UF2
cp ./tests/.esphome/build/nrf52-test-nrf-adafruit/.pioenvs/nrf52-test-nrf-adafruit/zephyr/zephyr.uf2 /media/0321-5432/
  1. BLE OTA with mcuboot python -m esphome upload tests/test12.2.yaml to specify mac address of BLE device python -m esphome upload tests/test12.2.yaml --device AA:BB:CC:11:22:33

TODO

Arduino
moved to #6228

Zephyr

  • GPIO
  • CDC DFU (0.8.2 is more stable) - adafruit
  • CDC logger
  • nrf-sdk based
  • UART0 logger
  • UART0 logger tested
  • BLE OTA - mcuboot
  • CDC OTA - mcuboot recover (`FLASH' overflowed by 11320 bytes)
  • CDC OTA - mcuboot
  • serial OTA - mcuboot recovery
  • watchdog
  • BLE logger
  • preferences for restore mode
  • preferences for adafruit
  • disable update of preferences during OTA - why is it done for other platforms?
  • fix arm builds
  • add pyocd to upload
  • move to main branch for smp
  • move to main branch for smpclient
  • move debug config to separate component
  • validate prj.conf after compilation to make sure that config was applied correctly
  • GPIO interrupts
  • what to do if image cannot be confirmed during OTA?
  • implement get mac
  • change name for dfu?
  • adc
  • pull up does not work for P0.17 and P0.20
  • change voltage (it works only twice without erase)
  • delete files if app.overlay change
  • i2c
  • smpclient do not support serial serial update do not work intercreate/smpclient#22

Fix review comments #6075 (comment)

How to communicate with HA? Options:

The SoC has only 1MB of memory. All above consume:

RAM:   [=         ]   5.9% (used 14736 bytes from 248832 bytes)
Flash: [==        ]  17.9% (used 146000 bytes from 815104 bytes)

update adafruit bootloader

git clone https://github.com/adafruit/Adafruit_nRF52_Bootloader
cd Adafruit_nRF52_Bootloader
make BOARD=nice_nano all
make BOARD=nice_nano SERIAL=/dev/ttyACM0 flash-dfu

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
  • BK72xx
  • RTL87xx
  • nrf52840

Example entry for config.yaml:

---
nrf52:
  # board: adafruit_itsybitsy_nrf52840
  board: adafruit_feather_nrf52840
  # framework:
  # variant: nrf-sdk
  # bootloader: adafruit
  # bootloader: mcuboot

esphome:
  name: nrf52-test-nrf

logger:
  level: DEBUG
  logs:
    switch: NONE

switch:
  - platform: gpio
    pin:
      number: 15
      inverted: true
      mode:
        output: true
    id: gpio_15

interval:
  - interval: 500ms
    then:
      - switch.toggle: gpio_15

output:
  - platform: gpio
    pin:
      number: 14
      inverted: true
      mode:
        output: true
    id: rest_gpio

ota:
  - platform: zephyr_mcumgr

zephyr_ble_server:

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:

@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!
(message by CodeOwnersMention)

@probot-esphome
Copy link

Hey there @tomaszduda23,
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 = ["@tomaszduda23"]

And run script/build_codeowners.py

(message by NeedsCodeownersLabel)

@codecov-commenter
Copy link

codecov-commenter commented Jan 10, 2024

Codecov Report

Attention: Patch coverage is 20.57416% with 166 lines in your changes missing coverage. Please review.

Project coverage is 53.79%. Comparing base (4d8b5ed) to head (c2698a5).
Report is 727 commits behind head on dev.

Files Patch % Lines
esphome/zephyr_tools.py 22.05% 106 Missing ⚠️
esphome/__main__.py 5.45% 52 Missing ⚠️
esphome/writer.py 0.00% 3 Missing ⚠️
esphome/config_validation.py 60.00% 2 Missing ⚠️
esphome/core/config.py 33.33% 1 Missing and 1 partial ⚠️
esphome/core/__init__.py 83.33% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##              dev    #6075      +/-   ##
==========================================
+ Coverage   53.70%   53.79%   +0.08%     
==========================================
  Files          50       51       +1     
  Lines        9408     9824     +416     
  Branches     1654     1737      +83     
==========================================
+ Hits         5053     5285     +232     
- Misses       4056     4213     +157     
- Partials      299      326      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@probot-esphome
Copy link

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

@lboue
Copy link
Contributor

lboue commented Jan 11, 2024

I am very interested in testing this platform in ESPHome because I own several nrf52840 modules.

@probot-esphome
Copy link

Hey there @OttoWinter, mind taking a look at this pull request as it has been labeled with an integration (api) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-esphome
Copy link

Hey there @OttoWinter, mind taking a look at this pull request as it has been labeled with an integration (debug) you are listed as a code owner for? Thanks!
(message by CodeOwnersMention)

@probot-esphome
Copy link

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

@tomaszduda23 tomaszduda23 marked this pull request as ready for review May 20, 2024 21:57
@probot-esphome
Copy link

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

@probot-esphome
Copy link

Hey there @esphome/core, mind taking a look at this pull request as it has been labeled with an integration (i2c) 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 (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!
(message by CodeOwnersMention)

@Hedda
Copy link

Hedda commented May 21, 2024

@tomaszduda23 FYI, just stumbled on a forum thread by @natelust about his ESPHome to Zephyr RTOS porting project and was wondering if you were aware of his previous work on getting ESPHome running on a nRF5 platform for nRF52 and nRF53?

https://community.home-assistant.io/t/announcement-esphome-running-on-adafruit-52840-over-thread-mesh-networking-using-zephyrrtos/363712

Looks like he has not updated his forked esphome repository with his code patch to add Zephyr platform support patches in the last year as it seems like his "zephyr-dev" branch there in his esphome fork contains his latest updates:

https://github.com/natelust/esphome/tree/zephyr-dev

There do not seem to be any new posts from him on this since he posted this zephyr ESPHome Guide in little over 1-year ago:

https://github.com/natelust/zephyrESPHomeGuide

PS: Noted that he also looks to have made more progress on OpenThread/Thread support in ESPHome than anyone else I have seen posting in public repository so far?

esphome/feature-requests#1397

@tomaszduda23
Copy link
Contributor Author

I've seen that fork some time ago.
OpenThread was the reason to move from generic zephyr to nrf-sdk. It can be done. I haven't had time to look into details though.

@natelust
Copy link
Contributor

natelust commented May 30, 2024

Hi all, sorry your message before caught me in the middle of a lot of stuff going on, and I am sitting down now trying to work through all I need to catch up on. What can I help out with? I had personally chosen to go with core zephyr over the nrf sdk, but things might have changed since then. I had everything up and running stable for some time and did not have much more time to put into it (also getting in upstreamed was stalled a bit). However, recently I had to redo my whole setup, and was going to revive the work, especially now that home assistant has been modified with ipv6 native to work with matter. The way I had things set up (I have not looked at this work yet) it was nice because it was relatively easy to support all the boards that zephyr supported.

@tomaszduda23
Copy link
Contributor Author

@natelust it supported both nrf-sdk and generic zephyr. I dropped generic zephyr to avoid extra testing. It could be added back though.
Merge any NRF52 framework to upstream would be great. If you would like to help with things here I'm down. Openthread would be interesting.
For me BLE is thing which I want to complete first since it is needed for my project. Recently I looked to zigbee. It is quite a lot of work to support different sensor though.

@Hedda
Copy link

Hedda commented May 31, 2024

it supported both nrf-sdk and generic zephyr. I dropped generic zephyr to avoid extra testing.

I am assuming it would be much less complex to only try to get one of those platforms merged at first, however, probably worth considering adding both platforms sooner or later as more similar platforms can gain long-term benefits as it would kind of be following "chaos engineering" principle as experimenting with similar platform could help identify potential issues or problems? ;)

@tomaszduda23
Copy link
Contributor Author

tomaszduda23 commented May 31, 2024

Nrf sdk is based on zephyr. It contains extra stuff for nrf chips. The build system and most of drivers are the same. A lot of things will just work for both.

@natelust
Copy link
Contributor

I was looking at some of the changes to home assistant last night now that matter/thread support is "built in". I think this greatly lowers the barrier to what I have implemented being used. The ESPHome boards should be able to run right over the same thread network and border router, and mostly be transparent to end users.

I'm trying to figure out what would be the most helpful thing for me right now. How does me getting my fork updated against the main, and then coming back and having a discussion on how this work dovetails with what I have done sound? I had not put any effort into the bluetooth stack before, so it might be nice if the solutions can come together.

@Hedda
Copy link

Hedda commented Jun 1, 2024

I'm trying to figure out what would be the most helpful thing for me right now. How does me getting my fork updated against the main, and then coming back and having a discussion on how this work dovetails with what I have done sound? I had not put any effort into the bluetooth stack before, so it might be nice if the solutions can come together.

I have not seen or heard of else started work on OpenThread support for ESPHome, so you might want to consider that if you want to work on something that no one else is working on. If so please also make Thread protocol support compatible with ESP32-H2 and ESP32-C6 SoCs too.

@tomaszduda23
Copy link
Contributor Author

PoC of zigbee tomaszduda23#2

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

Successfully merging this pull request may close these issues.

None yet