wm_touchsensor.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. regval = tls_reg_read32(HR_TC_CONFIG);
  34. if (scan_period >=0x3F)
  35. {
  36. regval &= ~(0x3F<<SCAN_PERID_SHIFT_BIT);
  37. regval |= (scan_period<<SCAN_PERID_SHIFT_BIT);
  38. }
  39. if (window)
  40. {
  41. regval &= ~(0x3F<<CAPDET_CNT_SHIFT_BIT);
  42. regval |= (window<<CAPDET_CNT_SHIFT_BIT);
  43. }
  44. if (sensorno && (sensorno <= 15))
  45. {
  46. regval |= (1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
  47. }
  48. if (enable)
  49. {
  50. regval |= (1<<TOUCH_SENSOR_EN_BIT);
  51. }
  52. tls_reg_write32(HR_TC_CONFIG,regval);
  53. return 0;
  54. }
  55. /**
  56. * @brief This function is used to deinit touch sensor's selection and disable touch.
  57. *
  58. * @param[in] sensorno is the touch sensor number from 1-15
  59. *
  60. * @retval 0:success
  61. *
  62. * @note if do not use touch sensor, user can deinit by this interface and configure this touch sensor as GPIO.
  63. */
  64. int tls_touchsensor_deinit(u32 sensorno)
  65. {
  66. u32 regval = 0;
  67. regval = tls_reg_read32(HR_TC_CONFIG);
  68. if (sensorno && (sensorno <= 15))
  69. {
  70. regval &= ~(1<<(sensorno-1+TOUCH_SENSOR_SEL_SHIFT_BIT));
  71. }
  72. regval &= ~(1<<TOUCH_SENSOR_EN_BIT);
  73. tls_reg_write32(HR_TC_CONFIG,regval);
  74. return 0;
  75. }
  76. /**
  77. * @brief This function is used to set threshold per touch sensor.
  78. *
  79. * @param[in] sensorno is the touch sensor number from 1-15
  80. * @param[in] threshold is the sensorno's touch sensor threshold,max value is 127.
  81. *
  82. * @retval 0:success. minus value: parameter wrong.
  83. *
  84. * @note None
  85. */
  86. int tls_touchsensor_threshold_config(u32 sensorno, u8 threshold)
  87. {
  88. u32 regvalue = 0;
  89. if((sensorno == 0) || (sensorno > 15))
  90. {
  91. return -1;
  92. }
  93. if (threshold > 0x7F)
  94. {
  95. return -2;
  96. }
  97. regvalue = tls_reg_read32(HR_TC_CONFIG+sensorno*4);
  98. regvalue &= ~(0x7F);
  99. regvalue |= threshold;
  100. tls_reg_write32(HR_TC_CONFIG + sensorno*4, regvalue);
  101. return 0;
  102. }
  103. /**
  104. * @brief This function is used to get touch sensor's count number.
  105. *
  106. * @param[in] sensorno is the touch sensor number from 1 to 15.
  107. *
  108. * @retval sensorno's count number .
  109. *
  110. * @note None
  111. */
  112. int tls_touchsensor_countnum_get(u32 sensorno)
  113. {
  114. if((sensorno == 0) || (sensorno > 15))
  115. {
  116. return -1;
  117. }
  118. return ((tls_reg_read32(HR_TC_CONFIG+sensorno*4)>>8)&0x3FFF);
  119. }
  120. /**
  121. * @brief This function is used to enable touch sensor's irq.
  122. *
  123. * @param[in] sensorno is the touch sensor number from 1 to 15.
  124. *
  125. * @retval 0:successfully enable irq, -1:parameter wrong.
  126. *
  127. * @note None
  128. */
  129. int tls_touchsensor_irq_enable(u32 sensorno)
  130. {
  131. u32 value = 0;
  132. if (sensorno && (sensorno <= 15))
  133. {
  134. value = tls_reg_read32(HR_TC_INT_EN);
  135. value |= (1<<(sensorno+15));
  136. tls_reg_write32(HR_TC_INT_EN, value);
  137. tls_irq_enable(TOUCH_IRQn);
  138. return 0;
  139. }
  140. return -1;
  141. }
  142. /**
  143. * @brief This function is used to disable touch sensor's irq.
  144. *
  145. * @param[in] sensorno is the touch sensor number from 1 to 15.
  146. *
  147. * @retval 0:successfully disable irq, -1:parameter wrong.
  148. *
  149. * @note None
  150. */
  151. int tls_touchsensor_irq_disable(u32 sensorno)
  152. {
  153. u32 value = 0;
  154. if (sensorno && (sensorno <= 15))
  155. {
  156. value = tls_reg_read32(HR_TC_INT_EN);
  157. value &= ~(1<<(sensorno+15));
  158. tls_reg_write32(HR_TC_INT_EN, value);
  159. if ((value & 0xFFFF0000) == 0)
  160. {
  161. tls_irq_disable(TOUCH_IRQn);
  162. }
  163. return 0;
  164. }
  165. return -1;
  166. }
  167. /**
  168. * @brief This function is used to register touch sensor's irq callback.
  169. *
  170. * @param[in] callback is call back for user's application.
  171. *
  172. * @retval None.
  173. *
  174. * @note None
  175. */
  176. void tls_touchsensor_irq_register(void (*callback)(u32 status))
  177. {
  178. tc_callback = callback;
  179. }
  180. /**
  181. * @brief This function is touch sensor's irq handler.
  182. *
  183. * @param[in] None
  184. *
  185. * @retval None
  186. *
  187. * @note None
  188. */
  189. //static u32 tc1cnt[16] = {0};
  190. ATTRIBUTE_ISR void tls_touchsensor_irq_handler(void)
  191. {
  192. u32 value = 0;
  193. // int i = 0;
  194. value = tls_reg_read32(HR_TC_INT_EN);
  195. #if 0
  196. for (i = 0; i < 15; i++)
  197. {
  198. if (value&BIT(i))
  199. {
  200. tc1cnt[i]++;
  201. printf("tcnum[%02d]:%04d,%04d\r\n", i+1, tc1cnt[i], tls_touchsensor_countnum_get(i+1));
  202. }
  203. }
  204. totalvalue |= (value&0xFFFF);
  205. printf("val:%04x,%04x\r\n", value&0xFFFF, totalvalue);
  206. #endif
  207. if (tc_callback)
  208. {
  209. tc_callback(value&0xFFFF);
  210. }
  211. tls_reg_write32(HR_TC_INT_EN, value);
  212. }
  213. /**
  214. * @brief This function is used to get touch sensor's irq status.
  215. *
  216. * @param[in] sensorno is the touch sensor number from 1 to 15.
  217. *
  218. * @retval >=0:irq status, -1:parameter wrong.
  219. *
  220. * @note None
  221. */
  222. int tls_touchsensor_irq_status_get(u32 sensorno)
  223. {
  224. u32 value = 0;
  225. if (sensorno && (sensorno <= 15))
  226. {
  227. value = tls_reg_read32(HR_TC_INT_EN);
  228. return (value&(1<<(sensorno-1)))?1:0;
  229. }
  230. return -1;
  231. }