visual-embedded-rust
Connect PineTime to Raspberry Pi1️⃣ Carefully pry open the PineTime casing. Use tweezers to pivot the shiny battery gently to the side. Be careful not to break the red and black wires that connect the battery to the watch! 2️⃣ Just above the battery we see 4 shiny rings. This is the Serial Wire Debug (SWD) Port for PineTime. We’ll use this port to flash our firmware to PineTime. The 4 pins (from left to right) are SWDIO (Data I/O), SWDCLK (Clock), 3.3V, GND. 🛈 What is “flash memory” / “flashing” / “firmware”? Read this The exposed copper wire at the top centre of the photo is the Bluetooth antenna. Bend it upwards so that it doesn’t come into contact with anything. 3️⃣ At lower right we see a pad marked 5V. We’ll connect this pad to Raspberry Pi to charge the battery. If charging of the battery is not needed during development, we may leave5V disconnected. 4️⃣ Connect the SWD Port and the 5V Pad (optional) to the Raspberry Pi with Solid-Core Wire (22 AWG) and Female-To-Female Jumper Cables…
Based on https://pinout.xyz/ 5️⃣ We may use Raspberry Pi Zero, 1, 2, 3 or 4. 6️⃣ The PineTime touchscreen needs to be accessible during development, so I mounted PineTime on a $2 clear box cover from Daiso with Blu Tack and sticky tape. Remove PineTime Flash ProtectionPineTime is shipped with preloaded demo firmware. We need to erase the demo firmware and unprotect PineTime’s flash memory so that we may flash our own firmware. 🛈 What is “flash protection”? Read this 1️⃣ Power on the Raspberry Pi. Open a command prompt and enter the following…
Select Select At the command prompt, enter the following…
2️⃣ At the
If you see this error…
It means that the sub-repository for one of the dependencies jimtcl is temporarily down. You may download the pre-built 3️⃣ When the installation has completed, enter the following at the command prompt…
4️⃣ We should see If you see
Then the connection to the SWD Port is probably loose, check the pins. Also enter If you see this instead…
Then
5️⃣ Shut down and power off your Raspberry Pi. Wait 30 seconds for the red and green LEDs on your Pi to turn off. Power on your Pi. Enter the same commands at a command prompt…
6️⃣ We should see PineTime’s demo firmware has been erased and the flash protection has been removed. 🛈 What is OpenOCD? Why Raspberry Pi and not ROCK64 or Nvidia Jetson Nano? Read this Edit The Visual Rust ApplicationWe shall be using VSCode with the Visual Embedded Rust Extension to edit our Visual Rust application graphically. 🛈 What is VSCode? Is it related to Visual Studio? How is Microsoft involved? Read this 1️⃣ Launch VSCode by clicking the Raspberry Pi Menu (top left corner) → Programming → Code OSS Headmelted In VSCode, click Under When prompted to open the workspace, click Open Workspace When prompted to install Extension Recommendations, click Ignore the message 2️⃣ Install the Click Search for Install the extension 3️⃣ Enable the Visual Rust application... Browse to Modify the file such that
4️⃣ Edit the Visual Rust application... Browse to Click Use the Visual Editor to edit the Visual Rust application 5️⃣ After editing, save the Rust Source Code generated from Visual Rust application The Visual Rust application shows a button that increments a counter... Let's look at the blocks in the Visual Rust application... On StartUpon starting the Watch App, we initialise the This block generates the following Rust code...
Create AppWe create a Watch App with two Widgets...
This block generates the following Rust code...
On Label ShowThis block is called to generate the text that will be shown on the label We return the variable This block generates the following Rust code...
On Button PressThis block is called to when the button We increment the variable This block generates the following Rust code...
Build And Flash The FirmwareWe’ll be flashing the PineTime firmware that’s based on open-source Apache Mynewt embedded operating system. Mynewt OS contains two components that we shall flash to PineTime… Mynewt Bootloader: This is the C code that’s run whenever we power on PineTime. The Bootloader executes the Mynewt Application upon startup. Mynewt Application: Contains a Rust application that controls the PineTime functions, and low-level system functions written in C. The Bootloader and Application firmware image files may be found at these locations…
From https://github.com/lupyuen/pinetime-rust-mynewt/blob/master/hw/bsp/nrf52/bsp.yml 🛈 What is a Bootloader? Read this 1️⃣ At the lower left corner, there is a panel 2️⃣ In the Task Runner, click When the Terminal Panel appears, right-click the After the building the Bootloader, we should see Ignore the message The Bootloader only needs to be built once. 3️⃣ In the Task Runner, click After the building the Application, we should see If you see the message The Application needs to be rebuilt whenever a source file has been changed. 4️⃣ In the Task Runner, click After the creating the Firmware Image, we should see 5️⃣ In the Task Runner, click After flashing the Bootloader Firmware to PineTime, we should see
From https://github.com/lupyuen/pinetime-rust-mynewt/blob/master/logs/load-bootloader-pi.log The Bootloader only needs to be flashed once. 6️⃣ In the Task Runner, click After the flashing the Application Firmware to PineTime, we should see
From https://github.com/lupyuen/pinetime-rust-mynewt/blob/master/logs/load-application-pi.log 7️⃣ Our Visual Rust application starts running on PineTime 8️⃣ Click the Trash icon 🗑 near the top right to terminate the application. If we click the Close icon ❌ instead of the Trash icon, the next flash or debug command will fail. Debug The Firmware1️⃣ Build the application: In the Task Runner, click 2️⃣ Click This starts the VSCode Debugger and automatically flashes our updated firmware to PineTime. 3️⃣ Click In the Output Panel, select The debugging messages will be displayed here. 4️⃣ The program has paused at first line of code in our firmware, the Reset Handler. In the Debug Toolbar, click 🛈 What’s a Reset Handler? Read this 5️⃣ The debugger now pauses at the first line of the main function that’s defined in rust/app/src/lib.rs This is the first line of Rust code in our Rust Application, which will call test_display in a while. In the Debug Toolbar, click Continue or press F5 🛈 What’s a main function? Read this Edit, Build and Debug the Visual Rust Application on WindowsTo edit, build and debug the Visual Rust Application on Windows, follow these steps... [Windows] Connect PineTime to ST-LinkIf we’re doing serious development with PineTime, I recommend getting an ST-Link v2 USB dongle ($2) that connects PineTime directly to our Windows, macOS or Linux computer. ST-Link allows us to flash PineTime directly from our computer, and it even supports firmware debugging (setting breakpoints, checking values of variables at runtime, …) Here’s how we connect PineTime to ST-Link…
Before connecting ST-Link to our Windows computer, the ST-Link USB driver should be installed... Download the ST-Link USB driver from ST-Link Driver Website (email registration required)… https://www.st.com/en/development-tools/stsw-link009.html Click Unzip the downloaded file. Double-click the driver installer:
[Windows] Remove PineTime Flash ProtectionThis must be done with a Raspberry Pi, not on Windows, because ST-Link is a High-Level Adapter that doesn't implement all flash commands. Follow the instructions above for Raspberry Pi. [Windows] Install PineTime Build Tools1️⃣ Download the pinetime-rust-mynewt.7z file attached below… https://github.com/lupyuen/pinetime-rust-mynewt/releases/download/v3.0.1/pinetime-rust-mynewt.7z Expand the https://www.7-zip.org/download.html 2️⃣ Click here to install Build Tools For Visual Studio 2019: https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2019 Click the Select the following components:
3️⃣ Install Click the link provided to download Launch the downloaded file If you see the message Click Click At the
4️⃣ Open the Command Prompt and enter…
5️⃣ Install GNU Arm Embedded Toolchain for Windows from Arm Developer Website… Select this option at the last install step:
6️⃣ Install VSCode… https://code.visualstudio.com/ [Windows] Edit The Visual Rust Application1️⃣ Launch VSCode Click Select the downloaded folder When prompted to open the workspace, click Open Workspace When prompted to install Extension Recommendations, click 2️⃣ Install the Click Search for Install the extension 3️⃣ Enable the Visual Rust application... Browse to Modify the file such that
4️⃣ Edit the Visual Rust application... Browse to Click Use the Visual Editor to edit the Visual Rust application 5️⃣ After editing, save the [Windows] Build And Flash The Firmware1️⃣ At the lower left corner, there is a panel 2️⃣ In the Task Runner, click When the Terminal Panel appears, right-click the After the building the Bootloader, we should see Ignore the message The Bootloader only needs to be built once. 3️⃣ In the Task Runner, click After the building the Application, we should see If you see the message The Application needs to be rebuilt whenever a source file has been changed. Note: When we run 4️⃣ In the Task Runner, click After the creating the Firmware Image, we should see 5️⃣ In the Task Runner, click After flashing the Bootloader Firmware to PineTime, we should see
From https://github.com/lupyuen/pinetime-rust-mynewt/blob/master/logs/load-bootloader-pi.log The Bootloader only needs to be flashed once. 6️⃣ In the Task Runner, click After the flashing the Application Firmware to PineTime, we should see
From https://github.com/lupyuen/pinetime-rust-mynewt/blob/master/logs/load-application-pi.log 7️⃣ Our Visual Rust application starts running on PineTime 8️⃣ Click the Trash icon 🗑 near the top right to terminate the application. If we click the Close icon ❌ instead of the Trash icon, the next flash or debug command will fail. [Windows] Debug The Firmware1️⃣ Build the application: In the Task Runner, click The build script will also overwrite the default 2️⃣ Click This starts the VSCode Debugger and automatically flashes our updated firmware to PineTime. 3️⃣ Click In the Output Panel, select The debugging messages will be displayed here. 4️⃣ The program has paused at first line of code in our firmware, the Reset Handler. In the Debug Toolbar, click 🛈 What’s a Reset Handler? Read this 5️⃣ The debugger now pauses at the first line of the main function that’s defined in rust/app/src/lib.rs This is the first line of Rust code in our Rust Application, which will call test_display in a while. In the Debug Toolbar, click Continue or press F5 🛈 What’s a main function? Read this TODO: Edit, Build and Debug the Visual Rust Application on macOS |