You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

825 lines
25 KiB

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