Skip to content

Commit

Permalink
Merge pull request #1 from JF002/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
JF002 authored Jan 3, 2021
2 parents 16e5b05 + a2d2f93 commit 53918c7
Show file tree
Hide file tree
Showing 10 changed files with 482 additions and 70 deletions.
19 changes: 17 additions & 2 deletions libs/pinetime_boot/include/pinetime_boot/pinetime_boot.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,36 @@
extern "C" { // Expose the types and functions below to C functions.
#endif

// Colors
#define BLACK 0
#define WHITE 0xffff
#define RED 0xF800
#define BLUE 0x001F
#define GREEN 0x07E0

/// Init the display and render the boot graphic. Called by sysinit() during startup, defined in pkg.yml.
void pinetime_boot_init(void);

/// Write a converted graphic file to SPI Flash
int pinetime_boot_write_image(void);

/// Display the image in SPI Flash to ST7789 display controller
/// Display the boot logo to ST7789 display controller
int pinetime_boot_display_image(void);

/// Display the boot logo to ST7789 display controller using 2 colors. The first x lines (x = colorLine)
/// will be drawn in color1, the rest in color2.
int pinetime_boot_display_image_colors(uint16_t color1, uint16_t color2, uint8_t colorLine);

/// Display the bootloader version to ST7789 display controller
int pinetime_version_image(void);

/// Clear the display
void pinetime_clear_screen(void);

/// Check whether the watch button is pressed
void pinetime_boot_check_button(void);

int pinetime_boot_display_image_colors(uint16_t color1, uint16_t color2, uint8_t colorLine);


#ifdef __cplusplus
}
Expand Down
2 changes: 2 additions & 0 deletions libs/pinetime_boot/include/pinetime_boot/pinetime_factory.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef __PINETIME_FACTORY_H__
#define __PINETIME_FACTORY_H__

/// Copy the recovery firmware from the external SPI Flash memory to the secondary slot.
/// It'll be installed in the primary slot by MCUBoot.
void restore_factory(void);


Expand Down
75 changes: 40 additions & 35 deletions libs/pinetime_boot/src/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@

// ST7789 Colour Settings
#define INVERTED 1 // Display colours are inverted
#define RGB 1 // Display colours are RGB
#define RGB 1 // Display colours are RGB

// Flash Device for Image
#define FLASH_DEVICE 1 // 0 for Internal Flash ROM, 1 for External SPI Flash
Expand Down Expand Up @@ -105,67 +105,70 @@ static int write_data(const uint8_t *data, uint16_t len);
static int transmit_spi(const uint8_t *data, uint16_t len);

/// Buffer for reading flash and writing to display
static uint8_t flash_buffer[240*2];
static uint8_t flash_buffer[COL_COUNT * BYTES_PER_PIXEL];

