lv_port_indev.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. * Copyright 2022 NXP
  4. *
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Change Logs:
  8. * Date Author Notes
  9. * 2021-10-18 Meco Man The first version
  10. * 2022-05-17 Ting Liu Support touchpad
  11. */
  12. #define LOG_TAG "LVGL.port.indev"
  13. #include <drv_log.h>
  14. #include "lvgl.h"
  15. #include "board.h"
  16. #include "fsl_video_common.h"
  17. #include "fsl_lpi2c.h"
  18. #include "fsl_gpio.h"
  19. #ifdef DEMO_PANEL_RK043FN66HS
  20. #include "fsl_gt911.h"
  21. #else
  22. #include "fsl_ft5406_rt.h"
  23. #endif
  24. #if LV_USE_GPU_NXP_PXP
  25. #include "src/gpu/lv_gpu_nxp_pxp.h"
  26. #include "src/gpu/lv_gpu_nxp_pxp_osa.h"
  27. #endif
  28. /*******************************************************************************
  29. * Definitions
  30. ******************************************************************************/
  31. /* @Brief Board touch panel configuration */
  32. #define BOARD_TOUCH_I2C_BASEADDR LPI2C1
  33. #define BOARD_TOUCH_RST_GPIO GPIO1
  34. #define BOARD_TOUCH_RST_PIN 2
  35. #define BOARD_TOUCH_INT_GPIO GPIO1
  36. #define BOARD_TOUCH_INT_PIN 11
  37. /* Macros for the touch touch controller. */
  38. #define TOUCH_I2C LPI2C1
  39. /* Select USB1 PLL (480 MHz) as master lpi2c clock source */
  40. #define TOUCH_LPI2C_CLOCK_SOURCE_SELECT (0U)
  41. /* Clock divider for master lpi2c clock source */
  42. #define TOUCH_LPI2C_CLOCK_SOURCE_DIVIDER (5U)
  43. #define TOUCH_I2C_CLOCK_FREQ ((CLOCK_GetFreq(kCLOCK_Usb1PllClk) / 8) / (TOUCH_LPI2C_CLOCK_SOURCE_DIVIDER + 1U))
  44. #define TOUCH_I2C_BAUDRATE 100000U
  45. /*******************************************************************************
  46. * Prototypes
  47. ******************************************************************************/
  48. static void DEMO_InitTouch(void);
  49. static void DEMO_ReadTouch(lv_indev_drv_t *drv, lv_indev_data_t *data);
  50. #ifdef DEMO_PANEL_RK043FN66HS
  51. static void BOARD_PullTouchResetPin(bool pullUp);
  52. static void BOARD_ConfigTouchIntPin(gt911_int_pin_mode_t mode);
  53. #endif
  54. /*******************************************************************************
  55. * Variables
  56. ******************************************************************************/
  57. #ifdef DEMO_PANEL_RK043FN66HS
  58. static gt911_handle_t s_touchHandle;
  59. static const gt911_config_t s_touchConfig = {
  60. .I2C_SendFunc = BOARD_Touch_I2C_Send,
  61. .I2C_ReceiveFunc = BOARD_Touch_I2C_Receive,
  62. .pullResetPinFunc = BOARD_PullTouchResetPin,
  63. .intPinFunc = BOARD_ConfigTouchIntPin,
  64. .timeDelayMsFunc = VIDEO_DelayMs,
  65. .touchPointNum = 1,
  66. .i2cAddrMode = kGT911_I2cAddrMode0,
  67. .intTrigMode = kGT911_IntRisingEdge,
  68. };
  69. static int s_touchResolutionX;
  70. static int s_touchResolutionY;
  71. #else
  72. static ft5406_rt_handle_t touchHandle;
  73. #endif
  74. void lv_port_indev_init(void)
  75. {
  76. static lv_indev_drv_t indev_drv;
  77. /*------------------
  78. * Touchpad
  79. * -----------------*/
  80. /*Initialize your touchpad */
  81. DEMO_InitTouch();
  82. /*Register a touchpad input device*/
  83. lv_indev_drv_init(&indev_drv);
  84. indev_drv.type = LV_INDEV_TYPE_POINTER;
  85. indev_drv.read_cb = DEMO_ReadTouch;
  86. lv_indev_drv_register(&indev_drv);
  87. }
  88. #ifdef DEMO_PANEL_RK043FN66HS
  89. static void BOARD_PullTouchResetPin(bool pullUp)
  90. {
  91. if (pullUp)
  92. {
  93. GPIO_PinWrite(BOARD_TOUCH_RST_GPIO, BOARD_TOUCH_RST_PIN, 1);
  94. }
  95. else
  96. {
  97. GPIO_PinWrite(BOARD_TOUCH_RST_GPIO, BOARD_TOUCH_RST_PIN, 0);
  98. }
  99. }
  100. static void BOARD_ConfigTouchIntPin(gt911_int_pin_mode_t mode)
  101. {
  102. if (mode == kGT911_IntPinInput)
  103. {
  104. BOARD_TOUCH_INT_GPIO->GDIR &= ~(1UL << BOARD_TOUCH_INT_PIN);
  105. }
  106. else
  107. {
  108. if (mode == kGT911_IntPinPullDown)
  109. {
  110. GPIO_PinWrite(BOARD_TOUCH_INT_GPIO, BOARD_TOUCH_INT_PIN, 0);
  111. }
  112. else
  113. {
  114. GPIO_PinWrite(BOARD_TOUCH_INT_GPIO, BOARD_TOUCH_INT_PIN, 1);
  115. }
  116. BOARD_TOUCH_INT_GPIO->GDIR |= (1UL << BOARD_TOUCH_INT_PIN);
  117. }
  118. }
  119. /*Initialize your touchpad*/
  120. static void DEMO_InitTouch(void)
  121. {
  122. status_t status;
  123. const gpio_pin_config_t resetPinConfig = {
  124. .direction = kGPIO_DigitalOutput, .outputLogic = 0, .interruptMode = kGPIO_NoIntmode};
  125. GPIO_PinInit(BOARD_TOUCH_INT_GPIO, BOARD_TOUCH_INT_PIN, &resetPinConfig);
  126. GPIO_PinInit(BOARD_TOUCH_RST_GPIO, BOARD_TOUCH_RST_PIN, &resetPinConfig);
  127. /*Clock setting for LPI2C*/
  128. CLOCK_SetMux(kCLOCK_Lpi2cMux, TOUCH_LPI2C_CLOCK_SOURCE_SELECT);
  129. CLOCK_SetDiv(kCLOCK_Lpi2cDiv, TOUCH_LPI2C_CLOCK_SOURCE_DIVIDER);
  130. BOARD_LPI2C_Init(TOUCH_I2C, TOUCH_I2C_CLOCK_FREQ);
  131. status = GT911_Init(&s_touchHandle, &s_touchConfig);
  132. if (kStatus_Success != status)
  133. {
  134. //PRINTF("Touch IC initialization failed\r\n");
  135. assert(false);
  136. }
  137. GT911_GetResolution(&s_touchHandle, &s_touchResolutionX, &s_touchResolutionY);
  138. }
  139. /* Will be called by the library to read the touchpad */
  140. static void DEMO_ReadTouch(lv_indev_drv_t *drv, lv_indev_data_t *data)
  141. {
  142. static int touch_x = 0;
  143. static int touch_y = 0;
  144. if (kStatus_Success == GT911_GetSingleTouch(&s_touchHandle, &touch_x, &touch_y))
  145. {
  146. data->state = LV_INDEV_STATE_PR;
  147. }
  148. else
  149. {
  150. data->state = LV_INDEV_STATE_REL;
  151. }
  152. /*Set the last pressed coordinates*/
  153. data->point.x = touch_x * LCD_WIDTH / s_touchResolutionX;
  154. data->point.y = touch_y * LCD_HEIGHT / s_touchResolutionY;
  155. }
  156. #else
  157. /*Initialize your touchpad*/
  158. static void DEMO_InitTouch(void)
  159. {
  160. status_t status;
  161. lpi2c_master_config_t masterConfig = {0};
  162. /*Clock setting for LPI2C*/
  163. CLOCK_SetMux(kCLOCK_Lpi2cMux, TOUCH_LPI2C_CLOCK_SOURCE_SELECT);
  164. CLOCK_SetDiv(kCLOCK_Lpi2cDiv, TOUCH_LPI2C_CLOCK_SOURCE_DIVIDER);
  165. /*
  166. * masterConfig.debugEnable = false;
  167. * masterConfig.ignoreAck = false;
  168. * masterConfig.pinConfig = kLPI2C_2PinOpenDrain;
  169. * masterConfig.baudRate_Hz = 100000U;
  170. * masterConfig.busIdleTimeout_ns = 0;
  171. * masterConfig.pinLowTimeout_ns = 0;
  172. * masterConfig.sdaGlitchFilterWidth_ns = 0;
  173. * masterConfig.sclGlitchFilterWidth_ns = 0;
  174. */
  175. LPI2C_MasterGetDefaultConfig(&masterConfig);
  176. /* Change the default baudrate configuration */
  177. masterConfig.baudRate_Hz = TOUCH_I2C_BAUDRATE;
  178. /* Initialize the LPI2C master peripheral */
  179. LPI2C_MasterInit(TOUCH_I2C, &masterConfig, TOUCH_I2C_CLOCK_FREQ);
  180. /* Initialize touch panel controller */
  181. status = FT5406_RT_Init(&touchHandle, TOUCH_I2C);
  182. if (status != kStatus_Success)
  183. {
  184. //PRINTF("Touch panel init failed\n");
  185. assert(0);
  186. }
  187. }
  188. /* Will be called by the library to read the touchpad */
  189. static void DEMO_ReadTouch(lv_indev_drv_t *drv, lv_indev_data_t *data)
  190. {
  191. touch_event_t touch_event;
  192. static int touch_x = 0;
  193. static int touch_y = 0;
  194. data->state = LV_INDEV_STATE_REL;
  195. if (kStatus_Success == FT5406_RT_GetSingleTouch(&touchHandle, &touch_event, &touch_x, &touch_y))
  196. {
  197. if ((touch_event == kTouch_Down) || (touch_event == kTouch_Contact))
  198. {
  199. data->state = LV_INDEV_STATE_PR;
  200. }
  201. }
  202. /*Set the last pressed coordinates*/
  203. data->point.x = touch_y;
  204. data->point.y = touch_x;
  205. }
  206. #endif