From 6af5bbcbc87eecccf7613785a3c9540c07d2fd3b Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 19 Jul 2020 20:30:44 +0200 Subject: [PATCH 1/3] =?UTF-8?q?New=20implementation=20of=20the=20I=C2=B2C/?= =?UTF-8?q?TWI=20driver.=20Fix=20reset=20timing=20and=20add=20dummy=20read?= =?UTF-8?q?ing=20in=20Cst816S=20to=20fix=20init=20error=20on=20some=20devi?= =?UTF-8?q?ces.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/CMakeLists.txt | 2 + src/SystemTask/SystemTask.cpp | 7 +- src/SystemTask/SystemTask.h | 4 +- src/drivers/Cst816s.cpp | 54 ++++++------- src/drivers/Cst816s.h | 30 ++++---- src/drivers/TwiMaster.cpp | 140 ++++++++++++++++++++++++++++++++++ src/drivers/TwiMaster.h | 38 +++++++++ src/main.cpp | 15 +++- src/sdk_config.h | 4 +- 9 files changed, 239 insertions(+), 55 deletions(-) create mode 100644 src/drivers/TwiMaster.cpp create mode 100644 src/drivers/TwiMaster.h diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ff2e1812..e60c2cfe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -371,6 +371,7 @@ list(APPEND SOURCE_FILES DisplayApp/Fonts/jetbrains_mono_bold_20.c SystemTask/SystemTask.cpp + drivers/TwiMaster.cpp ) list(APPEND GRAPHICS_SOURCE_FILES @@ -444,6 +445,7 @@ set(INCLUDE_FILES SystemTask/SystemTask.h SystemTask/SystemMonitor.h DisplayApp/Screens/Symbols.h + drivers/TwiMaster.h ) include_directories( diff --git a/src/SystemTask/SystemTask.cpp b/src/SystemTask/SystemTask.cpp index 39e9751b..8f565860 100644 --- a/src/SystemTask/SystemTask.cpp +++ b/src/SystemTask/SystemTask.cpp @@ -24,12 +24,14 @@ void IdleTimerCallback(TimerHandle_t xTimer) { 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, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, 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}, watchdog{}, watchdogView{watchdog}, notificationManager{notificationManager}, nimbleController(*this, bleController,dateTimeController, notificationManager, spiNorFlash) { @@ -67,6 +69,7 @@ void SystemTask::Work() { nimbleController.StartAdvertising(); lcd.Init(); + twiMaster.Init(); touchPanel.Init(); batteryController.Init(); diff --git a/src/SystemTask/SystemTask.h b/src/SystemTask/SystemTask.h index 860647a1..8fa7e7d1 100644 --- a/src/SystemTask/SystemTask.h +++ b/src/SystemTask/SystemTask.h @@ -21,7 +21,8 @@ namespace Pinetime { }; 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, Controllers::Battery &batteryController, Controllers::Ble &bleController, Controllers::DateTime &dateTimeController, @@ -42,6 +43,7 @@ namespace Pinetime { Pinetime::Drivers::SpiMaster& spi; Pinetime::Drivers::St7789& lcd; Pinetime::Drivers::SpiNorFlash& spiNorFlash; + Pinetime::Drivers::TwiMaster& twiMaster; Pinetime::Drivers::Cst816S& touchPanel; Pinetime::Components::LittleVgl& lvgl; Pinetime::Controllers::Battery& batteryController; diff --git a/src/drivers/Cst816s.cpp b/src/drivers/Cst816s.cpp index 61bce94c..60cd402c 100644 --- a/src/drivers/Cst816s.cpp +++ b/src/drivers/Cst816s.cpp @@ -13,51 +13,42 @@ using namespace Pinetime::Drivers; * 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_pin_clear(pinReset); - vTaskDelay(20); 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; - config.frequency = NRF_TWI_FREQ_400K; - config.scl = 7; - config.sda = 6; - config.interrupt_priority = NRFX_TWI_DEFAULT_CONFIG_IRQ_PRIORITY; - config.hold_bus_uninit = NRFX_TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT; + // Wake the touchpanel up + uint8_t dummy; + twiMaster.Read(twiAddress, 0x15, &dummy, 1); + vTaskDelay(5); + twiMaster.Read(twiAddress, 0xa7, &dummy, 1); - // 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 info; - nrfx_twi_rx(&twi, address, touchData, 63); + twiMaster.Read(twiAddress, 0, touchData, 63); auto nbTouchPoints = touchData[2] & 0x0f; // uint8_t i = 0; // NRF_LOG_INFO("#########################") for(int i = 0; i < 1; i++) { - uint8_t pointId = (touchData[touchIdIndex + (touchStep * i)]) >> 4; - if(nbTouchPoints == 0 && pointId == lastTouchId) return info; + uint8_t pointId = (touchData[touchIdIndex + (touchStep * i)]) >> 4; + if(nbTouchPoints == 0 && pointId == lastTouchId) return info; - // We fetch only the first touch point (the controller seems to handle only one anyway...) - info.isTouch = true; + // We fetch only the first touch point (the controller seems to handle only one anyway...) + info.isTouch = true; auto xHigh = touchData[touchXHighIndex + (touchStep * i)] & 0x0f; @@ -106,11 +97,12 @@ Cst816S::TouchInfos Cst816S::GetTouchInfo() { } void Cst816S::Sleep() { - nrfx_twi_disable(&twi); + // TODO re enable sleep mode + //twiMaster.Sleep(); nrf_gpio_cfg_default(6); nrf_gpio_cfg_default(7); } void Cst816S::Wakeup() { Init(); -} +} \ No newline at end of file diff --git a/src/drivers/Cst816s.h b/src/drivers/Cst816s.h index 4a5dda60..b115a688 100644 --- a/src/drivers/Cst816s.h +++ b/src/drivers/Cst816s.h @@ -1,20 +1,21 @@ #pragma once #include +#include "TwiMaster.h" namespace Pinetime { namespace Drivers { class Cst816S { public : enum class Gestures : uint8_t { - None = 0x00, - SlideDown = 0x01, - SlideUp = 0x02, - SlideLeft = 0x03, - SlideRight = 0x04, - SingleTap = 0x05, - DoubleTap = 0x0B, - LongPress = 0x0C + None = 0x00, + SlideDown = 0x01, + SlideUp = 0x02, + SlideLeft = 0x03, + SlideRight = 0x04, + SingleTap = 0x05, + DoubleTap = 0x0B, + LongPress = 0x0C }; struct TouchInfos { uint16_t x; @@ -27,21 +28,19 @@ namespace Pinetime { bool isTouch = false; }; - Cst816S() = default; + Cst816S(TwiMaster& twiMaster, uint8_t twiAddress); Cst816S(const Cst816S&) = delete; Cst816S& operator=(const Cst816S&) = delete; Cst816S(Cst816S&&) = delete; Cst816S& operator=(Cst816S&&) = delete; void Init(); - void Probe(); TouchInfos GetTouchInfo(); void Sleep(); void Wakeup(); private: static constexpr uint8_t pinIrq = 28; static constexpr uint8_t pinReset = 10; - static constexpr uint8_t address = 0x15; static constexpr uint8_t lastTouchId = 0x0f; static constexpr uint8_t touchPointNumIndex = 2; static constexpr uint8_t touchMiscIndex = 8; @@ -56,12 +55,9 @@ namespace Pinetime { static constexpr uint8_t gestureIndex = 1; uint8_t touchData[63]; - - // TODO TWI (i²C) should be created outside and injected into this class - // 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 + TwiMaster& twiMaster; + uint8_t twiAddress; }; } -} +} \ No newline at end of file diff --git a/src/drivers/TwiMaster.cpp b/src/drivers/TwiMaster.cpp new file mode 100644 index 00000000..5d8fcf6b --- /dev/null +++ b/src/drivers/TwiMaster.cpp @@ -0,0 +1,140 @@ +#include +#include +#include +#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(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, ®isterAddress, 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; + } +} \ No newline at end of file diff --git a/src/drivers/TwiMaster.h b/src/drivers/TwiMaster.h new file mode 100644 index 00000000..3b7555f5 --- /dev/null +++ b/src/drivers/TwiMaster.h @@ -0,0 +1,38 @@ +#pragma once +#include +#include +#include + + +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]; + + }; + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 41943892..fe413585 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -42,6 +42,9 @@ static constexpr uint8_t pinSpiMiso = 4; static constexpr uint8_t pinSpiFlashCsn = 5; static constexpr uint8_t pinLcdCsn = 25; 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::BitOrder::Msb_Lsb, @@ -58,7 +61,15 @@ Pinetime::Drivers::St7789 lcd {lcdSpi, pinLcdDataCommand}; Pinetime::Drivers::Spi flashSpi {spi, pinSpiFlashCsn}; 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}; @@ -213,7 +224,7 @@ int main(void) { 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)); systemTask->Start(); nimble_port_init(); diff --git a/src/sdk_config.h b/src/sdk_config.h index e83bde4b..aa1bbb3a 100644 --- a/src/sdk_config.h +++ b/src/sdk_config.h @@ -5244,7 +5244,7 @@ // NRFX_TWI_ENABLED - nrfx_twi - TWI peripheral driver //========================================================== #ifndef NRFX_TWI_ENABLED -#define NRFX_TWI_ENABLED 1 +#define NRFX_TWI_ENABLED 0 #endif // NRFX_TWI0_ENABLED - Enable TWI0 instance @@ -5257,7 +5257,7 @@ #ifndef NRFX_TWI1_ENABLED -#define NRFX_TWI1_ENABLED 1 +#define NRFX_TWI1_ENABLED 0 #endif // NRFX_TWI_DEFAULT_CONFIG_FREQUENCY - Frequency From 377fca9e58a7f14d9b923eee9fc9e9f5ed5b70eb Mon Sep 17 00:00:00 2001 From: JF Date: Mon, 20 Jul 2020 21:28:06 +0200 Subject: [PATCH 2/3] Set version to 0.7.1 --- CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 51ac2d18..ed8abfc9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(pinetime VERSION 0.7.0 LANGUAGES C CXX ASM) +project(pinetime VERSION 0.7.1 LANGUAGES C CXX ASM) set(NRF_TARGET "nrf52") From 55417794551211c0e86eebc661b76339d35de8ef Mon Sep 17 00:00:00 2001 From: JF Date: Mon, 20 Jul 2020 21:30:00 +0200 Subject: [PATCH 3/3] Fix broken link in README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b5554ed5..643c3717 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ As of now, here is the list of achievements of this project: - [BLE implementation and API](./doc/ble.md) ### Architecture and technical topics - - [Memory analysis](./doc/MemoryAnalysis) + - [Memory analysis](./doc/MemoryAnalysis.md) ### Using the firmware - Integration with Gadgetbridge