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.
 
 
 

1123 lines
34 KiB

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