stm32f1xx_hal_tim_ex.c 61 KB

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