stm32l4xx_hal_tsc.c 26 KB

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