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

Migrate code to the nRF Connect SDK #76

Merged
merged 55 commits into from
Dec 7, 2022
Merged

Migrate code to the nRF Connect SDK #76

merged 55 commits into from
Dec 7, 2022

Conversation

rbaron
Copy link
Owner

@rbaron rbaron commented Nov 29, 2022

This PR rewrites all the code from the legacy nRF SDK to the nRF Connect SDK. I will keep pushing updates to this PR and merge it when all tasks are finished and tested. I am only going for feature parity with this rewrite, but I'm factoring out a common core that can be reused in different applications/samples.

All help is welcome in testing the parts that are already implemented.

Tasks

  • Board definitions
    • nRF52840 (code/nrf-connect/prstlib/boards/arm/bparasite_nrf52840/)
      • 1.0.0
      • 1.1.0
      • 1.2.0
    • nRF52833 (Untested, code/nrf-connect/prstlib/boards/arm/bparasite_nrf52833/)
      • 1.0.0
      • 1.1.0
      • 1.2.0
  • Read sensors (implemented in common lib code/nrf-connect/prstlib)
    • SHT30C
    • Photo diode
    • Soil moisture
  • Blinky sample (code/nrf-connect/samples/blinky)
  • Read soil moisture sample (code/nrf-connect/samples/soil_read_loop)
  • BLE sample (code/nrf-connect/samples/ble) (this is the rewrite of the old firmware)
    • Protocols
      • b-parasite v2
      • BTHome v1
      • BTHome v2
    • BT address config
      • Random, static
      • User defined
    • Power profile (2.7uA in sleep)
    • Test in Home Assistant
  • Set up GitHub workflow
    • Lint all samples
    • Build all samples
    • Store build artifacts for samples
  • Build samples with Docker
  • Write docs
    • How to build
      • With nRF Connect for Desktop
      • With Docker
    • Remove old docs
  • Remove old code/b-parasite/

Notes

Building the Samples

With nRF Connect for Desktop + Visual Studio Code

This is the usual way of building the nRF Connect SDK samples. The only caveat is that, for each sample here, you also need to add the code/nrf-connect/prstlib directory to your workspace in addition to the sample directory itself. The prstlib/ contains common source code for reading sensors and also common board definitions. There is a .code-workspace file inside each sample that does this automatically.

With Docker

To build the ble sample for the nRF52840, revision 1.2.0, cd into code/nrf-connect/ and run:

$ docker run --rm -v ${PWD}:/code -w /code/samples/ble nordicplayground/nrfconnect-sdk:v2.1-branch west build --build-dir ./build --pristine --board bparasite_nrf52840@1.2.0

The output will be in code/nrf-connect/samples/ble/build/zephyr/zephyr.hex.

[Experimental but super cool] With GitHub Codespaces

GitHub Codespaces let you run an instance of VS Code directly in your browser. More importantly, it also supports plugins, including nRF Connect for VS Code! This PR introduces .devcontainer/devcontainer.json, which configures a Codespace with both the nRF VS Code plugin, as well as the whole nRF Connect SDK + toolchain. This lets you edit code and build the sample straight in your browser.

  1. Go to Codespaces and click "New codespace"
  2. Select rbaron/b-parasite, branch nrf-connect
  3. Wait. The underlying Docker image is pretty big and the build might fail. It worked on the second try for me
  4. Once the codespace is ready, navigate to code/nrf-connect/samples/ble, select ble.code-workspace and click on "Open workspace". This makes sure both the ble sample and the prstlib are present in the workspace
  5. Build the sample normally, as described in the nRF Connect SDK docs
  6. Download the built sample from ble/build/zephyr/zephyr.hex

Prebuilt Binaries

The GitHub workflow in this PR clang-format-checks, builds and store the built binaries for all sample as artifacts. You can download them here (example). These are built with the default settings, but we may extend this machinery to build a few variants with different configs for each sample.

@rbaron
Copy link
Owner Author

rbaron commented Dec 3, 2022

@jhbruhn I think I found a nice way to do it. I added board revisions for 1.0.0, 1.1.0 (with LDR) and 1.2.0 (with phototransistor). We can just select the variant + revision in the VSCode UI:

image

For building from the command line, we can just append @$revision to the --board switch. For example to build for nRF52833@1.1.0 using the BTHome v1 protocol with sleep time of 1s for quick testing:

