Skip to content

Commit

Permalink
Add P8 smartwatch configuration
Browse files Browse the repository at this point in the history
* Split into pinetime (default) and p8 target
* Change LFCLK to LFRC to support devices without LF crystal
* Support various SPI flash chips by ignoring JEDEC IDs
* Use recent mynewt-core and mcu-tools
  • Loading branch information
StarGate01 committed Aug 3, 2022
1 parent 2e1a66e commit 12d5a73
Show file tree
Hide file tree
Showing 19 changed files with 163 additions and 40 deletions.
4 changes: 2 additions & 2 deletions .vscode/launch-nrf52-pi.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// Application Executable to be flashed before debugging. Note that the Application ELF image does not contain a valid Image Header, so we must install the Stub Bootloader.
"executable": "bin/targets/nrf52_my_sensor/app/apps/my_sensor_app/my_sensor_app.elf",
// Uncomment one of these lines to debug MCUBoot or Stub Bootloader:
// "executable": "bin/targets/nrf52_boot/app/boot/mynewt/mynewt.elf",
// "executable": "bin/targets/nrf52_boot/app/apps/boot_stub/boot_stub.elf",
// "executable": "bin/targets/pinetime/app/boot/mynewt/mynewt.elf",
// "executable": "bin/targets/pinetime/app/apps/boot_stub/boot_stub.elf",
"device": "nRF52832",
// Arm System View Description, from apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/mdk
"svdFile": "scripts/nrf52/nrf52.svd",
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch-nrf52.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// Application Executable to be flashed before debugging. Note that the Application ELF image does not contain a valid Image Header, so we must install the Stub Bootloader.
"executable": "bin/targets/nrf52_my_sensor/app/apps/my_sensor_app/my_sensor_app.elf",
// Uncomment one of these lines to debug MCUBoot or Stub Bootloader:
// "executable": "bin/targets/nrf52_boot/app/boot/mynewt/mynewt.elf",
// "executable": "bin/targets/nrf52_boot/app/apps/boot_stub/boot_stub.elf",
// "executable": "bin/targets/pinetime/app/boot/mynewt/mynewt.elf",
// "executable": "bin/targets/pinetime/app/apps/boot_stub/boot_stub.elf",
"device": "nRF52832",
// Arm System View Description, from apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/mdk
"svdFile": "scripts/nrf52/nrf52.svd",
Expand Down
4 changes: 2 additions & 2 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
// Application Executable to be flashed before debugging. Note that the Application ELF image does not contain a valid Image Header, so we must install the Stub Bootloader.
"executable": "bin/targets/nrf52_my_sensor/app/apps/my_sensor_app/my_sensor_app.elf",
// Uncomment one of these lines to debug MCUBoot or Stub Bootloader:
// "executable": "bin/targets/nrf52_boot/app/boot/mynewt/mynewt.elf",
// "executable": "bin/targets/nrf52_boot/app/apps/boot_stub/boot_stub.elf",
// "executable": "bin/targets/pinetime/app/boot/mynewt/mynewt.elf",
// "executable": "bin/targets/pinetime/app/apps/boot_stub/boot_stub.elf",
"device": "nRF52832",
// Arm System View Description, from apache-mynewt-core/hw/mcu/nordic/src/ext/nrfx/mdk
"svdFile": "scripts/nrf52/nrf52.svd",
Expand Down
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,11 @@ The goal of this firmware is to provide a mean for the user to OTA a new firmwar
- Clone the project and `cd` into it
- Init and update submodules : `git submodule update --init --recursive`
- Configure mynewt : `newt upgrade`
- Build : `scripts/nrf52/build-boot.sh`. The firmware is generated in `bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf` and the DFU file for the reloader : `reloader/build-pinetime/reloader-mcuboot.zip`
- Build : `scripts/nrf52/build-boot.sh`. The firmware is generated in `bin/targets/pinetime/app/@mcuboot/boot/mynewt/mynewt.elf` and the DFU file for the reloader : `reloader/build-pinetime/reloader-mcuboot.zip`

