From 6af5bbcbc87eecccf7613785a3c9540c07d2fd3b Mon Sep 17 00:00:00 2001 From: JF Date: Sun, 19 Jul 2020 20:30:44 +0200 Subject: New implementation of the I²C/TWI driver. Fix reset timing and add dummy reading in Cst816S to fix init error on some devices. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/drivers/Cst816s.cpp | 54 +++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 31 deletions(-) (limited to 'src/drivers/Cst816s.cpp') 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 -- cgit v1.2.3