$ docker run --rm -v ${PWD}:/code -w /code/samples/ble nordicplayground/nrfconnect-sdk:v2.1-branch west build --build-dir ./build --pristine --board bparasite_nrf52833@1.1.0 -- -DCONFIG_PRST_BLE_ENCODING_BTHOME_V1=y -DCONFIG_PRST_SLEEP_DURATION_SEC=1

@kzyapkov
Copy link

kzyapkov commented Dec 4, 2022

I had started on the same effort, but this is much nicer than what I have. Kudos for the reusable lib part and the board revisions. Awesome addition to an awesome project.

@rbaron
Copy link
Owner Author

rbaron commented Dec 4, 2022

Thanks for the kind words @kzyapkov. I will finish writing the updated docs and merge this in.

Copy link
Contributor

@jhbruhn jhbruhn left a comment

Choose a reason for hiding this comment

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

I'm finally testing this on the nrf52833. Unfortunately I'm having a couple of issues. I already commented on one incorrect pin definition, but more seems wrong:

  • I cannot get a battery measurement, the adc channel definitions seem to be different compared to the nrf52840
  • Same for the illuminance measurement

I tested BTHome V1 and V2 and am successfully receiving packets on my smartphone/computer, but HomeAssistant does not seem to recognize them. But I think the problem is on my end.

I have some logs here, maybe they can help?

Transfer rate: 0 Bytes/s Data written: 1.88 KB *** Booting Zephyr OS build db187bb94af5  ***
[00:00:00.000,274] <inf> sdc_hci_driver: SoftDevice Controller build revision: 
                                         37 22 e7 c6 07 57 fd bf  ad df cd 17 8d 10 8a d6 |7"...W.. ........
                                         ae ca 94 e1                                      |....             
[00:00:00.003,723] <inf> ble: MAC Address: d7:b5:1c:48:fd:69
Transfer rate: 0 Bytes/s Data written: 2.30 KB [00:00:00.834,289] <dbg> adc: get_soil_moisture_percent: Read soil moisture: 33.20 | Raw 210 | Batt: -0.01 | Dry: 304.89 | Wet: 19.06
[00:00:00.834,350] <dbg> adc: get_soil_moisture_percent: Read soil moisture: 33.20 | Raw 210 | Batt: -0.01 | Dry: 304.89 | Wet: 19.06
[00:00:00.834,411] <dbg> adc: get_soil_moisture_percent: Read soil moisture: 33.20 | Raw 210 | Batt: -0.01 | Dry: 304.89 | Wet: 19.06
[00:00:00.834,472] <dbg> adc: get_soil_moisture_percent: Read soil moisture: 33.20 | Raw 210 | Batt: -0.01 | Dry: 304.89 | Wet: 19.06
[00:00:00.844,604] <dbg> adc: prst_adc_photo_read: Read phototransistor: 0 lx | -0.00 V
[00:00:00.862,548] <dbg> shtc3: prst_shtc3_read: Read temp: 18.211060 oC (18)
[00:00:00.862,579] <dbg> shtc3: prst_shtc3_read: Read humi: 60 %
[00:00:00.862,579] <dbg> sensors: prst_sensors_read_all: Batt: -11 mV
[00:00:00.862,609] <dbg> sensors: prst_sensors_read_all: Soil: 33 % (0.000 mV)
[00:00:00.862,609] <dbg> sensors: prst_sensors_read_all: Photo: 0 lx (0.000 mV)
[00:00:00.862,640] <dbg> sensors: prst_sensors_read_all: Temp: 18.211060 oC
[00:00:00.862,640] <dbg> sensors: prst_sensors_read_all: Humi: 60 %
[00:00:00.862,670] <dbg> sensors: prst_sensors_read_all: --------------------------------------------------

compatible = "gpio-keys";
fast_disch: fast_disch {
// P0.25.
gpios = <&gpio1 10 GPIO_ACTIVE_HIGH>;
Copy link
Contributor

Choose a reason for hiding this comment

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

Sorry for chiming in so late, but it seems that the wrong pin is used here (despite the comment).

@rbaron
Copy link
Owner Author

rbaron commented Dec 20, 2022

Tks for trying this out @jhbruhn. I will take a closer look today. From the logs it looks like the ADC channels are not set up properly (in addition too the wrong fast discharge pin).

Did you build the firmware with VSCode or Docker? For the 1.1.0 revision, right?

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

Successfully merging this pull request may close these issues.

None yet

4 participants