Merge pull request #2 from JF002/develop
merge update from JF002 into Develop
This commit is contained in:
commit
445e7f38e2
|
@ -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")
|
||||
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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(
|
||||
|
|
|
@ -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();
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -13,41 +13,32 @@ 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;
|
||||
|
@ -106,7 +97,8 @@ 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);
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <nrfx_twi.h>
|
||||
#include "TwiMaster.h"
|
||||
|
||||
namespace Pinetime {
|
||||
namespace Drivers {
|
||||
|
@ -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,11 +55,8 @@ 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;
|
||||
};
|
||||
|
||||
}
|
||||
|
|
140
src/drivers/TwiMaster.cpp
Normal file
140
src/drivers/TwiMaster.cpp
Normal 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, ®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;
|
||||
}
|
||||
}
|
38
src/drivers/TwiMaster.h
Normal file
38
src/drivers/TwiMaster.h
Normal 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];
|
||||
|
||||
};
|
||||
}
|
||||
}
|
15
src/main.cpp
15
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();
|
||||
|
|
|
@ -5244,7 +5244,7 @@
|
|||
// <e> NRFX_TWI_ENABLED - nrfx_twi - TWI peripheral driver
|
||||
//==========================================================
|
||||
#ifndef NRFX_TWI_ENABLED
|
||||
#define NRFX_TWI_ENABLED 1
|
||||
#define NRFX_TWI_ENABLED 0
|
||||
#endif
|
||||
// <q> 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
|
||||
|
||||
// <o> NRFX_TWI_DEFAULT_CONFIG_FREQUENCY - Frequency
|
||||
|
|
Loading…
Reference in New Issue
Block a user