n32g45x_tsc.h 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_tsc.h
  29. * @author Nations
  30. * @version v1.0.3
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __N32G45X_TSC_H__
  35. #define __N32G45X_TSC_H__
  36. #ifdef __cplusplus
  37. extern "C" {
  38. #endif
  39. #include "n32g45x.h"
  40. /** @addtogroup N32G45X_StdPeriph_Driver
  41. * @{
  42. */
  43. /** @addtogroup TSC
  44. * @{
  45. */
  46. /**
  47. * @brief TSC error code
  48. */
  49. typedef enum {
  50. TSC_ERROR_OK = 0x00U, /*!< No error */
  51. TSC_ERROR_CLOCK = 0x01U, /*!< clock config error */
  52. TSC_ERROR_PARAMETER = 0x02U, /*!< parameter error */
  53. TSC_ERROR_HW_MODE = 0x02U, /*!< Exit hw mode timeout */
  54. }TSC_ErrorTypeDef;
  55. /**
  56. * @
  57. */
  58. /**
  59. * @brief TSC clock source
  60. */
  61. #define TSC_CLK_SRC_LSI (0x00000000) /*!< LSI*/
  62. #define TSC_CLK_SRC_LSE (RCC_LSE_ENABLE) /*!< LSE */
  63. #define TSC_CLK_SRC_LSE_BYPASS (RCC_LSE_BYPASS) /*!< LSE bypass */
  64. /**
  65. * @
  66. */
  67. /**
  68. * @defgroup Detect_Period
  69. */
  70. #define TSC_DET_PERIOD_8 (0x00000000U) /*!< DET_PERIOD[3:0] = 8/TSC_CLOCK */
  71. #define TSC_DET_PERIOD_16 (0x01UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000001U DET_PERIOD[3:0] = 16/TSC_CLOCK */
  72. #define TSC_DET_PERIOD_24 (0x02UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000002U DET_PERIOD[3:0] = 24/TSC_CLOCK */
  73. #define TSC_DET_PERIOD_32 (0x03UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000003U DET_PERIOD[3:0] = 32/TSC_CLOCK(default) */
  74. #define TSC_DET_PERIOD_40 (0x04UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000004U DET_PERIOD[3:0] = 40/TSC_CLOCK */
  75. #define TSC_DET_PERIOD_48 (0x05UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000005U DET_PERIOD[3:0] = 48/TSC_CLOCK */
  76. #define TSC_DET_PERIOD_56 (0x06UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000006U DET_PERIOD[3:0] = 56/TSC_CLOCK */
  77. #define TSC_DET_PERIOD_64 (0x07UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000007U DET_PERIOD[3:0] = 64/TSC_CLOCK */
  78. #define TSC_DET_PERIOD_72 (0x08UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000008U DET_PERIOD[3:0] = 72/TSC_CLOCK */
  79. #define TSC_DET_PERIOD_80 (0x09UL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x00000009U DET_PERIOD[3:0] = 80/TSC_CLOCK */
  80. #define TSC_DET_PERIOD_88 (0x0AUL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x0000000AU DET_PERIOD[3:0] = 88/TSC_CLOCK */
  81. #define TSC_DET_PERIOD_96 (0x0BUL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x0000000BU DET_PERIOD[3:0] = 96/TSC_CLOCK */
  82. #define TSC_DET_PERIOD_104 (0x0CUL << TSC_CTRL_DET_PERIOD_SHIFT) /*!< 0x0000000CU DET_PERIOD[3:0] = 104/TSC_CLOCK */
  83. #define TSC_DET_PERIOD_8_32KHZ ((uint8_t)TSC_DET_PERIOD_8)
  84. #define TSC_DET_PERIOD_16_32KHZ ((uint8_t)TSC_DET_PERIOD_16)
  85. #define TSC_DET_PERIOD_24_32KHZ ((uint8_t)TSC_DET_PERIOD_24)
  86. #define TSC_DET_PERIOD_32_32KHZ ((uint8_t)TSC_DET_PERIOD_32)
  87. #define TSC_DET_PERIOD_40_32KHZ ((uint8_t)TSC_DET_PERIOD_40)
  88. #define TSC_DET_PERIOD_48_32KHZ ((uint8_t)TSC_DET_PERIOD_48)
  89. #define TSC_DET_PERIOD_56_32KHZ ((uint8_t)TSC_DET_PERIOD_56)
  90. #define TSC_DET_PERIOD_64_32KHZ ((uint8_t)TSC_DET_PERIOD_64)
  91. #define TSC_DET_PERIOD_72_32KHZ ((uint8_t)TSC_DET_PERIOD_72)
  92. #define TSC_DET_PERIOD_80_32KHZ ((uint8_t)TSC_DET_PERIOD_80)
  93. #define TSC_DET_PERIOD_88_32KHZ ((uint8_t)TSC_DET_PERIOD_88)
  94. #define TSC_DET_PERIOD_96_32KHZ ((uint8_t)TSC_DET_PERIOD_96)
  95. #define TSC_DET_PERIOD_104_32KHZ ((uint8_t)TSC_DET_PERIOD_104)
  96. /**
  97. * @
  98. */
  99. /**
  100. * @defgroup Detect_Filter
  101. */
  102. #define TSC_DET_FILTER_1 (0x00000000U) /*!< DET_FILTER[3:0] = 1 sample */
  103. #define TSC_DET_FILTER_2 (0x01UL << TSC_CTRL_DET_FILTER_SHIFT) /*!< 0x00000010U DET_FILTER[3:0] = 2 samples */
  104. #define TSC_DET_FILTER_3 (0x02UL << TSC_CTRL_DET_FILTER_SHIFT) /*!< 0x00000020U DET_FILTER[3:0] = 3 samples */
  105. #define TSC_DET_FILTER_4 (0x03UL << TSC_CTRL_DET_FILTER_SHIFT) /*!< 0x00000030U DET_FILTER[3:0] = 4 samples */
  106. #define TSC_HW_DET_FILTER_1 ((uint8_t)TSC_DET_FILTER_1)
  107. #define TSC_HW_DET_FILTER_2 ((uint8_t)TSC_DET_FILTER_2)
  108. #define TSC_HW_DET_FILTER_3 ((uint8_t)TSC_DET_FILTER_3)
  109. #define TSC_HW_DET_FILTER_4 ((uint8_t)TSC_DET_FILTER_4)
  110. /**
  111. * @
  112. */
  113. /**
  114. * @defgroup HW_Detect_Mode
  115. */
  116. #define TSC_HW_DET_MODE_DISABLE (0x00000000U) /*!< Hardware detect mode disable */
  117. #define TSC_HW_DET_MODE_ENABLE (0x01UL << TSC_CTRL_HW_DET_MODE_SHIFT) /*!< 0x00000040U Hardware detect mode enable */
  118. #define TSC_HW_DET_ENABLE TSC_HW_DET_MODE_ENABLE
  119. /**
  120. * @
  121. */
  122. /**
  123. * @defgroup Detect_Type
  124. */
  125. #define TSC_DET_TYPE_MASK (TSC_CTRL_LESS_DET_SEL_MASK|TSC_CTRL_GREAT_DET_SEL_MASK)
  126. #define TSC_DET_TYPE_SHIFT (TSC_CTRL_LESS_DET_SEL_SHIFT)
  127. #define TSC_DET_TYPE_NONE (0UL) /*!< 0x00000000U Disable detect */
  128. #define TSC_DET_TYPE_LESS (0x01UL << TSC_DET_TYPE_SHIFT) /*!< 0x00000100U Less detect enable */
  129. #define TSC_DET_TYPE_GREAT (0x02UL << TSC_DET_TYPE_SHIFT) /*!< 0x00000200U Great detect enable */
  130. #define TSC_DET_TYPE_PERIOD (0x03UL << TSC_DET_TYPE_SHIFT) /*!< 0x00000300U Both great and less detct enable */
  131. /**
  132. * @
  133. */
  134. /**
  135. * @defgroup TSC_Interrupt
  136. */
  137. #define TSC_IT_DET_ENABLE (TSC_CTRL_DET_INTEN) /*!< Enable TSC detect interrupt */
  138. #define TSC_IT_DET_DISABLE (0UL) /*!< Disable TSC detect interrupt */
  139. /**
  140. * @
  141. */
  142. /**
  143. * @defgroup TSC_Out
  144. */
  145. #define TSC_OUT_PIN (0x00000000U) /*!< TSC output to TSC_OUT pin */
  146. #define TSC_OUT_TIM4_ETR (0x1UL << TSC_CTRL_TM4_ETR_SHIFT) /*!< TSC output to TIM4 ETR */
  147. #define TSC_OUT_TIM2_ETR (0x2UL << TSC_CTRL_TM4_ETR_SHIFT) /*!< TSC output to TIM2 ETR and TIM2 CH1*/
  148. /**
  149. * @
  150. */
  151. /**
  152. * @defgroup TSC_Flag
  153. */
  154. #define TSC_FLAG_HW (0x1UL << TSC_CTRL_HW_DET_ST_SHIFT) /*!< Flag of hardware detect mode */
  155. #define TSC_FLAG_GREAT_DET (0x1UL << TSC_STS_GREAT_DET_SHIFT) /*!< Flag of great detect type */
  156. #define TSC_FLAG_LESS_DET (0x1UL << TSC_STS_LESS_DET_SHIFT) /*!< Flag of less detect type */
  157. #define TSC_FLAG_PERIOD_DET (TSC_FLAG_GREAT_DET|TSC_FLAG_LESS_DET) /*!< Flag of period detect type */
  158. /**
  159. * @
  160. */
  161. /**
  162. * @defgroup TSC_SW_Detect
  163. */
  164. #define TSC_SW_MODE_DISABLE (0x00000000U) /*!< Disable software detect mode */
  165. #define TSC_SW_MODE_ENABLE (0x1UL << TSC_ANA_CTRL_SW_TSC_EN_SHIFT) /*!< Enable software detect mode */
  166. /**
  167. * @
  168. */
  169. /**
  170. * @defgroup TSC_PadOption
  171. */
  172. #define TSC_PAD_INTERNAL_RES (0x00000000U) /*!< Use internal resistor */
  173. #define TSC_PAD_EXTERNAL_RES (0x1UL << TSC_ANA_SEL_PAD_OPT_SHIFT) /*!< Use external resistor */
  174. #define TSC_INNER_RESIST TSC_PAD_INTERNAL_RES
  175. /**
  176. * @
  177. */
  178. /**
  179. * @defgroup TSC_PadSpeed
  180. */
  181. #define TSC_PAD_SPEED_0 (0x00000000U) /*!< Low speed,about 100K */
  182. #define TSC_PAD_SPEED_1 (0x1UL << TSC_ANA_SEL_SP_OPT_SHIFT) /*!< Middle spped */
  183. #define TSC_PAD_SPEED_2 (0x2UL << TSC_ANA_SEL_SP_OPT_SHIFT) /*!< Middle spped */
  184. #define TSC_PAD_SPEED_3 (0x3UL << TSC_ANA_SEL_SP_OPT_SHIFT) /*!< Middle spped */
  185. #define TSC_CMP_MASK (0x03UL) // comparator offset bit mask
  186. #define TSC_CMP_OFFSET TSC_ANA_SEL_SP_OPT_SHIFT // offset of comparator speed configuration
  187. #define TSC_CMP_SPEED_0 TSC_PAD_SPEED_0 // 100KHZ~200KHZ
  188. #define TSC_CMP_SPEED_1 TSC_PAD_SPEED_1 // 300KHZ~700KHZ
  189. #define TSC_CMP_SPEED_2 TSC_PAD_SPEED_2 // 300KHZ~700KHZ
  190. #define TSC_CMP_SPEED_3 TSC_PAD_SPEED_3 // 300KHZ~700KHZ
  191. /**
  192. * @
  193. */
  194. /**
  195. * @defgroup TSC_Touch_Lib
  196. */
  197. #define TSC_RESIST_1M TSC_RESR_CHN_RESIST_1M
  198. #define TSC_RESIST_875K TSC_RESR_CHN_RESIST_875K
  199. #define TSC_RESIST_750K TSC_RESR_CHN_RESIST_750K
  200. #define TSC_RESIST_625K TSC_RESR_CHN_RESIST_625K
  201. #define TSC_RESIST_500K TSC_RESR_CHN_RESIST_500K
  202. #define TSC_RESIST_375K TSC_RESR_CHN_RESIST_375K
  203. #define TSC_RESIST_250K TSC_RESR_CHN_RESIST_250K
  204. #define TSC_RESIST_125K TSC_RESR_CHN_RESIST_125K
  205. #define TSC_HW_CHN_MASK (0x00FFFFFF)
  206. #define TSC_CHN_ADDR_WIDTH (4)
  207. #define TSC_HW_BASE_BITS_OFFSET (0)
  208. #define TSC_HW_DELTA_BITS_OFFSET (16)
  209. /**
  210. * @
  211. */
  212. /**
  213. * @defgroup TSC_Constant
  214. */
  215. #define TSC_CHN_SEL_ALL (TSC_CHNEN_CHN_SEL_MASK)
  216. #define MAX_TSC_HW_CHN (24) /*Maximum number of tsc pin*/
  217. #define TSC_DET_MAX_CHN_COUNT MAX_TSC_HW_CHN
  218. #define MAX_TSC_THRESHOLD_BASE (2047) /*Maximum detect base value of threshold*/
  219. #define MAX_TSC_THRESHOLD_DELTA (255) /*Maximum detect delta value of threshold*/
  220. #define TSC_TIMEOUT (0x01000000) /*TSC normal timeout */
  221. /**
  222. * @
  223. */
  224. /* TSC Exported macros -----------------------------------------------------------*/
  225. /** @defgroup TSC_Exported_Macros
  226. * @{
  227. */
  228. /** @brief Enable the TSC HW detect mode
  229. * @param None
  230. * @retval None
  231. */
  232. #define __TSC_HW_ENABLE() SET_BIT(TSC->CTRL, TSC_HW_DET_MODE_ENABLE)
  233. /** @brief Disable the TSC HW detect mode
  234. * @param None
  235. * @retval None
  236. */
  237. #define __TSC_HW_DISABLE() CLEAR_BIT(TSC->CTRL, TSC_HW_DET_MODE_ENABLE)
  238. /** @brief Config TSC detect period for HW detect mode
  239. * @param __PERIOD__ specifies the TSC detect period during HW detect mode
  240. * @arg TSC_DET_PERIOD_8: Detect period = 8/TSC_CLK
  241. * @arg TSC_DET_PERIOD_16: Detect Period = 1/TSC_CLK
  242. * @arg TSC_DET_PERIOD_24: Detect Period = 2/TSC_CLK
  243. * @arg TSC_DET_PERIOD_32: Detect Period = 3/TSC_CLK
  244. * @arg TSC_DET_PERIOD_40: Detect Period = 4/TSC_CLK
  245. * @arg TSC_DET_PERIOD_48: Detect Period = 5/TSC_CLK
  246. * @arg TSC_DET_PERIOD_56: Detect Period = 6/TSC_CLK
  247. * @arg TSC_DET_PERIOD_64: Detect Period = 7/TSC_CLK
  248. * @arg TSC_DET_PERIOD_72: Detect Period = 7/TSC_CLK
  249. * @arg TSC_DET_PERIOD_80: Detect Period = 7/TSC_CLK
  250. * @arg TSC_DET_PERIOD_88: Detect Period = 7/TSC_CLK
  251. * @arg TSC_DET_PERIOD_96: Detect Period = 7/TSC_CLK
  252. * @arg TSC_DET_PERIOD_104:Detect Period = 7/TSC_CLK
  253. * @retval None
  254. */
  255. #define __TSC_PERIOD_CONFIG(__PERIOD__) MODIFY_REG(TSC->CTRL, TSC_CTRL_DET_PERIOD_MASK,__PERIOD__)
  256. /** @brief Config TSC detect filter for HW detect mode
  257. * @param __FILTER__ specifies the least usefull continuous samples during HW detect mode
  258. * @arg TSC_DET_FILTER_1: Detect filter = 1 pulse
  259. * @arg TSC_DET_FILTER_2: Detect filter = 2 pulse
  260. * @arg TSC_DET_FILTER_3: Detect filter = 3 pulse
  261. * @arg TSC_DET_FILTER_4: Detect filter = 4 pulse
  262. * @retval None
  263. */
  264. #define __TSC_FILTER_CONFIG(__FILTER__) MODIFY_REG(TSC->CTRL, TSC_CTRL_DET_FILTER_MASK,__FILTER__)
  265. /** @brief Config TSC detect type for HW detect mode,less great or both
  266. * @param __TYPE__ specifies the detect type of a sample during HW detect mode
  267. * @arg TSC_DET_TYPE_NONE: Detect disable
  268. * @arg TSC_DET_TYPE_LESS: Pulse number must be greater than the threshold(basee-delta) during a sample time
  269. * @arg TSC_DET_TYPE_GREAT: Pulse number must be less than the threshold(basee+delta) during a sample time
  270. * @arg TSC_DET_TYPE_PERIOD:Pulse number must be greater than (basee-delta)
  271. and also be less than (basee+delta) during a sample time
  272. * @retval None
  273. */
  274. #define __TSC_LESS_GREAT_CONFIG(__TYPE__) MODIFY_REG(TSC->CTRL, \
  275. (TSC_CTRL_LESS_DET_SEL_MASK|TSC_CTRL_GREAT_DET_SEL_MASK), \
  276. __TYPE__)
  277. /** @brief Enable TSC interrupt
  278. * @param None
  279. * @retval None
  280. */
  281. #define __TSC_INT_ENABLE() SET_BIT(TSC->CTRL, TSC_IT_DET_ENABLE)
  282. /** @brief Disable TSC interrupt
  283. * @param None
  284. * @retval None
  285. */
  286. #define __TSC_INT_DISABLE() CLEAR_BIT(TSC->CTRL, TSC_IT_DET_ENABLE)
  287. /** @brief Config the TSC output
  288. * @param __OUT__ specifies where the TSC output should go
  289. * @arg TSC_OUT_PIN: TSC output to the TSC_OUT pin
  290. * @arg TSC_OUT_TIM4_ETR: TSC output to TIM4 as ETR
  291. * @arg TSC_OUT_TIM2_ETR: TSC output to TIM2 as ETR
  292. * @retval None
  293. */
  294. #define __TSC_OUT_CONFIG(__OUT__) MODIFY_REG( TSC->CTRL, \
  295. (TSC_CTRL_TM4_ETR_MASK|TSC_CTRL_TM2_ETR_CH1_MASK),\
  296. __OUT__)
  297. /** @brief Config the TSC channel
  298. * @param __CHN__ specifies the pin of channels used for detect
  299. * This parameter:bit[0:23] used,bit[24:31] must be 0
  300. * bitx: TSC channel x
  301. * @retval None
  302. */
  303. #define __TSC_CHN_CONFIG(__CHN__) WRITE_REG(TSC->CHNEN, __CHN__)
  304. /** @brief Enable the TSC SW detect mode
  305. * @param None
  306. * @retval None
  307. */
  308. #define __TSC_SW_ENABLE() SET_BIT(TSC->ANA_CTRL, TSC_ANA_CTRL_SW_TSC_EN)
  309. /** @brief Disable the TSC SW detect mode
  310. * @param None
  311. * @retval None
  312. */
  313. #define __TSC_SW_DISABLE() CLEAR_BIT(TSC->ANA_CTRL, TSC_ANA_CTRL_SW_TSC_EN)
  314. /** @brief Config the detect channel number during SW detect mode
  315. * @param __NUM__ specifies channel number,must be less than MAX_TSC_HW_CHN
  316. * @retval None
  317. */
  318. #define __TSC_SW_CHN_NUM_CONFIG(__NUM__) MODIFY_REG(TSC->ANA_CTRL, TSC_ANA_CTRL_SW_PAD_MUX_MASK,__NUM__)
  319. /** @brief Config the pad charge type
  320. * @param __OPT__ specifies which resistor is used for charge
  321. * @arg TSC_PAD_INTERNAL_RES: Internal resistor is used
  322. * @arg TSC_PAD_EXTERNAL_RES: External resistor is used
  323. * @retval None
  324. */
  325. #define __TSC_PAD_OPT_CONFIG(__OPT__) MODIFY_REG(TSC->ANA_SEL, TSC_ANA_SEL_PAD_OPT_MASK,__OPT__)
  326. /** @brief Config TSC speed
  327. * @param __SPEED__ specifies the TSC speed range
  328. * @arg TSC_PAD_SPEED_0: Low speed
  329. * @arg TSC_PAD_SPEED_1: Middle speed
  330. * @arg TSC_PAD_SPEED_2: Middle speed
  331. * @arg TSC_PAD_SPEED_3: High speed
  332. * @retval None
  333. */
  334. #define __TSC_PAD_SPEED_CONFIG(__SPEED__) MODIFY_REG(TSC->ANA_SEL, TSC_ANA_SEL_SP_OPT_MASK,__SPEED__)
  335. /** @brief Check if the HW detect mode is enable
  336. * @param None
  337. * @retval Current state of HW detect mode
  338. */
  339. #define __TSC_GET_HW_MODE() (((TSC->CTRL) & TSC_FLAG_HW) == (TSC_FLAG_HW))
  340. /** @brief Check the detect type during HW detect mode
  341. * @param __FLAG__ specifies the flag of detect type
  342. * @arg TSC_FLAG_LESS_DET: Flag of less detect type
  343. * @arg TSC_FLAG_GREAT_DET: Flag of great detect type
  344. * @arg TSC_FLAG_PERIOD_DET: Flag of priod detect type
  345. * @retval Current state of flag
  346. */
  347. #define __TSC_GET_HW_DET_TYPE(__FLAG__) (((TSC->STS) & (__FLAG__))==(__FLAG__))
  348. /** @brief Get the number of channel which is detected now
  349. * @param None
  350. * @retval Current channel number
  351. */
  352. #define __TSC_GET_CHN_NUMBER() (((TSC->STS) & TSC_STS_CHN_NUM_MASK) >> TSC_STS_CHN_NUM_SHIFT )
  353. /** @brief Get the count value of pulse
  354. * @param None
  355. * @retval Pulse count of current channel
  356. */
  357. #define __TSC_GET_CHN_CNT() (((TSC->STS) & TSC_STS_CNT_VAL_MASK ) >> TSC_STS_CNT_VAL_SHIFT )
  358. /** @brief Get the base value of one channel
  359. * @param __NUM__ specifies channel number,must be less than MAX_TSC_HW_CHN
  360. * @retval base value of the channel
  361. */
  362. #define __TSC_GET_CHN_BASE(__NUM__) ((*((&(TSC->THRHD0))+(__NUM__)) & TSC_THRHD_BASE_MASK ) >> TSC_THRHD_BASE_SHIFT)
  363. /** @brief Get the delta value of one channel
  364. * @param __NUM__ specifies channel number,must be less than MAX_TSC_HW_CHN
  365. * @retval delta value of the channel
  366. */
  367. #define __TSC_GET_CHN_DELTA(__NUM__) ((*((&(TSC->THRHD0))+(__NUM__)) & TSC_THRHD_DELTA_MASK ) >> TSC_THRHD_DELTA_SHIFT )
  368. /** @brief Get the internal resist value of one channel
  369. * @param __NUM__ specifies channel number,must be less than MAX_TSC_HW_CHN
  370. * @retval resist value of the channel
  371. */
  372. #define __TSC_GET_CHN_RESIST(__NUM__) (((*((&(TSC->RESR0))+((__NUM__)>>3))) >>(((__NUM__) & 0x7UL)*4)) & TSC_RESR_CHN_RESIST_MASK)
  373. /* Private macros ------------------------------------------------------------*/
  374. /** @defgroup TSC_Private_Macros
  375. * @{
  376. */
  377. #define IS_TSC_DET_PERIOD(_PERIOD_) \
  378. (((_PERIOD_)==TSC_DET_PERIOD_8) ||((_PERIOD_)==TSC_DET_PERIOD_16)||((_PERIOD_)==TSC_DET_PERIOD_24) \
  379. ||((_PERIOD_)==TSC_DET_PERIOD_32)||((_PERIOD_)==TSC_DET_PERIOD_40)||((_PERIOD_)==TSC_DET_PERIOD_48) \
  380. ||((_PERIOD_)==TSC_DET_PERIOD_56)||((_PERIOD_)==TSC_DET_PERIOD_64)||((_PERIOD_)==TSC_DET_PERIOD_72) \
  381. ||((_PERIOD_)==TSC_DET_PERIOD_80)||((_PERIOD_)==TSC_DET_PERIOD_88)||((_PERIOD_)==TSC_DET_PERIOD_96) \
  382. ||((_PERIOD_)==TSC_DET_PERIOD_104) )
  383. #define IS_TSC_FILTER(_FILTER_) \
  384. ( ((_FILTER_)==TSC_DET_FILTER_1) ||((_FILTER_)==TSC_DET_FILTER_2)\
  385. ||((_FILTER_)==TSC_DET_FILTER_3) ||((_FILTER_)==TSC_DET_FILTER_4) )
  386. #define IS_TSC_DET_MODE(_MODE_) \
  387. ( ((_MODE_)==TSC_HW_DETECT_MODE) ||((_MODE_)==TSC_SW_DETECT_MODE) )
  388. #define IS_TSC_DET_TYPE(_TYPE_) \
  389. ( ((_TYPE_)==TSC_DET_TYPE_GREAT) ||((_TYPE_)==TSC_DET_TYPE_LESS) \
  390. ||((_TYPE_)==TSC_DET_TYPE_PERIOD)|| ((_TYPE_)==TSC_DET_TYPE_NONE) )
  391. #define IS_TSC_INT(_INT_) (((_INT_)==TSC_IT_DET_ENABLE)||((_INT_)==TSC_IT_DET_DISABLE))
  392. #define IS_TSC_OUT(_ETR_) (((_ETR_)==TSC_OUT_PIN)||((_ETR_)==TSC_OUT_TIM2_ETR)||((_ETR_)==TSC_OUT_TIM4_ETR))
  393. #define IS_TSC_CHN(_CHN_) (0==((_CHN_)&(~TSC_CHNEN_CHN_SEL_MASK)))
  394. #define IS_TSC_CHN_NUMBER(_NUM_) ((uint32_t)(_NUM_)<MAX_TSC_HW_CHN)
  395. #define IS_TSC_PAD_OPTION(_OPT_) (((_OPT_)==TSC_PAD_INTERNAL_RES)||((_OPT_)==TSC_PAD_EXTERNAL_RES))
  396. #define IS_TSC_PAD_SPEED(_SPEED_) \
  397. ( ((_SPEED_)==TSC_PAD_SPEED_0)||((_SPEED_)==TSC_PAD_SPEED_1) \
  398. ||((_SPEED_)==TSC_PAD_SPEED_2)||((_SPEED_)==TSC_PAD_SPEED_3) )
  399. #define IS_TSC_RESISTOR_VALUE(_RES_) \
  400. ( ((_RES_)==TSC_RESR_CHN_RESIST_1M) ||((_RES_)==TSC_RESR_CHN_RESIST_875K) \
  401. ||((_RES_)==TSC_RESR_CHN_RESIST_750K)||((_RES_)==TSC_RESR_CHN_RESIST_625K) \
  402. ||((_RES_)==TSC_RESR_CHN_RESIST_500K)||((_RES_)==TSC_RESR_CHN_RESIST_375K) \
  403. ||((_RES_)==TSC_RESR_CHN_RESIST_250K)||((_RES_)==TSC_RESR_CHN_RESIST_125K) )
  404. #define IS_TSC_THRESHOLD_BASE(_BASE_) ( (_BASE_)<=MAX_TSC_THRESHOLD_BASE)
  405. #define IS_TSC_THRESHOLD_DELTA(_DELTA_) ( (_DELTA_)<=MAX_TSC_THRESHOLD_DELTA)
  406. /**
  407. * @brief define tsc channel num
  408. */
  409. typedef enum
  410. {
  411. TSC_CHN0 = 0x00000001, ///< tsc channel 0
  412. TSC_CHN1 = 0x00000002, ///< tsc channel 1
  413. TSC_CHN2 = 0x00000004, ///< tsc channel 2
  414. TSC_CHN3 = 0x00000008, ///< tsc channel 3
  415. TSC_CHN4 = 0x00000010, ///< tsc channel 4
  416. TSC_CHN5 = 0x00000020, ///< tsc channel 5
  417. TSC_CHN6 = 0x00000040, ///< tsc channel 6
  418. TSC_CHN7 = 0x00000080, ///< tsc channel 7
  419. TSC_CHN8 = 0x00000100, ///< tsc channel 8
  420. TSC_CHN9 = 0x00000200, ///< tsc channel 9
  421. TSC_CHN10 = 0x00000400, ///< tsc channel 10
  422. TSC_CHN11 = 0x00000800, ///< tsc channel 11
  423. TSC_CHN12 = 0x00001000, ///< tsc channel 12
  424. TSC_CHN13 = 0x00002000, ///< tsc channel 13
  425. TSC_CHN14 = 0x00004000, ///< tsc channel 14
  426. TSC_CHN15 = 0x00008000, ///< tsc channel 15
  427. TSC_CHN16 = 0x00010000, ///< tsc channel 16
  428. TSC_CHN17 = 0x00020000, ///< tsc channel 17
  429. TSC_CHN18 = 0x00040000, ///< tsc channel 18
  430. TSC_CHN19 = 0x00080000, ///< tsc channel 19
  431. TSC_CHN20 = 0x00100000, ///< tsc channel 20
  432. TSC_CHN21 = 0x00200000, ///< tsc channel 21
  433. TSC_CHN22 = 0x00400000, ///< tsc channel 22
  434. TSC_CHN23 = 0x00800000 ///< tsc channel 23
  435. } TSC_Channel;
  436. /**
  437. * @brief define tsc status type for function TSC_GetStatus
  438. */
  439. #define TSC_GET_STS_CNTVALUE 0 ///< tsc count of hardware detect
  440. #define TSC_GET_STS_LESS_DET 1 ///< tsc less flag of hardware detect
  441. #define TSC_GET_STS_GREAT_DET 2 ///< tsc great flag of hardware detect
  442. #define TSC_GET_STS_CHN_NUM 3 ///< tsc chn number of hardware detect
  443. #define TSC_GET_STS_DET_ST 4 ///< tsc hw detect mode start status
  444. /**
  445. * @brief TSC Init structure definition
  446. */
  447. typedef struct
  448. {
  449. uint8_t TSC_DetIntEnable; /*!< Enalbe the TSC interrupt for hardware mode.
  450. This parameter can be ENABLE or DISABLE */
  451. uint8_t TSC_GreatEnable; /*!< Enable pulse count greater above threshold for single detection
  452. This parameter can be ENABLE or DISABLE */
  453. uint8_t TSC_LessEnable; /*!< Enable pulse conut less than threshold for single detection
  454. This parameter can be ENABLE or DISABLE */
  455. uint8_t TSC_FilterCount; /*!< Config how many detections for filter
  456. This parameter can be one value of @ref Detect_Filter */
  457. uint8_t TSC_DetPeriod; /*!< Config the detect time period for single detection
  458. This parameter can be one value of @ref Detect_Period */
  459. }TSC_InitType;
  460. typedef struct
  461. {
  462. uint16_t TSC_Base; /*!< base value */
  463. uint8_t TSC_Delta; /*!< offset value */
  464. uint8_t TSC_ResisValue; /*!< resistance value configuration*/
  465. } TSC_ChnCfg;
  466. /**
  467. * @brief Analog parameter configuration
  468. */
  469. typedef struct
  470. {
  471. uint8_t TSC_AnaoptrSpeedOption; // speed option
  472. uint8_t TSC_AnaoptrResisOption; // internal or external resistance option select
  473. } TSC_AnaoCfg;
  474. TSC_ErrorTypeDef TSC_Init(TSC_Module* TSC_Def, TSC_InitType* CtrlCfg);
  475. TSC_ErrorTypeDef TSC_ClockConfig(uint32_t TSC_ClkSource);
  476. TSC_ErrorTypeDef TSC_ConfigInternalResistor(TSC_Module* TSC_Def, uint32_t Channels, uint32_t res );
  477. TSC_ErrorTypeDef TSC_ConfigThreshold( TSC_Module* TSC_Def, uint32_t Channels, uint32_t base, uint32_t delta);
  478. TSC_ErrorTypeDef TSC_GetChannelCfg(TSC_Module* TSC_Def, TSC_ChnCfg* ChnCfg, uint32_t Channels);
  479. uint32_t TSC_GetStatus(TSC_Module* TSC_Def, uint32_t type);
  480. TSC_ErrorTypeDef TSC_Cmd(TSC_Module* TSC_Def, uint32_t Channels, FunctionalState Cmd);
  481. TSC_ErrorTypeDef TSC_SW_SwtichChn(TSC_Module* TSC_Def, uint32_t Channel, TIM_Module* TIMx, FunctionalState Cmd);
  482. TSC_ErrorTypeDef TSC_SetAnaoCfg(TSC_Module* TSC_Def, TSC_AnaoCfg* AnaoCfg);
  483. TSC_ErrorTypeDef TSC_SetChannelCfg(TSC_Module* TSC_Def, TSC_ChnCfg* ChnCfg, uint32_t Channels);
  484. #ifdef __cplusplus
  485. }
  486. #endif
  487. #endif /* __N32G45X_TSC_H__ */
  488. /**
  489. * @}
  490. */
  491. /**
  492. * @}
  493. */