Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LCD delay improvements #2043

Merged
merged 4 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions src/drivers/St7789.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "drivers/St7789.h"
#include <hal/nrf_gpio.h>
#include <libraries/delay/nrf_delay.h>
#include <nrfx_log.h>
#include "drivers/Spi.h"
#include "task.h"

using namespace Pinetime::Drivers;

Expand Down Expand Up @@ -44,22 +44,52 @@ void St7789::WriteSpi(const uint8_t* data, size_t size) {
}

void St7789::SoftwareReset() {
EnsureSleepOutPostDelay();
WriteCommand(static_cast<uint8_t>(Commands::SoftwareReset));
nrf_delay_ms(150);
// If sleep in: must wait 120ms before sleep out can sent (see driver datasheet)
// Unconditionally wait as software reset doesn't need to be performant
sleepIn = true;
lastSleepExit = xTaskGetTickCount();
vTaskDelay(pdMS_TO_TICKS(125));
}

void St7789::SleepOut() {
if (!sleepIn) {
return;
}
WriteCommand(static_cast<uint8_t>(Commands::SleepOut));
// Wait 5ms for clocks to stabilise
// pdMS rounds down => 6 used here
vTaskDelay(pdMS_TO_TICKS(6));
// Cannot send sleep in or software reset for 120ms
lastSleepExit = xTaskGetTickCount();
sleepIn = false;
}

void St7789::EnsureSleepOutPostDelay() {
TickType_t delta = xTaskGetTickCount() - lastSleepExit;
// Due to timer wraparound, there is a chance of delaying when not necessary
// It is very low (pdMS_TO_TICKS(125)/2^32) and waiting an extra 125ms isn't too bad
if (delta < pdMS_TO_TICKS(125)) {
vTaskDelay(pdMS_TO_TICKS(125) - delta);
}
}

void St7789::SleepIn() {
if (sleepIn) {
return;
}
EnsureSleepOutPostDelay();
WriteCommand(static_cast<uint8_t>(Commands::SleepIn));
// Wait 5ms for clocks to stabilise
// pdMS rounds down => 6 used here
vTaskDelay(pdMS_TO_TICKS(6));
sleepIn = true;
}

void St7789::ColMod() {
WriteCommand(static_cast<uint8_t>(Commands::ColMod));
WriteData(0x55);
nrf_delay_ms(10);
}

void St7789::MemoryDataAccessControl() {
Expand Down Expand Up @@ -96,12 +126,10 @@ void St7789::RowAddressSet() {

void St7789::DisplayInversionOn() {
WriteCommand(static_cast<uint8_t>(Commands::DisplayInversionOn));
nrf_delay_ms(10);
}

void St7789::NormalModeOn() {
WriteCommand(static_cast<uint8_t>(Commands::NormalModeOn));
nrf_delay_ms(10);
}

void St7789::DisplayOn() {
Expand Down Expand Up @@ -137,7 +165,6 @@ void St7789::SetVdv() {

void St7789::DisplayOff() {
WriteCommand(static_cast<uint8_t>(Commands::DisplayOff));
nrf_delay_ms(500);
}

void St7789::VerticalScrollStartAddress(uint16_t line) {
Expand All @@ -158,8 +185,13 @@ void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height,

void St7789::HardwareReset() {
nrf_gpio_pin_clear(pinReset);
nrf_delay_ms(10);
vTaskDelay(pdMS_TO_TICKS(1));
nrf_gpio_pin_set(pinReset);
// If hardware reset started while sleep out, reset time may be up to 120ms
// Unconditionally wait as hardware reset doesn't need to be performant
sleepIn = true;
lastSleepExit = xTaskGetTickCount();
vTaskDelay(pdMS_TO_TICKS(125));
}

void St7789::Sleep() {
Expand Down
5 changes: 5 additions & 0 deletions src/drivers/St7789.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#include <cstddef>
#include <cstdint>

#include <FreeRTOS.h>

mark9064 marked this conversation as resolved.
Show resolved Hide resolved
namespace Pinetime {
namespace Drivers {
class Spi;
Expand Down Expand Up @@ -29,10 +31,13 @@ namespace Pinetime {
uint8_t pinDataCommand;
uint8_t pinReset;
uint8_t verticalScrollingStartAddress = 0;
bool sleepIn;
TickType_t lastSleepExit;

void HardwareReset();
void SoftwareReset();
void SleepOut();
void EnsureSleepOutPostDelay();
void SleepIn();
void ColMod();
void MemoryDataAccessControl();
Expand Down
Loading