2020-02-23 12:44:39 +00:00
|
|
|
#include <libraries/log/nrf_log.h>
|
|
|
|
#include <libraries/gpiote/app_gpiote.h>
|
|
|
|
#include <drivers/Cst816s.h>
|
|
|
|
#include <DisplayApp/LittleVgl.h>
|
|
|
|
#include <hal/nrf_rtc.h>
|
2020-03-28 18:05:28 +00:00
|
|
|
#include <Components/Ble/NotificationManager.h>
|
2020-04-19 18:44:59 +00:00
|
|
|
#include <host/ble_gatt.h>
|
|
|
|
#include <host/ble_hs_adv.h>
|
2020-02-23 12:44:39 +00:00
|
|
|
#include "SystemTask.h"
|
2020-04-19 18:44:59 +00:00
|
|
|
#include <nimble/hci_common.h>
|
|
|
|
#include <host/ble_gap.h>
|
|
|
|
#include <host/util/util.h>
|
2020-05-24 18:30:06 +00:00
|
|
|
#include <drivers/InternalFlash.h>
|
2020-02-23 12:44:39 +00:00
|
|
|
#include "../main.h"
|
2020-04-19 19:26:09 +00:00
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
using namespace Pinetime::System;
|
|
|
|
|
2020-06-01 18:40:11 +00:00
|
|
|
void IdleTimerCallback(TimerHandle_t xTimer) {
|
2020-06-13 15:33:49 +00:00
|
|
|
|
|
|
|
NRF_LOG_INFO("IdleTimerCallback");
|
2020-06-01 18:40:11 +00:00
|
|
|
auto sysTask = static_cast<SystemTask *>(pvTimerGetTimerID(xTimer));
|
|
|
|
sysTask->OnIdle();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-05-07 17:53:51 +00:00
|
|
|
SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
|
|
|
|
Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel,
|
2020-03-28 18:05:28 +00:00
|
|
|
Components::LittleVgl &lvgl,
|
|
|
|
Controllers::Battery &batteryController, Controllers::Ble &bleController,
|
|
|
|
Controllers::DateTime &dateTimeController,
|
|
|
|
Pinetime::Controllers::NotificationManager& notificationManager) :
|
2020-05-07 17:53:51 +00:00
|
|
|
spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController},
|
2020-03-22 11:03:17 +00:00
|
|
|
bleController{bleController}, dateTimeController{dateTimeController},
|
2020-04-27 18:13:27 +00:00
|
|
|
watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager},
|
2020-05-11 16:50:37 +00:00
|
|
|
nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) {
|
2020-02-23 12:44:39 +00:00
|
|
|
systemTaksMsgQueue = xQueueCreate(10, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Start() {
|
2020-02-23 15:14:03 +00:00
|
|
|
if (pdPASS != xTaskCreate(SystemTask::Process, "MAIN", 350, this, 0, &taskHandle))
|
2020-02-23 12:44:39 +00:00
|
|
|
APP_ERROR_HANDLER(NRF_ERROR_NO_MEM);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Process(void *instance) {
|
|
|
|
auto *app = static_cast<SystemTask *>(instance);
|
|
|
|
NRF_LOG_INFO("SystemTask task started!");
|
|
|
|
app->Work();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::Work() {
|
2020-06-27 14:11:42 +00:00
|
|
|
watchdog.Setup(7);
|
|
|
|
watchdog.Start();
|
2020-02-23 20:09:11 +00:00
|
|
|
NRF_LOG_INFO("Last reset reason : %s", Pinetime::Drivers::Watchdog::ResetReasonToString(watchdog.ResetReason()));
|
2020-02-23 12:44:39 +00:00
|
|
|
APP_GPIOTE_INIT(2);
|
2020-04-19 18:44:59 +00:00
|
|
|
|
2020-05-11 16:50:37 +00:00
|
|
|
spi.Init();
|
|
|
|
spiNorFlash.Init();
|
2020-05-24 07:15:59 +00:00
|
|
|
|
2020-05-24 18:30:06 +00:00
|
|
|
// Write the 'image OK' flag if it's not already done
|
|
|
|
// TODO implement a better verification mecanism for the image (ask for user confirmation via UI/BLE ?)
|
|
|
|
uint32_t* imageOkPtr = reinterpret_cast<uint32_t *>(0x7BFE8);
|
|
|
|
uint32_t imageOk = *imageOkPtr;
|
|
|
|
if(imageOk != 1)
|
|
|
|
Pinetime::Drivers::InternalFlash::WriteWord(0x7BFE8, 1);
|
2020-05-24 07:15:59 +00:00
|
|
|
|
2020-04-19 19:26:09 +00:00
|
|
|
nimbleController.Init();
|
|
|
|
nimbleController.StartAdvertising();
|
2020-02-23 12:44:39 +00:00
|
|
|
lcd.Init();
|
2020-05-11 16:50:37 +00:00
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
touchPanel.Init();
|
|
|
|
batteryController.Init();
|
|
|
|
|
2020-03-28 18:05:28 +00:00
|
|
|
displayApp.reset(new Pinetime::Applications::DisplayApp(lcd, lvgl, touchPanel, batteryController, bleController,
|
|
|
|
dateTimeController, watchdogView, *this, notificationManager));
|
2020-02-23 12:44:39 +00:00
|
|
|
displayApp->Start();
|
|
|
|
|
|
|
|
batteryController.Update();
|
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateBatteryLevel);
|
|
|
|
|
|
|
|
nrf_gpio_cfg_sense_input(pinButton, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_High);
|
|
|
|
nrf_gpio_cfg_output(15);
|
|
|
|
nrf_gpio_pin_set(15);
|
|
|
|
|
|
|
|
nrfx_gpiote_in_config_t pinConfig;
|
|
|
|
pinConfig.skip_gpio_setup = true;
|
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.is_watcher = false;
|
|
|
|
pinConfig.sense = (nrf_gpiote_polarity_t)NRF_GPIOTE_POLARITY_HITOLO;
|
|
|
|
pinConfig.pull = (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pulldown;
|
|
|
|
|
|
|
|
nrfx_gpiote_in_init(pinButton, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
|
|
|
nrf_gpio_cfg_sense_input(pinTouchIrq, (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup, (nrf_gpio_pin_sense_t)GPIO_PIN_CNF_SENSE_Low);
|
|
|
|
|
|
|
|
pinConfig.skip_gpio_setup = true;
|
|
|
|
pinConfig.hi_accuracy = false;
|
|
|
|
pinConfig.is_watcher = false;
|
|
|
|
pinConfig.sense = (nrf_gpiote_polarity_t)NRF_GPIOTE_POLARITY_HITOLO;
|
|
|
|
pinConfig.pull = (nrf_gpio_pin_pull_t)GPIO_PIN_CNF_PULL_Pullup;
|
|
|
|
|
|
|
|
nrfx_gpiote_in_init(pinTouchIrq, &pinConfig, nrfx_gpiote_evt_handler);
|
|
|
|
|
2020-06-01 18:40:11 +00:00
|
|
|
idleTimer = xTimerCreate ("idleTimer", idleTime, pdFALSE, this, IdleTimerCallback);
|
|
|
|
xTimerStart(idleTimer, 0);
|
2020-02-23 12:44:39 +00:00
|
|
|
|
|
|
|
while(true) {
|
|
|
|
uint8_t msg;
|
2020-02-23 20:09:11 +00:00
|
|
|
if (xQueueReceive(systemTaksMsgQueue, &msg, isSleeping?2500 : 1000)) {
|
2020-02-23 12:44:39 +00:00
|
|
|
Messages message = static_cast<Messages >(msg);
|
|
|
|
switch(message) {
|
2020-06-01 18:40:11 +00:00
|
|
|
case Messages::GoToRunning:
|
|
|
|
isSleeping = false;
|
|
|
|
xTimerStart(idleTimer, 0);
|
|
|
|
nimbleController.StartAdvertising();
|
|
|
|
break;
|
2020-02-23 12:44:39 +00:00
|
|
|
case Messages::GoToSleep:
|
|
|
|
NRF_LOG_INFO("[SystemTask] Going to sleep");
|
2020-06-13 15:33:49 +00:00
|
|
|
xTimerStop(idleTimer, 0);
|
2020-02-23 12:44:39 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::GoToSleep);
|
2020-06-01 18:40:11 +00:00
|
|
|
isSleeping = true;
|
|
|
|
break;
|
2020-03-25 20:23:40 +00:00
|
|
|
case Messages::OnNewTime:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2020-03-25 20:23:40 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::UpdateDateTime);
|
|
|
|
break;
|
|
|
|
case Messages::OnNewNotification:
|
2020-07-04 16:10:30 +00:00
|
|
|
if(isSleeping) GoToRunning();
|
2020-03-25 20:23:40 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::NewNotification);
|
|
|
|
break;
|
2020-05-01 19:58:31 +00:00
|
|
|
case Messages::BleConnected:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2020-05-01 19:58:31 +00:00
|
|
|
isBleDiscoveryTimerRunning = true;
|
|
|
|
bleDiscoveryTimer = 5;
|
|
|
|
break;
|
2020-05-02 12:16:57 +00:00
|
|
|
case Messages::BleFirmwareUpdateStarted:
|
2020-06-01 18:40:11 +00:00
|
|
|
doNotGoToSleep = true;
|
|
|
|
if(isSleeping) GoToRunning();
|
2020-05-02 12:16:57 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateStarted);
|
|
|
|
break;
|
|
|
|
case Messages::BleFirmwareUpdateFinished:
|
2020-06-01 18:40:11 +00:00
|
|
|
doNotGoToSleep = false;
|
|
|
|
xTimerStart(idleTimer, 0);
|
2020-05-02 12:16:57 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::BleFirmwareUpdateFinished);
|
2020-06-01 14:30:24 +00:00
|
|
|
if(bleController.State() == Pinetime::Controllers::Ble::FirmwareUpdateStates::Validated)
|
|
|
|
NVIC_SystemReset();
|
2020-05-02 12:16:57 +00:00
|
|
|
break;
|
2020-06-01 18:40:11 +00:00
|
|
|
case Messages::OnTouchEvent:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
|
|
|
case Messages::OnButtonEvent:
|
2020-07-04 16:10:30 +00:00
|
|
|
ReloadIdleTimer();
|
2020-06-01 18:40:11 +00:00
|
|
|
break;
|
2020-02-23 12:44:39 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
2020-05-01 19:58:31 +00:00
|
|
|
|
|
|
|
if(isBleDiscoveryTimerRunning) {
|
|
|
|
if(bleDiscoveryTimer == 0) {
|
|
|
|
isBleDiscoveryTimerRunning = false;
|
|
|
|
// Services discovery is deffered from 3 seconds to avoid the conflicts between the host communicating with the
|
|
|
|
// tharget and vice-versa. I'm not sure if this is the right way to handle this...
|
|
|
|
nimbleController.StartDiscovery();
|
|
|
|
} else {
|
|
|
|
bleDiscoveryTimer--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
uint32_t systick_counter = nrf_rtc_counter_get(portNRF_RTC_REG);
|
|
|
|
dateTimeController.UpdateTime(systick_counter);
|
2020-03-14 15:33:47 +00:00
|
|
|
batteryController.Update();
|
2020-02-23 20:09:11 +00:00
|
|
|
|
2020-07-02 19:38:52 +00:00
|
|
|
monitor.Process();
|
|
|
|
|
2020-02-23 20:09:11 +00:00
|
|
|
if(!nrf_gpio_pin_read(pinButton))
|
|
|
|
watchdog.Kick();
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::OnButtonPushed() {
|
|
|
|
if(!isSleeping) {
|
|
|
|
NRF_LOG_INFO("[SystemTask] Button pushed");
|
2020-06-01 18:40:11 +00:00
|
|
|
PushMessage(Messages::OnButtonEvent);
|
2020-02-23 12:44:39 +00:00
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::ButtonPushed);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
NRF_LOG_INFO("[SystemTask] Button pushed, waking up");
|
2020-06-01 18:40:11 +00:00
|
|
|
GoToRunning();
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-01 18:40:11 +00:00
|
|
|
void SystemTask::GoToRunning() {
|
|
|
|
PushMessage(Messages::GoToRunning);
|
|
|
|
displayApp->PushMessage(Applications::DisplayApp::Messages::GoToRunning);
|
|
|
|
displayApp->PushMessage(Applications::DisplayApp::Messages::UpdateBatteryLevel);
|
|
|
|
}
|
|
|
|
|
2020-02-23 12:44:39 +00:00
|
|
|
void SystemTask::OnTouchEvent() {
|
|
|
|
NRF_LOG_INFO("[SystemTask] Touch event");
|
2020-06-01 18:40:11 +00:00
|
|
|
if(!isSleeping) {
|
|
|
|
PushMessage(Messages::OnTouchEvent);
|
|
|
|
displayApp->PushMessage(Pinetime::Applications::DisplayApp::Messages::TouchEvent);
|
|
|
|
}
|
2020-02-23 12:44:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void SystemTask::PushMessage(SystemTask::Messages msg) {
|
|
|
|
BaseType_t xHigherPriorityTaskWoken;
|
|
|
|
xHigherPriorityTaskWoken = pdFALSE;
|
|
|
|
xQueueSendFromISR(systemTaksMsgQueue, &msg, &xHigherPriorityTaskWoken);
|
|
|
|
if (xHigherPriorityTaskWoken) {
|
|
|
|
/* Actual macro used here is port specific. */
|
|
|
|
// TODO : should I do something here?
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 18:40:11 +00:00
|
|
|
|
|
|
|
void SystemTask::OnIdle() {
|
|
|
|
if(doNotGoToSleep) return;
|
|
|
|
NRF_LOG_INFO("Idle timeout -> Going to sleep")
|
|
|
|
PushMessage(Messages::GoToSleep);
|
|
|
|
}
|
2020-07-04 16:10:30 +00:00
|
|
|
|
|
|
|
void SystemTask::ReloadIdleTimer() const {
|
|
|
|
if(isSleeping) return;
|
|
|
|
xTimerReset(idleTimer, 0);
|
|
|
|
}
|