summaryrefslogtreecommitdiff
path: root/src/drivers
diff options
context:
space:
mode:
Diffstat (limited to 'src/drivers')
-rw-r--r--src/drivers/Cst816s.cpp54
-rw-r--r--src/drivers/Cst816s.h30
-rw-r--r--src/drivers/TwiMaster.cpp140
-rw-r--r--src/drivers/TwiMaster.h38
4 files changed, 214 insertions, 48 deletions
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_set(pinReset);
+ vTaskDelay(50);
nrf_gpio_pin_clear(pinReset);
- vTaskDelay(20);
+ vTaskDelay(5);
nrf_gpio_pin_set(pinReset);
- vTaskDelay(200);
-
- 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;
-
- // Configure TWI in blocking mode (event_handler = nullptr)
- auto ret = nrfx_twi_init(&twi, &config, nullptr, this);
- nrfx_twi_enable(&twi);
-}
+ vTaskDelay(50);
+ // Wake the touchpanel up
+ uint8_t dummy;
+ twiMaster.Read(twiAddress, 0x15, &dummy, 1);
+ vTaskDelay(5);
+ twiMaster.Read(twiAddress, 0xa7, &dummy, 1);
-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 <nrfx_twi.h>
+#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..4a0c536d
--- /dev/null
+++ b/src/drivers/TwiMaster.cpp
@@ -0,0 +1,140 @@
+#include <nrfx_log.h>
+#include <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;
+ }
+} \ 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 <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];
+
+ };
+ }
+} \ No newline at end of file