You can also build the firmware for the P8 watch, which has a slightly different pin mapping. The `build-boot.sh` accepts an optional argument (default: `pinetime`, options: `pinetime`, `p8`) to specify the target to build.

- Build : `scripts/nrf52/build-boot.sh p8`. The firmware is generated in `bin/targets/p8/app/@mcuboot/boot/mynewt/mynewt.elf` and the DFU file for the reloader : `reloader/build-p8/reloader-mcuboot.zip`

## About the code

Expand Down
8 changes: 5 additions & 3 deletions hw/bsp/nrf52/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,18 @@ syscfg.vals:

###########################################################################
# SPI Flash
# XTX XT25F32B 32 Mb (4 MB) SPI NOR Flash (similar to QuadSPI SPI NOR Flash like Macronix 32 Mb (4 MB) MX25L3233F)
# manufacturer (0x0b), device (0x15), memory type (0x40), density (0x16)
# Settings below are documented at https://github.com/apache/mynewt-core/blob/master/hw/drivers/flash/spiflash/syscfg.yml

SPIFLASH: 1 # Enable SPI Flash
SPIFLASH_SPI_NUM: 0 # SPI Interface 0
SPIFLASH_SPI_CS_PIN: 5 # SPI interface CS pin: P0.05/AIN3, SPI-CE# (SPI-NOR)
SPIFLASH_BAUDRATE: 8000 # Requested baudrate, 8000 is the fastest baudrate supported by nRF52832

SPIFLASH_MANUFACTURER: 0x0B # Expected SpiFlash manufacturer as read by Read JEDEC ID command 9FH
SPIFLASH_MEMORY_TYPE: 0x40 # Expected SpiFlash memory type as read by Read JEDEC ID command 9FH
SPIFLASH_IGNORE_MANUFACTURER: 1 # Many P8 watches contain a whole zoo of different SPI flash chips across variants
SPIFLASH_IGNORE_MEMORY_TYPE: 1 # The chips still behave the same as the PineTime one.

SPIFLASH_MEMORY_CAPACITY: 0x16 # Expected SpiFlash memory capactity as read by Read JEDEC ID command 9FH (2 ^ 0x16 = 32 Mb)
SPIFLASH_SECTOR_COUNT: 1024 # Number of sectors: 1024 sectors of 4 KB each
SPIFLASH_SECTOR_SIZE: 4096 # TODO Number of bytes that can be erased at a time: 4 KB sector size
Expand Down Expand Up @@ -92,7 +94,7 @@ syscfg.vals:
COREDUMP_FLASH_AREA: FLASH_AREA_IMAGE_1

MCU_DCDC_ENABLED: 1
MCU_LFCLK_SOURCE: LFXO
MCU_LFCLK_SOURCE: LFRC # Compatible with all variants, with or without external crystal
BOOT_SERIAL_DETECT_PIN: 20

###########################################################################
Expand Down
2 changes: 1 addition & 1 deletion libs/pinetime_boot/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pkg.deps:
# Mynewt consolidates the initialisation functions into sysinit()
# and calls them according to the Stage number, highest number first.
# Stage 500 is used by Sensor Creator so we use Stage 600 onwards.
# Generated sysinit() for Bootloader: bin/targets/nrf52_boot/generated/src/nrf52_boot-sysinit-app.c
# Generated sysinit() for Bootloader: bin/targets/pinetime/generated/src/pinetime-sysinit-app.c

pkg.init:
# pinetime_boot should be initialised last, when SPI and Semihosting Console are up
Expand Down
4 changes: 1 addition & 3 deletions libs/pinetime_boot/src/blink.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
#include <os/mynewt.h>
#include <hal/hal_gpio.h>

#define PUSH_BUTTON_IN 13 // GPIO Pin P0.13: PUSH BUTTON_IN

