stm32f4xx_hal_tim_ex.c 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_tim_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  7. * @brief TIM HAL module driver.
  8. * This file provides firmware functions to manage the following
  9. * functionalities of the Timer extension peripheral:
  10. * + Time Hall Sensor Interface Initialization
  11. * + Time Hall Sensor Interface Start
  12. * + Time Complementary signal bread and dead time configuration
  13. * + Time Master and Slave synchronization configuration
  14. @verbatim
  15. ==============================================================================
  16. ##### TIMER Extended features #####
  17. ==============================================================================
  18. [..]
  19. The Timer Extension features include:
  20. (#) Complementary outputs with programmable dead-time for :
  21. (++) Input Capture
  22. (++) Output Compare
  23. (++) PWM generation (Edge and Center-aligned Mode)
  24. (++) One-pulse mode output
  25. (#) Synchronization circuit to control the timer with external signals and to
  26. interconnect several timers together.
  27. (#) Break input to put the timer output signals in reset state or in a known state.
  28. (#) Supports incremental (quadrature) encoder and hall-sensor circuitry for
  29. positioning purposes
  30. ##### How to use this driver #####
  31. ==============================================================================
  32. [..]
  33. (#) Initialize the TIM low level resources by implementing the following functions
  34. depending from feature used :
  35. (++) Complementary Output Compare : HAL_TIM_OC_MspInit()
  36. (++) Complementary PWM generation : HAL_TIM_PWM_MspInit()
  37. (++) Complementary One-pulse mode output : HAL_TIM_OnePulse_MspInit()
  38. (++) Hall Sensor output : HAL_TIM_HallSensor_MspInit()
  39. (#) Initialize the TIM low level resources :
  40. (##) Enable the TIM interface clock using __TIMx_CLK_ENABLE();
  41. (##) TIM pins configuration
  42. (+++) Enable the clock for the TIM GPIOs using the following function:
  43. __GPIOx_CLK_ENABLE();
  44. (+++) Configure these TIM pins in Alternate function mode using HAL_GPIO_Init();
  45. (#) The external Clock can be configured, if needed (the default clock is the
  46. internal clock from the APBx), using the following function:
  47. HAL_TIM_ConfigClockSource, the clock configuration should be done before
  48. any start function.
  49. (#) Configure the TIM in the desired functioning mode using one of the
  50. initialization function of this driver:
  51. (++) HAL_TIMEx_HallSensor_Init and HAL_TIMEx_ConfigCommutationEvent: to use the
  52. Timer Hall Sensor Interface and the commutation event with the corresponding
  53. Interrupt and DMA request if needed (Note that One Timer is used to interface
  54. with the Hall sensor Interface and another Timer should be used to use
  55. the commutation event).
  56. (#) Activate the TIM peripheral using one of the start functions:
  57. (++) Complementary Output Compare : HAL_TIMEx_OCN_Start(), HAL_TIMEx_OCN_Start_DMA(), HAL_TIMEx_OC_Start_IT()
  58. (++) Complementary PWM generation : HAL_TIMEx_PWMN_Start(), HAL_TIMEx_PWMN_Start_DMA(), HAL_TIMEx_PWMN_Start_IT()
  59. (++) Complementary One-pulse mode output : HAL_TIMEx_OnePulseN_Start(), HAL_TIMEx_OnePulseN_Start_IT()
  60. (++) Hall Sensor output : HAL_TIMEx_HallSensor_Start(), HAL_TIMEx_HallSensor_Start_DMA(), HAL_TIMEx_HallSensor_Start_IT().
  61. @endverbatim
  62. ******************************************************************************
  63. * @attention
  64. *
  65. * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
  66. *
  67. * Redistribution and use in source and binary forms, with or without modification,
  68. * are permitted provided that the following conditions are met:
  69. * 1. Redistributions of source code must retain the above copyright notice,
  70. * this list of conditions and the following disclaimer.
  71. * 2. Redistributions in binary form must reproduce the above copyright notice,
  72. * this list of conditions and the following disclaimer in the documentation
  73. * and/or other materials provided with the distribution.
  74. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  75. * may be used to endorse or promote products derived from this software
  76. * without specific prior written permission.
  77. *
  78. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  79. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  80. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  81. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  82. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  83. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  84. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  85. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  86. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  87. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  88. *
  89. ******************************************************************************
  90. */
  91. /* Includes ------------------------------------------------------------------*/
  92. #include "stm32f4xx_hal.h"
  93. /** @addtogroup STM32F4xx_HAL_Driver
  94. * @{
  95. */
  96. /** @defgroup TIMEx TIMEx
  97. * @brief TIM HAL module driver
  98. * @{
  99. */
  100. #ifdef HAL_TIM_MODULE_ENABLED
  101. /* Private typedef -----------------------------------------------------------*/
  102. /* Private define ------------------------------------------------------------*/
  103. /* Private macro -------------------------------------------------------------*/
  104. /* Private variables ---------------------------------------------------------*/
  105. /** @addtogroup TIMEx_Private_Functions
  106. * @{
  107. */
  108. /* Private function prototypes -----------------------------------------------*/
  109. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState);
  110. /**
  111. * @}
  112. */
  113. /* Exported functions --------------------------------------------------------*/
  114. /** @defgroup TIMEx_Exported_Functions TIM Exported Functions
  115. * @{
  116. */
  117. /** @defgroup TIMEx_Exported_Functions_Group1 Timer Hall Sensor functions
  118. * @brief Timer Hall Sensor functions
  119. *
  120. @verbatim
  121. ==============================================================================
  122. ##### Timer Hall Sensor functions #####
  123. ==============================================================================
  124. [..]
  125. This section provides functions allowing to:
  126. (+) Initialize and configure TIM HAL Sensor.
  127. (+) De-initialize TIM HAL Sensor.
  128. (+) Start the Hall Sensor Interface.
  129. (+) Stop the Hall Sensor Interface.
  130. (+) Start the Hall Sensor Interface and enable interrupts.
  131. (+) Stop the Hall Sensor Interface and disable interrupts.
  132. (+) Start the Hall Sensor Interface and enable DMA transfers.
  133. (+) Stop the Hall Sensor Interface and disable DMA transfers.
  134. @endverbatim
  135. * @{
  136. */
  137. /**
  138. * @brief Initializes the TIM Hall Sensor Interface and create the associated handle.
  139. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  140. * the configuration information for TIM module.
  141. * @param sConfig: TIM Hall Sensor configuration structure
  142. * @retval HAL status
  143. */
  144. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Init(TIM_HandleTypeDef *htim, TIM_HallSensor_InitTypeDef* sConfig)
  145. {
  146. TIM_OC_InitTypeDef OC_Config;
  147. /* Check the TIM handle allocation */
  148. if(htim == NULL)
  149. {
  150. return HAL_ERROR;
  151. }
  152. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  153. assert_param(IS_TIM_COUNTER_MODE(htim->Init.CounterMode));
  154. assert_param(IS_TIM_CLOCKDIVISION_DIV(htim->Init.ClockDivision));
  155. assert_param(IS_TIM_IC_POLARITY(sConfig->IC1Polarity));
  156. assert_param(IS_TIM_IC_PRESCALER(sConfig->IC1Prescaler));
  157. assert_param(IS_TIM_IC_FILTER(sConfig->IC1Filter));
  158. /* Set the TIM state */
  159. htim->State= HAL_TIM_STATE_BUSY;
  160. /* Init the low level hardware : GPIO, CLOCK, NVIC and DMA */
  161. HAL_TIMEx_HallSensor_MspInit(htim);
  162. /* Configure the Time base in the Encoder Mode */
  163. TIM_Base_SetConfig(htim->Instance, &htim->Init);
  164. /* Configure the Channel 1 as Input Channel to interface with the three Outputs of the Hall sensor */
  165. TIM_TI1_SetConfig(htim->Instance, sConfig->IC1Polarity, TIM_ICSELECTION_TRC, sConfig->IC1Filter);
  166. /* Reset the IC1PSC Bits */
  167. htim->Instance->CCMR1 &= ~TIM_CCMR1_IC1PSC;
  168. /* Set the IC1PSC value */
  169. htim->Instance->CCMR1 |= sConfig->IC1Prescaler;
  170. /* Enable the Hall sensor interface (XOR function of the three inputs) */
  171. htim->Instance->CR2 |= TIM_CR2_TI1S;
  172. /* Select the TIM_TS_TI1F_ED signal as Input trigger for the TIM */
  173. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  174. htim->Instance->SMCR |= TIM_TS_TI1F_ED;
  175. /* Use the TIM_TS_TI1F_ED signal to reset the TIM counter each edge detection */
  176. htim->Instance->SMCR &= ~TIM_SMCR_SMS;
  177. htim->Instance->SMCR |= TIM_SLAVEMODE_RESET;
  178. /* Program channel 2 in PWM 2 mode with the desired Commutation_Delay*/
  179. OC_Config.OCFastMode = TIM_OCFAST_DISABLE;
  180. OC_Config.OCIdleState = TIM_OCIDLESTATE_RESET;
  181. OC_Config.OCMode = TIM_OCMODE_PWM2;
  182. OC_Config.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  183. OC_Config.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  184. OC_Config.OCPolarity = TIM_OCPOLARITY_HIGH;
  185. OC_Config.Pulse = sConfig->Commutation_Delay;
  186. TIM_OC2_SetConfig(htim->Instance, &OC_Config);
  187. /* Select OC2REF as trigger output on TRGO: write the MMS bits in the TIMx_CR2
  188. register to 101 */
  189. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  190. htim->Instance->CR2 |= TIM_TRGO_OC2REF;
  191. /* Initialize the TIM state*/
  192. htim->State= HAL_TIM_STATE_READY;
  193. return HAL_OK;
  194. }
  195. /**
  196. * @brief DeInitializes the TIM Hall Sensor interface
  197. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  198. * the configuration information for TIM module.
  199. * @retval HAL status
  200. */
  201. HAL_StatusTypeDef HAL_TIMEx_HallSensor_DeInit(TIM_HandleTypeDef *htim)
  202. {
  203. /* Check the parameters */
  204. assert_param(IS_TIM_INSTANCE(htim->Instance));
  205. htim->State = HAL_TIM_STATE_BUSY;
  206. /* Disable the TIM Peripheral Clock */
  207. __HAL_TIM_DISABLE(htim);
  208. /* DeInit the low level hardware: GPIO, CLOCK, NVIC */
  209. HAL_TIMEx_HallSensor_MspDeInit(htim);
  210. /* Change TIM state */
  211. htim->State = HAL_TIM_STATE_RESET;
  212. /* Release Lock */
  213. __HAL_UNLOCK(htim);
  214. return HAL_OK;
  215. }
  216. /**
  217. * @brief Initializes the TIM Hall Sensor MSP.
  218. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  219. * the configuration information for TIM module.
  220. * @retval None
  221. */
  222. __weak void HAL_TIMEx_HallSensor_MspInit(TIM_HandleTypeDef *htim)
  223. {
  224. /* NOTE : This function Should not be modified, when the callback is needed,
  225. the HAL_TIMEx_HallSensor_MspInit could be implemented in the user file
  226. */
  227. }
  228. /**
  229. * @brief DeInitializes TIM Hall Sensor MSP.
  230. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  231. * the configuration information for TIM module.
  232. * @retval None
  233. */
  234. __weak void HAL_TIMEx_HallSensor_MspDeInit(TIM_HandleTypeDef *htim)
  235. {
  236. /* NOTE : This function Should not be modified, when the callback is needed,
  237. the HAL_TIMEx_HallSensor_MspDeInit could be implemented in the user file
  238. */
  239. }
  240. /**
  241. * @brief Starts the TIM Hall Sensor Interface.
  242. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  243. * the configuration information for TIM module.
  244. * @retval HAL status
  245. */
  246. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start(TIM_HandleTypeDef *htim)
  247. {
  248. /* Check the parameters */
  249. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  250. /* Enable the Input Capture channels 1
  251. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  252. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  253. /* Enable the Peripheral */
  254. __HAL_TIM_ENABLE(htim);
  255. /* Return function status */
  256. return HAL_OK;
  257. }
  258. /**
  259. * @brief Stops the TIM Hall sensor Interface.
  260. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  261. * the configuration information for TIM module.
  262. * @retval HAL status
  263. */
  264. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop(TIM_HandleTypeDef *htim)
  265. {
  266. /* Check the parameters */
  267. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  268. /* Disable the Input Capture channels 1, 2 and 3
  269. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  270. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  271. /* Disable the Peripheral */
  272. __HAL_TIM_DISABLE(htim);
  273. /* Return function status */
  274. return HAL_OK;
  275. }
  276. /**
  277. * @brief Starts the TIM Hall Sensor Interface in interrupt mode.
  278. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  279. * the configuration information for TIM module.
  280. * @retval HAL status
  281. */
  282. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_IT(TIM_HandleTypeDef *htim)
  283. {
  284. /* Check the parameters */
  285. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  286. /* Enable the capture compare Interrupts 1 event */
  287. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  288. /* Enable the Input Capture channels 1
  289. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  290. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  291. /* Enable the Peripheral */
  292. __HAL_TIM_ENABLE(htim);
  293. /* Return function status */
  294. return HAL_OK;
  295. }
  296. /**
  297. * @brief Stops the TIM Hall Sensor Interface in interrupt mode.
  298. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  299. * the configuration information for TIM module.
  300. * @retval HAL status
  301. */
  302. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_IT(TIM_HandleTypeDef *htim)
  303. {
  304. /* Check the parameters */
  305. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  306. /* Disable the Input Capture channels 1
  307. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  308. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  309. /* Disable the capture compare Interrupts event */
  310. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  311. /* Disable the Peripheral */
  312. __HAL_TIM_DISABLE(htim);
  313. /* Return function status */
  314. return HAL_OK;
  315. }
  316. /**
  317. * @brief Starts the TIM Hall Sensor Interface in DMA mode.
  318. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  319. * the configuration information for TIM module.
  320. * @param pData: The destination Buffer address.
  321. * @param Length: The length of data to be transferred from TIM peripheral to memory.
  322. * @retval HAL status
  323. */
  324. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Start_DMA(TIM_HandleTypeDef *htim, uint32_t *pData, uint16_t Length)
  325. {
  326. /* Check the parameters */
  327. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  328. if((htim->State == HAL_TIM_STATE_BUSY))
  329. {
  330. return HAL_BUSY;
  331. }
  332. else if((htim->State == HAL_TIM_STATE_READY))
  333. {
  334. if(((uint32_t)pData == 0 ) && (Length > 0))
  335. {
  336. return HAL_ERROR;
  337. }
  338. else
  339. {
  340. htim->State = HAL_TIM_STATE_BUSY;
  341. }
  342. }
  343. /* Enable the Input Capture channels 1
  344. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  345. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_ENABLE);
  346. /* Set the DMA Input Capture 1 Callback */
  347. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMACaptureCplt;
  348. /* Set the DMA error callback */
  349. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  350. /* Enable the DMA Stream for Capture 1*/
  351. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)&htim->Instance->CCR1, (uint32_t)pData, Length);
  352. /* Enable the capture compare 1 Interrupt */
  353. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  354. /* Enable the Peripheral */
  355. __HAL_TIM_ENABLE(htim);
  356. /* Return function status */
  357. return HAL_OK;
  358. }
  359. /**
  360. * @brief Stops the TIM Hall Sensor Interface in DMA mode.
  361. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  362. * the configuration information for TIM module.
  363. * @retval HAL status
  364. */
  365. HAL_StatusTypeDef HAL_TIMEx_HallSensor_Stop_DMA(TIM_HandleTypeDef *htim)
  366. {
  367. /* Check the parameters */
  368. assert_param(IS_TIM_XOR_INSTANCE(htim->Instance));
  369. /* Disable the Input Capture channels 1
  370. (in the Hall Sensor Interface the Three possible channels that can be used are TIM_CHANNEL_1, TIM_CHANNEL_2 and TIM_CHANNEL_3) */
  371. TIM_CCxChannelCmd(htim->Instance, TIM_CHANNEL_1, TIM_CCx_DISABLE);
  372. /* Disable the capture compare Interrupts 1 event */
  373. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  374. /* Disable the Peripheral */
  375. __HAL_TIM_DISABLE(htim);
  376. /* Return function status */
  377. return HAL_OK;
  378. }
  379. /**
  380. * @}
  381. */
  382. /** @defgroup TIMEx_Exported_Functions_Group2 Timer Complementary Output Compare functions
  383. * @brief Timer Complementary Output Compare functions
  384. *
  385. @verbatim
  386. ==============================================================================
  387. ##### Timer Complementary Output Compare functions #####
  388. ==============================================================================
  389. [..]
  390. This section provides functions allowing to:
  391. (+) Start the Complementary Output Compare/PWM.
  392. (+) Stop the Complementary Output Compare/PWM.
  393. (+) Start the Complementary Output Compare/PWM and enable interrupts.
  394. (+) Stop the Complementary Output Compare/PWM and disable interrupts.
  395. (+) Start the Complementary Output Compare/PWM and enable DMA transfers.
  396. (+) Stop the Complementary Output Compare/PWM and disable DMA transfers.
  397. @endverbatim
  398. * @{
  399. */
  400. /**
  401. * @brief Starts the TIM Output Compare signal generation on the complementary
  402. * output.
  403. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  404. * the configuration information for TIM module.
  405. * @param Channel: TIM Channel to be enabled.
  406. * This parameter can be one of the following values:
  407. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  408. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  409. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  410. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  411. * @retval HAL status
  412. */
  413. HAL_StatusTypeDef HAL_TIMEx_OCN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  414. {
  415. /* Check the parameters */
  416. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  417. /* Enable the Capture compare channel N */
  418. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  419. /* Enable the Main Output */
  420. __HAL_TIM_MOE_ENABLE(htim);
  421. /* Enable the Peripheral */
  422. __HAL_TIM_ENABLE(htim);
  423. /* Return function status */
  424. return HAL_OK;
  425. }
  426. /**
  427. * @brief Stops the TIM Output Compare signal generation on the complementary
  428. * output.
  429. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  430. * the configuration information for TIM module.
  431. * @param Channel: TIM Channel to be disabled.
  432. * This parameter can be one of the following values:
  433. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  434. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  435. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  436. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  437. * @retval HAL status
  438. */
  439. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  440. {
  441. /* Check the parameters */
  442. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  443. /* Disable the Capture compare channel N */
  444. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  445. /* Disable the Main Output */
  446. __HAL_TIM_MOE_DISABLE(htim);
  447. /* Disable the Peripheral */
  448. __HAL_TIM_DISABLE(htim);
  449. /* Return function status */
  450. return HAL_OK;
  451. }
  452. /**
  453. * @brief Starts the TIM Output Compare signal generation in interrupt mode
  454. * on the complementary output.
  455. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  456. * the configuration information for TIM module.
  457. * @param Channel: TIM Channel to be enabled.
  458. * This parameter can be one of the following values:
  459. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  460. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  461. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  462. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  463. * @retval HAL status
  464. */
  465. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  466. {
  467. /* Check the parameters */
  468. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  469. switch (Channel)
  470. {
  471. case TIM_CHANNEL_1:
  472. {
  473. /* Enable the TIM Output Compare interrupt */
  474. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  475. }
  476. break;
  477. case TIM_CHANNEL_2:
  478. {
  479. /* Enable the TIM Output Compare interrupt */
  480. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  481. }
  482. break;
  483. case TIM_CHANNEL_3:
  484. {
  485. /* Enable the TIM Output Compare interrupt */
  486. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  487. }
  488. break;
  489. case TIM_CHANNEL_4:
  490. {
  491. /* Enable the TIM Output Compare interrupt */
  492. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  493. }
  494. break;
  495. default:
  496. break;
  497. }
  498. /* Enable the TIM Break interrupt */
  499. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  500. /* Enable the Capture compare channel N */
  501. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  502. /* Enable the Main Output */
  503. __HAL_TIM_MOE_ENABLE(htim);
  504. /* Enable the Peripheral */
  505. __HAL_TIM_ENABLE(htim);
  506. /* Return function status */
  507. return HAL_OK;
  508. }
  509. /**
  510. * @brief Stops the TIM Output Compare signal generation in interrupt mode
  511. * on the complementary output.
  512. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  513. * the configuration information for TIM module.
  514. * @param Channel: TIM Channel to be disabled.
  515. * This parameter can be one of the following values:
  516. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  517. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  518. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  519. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  520. * @retval HAL status
  521. */
  522. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  523. {
  524. /* Check the parameters */
  525. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  526. switch (Channel)
  527. {
  528. case TIM_CHANNEL_1:
  529. {
  530. /* Disable the TIM Output Compare interrupt */
  531. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  532. }
  533. break;
  534. case TIM_CHANNEL_2:
  535. {
  536. /* Disable the TIM Output Compare interrupt */
  537. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  538. }
  539. break;
  540. case TIM_CHANNEL_3:
  541. {
  542. /* Disable the TIM Output Compare interrupt */
  543. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  544. }
  545. break;
  546. case TIM_CHANNEL_4:
  547. {
  548. /* Disable the TIM Output Compare interrupt */
  549. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  550. }
  551. break;
  552. default:
  553. break;
  554. }
  555. /* Disable the Capture compare channel N */
  556. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  557. /* Disable the TIM Break interrupt (only if no more channel is active) */
  558. if((READ_REG(htim->Instance->CCER) & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  559. {
  560. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  561. }
  562. /* Disable the Main Output */
  563. __HAL_TIM_MOE_DISABLE(htim);
  564. /* Disable the Peripheral */
  565. __HAL_TIM_DISABLE(htim);
  566. /* Return function status */
  567. return HAL_OK;
  568. }
  569. /**
  570. * @brief Starts the TIM Output Compare signal generation in DMA mode
  571. * on the complementary output.
  572. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  573. * the configuration information for TIM module.
  574. * @param Channel: TIM Channel to be enabled.
  575. * This parameter can be one of the following values:
  576. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  577. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  578. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  579. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  580. * @param pData: The source Buffer address.
  581. * @param Length: The length of data to be transferred from memory to TIM peripheral
  582. * @retval HAL status
  583. */
  584. HAL_StatusTypeDef HAL_TIMEx_OCN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  585. {
  586. /* Check the parameters */
  587. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  588. if((htim->State == HAL_TIM_STATE_BUSY))
  589. {
  590. return HAL_BUSY;
  591. }
  592. else if((htim->State == HAL_TIM_STATE_READY))
  593. {
  594. if(((uint32_t)pData == 0 ) && (Length > 0))
  595. {
  596. return HAL_ERROR;
  597. }
  598. else
  599. {
  600. htim->State = HAL_TIM_STATE_BUSY;
  601. }
  602. }
  603. switch (Channel)
  604. {
  605. case TIM_CHANNEL_1:
  606. {
  607. /* Set the DMA Period elapsed callback */
  608. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  609. /* Set the DMA error callback */
  610. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  611. /* Enable the DMA Stream */
  612. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  613. /* Enable the TIM Output Compare DMA request */
  614. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  615. }
  616. break;
  617. case TIM_CHANNEL_2:
  618. {
  619. /* Set the DMA Period elapsed callback */
  620. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  621. /* Set the DMA error callback */
  622. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  623. /* Enable the DMA Stream */
  624. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  625. /* Enable the TIM Output Compare DMA request */
  626. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  627. }
  628. break;
  629. case TIM_CHANNEL_3:
  630. {
  631. /* Set the DMA Period elapsed callback */
  632. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  633. /* Set the DMA error callback */
  634. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  635. /* Enable the DMA Stream */
  636. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  637. /* Enable the TIM Output Compare DMA request */
  638. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  639. }
  640. break;
  641. case TIM_CHANNEL_4:
  642. {
  643. /* Set the DMA Period elapsed callback */
  644. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  645. /* Set the DMA error callback */
  646. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  647. /* Enable the DMA Stream */
  648. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  649. /* Enable the TIM Output Compare DMA request */
  650. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  651. }
  652. break;
  653. default:
  654. break;
  655. }
  656. /* Enable the Capture compare channel N */
  657. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  658. /* Enable the Main Output */
  659. __HAL_TIM_MOE_ENABLE(htim);
  660. /* Enable the Peripheral */
  661. __HAL_TIM_ENABLE(htim);
  662. /* Return function status */
  663. return HAL_OK;
  664. }
  665. /**
  666. * @brief Stops the TIM Output Compare signal generation in DMA mode
  667. * on the complementary output.
  668. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  669. * the configuration information for TIM module.
  670. * @param Channel: TIM Channel to be disabled.
  671. * This parameter can be one of the following values:
  672. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  673. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  674. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  675. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  676. * @retval HAL status
  677. */
  678. HAL_StatusTypeDef HAL_TIMEx_OCN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  679. {
  680. /* Check the parameters */
  681. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  682. switch (Channel)
  683. {
  684. case TIM_CHANNEL_1:
  685. {
  686. /* Disable the TIM Output Compare DMA request */
  687. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  688. }
  689. break;
  690. case TIM_CHANNEL_2:
  691. {
  692. /* Disable the TIM Output Compare DMA request */
  693. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  694. }
  695. break;
  696. case TIM_CHANNEL_3:
  697. {
  698. /* Disable the TIM Output Compare DMA request */
  699. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  700. }
  701. break;
  702. case TIM_CHANNEL_4:
  703. {
  704. /* Disable the TIM Output Compare interrupt */
  705. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  706. }
  707. break;
  708. default:
  709. break;
  710. }
  711. /* Disable the Capture compare channel N */
  712. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  713. /* Disable the Main Output */
  714. __HAL_TIM_MOE_DISABLE(htim);
  715. /* Disable the Peripheral */
  716. __HAL_TIM_DISABLE(htim);
  717. /* Change the htim state */
  718. htim->State = HAL_TIM_STATE_READY;
  719. /* Return function status */
  720. return HAL_OK;
  721. }
  722. /**
  723. * @}
  724. */
  725. /** @defgroup TIMEx_Exported_Functions_Group3 Timer Complementary PWM functions
  726. * @brief Timer Complementary PWM functions
  727. *
  728. @verbatim
  729. ==============================================================================
  730. ##### Timer Complementary PWM functions #####
  731. ==============================================================================
  732. [..]
  733. This section provides functions allowing to:
  734. (+) Start the Complementary PWM.
  735. (+) Stop the Complementary PWM.
  736. (+) Start the Complementary PWM and enable interrupts.
  737. (+) Stop the Complementary PWM and disable interrupts.
  738. (+) Start the Complementary PWM and enable DMA transfers.
  739. (+) Stop the Complementary PWM and disable DMA transfers.
  740. (+) Start the Complementary Input Capture measurement.
  741. (+) Stop the Complementary Input Capture.
  742. (+) Start the Complementary Input Capture and enable interrupts.
  743. (+) Stop the Complementary Input Capture and disable interrupts.
  744. (+) Start the Complementary Input Capture and enable DMA transfers.
  745. (+) Stop the Complementary Input Capture and disable DMA transfers.
  746. (+) Start the Complementary One Pulse generation.
  747. (+) Stop the Complementary One Pulse.
  748. (+) Start the Complementary One Pulse and enable interrupts.
  749. (+) Stop the Complementary One Pulse and disable interrupts.
  750. @endverbatim
  751. * @{
  752. */
  753. /**
  754. * @brief Starts the PWM signal generation on the complementary output.
  755. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  756. * the configuration information for TIM module.
  757. * @param Channel: TIM Channel to be enabled.
  758. * This parameter can be one of the following values:
  759. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  760. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  761. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  762. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  763. * @retval HAL status
  764. */
  765. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start(TIM_HandleTypeDef *htim, uint32_t Channel)
  766. {
  767. /* Check the parameters */
  768. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  769. /* Enable the complementary PWM output */
  770. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  771. /* Enable the Main Output */
  772. __HAL_TIM_MOE_ENABLE(htim);
  773. /* Enable the Peripheral */
  774. __HAL_TIM_ENABLE(htim);
  775. /* Return function status */
  776. return HAL_OK;
  777. }
  778. /**
  779. * @brief Stops the PWM signal generation on the complementary output.
  780. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  781. * the configuration information for TIM module.
  782. * @param Channel: TIM Channel to be disabled.
  783. * This parameter can be one of the following values:
  784. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  785. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  786. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  787. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  788. * @retval HAL status
  789. */
  790. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop(TIM_HandleTypeDef *htim, uint32_t Channel)
  791. {
  792. /* Check the parameters */
  793. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  794. /* Disable the complementary PWM output */
  795. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  796. /* Disable the Main Output */
  797. __HAL_TIM_MOE_DISABLE(htim);
  798. /* Disable the Peripheral */
  799. __HAL_TIM_DISABLE(htim);
  800. /* Return function status */
  801. return HAL_OK;
  802. }
  803. /**
  804. * @brief Starts the PWM signal generation in interrupt mode on the
  805. * complementary output.
  806. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  807. * the configuration information for TIM module.
  808. * @param Channel: TIM Channel to be disabled.
  809. * This parameter can be one of the following values:
  810. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  811. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  812. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  813. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  814. * @retval HAL status
  815. */
  816. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_IT(TIM_HandleTypeDef *htim, uint32_t Channel)
  817. {
  818. /* Check the parameters */
  819. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  820. switch (Channel)
  821. {
  822. case TIM_CHANNEL_1:
  823. {
  824. /* Enable the TIM Capture/Compare 1 interrupt */
  825. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  826. }
  827. break;
  828. case TIM_CHANNEL_2:
  829. {
  830. /* Enable the TIM Capture/Compare 2 interrupt */
  831. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  832. }
  833. break;
  834. case TIM_CHANNEL_3:
  835. {
  836. /* Enable the TIM Capture/Compare 3 interrupt */
  837. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC3);
  838. }
  839. break;
  840. case TIM_CHANNEL_4:
  841. {
  842. /* Enable the TIM Capture/Compare 4 interrupt */
  843. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC4);
  844. }
  845. break;
  846. default:
  847. break;
  848. }
  849. /* Enable the TIM Break interrupt */
  850. __HAL_TIM_ENABLE_IT(htim, TIM_IT_BREAK);
  851. /* Enable the complementary PWM output */
  852. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  853. /* Enable the Main Output */
  854. __HAL_TIM_MOE_ENABLE(htim);
  855. /* Enable the Peripheral */
  856. __HAL_TIM_ENABLE(htim);
  857. /* Return function status */
  858. return HAL_OK;
  859. }
  860. /**
  861. * @brief Stops the PWM signal generation in interrupt mode on the
  862. * complementary output.
  863. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  864. * the configuration information for TIM module.
  865. * @param Channel: TIM Channel to be disabled.
  866. * This parameter can be one of the following values:
  867. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  868. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  869. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  870. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  871. * @retval HAL status
  872. */
  873. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_IT (TIM_HandleTypeDef *htim, uint32_t Channel)
  874. {
  875. /* Check the parameters */
  876. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  877. switch (Channel)
  878. {
  879. case TIM_CHANNEL_1:
  880. {
  881. /* Disable the TIM Capture/Compare 1 interrupt */
  882. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  883. }
  884. break;
  885. case TIM_CHANNEL_2:
  886. {
  887. /* Disable the TIM Capture/Compare 2 interrupt */
  888. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  889. }
  890. break;
  891. case TIM_CHANNEL_3:
  892. {
  893. /* Disable the TIM Capture/Compare 3 interrupt */
  894. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC3);
  895. }
  896. break;
  897. case TIM_CHANNEL_4:
  898. {
  899. /* Disable the TIM Capture/Compare 3 interrupt */
  900. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC4);
  901. }
  902. break;
  903. default:
  904. break;
  905. }
  906. /* Disable the complementary PWM output */
  907. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  908. /* Disable the TIM Break interrupt (only if no more channel is active) */
  909. if((READ_REG(htim->Instance->CCER) & (TIM_CCER_CC1NE | TIM_CCER_CC2NE | TIM_CCER_CC3NE)) == RESET)
  910. {
  911. __HAL_TIM_DISABLE_IT(htim, TIM_IT_BREAK);
  912. }
  913. /* Disable the Main Output */
  914. __HAL_TIM_MOE_DISABLE(htim);
  915. /* Disable the Peripheral */
  916. __HAL_TIM_DISABLE(htim);
  917. /* Return function status */
  918. return HAL_OK;
  919. }
  920. /**
  921. * @brief Starts the TIM PWM signal generation in DMA mode on the
  922. * complementary output
  923. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  924. * the configuration information for TIM module.
  925. * @param Channel: TIM Channel to be enabled.
  926. * This parameter can be one of the following values:
  927. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  928. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  929. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  930. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  931. * @param pData: The source Buffer address.
  932. * @param Length: The length of data to be transferred from memory to TIM peripheral
  933. * @retval HAL status
  934. */
  935. HAL_StatusTypeDef HAL_TIMEx_PWMN_Start_DMA(TIM_HandleTypeDef *htim, uint32_t Channel, uint32_t *pData, uint16_t Length)
  936. {
  937. /* Check the parameters */
  938. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  939. if((htim->State == HAL_TIM_STATE_BUSY))
  940. {
  941. return HAL_BUSY;
  942. }
  943. else if((htim->State == HAL_TIM_STATE_READY))
  944. {
  945. if(((uint32_t)pData == 0 ) && (Length > 0))
  946. {
  947. return HAL_ERROR;
  948. }
  949. else
  950. {
  951. htim->State = HAL_TIM_STATE_BUSY;
  952. }
  953. }
  954. switch (Channel)
  955. {
  956. case TIM_CHANNEL_1:
  957. {
  958. /* Set the DMA Period elapsed callback */
  959. htim->hdma[TIM_DMA_ID_CC1]->XferCpltCallback = TIM_DMADelayPulseCplt;
  960. /* Set the DMA error callback */
  961. htim->hdma[TIM_DMA_ID_CC1]->XferErrorCallback = TIM_DMAError ;
  962. /* Enable the DMA Stream */
  963. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC1], (uint32_t)pData, (uint32_t)&htim->Instance->CCR1, Length);
  964. /* Enable the TIM Capture/Compare 1 DMA request */
  965. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC1);
  966. }
  967. break;
  968. case TIM_CHANNEL_2:
  969. {
  970. /* Set the DMA Period elapsed callback */
  971. htim->hdma[TIM_DMA_ID_CC2]->XferCpltCallback = TIM_DMADelayPulseCplt;
  972. /* Set the DMA error callback */
  973. htim->hdma[TIM_DMA_ID_CC2]->XferErrorCallback = TIM_DMAError ;
  974. /* Enable the DMA Stream */
  975. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC2], (uint32_t)pData, (uint32_t)&htim->Instance->CCR2, Length);
  976. /* Enable the TIM Capture/Compare 2 DMA request */
  977. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC2);
  978. }
  979. break;
  980. case TIM_CHANNEL_3:
  981. {
  982. /* Set the DMA Period elapsed callback */
  983. htim->hdma[TIM_DMA_ID_CC3]->XferCpltCallback = TIM_DMADelayPulseCplt;
  984. /* Set the DMA error callback */
  985. htim->hdma[TIM_DMA_ID_CC3]->XferErrorCallback = TIM_DMAError ;
  986. /* Enable the DMA Stream */
  987. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC3], (uint32_t)pData, (uint32_t)&htim->Instance->CCR3,Length);
  988. /* Enable the TIM Capture/Compare 3 DMA request */
  989. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC3);
  990. }
  991. break;
  992. case TIM_CHANNEL_4:
  993. {
  994. /* Set the DMA Period elapsed callback */
  995. htim->hdma[TIM_DMA_ID_CC4]->XferCpltCallback = TIM_DMADelayPulseCplt;
  996. /* Set the DMA error callback */
  997. htim->hdma[TIM_DMA_ID_CC4]->XferErrorCallback = TIM_DMAError ;
  998. /* Enable the DMA Stream */
  999. HAL_DMA_Start_IT(htim->hdma[TIM_DMA_ID_CC4], (uint32_t)pData, (uint32_t)&htim->Instance->CCR4, Length);
  1000. /* Enable the TIM Capture/Compare 4 DMA request */
  1001. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_CC4);
  1002. }
  1003. break;
  1004. default:
  1005. break;
  1006. }
  1007. /* Enable the complementary PWM output */
  1008. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_ENABLE);
  1009. /* Enable the Main Output */
  1010. __HAL_TIM_MOE_ENABLE(htim);
  1011. /* Enable the Peripheral */
  1012. __HAL_TIM_ENABLE(htim);
  1013. /* Return function status */
  1014. return HAL_OK;
  1015. }
  1016. /**
  1017. * @brief Stops the TIM PWM signal generation in DMA mode on the complementary
  1018. * output
  1019. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1020. * the configuration information for TIM module.
  1021. * @param Channel: TIM Channel to be disabled.
  1022. * This parameter can be one of the following values:
  1023. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1024. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1025. * @arg TIM_CHANNEL_3: TIM Channel 3 selected
  1026. * @arg TIM_CHANNEL_4: TIM Channel 4 selected
  1027. * @retval HAL status
  1028. */
  1029. HAL_StatusTypeDef HAL_TIMEx_PWMN_Stop_DMA(TIM_HandleTypeDef *htim, uint32_t Channel)
  1030. {
  1031. /* Check the parameters */
  1032. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, Channel));
  1033. switch (Channel)
  1034. {
  1035. case TIM_CHANNEL_1:
  1036. {
  1037. /* Disable the TIM Capture/Compare 1 DMA request */
  1038. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC1);
  1039. }
  1040. break;
  1041. case TIM_CHANNEL_2:
  1042. {
  1043. /* Disable the TIM Capture/Compare 2 DMA request */
  1044. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC2);
  1045. }
  1046. break;
  1047. case TIM_CHANNEL_3:
  1048. {
  1049. /* Disable the TIM Capture/Compare 3 DMA request */
  1050. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC3);
  1051. }
  1052. break;
  1053. case TIM_CHANNEL_4:
  1054. {
  1055. /* Disable the TIM Capture/Compare 4 DMA request */
  1056. __HAL_TIM_DISABLE_DMA(htim, TIM_DMA_CC4);
  1057. }
  1058. break;
  1059. default:
  1060. break;
  1061. }
  1062. /* Disable the complementary PWM output */
  1063. TIM_CCxNChannelCmd(htim->Instance, Channel, TIM_CCxN_DISABLE);
  1064. /* Disable the Main Output */
  1065. __HAL_TIM_MOE_DISABLE(htim);
  1066. /* Disable the Peripheral */
  1067. __HAL_TIM_DISABLE(htim);
  1068. /* Change the htim state */
  1069. htim->State = HAL_TIM_STATE_READY;
  1070. /* Return function status */
  1071. return HAL_OK;
  1072. }
  1073. /**
  1074. * @}
  1075. */
  1076. /** @defgroup TIMEx_Exported_Functions_Group4 Timer Complementary One Pulse functions
  1077. * @brief Timer Complementary One Pulse functions
  1078. *
  1079. @verbatim
  1080. ==============================================================================
  1081. ##### Timer Complementary One Pulse functions #####
  1082. ==============================================================================
  1083. [..]
  1084. This section provides functions allowing to:
  1085. (+) Start the Complementary One Pulse generation.
  1086. (+) Stop the Complementary One Pulse.
  1087. (+) Start the Complementary One Pulse and enable interrupts.
  1088. (+) Stop the Complementary One Pulse and disable interrupts.
  1089. @endverbatim
  1090. * @{
  1091. */
  1092. /**
  1093. * @brief Starts the TIM One Pulse signal generation on the complementary
  1094. * output.
  1095. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1096. * the configuration information for TIM module.
  1097. * @param OutputChannel: TIM Channel to be enabled.
  1098. * This parameter can be one of the following values:
  1099. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1100. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1101. * @retval HAL status
  1102. */
  1103. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1104. {
  1105. /* Check the parameters */
  1106. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1107. /* Enable the complementary One Pulse output */
  1108. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1109. /* Enable the Main Output */
  1110. __HAL_TIM_MOE_ENABLE(htim);
  1111. /* Return function status */
  1112. return HAL_OK;
  1113. }
  1114. /**
  1115. * @brief Stops the TIM One Pulse signal generation on the complementary
  1116. * output.
  1117. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1118. * the configuration information for TIM module.
  1119. * @param OutputChannel: TIM Channel to be disabled.
  1120. * This parameter can be one of the following values:
  1121. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1122. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1123. * @retval HAL status
  1124. */
  1125. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1126. {
  1127. /* Check the parameters */
  1128. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1129. /* Disable the complementary One Pulse output */
  1130. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1131. /* Disable the Main Output */
  1132. __HAL_TIM_MOE_DISABLE(htim);
  1133. /* Disable the Peripheral */
  1134. __HAL_TIM_DISABLE(htim);
  1135. /* Return function status */
  1136. return HAL_OK;
  1137. }
  1138. /**
  1139. * @brief Starts the TIM One Pulse signal generation in interrupt mode on the
  1140. * complementary channel.
  1141. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1142. * the configuration information for TIM module.
  1143. * @param OutputChannel: TIM Channel to be enabled.
  1144. * This parameter can be one of the following values:
  1145. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1146. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1147. * @retval HAL status
  1148. */
  1149. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Start_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1150. {
  1151. /* Check the parameters */
  1152. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1153. /* Enable the TIM Capture/Compare 1 interrupt */
  1154. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC1);
  1155. /* Enable the TIM Capture/Compare 2 interrupt */
  1156. __HAL_TIM_ENABLE_IT(htim, TIM_IT_CC2);
  1157. /* Enable the complementary One Pulse output */
  1158. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_ENABLE);
  1159. /* Enable the Main Output */
  1160. __HAL_TIM_MOE_ENABLE(htim);
  1161. /* Return function status */
  1162. return HAL_OK;
  1163. }
  1164. /**
  1165. * @brief Stops the TIM One Pulse signal generation in interrupt mode on the
  1166. * complementary channel.
  1167. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1168. * the configuration information for TIM module.
  1169. * @param OutputChannel: TIM Channel to be disabled.
  1170. * This parameter can be one of the following values:
  1171. * @arg TIM_CHANNEL_1: TIM Channel 1 selected
  1172. * @arg TIM_CHANNEL_2: TIM Channel 2 selected
  1173. * @retval HAL status
  1174. */
  1175. HAL_StatusTypeDef HAL_TIMEx_OnePulseN_Stop_IT(TIM_HandleTypeDef *htim, uint32_t OutputChannel)
  1176. {
  1177. /* Check the parameters */
  1178. assert_param(IS_TIM_CCXN_INSTANCE(htim->Instance, OutputChannel));
  1179. /* Disable the TIM Capture/Compare 1 interrupt */
  1180. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC1);
  1181. /* Disable the TIM Capture/Compare 2 interrupt */
  1182. __HAL_TIM_DISABLE_IT(htim, TIM_IT_CC2);
  1183. /* Disable the complementary One Pulse output */
  1184. TIM_CCxNChannelCmd(htim->Instance, OutputChannel, TIM_CCxN_DISABLE);
  1185. /* Disable the Main Output */
  1186. __HAL_TIM_MOE_DISABLE(htim);
  1187. /* Disable the Peripheral */
  1188. __HAL_TIM_DISABLE(htim);
  1189. /* Return function status */
  1190. return HAL_OK;
  1191. }
  1192. /**
  1193. * @}
  1194. */
  1195. /** @defgroup TIMEx_Exported_Functions_Group5 Peripheral Control functions
  1196. * @brief Peripheral Control functions
  1197. *
  1198. @verbatim
  1199. ==============================================================================
  1200. ##### Peripheral Control functions #####
  1201. ==============================================================================
  1202. [..]
  1203. This section provides functions allowing to:
  1204. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  1205. (+) Configure External Clock source.
  1206. (+) Configure Complementary channels, break features and dead time.
  1207. (+) Configure Master and the Slave synchronization.
  1208. (+) Configure the commutation event in case of use of the Hall sensor interface.
  1209. (+) Configure the DMA Burst Mode.
  1210. @endverbatim
  1211. * @{
  1212. */
  1213. /**
  1214. * @brief Configure the TIM commutation event sequence.
  1215. * @note This function is mandatory to use the commutation event in order to
  1216. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1217. * the typical use of this feature is with the use of another Timer(interface Timer)
  1218. * configured in Hall sensor interface, this interface Timer will generate the
  1219. * commutation at its TRGO output (connected to Timer used in this function) each time
  1220. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1221. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1222. * the configuration information for TIM module.
  1223. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1224. * This parameter can be one of the following values:
  1225. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1226. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1227. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1228. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1229. * @arg TIM_TS_NONE: No trigger is needed
  1230. * @param CommutationSource: the Commutation Event source.
  1231. * This parameter can be one of the following values:
  1232. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1233. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1234. * @retval HAL status
  1235. */
  1236. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1237. {
  1238. /* Check the parameters */
  1239. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1240. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1241. __HAL_LOCK(htim);
  1242. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1243. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1244. {
  1245. /* Select the Input trigger */
  1246. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1247. htim->Instance->SMCR |= InputTrigger;
  1248. }
  1249. /* Select the Capture Compare preload feature */
  1250. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1251. /* Select the Commutation event source */
  1252. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1253. htim->Instance->CR2 |= CommutationSource;
  1254. __HAL_UNLOCK(htim);
  1255. return HAL_OK;
  1256. }
  1257. /**
  1258. * @brief Configure the TIM commutation event sequence with interrupt.
  1259. * @note This function is mandatory to use the commutation event in order to
  1260. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1261. * the typical use of this feature is with the use of another Timer(interface Timer)
  1262. * configured in Hall sensor interface, this interface Timer will generate the
  1263. * commutation at its TRGO output (connected to Timer used in this function) each time
  1264. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1265. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1266. * the configuration information for TIM module.
  1267. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1268. * This parameter can be one of the following values:
  1269. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1270. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1271. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1272. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1273. * @arg TIM_TS_NONE: No trigger is needed
  1274. * @param CommutationSource: the Commutation Event source.
  1275. * This parameter can be one of the following values:
  1276. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1277. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1278. * @retval HAL status
  1279. */
  1280. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_IT(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1281. {
  1282. /* Check the parameters */
  1283. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1284. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1285. __HAL_LOCK(htim);
  1286. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1287. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1288. {
  1289. /* Select the Input trigger */
  1290. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1291. htim->Instance->SMCR |= InputTrigger;
  1292. }
  1293. /* Select the Capture Compare preload feature */
  1294. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1295. /* Select the Commutation event source */
  1296. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1297. htim->Instance->CR2 |= CommutationSource;
  1298. /* Enable the Commutation Interrupt Request */
  1299. __HAL_TIM_ENABLE_IT(htim, TIM_IT_COM);
  1300. __HAL_UNLOCK(htim);
  1301. return HAL_OK;
  1302. }
  1303. /**
  1304. * @brief Configure the TIM commutation event sequence with DMA.
  1305. * @note This function is mandatory to use the commutation event in order to
  1306. * update the configuration at each commutation detection on the TRGI input of the Timer,
  1307. * the typical use of this feature is with the use of another Timer(interface Timer)
  1308. * configured in Hall sensor interface, this interface Timer will generate the
  1309. * commutation at its TRGO output (connected to Timer used in this function) each time
  1310. * the TI1 of the Interface Timer detect a commutation at its input TI1.
  1311. * @note: The user should configure the DMA in his own software, in This function only the COMDE bit is set
  1312. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1313. * the configuration information for TIM module.
  1314. * @param InputTrigger: the Internal trigger corresponding to the Timer Interfacing with the Hall sensor.
  1315. * This parameter can be one of the following values:
  1316. * @arg TIM_TS_ITR0: Internal trigger 0 selected
  1317. * @arg TIM_TS_ITR1: Internal trigger 1 selected
  1318. * @arg TIM_TS_ITR2: Internal trigger 2 selected
  1319. * @arg TIM_TS_ITR3: Internal trigger 3 selected
  1320. * @arg TIM_TS_NONE: No trigger is needed
  1321. * @param CommutationSource: the Commutation Event source.
  1322. * This parameter can be one of the following values:
  1323. * @arg TIM_COMMUTATION_TRGI: Commutation source is the TRGI of the Interface Timer
  1324. * @arg TIM_COMMUTATION_SOFTWARE: Commutation source is set by software using the COMG bit
  1325. * @retval HAL status
  1326. */
  1327. HAL_StatusTypeDef HAL_TIMEx_ConfigCommutationEvent_DMA(TIM_HandleTypeDef *htim, uint32_t InputTrigger, uint32_t CommutationSource)
  1328. {
  1329. /* Check the parameters */
  1330. assert_param(IS_TIM_ADVANCED_INSTANCE(htim->Instance));
  1331. assert_param(IS_TIM_INTERNAL_TRIGGEREVENT_SELECTION(InputTrigger));
  1332. __HAL_LOCK(htim);
  1333. if ((InputTrigger == TIM_TS_ITR0) || (InputTrigger == TIM_TS_ITR1) ||
  1334. (InputTrigger == TIM_TS_ITR2) || (InputTrigger == TIM_TS_ITR3))
  1335. {
  1336. /* Select the Input trigger */
  1337. htim->Instance->SMCR &= ~TIM_SMCR_TS;
  1338. htim->Instance->SMCR |= InputTrigger;
  1339. }
  1340. /* Select the Capture Compare preload feature */
  1341. htim->Instance->CR2 |= TIM_CR2_CCPC;
  1342. /* Select the Commutation event source */
  1343. htim->Instance->CR2 &= ~TIM_CR2_CCUS;
  1344. htim->Instance->CR2 |= CommutationSource;
  1345. /* Enable the Commutation DMA Request */
  1346. /* Set the DMA Commutation Callback */
  1347. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferCpltCallback = TIMEx_DMACommutationCplt;
  1348. /* Set the DMA error callback */
  1349. htim->hdma[TIM_DMA_ID_COMMUTATION]->XferErrorCallback = TIM_DMAError;
  1350. /* Enable the Commutation DMA Request */
  1351. __HAL_TIM_ENABLE_DMA(htim, TIM_DMA_COM);
  1352. __HAL_UNLOCK(htim);
  1353. return HAL_OK;
  1354. }
  1355. /**
  1356. * @brief Configures the TIM in master mode.
  1357. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1358. * the configuration information for TIM module.
  1359. * @param sMasterConfig: pointer to a TIM_MasterConfigTypeDef structure that
  1360. * contains the selected trigger output (TRGO) and the Master/Slave
  1361. * mode.
  1362. * @retval HAL status
  1363. */
  1364. HAL_StatusTypeDef HAL_TIMEx_MasterConfigSynchronization(TIM_HandleTypeDef *htim, TIM_MasterConfigTypeDef * sMasterConfig)
  1365. {
  1366. /* Check the parameters */
  1367. assert_param(IS_TIM_MASTER_INSTANCE(htim->Instance));
  1368. assert_param(IS_TIM_TRGO_SOURCE(sMasterConfig->MasterOutputTrigger));
  1369. assert_param(IS_TIM_MSM_STATE(sMasterConfig->MasterSlaveMode));
  1370. __HAL_LOCK(htim);
  1371. htim->State = HAL_TIM_STATE_BUSY;
  1372. /* Reset the MMS Bits */
  1373. htim->Instance->CR2 &= ~TIM_CR2_MMS;
  1374. /* Select the TRGO source */
  1375. htim->Instance->CR2 |= sMasterConfig->MasterOutputTrigger;
  1376. /* Reset the MSM Bit */
  1377. htim->Instance->SMCR &= ~TIM_SMCR_MSM;
  1378. /* Set or Reset the MSM Bit */
  1379. htim->Instance->SMCR |= sMasterConfig->MasterSlaveMode;
  1380. htim->State = HAL_TIM_STATE_READY;
  1381. __HAL_UNLOCK(htim);
  1382. return HAL_OK;
  1383. }
  1384. /**
  1385. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1386. * and the AOE(automatic output enable).
  1387. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1388. * the configuration information for TIM module.
  1389. * @param sBreakDeadTimeConfig: pointer to a TIM_ConfigBreakDeadConfig_TypeDef structure that
  1390. * contains the BDTR Register configuration information for the TIM peripheral.
  1391. * @retval HAL status
  1392. */
  1393. HAL_StatusTypeDef HAL_TIMEx_ConfigBreakDeadTime(TIM_HandleTypeDef *htim,
  1394. TIM_BreakDeadTimeConfigTypeDef * sBreakDeadTimeConfig)
  1395. {
  1396. /* Check the parameters */
  1397. assert_param(IS_TIM_CC4_INSTANCE(htim->Instance));
  1398. assert_param(IS_TIM_OSSR_STATE(sBreakDeadTimeConfig->OffStateRunMode));
  1399. assert_param(IS_TIM_OSSI_STATE(sBreakDeadTimeConfig->OffStateIDLEMode));
  1400. assert_param(IS_TIM_LOCK_LEVEL(sBreakDeadTimeConfig->LockLevel));
  1401. assert_param(IS_TIM_BREAK_STATE(sBreakDeadTimeConfig->BreakState));
  1402. assert_param(IS_TIM_BREAK_POLARITY(sBreakDeadTimeConfig->BreakPolarity));
  1403. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(sBreakDeadTimeConfig->AutomaticOutput));
  1404. assert_param(IS_TIM_DEADTIME(sBreakDeadTimeConfig->DeadTime));
  1405. /* Process Locked */
  1406. __HAL_LOCK(htim);
  1407. htim->State = HAL_TIM_STATE_BUSY;
  1408. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1409. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1410. htim->Instance->BDTR = (uint32_t)sBreakDeadTimeConfig->OffStateRunMode |
  1411. sBreakDeadTimeConfig->OffStateIDLEMode |
  1412. sBreakDeadTimeConfig->LockLevel |
  1413. sBreakDeadTimeConfig->DeadTime |
  1414. sBreakDeadTimeConfig->BreakState |
  1415. sBreakDeadTimeConfig->BreakPolarity |
  1416. sBreakDeadTimeConfig->AutomaticOutput;
  1417. htim->State = HAL_TIM_STATE_READY;
  1418. __HAL_UNLOCK(htim);
  1419. return HAL_OK;
  1420. }
  1421. /**
  1422. * @brief Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.
  1423. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1424. * the configuration information for TIM module.
  1425. * @param Remap: specifies the TIM input remapping source.
  1426. * This parameter can be one of the following values:
  1427. * @arg TIM_TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)
  1428. * @arg TIM_TIM2_ETH_PTP: TIM2 ITR1 input is connected to ETH PTP trigger output.
  1429. * @arg TIM_TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF.
  1430. * @arg TIM_TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF.
  1431. * @arg TIM_TIM5_GPIO: TIM5 CH4 input is connected to dedicated Timer pin(default)
  1432. * @arg TIM_TIM5_LSI: TIM5 CH4 input is connected to LSI clock.
  1433. * @arg TIM_TIM5_LSE: TIM5 CH4 input is connected to LSE clock.
  1434. * @arg TIM_TIM5_RTC: TIM5 CH4 input is connected to RTC Output event.
  1435. * @arg TIM_TIM11_GPIO: TIM11 CH4 input is connected to dedicated Timer pin(default)
  1436. * @arg TIM_TIM11_HSE: TIM11 CH4 input is connected to HSE_RTC clock
  1437. * (HSE divided by a programmable prescaler)
  1438. * @retval HAL status
  1439. */
  1440. HAL_StatusTypeDef HAL_TIMEx_RemapConfig(TIM_HandleTypeDef *htim, uint32_t Remap)
  1441. {
  1442. __HAL_LOCK(htim);
  1443. /* Check parameters */
  1444. assert_param(IS_TIM_REMAP_INSTANCE(htim->Instance));
  1445. assert_param(IS_TIM_REMAP(Remap));
  1446. /* Set the Timer remapping configuration */
  1447. htim->Instance->OR = Remap;
  1448. htim->State = HAL_TIM_STATE_READY;
  1449. __HAL_UNLOCK(htim);
  1450. return HAL_OK;
  1451. }
  1452. /**
  1453. * @}
  1454. */
  1455. /** @defgroup TIMEx_Exported_Functions_Group6 Extension Callbacks functions
  1456. * @brief Extension Callbacks functions
  1457. *
  1458. @verbatim
  1459. ==============================================================================
  1460. ##### Extension Callbacks functions #####
  1461. ==============================================================================
  1462. [..]
  1463. This section provides Extension TIM callback functions:
  1464. (+) Timer Commutation callback
  1465. (+) Timer Break callback
  1466. @endverbatim
  1467. * @{
  1468. */
  1469. /**
  1470. * @brief Hall commutation changed callback in non blocking mode
  1471. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1472. * the configuration information for TIM module.
  1473. * @retval None
  1474. */
  1475. __weak void HAL_TIMEx_CommutationCallback(TIM_HandleTypeDef *htim)
  1476. {
  1477. /* NOTE : This function Should not be modified, when the callback is needed,
  1478. the HAL_TIMEx_CommutationCallback could be implemented in the user file
  1479. */
  1480. }
  1481. /**
  1482. * @brief Hall Break detection callback in non blocking mode
  1483. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1484. * the configuration information for TIM module.
  1485. * @retval None
  1486. */
  1487. __weak void HAL_TIMEx_BreakCallback(TIM_HandleTypeDef *htim)
  1488. {
  1489. /* NOTE : This function Should not be modified, when the callback is needed,
  1490. the HAL_TIMEx_BreakCallback could be implemented in the user file
  1491. */
  1492. }
  1493. /**
  1494. * @}
  1495. */
  1496. /** @defgroup TIMEx_Exported_Functions_Group7 Extension Peripheral State functions
  1497. * @brief Extension Peripheral State functions
  1498. *
  1499. @verbatim
  1500. ==============================================================================
  1501. ##### Extension Peripheral State functions #####
  1502. ==============================================================================
  1503. [..]
  1504. This subsection permits to get in run-time the status of the peripheral
  1505. and the data flow.
  1506. @endverbatim
  1507. * @{
  1508. */
  1509. /**
  1510. * @brief Return the TIM Hall Sensor interface state
  1511. * @param htim: pointer to a TIM_HandleTypeDef structure that contains
  1512. * the configuration information for TIM module.
  1513. * @retval HAL state
  1514. */
  1515. HAL_TIM_StateTypeDef HAL_TIMEx_HallSensor_GetState(TIM_HandleTypeDef *htim)
  1516. {
  1517. return htim->State;
  1518. }
  1519. /**
  1520. * @}
  1521. */
  1522. /**
  1523. * @brief TIM DMA Commutation callback.
  1524. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  1525. * the configuration information for the specified DMA module.
  1526. * @retval None
  1527. */
  1528. void TIMEx_DMACommutationCplt(DMA_HandleTypeDef *hdma)
  1529. {
  1530. TIM_HandleTypeDef* htim = ( TIM_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  1531. htim->State= HAL_TIM_STATE_READY;
  1532. HAL_TIMEx_CommutationCallback(htim);
  1533. }
  1534. /**
  1535. * @}
  1536. */
  1537. /**
  1538. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1539. * @param TIMx to select the TIM peripheral
  1540. * @param Channel: specifies the TIM Channel
  1541. * This parameter can be one of the following values:
  1542. * @arg TIM_Channel_1: TIM Channel 1
  1543. * @arg TIM_Channel_2: TIM Channel 2
  1544. * @arg TIM_Channel_3: TIM Channel 3
  1545. * @param ChannelNState: specifies the TIM Channel CCxNE bit new state.
  1546. * This parameter can be: TIM_CCxN_ENABLE or TIM_CCxN_Disable.
  1547. * @retval None
  1548. */
  1549. static void TIM_CCxNChannelCmd(TIM_TypeDef* TIMx, uint32_t Channel, uint32_t ChannelNState)
  1550. {
  1551. uint32_t tmp = 0;
  1552. /* Check the parameters */
  1553. assert_param(IS_TIM_CC4_INSTANCE(TIMx));
  1554. assert_param(IS_TIM_COMPLEMENTARY_CHANNELS(Channel));
  1555. tmp = TIM_CCER_CC1NE << Channel;
  1556. /* Reset the CCxNE Bit */
  1557. TIMx->CCER &= ~tmp;
  1558. /* Set or reset the CCxNE Bit */
  1559. TIMx->CCER |= (uint32_t)(ChannelNState << Channel);
  1560. }
  1561. /**
  1562. * @}
  1563. */
  1564. #endif /* HAL_TIM_MODULE_ENABLED */
  1565. /**
  1566. * @}
  1567. */
  1568. /**
  1569. * @}
  1570. */
  1571. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/