stm32l4xx_hal_tsc.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /**
  2. ******************************************************************************
  3. * @file stm32l4xx_hal_tsc.c
  4. * @author MCD Application Team
  5. * @brief This file provides firmware functions to manage the following
  6. * functionalities of the Touch Sensing Controller (TSC) peripheral:
  7. * + Initialization and De-initialization
  8. * + Channel IOs, Shield IOs and Sampling IOs configuration
  9. * + Start and Stop an acquisition
  10. * + Read acquisition result
  11. * + Interrupts and flags management
  12. *
  13. @verbatim
  14. ================================================================================
  15. ##### TSC specific features #####
  16. ================================================================================
  17. [..]
  18. (#) Proven and robust surface charge transfer acquisition principle
  19. (#) Supports up to 3 capacitive sensing channels per group
  20. (#) Capacitive sensing channels can be acquired in parallel offering a very good
  21. response time
  22. (#) Spread spectrum feature to improve system robustness in noisy environments
  23. (#) Full hardware management of the charge transfer acquisition sequence
  24. (#) Programmable charge transfer frequency
  25. (#) Programmable sampling capacitor I/O pin
  26. (#) Programmable channel I/O pin
  27. (#) Programmable max count value to avoid long acquisition when a channel is faulty
  28. (#) Dedicated end of acquisition and max count error flags with interrupt capability
  29. (#) One sampling capacitor for up to 3 capacitive sensing channels to reduce the system
  30. components
  31. (#) Compatible with proximity, touchkey, linear and rotary touch sensor implementation
  32. ##### How to use this driver #####
  33. ================================================================================
  34. [..]
  35. (#) Enable the TSC interface clock using __HAL_RCC_TSC_CLK_ENABLE() macro.
  36. (#) GPIO pins configuration
  37. (++) Enable the clock for the TSC GPIOs using __HAL_RCC_GPIOx_CLK_ENABLE() macro.
  38. (++) Configure the TSC pins used as sampling IOs in alternate function output Open-Drain mode,
  39. and TSC pins used as channel/shield IOs in alternate function output Push-Pull mode
  40. using HAL_GPIO_Init() function.
  41. (#) Interrupts configuration
  42. (++) Configure the NVIC (if the interrupt model is used) using HAL_NVIC_SetPriority()
  43. and HAL_NVIC_EnableIRQ() and function.
  44. (#) TSC configuration
  45. (++) Configure all TSC parameters and used TSC IOs using HAL_TSC_Init() function.
  46. [..] TSC peripheral alternate functions are mapped on AF9.
  47. *** Acquisition sequence ***
  48. ===================================
  49. [..]
  50. (+) Discharge all IOs using HAL_TSC_IODischarge() function.
  51. (+) Wait a certain time allowing a good discharge of all capacitors. This delay depends
  52. of the sampling capacitor and electrodes design.
  53. (+) Select the channel IOs to be acquired using HAL_TSC_IOConfig() function.
  54. (+) Launch the acquisition using either HAL_TSC_Start() or HAL_TSC_Start_IT() function.
  55. If the synchronized mode is selected, the acquisition will start as soon as the signal
  56. is received on the synchro pin.
  57. (+) Wait the end of acquisition using either HAL_TSC_PollForAcquisition() or
  58. HAL_TSC_GetState() function or using WFI instruction for example.
  59. (+) Check the group acquisition status using HAL_TSC_GroupGetStatus() function.
  60. (+) Read the acquisition value using HAL_TSC_GroupGetValue() function.
  61. @endverbatim
  62. ******************************************************************************
  63. Table 1. IOs for the STM32L4xx devices
  64. +--------------------------------+
  65. | IOs | TSC functions |
  66. |--------------|-----------------|
  67. | PB12 (AF) | TSC_G1_IO1 |
  68. | PB13 (AF) | TSC_G1_IO2 |
  69. | PB14 (AF) | TSC_G1_IO3 |
  70. | PB15 (AF) | TSC_G1_IO4 |
  71. |--------------|-----------------|
  72. | PB4 (AF) | TSC_G2_IO1 |
  73. | PB5 (AF) | TSC_G2_IO2 |
  74. | PB6 (AF) | TSC_G2_IO3 |
  75. | PB7 (AF) | TSC_G2_IO4 |
  76. |--------------|-----------------|
  77. | PA15 (AF) | TSC_G3_IO1 |
  78. | PC10 (AF) | TSC_G3_IO2 |
  79. | PC11 (AF) | TSC_G3_IO3 |
  80. | PC12 (AF) | TSC_G3_IO4 |
  81. |--------------|-----------------|
  82. | PC6 (AF) | TSC_G4_IO1 |
  83. | PC7 (AF) | TSC_G4_IO2 |
  84. | PC8 (AF) | TSC_G4_IO3 |
  85. | PC9 (AF) | TSC_G4_IO4 |
  86. |--------------|-----------------|
  87. | PE10 (AF) | TSC_G5_IO1 |
  88. | PE11 (AF) | TSC_G5_IO2 |
  89. | PE12 (AF) | TSC_G5_IO3 |
  90. | PE13 (AF) | TSC_G5_IO4 |
  91. |--------------|-----------------|
  92. | PD10 (AF) | TSC_G6_IO1 |
  93. | PD11 (AF) | TSC_G6_IO2 |
  94. | PD12 (AF) | TSC_G6_IO3 |
  95. | PD13 (AF) | TSC_G6_IO4 |
  96. |--------------|-----------------|
  97. | PE2 (AF) | TSC_G7_IO1 |
  98. | PE3 (AF) | TSC_G7_IO2 |
  99. | PE4 (AF) | TSC_G7_IO3 |
  100. | PE5 (AF) | TSC_G7_IO4 |
  101. |--------------|-----------------|
  102. | PF14 (AF) | TSC_G8_IO1 |
  103. | PF15 (AF) | TSC_G8_IO2 |
  104. | PG0 (AF) | TSC_G8_IO3 |
  105. | PG1 (AF) | TSC_G8_IO4 |
  106. |--------------|-----------------|
  107. | PB10 (AF) | TSC_SYNC |
  108. | PD2 (AF) | |
  109. +--------------------------------+
  110. ******************************************************************************
  111. * @attention
  112. *
  113. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  114. *
  115. * Redistribution and use in source and binary forms, with or without modification,
  116. * are permitted provided that the following conditions are met:
  117. * 1. Redistributions of source code must retain the above copyright notice,
  118. * this list of conditions and the following disclaimer.
  119. * 2. Redistributions in binary form must reproduce the above copyright notice,
  120. * this list of conditions and the following disclaimer in the documentation
  121. * and/or other materials provided with the distribution.
  122. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  123. * may be used to endorse or promote products derived from this software
  124. * without specific prior written permission.
  125. *
  126. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  127. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  128. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  129. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  130. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  131. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  132. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  133. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  134. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  135. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  136. *
  137. ******************************************************************************
  138. */
  139. /* Includes ------------------------------------------------------------------*/
  140. #include "stm32l4xx_hal.h"
  141. /** @addtogroup STM32L4xx_HAL_Driver
  142. * @{
  143. */
  144. /** @defgroup TSC TSC
  145. * @brief HAL TSC module driver
  146. * @{
  147. */
  148. #ifdef HAL_TSC_MODULE_ENABLED
  149. /* Private typedef -----------------------------------------------------------*/
  150. /* Private define ------------------------------------------------------------*/
  151. /* Private macro -------------------------------------------------------------*/
  152. /* Private variables ---------------------------------------------------------*/
  153. /* Private function prototypes -----------------------------------------------*/
  154. static uint32_t TSC_extract_groups(uint32_t iomask);
  155. /* Exported functions --------------------------------------------------------*/
  156. /** @defgroup TSC_Exported_Functions Exported Functions
  157. * @{
  158. */
  159. /** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions
  160. * @brief Initialization and Configuration functions
  161. *
  162. @verbatim
  163. ===============================================================================
  164. ##### Initialization and de-initialization functions #####
  165. ===============================================================================
  166. [..] This section provides functions allowing to:
  167. (+) Initialize and configure the TSC.
  168. (+) De-initialize the TSC.
  169. @endverbatim
  170. * @{
  171. */
  172. /**
  173. * @brief Initialize the TSC peripheral according to the specified parameters
  174. * in the TSC_InitTypeDef structure and initialize the associated handle.
  175. * @param htsc: TSC handle
  176. * @retval HAL status
  177. */
  178. HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef* htsc)
  179. {
  180. /* Check TSC handle allocation */
  181. if (htsc == NULL)
  182. {
  183. return HAL_ERROR;
  184. }
  185. /* Check the parameters */
  186. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  187. assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
  188. assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
  189. assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
  190. assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
  191. assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
  192. assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
  193. assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
  194. assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
  195. assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
  196. assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
  197. assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
  198. if(htsc->State == HAL_TSC_STATE_RESET)
  199. {
  200. /* Allocate lock resource and initialize it */
  201. htsc->Lock = HAL_UNLOCKED;
  202. }
  203. /* Initialize the TSC state */
  204. htsc->State = HAL_TSC_STATE_BUSY;
  205. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  206. HAL_TSC_MspInit(htsc);
  207. /*--------------------------------------------------------------------------*/
  208. /* Set TSC parameters */
  209. /* Enable TSC */
  210. htsc->Instance->CR = TSC_CR_TSCE;
  211. /* Set all functions */
  212. htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
  213. htsc->Init.CTPulseLowLength |
  214. (uint32_t)(htsc->Init.SpreadSpectrumDeviation << 17) |
  215. htsc->Init.SpreadSpectrumPrescaler |
  216. htsc->Init.PulseGeneratorPrescaler |
  217. htsc->Init.MaxCountValue |
  218. htsc->Init.SynchroPinPolarity |
  219. htsc->Init.AcquisitionMode);
  220. /* Spread spectrum */
  221. if (htsc->Init.SpreadSpectrum == ENABLE)
  222. {
  223. htsc->Instance->CR |= TSC_CR_SSE;
  224. }
  225. /* Disable Schmitt trigger hysteresis on all used TSC IOs */
  226. htsc->Instance->IOHCR = (uint32_t)(~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
  227. /* Set channel and shield IOs */
  228. htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
  229. /* Set sampling IOs */
  230. htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
  231. /* Set the groups to be acquired */
  232. htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
  233. /* Disable interrupts */
  234. htsc->Instance->IER &= (uint32_t)(~(TSC_IT_EOA | TSC_IT_MCE));
  235. /* Clear flags */
  236. htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
  237. /*--------------------------------------------------------------------------*/
  238. /* Initialize the TSC state */
  239. htsc->State = HAL_TSC_STATE_READY;
  240. /* Return function status */
  241. return HAL_OK;
  242. }
  243. /**
  244. * @brief Deinitialize the TSC peripheral registers to their default reset values.
  245. * @param htsc: TSC handle
  246. * @retval HAL status
  247. */
  248. HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef* htsc)
  249. {
  250. /* Check TSC handle allocation */
  251. if (htsc == NULL)
  252. {
  253. return HAL_ERROR;
  254. }
  255. /* Check the parameters */
  256. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  257. /* Change TSC state */
  258. htsc->State = HAL_TSC_STATE_BUSY;
  259. /* DeInit the low level hardware */
  260. HAL_TSC_MspDeInit(htsc);
  261. /* Change TSC state */
  262. htsc->State = HAL_TSC_STATE_RESET;
  263. /* Process unlocked */
  264. __HAL_UNLOCK(htsc);
  265. /* Return function status */
  266. return HAL_OK;
  267. }
  268. /**
  269. * @brief Initialize the TSC MSP.
  270. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  271. * the configuration information for the specified TSC.
  272. * @retval None
  273. */
  274. __weak void HAL_TSC_MspInit(TSC_HandleTypeDef* htsc)
  275. {
  276. /* Prevent unused argument(s) compilation warning */
  277. UNUSED(htsc);
  278. /* NOTE : This function should not be modified, when the callback is needed,
  279. the HAL_TSC_MspInit could be implemented in the user file.
  280. */
  281. }
  282. /**
  283. * @brief DeInitialize the TSC MSP.
  284. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  285. * the configuration information for the specified TSC.
  286. * @retval None
  287. */
  288. __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef* htsc)
  289. {
  290. /* Prevent unused argument(s) compilation warning */
  291. UNUSED(htsc);
  292. /* NOTE : This function should not be modified, when the callback is needed,
  293. the HAL_TSC_MspDeInit could be implemented in the user file.
  294. */
  295. }
  296. /**
  297. * @}
  298. */
  299. /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
  300. * @brief Input and Output operation functions
  301. *
  302. @verbatim
  303. ===============================================================================
  304. ##### IO Operation functions #####
  305. ===============================================================================
  306. [..] This section provides functions allowing to:
  307. (+) Start acquisition in polling mode.
  308. (+) Start acquisition in interrupt mode.
  309. (+) Stop conversion in polling mode.
  310. (+) Stop conversion in interrupt mode.
  311. (+) Poll for acquisition completed.
  312. (+) Get group acquisition status.
  313. (+) Get group acquisition value.
  314. @endverbatim
  315. * @{
  316. */
  317. /**
  318. * @brief Start the acquisition.
  319. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  320. * the configuration information for the specified TSC.
  321. * @retval HAL status
  322. */
  323. HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef* htsc)
  324. {
  325. /* Check the parameters */
  326. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  327. /* Process locked */
  328. __HAL_LOCK(htsc);
  329. /* Change TSC state */
  330. htsc->State = HAL_TSC_STATE_BUSY;
  331. /* Clear interrupts */
  332. __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
  333. /* Clear flags */
  334. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  335. /* Set touch sensing IOs not acquired to the specified IODefaultMode */
  336. if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
  337. {
  338. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  339. }
  340. else
  341. {
  342. __HAL_TSC_SET_IODEF_INFLOAT(htsc);
  343. }
  344. /* Launch the acquisition */
  345. __HAL_TSC_START_ACQ(htsc);
  346. /* Process unlocked */
  347. __HAL_UNLOCK(htsc);
  348. /* Return function status */
  349. return HAL_OK;
  350. }
  351. /**
  352. * @brief Start the acquisition in interrupt mode.
  353. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  354. * the configuration information for the specified TSC.
  355. * @retval HAL status.
  356. */
  357. HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef* htsc)
  358. {
  359. /* Check the parameters */
  360. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  361. assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
  362. /* Process locked */
  363. __HAL_LOCK(htsc);
  364. /* Change TSC state */
  365. htsc->State = HAL_TSC_STATE_BUSY;
  366. /* Enable end of acquisition interrupt */
  367. __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
  368. /* Enable max count error interrupt (optional) */
  369. if (htsc->Init.MaxCountInterrupt == ENABLE)
  370. {
  371. __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
  372. }
  373. else
  374. {
  375. __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
  376. }
  377. /* Clear flags */
  378. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  379. /* Set touch sensing IOs not acquired to the specified IODefaultMode */
  380. if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
  381. {
  382. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  383. }
  384. else
  385. {
  386. __HAL_TSC_SET_IODEF_INFLOAT(htsc);
  387. }
  388. /* Launch the acquisition */
  389. __HAL_TSC_START_ACQ(htsc);
  390. /* Process unlocked */
  391. __HAL_UNLOCK(htsc);
  392. /* Return function status */
  393. return HAL_OK;
  394. }
  395. /**
  396. * @brief Stop the acquisition previously launched in polling mode.
  397. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  398. * the configuration information for the specified TSC.
  399. * @retval HAL status
  400. */
  401. HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef* htsc)
  402. {
  403. /* Check the parameters */
  404. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  405. /* Process locked */
  406. __HAL_LOCK(htsc);
  407. /* Stop the acquisition */
  408. __HAL_TSC_STOP_ACQ(htsc);
  409. /* Set touch sensing IOs in low power mode (output push-pull) */
  410. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  411. /* Clear flags */
  412. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  413. /* Change TSC state */
  414. htsc->State = HAL_TSC_STATE_READY;
  415. /* Process unlocked */
  416. __HAL_UNLOCK(htsc);
  417. /* Return function status */
  418. return HAL_OK;
  419. }
  420. /**
  421. * @brief Stop the acquisition previously launched in interrupt mode.
  422. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  423. * the configuration information for the specified TSC.
  424. * @retval HAL status
  425. */
  426. HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef* htsc)
  427. {
  428. /* Check the parameters */
  429. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  430. /* Process locked */
  431. __HAL_LOCK(htsc);
  432. /* Stop the acquisition */
  433. __HAL_TSC_STOP_ACQ(htsc);
  434. /* Set touch sensing IOs in low power mode (output push-pull) */
  435. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  436. /* Disable interrupts */
  437. __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
  438. /* Clear flags */
  439. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  440. /* Change TSC state */
  441. htsc->State = HAL_TSC_STATE_READY;
  442. /* Process unlocked */
  443. __HAL_UNLOCK(htsc);
  444. /* Return function status */
  445. return HAL_OK;
  446. }
  447. /**
  448. * @brief Start acquisition and wait until completion.
  449. * @note There is no need of a timeout parameter as the max count error is already
  450. * managed by the TSC peripheral.
  451. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  452. * the configuration information for the specified TSC.
  453. * @retval HAL state
  454. */
  455. HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef* htsc)
  456. {
  457. /* Check the parameters */
  458. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  459. /* Process locked */
  460. __HAL_LOCK(htsc);
  461. /* Check end of acquisition */
  462. while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
  463. {
  464. /* The timeout (max count error) is managed by the TSC peripheral itself. */
  465. }
  466. /* Process unlocked */
  467. __HAL_UNLOCK(htsc);
  468. return HAL_OK;
  469. }
  470. /**
  471. * @brief Get the acquisition status for a group.
  472. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  473. * the configuration information for the specified TSC.
  474. * @param gx_index: Index of the group
  475. * @retval Group status
  476. */
  477. TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef* htsc, uint32_t gx_index)
  478. {
  479. /* Check the parameters */
  480. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  481. assert_param(IS_TSC_GROUP_INDEX(gx_index));
  482. /* Return the group status */
  483. return(__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
  484. }
  485. /**
  486. * @brief Get the acquisition measure for a group.
  487. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  488. * the configuration information for the specified TSC.
  489. * @param gx_index: Index of the group
  490. * @retval Acquisition measure
  491. */
  492. uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef* htsc, uint32_t gx_index)
  493. {
  494. /* Check the parameters */
  495. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  496. assert_param(IS_TSC_GROUP_INDEX(gx_index));
  497. /* Return the group acquisition counter */
  498. return htsc->Instance->IOGXCR[gx_index];
  499. }
  500. /**
  501. * @}
  502. */
  503. /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
  504. * @brief Peripheral Control functions
  505. *
  506. @verbatim
  507. ===============================================================================
  508. ##### Peripheral Control functions #####
  509. ===============================================================================
  510. [..] This section provides functions allowing to:
  511. (+) Configure TSC IOs
  512. (+) Discharge TSC IOs
  513. @endverbatim
  514. * @{
  515. */
  516. /**
  517. * @brief Configure TSC IOs.
  518. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  519. * the configuration information for the specified TSC.
  520. * @param config: pointer to the configuration structure.
  521. * @retval HAL status
  522. */
  523. HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef* htsc, TSC_IOConfigTypeDef* config)
  524. {
  525. /* Check the parameters */
  526. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  527. /* Process locked */
  528. __HAL_LOCK(htsc);
  529. /* Stop acquisition */
  530. __HAL_TSC_STOP_ACQ(htsc);
  531. /* Disable Schmitt trigger hysteresis on all used TSC IOs */
  532. htsc->Instance->IOHCR = (uint32_t)(~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
  533. /* Set channel and shield IOs */
  534. htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
  535. /* Set sampling IOs */
  536. htsc->Instance->IOSCR = config->SamplingIOs;
  537. /* Set groups to be acquired */
  538. htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
  539. /* Process unlocked */
  540. __HAL_UNLOCK(htsc);
  541. /* Return function status */
  542. return HAL_OK;
  543. }
  544. /**
  545. * @brief Discharge TSC IOs.
  546. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  547. * the configuration information for the specified TSC.
  548. * @param choice: enable or disable
  549. * @retval HAL status
  550. */
  551. HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef* htsc, uint32_t choice)
  552. {
  553. /* Check the parameters */
  554. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  555. /* Process locked */
  556. __HAL_LOCK(htsc);
  557. if (choice == ENABLE)
  558. {
  559. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  560. }
  561. else
  562. {
  563. __HAL_TSC_SET_IODEF_INFLOAT(htsc);
  564. }
  565. /* Process unlocked */
  566. __HAL_UNLOCK(htsc);
  567. /* Return the group acquisition counter */
  568. return HAL_OK;
  569. }
  570. /**
  571. * @}
  572. */
  573. /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
  574. * @brief Peripheral State and Errors functions
  575. *
  576. @verbatim
  577. ===============================================================================
  578. ##### State and Errors functions #####
  579. ===============================================================================
  580. [..]
  581. This subsection provides functions allowing to
  582. (+) Get TSC state.
  583. @endverbatim
  584. * @{
  585. */
  586. /**
  587. * @brief Return the TSC handle state.
  588. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  589. * the configuration information for the specified TSC.
  590. * @retval HAL state
  591. */
  592. HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef* htsc)
  593. {
  594. /* Check the parameters */
  595. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  596. if (htsc->State == HAL_TSC_STATE_BUSY)
  597. {
  598. /* Check end of acquisition flag */
  599. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
  600. {
  601. /* Check max count error flag */
  602. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
  603. {
  604. /* Change TSC state */
  605. htsc->State = HAL_TSC_STATE_ERROR;
  606. }
  607. else
  608. {
  609. /* Change TSC state */
  610. htsc->State = HAL_TSC_STATE_READY;
  611. }
  612. }
  613. }
  614. /* Return TSC state */
  615. return htsc->State;
  616. }
  617. /**
  618. * @}
  619. */
  620. /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  621. * @{
  622. */
  623. /**
  624. * @brief Handle TSC interrupt request.
  625. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  626. * the configuration information for the specified TSC.
  627. * @retval None
  628. */
  629. void HAL_TSC_IRQHandler(TSC_HandleTypeDef* htsc)
  630. {
  631. /* Check the parameters */
  632. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  633. /* Check if the end of acquisition occurred */
  634. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
  635. {
  636. /* Clear EOA flag */
  637. __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
  638. }
  639. /* Check if max count error occurred */
  640. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
  641. {
  642. /* Clear MCE flag */
  643. __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
  644. /* Change TSC state */
  645. htsc->State = HAL_TSC_STATE_ERROR;
  646. /* Conversion completed callback */
  647. HAL_TSC_ErrorCallback(htsc);
  648. }
  649. else
  650. {
  651. /* Change TSC state */
  652. htsc->State = HAL_TSC_STATE_READY;
  653. /* Conversion completed callback */
  654. HAL_TSC_ConvCpltCallback(htsc);
  655. }
  656. }
  657. /**
  658. * @brief Acquisition completed callback in non-blocking mode.
  659. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  660. * the configuration information for the specified TSC.
  661. * @retval None
  662. */
  663. __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef* htsc)
  664. {
  665. /* Prevent unused argument(s) compilation warning */
  666. UNUSED(htsc);
  667. /* NOTE : This function should not be modified, when the callback is needed,
  668. the HAL_TSC_ConvCpltCallback could be implemented in the user file.
  669. */
  670. }
  671. /**
  672. * @brief Error callback in non-blocking mode.
  673. * @param htsc: pointer to a TSC_HandleTypeDef structure that contains
  674. * the configuration information for the specified TSC.
  675. * @retval None
  676. */
  677. __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef* htsc)
  678. {
  679. /* Prevent unused argument(s) compilation warning */
  680. UNUSED(htsc);
  681. /* NOTE : This function should not be modified, when the callback is needed,
  682. the HAL_TSC_ErrorCallback could be implemented in the user file.
  683. */
  684. }
  685. /**
  686. * @}
  687. */
  688. /**
  689. * @}
  690. */
  691. /* Private functions ---------------------------------------------------------*/
  692. /** @defgroup TSC_Private_Functions Private Functions
  693. * @{
  694. */
  695. /**
  696. * @brief Utility function used to set the acquired groups mask.
  697. * @param iomask: Channels IOs mask
  698. * @retval Acquired groups mask
  699. */
  700. static uint32_t TSC_extract_groups(uint32_t iomask)
  701. {
  702. uint32_t groups = 0;
  703. uint32_t idx;
  704. for (idx = 0; idx < TSC_NB_OF_GROUPS; idx++)
  705. {
  706. if ((iomask & ((uint32_t)0x0F << (idx * 4))) != RESET)
  707. {
  708. groups |= ((uint32_t)1 << idx);
  709. }
  710. }
  711. return groups;
  712. }
  713. /**
  714. * @}
  715. */
  716. #endif /* HAL_TSC_MODULE_ENABLED */
  717. /**
  718. * @}
  719. */
  720. /**
  721. * @}
  722. */
  723. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/