/// GPIO settings for the backlight: LCD_BACKLIGHT_{LOW,MID,HIGH} (P0.14, 22, 23)
static const uint8_t backlights[] = {
14, // Low Backlight
Expand Down Expand Up @@ -130,6 +128,6 @@ static void delay_ms(uint32_t ms) {
uint8_t button_samples = 0;
for (int i = 0; i < ms; i++) {
for (int delay = 0; delay < 100000; delay++) {}
button_samples += hal_gpio_read(PUSH_BUTTON_IN);
button_samples += hal_gpio_read(MYNEWT_VAL(PUSH_BUTTON_IN));
}
}
1 change: 0 additions & 1 deletion libs/pinetime_boot/src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#define DISPLAY_RST 26 // LCD_RESET (P0.26): Display reset
#define DISPLAY_HIGH 23 // LCD_BACKLIGHT_{LOW,MID,HIGH} (P0.14, 22, 23): Backlight (active low)
#define BATCH_SIZE 256 // Max number of SPI data bytes to be transmitted
#define PUSH_BUTTON_IN 13 // GPIO Pin P0.13: PUSH BUTTON_IN

// Screen Size
#define ROW_COUNT 240
Expand Down
11 changes: 4 additions & 7 deletions libs/pinetime_boot/src/pinetime_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,6 @@
#include <hal/hal_watchdog.h>
#include "pinetime_boot/version.h"

#define PUSH_BUTTON_IN 13 // GPIO Pin P0.13: PUSH BUTTON_IN
#define PUSH_BUTTON_OUT 15 // GPIO Pin P0.15/TRACEDATA2: PUSH BUTTON_OUT

/// Vector Table will be relocated here.
#define RELOCATED_VECTOR_TABLE 0x7F00

Expand All @@ -54,9 +51,9 @@ void pinetime_boot_init(void) {

// Init the push button. The button on the side of the PineTime is disabled by default. To enable it, drive the button out pin (P0.15) high.
// While enabled, the button in pin (P0.13) will be high when the button is pressed, and low when it is not pressed.
hal_gpio_init_in(PUSH_BUTTON_IN, HAL_GPIO_PULL_DOWN);
hal_gpio_init_out(PUSH_BUTTON_OUT, 1);
hal_gpio_write(PUSH_BUTTON_OUT, 1); // Enable the button
hal_gpio_init_in(MYNEWT_VAL(PUSH_BUTTON_IN), HAL_GPIO_PULL_DOWN);
hal_gpio_init_out(MYNEWT_VAL(PUSH_BUTTON_OUT), 1);
hal_gpio_write(MYNEWT_VAL(PUSH_BUTTON_OUT), 1); // Enable the button
// blink_backlight(1, 1);

// Display the image.
Expand All @@ -70,7 +67,7 @@ void pinetime_boot_init(void) {
console_printf("Waiting 5 seconds for button...\n"); console_flush();
for (int i = 0; i < 64 * 5; i++) {
for (int delay = 0; delay < 3000; delay++) {
button_samples += hal_gpio_read(PUSH_BUTTON_IN);
button_samples += hal_gpio_read(MYNEWT_VAL(PUSH_BUTTON_IN));
}
if(i % 64 == 0) {
console_printf("step %d - %d\n", (i / (64)) + 1, (int)button_samples); console_flush();
Expand Down
9 changes: 9 additions & 0 deletions libs/pinetime_boot/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,12 @@
# Strings must be enclosed by '"..."'

syscfg.defs:
PUSH_BUTTON_IN:
description: Pin of the pushbutton (in)
value:
restrictions: $notnull

PUSH_BUTTON_OUT:
description: Pin of the pushbutton (out)
value:
restrictions: $notnull
8 changes: 4 additions & 4 deletions project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# under the License.
#

project.name: "my_project"
project.name: "pinetime_mcuboot_bootloader"

project.repositories:
- apache-mynewt-core
Expand All @@ -28,12 +28,12 @@ project.repositories:
#
repository.apache-mynewt-core:
type: github
vers: 1.8.0
vers: db05f05fedadc7782e6b857400779b8af69eb857-commit # Contains JEDEC ignore options
user: apache
repo: mynewt-core

repository.mcuboot:
type: github
vers: 1.5.0
user: JuulLabs-OSS
vers: c657cbea75f2bb1faf1fceacf972a0537a8d26dd-commit # 1.9.0
user: mcu-tools
repo: mcuboot
4 changes: 2 additions & 2 deletions scripts/nrf52/build-boot.cmd
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
:: Build Mynewt bootloader on Windows

:: Build the bootloader.
newt\newt.exe build nrf52_boot
newt\newt.exe build pinetime

:: Show the size.
newt\newt.exe size -v nrf52_boot
newt\newt.exe size -v pinetime
21 changes: 12 additions & 9 deletions scripts/nrf52/build-boot.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#!/usr/bin/env bash
# Build Mynewt bootloader on macOS and Linux

TARGET=${1:-pinetime}
echo "Building target $TARGET"

set -e # Exit when any command fails.
set -x # Echo all commands.

Expand All @@ -10,17 +13,17 @@ source paths.bash
arm-none-eabi-gcc --version

# Build the bootloader.
newt build nrf52_boot
newt build $TARGET

# Show the size.
newt size -v nrf52_boot
newt size -v $TARGET

arm-none-eabi-objcopy -I binary -O ihex bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf.bin bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf.hex
scripts/hex2c.py bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf.hex > reloader/src/boards/pinetime/bootloader.h
make -C reloader BOARD=pinetime
arm-none-eabi-objcopy -I binary -O ihex bin/targets/$TARGET/app/@mcuboot/boot/mynewt/mynewt.elf.bin bin/targets/$TARGET/app/@mcuboot/boot/mynewt/mynewt.elf.hex
scripts/hex2c.py bin/targets/$TARGET/app/@mcuboot/boot/mynewt/mynewt.elf.hex > reloader/src/boards/$TARGET/bootloader.h
make -C reloader build-$TARGET/reloader-mcuboot.zip all BOARD=$TARGET

set +x
echo "Bootloader firmware (elf) : bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.elf"
echo "Bootloader firmware (bin) : bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.bin"
echo "Bootloader firmware (hex) : bin/targets/nrf52_boot/app/@mcuboot/boot/mynewt/mynewt.hex"
echo "Reloader (DFU) : reloader/build-pinetime/reloader-mcuboot.zip"
echo "Bootloader firmware (elf) : bin/targets/$TARGET/app/@mcuboot/boot/mynewt/mynewt.elf"
echo "Bootloader firmware (bin) : bin/targets/$TARGET/app/@mcuboot/boot/mynewt/mynewt.bin"
echo "Bootloader firmware (hex) : bin/targets/$TARGET/app/@mcuboot/boot/mynewt/mynewt.hex"
echo "Reloader (DFU) : reloader/build-$TARGET/reloader-mcuboot.zip"
35 changes: 35 additions & 0 deletions targets/p8/pkg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# Target Settings
pkg.name: targets/p8
pkg.type: target
pkg.description:
pkg.author:
pkg.homepage:

# Package Dependencies: MCUBoot is dependent on these drivers and libraries.
pkg.deps:
- "libs/semihosting_console" # Semihosting Console
- "libs/pinetime_boot" # Render boot graphic and check for rollback. Comment out for Stub Bootloader.

# C compiler flags
pkg.cflags:
- -DMCUBOOT_HAVE_LOGGING=1 # So that sysinit() will be run, needed for displaying boot graphic
- -DDISABLE_SEMIHOSTING # Uncomment to disable Arm Semihosting. Must be uncommented for production.
# - -Os # Optimise for smallest size
6 changes: 5 additions & 1 deletion targets/nrf52_boot/syscfg.yml → targets/p8/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

# MCUBoot Bootloader Settings
# Target Settings

syscfg.vals:
BOOT_CUSTOM_START: 1 # Use custom boot function boot_custom_start()
Expand Down Expand Up @@ -54,3 +54,7 @@ syscfg.vals:
SANITY_INTERVAL: 1000 # The interval (in milliseconds) at which the sanity checks should run, should be at least 200ms prior to watchdog
WATCHDOG_INTERVAL: 2000 # The interval (in milliseconds) at which the watchdog should reset if not tickled, in ms

# Specific target hardware config

PUSH_BUTTON_IN: 17
PUSH_BUTTON_OUT: -1
File renamed without changes.
4 changes: 2 additions & 2 deletions targets/nrf52_boot/pkg.yml → targets/pinetime/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
# specific language governing permissions and limitations
# under the License.

# MCUBoot Package Settings
pkg.name: targets/nrf52_boot
# Target Settings
pkg.name: targets/pinetime
pkg.type: target
pkg.description:
pkg.author:
Expand Down
60 changes: 60 additions & 0 deletions targets/pinetime/syscfg.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.

# Target Settings

syscfg.vals:
BOOT_CUSTOM_START: 1 # Use custom boot function boot_custom_start()
OS_MAIN_STACK_SIZE: 1024 # Small stack size: 4 KB
MSYS_1_BLOCK_COUNT: 64 # Allocate MSYS buffers for Semihosting Console
HAL_ENABLE_SOFTWARE_BREAKPOINTS: 0 # In case of assertion failure, don't breakpoint. Must be set to 0 so that bootloader will reboot and won't hang in case of assertion failures.
MCU_DEBUG_IGNORE_BKPT: 1

###########################################################################
# Hardware Settings

SPIFLASH: 1 # Enable SPI Flash
SPI_0_MASTER: 1 # Enable SPI port 0 for ST7789 display and SPI Flash
I2C_1: 0 # Disable I2C port 1 for CST816S touch controller, BMA421 accelerometer, HRS3300 heart rate sensor
UART_0: 0 # Disable UART port to reduce ROM size

###########################################################################
# Common Settings for minimal ROM size

CONSOLE_COMPAT: 0 # Disable console input
CONSOLE_RTT: 0 # Disable RTT Console
CONSOLE_UART: 0 # Disable UART Console
LOG_CLI: 0 # Disable logging command-line interface
LOG_LEVEL: 255 # Disable logs
SENSOR_CLI: 0 # Disable sensor command-line interface
SENSOR_OIC: 0 # Disable sensor OIC functions
SHELL_CMD_HELP: 0 # Disable shell help
SHELL_OS_MODULE: 0 # Disable shell module
SHELL_TASK: 0 # Disable shell task
STATS_CLI: 0 # Disable statistics command-line interface
OS_SYSVIEW_TRACE_CALLOUT: 0 # Disable trace of callouts
OS_SYSVIEW_TRACE_EVENTQ: 0 # Disable trace of event queues
OS_SYSVIEW_TRACE_MUTEX: 0 # Disable trace of mutex
OS_SYSVIEW_TRACE_SEM: 0 # Disable trace of semaphores
BOOTUTIL_FEED_WATCHDOG: 1 # Enable watchdog feeding while performing a swap upgrade
SANITY_INTERVAL: 1000 # The interval (in milliseconds) at which the sanity checks should run, should be at least 200ms prior to watchdog
WATCHDOG_INTERVAL: 2000 # The interval (in milliseconds) at which the watchdog should reset if not tickled, in ms

# Specific target hardware config

PUSH_BUTTON_IN: 13
PUSH_BUTTON_OUT: 15
12 changes: 12 additions & 0 deletions targets/pinetime/target.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Bootloader Target Settings

# Bootloader will be based on MCUBoot. Select "apps/boot_stub" for the Stub Bootloader.
target.app: "@mcuboot/boot/mynewt" # Use MCUBoot, which doesn't support debugging
# target.app: "apps/boot_stub" # Use Stub Bootloader, which supports debugging

# Board Support Package (BSP) for the target
target.bsp: "hw/bsp/nrf52"

# Build with debug support. Or select "optimized" to remove debug support.
#target.build_profile: debug # For debugging
target.build_profile: optimized # For production

0 comments on commit 12d5a73

Please sign in to comment.