stm32l4xx_hal_tsc.c 34 KB

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