Skip to content

Commit f7e40b1

Browse files
committed
Re-implement sleep/wakeup for touch panel, display, NOR Flash, SPI and TWI.
1 parent ecbbeb6 commit f7e40b1

File tree

11 files changed

+80
-24
lines changed

11 files changed

+80
-24
lines changed

src/DisplayApp/DisplayApp.cpp

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,10 @@ void DisplayApp::Refresh() {
9595
vTaskDelay(100);
9696
}
9797
lcd.DisplayOff();
98-
lcd.Sleep();
99-
touchPanel.Sleep();
98+
systemTask.PushMessage(System::SystemTask::Messages::OnDisplayTaskSleeping);
10099
state = States::Idle;
101100
break;
102101
case Messages::GoToRunning:
103-
lcd.Wakeup();
104-
touchPanel.Wakeup();
105-
106102
lcd.DisplayOn();
107103
brightnessController.Restore();
108104
state = States::Running;
@@ -173,7 +169,7 @@ void DisplayApp::Refresh() {
173169
}
174170
}
175171

176-
if(touchMode == TouchModes::Polling) {
172+
if(state != States::Idle && touchMode == TouchModes::Polling) {
177173
auto info = touchPanel.GetTouchInfo();
178174
if(info.action == 2) {// 2 = contact
179175
if(!currentScreen->OnTouchEvent(info.x, info.y)) {

src/SystemTask/SystemTask.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ void SystemTask::Work() {
145145
case Messages::OnButtonEvent:
146146
ReloadIdleTimer();
147147
break;
148+
case Messages::OnDisplayTaskSleeping:
149+
spiNorFlash.Sleep();
150+
lcd.Sleep();
151+
touchPanel.Sleep();
152+
153+
spi.Sleep();
154+
twiMaster.Sleep();
155+
break;
148156
default: break;
149157
}
150158
}
@@ -185,6 +193,13 @@ void SystemTask::OnButtonPushed() {
185193

186194
void SystemTask::GoToRunning() {
187195
PushMessage(Messages::GoToRunning);
196+
spi.Wakeup();
197+
twiMaster.Wakeup();
198+
199+
spiNorFlash.Wakeup();
200+
lcd.Wakeup();
201+
touchPanel.Wakeup();
202+
188203
displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning);
189204
displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel);
190205
}

src/SystemTask/SystemTask.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Pinetime {
1818
class SystemTask {
1919
public:
2020
enum class Messages {GoToSleep, GoToRunning, OnNewTime, OnNewNotification, BleConnected,
21-
BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent
21+
BleFirmwareUpdateStarted, BleFirmwareUpdateFinished, OnTouchEvent, OnButtonEvent, OnDisplayTaskSleeping
2222
};
2323

2424
SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,

src/drivers/Cst816s.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <task.h>
33
#include <nrfx_log.h>
44
#include <legacy/nrf_drv_gpiote.h>
5-
65
#include "Cst816s.h"
76
using namespace Pinetime::Drivers;
87

@@ -96,12 +95,16 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() {
9695
}
9796

9897
void Cst816S::Sleep() {
99-
// TODO re enable sleep mode
100-
//twiMaster.Sleep();
101-
nrf_gpio_cfg_default(6);
102-
nrf_gpio_cfg_default(7);
98+
nrf_gpio_pin_clear(pinReset);
99+
vTaskDelay(5);
100+
nrf_gpio_pin_set(pinReset);
101+
vTaskDelay(50);
102+
static constexpr uint8_t sleepValue = 0x03;
103+
twiMaster.Write(twiAddress, 0xA5, &sleepValue, 1);
104+
NRF_LOG_INFO("[TOUCHPANEL] Sleep");
103105
}
104106

105107
void Cst816S::Wakeup() {
106108
Init();
109+
NRF_LOG_INFO("[TOUCHPANEL] Wakeup");
107110
}

src/drivers/Spi.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <hal/nrf_gpio.h>
2+
#include <nrfx_log.h>
23
#include "Spi.h"
34

45
using namespace Pinetime::Drivers;
@@ -18,17 +19,23 @@ bool Spi::Read(uint8_t* cmd, size_t cmdSize, uint8_t *data, size_t dataSize) {
1819
}
1920

2021
void Spi::Sleep() {
21-
// TODO sleep spi
2222
nrf_gpio_cfg_default(pinCsn);
23+
NRF_LOG_INFO("[SPI] Sleep")
24+
}
25+
26+
bool Spi::WriteCmdAndBuffer(const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) {
27+
return spiMaster.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize);
2328
}
2429

2530
bool Spi::Init() {
2631
nrf_gpio_pin_set(pinCsn); /* disable Set slave select (inactive high) */
2732
return true;
2833
}
2934

30-
bool Spi::WriteCmdAndBuffer(const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) {
31-
return spiMaster.WriteCmdAndBuffer(pinCsn, cmd, cmdSize, data, dataSize);
35+
void Spi::Wakeup() {
36+
nrf_gpio_cfg_output(pinCsn);
37+
nrf_gpio_pin_set(pinCsn);
38+
NRF_LOG_INFO("[SPI] Wakeup")
3239
}
3340

3441

src/drivers/SpiMaster.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "SpiMaster.h"
55
#include <algorithm>
66
#include <task.h>
7+
#include <nrfx_log.h>
78

89
using namespace Pinetime::Drivers;
910

@@ -231,10 +232,13 @@ void SpiMaster::Sleep() {
231232
nrf_gpio_cfg_default(params.pinSCK);
232233
nrf_gpio_cfg_default(params.pinMOSI);
233234
nrf_gpio_cfg_default(params.pinMISO);
235+
236+
NRF_LOG_INFO("[SPIMASTER] sleep")
234237
}
235238

236239
void SpiMaster::Wakeup() {
237240
Init();
241+
NRF_LOG_INFO("[SPIMASTER] Wakeup");
238242
}
239243

240244
bool SpiMaster::WriteCmdAndBuffer(uint8_t pinCsn, const uint8_t *cmd, size_t cmdSize, const uint8_t *data, size_t dataSize) {

src/drivers/SpiNorFlash.cpp

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,34 @@ SpiNorFlash::SpiNorFlash(Spi& spi) : spi{spi} {
1111
}
1212

1313
void SpiNorFlash::Init() {
14-
auto id = ReadIdentificaion();
15-
NRF_LOG_INFO("[SPI FLASH] Manufacturer : %d, Memory type : %d, memory density : %d", id.manufacturer, id.type, id.density);
14+
device_id = ReadIdentificaion();
15+
NRF_LOG_INFO("[SPI FLASH] Manufacturer : %d, Memory type : %d, memory density : %d", device_id.manufacturer, device_id.type, device_id.density);
1616
}
1717

1818
void SpiNorFlash::Uninit() {
1919

2020
}
2121

2222
void SpiNorFlash::Sleep() {
23-
23+
auto cmd = static_cast<uint8_t>(Commands::DeepPowerDown);
24+
spi.Write(&cmd, sizeof(uint8_t));
25+
NRF_LOG_INFO("[FLASH] Sleep")
2426
}
2527

2628
void SpiNorFlash::Wakeup() {
27-
29+
// send Commands::ReleaseFromDeepPowerDown then 3 dummy bytes before reading Device ID
30+
static constexpr uint8_t cmdSize = 4;
31+
uint8_t cmd[cmdSize] = {static_cast<uint8_t>(Commands::ReleaseFromDeepPowerDown), 0x01, 0x02, 0x03};
32+
uint8_t id = 0;
33+
spi.Read(reinterpret_cast<uint8_t *>(&cmd), cmdSize, &id, 1);
34+
auto devId = device_id = ReadIdentificaion();
35+
if(devId.type != device_id.type) {
36+
NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: Failed");
37+
}
38+
else {
39+
NRF_LOG_INFO("[SpiNorFlash] ID on Wakeup: %d", id);
40+
}
41+
NRF_LOG_INFO("[FLASH] Wakeup")
2842
}
2943

3044
SpiNorFlash::Identification SpiNorFlash::ReadIdentificaion() {

src/drivers/SpiNorFlash.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,13 @@ namespace Pinetime {
4848
SectorErase = 0x20,
4949
ReadSecurityRegister = 0x2B,
5050
ReadIdentification = 0x9F,
51+
ReleaseFromDeepPowerDown = 0xAB,
52+
DeepPowerDown = 0xB9
5153
};
5254
static constexpr uint16_t pageSize = 256;
5355

5456
Spi& spi;
55-
57+
Identification device_id;
5658
};
5759
}
5860
}

src/drivers/St7789.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <hal/nrf_gpio.h>
22
#include <libraries/delay/nrf_delay.h>
3+
#include <nrfx_log.h>
34
#include "St7789.h"
45
#include "Spi.h"
56

@@ -174,12 +175,10 @@ void St7789::HardwareReset() {
174175
void St7789::Sleep() {
175176
SleepIn();
176177
nrf_gpio_cfg_default(pinDataCommand);
177-
// spi.Sleep(); // TODO sleep SPI
178+
NRF_LOG_INFO("[LCD] Sleep");
178179
}
179180

180181
void St7789::Wakeup() {
181-
// spi.Wakeup(); // TODO wake up SPI
182-
183182
nrf_gpio_cfg_output(pinDataCommand);
184183
// TODO why do we need to reset the controller?
185184
HardwareReset();
@@ -193,4 +192,5 @@ void St7789::Wakeup() {
193192
NormalModeOn();
194193
VerticalScrollStartAddress(verticalScrollingStartAddress);
195194
DisplayOn();
195+
NRF_LOG_INFO("[LCD] Wakeup")
196196
}

src/drivers/TwiMaster.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,16 @@ void TwiMaster::Write(uint8_t deviceAddress, const uint8_t *data, size_t size, b
137137
uint32_t error = twiBaseAddress->ERRORSRC;
138138
twiBaseAddress->ERRORSRC = error;
139139
}
140-
}
140+
}
141+
142+
void TwiMaster::Sleep() {
143+
nrf_gpio_cfg_default(6);
144+
nrf_gpio_cfg_default(7);
145+
twiBaseAddress->ENABLE = 0;
146+
NRF_LOG_INFO("[TWIMASTER] Sleep");
147+
}
148+
149+
void TwiMaster::Wakeup() {
150+
Init();
151+
NRF_LOG_INFO("[TWIMASTER] Wakeup");
152+
}

0 commit comments

Comments
 (0)