int pinetime_display_image_colors(struct imgInfo* info, int posx, int posy, uint16_t color1, uint16_t color2, uint8_t colorLine) {
/// Display the image described by info to the ST7789 display controller using 2 colors. The first x lines (x = colorLine)
/// will be drawn in color1, the rest in color2.
int pinetime_display_image_colors(struct imgInfo* info, int posX, int posY, uint16_t color1, uint16_t color2, uint8_t colorLine) {
int rc;
int y = 0;
uint16_t bp = 0;
uint16_t fg = 0xffff;
const uint16_t bg = 0;
uint16_t color = bg;
uint16_t trueColor = color;
for (int i=0; i<info->dataSize; i++) {
uint8_t rl = info->data[i];
while (rl) {
flash_buffer[bp] = trueColor >> 8;
flash_buffer[bp + 1] = trueColor & 0xff;
bp += 2;
rl -= 1;

if (bp >= (info->width*2)) {
rc = set_window(posx, y+posy, posx+info->width-1, y+posy); assert(rc == 0);
uint16_t bufferIndex = 0;
uint8_t isBackground = 1;
const uint16_t backgroundColor = BLACK;
uint16_t trueColor = backgroundColor;

for (int i = 0; i < info->dataSize; i++) {
uint8_t runLength = info->data[i];
while (runLength) {
flash_buffer[bufferIndex] = trueColor >> 8;
flash_buffer[bufferIndex + 1] = trueColor & 0xff;
bufferIndex += BYTES_PER_PIXEL;
runLength -= 1;

if (bufferIndex >= (info->width * BYTES_PER_PIXEL)) {
rc = set_window(posX, y + posY, posX + info->width - 1, y + posY); assert(rc == 0);

// Write Pixels (RAMWR): st7735_lcd::draw() → set_pixel()
rc = write_command(RAMWR, NULL, 0); assert(rc == 0);
rc = write_data(flash_buffer, info->width*2); assert(rc == 0);
bp = 0;
rc = write_data(flash_buffer, info->width * BYTES_PER_PIXEL); assert(rc == 0);
bufferIndex = 0;
y += 1;
}
}

if (color == bg) {
color = fg;
if (isBackground) {
isBackground = 0;
trueColor = (y < colorLine) ? color1 : color2;
}
else {
color = bg;
trueColor = color;
isBackground = 1;
trueColor = backgroundColor;
}
if(y >= info->height)
break;
}
return 0;
}

/// Clear the display
void pinetime_clear_screen(void) {
int rc = 0;
for(int i = 0 ; i < 240*2; i++) {
for(int i = 0 ; i < COL_COUNT * BYTES_PER_PIXEL; i++) {
flash_buffer[i] = 0;
}
for(int i = 0; i < 240; i++) {
rc = set_window(0, i, 239, i); assert(rc == 0);
for(int i = 0; i < ROW_COUNT; i++) {
rc = set_window(0, i, COL_COUNT-1, i); assert(rc == 0);
rc = write_command(RAMWR, NULL, 0); assert(rc == 0);
rc = write_data(flash_buffer, 240*2); assert(rc == 0);
rc = write_data(flash_buffer, COL_COUNT * BYTES_PER_PIXEL); assert(rc == 0);
}
}

int pinetime_display_image(struct imgInfo* info, int posx, int posy) {
return pinetime_display_image_colors(info, posx, posy, 0xffff, 0xffff, 0);
/// Display the image described by info at position (posX, posY) using default color (black and white)
int pinetime_display_image(struct imgInfo* info, int posX, int posY) {
return pinetime_display_image_colors(info, posX, posY, WHITE, WHITE, 0);
}

/// Display the image in SPI Flash to ST7789 display controller.
/// Derived from https://github.com/lupyuen/pinetime-rust-mynewt/blob/main/logs/spi-non-blocking.log
/// Display the boot logo to ST7789 display controller
int pinetime_boot_display_image(void) {
console_printf("Displaying boot logo...\n"); console_flush();

Expand All @@ -175,14 +178,16 @@ int pinetime_boot_display_image(void) {
return pinetime_display_image(&bootLogoInfo, 0, 0);
}

/// Display the boot logo to ST7789 display controller using 2 colors. The first x lines (x = colorLine)
/// will be drawn in color1, the rest in color2.
int pinetime_boot_display_image_colors(uint16_t color1, uint16_t color2, uint8_t colorLine) {
return pinetime_display_image_colors(&bootLogoInfo, 0, 0, color1, color2, colorLine);
}


/// Display the bootloader version to ST7789 display controller on the bottom of the display (centered)
int pinetime_version_image(void) {
console_printf("Displaying version image...\n"); console_flush();
return pinetime_display_image(&versionInfo, 120 - (versionInfo.width/2), 240 - (versionInfo.height));
return pinetime_display_image(&versionInfo, (COL_COUNT/2) - (versionInfo.width/2), ROW_COUNT - (versionInfo.height));
}

/// Set the ST7789 display window to the coordinates (left, top), (right, bottom)
Expand Down Expand Up @@ -316,7 +321,7 @@ static int transmit_spi(const uint8_t *data, uint16_t len) {
// Select the device
hal_gpio_write(DISPLAY_CS, 0);
// Send the data
int rc = hal_spi_txrx(DISPLAY_SPI,
int rc = hal_spi_txrx(DISPLAY_SPI,
(void *) data, // TX Buffer
NULL, // RX Buffer (don't receive)
len); // Length
Expand Down
55 changes: 28 additions & 27 deletions libs/pinetime_boot/src/graphic.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,40 +110,41 @@ struct imgInfo bootLogoInfo = {
bootLogoRle
};

// /home/jf/nrf52/Pinetime/tools/rle_encode.py /home/jf/nrf52/pinetime-rust-mynewt/libs/pinetime_boot/src/version-1.2.3.png --c
// /home/jf/nrf52/Pinetime/tools/rle_encode.py /home/jf/nrf52/pinetime-rust-mynewt/libs/pinetime_boot/src/version-0.0.1.png --c
static const uint8_t versionRle[] = {
0x59, 0x56, 0x2, 0x1, 0x3, 0xc, 0x3, 0xa, 0x1, 0x38, 0x2, 0x1,
0x3, 0xc, 0x3, 0x9, 0x2, 0x14, 0x6, 0x13, 0x6, 0x5, 0x2, 0x2,
0x3, 0xb, 0x2, 0xa, 0x2, 0x13, 0x9, 0xf, 0xa, 0x3, 0x2, 0x2,
0x3, 0xa, 0x3, 0x9, 0x3, 0x12, 0x4, 0x3, 0x4, 0xd, 0x5, 0x2,
0x5, 0x2, 0x2, 0x2, 0x3, 0xa, 0x3, 0x6, 0x6, 0x11, 0x3, 0x6,
0x4, 0xc, 0x3, 0x6, 0x3, 0x2, 0x2, 0x3, 0x3, 0x9, 0x2, 0x6,
0x7, 0x11, 0x3, 0x7, 0x3, 0xc, 0x2, 0x8, 0x2, 0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 0x10,
0x2, 0xa, 0x2, 0x7, 0x2, 0x3, 0x2, 0x7, 0x2, 0x9, 0x2, 0xb,
0x2, 0x9, 0x2, 0x10, 0x2, 0xa, 0x2, 0x7, 0x2, 0x3, 0x2, 0x8,
0x2, 0x7, 0x2, 0xd, 0x2, 0x7, 0x2, 0x11, 0x2, 0xa, 0x2, 0x8,
0x2, 0x1, 0x2, 0x9, 0x2, 0x7, 0x2, 0xd, 0x2, 0x7, 0x2, 0x11,
0x2, 0xa, 0x2, 0x8, 0x2, 0x1, 0x2, 0xa, 0x2, 0x5, 0x2, 0x6,
0x2, 0x7, 0x2, 0x5, 0x2, 0x6, 0x2, 0xa, 0x2, 0xa, 0x2, 0x8,
0x5, 0xa, 0x9, 0x6, 0x2, 0x7, 0x9, 0x6, 0x2, 0x6, 0xa, 0x6,
0x2, 0x9, 0x3, 0xd, 0x5, 0x8, 0x2, 0x9, 0x5, 0x8, 0x2, 0x6,
0xa, 0x6, 0x2, 0x56, 0x2, 0x56, 0x59,

};


struct imgInfo versionInfo = {
88,
26,
299,
295,
versionRle
};

Expand Down
13 changes: 8 additions & 5 deletions libs/pinetime_boot/src/pinetime_boot.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#include "pinetime_boot/pinetime_boot.h"
#include "pinetime_boot/pinetime_factory.h"
#include "pinetime_boot/pinetime_delay.h"
#include <hal/hal_watchdog.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
Expand Down Expand Up @@ -71,19 +72,20 @@ void pinetime_boot_init(void) {
}
if(i % 64 == 0) {
console_printf("step %d - %d\n", (i / (64)) + 1, (int)button_samples); console_flush();
hal_watchdog_tickle();
}

if(i % 8 == 0) {
uint16_t color = 0xF800;
uint16_t color = RED;
if (button_samples < 3000 * 64 * 2) {
color = 0x07E0;
color = GREEN;
} else if (button_samples < 3000 * 64 * 4) {
color = 0x001F;
color = BLUE;
} else {
color = 0xF800;
color = RED;
}

pinetime_boot_display_image_colors(0xffff, color, 240 - ((i / 8) * 6) + 1);
pinetime_boot_display_image_colors(WHITE, color, 240 - ((i / 8) * 6) + 1);
}
}
console_printf("Waited 5 seconds (%d)\n", (int)button_samples); console_flush();
Expand All @@ -109,6 +111,7 @@ void pinetime_boot_init(void) {
}
}

/// Configure and start the watchdog
void setup_watchdog() {
NRF_WDT->CONFIG &= ~(WDT_CONFIG_SLEEP_Msk << WDT_CONFIG_SLEEP_Pos);
NRF_WDT->CONFIG |= (WDT_CONFIG_HALT_Run << WDT_CONFIG_SLEEP_Pos);
Expand Down
3 changes: 3 additions & 0 deletions libs/pinetime_boot/src/pinetime_factory.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "pinetime_boot/pinetime_factory.h"
#include <hal/hal_flash.h>
#include "os/mynewt.h"
#include <hal/hal_watchdog.h>

// Flash Device for Image
#define FLASH_DEVICE 1 // 0 for Internal Flash ROM, 1 for External SPI Flash
Expand All @@ -18,9 +19,11 @@ void restore_factory(void) {
int rc;
for (uint32_t erased = 0; erased < FACTORY_SIZE; erased += 0x1000) {
rc = hal_flash_erase_sector(FLASH_DEVICE, FACTORY_OFFSET_DESTINATION + erased);
hal_watchdog_tickle();
}

for(uint32_t offset = 0; offset < FACTORY_SIZE; offset += BATCH_SIZE) {
hal_watchdog_tickle();
rc = hal_flash_read(FLASH_DEVICE, FACTORY_OFFSET_SOURCE + offset, flash_buffer, BATCH_SIZE);
assert(rc == 0);
rc = hal_flash_write(FLASH_DEVICE, FACTORY_OFFSET_DESTINATION + offset, flash_buffer, BATCH_SIZE);
Expand Down
Binary file added libs/pinetime_boot/src/version-0.0.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added libs/pinetime_boot/src/version-0.0.1.xcf
Binary file not shown.
6 changes: 5 additions & 1 deletion targets/nrf52_boot/syscfg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ syscfg.vals:
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
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

Loading

0 comments on commit 53918c7

Please sign in to comment.