Skip to content

Commit

Permalink
[nrfconnect] Add support for nRF52840 Dongle to lighting-app (#8080)
Browse files Browse the repository at this point in the history
* [nrfconnect] Add support for nRF52840 Dongle to lighting-app

Allow to build nRF Connect Lighting Example for the
nRF52840 Dongle.

* Apply code review comments

* Restyled by prettier-markdown

Co-authored-by: Restyled.io <commits@restyled.io>
  • Loading branch information
2 people authored and pull[bot] committed Aug 6, 2021
1 parent e39590e commit 37b0510
Show file tree
Hide file tree
Showing 12 changed files with 173 additions and 30 deletions.
1 change: 1 addition & 0 deletions config/nrfconnect/chip-module/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ chip_gn_arg_string("zephyr_ar" ${CMAKE_AR})
chip_gn_arg_string("zephyr_cc" ${CMAKE_C_COMPILER})
chip_gn_arg_string("zephyr_cxx" ${CMAKE_CXX_COMPILER})
chip_gn_arg_bool ("is_debug" CONFIG_DEBUG)
chip_gn_arg_bool ("chip_logging" CONFIG_LOG)
chip_gn_arg_bool ("chip_enable_openthread" CONFIG_NET_L2_OPENTHREAD)
chip_gn_arg_bool ("chip_config_network_layer_ble" CONFIG_BT)
chip_gn_arg_bool ("chip_inet_config_enable_ipv4" CONFIG_NET_IPV4)
Expand Down
6 changes: 5 additions & 1 deletion examples/lighting-app/nrfconnect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ if (EXISTS boards/${BOARD}.conf)
endif()

# TODO: temporary fix to remove after solving static addressing problem on nrf5340
if(${BOARD} STREQUAL "nrf5340dk_nrf5340_cpuapp")
if(BOARD STREQUAL "nrf5340dk_nrf5340_cpuapp")
list(INSERT OVERLAY_CONFIG 0 ${CHIP_ROOT}/config/nrfconnect/app/overlay-bt_private_addresses.conf)
endif()

Expand Down Expand Up @@ -96,6 +96,10 @@ target_sources(app PRIVATE
${CHIP_ROOT}/src/app/clusters/network-commissioning/network-commissioning.cpp
)

if (BOARD STREQUAL "nrf52840dongle_nrf52840")
target_sources(app PRIVATE ${NRFCONNECT_COMMON}/util/DFUTrigger.cpp)
endif()

if (CONFIG_CHIP_PW_RPC)

set(PIGWEED_ROOT "${CHIP_ROOT}/third_party/pigweed/repo")
Expand Down
40 changes: 33 additions & 7 deletions examples/lighting-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ into an existing CHIP network and can be controlled by this network.
- [Building with Device Firmware Upgrade support](#building-with-device-firmware-upgrade-support)
- [Configuring the example](#configuring-the-example)
- [Flashing and debugging](#flashing-and-debugging)
- [Flashing on the development kits](#nrfdks_flashing)
- [Flashing on the nRF52840 Dongle](#nrf52840dongle_flashing)
- [Testing the example](#testing-the-example)
- [Testing using CHIPTool](#testing-using-chiptool)
- [Testing Device Firmware Upgrade](#testing-device-firmware-upgrade)
Expand Down Expand Up @@ -75,9 +77,8 @@ manually after powering up the device by pressing **Button 4**.

### Bluetooth LE rendezvous

In CHIP, the commissioning procedure (called rendezvous) is done over Bluetooth
LE between a CHIP device and the CHIP controller, where the controller has the
commissioner role.
In CHIP, the commissioning procedure is done over Bluetooth LE between a CHIP
device and the CHIP controller, where the controller has the commissioner role.

To start the rendezvous, the controller must get the commissioning information
from the CHIP device. The data payload is encoded within a QR code, printed to
Expand Down Expand Up @@ -147,10 +148,11 @@ more information.

The example supports building and running on the following devices:

| Hardware platform | Build target | Platform image |
| ----------------------------------------------------------------------------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------ |
| [nRF52840 DK](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK) | `nrf52840dk_nrf52840` | <details><summary>nRF52840 DK</summary><img src="../../platform/nrfconnect/doc/images/nRF52840_DK_info-medium.jpg" alt="nRF52840 DK"/></details> |
| [nRF5340 DK](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF5340-DK) | `nrf5340dk_nrf5340_cpuapp` | <details><summary>nRF5340 DK</summary><img src="../../platform/nrfconnect/doc/images/nRF5340_DK_info-medium.jpg" alt="nRF5340 DK"/></details> |
| Hardware platform | Build target | Platform image |
| ------------------------------------------------------------------------------------------------- | -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------- |
| [nRF52840 DK](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-DK) | `nrf52840dk_nrf52840` | <details><summary>nRF52840 DK</summary><img src="../../platform/nrfconnect/doc/images/nRF52840_DK_info-medium.jpg" alt="nRF52840 DK"/></details> |
| [nRF5340 DK](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF5340-DK) | `nrf5340dk_nrf5340_cpuapp` | <details><summary>nRF5340 DK</summary><img src="../../platform/nrfconnect/doc/images/nRF5340_DK_info-medium.jpg" alt="nRF5340 DK"/></details> |
| [nRF52840 Dongle](https://www.nordicsemi.com/Software-and-Tools/Development-Kits/nRF52840-Dongle) | `nrf52840dongle_nrf52840` | <details><summary>nRF52840 Dongle</summary><img src="../../platform/nrfconnect/doc/images/nRF52840_Dongle-medium.jpg" alt="nRF52840 Dongle"/></details> |

<hr>

Expand All @@ -162,6 +164,15 @@ This section lists the User Interface elements that you can use to control and
monitor the state of the device. These correspond to PCB components on the
platform image.

> **Note**:
>
> The following Device UI elements are missing on the nRF52840 Dongle: **Button
> 2**, **Button 3**, **Button 4**, **SEGGER J-Link USB port**, and **NFC port
> with antenna attached**. You can collect logs from the nRF52840 Dongle using
> the **nRF USB port** instead of the **SEGGER J-Link USB port**.
> Functionalities associated with the remaining missing elements are
> inaccessible.
**LED 1** shows the overall state of the device and its connectivity. The
following states are possible:

Expand Down Expand Up @@ -432,6 +443,13 @@ page.

## Flashing and debugging

The flashing and debugging procedure is different for the development kits and
the nRF52840 Dongle.

<a name="nrfdks_flashing"></a>

### Flashing on the development kits

To flash the application to the device, use the west tool and run the following
command from the example directory:

Expand All @@ -445,6 +463,14 @@ directory:

$ west debug

<a name="nrf52840dongle_flashing"></a>

### Flashing on the nRF52840 Dongle

Visit
[Programming and Debugging nRF52840 Dongle](https://docs.zephyrproject.org/latest/boards/arm/nrf52840dongle_nrf52840/doc/index.html#programming-and-debugging)
to read more about flashing on the nRF52840 Dongle.

<hr>

## Testing the example
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#
# Copyright (c) 2021 Project CHIP Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

# Make sure the pairing window starts automatically upon startup as the nRF52840 Dongle
# has only one button and it is reserved for the factory reset
CONFIG_CHIP_ENABLE_PAIRING_AUTOSTART=y

# Configure shell and logging over USB
CONFIG_USB=y
CONFIG_USB_UART_CONSOLE=y
CONFIG_UART_INTERRUPT_DRIVEN=y
CONFIG_UART_LINE_CTRL=y
CONFIG_UART_SHELL_ON_DEV_NAME="CDC_ACM_0"
CONFIG_SHELL_BACKEND_SERIAL_INIT_PRIORITY=51

# The minimal logging mode does not work properly with the USB CDC device, so use the deferred mode
CONFIG_LOG_MODE_MINIMAL=n
CONFIG_LOG_MODE_DEFERRED=y
CONFIG_LOG_STRDUP_MAX_STRING=128
CONFIG_LOG_STRDUP_BUF_COUNT=24
15 changes: 14 additions & 1 deletion examples/lighting-app/nrfconnect/main/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@

#include <kernel.h>

#ifdef CONFIG_USB
#include <usb/usb_device.h>
#endif

LOG_MODULE_REGISTER(app);

using namespace ::chip;
Expand All @@ -32,12 +36,21 @@ using namespace ::chip::DeviceLayer;

int main(void)
{
#if CONFIG_CHIP_PW_RPC
#ifdef CONFIG_CHIP_PW_RPC
chip::rpc::Init();
#endif

int ret = 0;

#ifdef CONFIG_USB
ret = usb_enable(nullptr);
if (ret)
{
LOG_ERR("Failed to initialize USB device");
goto exit;
}
#endif

ret = chip::Platform::MemoryInit();
if (ret != CHIP_NO_ERROR)
{
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/nrfconnect/prj.conf
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ CONFIG_MPU_STACK_GUARD=y

# CHIP configuration
CONFIG_CHIP_PROJECT_CONFIG="main/include/CHIPProjectConfig.h"

6 changes: 3 additions & 3 deletions examples/lock-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ LE advertising manually after powering up the device by pressing **Button 4**.

### Bluetooth LE rendezvous

In this example, the commissioning procedure (called rendezvous) is done over
Bluetooth LE between a CHIP device and the CHIP controller, where the controller
has the commissioner role.
In this example, the commissioning procedure is done over Bluetooth LE between a
CHIP device and the CHIP controller, where the controller has the commissioner
role.

To start the rendezvous, the controller must get the commissioning information
from the CHIP device. The data payload is encoded within a QR code, printed to
Expand Down
2 changes: 1 addition & 1 deletion examples/pigweed-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ directory:
### Flashing on the nRF52840 Dongle

Visit
[Programming and Flashing nRF52840 Dongle](https://docs.zephyrproject.org/latest/boards/arm/nrf52840dongle_nrf52840/doc/index.html#programming-and-debugging)
[Programming and Debugging nRF52840 Dongle](https://docs.zephyrproject.org/latest/boards/arm/nrf52840dongle_nrf52840/doc/index.html#programming-and-debugging)
to read more about flashing on the nRF52840 Dongle.

<hr>
Expand Down
60 changes: 60 additions & 0 deletions examples/platform/nrfconnect/util/DFUTrigger.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#ifndef CONFIG_BOARD_NRF52840DONGLE_NRF52840
#error "The command for triggerring the DFU is available for nRF52840 Dongle only"
#endif

#include <drivers/gpio.h>
#include <shell/shell.h>

namespace {
constexpr const char * kGPIOController = "GPIO_0";
constexpr gpio_pin_t kGPIOResetPin = 19;

int cmd_dfu(const struct shell * shell, size_t argc, char ** argv)
{
// nRF52840 Dongle contains immutable bootloader which supports the DFU over a serial port.
// Normally, a user must press the Reset button to reboot into the bootloader, but it can
// also be done programatically by setting an appropriate GPIO pin.

const device * gpioController = device_get_binding(kGPIOController);

if (!gpioController)
{
shell_fprintf(shell, SHELL_NORMAL, "Cannot find GPIO controller");
return -ENOEXEC;
}

if (gpio_pin_configure(gpioController, kGPIOResetPin, GPIO_OUTPUT))
{
shell_fprintf(shell, SHELL_NORMAL, "Cannot configure GPIO reset pin");
return -ENOEXEC;
}

if (gpio_pin_set_raw(gpioController, kGPIOResetPin, 0))
{
shell_fprintf(shell, SHELL_NORMAL, "Cannot set GPIO reset pin");
return -ENOEXEC;
}

return 0;
}
} // namespace

SHELL_CMD_ARG_REGISTER(dfu, NULL, "Trigger DFU over serial port", cmd_dfu, 0, 0);
6 changes: 3 additions & 3 deletions examples/pump-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ LE advertising manually after powering up the device by pressing **Button 4**.

### Bluetooth LE rendezvous

In this example, the commissioning procedure (called rendezvous) is done over
Bluetooth LE between a CHIP device and the CHIP controller, where the controller
has the commissioner role.
In this example, the commissioning procedure is done over Bluetooth LE between a
CHIP device and the CHIP controller, where the controller has the commissioner
role.

To start the rendezvous, the controller must get the commissioning information
from the CHIP device. The data payload is encoded within a QR code, printed to
Expand Down
6 changes: 3 additions & 3 deletions examples/pump-controller-app/nrfconnect/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,9 @@ LE advertising manually after powering up the device by pressing **Button 4**.

### Bluetooth LE rendezvous

In this example, the commissioning procedure (called rendezvous) is done over
Bluetooth LE between a CHIP device and the CHIP controller, where the controller
has the commissioner role.
In this example, the commissioning procedure is done over Bluetooth LE between a
CHIP device and the CHIP controller, where the controller has the commissioner
role.

To start the rendezvous, the controller must get the commissioning information
from the CHIP device. The data payload is encoded within a QR code, printed to
Expand Down
27 changes: 16 additions & 11 deletions src/platform/Zephyr/Logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,15 @@
#include <logging/log.h>
#include <sys/cbprintf.h>

// If CONFIG_LOG_MODE_MINIMAL the timestamp is NOT added automatically by the Zephyr logger
#ifdef CONFIG_LOG_MODE_MINIMAL
#define LOG_FORMAT "%u %s"
#define LOG_MESSAGE(msg) k_uptime_get_32(), (msg)
#else
#define LOG_FORMAT "%s"
#define LOG_MESSAGE(msg) (msg)
#endif

LOG_MODULE_REGISTER(chip, LOG_LEVEL_DBG);

namespace chip {
Expand All @@ -34,17 +43,13 @@ namespace Platform {
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
snprintfcb(formattedMsg, sizeof(formattedMsg), "[%s]", module);

snprintfcb(formattedMsg, sizeof(formattedMsg), "%u [%s]", k_uptime_get_32(), module);

// -2 to ensure at least one byte available for vsnprintf below.
formattedMsg[sizeof(formattedMsg) - 2] = 0;

size_t prefixLen = strlen(formattedMsg);

// Append the log message.
const size_t prefixLen = strlen(formattedMsg);
vsnprintfcb(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);

const char * allocatedMsg = log_strdup(formattedMsg);

// Invoke the Zephyr logging library to log the message.
//
// Unfortunately the Zephyr logging macros end up assigning uint16_t
Expand All @@ -56,14 +61,14 @@ void LogV(const char * module, uint8_t category, const char * msg, va_list v)
switch (category)
{
case kLogCategory_Error:
LOG_ERR("%s", log_strdup(formattedMsg));
LOG_ERR(LOG_FORMAT, LOG_MESSAGE(allocatedMsg));
break;
case kLogCategory_Progress:
default:
LOG_INF("%s", log_strdup(formattedMsg));
LOG_INF(LOG_FORMAT, LOG_MESSAGE(allocatedMsg));
break;
case kLogCategory_Detail:
LOG_DBG("%s", log_strdup(formattedMsg));
LOG_DBG(LOG_FORMAT, LOG_MESSAGE(allocatedMsg));
break;
}
#pragma GCC diagnostic pop
Expand Down

0 comments on commit 37b0510

Please sign in to comment.