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.
 
 
 

1071 lines
32 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32l0xx_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 AF3.
  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. *** Callback registration ***
  62. =============================================
  63. The compilation flag USE_HAL_TSC_REGISTER_CALLBACKS when set to 1
  64. allows the user to configure dynamically the driver callbacks.
  65. Use Functions @ref HAL_TSC_RegisterCallback() to register an interrupt callback.
  66. Function @ref HAL_TSC_RegisterCallback() allows to register following callbacks:
  67. (+) ConvCpltCallback : callback for conversion complete process.
  68. (+) ErrorCallback : callback for error detection.
  69. (+) MspInitCallback : callback for Msp Init.
  70. (+) MspDeInitCallback : callback for Msp DeInit.
  71. This function takes as parameters the HAL peripheral handle, the Callback ID
  72. and a pointer to the user callback function.
  73. Use function @ref HAL_TSC_UnRegisterCallback to reset a callback to the default
  74. weak function.
  75. @ref HAL_TSC_UnRegisterCallback takes as parameters the HAL peripheral handle,
  76. and the Callback ID.
  77. This function allows to reset following callbacks:
  78. (+) ConvCpltCallback : callback for conversion complete process.
  79. (+) ErrorCallback : callback for error detection.
  80. (+) MspInitCallback : callback for Msp Init.
  81. (+) MspDeInitCallback : callback for Msp DeInit.
  82. By default, after the @ref HAL_TSC_Init() and when the state is @ref HAL_TSC_STATE_RESET
  83. all callbacks are set to the corresponding weak functions:
  84. examples @ref HAL_TSC_ConvCpltCallback(), @ref HAL_TSC_ErrorCallback().
  85. Exception done for MspInit and MspDeInit functions that are
  86. reset to the legacy weak functions in the @ref HAL_TSC_Init()/ @ref HAL_TSC_DeInit() only when
  87. these callbacks are null (not registered beforehand).
  88. If MspInit or MspDeInit are not null, the @ref HAL_TSC_Init()/ @ref HAL_TSC_DeInit()
  89. keep and use the user MspInit/MspDeInit callbacks (registered beforehand) whatever the state.
  90. Callbacks can be registered/unregistered in @ref HAL_TSC_STATE_READY state only.
  91. Exception done MspInit/MspDeInit functions that can be registered/unregistered
  92. in @ref HAL_TSC_STATE_READY or @ref HAL_TSC_STATE_RESET state,
  93. thus registered (user) MspInit/DeInit callbacks can be used during the Init/DeInit.
  94. Then, the user first registers the MspInit/MspDeInit user callbacks
  95. using @ref HAL_TSC_RegisterCallback() before calling @ref HAL_TSC_DeInit()
  96. or @ref HAL_TSC_Init() function.
  97. When the compilation flag USE_HAL_TSC_REGISTER_CALLBACKS is set to 0 or
  98. not defined, the callback registration feature is not available and all callbacks
  99. are set to the corresponding weak functions.
  100. @endverbatim
  101. ******************************************************************************
  102. ******************************************************************************
  103. * @attention
  104. *
  105. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  106. * All rights reserved.</center></h2>
  107. *
  108. * This software component is licensed by ST under BSD 3-Clause license,
  109. * the "License"; You may not use this file except in compliance with the
  110. * License. You may obtain a copy of the License at:
  111. * opensource.org/licenses/BSD-3-Clause
  112. *
  113. ******************************************************************************
  114. */
  115. /* Includes ------------------------------------------------------------------*/
  116. #include "stm32l0xx_hal.h"
  117. #if defined(TSC)
  118. /** @addtogroup STM32L0xx_HAL_Driver
  119. * @{
  120. */
  121. /** @defgroup TSC TSC
  122. * @brief HAL TSC module driver
  123. * @{
  124. */
  125. #ifdef HAL_TSC_MODULE_ENABLED
  126. /* Private typedef -----------------------------------------------------------*/
  127. /* Private define ------------------------------------------------------------*/
  128. /* Private macro -------------------------------------------------------------*/
  129. /* Private variables ---------------------------------------------------------*/
  130. /* Private function prototypes -----------------------------------------------*/
  131. static uint32_t TSC_extract_groups(uint32_t iomask);
  132. /* Exported functions --------------------------------------------------------*/
  133. /** @defgroup TSC_Exported_Functions Exported Functions
  134. * @{
  135. */
  136. /** @defgroup TSC_Exported_Functions_Group1 Initialization and de-initialization functions
  137. * @brief Initialization and Configuration functions
  138. *
  139. @verbatim
  140. ===============================================================================
  141. ##### Initialization and de-initialization functions #####
  142. ===============================================================================
  143. [..] This section provides functions allowing to:
  144. (+) Initialize and configure the TSC.
  145. (+) De-initialize the TSC.
  146. @endverbatim
  147. * @{
  148. */
  149. /**
  150. * @brief Initialize the TSC peripheral according to the specified parameters
  151. * in the TSC_InitTypeDef structure and initialize the associated handle.
  152. * @param htsc TSC handle
  153. * @retval HAL status
  154. */
  155. HAL_StatusTypeDef HAL_TSC_Init(TSC_HandleTypeDef *htsc)
  156. {
  157. /* Check TSC handle allocation */
  158. if (htsc == NULL)
  159. {
  160. return HAL_ERROR;
  161. }
  162. /* Check the parameters */
  163. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  164. assert_param(IS_TSC_CTPH(htsc->Init.CTPulseHighLength));
  165. assert_param(IS_TSC_CTPL(htsc->Init.CTPulseLowLength));
  166. assert_param(IS_TSC_SS(htsc->Init.SpreadSpectrum));
  167. assert_param(IS_TSC_SSD(htsc->Init.SpreadSpectrumDeviation));
  168. assert_param(IS_TSC_SS_PRESC(htsc->Init.SpreadSpectrumPrescaler));
  169. assert_param(IS_TSC_PG_PRESC(htsc->Init.PulseGeneratorPrescaler));
  170. assert_param(IS_TSC_MCV(htsc->Init.MaxCountValue));
  171. assert_param(IS_TSC_IODEF(htsc->Init.IODefaultMode));
  172. assert_param(IS_TSC_SYNC_POL(htsc->Init.SynchroPinPolarity));
  173. assert_param(IS_TSC_ACQ_MODE(htsc->Init.AcquisitionMode));
  174. assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
  175. assert_param(IS_TSC_GROUP(htsc->Init.ChannelIOs));
  176. assert_param(IS_TSC_GROUP(htsc->Init.ShieldIOs));
  177. assert_param(IS_TSC_GROUP(htsc->Init.SamplingIOs));
  178. if (htsc->State == HAL_TSC_STATE_RESET)
  179. {
  180. /* Allocate lock resource and initialize it */
  181. htsc->Lock = HAL_UNLOCKED;
  182. #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
  183. /* Init the TSC Callback settings */
  184. htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback */
  185. htsc->ErrorCallback = HAL_TSC_ErrorCallback; /* Legacy weak ErrorCallback */
  186. if (htsc->MspInitCallback == NULL)
  187. {
  188. htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit */
  189. }
  190. /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
  191. htsc->MspInitCallback(htsc);
  192. #else
  193. /* Init the low level hardware : GPIO, CLOCK, CORTEX */
  194. HAL_TSC_MspInit(htsc);
  195. #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
  196. }
  197. /* Initialize the TSC state */
  198. htsc->State = HAL_TSC_STATE_BUSY;
  199. /*--------------------------------------------------------------------------*/
  200. /* Set TSC parameters */
  201. /* Enable TSC */
  202. htsc->Instance->CR = TSC_CR_TSCE;
  203. /* Set all functions */
  204. htsc->Instance->CR |= (htsc->Init.CTPulseHighLength |
  205. htsc->Init.CTPulseLowLength |
  206. (uint32_t)(htsc->Init.SpreadSpectrumDeviation << TSC_CR_SSD_Pos) |
  207. htsc->Init.SpreadSpectrumPrescaler |
  208. htsc->Init.PulseGeneratorPrescaler |
  209. htsc->Init.MaxCountValue |
  210. htsc->Init.SynchroPinPolarity |
  211. htsc->Init.AcquisitionMode);
  212. /* Spread spectrum */
  213. if ((FunctionalState)htsc->Init.SpreadSpectrum == ENABLE)
  214. {
  215. htsc->Instance->CR |= TSC_CR_SSE;
  216. }
  217. /* Disable Schmitt trigger hysteresis on all used TSC IOs */
  218. htsc->Instance->IOHCR = (uint32_t)(~(htsc->Init.ChannelIOs | htsc->Init.ShieldIOs | htsc->Init.SamplingIOs));
  219. /* Set channel and shield IOs */
  220. htsc->Instance->IOCCR = (htsc->Init.ChannelIOs | htsc->Init.ShieldIOs);
  221. /* Set sampling IOs */
  222. htsc->Instance->IOSCR = htsc->Init.SamplingIOs;
  223. /* Set the groups to be acquired */
  224. htsc->Instance->IOGCSR = TSC_extract_groups(htsc->Init.ChannelIOs);
  225. /* Disable interrupts */
  226. htsc->Instance->IER &= (uint32_t)(~(TSC_IT_EOA | TSC_IT_MCE));
  227. /* Clear flags */
  228. htsc->Instance->ICR = (TSC_FLAG_EOA | TSC_FLAG_MCE);
  229. /*--------------------------------------------------------------------------*/
  230. /* Initialize the TSC state */
  231. htsc->State = HAL_TSC_STATE_READY;
  232. /* Return function status */
  233. return HAL_OK;
  234. }
  235. /**
  236. * @brief Deinitialize the TSC peripheral registers to their default reset values.
  237. * @param htsc TSC handle
  238. * @retval HAL status
  239. */
  240. HAL_StatusTypeDef HAL_TSC_DeInit(TSC_HandleTypeDef *htsc)
  241. {
  242. /* Check TSC handle allocation */
  243. if (htsc == NULL)
  244. {
  245. return HAL_ERROR;
  246. }
  247. /* Check the parameters */
  248. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  249. /* Change TSC state */
  250. htsc->State = HAL_TSC_STATE_BUSY;
  251. #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
  252. if (htsc->MspDeInitCallback == NULL)
  253. {
  254. htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit */
  255. }
  256. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  257. htsc->MspDeInitCallback(htsc);
  258. #else
  259. /* DeInit the low level hardware */
  260. HAL_TSC_MspDeInit(htsc);
  261. #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
  262. /* Change TSC state */
  263. htsc->State = HAL_TSC_STATE_RESET;
  264. /* Process unlocked */
  265. __HAL_UNLOCK(htsc);
  266. /* Return function status */
  267. return HAL_OK;
  268. }
  269. /**
  270. * @brief Initialize the TSC MSP.
  271. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  272. * the configuration information for the specified TSC.
  273. * @retval None
  274. */
  275. __weak void HAL_TSC_MspInit(TSC_HandleTypeDef *htsc)
  276. {
  277. /* Prevent unused argument(s) compilation warning */
  278. UNUSED(htsc);
  279. /* NOTE : This function should not be modified, when the callback is needed,
  280. the HAL_TSC_MspInit could be implemented in the user file.
  281. */
  282. }
  283. /**
  284. * @brief DeInitialize the TSC MSP.
  285. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  286. * the configuration information for the specified TSC.
  287. * @retval None
  288. */
  289. __weak void HAL_TSC_MspDeInit(TSC_HandleTypeDef *htsc)
  290. {
  291. /* Prevent unused argument(s) compilation warning */
  292. UNUSED(htsc);
  293. /* NOTE : This function should not be modified, when the callback is needed,
  294. the HAL_TSC_MspDeInit could be implemented in the user file.
  295. */
  296. }
  297. #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
  298. /**
  299. * @brief Register a User TSC Callback
  300. * To be used instead of the weak predefined callback
  301. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  302. * the configuration information for the specified TSC.
  303. * @param CallbackID ID of the callback to be registered
  304. * This parameter can be one of the following values:
  305. * @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
  306. * @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
  307. * @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
  308. * @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
  309. * @param pCallback pointer to the Callback function
  310. * @retval HAL status
  311. */
  312. HAL_StatusTypeDef HAL_TSC_RegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID, pTSC_CallbackTypeDef pCallback)
  313. {
  314. HAL_StatusTypeDef status = HAL_OK;
  315. if (pCallback == NULL)
  316. {
  317. /* Update the error code */
  318. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  319. return HAL_ERROR;
  320. }
  321. /* Process locked */
  322. __HAL_LOCK(htsc);
  323. if (HAL_TSC_STATE_READY == htsc->State)
  324. {
  325. switch (CallbackID)
  326. {
  327. case HAL_TSC_CONV_COMPLETE_CB_ID :
  328. htsc->ConvCpltCallback = pCallback;
  329. break;
  330. case HAL_TSC_ERROR_CB_ID :
  331. htsc->ErrorCallback = pCallback;
  332. break;
  333. case HAL_TSC_MSPINIT_CB_ID :
  334. htsc->MspInitCallback = pCallback;
  335. break;
  336. case HAL_TSC_MSPDEINIT_CB_ID :
  337. htsc->MspDeInitCallback = pCallback;
  338. break;
  339. default :
  340. /* Update the error code */
  341. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  342. /* Return error status */
  343. status = HAL_ERROR;
  344. break;
  345. }
  346. }
  347. else if (HAL_TSC_STATE_RESET == htsc->State)
  348. {
  349. switch (CallbackID)
  350. {
  351. case HAL_TSC_MSPINIT_CB_ID :
  352. htsc->MspInitCallback = pCallback;
  353. break;
  354. case HAL_TSC_MSPDEINIT_CB_ID :
  355. htsc->MspDeInitCallback = pCallback;
  356. break;
  357. default :
  358. /* Update the error code */
  359. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  360. /* Return error status */
  361. status = HAL_ERROR;
  362. break;
  363. }
  364. }
  365. else
  366. {
  367. /* Update the error code */
  368. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  369. /* Return error status */
  370. status = HAL_ERROR;
  371. }
  372. /* Release Lock */
  373. __HAL_UNLOCK(htsc);
  374. return status;
  375. }
  376. /**
  377. * @brief Unregister an TSC Callback
  378. * TSC callback is redirected to the weak predefined callback
  379. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  380. * the configuration information for the specified TSC.
  381. * @param CallbackID ID of the callback to be unregistered
  382. * This parameter can be one of the following values:
  383. * This parameter can be one of the following values:
  384. * @arg @ref HAL_TSC_CONV_COMPLETE_CB_ID Conversion completed callback ID
  385. * @arg @ref HAL_TSC_ERROR_CB_ID Error callback ID
  386. * @arg @ref HAL_TSC_MSPINIT_CB_ID MspInit callback ID
  387. * @arg @ref HAL_TSC_MSPDEINIT_CB_ID MspDeInit callback ID
  388. * @retval HAL status
  389. */
  390. HAL_StatusTypeDef HAL_TSC_UnRegisterCallback(TSC_HandleTypeDef *htsc, HAL_TSC_CallbackIDTypeDef CallbackID)
  391. {
  392. HAL_StatusTypeDef status = HAL_OK;
  393. /* Process locked */
  394. __HAL_LOCK(htsc);
  395. if (HAL_TSC_STATE_READY == htsc->State)
  396. {
  397. switch (CallbackID)
  398. {
  399. case HAL_TSC_CONV_COMPLETE_CB_ID :
  400. htsc->ConvCpltCallback = HAL_TSC_ConvCpltCallback; /* Legacy weak ConvCpltCallback */
  401. break;
  402. case HAL_TSC_ERROR_CB_ID :
  403. htsc->ErrorCallback = HAL_TSC_ErrorCallback; /* Legacy weak ErrorCallback */
  404. break;
  405. case HAL_TSC_MSPINIT_CB_ID :
  406. htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit */
  407. break;
  408. case HAL_TSC_MSPDEINIT_CB_ID :
  409. htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit */
  410. break;
  411. default :
  412. /* Update the error code */
  413. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  414. /* Return error status */
  415. status = HAL_ERROR;
  416. break;
  417. }
  418. }
  419. else if (HAL_TSC_STATE_RESET == htsc->State)
  420. {
  421. switch (CallbackID)
  422. {
  423. case HAL_TSC_MSPINIT_CB_ID :
  424. htsc->MspInitCallback = HAL_TSC_MspInit; /* Legacy weak MspInit */
  425. break;
  426. case HAL_TSC_MSPDEINIT_CB_ID :
  427. htsc->MspDeInitCallback = HAL_TSC_MspDeInit; /* Legacy weak MspDeInit */
  428. break;
  429. default :
  430. /* Update the error code */
  431. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  432. /* Return error status */
  433. status = HAL_ERROR;
  434. break;
  435. }
  436. }
  437. else
  438. {
  439. /* Update the error code */
  440. htsc->ErrorCode |= HAL_TSC_ERROR_INVALID_CALLBACK;
  441. /* Return error status */
  442. status = HAL_ERROR;
  443. }
  444. /* Release Lock */
  445. __HAL_UNLOCK(htsc);
  446. return status;
  447. }
  448. #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
  449. /**
  450. * @}
  451. */
  452. /** @defgroup TSC_Exported_Functions_Group2 Input and Output operation functions
  453. * @brief Input and Output operation functions
  454. *
  455. @verbatim
  456. ===============================================================================
  457. ##### IO Operation functions #####
  458. ===============================================================================
  459. [..] This section provides functions allowing to:
  460. (+) Start acquisition in polling mode.
  461. (+) Start acquisition in interrupt mode.
  462. (+) Stop conversion in polling mode.
  463. (+) Stop conversion in interrupt mode.
  464. (+) Poll for acquisition completed.
  465. (+) Get group acquisition status.
  466. (+) Get group acquisition value.
  467. @endverbatim
  468. * @{
  469. */
  470. /**
  471. * @brief Start the acquisition.
  472. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  473. * the configuration information for the specified TSC.
  474. * @retval HAL status
  475. */
  476. HAL_StatusTypeDef HAL_TSC_Start(TSC_HandleTypeDef *htsc)
  477. {
  478. /* Check the parameters */
  479. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  480. /* Process locked */
  481. __HAL_LOCK(htsc);
  482. /* Change TSC state */
  483. htsc->State = HAL_TSC_STATE_BUSY;
  484. /* Clear interrupts */
  485. __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
  486. /* Clear flags */
  487. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  488. /* Set touch sensing IOs not acquired to the specified IODefaultMode */
  489. if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
  490. {
  491. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  492. }
  493. else
  494. {
  495. __HAL_TSC_SET_IODEF_INFLOAT(htsc);
  496. }
  497. /* Launch the acquisition */
  498. __HAL_TSC_START_ACQ(htsc);
  499. /* Process unlocked */
  500. __HAL_UNLOCK(htsc);
  501. /* Return function status */
  502. return HAL_OK;
  503. }
  504. /**
  505. * @brief Start the acquisition in interrupt mode.
  506. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  507. * the configuration information for the specified TSC.
  508. * @retval HAL status.
  509. */
  510. HAL_StatusTypeDef HAL_TSC_Start_IT(TSC_HandleTypeDef *htsc)
  511. {
  512. /* Check the parameters */
  513. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  514. assert_param(IS_TSC_MCE_IT(htsc->Init.MaxCountInterrupt));
  515. /* Process locked */
  516. __HAL_LOCK(htsc);
  517. /* Change TSC state */
  518. htsc->State = HAL_TSC_STATE_BUSY;
  519. /* Enable end of acquisition interrupt */
  520. __HAL_TSC_ENABLE_IT(htsc, TSC_IT_EOA);
  521. /* Enable max count error interrupt (optional) */
  522. if ((FunctionalState)htsc->Init.MaxCountInterrupt == ENABLE)
  523. {
  524. __HAL_TSC_ENABLE_IT(htsc, TSC_IT_MCE);
  525. }
  526. else
  527. {
  528. __HAL_TSC_DISABLE_IT(htsc, TSC_IT_MCE);
  529. }
  530. /* Clear flags */
  531. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  532. /* Set touch sensing IOs not acquired to the specified IODefaultMode */
  533. if (htsc->Init.IODefaultMode == TSC_IODEF_OUT_PP_LOW)
  534. {
  535. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  536. }
  537. else
  538. {
  539. __HAL_TSC_SET_IODEF_INFLOAT(htsc);
  540. }
  541. /* Launch the acquisition */
  542. __HAL_TSC_START_ACQ(htsc);
  543. /* Process unlocked */
  544. __HAL_UNLOCK(htsc);
  545. /* Return function status */
  546. return HAL_OK;
  547. }
  548. /**
  549. * @brief Stop the acquisition previously launched in polling mode.
  550. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  551. * the configuration information for the specified TSC.
  552. * @retval HAL status
  553. */
  554. HAL_StatusTypeDef HAL_TSC_Stop(TSC_HandleTypeDef *htsc)
  555. {
  556. /* Check the parameters */
  557. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  558. /* Process locked */
  559. __HAL_LOCK(htsc);
  560. /* Stop the acquisition */
  561. __HAL_TSC_STOP_ACQ(htsc);
  562. /* Set touch sensing IOs in low power mode (output push-pull) */
  563. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  564. /* Clear flags */
  565. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  566. /* Change TSC state */
  567. htsc->State = HAL_TSC_STATE_READY;
  568. /* Process unlocked */
  569. __HAL_UNLOCK(htsc);
  570. /* Return function status */
  571. return HAL_OK;
  572. }
  573. /**
  574. * @brief Stop the acquisition previously launched in interrupt mode.
  575. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  576. * the configuration information for the specified TSC.
  577. * @retval HAL status
  578. */
  579. HAL_StatusTypeDef HAL_TSC_Stop_IT(TSC_HandleTypeDef *htsc)
  580. {
  581. /* Check the parameters */
  582. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  583. /* Process locked */
  584. __HAL_LOCK(htsc);
  585. /* Stop the acquisition */
  586. __HAL_TSC_STOP_ACQ(htsc);
  587. /* Set touch sensing IOs in low power mode (output push-pull) */
  588. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  589. /* Disable interrupts */
  590. __HAL_TSC_DISABLE_IT(htsc, (TSC_IT_EOA | TSC_IT_MCE));
  591. /* Clear flags */
  592. __HAL_TSC_CLEAR_FLAG(htsc, (TSC_FLAG_EOA | TSC_FLAG_MCE));
  593. /* Change TSC state */
  594. htsc->State = HAL_TSC_STATE_READY;
  595. /* Process unlocked */
  596. __HAL_UNLOCK(htsc);
  597. /* Return function status */
  598. return HAL_OK;
  599. }
  600. /**
  601. * @brief Start acquisition and wait until completion.
  602. * @note There is no need of a timeout parameter as the max count error is already
  603. * managed by the TSC peripheral.
  604. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  605. * the configuration information for the specified TSC.
  606. * @retval HAL state
  607. */
  608. HAL_StatusTypeDef HAL_TSC_PollForAcquisition(TSC_HandleTypeDef *htsc)
  609. {
  610. /* Check the parameters */
  611. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  612. /* Process locked */
  613. __HAL_LOCK(htsc);
  614. /* Check end of acquisition */
  615. while (HAL_TSC_GetState(htsc) == HAL_TSC_STATE_BUSY)
  616. {
  617. /* The timeout (max count error) is managed by the TSC peripheral itself. */
  618. }
  619. /* Process unlocked */
  620. __HAL_UNLOCK(htsc);
  621. return HAL_OK;
  622. }
  623. /**
  624. * @brief Get the acquisition status for a group.
  625. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  626. * the configuration information for the specified TSC.
  627. * @param gx_index Index of the group
  628. * @retval Group status
  629. */
  630. TSC_GroupStatusTypeDef HAL_TSC_GroupGetStatus(TSC_HandleTypeDef *htsc, uint32_t gx_index)
  631. {
  632. /* Check the parameters */
  633. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  634. assert_param(IS_TSC_GROUP_INDEX(gx_index));
  635. /* Return the group status */
  636. return (__HAL_TSC_GET_GROUP_STATUS(htsc, gx_index));
  637. }
  638. /**
  639. * @brief Get the acquisition measure for a group.
  640. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  641. * the configuration information for the specified TSC.
  642. * @param gx_index Index of the group
  643. * @retval Acquisition measure
  644. */
  645. uint32_t HAL_TSC_GroupGetValue(TSC_HandleTypeDef *htsc, uint32_t gx_index)
  646. {
  647. /* Check the parameters */
  648. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  649. assert_param(IS_TSC_GROUP_INDEX(gx_index));
  650. /* Return the group acquisition counter */
  651. return htsc->Instance->IOGXCR[gx_index];
  652. }
  653. /**
  654. * @}
  655. */
  656. /** @defgroup TSC_Exported_Functions_Group3 Peripheral Control functions
  657. * @brief Peripheral Control functions
  658. *
  659. @verbatim
  660. ===============================================================================
  661. ##### Peripheral Control functions #####
  662. ===============================================================================
  663. [..] This section provides functions allowing to:
  664. (+) Configure TSC IOs
  665. (+) Discharge TSC IOs
  666. @endverbatim
  667. * @{
  668. */
  669. /**
  670. * @brief Configure TSC IOs.
  671. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  672. * the configuration information for the specified TSC.
  673. * @param config Pointer to the configuration structure.
  674. * @retval HAL status
  675. */
  676. HAL_StatusTypeDef HAL_TSC_IOConfig(TSC_HandleTypeDef *htsc, TSC_IOConfigTypeDef *config)
  677. {
  678. /* Check the parameters */
  679. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  680. assert_param(IS_TSC_GROUP(config->ChannelIOs));
  681. assert_param(IS_TSC_GROUP(config->ShieldIOs));
  682. assert_param(IS_TSC_GROUP(config->SamplingIOs));
  683. /* Process locked */
  684. __HAL_LOCK(htsc);
  685. /* Stop acquisition */
  686. __HAL_TSC_STOP_ACQ(htsc);
  687. /* Disable Schmitt trigger hysteresis on all used TSC IOs */
  688. htsc->Instance->IOHCR = (uint32_t)(~(config->ChannelIOs | config->ShieldIOs | config->SamplingIOs));
  689. /* Set channel and shield IOs */
  690. htsc->Instance->IOCCR = (config->ChannelIOs | config->ShieldIOs);
  691. /* Set sampling IOs */
  692. htsc->Instance->IOSCR = config->SamplingIOs;
  693. /* Set groups to be acquired */
  694. htsc->Instance->IOGCSR = TSC_extract_groups(config->ChannelIOs);
  695. /* Process unlocked */
  696. __HAL_UNLOCK(htsc);
  697. /* Return function status */
  698. return HAL_OK;
  699. }
  700. /**
  701. * @brief Discharge TSC IOs.
  702. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  703. * the configuration information for the specified TSC.
  704. * @param choice This parameter can be set to ENABLE or DISABLE.
  705. * @retval HAL status
  706. */
  707. HAL_StatusTypeDef HAL_TSC_IODischarge(TSC_HandleTypeDef *htsc, uint32_t choice)
  708. {
  709. /* Check the parameters */
  710. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  711. /* Process locked */
  712. __HAL_LOCK(htsc);
  713. if ((FunctionalState)choice == ENABLE)
  714. {
  715. __HAL_TSC_SET_IODEF_OUTPPLOW(htsc);
  716. }
  717. else
  718. {
  719. __HAL_TSC_SET_IODEF_INFLOAT(htsc);
  720. }
  721. /* Process unlocked */
  722. __HAL_UNLOCK(htsc);
  723. /* Return the group acquisition counter */
  724. return HAL_OK;
  725. }
  726. /**
  727. * @}
  728. */
  729. /** @defgroup TSC_Exported_Functions_Group4 Peripheral State and Errors functions
  730. * @brief Peripheral State and Errors functions
  731. *
  732. @verbatim
  733. ===============================================================================
  734. ##### State and Errors functions #####
  735. ===============================================================================
  736. [..]
  737. This subsection provides functions allowing to
  738. (+) Get TSC state.
  739. @endverbatim
  740. * @{
  741. */
  742. /**
  743. * @brief Return the TSC handle state.
  744. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  745. * the configuration information for the specified TSC.
  746. * @retval HAL state
  747. */
  748. HAL_TSC_StateTypeDef HAL_TSC_GetState(TSC_HandleTypeDef *htsc)
  749. {
  750. /* Check the parameters */
  751. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  752. if (htsc->State == HAL_TSC_STATE_BUSY)
  753. {
  754. /* Check end of acquisition flag */
  755. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
  756. {
  757. /* Check max count error flag */
  758. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
  759. {
  760. /* Change TSC state */
  761. htsc->State = HAL_TSC_STATE_ERROR;
  762. }
  763. else
  764. {
  765. /* Change TSC state */
  766. htsc->State = HAL_TSC_STATE_READY;
  767. }
  768. }
  769. }
  770. /* Return TSC state */
  771. return htsc->State;
  772. }
  773. /**
  774. * @}
  775. */
  776. /** @defgroup TSC_IRQ_Handler_and_Callbacks IRQ Handler and Callbacks
  777. * @{
  778. */
  779. /**
  780. * @brief Handle TSC interrupt request.
  781. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  782. * the configuration information for the specified TSC.
  783. * @retval None
  784. */
  785. void HAL_TSC_IRQHandler(TSC_HandleTypeDef *htsc)
  786. {
  787. /* Check the parameters */
  788. assert_param(IS_TSC_ALL_INSTANCE(htsc->Instance));
  789. /* Check if the end of acquisition occurred */
  790. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_EOA) != RESET)
  791. {
  792. /* Clear EOA flag */
  793. __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_EOA);
  794. }
  795. /* Check if max count error occurred */
  796. if (__HAL_TSC_GET_FLAG(htsc, TSC_FLAG_MCE) != RESET)
  797. {
  798. /* Clear MCE flag */
  799. __HAL_TSC_CLEAR_FLAG(htsc, TSC_FLAG_MCE);
  800. /* Change TSC state */
  801. htsc->State = HAL_TSC_STATE_ERROR;
  802. #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
  803. htsc->ErrorCallback(htsc);
  804. #else
  805. /* Conversion completed callback */
  806. HAL_TSC_ErrorCallback(htsc);
  807. #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
  808. }
  809. else
  810. {
  811. /* Change TSC state */
  812. htsc->State = HAL_TSC_STATE_READY;
  813. #if (USE_HAL_TSC_REGISTER_CALLBACKS == 1)
  814. htsc->ConvCpltCallback(htsc);
  815. #else
  816. /* Conversion completed callback */
  817. HAL_TSC_ConvCpltCallback(htsc);
  818. #endif /* USE_HAL_TSC_REGISTER_CALLBACKS */
  819. }
  820. }
  821. /**
  822. * @brief Acquisition completed callback in non-blocking mode.
  823. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  824. * the configuration information for the specified TSC.
  825. * @retval None
  826. */
  827. __weak void HAL_TSC_ConvCpltCallback(TSC_HandleTypeDef *htsc)
  828. {
  829. /* Prevent unused argument(s) compilation warning */
  830. UNUSED(htsc);
  831. /* NOTE : This function should not be modified, when the callback is needed,
  832. the HAL_TSC_ConvCpltCallback could be implemented in the user file.
  833. */
  834. }
  835. /**
  836. * @brief Error callback in non-blocking mode.
  837. * @param htsc Pointer to a TSC_HandleTypeDef structure that contains
  838. * the configuration information for the specified TSC.
  839. * @retval None
  840. */
  841. __weak void HAL_TSC_ErrorCallback(TSC_HandleTypeDef *htsc)
  842. {
  843. /* Prevent unused argument(s) compilation warning */
  844. UNUSED(htsc);
  845. /* NOTE : This function should not be modified, when the callback is needed,
  846. the HAL_TSC_ErrorCallback could be implemented in the user file.
  847. */
  848. }
  849. /**
  850. * @}
  851. */
  852. /**
  853. * @}
  854. */
  855. /* Private functions ---------------------------------------------------------*/
  856. /** @defgroup TSC_Private_Functions Private Functions
  857. * @{
  858. */
  859. /**
  860. * @brief Utility function used to set the acquired groups mask.
  861. * @param iomask Channels IOs mask
  862. * @retval Acquired groups mask
  863. */
  864. static uint32_t TSC_extract_groups(uint32_t iomask)
  865. {
  866. uint32_t groups = 0UL;
  867. uint32_t idx;
  868. for (idx = 0UL; idx < (uint32_t)TSC_NB_OF_GROUPS; idx++)
  869. {
  870. if ((iomask & (0x0FUL << (idx * 4UL))) != 0UL )
  871. {
  872. groups |= (1UL << idx);
  873. }
  874. }
  875. return groups;
  876. }
  877. /**
  878. * @}
  879. */
  880. #endif /* HAL_TSC_MODULE_ENABLED */
  881. /**
  882. * @}
  883. */
  884. /**
  885. * @}
  886. */
  887. #endif /* TSC */
  888. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/