/pinetime-rust-mynewt

PineTime Smart Watch firmware based on Rust and Apache Mynewt OS

Primary LanguageAssemblyApache License 2.0Apache-2.0

PineTime Smart Watch Firmware with Apache Mynewt and Embedded Rust

This master branch contains the firmware source code for PineTime Smart Watch with Apache Mynewt and Embedded Rust. Refer to the articles...

  1. Visual Rust for PineTime Smart Watch

  2. Build and Flash Rust+Mynewt Firmware for PineTime Smart Watch

  3. Debug Rust+Mynewt Firmware for PineTime on Raspberry Pi

  4. Sneak Peek of PineTime Smart Watch… And why it's perfect for teaching IoT

  5. Building a Rust Driver for PineTime’s Touch Controller

  6. Porting [druid] Rust Widgets to PineTime Smart Watch

  7. Optimising PineTime’s Display Driver with Rust and Mynewt

  8. CHIP-8 Game Emulator in Rust for PineTime Smart Watch

  9. My First Week As Embedded FOSS Advocate

  10. Rust Documentation

The code structure is similar to the earlier article on nRF52...

Coding nRF52 with Rust and Apache Mynewt on Visual Studio Code

Build Instructions

If you are building from this repository from scratch instead of the Released Packages, here are the steps:

  1. Install rustup with support for nightly target thumbv7em-none-eabihf.

    Follow the instructions at https://rustup.rs/

    Then execute...

    rustup default nightly
    rustup update
    rustup target add thumbv7em-none-eabihf
  2. Install Arm toolchain gcc-arm-none-eabi and the newt build tool for Mynewt. Refer to this script...

    scripts/install-pi.sh

  3. Clone this repository...

    git clone --recursive https://github.com/lupyuen/pinetime-rust-mynewt
  4. repos folder should contain the Mynewt source code. If your repos folder is empty, install the Mynewt source code with the newt install command:

    cd pinetime-rust-mynewt
    newt install

    Ignore the error Error: Error updating "mcuboot"

  5. Build the bootloader...

    cd pinetime-rust-mynewt
    scripts/nrf52/build-boot.sh
  6. Build the application...

    scripts/build-app.sh

    If you see the error Undefined main, run scripts/build-app.sh again. It should fix the error.

  7. Create the application firmware image...

    scripts/nrf52/image-app.sh
  8. Flash the bootloader...

    scripts/nrf52-pi/flash-boot.sh
  9. Flash the application and run it...

    scripts/nrf52-pi/flash-app.sh
  10. You may need to edit the scripts to set the right path of OpenOCD.

    Also for Windows, the ST-Link interface for OpenOCD is stlink-v2.cfg instead of stlink.cfg.

  11. Check these articles in case of problems...

Fixes for Mynewt type conversion build warnings

These fixes should be applied manually when upgrading Mynewt or installing Mynewt from scratch. They suppress the compiler warning messages that stop the Mynewt build for C++ source files.

1️⃣ repos/apache-mynewt-core/kernel/os/include/os/os_mutex.h line 122

    return mu->mu_level;

Change to

    return (os_error_t) mu->mu_level;

2️⃣ repos/apache-mynewt-core/hw/sensor/include/sensor/sensor.h line 847

    return (sensor->s_types & sensor->s_mask & type);

Change to

    return (sensor_type_t) (sensor->s_types & sensor->s_mask & type);

3️⃣ repos/apache-mynewt-core/encoding/tinycbor/include/tinycbor/cbor.h line 201

    {   return encoder->writer->bytes_written; }

Change to

    {   return (CborError) encoder->writer->bytes_written; }

Installation, Build, Flash and Debug Logs

Sample logs for Windows may be found in the logs folder

Contents

This repository contains...

rust: Rust Application

Cargo.toml: Rust Build Settings

.cargo: Rust Target Settings

my_sensor_app: Mynewt Application Stub

boot_stub: Mynewt Bootloader Stub

adc_stm32f1: Mynewt Driver for ADC on STM32 F103 (Blue Pill). Used by temp_stm32 internal temperature sensor.

adc_stm32l4: Mynewt Driver for ADC on STM32 L476. Used by temp_stm32 internal temperature sensor.

bc95g: Mynewt Driver for Quectel BC95 NB-IoT module

buffered_serial: Buffered Serial Library used by bc95g NB-IoT driver and gps_l70r GPS driver

custom_sensor: Custom Sensor Definitions for Raw Temperature and Geolocation

esp8266: Mynewt Driver for ESP8266 WiFi module

gps_l70r: Mynewt Driver for Quectel L70-R GPS module

hmac_prng: HMAC pseudorandom number generator with entropy based on internal temperature sensor

low_power: Low Power functions for STM32 F103 (Blue Pill)

mynewt_rust: Helper functions for hosting Rust on Mynewt

nrf24l01: Mynewt Driver for nRF24L01

remote_sensor: Mynewt Driver for Remote Sensor

rust_app: Stub library that will be replaced by the compiled Rust application and Rust crates

rust_libcore: Stub library that will be replaced by the Rust Core Library

semihosting_console: Mynewt Console for Arm Semihosting

sensor_coap: Sensor CoAP Library

sensor_network: Sensor Network Library

temp_stm32: Mynewt Driver for Internal Temperature Sensor on STM32

temp_stub: Mynewt Driver for Stub Temperature Sensor that returns a fixed value

tiny_gps_plus: TinyGPS++ Library ported from Arduino. Used by gps_l70r GPS driver.

scripts: Install, build and deploy scripts

.vscode: Visual Studio Code macros for install, build and deploy

How This Application Was Created

The Windows version of the newt command-line tool in newt/newt.exe was created from

github.com/lupyuen/mynewt-newt

The Mynewt application was originally based on:

mynewt.apache.org/latest/tutorials/sensors/sensor_thingy_lis2dh12_onb.html

mynewt.apache.org/latest/tutorials/sensors/sensor_nrf52_bno055.html

cd /mnt/c
newt new stm32bluepill-mynewt-sensor
cd stm32bluepill-mynewt-sensor
cat project.yml

newt install
newt pkg new -t app apps/my_sensor_app
newt pkg new -t lib libs/semihosting_console

newt target create bluepill_boot
newt target set bluepill_boot bsp=@apache-mynewt-core/hw/bsp/bluepill
newt target set bluepill_boot app=@apache-mynewt-core/apps/boot
newt target set bluepill_boot build_profile=optimized

newt target create bluepill_my_sensor
newt target set bluepill_my_sensor bsp=@apache-mynewt-core/hw/bsp/bluepill
newt target set bluepill_my_sensor app=apps/my_sensor_app
newt target set bluepill_my_sensor build_profile=debug

project.yml should contain

project.name: "my_project"

project.repositories:
    - apache-mynewt-core

repository.apache-mynewt-core:
    type: github
    vers: 1.7.0
    user: apache
    repo: mynewt-core