2019-12-02 21:42:10 +03:00
# PineTime
2019-12-05 21:39:52 +03:00
> The PineTime is a free and open source smartwatch capable of running custom-built open operating systems. Some of the notable features include a heart rate monitor, a week-long battery as well as a capacitive touch IPS display that is legible in direct sunlight. It is a fully community driven side-project, which means that it will ultimately be up to the developers and end-users to determine when they deem the PineTime ready to ship.
2019-12-05 21:44:19 +03:00
2019-12-05 21:39:52 +03:00
> We envision the PineTime as a companion for not only your PinePhone but also for your favorite devices — any phone, tablet, or even PC.
2019-11-17 22:47:04 +03:00
2019-12-05 21:39:52 +03:00
*https://www.pine64.org/pinetime/*
The goal of this project is to build a firmware designed for the PineTime around FreeRTOS.
It's coded using a mix of C and C++ : the NRF52-SDK and FreeRTOS are written in C, but I prefer to write my own code in C++.
Please **NOTE : for now, this project is in heavy development state** . Feel free to use it, contribute to it or fork it !
This project is based on https://github.com/JF002/nrf52-baseproject.
It's a CMake project that configure everything needed to build applications for the PineTime (https://wiki.pine64.org/index.php/PineTime).
2019-11-17 22:47:04 +03:00
I tested this project (compile only) with the following versions:
* gcc-arm-none-eabi-8-2019-q3-update (from https://developer.arm.com/tools-and-software/open-source-software/developer-tools/gnu-toolchain/gnu-rm/downloads)
* nRF5_SDK_15.3.0_59ac345 (from https://www.nordicsemi.com/Software-and-Tools/Software/nRF5-SDK)
2019-12-05 21:39:52 +03:00
I've tested this project on the actual PineTime hardware.
## Current state
2020-03-02 23:30:45 +03:00
![Pinetime ](./images/v0.3.0.png "Pinetime" )
2019-12-27 19:13:09 +03:00
2019-12-05 21:39:52 +03:00
* Project builds and runs on the Pinetime;
* Logs available via JLink RTT;
2020-03-02 23:30:45 +03:00
* SPI (DMA & IRQ based) LCD driver;
2020-05-16 17:13:22 +03:00
* Open source BLE stack : [NimBLE ](https://github.com/apache/mynewt-nimble );
* BLE advertising and connection connection;
2020-03-02 23:30:45 +03:00
* BLE CTS client (retrieves the time from the connected device if it implements a CTS server);
2020-03-29 16:19:39 +03:00
* Push button to go to disable screen (and go to low power mode) / enable screen (and wake-up) and UI navigation
2020-03-02 23:30:45 +03:00
* Touch panel support;
2020-03-29 16:19:39 +03:00
* Rich user interface (using [LittleVGL ](https://littlevgl.com/ )) via display, touchpanel and push button.
* Digital watch face and 4 demo applications (spinning meter, analog gauche, push button and message box);
* Watchdog (automatic reset in case of firmware crash) and reset support (push and hold the button for 7 - 10s);
2020-05-24 17:36:04 +03:00
* BLE Notification support (still Work-In-Progress, [companion app ](https://github.com/JF002/gobbledegook ) needed);
2020-05-16 17:13:22 +03:00
* Supported by companion app [Amazfish ](https://openrepos.net/content/piggz/amazfish ) (time synchronization and notifications are integrated).
2020-05-24 17:36:04 +03:00
* **[EXPERIMENTAL]** Firmware update (OTA) via BLE.
2020-04-05 18:03:06 +03:00
## Documentation
* [BLE implementation and API ](./doc/ble.md )
2020-05-24 17:36:04 +03:00
* [Bootloader and DFU ](./bootloader/README.md )
2019-12-26 20:33:40 +03:00
2020-01-17 17:02:43 +03:00
## Stub using NRF52-DK
2020-01-02 17:19:28 +03:00
![Pinetime stub ](./images/pinetimestub1.jpg "PinetimeStub" )
See [this page ](./doc/PinetimeStubWithNrf52DK.md )
2019-11-17 22:47:04 +03:00
2019-12-26 14:02:01 +03:00
## How to build
2019-11-17 22:47:04 +03:00
* Download and unzip arm-none-eabi and NRF52 SDK
* Clone this repo
2020-01-17 16:56:56 +03:00
* **[JLINK]** Call CMake with the following command line argument
2019-11-17 22:47:04 +03:00
2020-01-26 18:40:45 +03:00
- -DARM_NONE_EABI_TOOLCHAIN_PATH=[Path to the toolchain directory]
- -DNRF5_SDK_PATH=[Path to the SDK directory]
- -DUSE_JLINK=1
- -DNRFJPROG=[Path to NRFJProg executable]
2019-11-17 22:47:04 +03:00
2020-01-17 17:03:34 +03:00
* OR
* **[GDB CLIENT (if you use a BlackMagicProbe, for example)]** Call CMake with the following command line argument
2020-01-17 16:56:56 +03:00
- -DARM_NONE_EABI_TOOLCHAIN_PATH=[Path to the toolchain directory]
- -DNRF5_SDK_PATH=[Path to the SDK directory]
- -DUSE_GDB_CLIENT=1
- -DGDB_CLIENT_BIN_PATH=[Path to arm-none-eabi-gdb executable]
- -DGDB_CLIENT_TARGET_REMOTE=[Target remote connetion string. Ex : /dev/ttyACM0]
2020-01-26 18:40:45 +03:00
* OR
* **[OPENOCD (if you use a STlink v2 clone, for example)]** Call CMake with the following command line argument
- -DARM_NONE_EABI_TOOLCHAIN_PATH=[Path to the toolchain directory]
- -DNRF5_SDK_PATH=[Path to the SDK directory]
- -DUSE_OPENOCD=1
* Optionally, if you want to use a another version then whats on your path
- -DOPENOCD_BIN_PATH=[path to openocd]
2020-01-17 16:56:56 +03:00
JLINK
2019-11-17 22:47:04 +03:00
```
$ mkdir build
$ cd build
2020-01-17 16:56:56 +03:00
$ cmake -DCMAKE_BUILD_TYPE=Debug -DARM_NONE_EABI_TOOLCHAIN_PATH=... -DNRF5_SDK_PATH=... -DUSE_JLINK=1 -DNRFJPROG=... ../
```
GDB (Back Magic Probe)
```
$ mkdir build
$ cd build
2020-06-16 21:44:55 +03:00
$ cmake -DARM_NONE_EABI_TOOLCHAIN_PATH=... -DNRF5_SDK_PATH=... -DUSE_GDB_CLIENT=1 -DGDB_CLIENT_BIN_PATH=... -DGDB_CLIENT_TARGET_REMOTE=... ../
2020-01-26 18:40:45 +03:00
```
OpenOCD (STlink v2 clones)
```
$ mkdir build
$ cd build
2020-06-16 21:44:55 +03:00
$ cmake -DARM_NONE_EABI_TOOLCHAIN_PATH=... -DNRF5_SDK_PATH=... -DUSE_OPENOCD=1 -DGDB_CLIENT_BIN_PATH=[optional] ../
2019-11-17 22:47:04 +03:00
```
2020-06-16 22:02:03 +03:00
**Note** : By default, the build configuration *Release* is applied. It means that the code is built with optimisations enabled (-O3). If you wanto to compile in *Debug* mode to ease debugging and step-by-step debugging, specify the CMake variable `CMAKE_BUILD_TYPE` in the CMake command line:
```
-DCMAKE_BUILD_TYPE=Debug
```
2019-11-17 22:47:04 +03:00
* Make
```
2020-01-17 16:56:56 +03:00
$ make -j pinetime-app
2019-11-17 22:47:04 +03:00
```
2020-01-17 16:56:56 +03:00
## How to program
2020-03-03 22:51:42 +03:00
### Using make
2020-01-17 16:56:56 +03:00
* Erase
2020-01-17 17:02:43 +03:00
2020-01-17 16:56:56 +03:00
```
$ make FLASH_ERASE
```
2020-05-24 17:36:04 +03:00
* Flash application
2020-01-17 17:02:43 +03:00
2020-01-17 16:56:56 +03:00
```
$ make FLASH_pinetime-app
```
2020-01-17 17:02:43 +03:00
* For your information : list make targets :
2020-01-17 16:56:56 +03:00
```
$ make help
```
2020-03-03 22:51:42 +03:00
### Using GDB and a Black Magic Probe (BMP)
You can use this procedure if you want to flash the firmware (from a release, for example) without having to build the project locally.
Enter the following command into GDB:
```
target extended-remote /dev/ttyACM0
monitor swdp_scan
attach 1
file ./pinetime-app-full.hex
load
run
```
*Note* : Sometimes, I need to run this procedure 2 times : the first time, the screen stays blank, and the second time, it works. This is something I'll need to debug...
Example:
```
$ /home/jf/nrf52/gcc-arm-none-eabi-8-2019-q3-update/bin/arm-none-eabi-gdb
(gdb) target extended-remote /dev/ttyACM0
Remote debugging using /dev/ttyACM0
(gdb) monitor swdp_scan
Target voltage: ABSENT!
Available Targets:
No. Att Driver
1 Nordic nRF52 M3/M4
2 Nordic nRF52 Access Port
(gdb) attach 1
Attaching to Remote target
warning: No executable has been specified and target does not support
determining executable automatically. Try using the "file" command.
0xfffffffe in ?? ()
(gdb) file ./pinetime-app-full.hex
A program is being debugged already.
Are you sure you want to change the file? (y or n) y
Reading symbols from ./pinetime-app-full.hex...
(No debugging symbols found in ./pinetime-app-full.hex)
(gdb) load
Loading section .sec1, size 0xb00 lma 0x0
Loading section .sec2, size 0xf000 lma 0x1000
Loading section .sec3, size 0x10000 lma 0x10000
Loading section .sec4, size 0x5150 lma 0x20000
Loading section .sec5, size 0xa000 lma 0x26000
Loading section .sec6, size 0x10000 lma 0x30000
Loading section .sec7, size 0xdf08 lma 0x40000
Start address 0x0, load size 314200
Transfer rate: 45 KB/sec, 969 bytes/write.
```
2020-01-17 16:56:56 +03:00
2019-11-17 22:47:04 +03:00
## RTT
RTT is a feature from Segger's JLink devices that allows bidirectionnal communication between the debugger and the target.
This feature can be used to get the logs from the embedded software on the development computer.
* Program the MCU with the code (see above)
* Start JLinkExe
```
$ JLinkExe -device nrf52 -if swd -speed 4000 -autoconnect 1
```
* Start JLinkRTTClient
```
$ JLinkRTTClient
2019-12-22 00:31:06 +03:00
```
## Tools
2019-12-26 14:02:01 +03:00
- https://github.com/eliotstock/memory : display the memory usage (FLASH/RAM) using the .map file from GCC.
2020-05-16 17:13:22 +03:00
## BLE connection and time synchronization
2019-12-26 14:02:01 +03:00
At runtime, BLE advertising is started. You can then use a smartphone or computer to connect and bond to your Pinetime.
As soon as a device is bonded, Pinetime will look for a **CTS** server (**C**urrent **T**ime **S**ervice) on the connected device.
2020-03-29 16:19:39 +03:00
### Using Android and NRFConnect
2019-12-26 14:02:01 +03:00
Here is how to do it with an Android smartphone running NRFConnect:
* Build and program the firmware on the Pinetime
* Install NRFConnect (https://www.nordicsemi.com/Software-and-Tools/Development-Tools/nRF-Connect-for-desktop)
* Start NRFConnect and create a CTS server :
- Tap the hamburger button on the top left and select "Configure GATT server"
- Tap "Add service" on the bottom
- Select server configuration "Current Time Service" and tap OK
* Go back to the main screen and scan for BLE devices. A device called "PineTime" should appear
* Tap the button "Connect" next to the PineTime device. It should connect to the PineTime and switch to a new tab.
2020-05-16 17:13:22 +03:00
* If a CTS server is found, the Pinetime should update its time with the time provided by the server.
2020-03-29 16:19:39 +03:00
### Using Linux and bluetoothctl
* Ensure that your bluetooth controller is enabled and working fine. I've tested this on a x86 Debian computer and on a RaspberryPi 3.
* Run bluetoothctl as root : `sudo bluetoothctl`
* Enter the following commands:
* `scan on` and wait for you Pinetime to be detected. Note the BLE MAC address
* `scan off'
* `trust <MAC ADDRESS>`
* `pair <MAC ADDRESS>`
* Wait for some time, and the connection should be established.
**NOTE : ** The commands above establish a BLE connection between your PC, but the time synchronization and notifications won't work because there is not CTS or ANS server running. I'm currently working on an application that'll provide both of these servers.
### Troubleshooting
If the connection cannot be established, or the time synchronization does not work, try the following steps.
On Android:
* Disable and re-enable your bluetooth device
* In NRFConnect, in the device tab corresponding to your pinetime, tap on the menu on the top left of the screen and select "Delete bond information".
* Then re-try to bond/connect.
On Linux:
* Reset the bluetooth device : `sudo hciconfig hci0 reset`
* Restart the Bluetooth service : `sudo systemctl restart bluetooth.service`
* In bluetootctl:
* `remove <MAC ADDRESS>`
* `trust <MAC ADDRESS>`
* `pair <MAC ADDRESS>`
Note that the current firmware only advertise for the first 3 minutes. If you cannot connect after more than 3 minutes, try resetting the device (push the button and hold it for 7-10 seconds).