Touch_driver.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "Touch_driver.h"
  3. #include <FreeRTOS.h>
  4. #include <string.h>
  5. #include <task.h>
  6. #include "../PikaStdDevice/pika_hal.h"
  7. /* Private typedef -----------------------------------------------------------*/
  8. /* Private define ------------------------------------------------------------*/
  9. /* Private macro -------------------------------------------------------------*/
  10. /* Private variables ---------------------------------------------------------*/
  11. static uint8_t GT911_Config[] = {
  12. 0x81, 0x00, 0x04, 0x58, 0x02, 0x0A, 0x0C,
  13. 0x20, 0x01, 0x08, 0x28, 0x05, 0x50, // 0x8047 - 0x8053
  14. 0x3C, 0x0F, 0x05, 0x00, 0x00, 0x00, 0x00,
  15. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x8054 - 0x8060
  16. 0x00, 0x89, 0x2A, 0x0B, 0x2D, 0x2B, 0x0F,
  17. 0x0A, 0x00, 0x00, 0x01, 0xA9, 0x03, // 0x8061 - 0x806D
  18. 0x2D, 0x00, 0x01, 0x00, 0x00, 0x00, 0x03,
  19. 0x00, 0x00, 0x00, 0x00, 0x00, 0x21, // 0x806E - 0x807A
  20. 0x59, 0x94, 0xC5, 0x02, 0x07, 0x00, 0x00,
  21. 0x04, 0x93, 0x24, 0x00, 0x7D, 0x2C, // 0x807B - 0x8087
  22. 0x00, 0x6B, 0x36, 0x00, 0x5D, 0x42, 0x00,
  23. 0x53, 0x50, 0x00, 0x53, 0x00, 0x00, // 0x8088 - 0x8094
  24. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x8095 - 0x80A1
  26. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  27. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80A2 - 0x80AD
  28. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  29. 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, // 0x80AE - 0x80BA
  30. 0x0C, 0x0E, 0x10, 0x12, 0x14, 0x16, 0xFF,
  31. 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, // 0x80BB - 0x80C7
  32. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  33. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80C8 - 0x80D4
  34. 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0F, 0x10,
  35. 0x12, 0x16, 0x18, 0x1C, 0x1D, 0x1E, // 0x80D5 - 0x80E1
  36. 0x1F, 0x20, 0x21, 0x22, 0x24, 0xFF, 0xFF,
  37. 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, // 0x80E2 - 0x80EE
  38. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
  39. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 0x80EF - 0x80FB
  40. 0x00, 0x00, 0xD6, 0x01}; // 0x80FC - 0x8100
  41. static GT911_Status_t CommunicationResult;
  42. static uint8_t TxBuffer[200];
  43. static uint8_t RxBuffer[200] = {0};
  44. /* Private function prototypes -----------------------------------------------*/
  45. static void GT911_Reset(void);
  46. static void GT911_CalculateCheckSum(void);
  47. static GT911_Status_t GT911_SetCommandRegister(uint8_t command);
  48. static GT911_Status_t GT911_GetProductID(uint32_t* id);
  49. static GT911_Status_t GT911_SendConfig(void);
  50. static GT911_Status_t GT911_GetStatus(uint8_t* status);
  51. static GT911_Status_t GT911_SetStatus(uint8_t status);
  52. GT911_Status_t GT911_I2C_Read_Mem(uint8_t Addr,
  53. uint16_t mem_addr,
  54. uint8_t* read_data,
  55. uint16_t read_length);
  56. GT911_Status_t GT911_I2C_Write_Mem(uint8_t Addr,
  57. uint16_t mem_addr,
  58. uint8_t* write_data,
  59. uint16_t write_length);
  60. /* API Implementation
  61. * --------------------------------------------------------*/
  62. GT911_Status_t GT911_Init(GT911_Config_t config) {
  63. // Set X resolution
  64. GT911_Config[1] = config.X_Resolution & 0x00FF;
  65. GT911_Config[2] = (config.X_Resolution >> 8) & 0x00FF;
  66. // Set Y resolution
  67. GT911_Config[3] = config.Y_Resolution & 0x00FF;
  68. GT911_Config[4] = (config.Y_Resolution >> 8) & 0x00FF;
  69. // Set touch number
  70. GT911_Config[5] = config.Number_Of_Touch_Support;
  71. // set reverse Y
  72. GT911_Config[6] = 0;
  73. GT911_Config[6] |= config.ReverseY << 7;
  74. // set reverse X
  75. GT911_Config[6] |= config.ReverseX << 6;
  76. // set switch X2Y
  77. GT911_Config[6] |= config.SwithX2Y << 3;
  78. // set Sito
  79. GT911_Config[6] |= config.SoftwareNoiseReduction << 2;
  80. // Reset chip
  81. GT911_Reset();
  82. // Get product ID
  83. uint32_t productID = 0;
  84. CommunicationResult = GT911_GetProductID(&productID);
  85. __platform_printf("GT911 ID:%d\r\n", productID);
  86. // Reset chip
  87. GT911_Reset();
  88. CommunicationResult = GT911_SendConfig();
  89. if (CommunicationResult != GT911_OK) {
  90. return CommunicationResult;
  91. }
  92. GT911_SetCommandRegister(0x00);
  93. return GT911_OK;
  94. }
  95. GT911_Status_t GT911_ReadTouch(TouchCordinate_t* cordinate,
  96. uint8_t* number_of_cordinate) {
  97. uint8_t StatusRegister;
  98. GT911_Status_t Result = GT911_NotResponse;
  99. Result = GT911_GetStatus(&StatusRegister);
  100. if (Result != GT911_OK) {
  101. return Result;
  102. }
  103. if ((StatusRegister & 0x80) != 0) {
  104. *number_of_cordinate = StatusRegister & 0x0F;
  105. if (*number_of_cordinate != 0) {
  106. for (uint8_t i = 0; i < *number_of_cordinate; i++) {
  107. TxBuffer[0] = ((GOODIX_POINT1_X_ADDR + (i * 8)) & 0xFF00) >> 8;
  108. TxBuffer[1] = (GOODIX_POINT1_X_ADDR + (i * 8)) & 0xFF;
  109. GT911_I2C_Write(GOODIX_ADDRESS, TxBuffer, 2);
  110. GT911_I2C_Read(GOODIX_ADDRESS, RxBuffer, 6);
  111. cordinate[i].x = RxBuffer[0];
  112. cordinate[i].x = (RxBuffer[1] << 8) + cordinate[i].x;
  113. cordinate[i].y = RxBuffer[2];
  114. cordinate[i].y = (RxBuffer[3] << 8) + cordinate[i].y;
  115. }
  116. }
  117. GT911_SetStatus(0);
  118. }
  119. return GT911_OK;
  120. }
  121. // Private functions Implementation
  122. // ---------------------------------------------------------*/
  123. static void GT911_Reset(void) {
  124. GT911_INT_Output();
  125. GT911_RST_Control(0);
  126. GT911_Delay(20);
  127. GT911_INT_Control(0);
  128. GT911_Delay(50);
  129. GT911_RST_Control(1);
  130. GT911_Delay(100);
  131. GT911_INT_Input();
  132. GT911_Delay(100);
  133. }
  134. static void GT911_CalculateCheckSum(void) {
  135. GT911_Config[184] = 0;
  136. for (uint8_t i = 0; i < 184; i++) {
  137. GT911_Config[184] += GT911_Config[i];
  138. }
  139. GT911_Config[184] = (~GT911_Config[184]) + 1;
  140. }
  141. static GT911_Status_t GT911_SetCommandRegister(uint8_t command) {
  142. TxBuffer[0] = (GOODIX_REG_COMMAND & 0xFF00) >> 8;
  143. TxBuffer[1] = GOODIX_REG_COMMAND & 0xFF;
  144. TxBuffer[2] = command;
  145. return GT911_I2C_Write(GOODIX_ADDRESS, TxBuffer, 3);
  146. }
  147. static GT911_Status_t GT911_GetProductID(uint32_t* id) {
  148. TxBuffer[0] = (GOODIX_REG_ID & 0xFF00) >> 8;
  149. TxBuffer[1] = GOODIX_REG_ID & 0xFF;
  150. GT911_Status_t Result = GT911_NotResponse;
  151. Result = GT911_I2C_Write(GOODIX_ADDRESS, TxBuffer, 2);
  152. if (Result == GT911_OK) {
  153. Result = GT911_I2C_Read(GOODIX_ADDRESS, RxBuffer, 4);
  154. if (Result == GT911_OK) {
  155. memcpy(id, RxBuffer, 4);
  156. }
  157. }
  158. return Result;
  159. }
  160. static GT911_Status_t GT911_SendConfig(void) {
  161. GT911_CalculateCheckSum();
  162. TxBuffer[0] = (GOODIX_REG_CONFIG_DATA & 0xFF00) >> 8;
  163. TxBuffer[1] = GOODIX_REG_CONFIG_DATA & 0xFF;
  164. memcpy(&TxBuffer[2], GT911_Config, sizeof(GT911_Config));
  165. return GT911_I2C_Write(GOODIX_ADDRESS, TxBuffer, sizeof(GT911_Config) + 2);
  166. }
  167. static GT911_Status_t GT911_GetStatus(uint8_t* status) {
  168. TxBuffer[0] = (GOODIX_READ_COORD_ADDR & 0xFF00) >> 8;
  169. TxBuffer[1] = GOODIX_READ_COORD_ADDR & 0xFF;
  170. GT911_Status_t Result = GT911_NotResponse;
  171. Result = GT911_I2C_Write(GOODIX_ADDRESS, TxBuffer, 2);
  172. if (Result == GT911_OK) {
  173. Result = GT911_I2C_Read(GOODIX_ADDRESS, RxBuffer, 1);
  174. if (Result == GT911_OK) {
  175. *status = RxBuffer[0];
  176. }
  177. }
  178. return Result;
  179. }
  180. static GT911_Status_t GT911_SetStatus(uint8_t status) {
  181. TxBuffer[0] = (GOODIX_READ_COORD_ADDR & 0xFF00) >> 8;
  182. TxBuffer[1] = GOODIX_READ_COORD_ADDR & 0xFF;
  183. TxBuffer[2] = status;
  184. return GT911_I2C_Write(GOODIX_ADDRESS, TxBuffer, 3);
  185. }
  186. static pika_dev* TOUCH_INT = NULL;
  187. static pika_dev* TOUCH_RST = NULL;
  188. static pika_dev* TOUCH_IIC = NULL;
  189. void GT911_INT_Input(void) {
  190. if (NULL == TOUCH_INT) {
  191. TOUCH_INT = pika_hal_open(PIKA_HAL_GPIO, "P17");
  192. }
  193. pika_hal_GPIO_config cfg = {0};
  194. cfg.dir = PIKA_HAL_GPIO_DIR_IN;
  195. cfg.pull = PIKA_HAL_GPIO_PULL_NONE;
  196. pika_hal_ioctl(TOUCH_INT, PIKA_HAL_IOCTL_CONFIG, &cfg);
  197. pika_hal_ioctl(TOUCH_INT, PIKA_HAL_IOCTL_ENABLE);
  198. }
  199. void GT911_INT_Output(void) {
  200. if (NULL == TOUCH_INT) {
  201. TOUCH_INT = pika_hal_open(PIKA_HAL_GPIO, "P17");
  202. }
  203. pika_hal_GPIO_config cfg = {0};
  204. cfg.dir = PIKA_HAL_GPIO_DIR_OUT;
  205. cfg.pull = PIKA_HAL_GPIO_PULL_NONE;
  206. pika_hal_ioctl(TOUCH_INT, PIKA_HAL_IOCTL_CONFIG, &cfg);
  207. pika_hal_ioctl(TOUCH_INT, PIKA_HAL_IOCTL_ENABLE);
  208. }
  209. void GT911_RST_Control(bool high_or_low) {
  210. if (NULL == TOUCH_RST) {
  211. TOUCH_RST = pika_hal_open(PIKA_HAL_GPIO, "P5");
  212. pika_hal_GPIO_config cfg = {0};
  213. cfg.dir = PIKA_HAL_GPIO_DIR_OUT;
  214. pika_hal_ioctl(TOUCH_RST, PIKA_HAL_IOCTL_CONFIG, &cfg);
  215. pika_hal_ioctl(TOUCH_RST, PIKA_HAL_IOCTL_ENABLE);
  216. }
  217. uint32_t val = 0;
  218. switch (high_or_low) {
  219. case true:
  220. val = 1;
  221. pika_hal_write(TOUCH_RST, &val, sizeof(uint32_t));
  222. break;
  223. case false:
  224. val = 0;
  225. pika_hal_write(TOUCH_RST, &val, sizeof(uint32_t));
  226. break;
  227. }
  228. }
  229. void GT911_INT_Control(bool high_or_low) {
  230. if (NULL == TOUCH_INT) {
  231. GT911_INT_Output();
  232. }
  233. uint32_t val = 0;
  234. switch (high_or_low) {
  235. case true:
  236. val = 1;
  237. pika_hal_write(TOUCH_INT, &val, sizeof(uint32_t));
  238. break;
  239. case false:
  240. val = 0;
  241. pika_hal_write(TOUCH_INT, &val, sizeof(uint32_t));
  242. break;
  243. }
  244. }
  245. void GT911_Delay(uint16_t ms) {
  246. // HAL_Delay(ms);
  247. vTaskDelay(ms / portTICK_PERIOD_MS);
  248. }
  249. GT911_Status_t GT911_I2C_Init(void) {
  250. if (NULL == TOUCH_IIC) {
  251. TOUCH_IIC = pika_hal_open(PIKA_HAL_IIC, "IIC3");
  252. pika_hal_IIC_config cfg = {0};
  253. cfg.slave_addr = GOODIX_ADDRESS;
  254. cfg.speed = 400000;
  255. cfg.timeout = 10000;
  256. pika_hal_ioctl(TOUCH_IIC, PIKA_HAL_IOCTL_CONFIG, &cfg);
  257. pika_hal_ioctl(TOUCH_IIC, PIKA_HAL_IOCTL_ENABLE);
  258. }
  259. return GT911_OK;
  260. }
  261. GT911_Status_t GT911_I2C_Write(uint8_t Addr,
  262. uint8_t* write_data,
  263. uint16_t write_length) {
  264. if (NULL == TOUCH_IIC) {
  265. GT911_I2C_Init();
  266. }
  267. pika_hal_IIC_config cfg = {0};
  268. cfg.slave_addr = Addr;
  269. cfg.mem_addr_ena = PIKA_HAL_IIC_MEM_ADDR_ENA_DISABLE;
  270. pika_hal_ioctl(TOUCH_IIC, PIKA_HAL_IOCTL_CONFIG, &cfg);
  271. pika_hal_write(TOUCH_IIC, write_data, write_length);
  272. return GT911_OK;
  273. }
  274. GT911_Status_t GT911_I2C_Write_Mem(uint8_t Addr,
  275. uint16_t mem_addr,
  276. uint8_t* write_data,
  277. uint16_t write_length) {
  278. if (NULL == TOUCH_IIC) {
  279. GT911_I2C_Init();
  280. }
  281. pika_hal_IIC_config cfg = {0};
  282. cfg.slave_addr = Addr;
  283. cfg.mem_addr = mem_addr;
  284. cfg.mem_addr_ena = PIKA_HAL_IIC_MEM_ADDR_ENA_ENABLE;
  285. cfg.mem_addr_size = sizeof(uint16_t);
  286. pika_hal_ioctl(TOUCH_IIC, PIKA_HAL_IOCTL_CONFIG, &cfg);
  287. pika_hal_write(TOUCH_IIC, write_data, write_length);
  288. return GT911_OK;
  289. }
  290. GT911_Status_t GT911_I2C_Read(uint8_t Addr,
  291. uint8_t* read_data,
  292. uint16_t read_length) {
  293. if (NULL == TOUCH_IIC) {
  294. GT911_I2C_Init();
  295. }
  296. pika_hal_IIC_config cfg = {0};
  297. cfg.slave_addr = Addr;
  298. cfg.mem_addr_ena = PIKA_HAL_IIC_MEM_ADDR_ENA_DISABLE;
  299. pika_hal_ioctl(TOUCH_IIC, PIKA_HAL_IOCTL_CONFIG, &cfg);
  300. pika_hal_read(TOUCH_IIC, read_data, read_length);
  301. return GT911_OK;
  302. }
  303. GT911_Status_t GT911_I2C_Read_Mem(uint8_t Addr,
  304. uint16_t mem_addr,
  305. uint8_t* read_data,
  306. uint16_t read_length) {
  307. if (NULL == TOUCH_IIC) {
  308. GT911_I2C_Init();
  309. }
  310. pika_hal_IIC_config cfg = {0};
  311. cfg.slave_addr = Addr;
  312. cfg.mem_addr = mem_addr;
  313. cfg.mem_addr_ena = PIKA_HAL_IIC_MEM_ADDR_ENA_ENABLE;
  314. cfg.mem_addr_size = sizeof(uint16_t);
  315. pika_hal_ioctl(TOUCH_IIC, PIKA_HAL_IOCTL_CONFIG, &cfg);
  316. pika_hal_read(TOUCH_IIC, read_data, read_length);
  317. return GT911_OK;
  318. }
  319. /************************ (C) COPYRIGHT STMicroelectronics *****END OF
  320. * FILE****/