wm_touchsensor.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. /**
  2. * @file wm_touchsensor.c
  3. *
  4. * @brief touchsensor Driver Module
  5. *
  6. * @author
  7. *
  8. * Copyright (c) 2021 Winner Microelectronics Co., Ltd.
  9. */
  10. #include "wm_debug.h"
  11. #include "wm_regs.h"
  12. #include "wm_irq.h"
  13. #include "wm_cpu.h"
  14. #include "wm_gpio.h"
  15. #define ATTRIBUTE_ISR __attribute__((isr))
  16. typedef void (*touchsensor_cb)(u32 status);
  17. touchsensor_cb tc_callback = NULL;
  18. /**
  19. * @brief This function is used to initialize touch sensor.
  20. *
  21. * @param[in] sensorno is the touch sensor number from 1-15
  22. * @param[in] scan_period is scan period for per touch sensor ,unit:16ms, >0
  23. * @param[in] window is count window, window must be greater than 2.Real count window is window - 2.
  24. * @param[in] enable is touch sensor enable bit.
  25. *
  26. * @retval 0:success
  27. *
  28. * @note if use touch sensor, user must configure the IO multiplex by API wm_touch_sensor_config.
  29. */
  30. int tls_touchsensor_init_config(u32 sensorno, u8 scan_period, u8 window, u32 enable)
  31. {
  32. u32 regval = 0;
  33. #if 0
  34. /*cfg touch bias*/
  35. regval = tls_reg_read32(HR_PMU_WLAN_STTS);
  36. regval &=~(0x70);
  37. regval |=(0x20);
  38. tls_reg_write32(HR_PMU_WLAN_STTS, regval);
  39. #endif
  40. regval = tls_reg_read32(HR_TC_CONFIG);
  41. /*firstly, disable scan function */
  42. tls_reg_write32(HR_TC_CONFIG,regval&(~(1<<TOUCH_SENSOR_EN_BIT)));
  43. if (scan_period <= 0x3F)
  44. {
  45. regval &= ~(0x3F<<SCAN_PERID_SHIFT_BIT);
  46. regval |= (scan_period<<SCAN_PERID_SHIFT_BIT);
  47. }
  48. if (window)
  49. {
  50. regval &= ~(0x3F<<CAPDET_CNT_SHIFT_BIT);
  51. regval |= (window<<CAPDET_CNT_SHIFT_BIT);
  52. }
  53. if (sensorno && (sensorno <= 15))
  54. {
  55. regval |= (1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
  56. }
  57. if (enable)
  58. {
  59. regval |= (1<<TOUCH_SENSOR_EN_BIT);
  60. }
  61. tls_reg_write32(HR_TC_CONFIG,regval);
  62. return 0;
  63. }
  64. /**
  65. * @brief This function is used to initialize touch scan channel.
  66. *
  67. * @param[in] sensorno is the touch sensor number from 1-15
  68. *
  69. * @retval 0:success
  70. *
  71. * @note if use touch sensor, user must configure the IO multiplex by API wm_touch_sensor_config.
  72. */
  73. int tls_touchsensor_chan_config(u32 sensorno)
  74. {
  75. u32 regval = 0;
  76. regval = tls_reg_read32(HR_TC_CONFIG);
  77. if (sensorno && (sensorno <= 15))
  78. {
  79. regval |= (1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
  80. }
  81. tls_reg_write32(HR_TC_CONFIG,regval);
  82. return 0;
  83. }
  84. /**
  85. * @brief This function is used to initialize touch general configuration.
  86. *
  87. * @param[in] scanperiod is scan period for per touch sensor ,unit:16ms, >0
  88. * @param[in] window is count window, window must be greater than 2.Real count window is window - 2.
  89. * @param[in] bias is touch sensor bias current
  90. *
  91. * @retval 0:success
  92. *
  93. * @note if use touch sensor, user must configure the IO multiplex by API wm_touch_sensor_config.
  94. */
  95. int tls_touchsensor_scan_config(u8 scanperiod, u8 window, u8 bias)
  96. {
  97. u32 regval = 0;
  98. regval = tls_reg_read32(HR_PMU_WLAN_STTS);
  99. if (bias <= 7)
  100. {
  101. regval &=~(0x70);
  102. regval |=(bias<<4);
  103. }
  104. else
  105. {
  106. regval &=~(0x70);
  107. regval |=(0x4<<4);
  108. }
  109. tls_reg_write32(HR_PMU_WLAN_STTS, regval);
  110. regval = tls_reg_read32(HR_TC_CONFIG);
  111. if (scanperiod <= 0x3F)
  112. {
  113. regval &= ~(0x3F<<SCAN_PERID_SHIFT_BIT);
  114. regval |= (scanperiod<<SCAN_PERID_SHIFT_BIT);
  115. }
  116. if (window)
  117. {
  118. regval &= ~(0x3F<<CAPDET_CNT_SHIFT_BIT);
  119. regval |= (window<<CAPDET_CNT_SHIFT_BIT);
  120. }
  121. tls_reg_write32(HR_TC_CONFIG,regval);
  122. return 0;
  123. }
  124. /**
  125. * @brief This function is used to start touch scan
  126. *
  127. * @retval 0:success
  128. *
  129. * @note if use touch sensor, user must configure the IO multiplex by API wm_touch_sensor_config.
  130. */
  131. int tls_touchsensor_scan_start(void)
  132. {
  133. u32 regval = 0;
  134. regval = tls_reg_read32(HR_TC_CONFIG);
  135. regval |= (1<<TOUCH_SENSOR_EN_BIT);
  136. tls_reg_write32(HR_TC_CONFIG,regval);
  137. return 0;
  138. }
  139. /**
  140. * @brief This function is used to stop touch scan
  141. *
  142. * @retval 0:success
  143. *
  144. * @note if use touch sensor, user must configure the IO multiplex by API wm_touch_sensor_config.
  145. */
  146. int tls_touchsensor_scan_stop(void)
  147. {
  148. u32 regval = 0;
  149. regval = tls_reg_read32(HR_TC_CONFIG);
  150. regval &= ~(1<<TOUCH_SENSOR_EN_BIT);
  151. tls_reg_write32(HR_TC_CONFIG,regval);
  152. return 0;
  153. }
  154. /**
  155. * @brief This function is used to deinit touch sensor's selection and disable touch.
  156. *
  157. * @param[in] sensorno is the touch sensor number from 1-15
  158. *
  159. * @retval 0:success
  160. *
  161. * @note if do not use touch sensor, user can deinit by this interface and configure this touch sensor as GPIO.
  162. */
  163. int tls_touchsensor_deinit(u32 sensorno)
  164. {
  165. u32 regval = 0;
  166. regval = tls_reg_read32(HR_TC_CONFIG);
  167. if (sensorno && (sensorno <= 15))
  168. {
  169. regval &= ~(1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
  170. }
  171. regval &= ~(1<<TOUCH_SENSOR_EN_BIT);
  172. tls_reg_write32(HR_TC_CONFIG,regval);
  173. return 0;
  174. }
  175. /**
  176. * @brief This function is used to set threshold per touch sensor.
  177. *
  178. * @param[in] sensorno is the touch sensor number from 1-15
  179. * @param[in] threshold is the sensorno's touch sensor threshold,max value is 127.
  180. *
  181. * @retval 0:success. minus value: parameter wrong.
  182. *
  183. * @note None
  184. */
  185. int tls_touchsensor_threshold_config(u32 sensorno, u8 threshold)
  186. {
  187. u32 regvalue = 0;
  188. if((sensorno == 0) || (sensorno > 15))
  189. {
  190. return -1;
  191. }
  192. if (threshold > 0x7F)
  193. {
  194. return -2;
  195. }
  196. regvalue = tls_reg_read32(HR_TC_CONFIG+sensorno*4);
  197. regvalue &= ~(0x7F);
  198. regvalue |= threshold;
  199. tls_reg_write32(HR_TC_CONFIG + sensorno*4, regvalue);
  200. return 0;
  201. }
  202. /**
  203. * @brief This function is used to get touch sensor's count number.
  204. *
  205. * @param[in] sensorno is the touch sensor number from 1 to 15.
  206. *
  207. * @retval sensorno's count number .
  208. *
  209. * @note None
  210. */
  211. int tls_touchsensor_countnum_get(u32 sensorno)
  212. {
  213. if((sensorno == 0) || (sensorno > 15))
  214. {
  215. return -1;
  216. }
  217. return ((tls_reg_read32(HR_TC_CONFIG+sensorno*4)>>8)&0x3FFF);
  218. }
  219. /**
  220. * @brief This function is used to enable touch sensor's irq.
  221. *
  222. * @param[in] sensorno is the touch sensor number from 1 to 15.
  223. *
  224. * @retval 0:successfully enable irq, -1:parameter wrong.
  225. *
  226. * @note None
  227. */
  228. int tls_touchsensor_irq_enable(u32 sensorno)
  229. {
  230. u32 value = 0;
  231. if (sensorno && (sensorno <= 15))
  232. {
  233. value = tls_reg_read32(HR_TC_INT_EN);
  234. value |= (1<<(sensorno+15));
  235. tls_reg_write32(HR_TC_INT_EN, value);
  236. tls_irq_enable(TOUCH_IRQn);
  237. return 0;
  238. }
  239. return -1;
  240. }
  241. /**
  242. * @brief This function is used to disable touch sensor's irq.
  243. *
  244. * @param[in] sensorno is the touch sensor number from 1 to 15.
  245. *
  246. * @retval 0:successfully disable irq, -1:parameter wrong.
  247. *
  248. * @note None
  249. */
  250. int tls_touchsensor_irq_disable(u32 sensorno)
  251. {
  252. u32 value = 0;
  253. if (sensorno && (sensorno <= 15))
  254. {
  255. value = tls_reg_read32(HR_TC_INT_EN);
  256. value &= ~(1<<(sensorno+15));
  257. tls_reg_write32(HR_TC_INT_EN, value);
  258. if ((value & 0xFFFF0000) == 0)
  259. {
  260. tls_irq_disable(TOUCH_IRQn);
  261. }
  262. return 0;
  263. }
  264. return -1;
  265. }
  266. /**
  267. * @brief This function is used to register touch sensor's irq callback.
  268. *
  269. * @param[in] callback is call back for user's application.
  270. *
  271. * @retval None.
  272. *
  273. * @note None
  274. */
  275. void tls_touchsensor_irq_register(void (*callback)(u32 status))
  276. {
  277. tc_callback = callback;
  278. }
  279. /**
  280. * @brief This function is touch sensor's irq handler.
  281. *
  282. * @param[in] None
  283. *
  284. * @retval None
  285. *
  286. * @note None
  287. */
  288. //static u32 tc1cnt[16] = {0};
  289. ATTRIBUTE_ISR void tls_touchsensor_irq_handler(void)
  290. {
  291. u32 value = 0;
  292. // int i = 0;
  293. value = tls_reg_read32(HR_TC_INT_EN);
  294. #if 0
  295. for (i = 0; i < 15; i++)
  296. {
  297. if (value&BIT(i))
  298. {
  299. tc1cnt[i]++;
  300. printf("tcnum[%02d]:%04d,%04d\r\n", i+1, tc1cnt[i], tls_touchsensor_countnum_get(i+1));
  301. }
  302. }
  303. totalvalue |= (value&0xFFFF);
  304. printf("val:%04x,%04x\r\n", value&0xFFFF, totalvalue);
  305. #endif
  306. if (tc_callback)
  307. {
  308. tc_callback(value&0xFFFF);
  309. }
  310. tls_reg_write32(HR_TC_INT_EN, value);
  311. }
  312. /**
  313. * @brief This function is used to get touch sensor's irq status.
  314. *
  315. * @param[in] sensorno is the touch sensor number from 1 to 15.
  316. *
  317. * @retval >=0:irq status, -1:parameter wrong.
  318. *
  319. * @note None
  320. */
  321. int tls_touchsensor_irq_status_get(u32 sensorno)
  322. {
  323. u32 value = 0;
  324. if (sensorno && (sensorno <= 15))
  325. {
  326. value = tls_reg_read32(HR_TC_INT_EN);
  327. return (value&(1<<(sensorno-1)))?1:0;
  328. }
  329. return -1;
  330. }