2021-10-13 16:08:35 -04:00
|
|
|
#include "drivers/St7789.h"
|
2019-12-05 13:23:46 -05:00
|
|
|
#include <hal/nrf_gpio.h>
|
|
|
|
#include <libraries/delay/nrf_delay.h>
|
2020-08-22 11:59:59 -04:00
|
|
|
#include <nrfx_log.h>
|
2021-10-13 16:08:35 -04:00
|
|
|
#include "drivers/Spi.h"
|
2019-12-05 13:23:46 -05:00
|
|
|
|
|
|
|
using namespace Pinetime::Drivers;
|
|
|
|
|
2023-05-18 09:19:44 -04:00
|
|
|
St7789::St7789(Spi& spi, uint8_t pinDataCommand, uint8_t pinReset) : spi {spi}, pinDataCommand {pinDataCommand}, pinReset{pinReset} {
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::Init() {
|
|
|
|
nrf_gpio_cfg_output(pinDataCommand);
|
2023-05-18 09:19:44 -04:00
|
|
|
nrf_gpio_cfg_output(pinReset);
|
|
|
|
nrf_gpio_pin_set(pinReset);
|
2020-01-02 08:47:59 -05:00
|
|
|
HardwareReset();
|
2019-12-05 13:23:46 -05:00
|
|
|
SoftwareReset();
|
|
|
|
SleepOut();
|
|
|
|
ColMod();
|
|
|
|
MemoryDataAccessControl();
|
|
|
|
ColumnAddressSet();
|
|
|
|
RowAddressSet();
|
2022-06-24 18:32:23 -04:00
|
|
|
// P8B Mirrored version does not need display inversion.
|
|
|
|
#ifndef DRIVER_DISPLAY_MIRROR
|
2019-12-05 13:23:46 -05:00
|
|
|
DisplayInversionOn();
|
2022-06-24 18:32:23 -04:00
|
|
|
#endif
|
2019-12-05 13:23:46 -05:00
|
|
|
NormalModeOn();
|
2022-04-26 06:09:30 -04:00
|
|
|
SetVdv();
|
2019-12-05 13:23:46 -05:00
|
|
|
DisplayOn();
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::WriteCommand(uint8_t cmd) {
|
|
|
|
nrf_gpio_pin_clear(pinDataCommand);
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteSpi(&cmd, 1);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::WriteData(uint8_t data) {
|
|
|
|
nrf_gpio_pin_set(pinDataCommand);
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteSpi(&data, 1);
|
|
|
|
}
|
|
|
|
|
2020-01-26 07:37:10 -05:00
|
|
|
void St7789::WriteSpi(const uint8_t* data, size_t size) {
|
2021-04-18 13:28:14 -04:00
|
|
|
spi.Write(data, size);
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::SoftwareReset() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::SoftwareReset));
|
|
|
|
nrf_delay_ms(150);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::SleepOut() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::SleepOut));
|
2020-01-17 16:16:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void St7789::SleepIn() {
|
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::SleepIn));
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::ColMod() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::ColMod));
|
|
|
|
WriteData(0x55);
|
|
|
|
nrf_delay_ms(10);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::MemoryDataAccessControl() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::MemoryDataAccessControl));
|
2022-06-24 18:32:23 -04:00
|
|
|
#ifdef DRIVER_DISPLAY_MIRROR
|
|
|
|
// [7] = MY = Page Address Order, 0 = Top to bottom, 1 = Bottom to top
|
|
|
|
// [6] = MX = Column Address Order, 0 = Left to right, 1 = Right to left
|
|
|
|
// [5] = MV = Page/Column Order, 0 = Normal mode, 1 = Reverse mode
|
|
|
|
// [4] = ML = Line Address Order, 0 = LCD refresh from top to bottom, 1 = Bottom to top
|
|
|
|
// [3] = RGB = RGB/BGR Order, 0 = RGB, 1 = BGR
|
|
|
|
// [2] = MH = Display Data Latch Order, 0 = LCD refresh from left to right, 1 = Right to left
|
|
|
|
// [0 .. 1] = Unused
|
|
|
|
WriteData(0b01000000);
|
|
|
|
#else
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteData(0x00);
|
2022-06-24 18:32:23 -04:00
|
|
|
#endif
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::ColumnAddressSet() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::ColumnAddressSet));
|
|
|
|
WriteData(0x00);
|
|
|
|
WriteData(0x00);
|
2020-03-08 16:46:25 -04:00
|
|
|
WriteData(Width >> 8u);
|
|
|
|
WriteData(Width & 0xffu);
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::RowAddressSet() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::RowAddressSet));
|
|
|
|
WriteData(0x00);
|
|
|
|
WriteData(0x00);
|
2020-03-08 16:46:25 -04:00
|
|
|
WriteData(320u >> 8u);
|
|
|
|
WriteData(320u & 0xffu);
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::DisplayInversionOn() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::DisplayInversionOn));
|
|
|
|
nrf_delay_ms(10);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::NormalModeOn() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::NormalModeOn));
|
|
|
|
nrf_delay_ms(10);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::DisplayOn() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::DisplayOn));
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::SetAddrWindow(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::ColumnAddressSet));
|
|
|
|
WriteData(x0 >> 8);
|
|
|
|
WriteData(x0 & 0xff);
|
|
|
|
WriteData(x1 >> 8);
|
|
|
|
WriteData(x1 & 0xff);
|
|
|
|
|
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::RowAddressSet));
|
2021-04-18 13:28:14 -04:00
|
|
|
WriteData(y0 >> 8);
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteData(y0 & 0xff);
|
|
|
|
WriteData(y1 >> 8);
|
|
|
|
WriteData(y1 & 0xff);
|
|
|
|
|
|
|
|
WriteToRam();
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::WriteToRam() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::WriteToRam));
|
|
|
|
}
|
|
|
|
|
2022-04-26 06:09:30 -04:00
|
|
|
void St7789::SetVdv() {
|
|
|
|
// By default there is a large step from pixel brightness zero to one.
|
|
|
|
// After experimenting with VCOMS, VRH and VDV, this was found to produce good results.
|
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::VdvSet));
|
|
|
|
WriteData(0x10);
|
2022-04-22 16:26:38 -04:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::DisplayOff() {
|
2019-12-05 13:23:46 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::DisplayOff));
|
|
|
|
nrf_delay_ms(500);
|
|
|
|
}
|
|
|
|
|
2020-02-15 09:12:29 -05:00
|
|
|
void St7789::VerticalScrollDefinition(uint16_t topFixedLines, uint16_t scrollLines, uint16_t bottomFixedLines) {
|
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::VerticalScrollDefinition));
|
|
|
|
WriteData(topFixedLines >> 8u);
|
|
|
|
WriteData(topFixedLines & 0x00ffu);
|
|
|
|
WriteData(scrollLines >> 8u);
|
|
|
|
WriteData(scrollLines & 0x00ffu);
|
|
|
|
WriteData(bottomFixedLines >> 8u);
|
|
|
|
WriteData(bottomFixedLines & 0x00ffu);
|
|
|
|
}
|
|
|
|
|
|
|
|
void St7789::VerticalScrollStartAddress(uint16_t line) {
|
2020-03-11 16:35:06 -04:00
|
|
|
verticalScrollingStartAddress = line;
|
2020-02-15 09:12:29 -05:00
|
|
|
WriteCommand(static_cast<uint8_t>(Commands::VerticalScrollStartAddress));
|
|
|
|
WriteData(line >> 8u);
|
|
|
|
WriteData(line & 0x00ffu);
|
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::Uninit() {
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
void St7789::DrawPixel(uint16_t x, uint16_t y, uint32_t color) {
|
2021-06-22 14:31:31 -04:00
|
|
|
if (x >= Width || y >= Height) {
|
2021-04-18 13:28:14 -04:00
|
|
|
return;
|
2021-06-22 14:31:31 -04:00
|
|
|
}
|
2019-12-05 13:23:46 -05:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
SetAddrWindow(x, y, x + 1, y + 1);
|
2019-12-05 13:23:46 -05:00
|
|
|
|
2019-12-07 11:11:50 -05:00
|
|
|
nrf_gpio_pin_set(pinDataCommand);
|
2021-04-18 13:28:14 -04:00
|
|
|
WriteSpi(reinterpret_cast<const uint8_t*>(&color), 2);
|
2019-12-05 13:23:46 -05:00
|
|
|
}
|
2019-12-07 11:11:50 -05:00
|
|
|
|
2021-04-18 13:28:14 -04:00
|
|
|
void St7789::DrawBuffer(uint16_t x, uint16_t y, uint16_t width, uint16_t height, const uint8_t* data, size_t size) {
|
2021-03-18 15:38:19 -04:00
|
|
|
SetAddrWindow(x, y, x + width - 1, y + height - 1);
|
2019-12-07 13:15:33 -05:00
|
|
|
nrf_gpio_pin_set(pinDataCommand);
|
2020-01-26 07:37:10 -05:00
|
|
|
WriteSpi(data, size);
|
2019-12-07 13:15:33 -05:00
|
|
|
}
|
|
|
|
|
2020-01-02 08:47:59 -05:00
|
|
|
void St7789::HardwareReset() {
|
2023-05-18 09:19:44 -04:00
|
|
|
nrf_gpio_pin_clear(pinReset);
|
2020-01-17 16:16:45 -05:00
|
|
|
nrf_delay_ms(10);
|
2023-05-18 09:19:44 -04:00
|
|
|
nrf_gpio_pin_set(pinReset);
|
2020-01-17 16:16:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void St7789::Sleep() {
|
|
|
|
SleepIn();
|
|
|
|
nrf_gpio_cfg_default(pinDataCommand);
|
2023-05-18 09:19:44 -04:00
|
|
|
nrf_gpio_cfg_default(pinReset);
|
2020-08-22 11:59:59 -04:00
|
|
|
NRF_LOG_INFO("[LCD] Sleep");
|
2020-01-17 16:16:45 -05:00
|
|
|
}
|
2020-01-02 08:47:59 -05:00
|
|
|
|
2020-01-17 16:16:45 -05:00
|
|
|
void St7789::Wakeup() {
|
|
|
|
nrf_gpio_cfg_output(pinDataCommand);
|
|
|
|
SleepOut();
|
2020-03-11 16:35:06 -04:00
|
|
|
VerticalScrollStartAddress(verticalScrollingStartAddress);
|
2020-01-17 16:16:45 -05:00
|
|
|
DisplayOn();
|
2020-08-22 11:59:59 -04:00
|
|
|
NRF_LOG_INFO("[LCD] Wakeup")
|
2020-01-02 08:47:59 -05:00
|
|
|
}
|