touch_sensor_ll.h 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  1. // Copyright 2015-2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. /*******************************************************************************
  15. * NOTICE
  16. * The ll is not public api, don't use in application code.
  17. * See readme.md in soc/include/hal/readme.md
  18. ******************************************************************************/
  19. // The Lowlevel layer for Touch Sensor
  20. #pragma once
  21. #include <stdlib.h>
  22. #include <stdbool.h>
  23. #include "soc/touch_sensor_periph.h"
  24. #include "hal/touch_sensor_types.h"
  25. /**
  26. * Set touch sensor touch sensor times of charge and discharge.
  27. *
  28. * @param meas_timers The times of charge and discharge in each measure process of touch channels.
  29. * The timer frequency is 8Mhz. Range: 0 ~ 0xffff.
  30. */
  31. static inline void touch_ll_set_meas_times(uint16_t meas_time)
  32. {
  33. //The times of charge and discharge in each measure process of touch channels.
  34. RTCCNTL.touch_ctrl1.touch_meas_num = meas_time;
  35. //the waiting cycles (in 8MHz) between TOUCH_START and TOUCH_XPD
  36. RTCCNTL.touch_ctrl2.touch_xpd_wait = SOC_TOUCH_PAD_MEASURE_WAIT; //wait volt stable
  37. }
  38. /**
  39. * Get touch sensor times of charge and discharge.
  40. *
  41. * @param meas_times Pointer to accept times count of charge and discharge.
  42. */
  43. static inline void touch_ll_get_measure_times(uint16_t *meas_time)
  44. {
  45. *meas_time = RTCCNTL.touch_ctrl1.touch_meas_num;
  46. }
  47. /**
  48. * Set touch sensor sleep time.
  49. *
  50. * @param sleep_cycle The touch sensor will sleep after each measurement.
  51. * sleep_cycle decide the interval between each measurement.
  52. * t_sleep = sleep_cycle / (RTC_SLOW_CLK frequency).
  53. * The approximate frequency value of RTC_SLOW_CLK can be obtained using rtc_clk_slow_freq_get_hz function.
  54. */
  55. static inline void touch_ll_set_sleep_time(uint16_t sleep_time)
  56. {
  57. // touch sensor sleep cycle Time = sleep_cycle / RTC_SLOW_CLK(90k)
  58. RTCCNTL.touch_ctrl1.touch_sleep_cycles = sleep_time;
  59. }
  60. /**
  61. * Get touch sensor sleep time.
  62. *
  63. * @param sleep_cycle Pointer to accept sleep cycle number.
  64. */
  65. static inline void touch_ll_get_sleep_time(uint16_t *sleep_time)
  66. {
  67. *sleep_time = RTCCNTL.touch_ctrl1.touch_sleep_cycles;
  68. }
  69. /**
  70. * Set touch sensor high voltage threshold of chanrge.
  71. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  72. * So the high threshold should be less than the supply voltage.
  73. *
  74. * @param refh The high voltage threshold of chanrge.
  75. */
  76. static inline void touch_ll_set_voltage_high(touch_high_volt_t refh)
  77. {
  78. RTCCNTL.touch_ctrl2.touch_drefh = refh;
  79. }
  80. /**
  81. * Get touch sensor high voltage threshold of chanrge.
  82. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  83. * So the high threshold should be less than the supply voltage.
  84. *
  85. * @param refh The high voltage threshold of chanrge.
  86. */
  87. static inline void touch_ll_get_voltage_high(touch_high_volt_t *refh)
  88. {
  89. *refh = (touch_high_volt_t)RTCCNTL.touch_ctrl2.touch_drefh;
  90. }
  91. /**
  92. * Set touch sensor low voltage threshold of discharge.
  93. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  94. *
  95. * @param refl The low voltage threshold of discharge.
  96. */
  97. static inline void touch_ll_set_voltage_low(touch_low_volt_t refl)
  98. {
  99. RTCCNTL.touch_ctrl2.touch_drefl = refl;
  100. }
  101. /**
  102. * Get touch sensor low voltage threshold of discharge.
  103. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  104. *
  105. * @param refl The low voltage threshold of discharge.
  106. */
  107. static inline void touch_ll_get_voltage_low(touch_low_volt_t *refl)
  108. {
  109. *refl = (touch_low_volt_t)RTCCNTL.touch_ctrl2.touch_drefl;
  110. }
  111. /**
  112. * Set touch sensor high voltage attenuation of chanrge. The actual charge threshold is high voltage threshold minus attenuation value.
  113. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  114. * So the high threshold should be less than the supply voltage.
  115. *
  116. * @param refh The high voltage threshold of chanrge.
  117. */
  118. static inline void touch_ll_set_voltage_attenuation(touch_volt_atten_t atten)
  119. {
  120. RTCCNTL.touch_ctrl2.touch_drange = atten;
  121. }
  122. /**
  123. * Get touch sensor high voltage attenuation of chanrge. The actual charge threshold is high voltage threshold minus attenuation value.
  124. * The touch sensor measures the channel capacitance value by charging and discharging the channel.
  125. * So the high threshold should be less than the supply voltage.
  126. *
  127. * @param refh The high voltage threshold of chanrge.
  128. */
  129. static inline void touch_ll_get_voltage_attenuation(touch_volt_atten_t *atten)
  130. {
  131. *atten = (touch_volt_atten_t)RTCCNTL.touch_ctrl2.touch_drange;
  132. }
  133. /**
  134. * Set touch sensor charge/discharge speed(currents) for each pad.
  135. * If the slope is 0, the counter would always be zero.
  136. * If the slope is 1, the charging and discharging would be slow. The measurement time becomes longer.
  137. * If the slope is set 7, which is the maximum value, the charging and discharging would be fast.
  138. * The measurement time becomes shorter.
  139. *
  140. * @note The higher the charge and discharge current, the greater the immunity of the touch channel,
  141. * but it will increase the system power consumption.
  142. * @param touch_num Touch pad index.
  143. * @param slope touch pad charge/discharge speed(currents).
  144. */
  145. static inline void touch_ll_set_slope(touch_pad_t touch_num, touch_cnt_slope_t slope)
  146. {
  147. RTCIO.touch_pad[touch_num].dac = slope;
  148. }
  149. /**
  150. * Get touch sensor charge/discharge speed(currents) for each pad.
  151. * If the slope is 0, the counter would always be zero.
  152. * If the slope is 1, the charging and discharging would be slow. The measurement time becomes longer.
  153. * If the slope is set 7, which is the maximum value, the charging and discharging would be fast.
  154. * The measurement time becomes shorter.
  155. *
  156. * @param touch_num Touch pad index.
  157. * @param slope touch pad charge/discharge speed(currents).
  158. */
  159. static inline void touch_ll_get_slope(touch_pad_t touch_num, touch_cnt_slope_t *slope)
  160. {
  161. *slope = (touch_cnt_slope_t)RTCIO.touch_pad[touch_num].dac;
  162. }
  163. /**
  164. * Set initial voltage state of touch channel for each measurement.
  165. *
  166. * @param touch_num Touch pad index.
  167. * @param opt Initial voltage state.
  168. */
  169. static inline void touch_ll_set_tie_option(touch_pad_t touch_num, touch_tie_opt_t opt)
  170. {
  171. RTCIO.touch_pad[touch_num].tie_opt = opt;
  172. }
  173. /**
  174. * Get initial voltage state of touch channel for each measurement.
  175. *
  176. * @param touch_num Touch pad index.
  177. * @param opt Initial voltage state.
  178. */
  179. static inline void touch_ll_get_tie_option(touch_pad_t touch_num, touch_tie_opt_t *opt)
  180. {
  181. *opt = (touch_tie_opt_t)RTCIO.touch_pad[touch_num].tie_opt;
  182. }
  183. /**
  184. * Set touch sensor FSM mode.
  185. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  186. *
  187. * @param mode FSM mode.
  188. */
  189. static inline void touch_ll_set_fsm_mode(touch_fsm_mode_t mode)
  190. {
  191. RTCCNTL.touch_ctrl2.touch_start_force = mode;
  192. }
  193. /**
  194. * Get touch sensor FSM mode.
  195. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  196. *
  197. * @param mode FSM mode.
  198. */
  199. static inline void touch_ll_get_fsm_mode(touch_fsm_mode_t *mode)
  200. {
  201. *mode = (touch_fsm_mode_t)RTCCNTL.touch_ctrl2.touch_start_force;
  202. }
  203. /**
  204. * Start touch sensor FSM timer.
  205. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  206. *
  207. * @param mode FSM mode.
  208. */
  209. static inline void touch_ll_start_fsm(void)
  210. {
  211. RTCCNTL.touch_ctrl2.touch_clkgate_en = 1; //enable touch clock for FSM. or force enable.
  212. RTCCNTL.touch_ctrl2.touch_slp_timer_en = (RTCCNTL.touch_ctrl2.touch_start_force == TOUCH_FSM_MODE_TIMER ? 1 : 0);
  213. }
  214. /**
  215. * Stop touch sensor FSM timer.
  216. * The measurement action can be triggered by the hardware timer, as well as by the software instruction.
  217. *
  218. * @param mode FSM mode.
  219. */
  220. static inline void touch_ll_stop_fsm(void)
  221. {
  222. RTCCNTL.touch_ctrl2.touch_start_en = 0; //stop touch fsm
  223. RTCCNTL.touch_ctrl2.touch_slp_timer_en = 0;
  224. RTCCNTL.touch_ctrl2.touch_clkgate_en = 0; //enable touch clock for FSM. or force enable.
  225. }
  226. /**
  227. * Trigger a touch sensor measurement, only support in SW mode of FSM.
  228. */
  229. static inline void touch_ll_start_sw_meas(void)
  230. {
  231. RTCCNTL.touch_ctrl2.touch_start_en = 0;
  232. RTCCNTL.touch_ctrl2.touch_start_en = 1;
  233. }
  234. /**
  235. * Set touch sensor interrupt threshold.
  236. *
  237. * @param touch_num touch pad index.
  238. * @param threshold threshold of touchpad count.
  239. */
  240. /**
  241. * Set the trigger threshold of touch sensor.
  242. * The threshold determines the sensitivity of the touch sensor.
  243. * The threshold is the original value of the trigger state minus the benchmark value.
  244. *
  245. * @note If set "TOUCH_PAD_THRESHOLD_MAX", the touch is never be trigered.
  246. * @param touch_num touch pad index
  247. * @param threshold threshold of touch sensor.
  248. */
  249. static inline void touch_ll_set_threshold(touch_pad_t touch_num, uint32_t threshold)
  250. {
  251. SENS.touch_thresh[touch_num - 1].thresh = threshold;
  252. }
  253. /**
  254. * Get the trigger threshold of touch sensor.
  255. * The threshold determines the sensitivity of the touch sensor.
  256. * The threshold is the original value of the trigger state minus the benchmark value.
  257. *
  258. * @param touch_num touch pad index.
  259. * @param threshold pointer to accept threshold.
  260. */
  261. static inline void touch_ll_get_threshold(touch_pad_t touch_num, uint32_t *threshold)
  262. {
  263. *threshold = SENS.touch_thresh[touch_num - 1].thresh;
  264. }
  265. /**
  266. * Enable touch sensor channel. Register touch channel into touch sensor measurement group.
  267. * The working mode of the touch sensor is simultaneous measurement.
  268. * This function will set the measure bits according to the given bitmask.
  269. *
  270. * @note If set this mask, the FSM timer should be stop firsty.
  271. * @note The touch sensor that in scan map, should be deinit GPIO function firstly.
  272. * @param enable_mask bitmask of touch sensor scan group.
  273. * e.g. TOUCH_PAD_NUM1 -> BIT(1)
  274. * @return
  275. * - ESP_OK on success
  276. */
  277. static inline void touch_ll_set_channel_mask(uint16_t enable_mask)
  278. {
  279. RTCCNTL.touch_scan_ctrl.touch_scan_pad_map |= (enable_mask & SOC_TOUCH_SENSOR_BIT_MASK_MAX);
  280. SENS.sar_touch_conf.touch_outen |= (enable_mask & SOC_TOUCH_SENSOR_BIT_MASK_MAX);
  281. }
  282. /**
  283. * Get touch sensor channel mask.
  284. *
  285. * @param enable_mask bitmask of touch sensor scan group.
  286. * e.g. TOUCH_PAD_NUM1 -> BIT(1)
  287. */
  288. static inline void touch_ll_get_channel_mask(uint16_t *enable_mask)
  289. {
  290. *enable_mask = SENS.sar_touch_conf.touch_outen \
  291. & RTCCNTL.touch_scan_ctrl.touch_scan_pad_map \
  292. & SOC_TOUCH_SENSOR_BIT_MASK_MAX;
  293. }
  294. /**
  295. * Disable touch sensor channel by bitmask.
  296. *
  297. * @param enable_mask bitmask of touch sensor scan group.
  298. * e.g. TOUCH_PAD_NUM1 -> BIT(1)
  299. */
  300. static inline void touch_ll_clear_channel_mask(uint16_t disable_mask)
  301. {
  302. SENS.sar_touch_conf.touch_outen &= ~(disable_mask & SOC_TOUCH_SENSOR_BIT_MASK_MAX);
  303. RTCCNTL.touch_scan_ctrl.touch_scan_pad_map &= ~(disable_mask & SOC_TOUCH_SENSOR_BIT_MASK_MAX);
  304. }
  305. /**
  306. * Get the touch sensor trigger status, usually used in ISR to decide which pads are 'touched'.
  307. *
  308. * @param status_mask The touch sensor status. e.g. Touch1 trigger status is `status_mask & (BIT1)`.
  309. */
  310. static inline void touch_ll_read_trigger_status_mask(uint32_t *status_mask)
  311. {
  312. *status_mask = SENS.sar_touch_chn_st.touch_pad_active;
  313. }
  314. /**
  315. * Clear all touch sensor status.
  316. *
  317. * @note Generally no manual removal is required.
  318. */
  319. static inline void touch_ll_clear_trigger_status_mask(void)
  320. {
  321. SENS.sar_touch_conf.touch_status_clr = 1;
  322. }
  323. /**
  324. * To enable touch pad interrupt.
  325. */
  326. static inline void touch_ll_enable_interrupt(touch_pad_intr_mask_t int_mask)
  327. {
  328. if (int_mask & TOUCH_PAD_INTR_MASK_DONE) {
  329. RTCCNTL.int_ena.rtc_touch_done = 1;
  330. }
  331. if (int_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  332. RTCCNTL.int_ena.rtc_touch_active = 1;
  333. }
  334. if (int_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  335. RTCCNTL.int_ena.rtc_touch_inactive = 1;
  336. }
  337. }
  338. /**
  339. * To disable touch pad interrupt.
  340. */
  341. static inline void touch_ll_disable_interrupt(touch_pad_intr_mask_t int_mask)
  342. {
  343. if (int_mask & TOUCH_PAD_INTR_MASK_DONE) {
  344. RTCCNTL.int_ena.rtc_touch_done = 0;
  345. }
  346. if (int_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  347. RTCCNTL.int_ena.rtc_touch_active = 0;
  348. }
  349. if (int_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  350. RTCCNTL.int_ena.rtc_touch_inactive = 0;
  351. }
  352. }
  353. /**
  354. * Get touch sensor raw data (touch sensor counter value) from register. No block.
  355. *
  356. * @param touch_num touch pad index.
  357. * @return touch_value pointer to accept touch sensor value.
  358. */
  359. static inline uint32_t touch_ll_read_raw_data(touch_pad_t touch_num)
  360. {
  361. return SENS.touch_meas[touch_num].meas_out;
  362. }
  363. /**
  364. * Get touch sensor measure status. No block.
  365. *
  366. * @return
  367. * - If touch sensors measure done.
  368. */
  369. static inline bool touch_ll_meas_is_done(void)
  370. {
  371. return (bool)SENS.sar_touch_chn_st.touch_meas_done;
  372. }
  373. /************************* esp32s2beta only *************************/
  374. /**
  375. * Reset the whole of touch module.
  376. *
  377. * @note Call this funtion after `touch_pad_fsm_stop`,
  378. */
  379. static inline void touch_ll_reset(void)
  380. {
  381. RTCCNTL.touch_ctrl2.touch_reset = 0;
  382. RTCCNTL.touch_ctrl2.touch_reset = 1;
  383. }
  384. /**
  385. * Set connection type of touch channel in idle status.
  386. * When a channel is in measurement mode, other initialized channels are in idle mode.
  387. * The touch channel is generally adjacent to the trace, so the connection state of the idle channel
  388. * affects the stability and sensitivity of the test channel.
  389. * The `CONN_HIGHZ`(high resistance) setting increases the sensitivity of touch channels.
  390. * The `CONN_GND`(grounding) setting increases the stability of touch channels.
  391. *
  392. * @param type Select idle channel connect to high resistance state or ground.
  393. */
  394. static inline void touch_ll_set_inactive_connect(touch_pad_conn_type_t type)
  395. {
  396. RTCCNTL.touch_scan_ctrl.touch_inactive_connection = type;
  397. }
  398. /**
  399. * Set connection type of touch channel in idle status.
  400. * When a channel is in measurement mode, other initialized channels are in idle mode.
  401. * The touch channel is generally adjacent to the trace, so the connection state of the idle channel
  402. * affects the stability and sensitivity of the test channel.
  403. * The `CONN_HIGHZ`(high resistance) setting increases the sensitivity of touch channels.
  404. * The `CONN_GND`(grounding) setting increases the stability of touch channels.
  405. *
  406. * @param type Select idle channel connect to high resistance state or ground.
  407. */
  408. static inline void touch_ll_get_inactive_connect(touch_pad_conn_type_t *type)
  409. {
  410. *type = RTCCNTL.touch_scan_ctrl.touch_inactive_connection;
  411. }
  412. /**
  413. * Get the current measure channel. Touch sensor measurement is cyclic scan mode.
  414. *
  415. * @return
  416. * - touch channel number
  417. */
  418. static inline touch_pad_t touch_ll_get_current_meas_channel(void)
  419. {
  420. return (touch_pad_t)(SENS.sar_touch_status0.touch_scan_curr);
  421. }
  422. /**
  423. * Enable touch sensor interrupt by bitmask.
  424. *
  425. * @param type interrupt type
  426. */
  427. static inline void touch_ll_intr_enable(touch_pad_intr_mask_t int_mask)
  428. {
  429. if (int_mask & TOUCH_PAD_INTR_MASK_DONE) {
  430. RTCCNTL.int_ena.rtc_touch_done = 1;
  431. }
  432. if (int_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  433. RTCCNTL.int_ena.rtc_touch_active = 1;
  434. }
  435. if (int_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  436. RTCCNTL.int_ena.rtc_touch_inactive = 1;
  437. }
  438. }
  439. /**
  440. * Disable touch sensor interrupt by bitmask.
  441. *
  442. * @param type interrupt type
  443. */
  444. static inline void touch_ll_intr_disable(touch_pad_intr_mask_t int_mask)
  445. {
  446. if (int_mask & TOUCH_PAD_INTR_MASK_DONE) {
  447. RTCCNTL.int_ena.rtc_touch_done = 0;
  448. }
  449. if (int_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
  450. RTCCNTL.int_ena.rtc_touch_active = 0;
  451. }
  452. if (int_mask & TOUCH_PAD_INTR_MASK_INACTIVE) {
  453. RTCCNTL.int_ena.rtc_touch_inactive = 0;
  454. }
  455. }
  456. /**
  457. * Get the bitmask of touch sensor interrupt status.
  458. *
  459. * @return type interrupt type
  460. */
  461. static inline uint32_t touch_ll_read_intr_status_mask(void)
  462. {
  463. uint32_t intr_st = RTCCNTL.int_st.val;
  464. uint32_t intr_msk = 0;
  465. if (intr_st & RTC_CNTL_TOUCH_DONE_INT_ST_M) {
  466. intr_msk |= TOUCH_PAD_INTR_MASK_DONE;
  467. }
  468. if (intr_st & RTC_CNTL_TOUCH_ACTIVE_INT_ST_M) {
  469. intr_msk |= TOUCH_PAD_INTR_MASK_ACTIVE;
  470. }
  471. if (intr_st & RTC_CNTL_TOUCH_INACTIVE_INT_ST_M) {
  472. intr_msk |= TOUCH_PAD_INTR_MASK_INACTIVE;
  473. }
  474. return (intr_msk & TOUCH_PAD_INTR_MASK_ALL);
  475. }
  476. /************************ Filter register setting ************************/
  477. /**
  478. * Get benchmark value of touch sensor.
  479. *
  480. * @note After initialization, the benchmark value is the maximum during the first measurement period.
  481. * @param touch_num touch pad index
  482. * @param touch_value pointer to accept touch sensor value
  483. */
  484. static inline void touch_ll_filter_read_benchmark(touch_pad_t touch_num, uint32_t *basedata)
  485. {
  486. *basedata = SENS.sar_touch_status[touch_num - 1].touch_pad_benchmark;
  487. }
  488. /**
  489. * Force reset benchmark to raw data of touch sensor.
  490. *
  491. * @param touch_num touch pad index
  492. * - TOUCH_PAD_MAX Reset basaline of all channels.
  493. */
  494. static inline void touch_ll_filter_reset_benchmark(touch_pad_t touch_num)
  495. {
  496. if (touch_num == TOUCH_PAD_MAX) {
  497. SENS.sar_touch_chn_st.touch_channel_clr = SOC_TOUCH_SENSOR_BIT_MASK_MAX;
  498. } else {
  499. SENS.sar_touch_chn_st.touch_channel_clr = (1U << touch_num);
  500. }
  501. }
  502. /**
  503. * Set filter mode. The input to the filter is raw data and the output is the benchmark value.
  504. * Larger filter coefficients increase the stability of the benchmark.
  505. *
  506. * @param mode Filter mode type. Refer to `touch_filter_mode_t`.
  507. */
  508. static inline void touch_ll_filter_set_filter_mode(touch_filter_mode_t mode)
  509. {
  510. RTCCNTL.touch_filter_ctrl.touch_filter_mode = mode;
  511. }
  512. /**
  513. * Get filter mode. The input to the filter is raw data and the output is the benchmark value.
  514. *
  515. * @param mode Filter mode type. Refer to `touch_filter_mode_t`.
  516. */
  517. static inline void touch_ll_filter_get_filter_mode(touch_filter_mode_t *mode)
  518. {
  519. *mode = (touch_filter_mode_t)RTCCNTL.touch_filter_ctrl.touch_filter_mode;
  520. }
  521. /**
  522. * Set debounce count, such as `n`. If the measured values continue to exceed
  523. * the threshold for `n` times, it is determined that the touch sensor state changes.
  524. *
  525. * @param dbc_cnt Debounce count value.
  526. */
  527. static inline void touch_ll_filter_set_debounce(uint32_t dbc_cnt)
  528. {
  529. RTCCNTL.touch_filter_ctrl.touch_debounce = dbc_cnt;
  530. }
  531. /**
  532. * Get debounce count.
  533. *
  534. * @param dbc_cnt Debounce count value.
  535. */
  536. static inline void touch_ll_filter_get_debounce(uint32_t *dbc_cnt)
  537. {
  538. *dbc_cnt = RTCCNTL.touch_filter_ctrl.touch_debounce;
  539. }
  540. /**
  541. * Set noise threshold coefficient. noise = noise_thr * touch threshold.
  542. * If (raw data - benchmark) > (noise), the benchmark stop updating.
  543. * If (raw data - benchmark) < (noise), the benchmark start updating.
  544. * Range: 0 ~ 3. The coefficient is 0: 1/2; 1: 3/8; 2: 1/4; 3: 1/8;
  545. *
  546. * @param hys_thr Noise threshold coefficient.
  547. */
  548. static inline void touch_ll_filter_set_noise_thres(uint32_t noise_thr)
  549. {
  550. RTCCNTL.touch_filter_ctrl.touch_noise_thres = noise_thr;
  551. RTCCNTL.touch_filter_ctrl.config2 = noise_thr;
  552. RTCCNTL.touch_filter_ctrl.config1 = 0xF;
  553. RTCCNTL.touch_filter_ctrl.config3 = 2;
  554. }
  555. /**
  556. * Get noise threshold coefficient. noise = noise_thr * touch threshold.
  557. * If (raw data - benchmark) > (noise), the benchmark stop updating.
  558. * If (raw data - benchmark) < (noise), the benchmark start updating.
  559. * Range: 0 ~ 3. The coefficient is 0: 1/2; 1: 3/8; 2: 1/4; 3: 1/8;
  560. *
  561. * @param noise_thr Noise threshold coefficient.
  562. */
  563. static inline void touch_ll_filter_get_noise_thres(uint32_t *noise_thr)
  564. {
  565. *noise_thr = RTCCNTL.touch_filter_ctrl.touch_noise_thres;
  566. }
  567. /**
  568. * Set jitter filter step size.
  569. * If filter mode is jitter, should set filter step for jitter.
  570. * Range: 0 ~ 15
  571. *
  572. * @param step The step size of the data change when the benchmark is updated.
  573. */
  574. static inline void touch_ll_filter_set_jitter_step(uint32_t step)
  575. {
  576. RTCCNTL.touch_filter_ctrl.touch_jitter_step = step;
  577. }
  578. /**
  579. * Get jitter filter step size.
  580. * If filter mode is jitter, should set filter step for jitter.
  581. * Range: 0 ~ 15
  582. *
  583. * @param step The step size of the data change when the benchmark is updated.
  584. */
  585. static inline void touch_ll_filter_get_jitter_step(uint32_t *step)
  586. {
  587. *step = RTCCNTL.touch_filter_ctrl.touch_jitter_step;
  588. }
  589. /**
  590. * Enable touch sensor filter and detection algorithm.
  591. * For more details on the detection algorithm, please refer to the application documentation.
  592. */
  593. static inline void touch_ll_filter_enable(void)
  594. {
  595. RTCCNTL.touch_filter_ctrl.touch_filter_en = 1;
  596. }
  597. /**
  598. * Disable touch sensor filter and detection algorithm.
  599. * For more details on the detection algorithm, please refer to the application documentation.
  600. */
  601. static inline void touch_ll_filter_disable(void)
  602. {
  603. RTCCNTL.touch_filter_ctrl.touch_filter_en = 0;
  604. }
  605. /************************ Denoise register setting ************************/
  606. /**
  607. * Enable denoise function.
  608. * T0 is an internal channel that does not have a corresponding external GPIO.
  609. * T0 will work simultaneously with the measured channel Tn. Finally, the actual
  610. * measured value of Tn is the value after subtracting lower bits of T0.
  611. * This denoise function filters out interference introduced on all channels,
  612. * such as noise introduced by the power supply and external EMI.
  613. */
  614. static inline void touch_ll_denoise_enable(void)
  615. {
  616. RTCCNTL.touch_scan_ctrl.touch_denoise_en = 1;
  617. }
  618. /**
  619. * Enable denoise function.
  620. * T0 is an internal channel that does not have a corresponding external GPIO.
  621. * T0 will work simultaneously with the measured channel Tn. Finally, the actual
  622. * measured value of Tn is the value after subtracting lower bits of T0.
  623. * This denoise function filters out interference introduced on all channels,
  624. * such as noise introduced by the power supply and external EMI.
  625. */
  626. static inline void touch_ll_denoise_disable(void)
  627. {
  628. RTCCNTL.touch_scan_ctrl.touch_denoise_en = 0;
  629. }
  630. /**
  631. * Set internal reference capacitance of denoise channel.
  632. * Select the appropriate internal reference capacitance value so that
  633. * the reading of denoise channel is closest to the reading of the channel being measured.
  634. *
  635. * @param cap_level Capacitance level.
  636. */
  637. static inline void touch_ll_denoise_set_cap_level(touch_pad_denoise_cap_t cap_level)
  638. {
  639. RTCCNTL.touch_ctrl2.touch_refc = cap_level;
  640. }
  641. /**
  642. * Get internal reference capacitance of denoise channel.
  643. * Select the appropriate internal reference capacitance value so that
  644. * the reading of denoise channel is closest to the reading of the channel being measured.
  645. *
  646. * @param cap_level Capacitance level.
  647. */
  648. static inline void touch_ll_denoise_get_cap_level(touch_pad_denoise_cap_t *cap_level)
  649. {
  650. *cap_level = RTCCNTL.touch_ctrl2.touch_refc;
  651. }
  652. /**
  653. * Set denoise range of denoise channel.
  654. * Determined by measuring the noise amplitude of the denoise channel.
  655. *
  656. * @param grade Denoise range of denoise channel.
  657. */
  658. static inline void touch_ll_denoise_set_grade(touch_pad_denoise_grade_t grade)
  659. {
  660. RTCCNTL.touch_scan_ctrl.touch_denoise_res = grade;
  661. }
  662. /**
  663. * Set denoise range of denoise channel.
  664. * Determined by measuring the noise amplitude of the denoise channel.
  665. *
  666. * @param grade Denoise range of denoise channel.
  667. */
  668. static inline void touch_ll_denoise_get_grade(touch_pad_denoise_grade_t *grade)
  669. {
  670. *grade = RTCCNTL.touch_scan_ctrl.touch_denoise_res;
  671. }
  672. /**
  673. * Read denoise measure value (TOUCH_PAD_NUM0).
  674. *
  675. * @param denoise value of denoise.
  676. */
  677. static inline uint32_t touch_ll_denoise_read_data(uint32_t *data)
  678. {
  679. return (uint32_t)SENS.sar_touch_status0.touch_denoise_data;
  680. }
  681. /************************ Waterproof register setting ************************/
  682. /**
  683. * Set touch channel use for guard pad.
  684. *
  685. * @param pad_num Touch sensor channel number.
  686. */
  687. static inline void touch_ll_waterproof_set_guard_pad(touch_pad_t pad_num)
  688. {
  689. RTCCNTL.touch_scan_ctrl.touch_out_ring = pad_num;
  690. }
  691. /**
  692. * Get touch channel use for guard pad.
  693. *
  694. * @param pad_num Touch sensor channel number.
  695. */
  696. static inline void touch_ll_waterproof_get_guard_pad(touch_pad_t *pad_num)
  697. {
  698. *pad_num = RTCCNTL.touch_scan_ctrl.touch_out_ring;
  699. }
  700. /**
  701. * Set max equivalent capacitance for sheild channel.
  702. * The equivalent capacitance of the shielded channel can be calculated
  703. * from the reading of denoise channel.
  704. *
  705. * @param pad_num Touch sensor channel number.
  706. */
  707. static inline void touch_ll_waterproof_set_sheild_driver(touch_pad_shield_driver_t driver_level)
  708. {
  709. RTCCNTL.touch_scan_ctrl.touch_bufdrv = driver_level;
  710. }
  711. /**
  712. * Get max equivalent capacitance for sheild channel.
  713. * The equivalent capacitance of the shielded channel can be calculated
  714. * from the reading of denoise channel.
  715. *
  716. * @param pad_num Touch sensor channel number.
  717. */
  718. static inline void touch_ll_waterproof_get_sheild_driver(touch_pad_shield_driver_t *driver_level)
  719. {
  720. *driver_level = RTCCNTL.touch_scan_ctrl.touch_bufdrv;
  721. }
  722. /**
  723. * Enable parameter of waterproof function.
  724. * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel.
  725. * The shielded channel outputs the same signal as the channel being measured.
  726. * It is generally designed as a grid and is placed around the touch buttons.
  727. * The shielded channel does not follow the measurement signal of the protection channel.
  728. * So that the guard channel can detect a large area of water.
  729. */
  730. static inline void touch_ll_waterproof_enable(void)
  731. {
  732. RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = 1;
  733. }
  734. /**
  735. * Disable parameter of waterproof function.
  736. * The waterproof function includes a shielded channel (TOUCH_PAD_NUM14) and a guard channel.
  737. * The shielded channel outputs the same signal as the channel being measured.
  738. * It is generally designed as a grid and is placed around the touch buttons.
  739. * The shielded channel does not follow the measurement signal of the protection channel.
  740. * So that the guard channel can detect a large area of water.
  741. */
  742. static inline void touch_ll_waterproof_disable(void)
  743. {
  744. RTCCNTL.touch_scan_ctrl.touch_shield_pad_en = 0;
  745. }
  746. /************************ Proximity register setting ************************/
  747. /**
  748. * Set touch channel number for proximity pad.
  749. * If disable the proximity pad, point this pad to `TOUCH_PAD_NUM0`
  750. *
  751. * @param prox_pad The array of three proximity pads.
  752. */
  753. static inline void touch_ll_proximity_set_channel_num(const touch_pad_t prox_pad[])
  754. {
  755. SENS.sar_touch_conf.touch_approach_pad0 = prox_pad[0];
  756. SENS.sar_touch_conf.touch_approach_pad1 = prox_pad[1];
  757. SENS.sar_touch_conf.touch_approach_pad2 = prox_pad[2];
  758. }
  759. /**
  760. * Get touch channel number for proximity pad.
  761. * If disable the proximity pad, point this pad to `TOUCH_PAD_NUM0`
  762. *
  763. * @param prox_pad The array of three proximity pads.
  764. */
  765. static inline void touch_ll_proximity_get_channel_num(touch_pad_t prox_pad[])
  766. {
  767. prox_pad[0] = SENS.sar_touch_conf.touch_approach_pad0;
  768. prox_pad[1] = SENS.sar_touch_conf.touch_approach_pad1;
  769. prox_pad[2] = SENS.sar_touch_conf.touch_approach_pad2;
  770. }
  771. /**
  772. * Set cumulative measurement times for proximity pad.
  773. *
  774. * @param times The cumulative number of measurement cycles.
  775. */
  776. static inline void touch_ll_proximity_set_meas_times(uint32_t times)
  777. {
  778. RTCCNTL.touch_approach.touch_approach_meas_time = times;
  779. }
  780. /**
  781. * Get cumulative measurement times for proximity pad.
  782. *
  783. * @param times The cumulative number of measurement cycles.
  784. */
  785. static inline void touch_ll_proximity_get_meas_times(uint32_t *times)
  786. {
  787. *times = RTCCNTL.touch_approach.touch_approach_meas_time;
  788. }
  789. /**
  790. * Read current cumulative measurement times for proximity pad.
  791. *
  792. * @param times The cumulative number of measurement cycles.
  793. */
  794. static inline void touch_ll_proximity_read_meas_cnt(touch_pad_t touch_num, uint32_t *cnt)
  795. {
  796. if (SENS.sar_touch_conf.touch_approach_pad0 == touch_num) {
  797. *cnt = SENS.sar_touch_appr_status.touch_approach_pad0_cnt;
  798. } else if (SENS.sar_touch_conf.touch_approach_pad1 == touch_num) {
  799. *cnt = SENS.sar_touch_appr_status.touch_approach_pad1_cnt;
  800. } else if (SENS.sar_touch_conf.touch_approach_pad2 == touch_num) {
  801. *cnt = SENS.sar_touch_appr_status.touch_approach_pad2_cnt;
  802. }
  803. }
  804. /**
  805. * Check if the touch sensor channel is the proximity pad.
  806. *
  807. * @param touch_num The touch sensor channel number.
  808. */
  809. static inline bool touch_ll_proximity_pad_check(touch_pad_t touch_num)
  810. {
  811. if ((SENS.sar_touch_conf.touch_approach_pad0 != touch_num)
  812. && (SENS.sar_touch_conf.touch_approach_pad1 != touch_num)
  813. && (SENS.sar_touch_conf.touch_approach_pad2 != touch_num)) {
  814. return false;
  815. } else {
  816. return true;
  817. }
  818. }
  819. /************** sleep pad setting ***********************/
  820. /**
  821. * Set touch channel number for sleep pad.
  822. *
  823. * @note Only one touch sensor channel is supported in deep sleep mode.
  824. * @param touch_num Touch sensor channel number.
  825. */
  826. static inline void touch_ll_sleep_set_channel_num(touch_pad_t touch_num)
  827. {
  828. RTCCNTL.touch_slp_thres.touch_slp_pad = touch_num;
  829. }
  830. /**
  831. * Get touch channel number for sleep pad.
  832. *
  833. * @note Only one touch sensor channel is supported in deep sleep mode.
  834. * @param touch_num Touch sensor channel number.
  835. */
  836. static inline void touch_ll_sleep_get_channel_num(touch_pad_t *touch_num)
  837. {
  838. *touch_num = RTCCNTL.touch_slp_thres.touch_slp_pad;
  839. }
  840. /**
  841. * Set the trigger threshold of touch sensor in deep sleep.
  842. * The threshold determines the sensitivity of the touch sensor.
  843. * The threshold is the original value of the trigger state minus the benchmark value.
  844. *
  845. * @note The threshold at sleep is the same as the threshold before sleep.
  846. */
  847. static inline void touch_ll_sleep_set_threshold(uint32_t touch_thres)
  848. {
  849. RTCCNTL.touch_slp_thres.touch_slp_th = touch_thres;
  850. }
  851. /**
  852. * Get the trigger threshold of touch sensor in deep sleep.
  853. * The threshold determines the sensitivity of the touch sensor.
  854. * The threshold is the original value of the trigger state minus the benchmark value.
  855. *
  856. * @note The threshold at sleep is the same as the threshold before sleep.
  857. */
  858. static inline void touch_ll_sleep_get_threshold(uint32_t *touch_thres)
  859. {
  860. *touch_thres = RTCCNTL.touch_slp_thres.touch_slp_th;
  861. }
  862. /**
  863. * Enable proximity function for sleep pad.
  864. */
  865. static inline void touch_ll_sleep_enable_approach(void)
  866. {
  867. RTCCNTL.touch_slp_thres.touch_slp_approach_en = 1;
  868. }
  869. /**
  870. * Disable proximity function for sleep pad.
  871. */
  872. static inline void touch_ll_sleep_disable_approach(void)
  873. {
  874. RTCCNTL.touch_slp_thres.touch_slp_approach_en = 0;
  875. }
  876. /**
  877. * Read benchmark of touch sensor for sleep pad.
  878. *
  879. * @param benchmark Pointer to accept touch sensor benchmark value.
  880. */
  881. static inline void touch_ll_sleep_read_benchmark(uint32_t *benchmark)
  882. {
  883. *benchmark = REG_GET_FIELD(SENS_SAR_TOUCH_SLP_STATUS_REG, SENS_TOUCH_SLP_BENCHMARK);
  884. }
  885. /**
  886. * Read debounce of touch sensor for sleep pad.
  887. *
  888. * @param debounce Pointer to accept touch sensor debounce value.
  889. */
  890. static inline void touch_ll_sleep_read_debounce(uint32_t *debounce)
  891. {
  892. *debounce = REG_GET_FIELD(SENS_SAR_TOUCH_SLP_STATUS_REG, SENS_TOUCH_SLP_DEBOUNCE);
  893. }
  894. /**
  895. * Read proximity count of touch sensor for sleep pad.
  896. * @param proximity_cnt Pointer to accept touch sensor proximity count value.
  897. */
  898. static inline void touch_ll_sleep_read_proximity_cnt(uint32_t *approach_cnt)
  899. {
  900. *approach_cnt = REG_GET_FIELD(SENS_SAR_TOUCH_APPR_STATUS_REG, SENS_TOUCH_SLP_APPROACH_CNT);
  901. }
  902. /**
  903. * Get the touch pad which caused wakeup from deep sleep.
  904. *
  905. * @param pad_num pointer to touch pad which caused wakeup.
  906. */
  907. static inline void touch_ll_get_wakeup_status(touch_pad_t *pad_num)
  908. {
  909. *pad_num = (touch_pad_t)RTCCNTL.touch_slp_thres.touch_slp_pad;
  910. }