From 78dc6e4b8eef69ee87a1e15fb380080fee95b550 Mon Sep 17 00:00:00 2001 From: Joel Bodenmann Date: Tue, 21 Jul 2015 15:04:49 +0200 Subject: [PATCH] Adding FT5336 touch driver. Not tested yet. --- boards/base/STM32F746-Discovery/board.mk | 3 +- .../gmouse_lld_FT5336_board.h | 226 ++++++++++++++++ .../stm32f746g_raw32_system.c | 4 +- .../stm32f746g_raw32_ugfx.c | 47 +++- drivers/ginput/touch/FT5336/driver.mk | 2 + drivers/ginput/touch/FT5336/ft5336.h | 241 ++++++++++++++++++ .../ginput/touch/FT5336/gmouse_lld_FT5336.c | 120 +++++++++ .../FT5336/gmouse_lld_FT5336_board_template.h | 59 +++++ 8 files changed, 697 insertions(+), 5 deletions(-) create mode 100644 boards/base/STM32F746-Discovery/gmouse_lld_FT5336_board.h create mode 100644 drivers/ginput/touch/FT5336/driver.mk create mode 100644 drivers/ginput/touch/FT5336/ft5336.h create mode 100644 drivers/ginput/touch/FT5336/gmouse_lld_FT5336.c create mode 100644 drivers/ginput/touch/FT5336/gmouse_lld_FT5336_board_template.h diff --git a/boards/base/STM32F746-Discovery/board.mk b/boards/base/STM32F746-Discovery/board.mk index 35e62757..864f2220 100644 --- a/boards/base/STM32F746-Discovery/board.mk +++ b/boards/base/STM32F746-Discovery/board.mk @@ -27,4 +27,5 @@ ifeq ($(OPT_OS),raw32) LDSCRIPT = $(GFXLIB)/boards/base/STM32F746-Discovery/stm32f746nghx_flash.ld endif -include $(GFXLIB)/drivers/gdisp/STM32LTDC/driver.mk \ No newline at end of file +include $(GFXLIB)/drivers/gdisp/STM32LTDC/driver.mk +include $(GFXLIB)/drivers/ginput/touch/FT5336/driver.mk \ No newline at end of file diff --git a/boards/base/STM32F746-Discovery/gmouse_lld_FT5336_board.h b/boards/base/STM32F746-Discovery/gmouse_lld_FT5336_board.h new file mode 100644 index 00000000..bf523212 --- /dev/null +++ b/boards/base/STM32F746-Discovery/gmouse_lld_FT5336_board.h @@ -0,0 +1,226 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#ifndef _GINPUT_LLD_MOUSE_BOARD_H +#define _GINPUT_LLD_MOUSE_BOARD_H + +#include "stm32f7xx.h" + +// Resolution and Accuracy Settings +#define GMOUSE_FT5336_PEN_CALIBRATE_ERROR 8 +#define GMOUSE_FT5336_PEN_CLICK_ERROR 6 +#define GMOUSE_FT5336_PEN_MOVE_ERROR 4 +#define GMOUSE_FT5336_FINGER_CALIBRATE_ERROR 14 +#define GMOUSE_FT5336_FINGER_CLICK_ERROR 18 +#define GMOUSE_FT5336_FINGER_MOVE_ERROR 14 + +// How much extra data to allocate at the end of the GMouse structure for the board's use +#define GMOUSE_FT5336_BOARD_DATA_SIZE 0 + +// Set this to TRUE if you want self-calibration. +// NOTE: This is not as accurate as real calibration. +// It requires the orientation of the touch panel to match the display. +// It requires the active area of the touch panel to exactly match the display size. +#define GMOUSE_FT5336_SELF_CALIBRATE FALSE + +// The FT5336 slave address +#define FT5336_ADDR 0x70 + +static bool_t init_board(GMouse* m, unsigned instance) +{ + (void)m; + (void)instance; + + // I2C3_SCL GPIOH7, alternate, opendrain, highspeed + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // Enable clock for + GPIOB->MODER |= GPIO_MODER_MODER7_1; // Alternate function + GPIOB->OTYPER |= GPIO_OTYPER_OT_7; // OpenDrain + GPIOB->PUPDR |= GPIO_PUPDR_PUPDR7_0; // PullUp + GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR7; // HighSpeed + GPIOB->AFR[0] |= (0b0100 << 4*0); // AF4 + + // I2C3_SDA GPIOH8, alternate, opendrain, highspeed + RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // Enable clock + GPIOB->MODER |= GPIO_MODER_MODER8_1; // Alternate function + GPIOB->OTYPER |= GPIO_OTYPER_OT_8; // OpenDrain + GPIOB->PUPDR |= GPIO_PUPDR_PUPDR8_0; // PullUp + GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR8; // HighSpeed + GPIOB->AFR[1] |= (0b0100 << 4*0); // AF4 + + // Enable I2C3 peripheral clock + RCC->APB1ENR |= RCC_APB1ENR_I2C3EN; + + // Reset I2C3 peripheral + RCC->APB1RSTR |= RCC_APB1RSTR_I2C3RST; + RCC->APB1RSTR &= ~RCC_APB1RSTR_I2C3RST; + + // Set Fm mode + I2C3->CCR |= I2C_CCR_FS; + + // Set Duty to 50:50 + I2C3->CCR &= ~I2C_CCR_DUTY; + + // Set peripheral clock frequency (APB1 frequency) + // APB1CLK running at 42 MHz + I2C3->CR2 |= 42; + + // Set I2C bus clock speed to 400 kHz + // Period of 400 kHz is 2.5 us, half of that is 1.25 us. TPCLK1 is + // 40 ns (see below). 1.25 us / 40 ns = 32. + I2C3->CCR |= (I2C_CCR_CCR & 32); + + // Rise time + // Period of 42 MHz is 24 ns. Rise time is 1000 ns. 1000/24 = 42. + I2C3->TRISE |= (I2C_TRISE_TRISE & 42); + + // Disable POS + I2C3->CR1 &=~ I2C_CR1_POS; + + // Enable I2C3 + I2C3->CR1 |= I2C_CR1_PE; + + return TRUE; +} + +static void write_reg(GMouse* m, uint8_t reg, uint8_t val) +{ + (void)m; + + // Generate start condition + I2C3->CR1 |= I2C_CR1_START; + while (!(I2C3->SR2 & I2C_SR2_MSL)); + while (!(I2C3->SR1 & I2C_SR1_SB)); + + // Send slave address (Write = last bit is 0) + I2C3->DR = ((FT5336_ADDR | 0) & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_ADDR)); + + // Read SR1/SR2 register to clear the SB bit (see STM32F4xx RM) + (void)I2C3->SR1; + (void)I2C3->SR2; + + // Send register address + while (!(I2C3->SR1 & I2C_SR1_TXE)); + I2C3->DR = (reg & I2C_DR_DR); + + // Send data + while (!(I2C3->SR1 & I2C_SR1_TXE)); + I2C3->DR = (val & I2C_DR_DR); + + // Generate stop condition when we are done transmitting + while (!(I2C3->SR1 & I2C_SR1_TXE)); + I2C3->CR1 |= I2C_CR1_STOP; +} + +static uint8_t read_byte(GMouse* m, uint8_t reg) +{ + (void)m; + + uint8_t ret = 0x00; + + // Generate start condition + I2C3->CR1 |= I2C_CR1_START; + while (!(I2C3->SR2 & I2C_SR2_MSL)); + while (!(I2C3->SR1 & I2C_SR1_SB)); + + // Send slave address (Write = last bit is 0) + I2C3->DR = ((FT5336_ADDR | 0) & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_ADDR)); + + // Read SR1/SR2 register to clear the SB bit (see STM32F4xx RM) + (void)I2C3->SR1; + (void)I2C3->SR2; + + // Send register address that we want to read + I2C3->DR = (reg & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_BTF)); + + // Generate start condition (repeated start) + I2C3->CR1 |= I2C_CR1_START; + while (!(I2C3->SR2 & I2C_SR2_MSL)); + while (!(I2C3->SR1 & I2C_SR1_SB)); + + // Send slave address (Read = last bit is 1) + I2C3->DR = ((FT5336_ADDR | 1) & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_ADDR)); + + // Set up for one byte receival + I2C3->CR1 &= ~I2C_CR1_POS; + //I2C3->CR1 |= I2C_CR1_ACK; + + // Read SR1/SR2 register to clear the SB bit (see STM32F4xx RM) + (void)I2C3->SR1; + (void)I2C3->SR2; + + // Clean SR1_ACK. This needs to be done on the last byte received from slave + I2C3->CR1 &= ~I2C_CR1_ACK; + + // Generate stop condition + I2C3->CR1 |= I2C_CR1_STOP; + + return ret; +} + +static uint16_t read_word(GMouse* m, uint8_t reg) +{ + (void)m; + + uint16_t ret = 0x00; + + // Generate start condition + I2C3->CR1 |= I2C_CR1_START; + while (!(I2C3->SR2 & I2C_SR2_MSL)); + while (!(I2C3->SR1 & I2C_SR1_SB)); + + // Send slave address (Write = last bit is 0) + I2C3->DR = ((FT5336_ADDR | 0) & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_ADDR)); + + // Read SR1/SR2 register to clear the SB bit (see STM32F4xx RM) + (void)I2C3->SR1; + (void)I2C3->SR2; + + // Send register address that we want to read + I2C3->DR = (reg & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_BTF)); + + // Generate start condition (repeated start) + I2C3->CR1 |= I2C_CR1_START; + while (!(I2C3->SR2 & I2C_SR2_MSL)); + while (!(I2C3->SR1 & I2C_SR1_SB)); + + // Send slave address (Read = last bit is 1) + I2C3->DR = ((FT5336_ADDR | 1) & I2C_DR_DR); + while (!(I2C3->SR1 & I2C_SR1_ADDR)); + + // Set up for two byte receival + I2C3->CR1 |= I2C_CR1_POS; + //I2C3->CR1 &= ~I2C_CR1_ACK; + I2C3->SR1 &= ~I2C_SR1_ADDR; + + // Read SR1/SR2 register to clear the SB bit (see STM32F4xx RM) + (void)I2C3->SR1; + (void)I2C3->SR2; + + // The slave should now send a byte to us. + while (!(I2C3->SR1 & I2C_SR1_RXNE)); + ret = (uint16_t)((I2C3->DR & 0x00FF) << 8); + + // Set STOP and clear ACK right after reading the second last byte + I2C3->CR1 |= I2C_CR1_STOP; + I2C3->CR1 &= ~I2C_CR1_ACK; + + // The second byte becomes available after sending the stop condition + ret |= (I2C3->DR & 0x00FF); + + // Get back to original state + I2C3->CR1 &= ~I2C_CR1_POS; + + return ret; +} + +#endif /* _GINPUT_LLD_MOUSE_BOARD_H */ diff --git a/boards/base/STM32F746-Discovery/stm32f746g_raw32_system.c b/boards/base/STM32F746-Discovery/stm32f746g_raw32_system.c index 9ffe25ee..f7c4952d 100644 --- a/boards/base/STM32F746-Discovery/stm32f746g_raw32_system.c +++ b/boards/base/STM32F746-Discovery/stm32f746g_raw32_system.c @@ -176,7 +176,9 @@ void SystemInit(void) RCC->CR &= (uint32_t)0xFEF6FFFF; /* Reset PLLCFGR register */ - RCC->PLLCFGR = 0x24003010; + //RCC->PLLCFGR = 0x24003010; // From discovery example + // M = 12 = 0b1100, N = 192 = 0b11000000, P = 2 = 0b10, Q = 2 = 0b10 + RCC->PLLCFGR = 0x00C0980C; /* Reset HSEBYP bit */ RCC->CR &= (uint32_t)0xFFFBFFFF; diff --git a/boards/base/STM32F746-Discovery/stm32f746g_raw32_ugfx.c b/boards/base/STM32F746-Discovery/stm32f746g_raw32_ugfx.c index 5fde982e..794d3c66 100644 --- a/boards/base/STM32F746-Discovery/stm32f746g_raw32_ugfx.c +++ b/boards/base/STM32F746-Discovery/stm32f746g_raw32_ugfx.c @@ -64,6 +64,7 @@ void Raw32OSInit(void) { */ void SystemClock_Config(void) { +#if 0 RCC_ClkInitTypeDef RCC_ClkInitStruct; RCC_OscInitTypeDef RCC_OscInitStruct; HAL_StatusTypeDef ret = HAL_OK; @@ -73,10 +74,10 @@ void SystemClock_Config(void) RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; - RCC_OscInitStruct.PLL.PLLM = 25; - RCC_OscInitStruct.PLL.PLLN = 400; // 432 + RCC_OscInitStruct.PLL.PLLM = 12; + RCC_OscInitStruct.PLL.PLLN = 192; // 432 RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; - RCC_OscInitStruct.PLL.PLLQ = 8; // 9 + RCC_OscInitStruct.PLL.PLLQ = 2; // 9 ret = HAL_RCC_OscConfig(&RCC_OscInitStruct); if(ret != HAL_OK) @@ -103,6 +104,46 @@ void SystemClock_Config(void) { while(1) { ; } } +#else + + RCC_OscInitTypeDef RCC_OscInitStruct; + RCC_ClkInitTypeDef RCC_ClkInitStruct; + RCC_PeriphCLKInitTypeDef PeriphClkInitStruct; + + __PWR_CLK_ENABLE(); + + __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); + + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSE; + RCC_OscInitStruct.HSEState = RCC_HSE_ON; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; + RCC_OscInitStruct.HSICalibrationValue = 16; + RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; + RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; + RCC_OscInitStruct.PLL.PLLM = 12; + RCC_OscInitStruct.PLL.PLLN = 192; + RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; + RCC_OscInitStruct.PLL.PLLQ = 2; + HAL_RCC_OscConfig(&RCC_OscInitStruct); + + HAL_PWREx_ActivateOverDrive(); + + RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK + |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; + RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; + RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; + RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4; + RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2; + HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_6); + + PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_I2C1; + PeriphClkInitStruct.I2c1ClockSelection = RCC_I2C1CLKSOURCE_PCLK1; + HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct); + + HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000); + + HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK); +#endif } /** diff --git a/drivers/ginput/touch/FT5336/driver.mk b/drivers/ginput/touch/FT5336/driver.mk new file mode 100644 index 00000000..be3cab6e --- /dev/null +++ b/drivers/ginput/touch/FT5336/driver.mk @@ -0,0 +1,2 @@ +# List the required driver. +GFXSRC += $(GFXLIB)/drivers/ginput/touch/FT5336/gmouse_lld_FT5336.c diff --git a/drivers/ginput/touch/FT5336/ft5336.h b/drivers/ginput/touch/FT5336/ft5336.h new file mode 100644 index 00000000..4940cc42 --- /dev/null +++ b/drivers/ginput/touch/FT5336/ft5336.h @@ -0,0 +1,241 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#ifndef _FT5336_H +#define _FT5336_H + +// I2C Slave address of touchscreen FocalTech FT5336 +#define FT5336_I2C_SLAVE_ADDRESS 0x70 + +// Maximum border values of the touchscreen pad +#define FT5336_MAX_WIDTH 480 // Touchscreen pad max width +#define FT5336_MAX_HEIGHT 272 // Touchscreen pad max height + +// Possible values of driver functions return status +#define FT5336_STATUS_OK 0x00 +#define FT5336_STATUS_NOT_OK 0x01 + +// Possible values of global variable 'TS_I2C_Initialized' +#define FT5336_I2C_NOT_INITIALIZED 0x00 +#define FT5336_I2C_INITIALIZED 0x01 + +// Max detectable simultaneous touches +#define FT5336_MAX_DETECTABLE_TOUCH 0x05 + +// Current mode register of the FT5336 (R)/W +#define FT5336_DEV_MODE_REG 0x00 + +// Possible values of FT5336_DEV_MODE_REG +#define FT5336_DEV_MODE_WORKING 0x00 +#define FT5336_DEV_MODE_FACTORY 0x04 + +#define FT5336_DEV_MODE_MASK 0x07 +#define FT5336_DEV_MODE_SHIFT 0x04 + +// Gesture ID register +#define FT5336_GEST_ID_REG 0x01 + +// Possible values of FT5336_GEST_ID_REG +#define FT5336_GEST_ID_NO_GESTURE 0x00 +#define FT5336_GEST_ID_MOVE_UP 0x10 +#define FT5336_GEST_ID_MOVE_RIGHT 0x14 +#define FT5336_GEST_ID_MOVE_DOWN 0x18 +#define FT5336_GEST_ID_MOVE_LEFT 0x1C +#define FT5336_GEST_ID_SINGLE_CLICK 0x20 +#define FT5336_GEST_ID_DOUBLE_CLICK 0x22 +#define FT5336_GEST_ID_ROTATE_CLOCKWISE 0x28 +#define FT5336_GEST_ID_ROTATE_C_CLOCKWISE 0x29 +#define FT5336_GEST_ID_ZOOM_IN 0x40 +#define FT5336_GEST_ID_ZOOM_OUT 0x49 + +// Touch Data Status register : gives number of active touch points (0..5) +#define FT5336_TD_STAT_REG 0x02 + +// Values related to FT5336_TD_STAT_REG +#define FT5336_TD_STAT_MASK 0x0F +#define FT5336_TD_STAT_SHIFT 0x00 + +// Values Pn_XH and Pn_YH related +#define FT5336_TOUCH_EVT_FLAG_PRESS_DOWN 0x00 +#define FT5336_TOUCH_EVT_FLAG_LIFT_UP 0x01 +#define FT5336_TOUCH_EVT_FLAG_CONTACT 0x02 +#define FT5336_TOUCH_EVT_FLAG_NO_EVENT 0x03 + +#define FT5336_TOUCH_EVT_FLAG_SHIFT 0x06 +#define FT5336_TOUCH_EVT_FLAG_MASK (3 << FT5336_TOUCH_EVT_FLAG_SHIFT) + +#define FT5336_TOUCH_POS_MSB_MASK 0x0F +#define FT5336_TOUCH_POS_MSB_SHIFT 0x00 + +// Values Pn_XL and Pn_YL related +#define FT5336_TOUCH_POS_LSB_MASK 0xFF +#define FT5336_TOUCH_POS_LSB_SHIFT 0x00 + +// Values Pn_WEIGHT related +#define FT5336_TOUCH_WEIGHT_MASK 0xFF +#define FT5336_TOUCH_WEIGHT_SHIFT 0x00 + +// Values related to FT5336_Pn_MISC_REG +#define FT5336_TOUCH_AREA_MASK (0x04 << 4) +#define FT5336_TOUCH_AREA_SHIFT 0x04 + +#define FT5336_P1_XH_REG 0x03 +#define FT5336_P1_XL_REG 0x04 +#define FT5336_P1_YH_REG 0x05 +#define FT5336_P1_YL_REG 0x06 +#define FT5336_P1_WEIGHT_REG 0x07 +#define FT5336_P1_MISC_REG 0x08 + +#define FT5336_P2_XH_REG 0x09 +#define FT5336_P2_XL_REG 0x0A +#define FT5336_P2_YH_REG 0x0B +#define FT5336_P2_YL_REG 0x0C +#define FT5336_P2_WEIGHT_REG 0x0D +#define FT5336_P2_MISC_REG 0x0E + +#define FT5336_P3_XH_REG 0x0F +#define FT5336_P3_XL_REG 0x10 +#define FT5336_P3_YH_REG 0x11 +#define FT5336_P3_YL_REG 0x12 +#define FT5336_P3_WEIGHT_REG 0x13 +#define FT5336_P3_MISC_REG 0x14 + +#define FT5336_P4_XH_REG 0x15 +#define FT5336_P4_XL_REG 0x16 +#define FT5336_P4_YH_REG 0x17 +#define FT5336_P4_YL_REG 0x18 +#define FT5336_P4_WEIGHT_REG 0x19 +#define FT5336_P4_MISC_REG 0x1A + +#define FT5336_P5_XH_REG 0x1B +#define FT5336_P5_XL_REG 0x1C +#define FT5336_P5_YH_REG 0x1D +#define FT5336_P5_YL_REG 0x1E +#define FT5336_P5_WEIGHT_REG 0x1F +#define FT5336_P5_MISC_REG 0x20 + +#define FT5336_P6_XH_REG 0x21 +#define FT5336_P6_XL_REG 0x22 +#define FT5336_P6_YH_REG 0x23 +#define FT5336_P6_YL_REG 0x24 +#define FT5336_P6_WEIGHT_REG 0x25 +#define FT5336_P6_MISC_REG 0x26 + +#define FT5336_P7_XH_REG 0x27 +#define FT5336_P7_XL_REG 0x28 +#define FT5336_P7_YH_REG 0x29 +#define FT5336_P7_YL_REG 0x2A +#define FT5336_P7_WEIGHT_REG 0x2B +#define FT5336_P7_MISC_REG 0x2C + +#define FT5336_P8_XH_REG 0x2D +#define FT5336_P8_XL_REG 0x2E +#define FT5336_P8_YH_REG 0x2F +#define FT5336_P8_YL_REG 0x30 +#define FT5336_P8_WEIGHT_REG 0x31 +#define FT5336_P8_MISC_REG 0x32 + +#define FT5336_P9_XH_REG 0x33 +#define FT5336_P9_XL_REG 0x34 +#define FT5336_P9_YH_REG 0x35 +#define FT5336_P9_YL_REG 0x36 +#define FT5336_P9_WEIGHT_REG 0x37 +#define FT5336_P9_MISC_REG 0x38 + +#define FT5336_P10_XH_REG 0x39 +#define FT5336_P10_XL_REG 0x3A +#define FT5336_P10_YH_REG 0x3B +#define FT5336_P10_YL_REG 0x3C +#define FT5336_P10_WEIGHT_REG 0x3D +#define FT5336_P10_MISC_REG 0x3E + +// Threshold for touch detection +#define FT5336_TH_GROUP_REG 0x80 + +// Values FT5336_TH_GROUP_REG : threshold related +#define FT5336_THRESHOLD_MASK 0xFF +#define FT5336_THRESHOLD_SHIFT 0x00 + +// Filter function coefficients +#define FT5336_TH_DIFF_REG 0x85 + +// Control register +#define FT5336_CTRL_REG 0x86 + +// Values related to FT5336_CTRL_REG + +// Will keep the Active mode when there is no touching +#define FT5336_CTRL_KEEP_ACTIVE_MODE 0x00 + +// Switching from Active mode to Monitor mode automatically when there is no touching +#define FT5336_CTRL_KEEP_AUTO_SWITCH_MONITOR_MODE 0x01 + +// The time period of switching from Active mode to Monitor mode when there is no touching +#define FT5336_TIMEENTERMONITOR_REG 0x87 + +// Report rate in Active mode +#define FT5336_PERIODACTIVE_REG 0x88 + +// Report rate in Monitor mode +#define FT5336_PERIODMONITOR_REG 0x89 + +// The value of the minimum allowed angle while Rotating gesture mode +#define FT5336_RADIAN_VALUE_REG 0x91 + +// Maximum offset while Moving Left and Moving Right gesture +#define FT5336_OFFSET_LEFT_RIGHT_REG 0x92 + +// Maximum offset while Moving Up and Moving Down gesture +#define FT5336_OFFSET_UP_DOWN_REG 0x93 + +// Minimum distance while Moving Left and Moving Right gesture +#define FT5336_DISTANCE_LEFT_RIGHT_REG 0x94 + +// Minimum distance while Moving Up and Moving Down gesture +#define FT5336_DISTANCE_UP_DOWN_REG 0x95 + +// Maximum distance while Zoom In and Zoom Out gesture +#define FT5336_DISTANCE_ZOOM_REG 0x96 + +// High 8-bit of LIB Version info +#define FT5336_LIB_VER_H_REG 0xA1 + +// Low 8-bit of LIB Version info +#define FT5336_LIB_VER_L_REG 0xA2 + +// Chip Selecting +#define FT5336_CIPHER_REG 0xA3 + +// Interrupt mode register (used when in interrupt mode +#define FT5336_GMODE_REG 0xA4 + +#define FT5336_G_MODE_INTERRUPT_MASK 0x03 +#define FT5336_G_MODE_INTERRUPT_SHIFT 0x00 + +// Possible values of FT5336_GMODE_REG +#define FT5336_G_MODE_INTERRUPT_POLLING 0x00 +#define FT5336_G_MODE_INTERRUPT_TRIGGER 0x01 + +// Current power mode the FT5336 system is in (R) +#define FT5336_PWR_MODE_REG 0xA5 + +// FT5336 firmware version +#define FT5336_FIRMID_REG 0xA6 + +// FT5336 Chip identification register +#define FT5336_CHIP_ID_REG 0xA8 + +// Possible values of FT5336_CHIP_ID_REG +#define FT5336_ID_VALUE 0x51 + +// Release code version +#define FT5336_RELEASE_CODE_ID_REG 0xAF + +// Current operating mode the FT5336 system is in (R) +#define FT5336_STATE_REG 0xBC + +#endif /* _FT5336_H */ diff --git a/drivers/ginput/touch/FT5336/gmouse_lld_FT5336.c b/drivers/ginput/touch/FT5336/gmouse_lld_FT5336.c new file mode 100644 index 00000000..70a3cdb9 --- /dev/null +++ b/drivers/ginput/touch/FT5336/gmouse_lld_FT5336.c @@ -0,0 +1,120 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#include "gfx.h" + +#if GFX_USE_GINPUT && GINPUT_NEED_MOUSE + +#define GMOUSE_DRIVER_VMT GMOUSEVMT_FT5336 +#include "src/ginput/ginput_driver_mouse.h" + +// Get the hardware interface +#include "gmouse_lld_FT5336_board.h" + +// Hardware definitions +#include "drivers/ginput/touch/FT5336/ft5336.h" + +static bool_t ft5336Init(GMouse* m, unsigned driverinstance) +{ + if (!init_board(m, driverinstance)) { + return FALSE; + } + + // Init default values. (From NHD-3.5-320240MF-ATXL-CTP-1 datasheet) + // Valid touching detect threshold + write_reg(m, FT5336_TH_GROUP_REG, 0x16); + + // Touch difference threshold + write_reg(m, FT5336_TH_DIFF_REG, 0xA0); + + // Delay to enter 'Monitor' status (s) + write_reg(m, FT5336_TIMEENTERMONITOR_REG, 0x0A); + + // Period of 'Active' status (ms) + write_reg(m, FT5336_PERIODACTIVE_REG, 0x06); + + // Timer to enter 'idle' when in 'Monitor' (ms) + write_reg(m, FT5336_PERIODMONITOR_REG, 0x28); + + return TRUE; +} + +static bool_t ft5336ReadXYZ(GMouse* m, GMouseReading* pdr) +{ + // Assume not touched. + pdr->buttons = 0; + pdr->z = 0; + + // Only take a reading if we are touched. + if ((read_byte(m, FT5336_TD_STAT_REG) & 0x07)) { + + /* Get the X, Y, Z values */ + pdr->x = (coord_t)(read_word(m, FT5336_P1_XH_REG) & 0x0FFF); + pdr->y = (coord_t)(read_word(m, FT5336_P1_YH_REG) & 0xFFFF); + pdr->z = 1; + + // Rescale X,Y if we are using self-calibration + #if GMOUSE_FT5336_SELF_CALIBRATE + #if GDISP_NEED_CONTROL + switch(gdispGGetOrientation(m->display)) { + default: + case GDISP_ROTATE_0: + case GDISP_ROTATE_180: + pdr->x = gdispGGetWidth(m->display) - pdr->x / (4096/gdispGGetWidth(m->display)); + pdr->y = pdr->y / (4096/gdispGGetHeight(m->display)); + break; + case GDISP_ROTATE_90: + case GDISP_ROTATE_270: + pdr->x = gdispGGetHeight(m->display) - pdr->x / (4096/gdispGGetHeight(m->display)); + pdr->y = pdr->y / (4096/gdispGGetWidth(m->display)); + break; + } + #else + pdr->x = gdispGGetWidth(m->display) - pdr->x / (4096/gdispGGetWidth(m->display)); + pdr->y = pdr->y / (4096/gdispGGetHeight(m->display)); + #endif + #endif + } + + return TRUE; +} + +const GMouseVMT const GMOUSE_DRIVER_VMT[1] = {{ + { + GDRIVER_TYPE_TOUCH, + #if GMOUSE_FT5336_SELF_CALIBRATE + GMOUSE_VFLG_TOUCH | GMOUSE_VFLG_ONLY_DOWN | GMOUSE_VFLG_POORUPDOWN, + #else + GMOUSE_VFLG_TOUCH | GMOUSE_VFLG_ONLY_DOWN | GMOUSE_VFLG_POORUPDOWN | GMOUSE_VFLG_CALIBRATE | GMOUSE_VFLG_CAL_TEST, + #endif + sizeof(GMouse) + GMOUSE_FT5336_BOARD_DATA_SIZE, + _gmouseInitDriver, + _gmousePostInitDriver, + _gmouseDeInitDriver + }, + 1, // z_max - (currently?) not supported + 0, // z_min - (currently?) not supported + 1, // z_touchon + 0, // z_touchoff + { // pen_jitter + GMOUSE_FT5336_PEN_CALIBRATE_ERROR, // calibrate + GMOUSE_FT5336_PEN_CLICK_ERROR, // click + GMOUSE_FT5336_PEN_MOVE_ERROR // move + }, + { // finger_jitter + GMOUSE_FT5336_FINGER_CALIBRATE_ERROR, // calibrate + GMOUSE_FT5336_FINGER_CLICK_ERROR, // click + GMOUSE_FT5336_FINGER_MOVE_ERROR // move + }, + ft5336Init, // init + 0, // deinit + ft5336ReadXYZ, // get + 0, // calsave + 0 // calload +}}; + +#endif /* GFX_USE_GINPUT && GINPUT_NEED_MOUSE */ diff --git a/drivers/ginput/touch/FT5336/gmouse_lld_FT5336_board_template.h b/drivers/ginput/touch/FT5336/gmouse_lld_FT5336_board_template.h new file mode 100644 index 00000000..1daa0b1e --- /dev/null +++ b/drivers/ginput/touch/FT5336/gmouse_lld_FT5336_board_template.h @@ -0,0 +1,59 @@ +/* + * This file is subject to the terms of the GFX License. If a copy of + * the license was not distributed with this file, you can obtain one at: + * + * http://ugfx.org/license.html + */ + +#ifndef _GINPUT_LLD_MOUSE_BOARD_H +#define _GINPUT_LLD_MOUSE_BOARD_H + +// Resolution and Accuracy Settings +#define GMOUSE_FT5336_PEN_CALIBRATE_ERROR 8 +#define GMOUSE_FT5336_PEN_CLICK_ERROR 6 +#define GMOUSE_FT5336_PEN_MOVE_ERROR 4 +#define GMOUSE_FT5336_FINGER_CALIBRATE_ERROR 14 +#define GMOUSE_FT5336_FINGER_CLICK_ERROR 18 +#define GMOUSE_FT5336_FINGER_MOVE_ERROR 14 + +// How much extra data to allocate at the end of the GMouse structure for the board's use +#define GMOUSE_FT5336_BOARD_DATA_SIZE 0 + +// Set this to TRUE if you want self-calibration. +// NOTE: This is not as accurate as real calibration. +// It requires the orientation of the touch panel to match the display. +// It requires the active area of the touch panel to exactly match the display size. +#define GMOUSE_FT5336_SELF_CALIBRATE FALSE + +static bool_t init_board(GMouse* m, unsigned instance) +{ + (void)m; + (void)instance; + + return TRUE; +} + +static void write_reg(GMouse* m, uint8_t reg, uint8_t val) +{ + (void)m; + (void)reg; + (void)val; +} + +static uint8_t read_byte(GMouse* m, uint8_t reg) +{ + (void)m; + (void)reg; + + return (uint16_t)0x00; +} + +static uint16_t read_word(GMouse* m, uint8_t reg) +{ + (void)m; + (void)reg; + + return (uint16_t)0x0000; +} + +#endif /* _GINPUT_LLD_MOUSE_BOARD_H */