Merge branch 'debug_touchscreen' of JF/PineTime into develop

This commit is contained in:
JF 2020-07-20 21:26:32 +02:00 committed by Gitea
commit 800e217e7c
9 changed files with 239 additions and 55 deletions

View File

@ -371,6 +371,7 @@ list(APPEND SOURCE_FILES
DisplayApp/Fonts/jetbrains_mono_bold_20.c DisplayApp/Fonts/jetbrains_mono_bold_20.c
SystemTask/SystemTask.cpp SystemTask/SystemTask.cpp
drivers/TwiMaster.cpp
) )
list(APPEND GRAPHICS_SOURCE_FILES list(APPEND GRAPHICS_SOURCE_FILES
@ -444,6 +445,7 @@ set(INCLUDE_FILES
SystemTask/SystemTask.h SystemTask/SystemTask.h
SystemTask/SystemMonitor.h SystemTask/SystemMonitor.h
DisplayApp/Screens/Symbols.h DisplayApp/Screens/Symbols.h
drivers/TwiMaster.h
) )
include_directories( include_directories(

View File

@ -24,12 +24,14 @@ void IdleTimerCallback(TimerHandle_t xTimer) {
SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, SystemTask::SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Drivers::TwiMaster& twiMaster, Drivers::Cst816S &touchPanel,
Components::LittleVgl &lvgl, Components::LittleVgl &lvgl,
Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Controllers::DateTime &dateTimeController,
Pinetime::Controllers::NotificationManager& notificationManager) : Pinetime::Controllers::NotificationManager& notificationManager) :
spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController}, spi{spi}, lcd{lcd}, spiNorFlash{spiNorFlash},
twiMaster{twiMaster}, touchPanel{touchPanel}, lvgl{lvgl}, batteryController{batteryController},
bleController{bleController}, dateTimeController{dateTimeController}, bleController{bleController}, dateTimeController{dateTimeController},
watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager},
nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) { nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) {
@ -67,6 +69,7 @@ void SystemTask::Work() {
nimbleController.StartAdvertising(); nimbleController.StartAdvertising();
lcd.Init(); lcd.Init();
twiMaster.Init();
touchPanel.Init(); touchPanel.Init();
batteryController.Init(); batteryController.Init();

View File

@ -21,7 +21,8 @@ namespace Pinetime {
}; };
SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd, SystemTask(Drivers::SpiMaster &spi, Drivers::St7789 &lcd,
Pinetime::Drivers::SpiNorFlash& spiNorFlash, Drivers::Cst816S &touchPanel, Pinetime::Drivers::SpiNorFlash& spiNorFlash,
Drivers::TwiMaster& twiMaster, Drivers::Cst816S &touchPanel,
Components::LittleVgl &lvgl, Components::LittleVgl &lvgl,
Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::Battery &batteryController, Controllers::Ble &bleController,
Controllers::DateTime &dateTimeController, Controllers::DateTime &dateTimeController,
@ -42,6 +43,7 @@ namespace Pinetime {
Pinetime::Drivers::SpiMaster& spi; Pinetime::Drivers::SpiMaster& spi;
Pinetime::Drivers::St7789& lcd; Pinetime::Drivers::St7789& lcd;
Pinetime::Drivers::SpiNorFlash& spiNorFlash; Pinetime::Drivers::SpiNorFlash& spiNorFlash;
Pinetime::Drivers::TwiMaster& twiMaster;
Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Drivers::Cst816S& touchPanel;
Pinetime::Components::LittleVgl& lvgl; Pinetime::Components::LittleVgl& lvgl;
Pinetime::Controllers::Battery& batteryController; Pinetime::Controllers::Battery& batteryController;

View File

@ -13,51 +13,42 @@ using namespace Pinetime::Drivers;
* TODO : we need a complete datasheet and protocol reference! * TODO : we need a complete datasheet and protocol reference!
* */ * */
void Pinetime::Drivers::Cst816S::Init() { Cst816S::Cst816S(TwiMaster &twiMaster, uint8_t twiAddress) : twiMaster{twiMaster}, twiAddress{twiAddress} {
}
void Cst816S::Init() {
nrf_gpio_cfg_output(pinReset); nrf_gpio_cfg_output(pinReset);
nrf_gpio_pin_clear(pinReset);
vTaskDelay(20);
nrf_gpio_pin_set(pinReset); nrf_gpio_pin_set(pinReset);
vTaskDelay(200); vTaskDelay(50);
nrf_gpio_pin_clear(pinReset);
vTaskDelay(5);
nrf_gpio_pin_set(pinReset);
vTaskDelay(50);
nrfx_twi_config_t config; // Wake the touchpanel up
config.frequency = NRF_TWI_FREQ_400K; uint8_t dummy;
config.scl = 7; twiMaster.Read(twiAddress, 0x15, &dummy, 1);
config.sda = 6; vTaskDelay(5);
config.interrupt_priority = NRFX_TWI_DEFAULT_CONFIG_IRQ_PRIORITY; twiMaster.Read(twiAddress, 0xa7, &dummy, 1);
config.hold_bus_uninit = NRFX_TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT;
// Configure TWI in blocking mode (event_handler = nullptr)
auto ret = nrfx_twi_init(&twi, &config, nullptr, this);
nrfx_twi_enable(&twi);
} }
void Cst816S::Probe() {
nrfx_err_t ret;
for(int i = 0; i < 127; i++) {
uint8_t data;
ret = nrfx_twi_rx(&twi, i, &data, 1);
if(ret == NRFX_SUCCESS) {
NRF_LOG_INFO("I2C device detected at address %d", i);
}
}
}
Cst816S::TouchInfos Cst816S::GetTouchInfo() { Cst816S::TouchInfos Cst816S::GetTouchInfo() {
Cst816S::TouchInfos info; Cst816S::TouchInfos info;
nrfx_twi_rx(&twi, address, touchData, 63); twiMaster.Read(twiAddress, 0, touchData, 63);
auto nbTouchPoints = touchData[2] & 0x0f; auto nbTouchPoints = touchData[2] & 0x0f;
// uint8_t i = 0; // uint8_t i = 0;
// NRF_LOG_INFO("#########################") // NRF_LOG_INFO("#########################")
for(int i = 0; i < 1; i++) { for(int i = 0; i < 1; i++) {
uint8_t pointId = (touchData[touchIdIndex + (touchStep * i)]) >> 4; uint8_t pointId = (touchData[touchIdIndex + (touchStep * i)]) >> 4;
if(nbTouchPoints == 0 && pointId == lastTouchId) return info; if(nbTouchPoints == 0 && pointId == lastTouchId) return info;
// We fetch only the first touch point (the controller seems to handle only one anyway...) // We fetch only the first touch point (the controller seems to handle only one anyway...)
info.isTouch = true; info.isTouch = true;
auto xHigh = touchData[touchXHighIndex + (touchStep * i)] & 0x0f; auto xHigh = touchData[touchXHighIndex + (touchStep * i)] & 0x0f;
@ -106,7 +97,8 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() {
} }
void Cst816S::Sleep() { void Cst816S::Sleep() {
nrfx_twi_disable(&twi); // TODO re enable sleep mode
//twiMaster.Sleep();
nrf_gpio_cfg_default(6); nrf_gpio_cfg_default(6);
nrf_gpio_cfg_default(7); nrf_gpio_cfg_default(7);
} }

View File

@ -1,20 +1,21 @@
#pragma once #pragma once
#include <nrfx_twi.h> #include <nrfx_twi.h>
#include "TwiMaster.h"
namespace Pinetime { namespace Pinetime {
namespace Drivers { namespace Drivers {
class Cst816S { class Cst816S {
public : public :
enum class Gestures : uint8_t { enum class Gestures : uint8_t {
None = 0x00, None = 0x00,
SlideDown = 0x01, SlideDown = 0x01,
SlideUp = 0x02, SlideUp = 0x02,
SlideLeft = 0x03, SlideLeft = 0x03,
SlideRight = 0x04, SlideRight = 0x04,
SingleTap = 0x05, SingleTap = 0x05,
DoubleTap = 0x0B, DoubleTap = 0x0B,
LongPress = 0x0C LongPress = 0x0C
}; };
struct TouchInfos { struct TouchInfos {
uint16_t x; uint16_t x;
@ -27,21 +28,19 @@ namespace Pinetime {
bool isTouch = false; bool isTouch = false;
}; };
Cst816S() = default; Cst816S(TwiMaster& twiMaster, uint8_t twiAddress);
Cst816S(const Cst816S&) = delete; Cst816S(const Cst816S&) = delete;
Cst816S& operator=(const Cst816S&) = delete; Cst816S& operator=(const Cst816S&) = delete;
Cst816S(Cst816S&&) = delete; Cst816S(Cst816S&&) = delete;
Cst816S& operator=(Cst816S&&) = delete; Cst816S& operator=(Cst816S&&) = delete;
void Init(); void Init();
void Probe();
TouchInfos GetTouchInfo(); TouchInfos GetTouchInfo();
void Sleep(); void Sleep();
void Wakeup(); void Wakeup();
private: private:
static constexpr uint8_t pinIrq = 28; static constexpr uint8_t pinIrq = 28;
static constexpr uint8_t pinReset = 10; static constexpr uint8_t pinReset = 10;
static constexpr uint8_t address = 0x15;
static constexpr uint8_t lastTouchId = 0x0f; static constexpr uint8_t lastTouchId = 0x0f;
static constexpr uint8_t touchPointNumIndex = 2; static constexpr uint8_t touchPointNumIndex = 2;
static constexpr uint8_t touchMiscIndex = 8; static constexpr uint8_t touchMiscIndex = 8;
@ -56,11 +55,8 @@ namespace Pinetime {
static constexpr uint8_t gestureIndex = 1; static constexpr uint8_t gestureIndex = 1;
uint8_t touchData[63]; uint8_t touchData[63];
TwiMaster& twiMaster;
// TODO TWI (i²C) should be created outside and injected into this class uint8_t twiAddress;
// It will be needed when implementing other I²C devices
// (0x15 = touch, 0x18 = accelerometer, 0x44 = HR sensor)
nrfx_twi_t twi = NRFX_TWI_INSTANCE(1); // Use instance 1, because instance 0 is already used by SPI
}; };
} }

140
src/drivers/TwiMaster.cpp Normal file
View File

@ -0,0 +1,140 @@
#include <sdk/integration/nrfx/nrfx_log.h>
#include <sdk/modules/nrfx/hal/nrf_gpio.h>
#include <cstring>
#include "TwiMaster.h"
using namespace Pinetime::Drivers;
// TODO use shortcut to automatically send STOP when receive LastTX, for example
// TODO use DMA/IRQ
TwiMaster::TwiMaster(const Modules module, const Parameters& params) : module{module}, params{params} {
mutex = xSemaphoreCreateBinary();
}
void TwiMaster::Init() {
NRF_GPIO->PIN_CNF[params.pinScl] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
NRF_GPIO->PIN_CNF[params.pinSda] = ((uint32_t)GPIO_PIN_CNF_DIR_Input << GPIO_PIN_CNF_DIR_Pos)
| ((uint32_t)GPIO_PIN_CNF_INPUT_Connect << GPIO_PIN_CNF_INPUT_Pos)
| ((uint32_t)GPIO_PIN_CNF_PULL_Pullup << GPIO_PIN_CNF_PULL_Pos)
| ((uint32_t)GPIO_PIN_CNF_DRIVE_S0D1 << GPIO_PIN_CNF_DRIVE_Pos)
| ((uint32_t)GPIO_PIN_CNF_SENSE_Disabled << GPIO_PIN_CNF_SENSE_Pos);
switch(module) {
case Modules::TWIM1: twiBaseAddress = NRF_TWIM1; break;
default:
return;
}
switch(static_cast<Frequencies>(params.frequency)) {
case Frequencies::Khz100 : twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K100; break;
case Frequencies::Khz250 : twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K250; break;
case Frequencies::Khz400 : twiBaseAddress->FREQUENCY = TWIM_FREQUENCY_FREQUENCY_K400; break;
}
twiBaseAddress->PSEL.SCL = params.pinScl;
twiBaseAddress->PSEL.SDA = params.pinSda;
twiBaseAddress->EVENTS_LASTRX = 0;
twiBaseAddress->EVENTS_STOPPED = 0;
twiBaseAddress->EVENTS_LASTTX = 0;
twiBaseAddress->EVENTS_ERROR = 0;
twiBaseAddress->EVENTS_RXSTARTED = 0;
twiBaseAddress->EVENTS_SUSPENDED = 0;
twiBaseAddress->EVENTS_TXSTARTED = 0;
twiBaseAddress->ENABLE = (TWIM_ENABLE_ENABLE_Enabled << TWIM_ENABLE_ENABLE_Pos);
/* // IRQ
NVIC_ClearPendingIRQ(_IRQn);
NVIC_SetPriority(_IRQn, 2);
NVIC_EnableIRQ(_IRQn);
*/
xSemaphoreGive(mutex);
}
void TwiMaster::Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t *data, size_t size) {
xSemaphoreTake(mutex, portMAX_DELAY);
Write(deviceAddress, &registerAddress, 1, false);
Read(deviceAddress, data, size, true);
xSemaphoreGive(mutex);
}
void TwiMaster::Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t *data, size_t size) {
ASSERT(size <= maxDataSize);
xSemaphoreTake(mutex, portMAX_DELAY);
internalBuffer[0] = registerAddress;
std::memcpy(internalBuffer+1, data, size);
Write(deviceAddress, internalBuffer, size+1, true);
xSemaphoreGive(mutex);
}
void TwiMaster::Read(uint8_t deviceAddress, uint8_t *buffer, size_t size, bool stop) {
twiBaseAddress->ADDRESS = deviceAddress;
twiBaseAddress->TASKS_RESUME = 0x1UL;
twiBaseAddress->RXD.PTR = (uint32_t)buffer;
twiBaseAddress->RXD.MAXCNT = size;
twiBaseAddress->TASKS_STARTRX = 1;
while(!twiBaseAddress->EVENTS_RXSTARTED && !twiBaseAddress->EVENTS_ERROR);
twiBaseAddress->EVENTS_RXSTARTED = 0x0UL;
while(!twiBaseAddress->EVENTS_LASTRX && !twiBaseAddress->EVENTS_ERROR);
twiBaseAddress->EVENTS_LASTRX = 0x0UL;
if (stop || twiBaseAddress->EVENTS_ERROR) {
twiBaseAddress->TASKS_STOP = 0x1UL;
while(!twiBaseAddress->EVENTS_STOPPED);
twiBaseAddress->EVENTS_STOPPED = 0x0UL;
}
else {
twiBaseAddress->TASKS_SUSPEND = 0x1UL;
while(!twiBaseAddress->EVENTS_SUSPENDED);
twiBaseAddress->EVENTS_SUSPENDED = 0x0UL;
}
if (twiBaseAddress->EVENTS_ERROR) {
twiBaseAddress->EVENTS_ERROR = 0x0UL;
}
}
void TwiMaster::Write(uint8_t deviceAddress, const uint8_t *data, size_t size, bool stop) {
twiBaseAddress->ADDRESS = deviceAddress;
twiBaseAddress->TASKS_RESUME = 0x1UL;
twiBaseAddress->TXD.PTR = (uint32_t)data;
twiBaseAddress->TXD.MAXCNT = size;
twiBaseAddress->TASKS_STARTTX = 1;
while(!twiBaseAddress->EVENTS_TXSTARTED && !twiBaseAddress->EVENTS_ERROR);
twiBaseAddress->EVENTS_TXSTARTED = 0x0UL;
while(!twiBaseAddress->EVENTS_LASTTX && !twiBaseAddress->EVENTS_ERROR);
twiBaseAddress->EVENTS_LASTTX = 0x0UL;
if (stop || twiBaseAddress->EVENTS_ERROR) {
twiBaseAddress->TASKS_STOP = 0x1UL;
while(!twiBaseAddress->EVENTS_STOPPED);
twiBaseAddress->EVENTS_STOPPED = 0x0UL;
}
else {
twiBaseAddress->TASKS_SUSPEND = 0x1UL;
while(!twiBaseAddress->EVENTS_SUSPENDED);
twiBaseAddress->EVENTS_SUSPENDED = 0x0UL;
}
if (twiBaseAddress->EVENTS_ERROR) {
twiBaseAddress->EVENTS_ERROR = 0x0UL;
uint32_t error = twiBaseAddress->ERRORSRC;
twiBaseAddress->ERRORSRC = error;
}
}

38
src/drivers/TwiMaster.h Normal file
View File

@ -0,0 +1,38 @@
#pragma once
#include <FreeRTOS.h>
#include <semphr.h>
#include <drivers/include/nrfx_twi.h>
namespace Pinetime {
namespace Drivers {
class TwiMaster {
public:
enum class Modules { TWIM1 };
enum class Frequencies {Khz100, Khz250, Khz400};
struct Parameters {
uint32_t frequency;
uint8_t pinSda;
uint8_t pinScl;
};
TwiMaster(const Modules module, const Parameters& params);
void Init();
void Read(uint8_t deviceAddress, uint8_t registerAddress, uint8_t* buffer, size_t size);
void Write(uint8_t deviceAddress, uint8_t registerAddress, const uint8_t* data, size_t size);
private:
void Read(uint8_t deviceAddress, uint8_t* buffer, size_t size, bool stop);
void Write(uint8_t deviceAddress, const uint8_t* data, size_t size, bool stop);
NRF_TWIM_Type* twiBaseAddress;
SemaphoreHandle_t mutex;
const Modules module;
const Parameters params;
static constexpr uint8_t maxDataSize{8};
static constexpr uint8_t registerSize{1};
uint8_t internalBuffer[maxDataSize + registerSize];
};
}
}

View File

@ -42,6 +42,9 @@ static constexpr uint8_t pinSpiMiso = 4;
static constexpr uint8_t pinSpiFlashCsn = 5; static constexpr uint8_t pinSpiFlashCsn = 5;
static constexpr uint8_t pinLcdCsn = 25; static constexpr uint8_t pinLcdCsn = 25;
static constexpr uint8_t pinLcdDataCommand = 18; static constexpr uint8_t pinLcdDataCommand = 18;
static constexpr uint8_t pinTwiScl = 7;
static constexpr uint8_t pinTwiSda = 6;
static constexpr uint8_t touchPanelTwiAddress = 0x15;
Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, { Pinetime::Drivers::SpiMaster spi{Pinetime::Drivers::SpiMaster::SpiModule::SPI0, {
Pinetime::Drivers::SpiMaster::BitOrder::Msb_Lsb, Pinetime::Drivers::SpiMaster::BitOrder::Msb_Lsb,
@ -58,7 +61,15 @@ Pinetime::Drivers::St7789 lcd {lcdSpi, pinLcdDataCommand};
Pinetime::Drivers::Spi flashSpi {spi, pinSpiFlashCsn}; Pinetime::Drivers::Spi flashSpi {spi, pinSpiFlashCsn};
Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi}; Pinetime::Drivers::SpiNorFlash spiNorFlash {flashSpi};
Pinetime::Drivers::Cst816S touchPanel {};
// The TWI device should work @ up to 400Khz but there is a HW bug which prevent it from
// respecting correct timings. According to erratas heet, this magic value makes it run
// at ~390Khz with correct timings.
static constexpr uint32_t MaxTwiFrequencyWithoutHardwareBug{0x06200000};
Pinetime::Drivers::TwiMaster twiMaster{Pinetime::Drivers::TwiMaster::Modules::TWIM1,
Pinetime::Drivers::TwiMaster::Parameters {
MaxTwiFrequencyWithoutHardwareBug, pinTwiSda, pinTwiScl}};
Pinetime::Drivers::Cst816S touchPanel {twiMaster, touchPanelTwiAddress};
Pinetime::Components::LittleVgl lvgl {lcd, touchPanel}; Pinetime::Components::LittleVgl lvgl {lcd, touchPanel};
@ -213,7 +224,7 @@ int main(void) {
debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback); debounceTimer = xTimerCreate ("debounceTimer", 200, pdFALSE, (void *) 0, DebounceTimerCallback);
systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, spiNorFlash, touchPanel, lvgl, batteryController, bleController, systemTask.reset(new Pinetime::System::SystemTask(spi, lcd, spiNorFlash, twiMaster, touchPanel, lvgl, batteryController, bleController,
dateTimeController, notificationManager)); dateTimeController, notificationManager));
systemTask->Start(); systemTask->Start();
nimble_port_init(); nimble_port_init();

View File

@ -5244,7 +5244,7 @@
// <e> NRFX_TWI_ENABLED - nrfx_twi - TWI peripheral driver // <e> NRFX_TWI_ENABLED - nrfx_twi - TWI peripheral driver
//========================================================== //==========================================================
#ifndef NRFX_TWI_ENABLED #ifndef NRFX_TWI_ENABLED
#define NRFX_TWI_ENABLED 1 #define NRFX_TWI_ENABLED 0
#endif #endif
// <q> NRFX_TWI0_ENABLED - Enable TWI0 instance // <q> NRFX_TWI0_ENABLED - Enable TWI0 instance
@ -5257,7 +5257,7 @@
#ifndef NRFX_TWI1_ENABLED #ifndef NRFX_TWI1_ENABLED
#define NRFX_TWI1_ENABLED 1 #define NRFX_TWI1_ENABLED 0
#endif #endif
// <o> NRFX_TWI_DEFAULT_CONFIG_FREQUENCY - Frequency // <o> NRFX_TWI_DEFAULT_CONFIG_FREQUENCY - Frequency