stm32l0xx_hal_tsc.c 33 KB

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