stm32f4xx_tim.c 122 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_tim.c
  4. * @author MCD Application Team
  5. * @version V1.0.0
  6. * @date 30-September-2011
  7. * @brief This file provides firmware functions to manage the following
  8. * functionalities of the TIM peripheral:
  9. * - TimeBase management
  10. * - Output Compare management
  11. * - Input Capture management
  12. * - Advanced-control timers (TIM1 and TIM8) specific features
  13. * - Interrupts, DMA and flags management
  14. * - Clocks management
  15. * - Synchronization management
  16. * - Specific interface management
  17. * - Specific remapping management
  18. *
  19. * @verbatim
  20. *
  21. * ===================================================================
  22. * How to use this driver
  23. * ===================================================================
  24. * This driver provides functions to configure and program the TIM
  25. * of all STM32F4xx devices.
  26. * These functions are split in 9 groups:
  27. *
  28. * 1. TIM TimeBase management: this group includes all needed functions
  29. * to configure the TM Timebase unit:
  30. * - Set/Get Prescaler
  31. * - Set/Get Autoreload
  32. * - Counter modes configuration
  33. * - Set Clock division
  34. * - Select the One Pulse mode
  35. * - Update Request Configuration
  36. * - Update Disable Configuration
  37. * - Auto-Preload Configuration
  38. * - Enable/Disable the counter
  39. *
  40. * 2. TIM Output Compare management: this group includes all needed
  41. * functions to configure the Capture/Compare unit used in Output
  42. * compare mode:
  43. * - Configure each channel, independently, in Output Compare mode
  44. * - Select the output compare modes
  45. * - Select the Polarities of each channel
  46. * - Set/Get the Capture/Compare register values
  47. * - Select the Output Compare Fast mode
  48. * - Select the Output Compare Forced mode
  49. * - Output Compare-Preload Configuration
  50. * - Clear Output Compare Reference
  51. * - Select the OCREF Clear signal
  52. * - Enable/Disable the Capture/Compare Channels
  53. *
  54. * 3. TIM Input Capture management: this group includes all needed
  55. * functions to configure the Capture/Compare unit used in
  56. * Input Capture mode:
  57. * - Configure each channel in input capture mode
  58. * - Configure Channel1/2 in PWM Input mode
  59. * - Set the Input Capture Prescaler
  60. * - Get the Capture/Compare values
  61. *
  62. * 4. Advanced-control timers (TIM1 and TIM8) specific features
  63. * - Configures the Break input, dead time, Lock level, the OSSI,
  64. * the OSSR State and the AOE(automatic output enable)
  65. * - Enable/Disable the TIM peripheral Main Outputs
  66. * - Select the Commutation event
  67. * - Set/Reset the Capture Compare Preload Control bit
  68. *
  69. * 5. TIM interrupts, DMA and flags management
  70. * - Enable/Disable interrupt sources
  71. * - Get flags status
  72. * - Clear flags/ Pending bits
  73. * - Enable/Disable DMA requests
  74. * - Configure DMA burst mode
  75. * - Select CaptureCompare DMA request
  76. *
  77. * 6. TIM clocks management: this group includes all needed functions
  78. * to configure the clock controller unit:
  79. * - Select internal/External clock
  80. * - Select the external clock mode: ETR(Mode1/Mode2), TIx or ITRx
  81. *
  82. * 7. TIM synchronization management: this group includes all needed
  83. * functions to configure the Synchronization unit:
  84. * - Select Input Trigger
  85. * - Select Output Trigger
  86. * - Select Master Slave Mode
  87. * - ETR Configuration when used as external trigger
  88. *
  89. * 8. TIM specific interface management, this group includes all
  90. * needed functions to use the specific TIM interface:
  91. * - Encoder Interface Configuration
  92. * - Select Hall Sensor
  93. *
  94. * 9. TIM specific remapping management includes the Remapping
  95. * configuration of specific timers
  96. *
  97. * @endverbatim
  98. *
  99. ******************************************************************************
  100. * @attention
  101. *
  102. * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  103. * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  104. * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  105. * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  106. * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  107. * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  108. *
  109. * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  110. ******************************************************************************
  111. */
  112. /* Includes ------------------------------------------------------------------*/
  113. #include "stm32f4xx_tim.h"
  114. #include "stm32f4xx_rcc.h"
  115. /** @addtogroup STM32F4xx_StdPeriph_Driver
  116. * @{
  117. */
  118. /** @defgroup TIM
  119. * @brief TIM driver modules
  120. * @{
  121. */
  122. /* Private typedef -----------------------------------------------------------*/
  123. /* Private define ------------------------------------------------------------*/
  124. /* ---------------------- TIM registers bit mask ------------------------ */
  125. #define SMCR_ETR_MASK ((uint16_t)0x00FF)
  126. #define CCMR_OFFSET ((uint16_t)0x0018)
  127. #define CCER_CCE_SET ((uint16_t)0x0001)
  128. #define CCER_CCNE_SET ((uint16_t)0x0004)
  129. #define CCMR_OC13M_MASK ((uint16_t)0xFF8F)
  130. #define CCMR_OC24M_MASK ((uint16_t)0x8FFF)
  131. /* Private macro -------------------------------------------------------------*/
  132. /* Private variables ---------------------------------------------------------*/
  133. /* Private function prototypes -----------------------------------------------*/
  134. static void TI1_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  135. uint16_t TIM_ICFilter);
  136. static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  137. uint16_t TIM_ICFilter);
  138. static void TI3_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  139. uint16_t TIM_ICFilter);
  140. static void TI4_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  141. uint16_t TIM_ICFilter);
  142. /* Private functions ---------------------------------------------------------*/
  143. /** @defgroup TIM_Private_Functions
  144. * @{
  145. */
  146. /** @defgroup TIM_Group1 TimeBase management functions
  147. * @brief TimeBase management functions
  148. *
  149. @verbatim
  150. ===============================================================================
  151. TimeBase management functions
  152. ===============================================================================
  153. ===================================================================
  154. TIM Driver: how to use it in Timing(Time base) Mode
  155. ===================================================================
  156. To use the Timer in Timing(Time base) mode, the following steps are mandatory:
  157. 1. Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE) function
  158. 2. Fill the TIM_TimeBaseInitStruct with the desired parameters.
  159. 3. Call TIM_TimeBaseInit(TIMx, &TIM_TimeBaseInitStruct) to configure the Time Base unit
  160. with the corresponding configuration
  161. 4. Enable the NVIC if you need to generate the update interrupt.
  162. 5. Enable the corresponding interrupt using the function TIM_ITConfig(TIMx, TIM_IT_Update)
  163. 6. Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  164. Note1: All other functions can be used separately to modify, if needed,
  165. a specific feature of the Timer.
  166. @endverbatim
  167. * @{
  168. */
  169. /**
  170. * @brief Deinitializes the TIMx peripheral registers to their default reset values.
  171. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  172. * @retval None
  173. */
  174. void TIM_DeInit(TIM_TypeDef* TIMx)
  175. {
  176. /* Check the parameters */
  177. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  178. if (TIMx == TIM1)
  179. {
  180. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, ENABLE);
  181. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM1, DISABLE);
  182. }
  183. else if (TIMx == TIM2)
  184. {
  185. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, ENABLE);
  186. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM2, DISABLE);
  187. }
  188. else if (TIMx == TIM3)
  189. {
  190. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, ENABLE);
  191. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM3, DISABLE);
  192. }
  193. else if (TIMx == TIM4)
  194. {
  195. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, ENABLE);
  196. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM4, DISABLE);
  197. }
  198. else if (TIMx == TIM5)
  199. {
  200. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM5, ENABLE);
  201. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM5, DISABLE);
  202. }
  203. else if (TIMx == TIM6)
  204. {
  205. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM6, ENABLE);
  206. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM6, DISABLE);
  207. }
  208. else if (TIMx == TIM7)
  209. {
  210. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM7, ENABLE);
  211. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM7, DISABLE);
  212. }
  213. else if (TIMx == TIM8)
  214. {
  215. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM8, ENABLE);
  216. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM8, DISABLE);
  217. }
  218. else if (TIMx == TIM9)
  219. {
  220. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM9, ENABLE);
  221. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM9, DISABLE);
  222. }
  223. else if (TIMx == TIM10)
  224. {
  225. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM10, ENABLE);
  226. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM10, DISABLE);
  227. }
  228. else if (TIMx == TIM11)
  229. {
  230. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM11, ENABLE);
  231. RCC_APB2PeriphResetCmd(RCC_APB2Periph_TIM11, DISABLE);
  232. }
  233. else if (TIMx == TIM12)
  234. {
  235. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM12, ENABLE);
  236. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM12, DISABLE);
  237. }
  238. else if (TIMx == TIM13)
  239. {
  240. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM13, ENABLE);
  241. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM13, DISABLE);
  242. }
  243. else
  244. {
  245. if (TIMx == TIM14)
  246. {
  247. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM14, ENABLE);
  248. RCC_APB1PeriphResetCmd(RCC_APB1Periph_TIM14, DISABLE);
  249. }
  250. }
  251. }
  252. /**
  253. * @brief Initializes the TIMx Time Base Unit peripheral according to
  254. * the specified parameters in the TIM_TimeBaseInitStruct.
  255. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  256. * @param TIM_TimeBaseInitStruct: pointer to a TIM_TimeBaseInitTypeDef structure
  257. * that contains the configuration information for the specified TIM peripheral.
  258. * @retval None
  259. */
  260. void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
  261. {
  262. uint16_t tmpcr1 = 0;
  263. /* Check the parameters */
  264. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  265. assert_param(IS_TIM_COUNTER_MODE(TIM_TimeBaseInitStruct->TIM_CounterMode));
  266. assert_param(IS_TIM_CKD_DIV(TIM_TimeBaseInitStruct->TIM_ClockDivision));
  267. tmpcr1 = TIMx->CR1;
  268. if((TIMx == TIM1) || (TIMx == TIM8)||
  269. (TIMx == TIM2) || (TIMx == TIM3)||
  270. (TIMx == TIM4) || (TIMx == TIM5))
  271. {
  272. /* Select the Counter Mode */
  273. tmpcr1 &= (uint16_t)(~(TIM_CR1_DIR | TIM_CR1_CMS));
  274. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode;
  275. }
  276. if((TIMx != TIM6) && (TIMx != TIM7))
  277. {
  278. /* Set the clock division */
  279. tmpcr1 &= (uint16_t)(~TIM_CR1_CKD);
  280. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision;
  281. }
  282. TIMx->CR1 = tmpcr1;
  283. /* Set the Autoreload value */
  284. TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;
  285. /* Set the Prescaler value */
  286. TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
  287. if ((TIMx == TIM1) || (TIMx == TIM8))
  288. {
  289. /* Set the Repetition Counter value */
  290. TIMx->RCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
  291. }
  292. /* Generate an update event to reload the Prescaler
  293. and the repetition counter(only for TIM1 and TIM8) value immediatly */
  294. TIMx->EGR = TIM_PSCReloadMode_Immediate;
  295. }
  296. /**
  297. * @brief Fills each TIM_TimeBaseInitStruct member with its default value.
  298. * @param TIM_TimeBaseInitStruct : pointer to a TIM_TimeBaseInitTypeDef
  299. * structure which will be initialized.
  300. * @retval None
  301. */
  302. void TIM_TimeBaseStructInit(TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
  303. {
  304. /* Set the default configuration */
  305. TIM_TimeBaseInitStruct->TIM_Period = 0xFFFFFFFF;
  306. TIM_TimeBaseInitStruct->TIM_Prescaler = 0x0000;
  307. TIM_TimeBaseInitStruct->TIM_ClockDivision = TIM_CKD_DIV1;
  308. TIM_TimeBaseInitStruct->TIM_CounterMode = TIM_CounterMode_Up;
  309. TIM_TimeBaseInitStruct->TIM_RepetitionCounter = 0x0000;
  310. }
  311. /**
  312. * @brief Configures the TIMx Prescaler.
  313. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  314. * @param Prescaler: specifies the Prescaler Register value
  315. * @param TIM_PSCReloadMode: specifies the TIM Prescaler Reload mode
  316. * This parameter can be one of the following values:
  317. * @arg TIM_PSCReloadMode_Update: The Prescaler is loaded at the update event.
  318. * @arg TIM_PSCReloadMode_Immediate: The Prescaler is loaded immediatly.
  319. * @retval None
  320. */
  321. void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
  322. {
  323. /* Check the parameters */
  324. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  325. assert_param(IS_TIM_PRESCALER_RELOAD(TIM_PSCReloadMode));
  326. /* Set the Prescaler value */
  327. TIMx->PSC = Prescaler;
  328. /* Set or reset the UG Bit */
  329. TIMx->EGR = TIM_PSCReloadMode;
  330. }
  331. /**
  332. * @brief Specifies the TIMx Counter Mode to be used.
  333. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  334. * @param TIM_CounterMode: specifies the Counter Mode to be used
  335. * This parameter can be one of the following values:
  336. * @arg TIM_CounterMode_Up: TIM Up Counting Mode
  337. * @arg TIM_CounterMode_Down: TIM Down Counting Mode
  338. * @arg TIM_CounterMode_CenterAligned1: TIM Center Aligned Mode1
  339. * @arg TIM_CounterMode_CenterAligned2: TIM Center Aligned Mode2
  340. * @arg TIM_CounterMode_CenterAligned3: TIM Center Aligned Mode3
  341. * @retval None
  342. */
  343. void TIM_CounterModeConfig(TIM_TypeDef* TIMx, uint16_t TIM_CounterMode)
  344. {
  345. uint16_t tmpcr1 = 0;
  346. /* Check the parameters */
  347. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  348. assert_param(IS_TIM_COUNTER_MODE(TIM_CounterMode));
  349. tmpcr1 = TIMx->CR1;
  350. /* Reset the CMS and DIR Bits */
  351. tmpcr1 &= (uint16_t)~(TIM_CR1_DIR | TIM_CR1_CMS);
  352. /* Set the Counter Mode */
  353. tmpcr1 |= TIM_CounterMode;
  354. /* Write to TIMx CR1 register */
  355. TIMx->CR1 = tmpcr1;
  356. }
  357. /**
  358. * @brief Sets the TIMx Counter Register value
  359. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  360. * @param Counter: specifies the Counter register new value.
  361. * @retval None
  362. */
  363. void TIM_SetCounter(TIM_TypeDef* TIMx, uint32_t Counter)
  364. {
  365. /* Check the parameters */
  366. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  367. /* Set the Counter Register value */
  368. TIMx->CNT = Counter;
  369. }
  370. /**
  371. * @brief Sets the TIMx Autoreload Register value
  372. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  373. * @param Autoreload: specifies the Autoreload register new value.
  374. * @retval None
  375. */
  376. void TIM_SetAutoreload(TIM_TypeDef* TIMx, uint32_t Autoreload)
  377. {
  378. /* Check the parameters */
  379. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  380. /* Set the Autoreload Register value */
  381. TIMx->ARR = Autoreload;
  382. }
  383. /**
  384. * @brief Gets the TIMx Counter value.
  385. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  386. * @retval Counter Register value
  387. */
  388. uint32_t TIM_GetCounter(TIM_TypeDef* TIMx)
  389. {
  390. /* Check the parameters */
  391. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  392. /* Get the Counter Register value */
  393. return TIMx->CNT;
  394. }
  395. /**
  396. * @brief Gets the TIMx Prescaler value.
  397. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  398. * @retval Prescaler Register value.
  399. */
  400. uint16_t TIM_GetPrescaler(TIM_TypeDef* TIMx)
  401. {
  402. /* Check the parameters */
  403. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  404. /* Get the Prescaler Register value */
  405. return TIMx->PSC;
  406. }
  407. /**
  408. * @brief Enables or Disables the TIMx Update event.
  409. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  410. * @param NewState: new state of the TIMx UDIS bit
  411. * This parameter can be: ENABLE or DISABLE.
  412. * @retval None
  413. */
  414. void TIM_UpdateDisableConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
  415. {
  416. /* Check the parameters */
  417. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  418. assert_param(IS_FUNCTIONAL_STATE(NewState));
  419. if (NewState != DISABLE)
  420. {
  421. /* Set the Update Disable Bit */
  422. TIMx->CR1 |= TIM_CR1_UDIS;
  423. }
  424. else
  425. {
  426. /* Reset the Update Disable Bit */
  427. TIMx->CR1 &= (uint16_t)~TIM_CR1_UDIS;
  428. }
  429. }
  430. /**
  431. * @brief Configures the TIMx Update Request Interrupt source.
  432. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  433. * @param TIM_UpdateSource: specifies the Update source.
  434. * This parameter can be one of the following values:
  435. * @arg TIM_UpdateSource_Global: Source of update is the counter
  436. * overflow/underflow or the setting of UG bit, or an update
  437. * generation through the slave mode controller.
  438. * @arg TIM_UpdateSource_Regular: Source of update is counter overflow/underflow.
  439. * @retval None
  440. */
  441. void TIM_UpdateRequestConfig(TIM_TypeDef* TIMx, uint16_t TIM_UpdateSource)
  442. {
  443. /* Check the parameters */
  444. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  445. assert_param(IS_TIM_UPDATE_SOURCE(TIM_UpdateSource));
  446. if (TIM_UpdateSource != TIM_UpdateSource_Global)
  447. {
  448. /* Set the URS Bit */
  449. TIMx->CR1 |= TIM_CR1_URS;
  450. }
  451. else
  452. {
  453. /* Reset the URS Bit */
  454. TIMx->CR1 &= (uint16_t)~TIM_CR1_URS;
  455. }
  456. }
  457. /**
  458. * @brief Enables or disables TIMx peripheral Preload register on ARR.
  459. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  460. * @param NewState: new state of the TIMx peripheral Preload register
  461. * This parameter can be: ENABLE or DISABLE.
  462. * @retval None
  463. */
  464. void TIM_ARRPreloadConfig(TIM_TypeDef* TIMx, FunctionalState NewState)
  465. {
  466. /* Check the parameters */
  467. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  468. assert_param(IS_FUNCTIONAL_STATE(NewState));
  469. if (NewState != DISABLE)
  470. {
  471. /* Set the ARR Preload Bit */
  472. TIMx->CR1 |= TIM_CR1_ARPE;
  473. }
  474. else
  475. {
  476. /* Reset the ARR Preload Bit */
  477. TIMx->CR1 &= (uint16_t)~TIM_CR1_ARPE;
  478. }
  479. }
  480. /**
  481. * @brief Selects the TIMx's One Pulse Mode.
  482. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  483. * @param TIM_OPMode: specifies the OPM Mode to be used.
  484. * This parameter can be one of the following values:
  485. * @arg TIM_OPMode_Single
  486. * @arg TIM_OPMode_Repetitive
  487. * @retval None
  488. */
  489. void TIM_SelectOnePulseMode(TIM_TypeDef* TIMx, uint16_t TIM_OPMode)
  490. {
  491. /* Check the parameters */
  492. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  493. assert_param(IS_TIM_OPM_MODE(TIM_OPMode));
  494. /* Reset the OPM Bit */
  495. TIMx->CR1 &= (uint16_t)~TIM_CR1_OPM;
  496. /* Configure the OPM Mode */
  497. TIMx->CR1 |= TIM_OPMode;
  498. }
  499. /**
  500. * @brief Sets the TIMx Clock Division value.
  501. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  502. * @param TIM_CKD: specifies the clock division value.
  503. * This parameter can be one of the following value:
  504. * @arg TIM_CKD_DIV1: TDTS = Tck_tim
  505. * @arg TIM_CKD_DIV2: TDTS = 2*Tck_tim
  506. * @arg TIM_CKD_DIV4: TDTS = 4*Tck_tim
  507. * @retval None
  508. */
  509. void TIM_SetClockDivision(TIM_TypeDef* TIMx, uint16_t TIM_CKD)
  510. {
  511. /* Check the parameters */
  512. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  513. assert_param(IS_TIM_CKD_DIV(TIM_CKD));
  514. /* Reset the CKD Bits */
  515. TIMx->CR1 &= (uint16_t)(~TIM_CR1_CKD);
  516. /* Set the CKD value */
  517. TIMx->CR1 |= TIM_CKD;
  518. }
  519. /**
  520. * @brief Enables or disables the specified TIM peripheral.
  521. * @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
  522. * @param NewState: new state of the TIMx peripheral.
  523. * This parameter can be: ENABLE or DISABLE.
  524. * @retval None
  525. */
  526. void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)
  527. {
  528. /* Check the parameters */
  529. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  530. assert_param(IS_FUNCTIONAL_STATE(NewState));
  531. if (NewState != DISABLE)
  532. {
  533. /* Enable the TIM Counter */
  534. TIMx->CR1 |= TIM_CR1_CEN;
  535. }
  536. else
  537. {
  538. /* Disable the TIM Counter */
  539. TIMx->CR1 &= (uint16_t)~TIM_CR1_CEN;
  540. }
  541. }
  542. /**
  543. * @}
  544. */
  545. /** @defgroup TIM_Group2 Output Compare management functions
  546. * @brief Output Compare management functions
  547. *
  548. @verbatim
  549. ===============================================================================
  550. Output Compare management functions
  551. ===============================================================================
  552. ===================================================================
  553. TIM Driver: how to use it in Output Compare Mode
  554. ===================================================================
  555. To use the Timer in Output Compare mode, the following steps are mandatory:
  556. 1. Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE) function
  557. 2. Configure the TIM pins by configuring the corresponding GPIO pins
  558. 2. Configure the Time base unit as described in the first part of this driver,
  559. if needed, else the Timer will run with the default configuration:
  560. - Autoreload value = 0xFFFF
  561. - Prescaler value = 0x0000
  562. - Counter mode = Up counting
  563. - Clock Division = TIM_CKD_DIV1
  564. 3. Fill the TIM_OCInitStruct with the desired parameters including:
  565. - The TIM Output Compare mode: TIM_OCMode
  566. - TIM Output State: TIM_OutputState
  567. - TIM Pulse value: TIM_Pulse
  568. - TIM Output Compare Polarity : TIM_OCPolarity
  569. 4. Call TIM_OCxInit(TIMx, &TIM_OCInitStruct) to configure the desired channel with the
  570. corresponding configuration
  571. 5. Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  572. Note1: All other functions can be used separately to modify, if needed,
  573. a specific feature of the Timer.
  574. Note2: In case of PWM mode, this function is mandatory:
  575. TIM_OCxPreloadConfig(TIMx, TIM_OCPreload_ENABLE);
  576. Note3: If the corresponding interrupt or DMA request are needed, the user should:
  577. 1. Enable the NVIC (or the DMA) to use the TIM interrupts (or DMA requests).
  578. 2. Enable the corresponding interrupt (or DMA request) using the function
  579. TIM_ITConfig(TIMx, TIM_IT_CCx) (or TIM_DMA_Cmd(TIMx, TIM_DMA_CCx))
  580. @endverbatim
  581. * @{
  582. */
  583. /**
  584. * @brief Initializes the TIMx Channel1 according to the specified parameters in
  585. * the TIM_OCInitStruct.
  586. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  587. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  588. * the configuration information for the specified TIM peripheral.
  589. * @retval None
  590. */
  591. void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  592. {
  593. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  594. /* Check the parameters */
  595. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  596. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  597. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  598. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  599. /* Disable the Channel 1: Reset the CC1E Bit */
  600. TIMx->CCER &= (uint16_t)~TIM_CCER_CC1E;
  601. /* Get the TIMx CCER register value */
  602. tmpccer = TIMx->CCER;
  603. /* Get the TIMx CR2 register value */
  604. tmpcr2 = TIMx->CR2;
  605. /* Get the TIMx CCMR1 register value */
  606. tmpccmrx = TIMx->CCMR1;
  607. /* Reset the Output Compare Mode Bits */
  608. tmpccmrx &= (uint16_t)~TIM_CCMR1_OC1M;
  609. tmpccmrx &= (uint16_t)~TIM_CCMR1_CC1S;
  610. /* Select the Output Compare Mode */
  611. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  612. /* Reset the Output Polarity level */
  613. tmpccer &= (uint16_t)~TIM_CCER_CC1P;
  614. /* Set the Output Compare Polarity */
  615. tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
  616. /* Set the Output State */
  617. tmpccer |= TIM_OCInitStruct->TIM_OutputState;
  618. if((TIMx == TIM1) || (TIMx == TIM8))
  619. {
  620. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  621. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  622. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  623. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  624. /* Reset the Output N Polarity level */
  625. tmpccer &= (uint16_t)~TIM_CCER_CC1NP;
  626. /* Set the Output N Polarity */
  627. tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
  628. /* Reset the Output N State */
  629. tmpccer &= (uint16_t)~TIM_CCER_CC1NE;
  630. /* Set the Output N State */
  631. tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
  632. /* Reset the Output Compare and Output Compare N IDLE State */
  633. tmpcr2 &= (uint16_t)~TIM_CR2_OIS1;
  634. tmpcr2 &= (uint16_t)~TIM_CR2_OIS1N;
  635. /* Set the Output Idle state */
  636. tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
  637. /* Set the Output N Idle state */
  638. tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
  639. }
  640. /* Write to TIMx CR2 */
  641. TIMx->CR2 = tmpcr2;
  642. /* Write to TIMx CCMR1 */
  643. TIMx->CCMR1 = tmpccmrx;
  644. /* Set the Capture Compare Register value */
  645. TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse;
  646. /* Write to TIMx CCER */
  647. TIMx->CCER = tmpccer;
  648. }
  649. /**
  650. * @brief Initializes the TIMx Channel2 according to the specified parameters
  651. * in the TIM_OCInitStruct.
  652. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  653. * peripheral.
  654. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  655. * the configuration information for the specified TIM peripheral.
  656. * @retval None
  657. */
  658. void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  659. {
  660. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  661. /* Check the parameters */
  662. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  663. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  664. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  665. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  666. /* Disable the Channel 2: Reset the CC2E Bit */
  667. TIMx->CCER &= (uint16_t)~TIM_CCER_CC2E;
  668. /* Get the TIMx CCER register value */
  669. tmpccer = TIMx->CCER;
  670. /* Get the TIMx CR2 register value */
  671. tmpcr2 = TIMx->CR2;
  672. /* Get the TIMx CCMR1 register value */
  673. tmpccmrx = TIMx->CCMR1;
  674. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  675. tmpccmrx &= (uint16_t)~TIM_CCMR1_OC2M;
  676. tmpccmrx &= (uint16_t)~TIM_CCMR1_CC2S;
  677. /* Select the Output Compare Mode */
  678. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  679. /* Reset the Output Polarity level */
  680. tmpccer &= (uint16_t)~TIM_CCER_CC2P;
  681. /* Set the Output Compare Polarity */
  682. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 4);
  683. /* Set the Output State */
  684. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 4);
  685. if((TIMx == TIM1) || (TIMx == TIM8))
  686. {
  687. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  688. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  689. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  690. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  691. /* Reset the Output N Polarity level */
  692. tmpccer &= (uint16_t)~TIM_CCER_CC2NP;
  693. /* Set the Output N Polarity */
  694. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 4);
  695. /* Reset the Output N State */
  696. tmpccer &= (uint16_t)~TIM_CCER_CC2NE;
  697. /* Set the Output N State */
  698. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 4);
  699. /* Reset the Output Compare and Output Compare N IDLE State */
  700. tmpcr2 &= (uint16_t)~TIM_CR2_OIS2;
  701. tmpcr2 &= (uint16_t)~TIM_CR2_OIS2N;
  702. /* Set the Output Idle state */
  703. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 2);
  704. /* Set the Output N Idle state */
  705. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 2);
  706. }
  707. /* Write to TIMx CR2 */
  708. TIMx->CR2 = tmpcr2;
  709. /* Write to TIMx CCMR1 */
  710. TIMx->CCMR1 = tmpccmrx;
  711. /* Set the Capture Compare Register value */
  712. TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse;
  713. /* Write to TIMx CCER */
  714. TIMx->CCER = tmpccer;
  715. }
  716. /**
  717. * @brief Initializes the TIMx Channel3 according to the specified parameters
  718. * in the TIM_OCInitStruct.
  719. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  720. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  721. * the configuration information for the specified TIM peripheral.
  722. * @retval None
  723. */
  724. void TIM_OC3Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  725. {
  726. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  727. /* Check the parameters */
  728. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  729. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  730. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  731. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  732. /* Disable the Channel 3: Reset the CC2E Bit */
  733. TIMx->CCER &= (uint16_t)~TIM_CCER_CC3E;
  734. /* Get the TIMx CCER register value */
  735. tmpccer = TIMx->CCER;
  736. /* Get the TIMx CR2 register value */
  737. tmpcr2 = TIMx->CR2;
  738. /* Get the TIMx CCMR2 register value */
  739. tmpccmrx = TIMx->CCMR2;
  740. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  741. tmpccmrx &= (uint16_t)~TIM_CCMR2_OC3M;
  742. tmpccmrx &= (uint16_t)~TIM_CCMR2_CC3S;
  743. /* Select the Output Compare Mode */
  744. tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  745. /* Reset the Output Polarity level */
  746. tmpccer &= (uint16_t)~TIM_CCER_CC3P;
  747. /* Set the Output Compare Polarity */
  748. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 8);
  749. /* Set the Output State */
  750. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 8);
  751. if((TIMx == TIM1) || (TIMx == TIM8))
  752. {
  753. assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
  754. assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
  755. assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
  756. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  757. /* Reset the Output N Polarity level */
  758. tmpccer &= (uint16_t)~TIM_CCER_CC3NP;
  759. /* Set the Output N Polarity */
  760. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCNPolarity << 8);
  761. /* Reset the Output N State */
  762. tmpccer &= (uint16_t)~TIM_CCER_CC3NE;
  763. /* Set the Output N State */
  764. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputNState << 8);
  765. /* Reset the Output Compare and Output Compare N IDLE State */
  766. tmpcr2 &= (uint16_t)~TIM_CR2_OIS3;
  767. tmpcr2 &= (uint16_t)~TIM_CR2_OIS3N;
  768. /* Set the Output Idle state */
  769. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 4);
  770. /* Set the Output N Idle state */
  771. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCNIdleState << 4);
  772. }
  773. /* Write to TIMx CR2 */
  774. TIMx->CR2 = tmpcr2;
  775. /* Write to TIMx CCMR2 */
  776. TIMx->CCMR2 = tmpccmrx;
  777. /* Set the Capture Compare Register value */
  778. TIMx->CCR3 = TIM_OCInitStruct->TIM_Pulse;
  779. /* Write to TIMx CCER */
  780. TIMx->CCER = tmpccer;
  781. }
  782. /**
  783. * @brief Initializes the TIMx Channel4 according to the specified parameters
  784. * in the TIM_OCInitStruct.
  785. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  786. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure that contains
  787. * the configuration information for the specified TIM peripheral.
  788. * @retval None
  789. */
  790. void TIM_OC4Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
  791. {
  792. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  793. /* Check the parameters */
  794. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  795. assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  796. assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  797. assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));
  798. /* Disable the Channel 4: Reset the CC4E Bit */
  799. TIMx->CCER &= (uint16_t)~TIM_CCER_CC4E;
  800. /* Get the TIMx CCER register value */
  801. tmpccer = TIMx->CCER;
  802. /* Get the TIMx CR2 register value */
  803. tmpcr2 = TIMx->CR2;
  804. /* Get the TIMx CCMR2 register value */
  805. tmpccmrx = TIMx->CCMR2;
  806. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  807. tmpccmrx &= (uint16_t)~TIM_CCMR2_OC4M;
  808. tmpccmrx &= (uint16_t)~TIM_CCMR2_CC4S;
  809. /* Select the Output Compare Mode */
  810. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->TIM_OCMode << 8);
  811. /* Reset the Output Polarity level */
  812. tmpccer &= (uint16_t)~TIM_CCER_CC4P;
  813. /* Set the Output Compare Polarity */
  814. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OCPolarity << 12);
  815. /* Set the Output State */
  816. tmpccer |= (uint16_t)(TIM_OCInitStruct->TIM_OutputState << 12);
  817. if((TIMx == TIM1) || (TIMx == TIM8))
  818. {
  819. assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
  820. /* Reset the Output Compare IDLE State */
  821. tmpcr2 &=(uint16_t) ~TIM_CR2_OIS4;
  822. /* Set the Output Idle state */
  823. tmpcr2 |= (uint16_t)(TIM_OCInitStruct->TIM_OCIdleState << 6);
  824. }
  825. /* Write to TIMx CR2 */
  826. TIMx->CR2 = tmpcr2;
  827. /* Write to TIMx CCMR2 */
  828. TIMx->CCMR2 = tmpccmrx;
  829. /* Set the Capture Compare Register value */
  830. TIMx->CCR4 = TIM_OCInitStruct->TIM_Pulse;
  831. /* Write to TIMx CCER */
  832. TIMx->CCER = tmpccer;
  833. }
  834. /**
  835. * @brief Fills each TIM_OCInitStruct member with its default value.
  836. * @param TIM_OCInitStruct: pointer to a TIM_OCInitTypeDef structure which will
  837. * be initialized.
  838. * @retval None
  839. */
  840. void TIM_OCStructInit(TIM_OCInitTypeDef* TIM_OCInitStruct)
  841. {
  842. /* Set the default configuration */
  843. TIM_OCInitStruct->TIM_OCMode = TIM_OCMode_Timing;
  844. TIM_OCInitStruct->TIM_OutputState = TIM_OutputState_Disable;
  845. TIM_OCInitStruct->TIM_OutputNState = TIM_OutputNState_Disable;
  846. TIM_OCInitStruct->TIM_Pulse = 0x00000000;
  847. TIM_OCInitStruct->TIM_OCPolarity = TIM_OCPolarity_High;
  848. TIM_OCInitStruct->TIM_OCNPolarity = TIM_OCPolarity_High;
  849. TIM_OCInitStruct->TIM_OCIdleState = TIM_OCIdleState_Reset;
  850. TIM_OCInitStruct->TIM_OCNIdleState = TIM_OCNIdleState_Reset;
  851. }
  852. /**
  853. * @brief Selects the TIM Output Compare Mode.
  854. * @note This function disables the selected channel before changing the Output
  855. * Compare Mode. If needed, user has to enable this channel using
  856. * TIM_CCxCmd() and TIM_CCxNCmd() functions.
  857. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  858. * @param TIM_Channel: specifies the TIM Channel
  859. * This parameter can be one of the following values:
  860. * @arg TIM_Channel_1: TIM Channel 1
  861. * @arg TIM_Channel_2: TIM Channel 2
  862. * @arg TIM_Channel_3: TIM Channel 3
  863. * @arg TIM_Channel_4: TIM Channel 4
  864. * @param TIM_OCMode: specifies the TIM Output Compare Mode.
  865. * This parameter can be one of the following values:
  866. * @arg TIM_OCMode_Timing
  867. * @arg TIM_OCMode_Active
  868. * @arg TIM_OCMode_Toggle
  869. * @arg TIM_OCMode_PWM1
  870. * @arg TIM_OCMode_PWM2
  871. * @arg TIM_ForcedAction_Active
  872. * @arg TIM_ForcedAction_InActive
  873. * @retval None
  874. */
  875. void TIM_SelectOCxM(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_OCMode)
  876. {
  877. uint32_t tmp = 0;
  878. uint16_t tmp1 = 0;
  879. /* Check the parameters */
  880. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  881. assert_param(IS_TIM_CHANNEL(TIM_Channel));
  882. assert_param(IS_TIM_OCM(TIM_OCMode));
  883. tmp = (uint32_t) TIMx;
  884. tmp += CCMR_OFFSET;
  885. tmp1 = CCER_CCE_SET << (uint16_t)TIM_Channel;
  886. /* Disable the Channel: Reset the CCxE Bit */
  887. TIMx->CCER &= (uint16_t) ~tmp1;
  888. if((TIM_Channel == TIM_Channel_1) ||(TIM_Channel == TIM_Channel_3))
  889. {
  890. tmp += (TIM_Channel>>1);
  891. /* Reset the OCxM bits in the CCMRx register */
  892. *(__IO uint32_t *) tmp &= CCMR_OC13M_MASK;
  893. /* Configure the OCxM bits in the CCMRx register */
  894. *(__IO uint32_t *) tmp |= TIM_OCMode;
  895. }
  896. else
  897. {
  898. tmp += (uint16_t)(TIM_Channel - (uint16_t)4)>> (uint16_t)1;
  899. /* Reset the OCxM bits in the CCMRx register */
  900. *(__IO uint32_t *) tmp &= CCMR_OC24M_MASK;
  901. /* Configure the OCxM bits in the CCMRx register */
  902. *(__IO uint32_t *) tmp |= (uint16_t)(TIM_OCMode << 8);
  903. }
  904. }
  905. /**
  906. * @brief Sets the TIMx Capture Compare1 Register value
  907. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  908. * @param Compare1: specifies the Capture Compare1 register new value.
  909. * @retval None
  910. */
  911. void TIM_SetCompare1(TIM_TypeDef* TIMx, uint32_t Compare1)
  912. {
  913. /* Check the parameters */
  914. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  915. /* Set the Capture Compare1 Register value */
  916. TIMx->CCR1 = Compare1;
  917. }
  918. /**
  919. * @brief Sets the TIMx Capture Compare2 Register value
  920. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  921. * peripheral.
  922. * @param Compare2: specifies the Capture Compare2 register new value.
  923. * @retval None
  924. */
  925. void TIM_SetCompare2(TIM_TypeDef* TIMx, uint32_t Compare2)
  926. {
  927. /* Check the parameters */
  928. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  929. /* Set the Capture Compare2 Register value */
  930. TIMx->CCR2 = Compare2;
  931. }
  932. /**
  933. * @brief Sets the TIMx Capture Compare3 Register value
  934. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  935. * @param Compare3: specifies the Capture Compare3 register new value.
  936. * @retval None
  937. */
  938. void TIM_SetCompare3(TIM_TypeDef* TIMx, uint32_t Compare3)
  939. {
  940. /* Check the parameters */
  941. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  942. /* Set the Capture Compare3 Register value */
  943. TIMx->CCR3 = Compare3;
  944. }
  945. /**
  946. * @brief Sets the TIMx Capture Compare4 Register value
  947. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  948. * @param Compare4: specifies the Capture Compare4 register new value.
  949. * @retval None
  950. */
  951. void TIM_SetCompare4(TIM_TypeDef* TIMx, uint32_t Compare4)
  952. {
  953. /* Check the parameters */
  954. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  955. /* Set the Capture Compare4 Register value */
  956. TIMx->CCR4 = Compare4;
  957. }
  958. /**
  959. * @brief Forces the TIMx output 1 waveform to active or inactive level.
  960. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  961. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  962. * This parameter can be one of the following values:
  963. * @arg TIM_ForcedAction_Active: Force active level on OC1REF
  964. * @arg TIM_ForcedAction_InActive: Force inactive level on OC1REF.
  965. * @retval None
  966. */
  967. void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  968. {
  969. uint16_t tmpccmr1 = 0;
  970. /* Check the parameters */
  971. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  972. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  973. tmpccmr1 = TIMx->CCMR1;
  974. /* Reset the OC1M Bits */
  975. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC1M;
  976. /* Configure The Forced output Mode */
  977. tmpccmr1 |= TIM_ForcedAction;
  978. /* Write to TIMx CCMR1 register */
  979. TIMx->CCMR1 = tmpccmr1;
  980. }
  981. /**
  982. * @brief Forces the TIMx output 2 waveform to active or inactive level.
  983. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  984. * peripheral.
  985. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  986. * This parameter can be one of the following values:
  987. * @arg TIM_ForcedAction_Active: Force active level on OC2REF
  988. * @arg TIM_ForcedAction_InActive: Force inactive level on OC2REF.
  989. * @retval None
  990. */
  991. void TIM_ForcedOC2Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  992. {
  993. uint16_t tmpccmr1 = 0;
  994. /* Check the parameters */
  995. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  996. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  997. tmpccmr1 = TIMx->CCMR1;
  998. /* Reset the OC2M Bits */
  999. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC2M;
  1000. /* Configure The Forced output Mode */
  1001. tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
  1002. /* Write to TIMx CCMR1 register */
  1003. TIMx->CCMR1 = tmpccmr1;
  1004. }
  1005. /**
  1006. * @brief Forces the TIMx output 3 waveform to active or inactive level.
  1007. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1008. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  1009. * This parameter can be one of the following values:
  1010. * @arg TIM_ForcedAction_Active: Force active level on OC3REF
  1011. * @arg TIM_ForcedAction_InActive: Force inactive level on OC3REF.
  1012. * @retval None
  1013. */
  1014. void TIM_ForcedOC3Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  1015. {
  1016. uint16_t tmpccmr2 = 0;
  1017. /* Check the parameters */
  1018. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1019. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  1020. tmpccmr2 = TIMx->CCMR2;
  1021. /* Reset the OC1M Bits */
  1022. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC3M;
  1023. /* Configure The Forced output Mode */
  1024. tmpccmr2 |= TIM_ForcedAction;
  1025. /* Write to TIMx CCMR2 register */
  1026. TIMx->CCMR2 = tmpccmr2;
  1027. }
  1028. /**
  1029. * @brief Forces the TIMx output 4 waveform to active or inactive level.
  1030. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1031. * @param TIM_ForcedAction: specifies the forced Action to be set to the output waveform.
  1032. * This parameter can be one of the following values:
  1033. * @arg TIM_ForcedAction_Active: Force active level on OC4REF
  1034. * @arg TIM_ForcedAction_InActive: Force inactive level on OC4REF.
  1035. * @retval None
  1036. */
  1037. void TIM_ForcedOC4Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
  1038. {
  1039. uint16_t tmpccmr2 = 0;
  1040. /* Check the parameters */
  1041. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1042. assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  1043. tmpccmr2 = TIMx->CCMR2;
  1044. /* Reset the OC2M Bits */
  1045. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC4M;
  1046. /* Configure The Forced output Mode */
  1047. tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
  1048. /* Write to TIMx CCMR2 register */
  1049. TIMx->CCMR2 = tmpccmr2;
  1050. }
  1051. /**
  1052. * @brief Enables or disables the TIMx peripheral Preload register on CCR1.
  1053. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1054. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1055. * This parameter can be one of the following values:
  1056. * @arg TIM_OCPreload_Enable
  1057. * @arg TIM_OCPreload_Disable
  1058. * @retval None
  1059. */
  1060. void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1061. {
  1062. uint16_t tmpccmr1 = 0;
  1063. /* Check the parameters */
  1064. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1065. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1066. tmpccmr1 = TIMx->CCMR1;
  1067. /* Reset the OC1PE Bit */
  1068. tmpccmr1 &= (uint16_t)(~TIM_CCMR1_OC1PE);
  1069. /* Enable or Disable the Output Compare Preload feature */
  1070. tmpccmr1 |= TIM_OCPreload;
  1071. /* Write to TIMx CCMR1 register */
  1072. TIMx->CCMR1 = tmpccmr1;
  1073. }
  1074. /**
  1075. * @brief Enables or disables the TIMx peripheral Preload register on CCR2.
  1076. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1077. * peripheral.
  1078. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1079. * This parameter can be one of the following values:
  1080. * @arg TIM_OCPreload_Enable
  1081. * @arg TIM_OCPreload_Disable
  1082. * @retval None
  1083. */
  1084. void TIM_OC2PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1085. {
  1086. uint16_t tmpccmr1 = 0;
  1087. /* Check the parameters */
  1088. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1089. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1090. tmpccmr1 = TIMx->CCMR1;
  1091. /* Reset the OC2PE Bit */
  1092. tmpccmr1 &= (uint16_t)(~TIM_CCMR1_OC2PE);
  1093. /* Enable or Disable the Output Compare Preload feature */
  1094. tmpccmr1 |= (uint16_t)(TIM_OCPreload << 8);
  1095. /* Write to TIMx CCMR1 register */
  1096. TIMx->CCMR1 = tmpccmr1;
  1097. }
  1098. /**
  1099. * @brief Enables or disables the TIMx peripheral Preload register on CCR3.
  1100. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1101. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1102. * This parameter can be one of the following values:
  1103. * @arg TIM_OCPreload_Enable
  1104. * @arg TIM_OCPreload_Disable
  1105. * @retval None
  1106. */
  1107. void TIM_OC3PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1108. {
  1109. uint16_t tmpccmr2 = 0;
  1110. /* Check the parameters */
  1111. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1112. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1113. tmpccmr2 = TIMx->CCMR2;
  1114. /* Reset the OC3PE Bit */
  1115. tmpccmr2 &= (uint16_t)(~TIM_CCMR2_OC3PE);
  1116. /* Enable or Disable the Output Compare Preload feature */
  1117. tmpccmr2 |= TIM_OCPreload;
  1118. /* Write to TIMx CCMR2 register */
  1119. TIMx->CCMR2 = tmpccmr2;
  1120. }
  1121. /**
  1122. * @brief Enables or disables the TIMx peripheral Preload register on CCR4.
  1123. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1124. * @param TIM_OCPreload: new state of the TIMx peripheral Preload register
  1125. * This parameter can be one of the following values:
  1126. * @arg TIM_OCPreload_Enable
  1127. * @arg TIM_OCPreload_Disable
  1128. * @retval None
  1129. */
  1130. void TIM_OC4PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
  1131. {
  1132. uint16_t tmpccmr2 = 0;
  1133. /* Check the parameters */
  1134. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1135. assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  1136. tmpccmr2 = TIMx->CCMR2;
  1137. /* Reset the OC4PE Bit */
  1138. tmpccmr2 &= (uint16_t)(~TIM_CCMR2_OC4PE);
  1139. /* Enable or Disable the Output Compare Preload feature */
  1140. tmpccmr2 |= (uint16_t)(TIM_OCPreload << 8);
  1141. /* Write to TIMx CCMR2 register */
  1142. TIMx->CCMR2 = tmpccmr2;
  1143. }
  1144. /**
  1145. * @brief Configures the TIMx Output Compare 1 Fast feature.
  1146. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1147. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1148. * This parameter can be one of the following values:
  1149. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1150. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1151. * @retval None
  1152. */
  1153. void TIM_OC1FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1154. {
  1155. uint16_t tmpccmr1 = 0;
  1156. /* Check the parameters */
  1157. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1158. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1159. /* Get the TIMx CCMR1 register value */
  1160. tmpccmr1 = TIMx->CCMR1;
  1161. /* Reset the OC1FE Bit */
  1162. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC1FE;
  1163. /* Enable or Disable the Output Compare Fast Bit */
  1164. tmpccmr1 |= TIM_OCFast;
  1165. /* Write to TIMx CCMR1 */
  1166. TIMx->CCMR1 = tmpccmr1;
  1167. }
  1168. /**
  1169. * @brief Configures the TIMx Output Compare 2 Fast feature.
  1170. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1171. * peripheral.
  1172. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1173. * This parameter can be one of the following values:
  1174. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1175. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1176. * @retval None
  1177. */
  1178. void TIM_OC2FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1179. {
  1180. uint16_t tmpccmr1 = 0;
  1181. /* Check the parameters */
  1182. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1183. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1184. /* Get the TIMx CCMR1 register value */
  1185. tmpccmr1 = TIMx->CCMR1;
  1186. /* Reset the OC2FE Bit */
  1187. tmpccmr1 &= (uint16_t)(~TIM_CCMR1_OC2FE);
  1188. /* Enable or Disable the Output Compare Fast Bit */
  1189. tmpccmr1 |= (uint16_t)(TIM_OCFast << 8);
  1190. /* Write to TIMx CCMR1 */
  1191. TIMx->CCMR1 = tmpccmr1;
  1192. }
  1193. /**
  1194. * @brief Configures the TIMx Output Compare 3 Fast feature.
  1195. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1196. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1197. * This parameter can be one of the following values:
  1198. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1199. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1200. * @retval None
  1201. */
  1202. void TIM_OC3FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1203. {
  1204. uint16_t tmpccmr2 = 0;
  1205. /* Check the parameters */
  1206. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1207. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1208. /* Get the TIMx CCMR2 register value */
  1209. tmpccmr2 = TIMx->CCMR2;
  1210. /* Reset the OC3FE Bit */
  1211. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC3FE;
  1212. /* Enable or Disable the Output Compare Fast Bit */
  1213. tmpccmr2 |= TIM_OCFast;
  1214. /* Write to TIMx CCMR2 */
  1215. TIMx->CCMR2 = tmpccmr2;
  1216. }
  1217. /**
  1218. * @brief Configures the TIMx Output Compare 4 Fast feature.
  1219. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1220. * @param TIM_OCFast: new state of the Output Compare Fast Enable Bit.
  1221. * This parameter can be one of the following values:
  1222. * @arg TIM_OCFast_Enable: TIM output compare fast enable
  1223. * @arg TIM_OCFast_Disable: TIM output compare fast disable
  1224. * @retval None
  1225. */
  1226. void TIM_OC4FastConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCFast)
  1227. {
  1228. uint16_t tmpccmr2 = 0;
  1229. /* Check the parameters */
  1230. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1231. assert_param(IS_TIM_OCFAST_STATE(TIM_OCFast));
  1232. /* Get the TIMx CCMR2 register value */
  1233. tmpccmr2 = TIMx->CCMR2;
  1234. /* Reset the OC4FE Bit */
  1235. tmpccmr2 &= (uint16_t)(~TIM_CCMR2_OC4FE);
  1236. /* Enable or Disable the Output Compare Fast Bit */
  1237. tmpccmr2 |= (uint16_t)(TIM_OCFast << 8);
  1238. /* Write to TIMx CCMR2 */
  1239. TIMx->CCMR2 = tmpccmr2;
  1240. }
  1241. /**
  1242. * @brief Clears or safeguards the OCREF1 signal on an external event
  1243. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1244. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1245. * This parameter can be one of the following values:
  1246. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1247. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1248. * @retval None
  1249. */
  1250. void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1251. {
  1252. uint16_t tmpccmr1 = 0;
  1253. /* Check the parameters */
  1254. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1255. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1256. tmpccmr1 = TIMx->CCMR1;
  1257. /* Reset the OC1CE Bit */
  1258. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC1CE;
  1259. /* Enable or Disable the Output Compare Clear Bit */
  1260. tmpccmr1 |= TIM_OCClear;
  1261. /* Write to TIMx CCMR1 register */
  1262. TIMx->CCMR1 = tmpccmr1;
  1263. }
  1264. /**
  1265. * @brief Clears or safeguards the OCREF2 signal on an external event
  1266. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1267. * peripheral.
  1268. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1269. * This parameter can be one of the following values:
  1270. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1271. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1272. * @retval None
  1273. */
  1274. void TIM_ClearOC2Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1275. {
  1276. uint16_t tmpccmr1 = 0;
  1277. /* Check the parameters */
  1278. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1279. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1280. tmpccmr1 = TIMx->CCMR1;
  1281. /* Reset the OC2CE Bit */
  1282. tmpccmr1 &= (uint16_t)~TIM_CCMR1_OC2CE;
  1283. /* Enable or Disable the Output Compare Clear Bit */
  1284. tmpccmr1 |= (uint16_t)(TIM_OCClear << 8);
  1285. /* Write to TIMx CCMR1 register */
  1286. TIMx->CCMR1 = tmpccmr1;
  1287. }
  1288. /**
  1289. * @brief Clears or safeguards the OCREF3 signal on an external event
  1290. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1291. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1292. * This parameter can be one of the following values:
  1293. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1294. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1295. * @retval None
  1296. */
  1297. void TIM_ClearOC3Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1298. {
  1299. uint16_t tmpccmr2 = 0;
  1300. /* Check the parameters */
  1301. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1302. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1303. tmpccmr2 = TIMx->CCMR2;
  1304. /* Reset the OC3CE Bit */
  1305. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC3CE;
  1306. /* Enable or Disable the Output Compare Clear Bit */
  1307. tmpccmr2 |= TIM_OCClear;
  1308. /* Write to TIMx CCMR2 register */
  1309. TIMx->CCMR2 = tmpccmr2;
  1310. }
  1311. /**
  1312. * @brief Clears or safeguards the OCREF4 signal on an external event
  1313. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1314. * @param TIM_OCClear: new state of the Output Compare Clear Enable Bit.
  1315. * This parameter can be one of the following values:
  1316. * @arg TIM_OCClear_Enable: TIM Output clear enable
  1317. * @arg TIM_OCClear_Disable: TIM Output clear disable
  1318. * @retval None
  1319. */
  1320. void TIM_ClearOC4Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
  1321. {
  1322. uint16_t tmpccmr2 = 0;
  1323. /* Check the parameters */
  1324. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1325. assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));
  1326. tmpccmr2 = TIMx->CCMR2;
  1327. /* Reset the OC4CE Bit */
  1328. tmpccmr2 &= (uint16_t)~TIM_CCMR2_OC4CE;
  1329. /* Enable or Disable the Output Compare Clear Bit */
  1330. tmpccmr2 |= (uint16_t)(TIM_OCClear << 8);
  1331. /* Write to TIMx CCMR2 register */
  1332. TIMx->CCMR2 = tmpccmr2;
  1333. }
  1334. /**
  1335. * @brief Configures the TIMx channel 1 polarity.
  1336. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1337. * @param TIM_OCPolarity: specifies the OC1 Polarity
  1338. * This parameter can be one of the following values:
  1339. * @arg TIM_OCPolarity_High: Output Compare active high
  1340. * @arg TIM_OCPolarity_Low: Output Compare active low
  1341. * @retval None
  1342. */
  1343. void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1344. {
  1345. uint16_t tmpccer = 0;
  1346. /* Check the parameters */
  1347. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1348. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1349. tmpccer = TIMx->CCER;
  1350. /* Set or Reset the CC1P Bit */
  1351. tmpccer &= (uint16_t)(~TIM_CCER_CC1P);
  1352. tmpccer |= TIM_OCPolarity;
  1353. /* Write to TIMx CCER register */
  1354. TIMx->CCER = tmpccer;
  1355. }
  1356. /**
  1357. * @brief Configures the TIMx Channel 1N polarity.
  1358. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1359. * @param TIM_OCNPolarity: specifies the OC1N Polarity
  1360. * This parameter can be one of the following values:
  1361. * @arg TIM_OCNPolarity_High: Output Compare active high
  1362. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1363. * @retval None
  1364. */
  1365. void TIM_OC1NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1366. {
  1367. uint16_t tmpccer = 0;
  1368. /* Check the parameters */
  1369. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1370. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1371. tmpccer = TIMx->CCER;
  1372. /* Set or Reset the CC1NP Bit */
  1373. tmpccer &= (uint16_t)~TIM_CCER_CC1NP;
  1374. tmpccer |= TIM_OCNPolarity;
  1375. /* Write to TIMx CCER register */
  1376. TIMx->CCER = tmpccer;
  1377. }
  1378. /**
  1379. * @brief Configures the TIMx channel 2 polarity.
  1380. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1381. * peripheral.
  1382. * @param TIM_OCPolarity: specifies the OC2 Polarity
  1383. * This parameter can be one of the following values:
  1384. * @arg TIM_OCPolarity_High: Output Compare active high
  1385. * @arg TIM_OCPolarity_Low: Output Compare active low
  1386. * @retval None
  1387. */
  1388. void TIM_OC2PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1389. {
  1390. uint16_t tmpccer = 0;
  1391. /* Check the parameters */
  1392. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1393. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1394. tmpccer = TIMx->CCER;
  1395. /* Set or Reset the CC2P Bit */
  1396. tmpccer &= (uint16_t)(~TIM_CCER_CC2P);
  1397. tmpccer |= (uint16_t)(TIM_OCPolarity << 4);
  1398. /* Write to TIMx CCER register */
  1399. TIMx->CCER = tmpccer;
  1400. }
  1401. /**
  1402. * @brief Configures the TIMx Channel 2N polarity.
  1403. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1404. * @param TIM_OCNPolarity: specifies the OC2N Polarity
  1405. * This parameter can be one of the following values:
  1406. * @arg TIM_OCNPolarity_High: Output Compare active high
  1407. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1408. * @retval None
  1409. */
  1410. void TIM_OC2NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1411. {
  1412. uint16_t tmpccer = 0;
  1413. /* Check the parameters */
  1414. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1415. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1416. tmpccer = TIMx->CCER;
  1417. /* Set or Reset the CC2NP Bit */
  1418. tmpccer &= (uint16_t)~TIM_CCER_CC2NP;
  1419. tmpccer |= (uint16_t)(TIM_OCNPolarity << 4);
  1420. /* Write to TIMx CCER register */
  1421. TIMx->CCER = tmpccer;
  1422. }
  1423. /**
  1424. * @brief Configures the TIMx channel 3 polarity.
  1425. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1426. * @param TIM_OCPolarity: specifies the OC3 Polarity
  1427. * This parameter can be one of the following values:
  1428. * @arg TIM_OCPolarity_High: Output Compare active high
  1429. * @arg TIM_OCPolarity_Low: Output Compare active low
  1430. * @retval None
  1431. */
  1432. void TIM_OC3PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1433. {
  1434. uint16_t tmpccer = 0;
  1435. /* Check the parameters */
  1436. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1437. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1438. tmpccer = TIMx->CCER;
  1439. /* Set or Reset the CC3P Bit */
  1440. tmpccer &= (uint16_t)~TIM_CCER_CC3P;
  1441. tmpccer |= (uint16_t)(TIM_OCPolarity << 8);
  1442. /* Write to TIMx CCER register */
  1443. TIMx->CCER = tmpccer;
  1444. }
  1445. /**
  1446. * @brief Configures the TIMx Channel 3N polarity.
  1447. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1448. * @param TIM_OCNPolarity: specifies the OC3N Polarity
  1449. * This parameter can be one of the following values:
  1450. * @arg TIM_OCNPolarity_High: Output Compare active high
  1451. * @arg TIM_OCNPolarity_Low: Output Compare active low
  1452. * @retval None
  1453. */
  1454. void TIM_OC3NPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCNPolarity)
  1455. {
  1456. uint16_t tmpccer = 0;
  1457. /* Check the parameters */
  1458. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1459. assert_param(IS_TIM_OCN_POLARITY(TIM_OCNPolarity));
  1460. tmpccer = TIMx->CCER;
  1461. /* Set or Reset the CC3NP Bit */
  1462. tmpccer &= (uint16_t)~TIM_CCER_CC3NP;
  1463. tmpccer |= (uint16_t)(TIM_OCNPolarity << 8);
  1464. /* Write to TIMx CCER register */
  1465. TIMx->CCER = tmpccer;
  1466. }
  1467. /**
  1468. * @brief Configures the TIMx channel 4 polarity.
  1469. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1470. * @param TIM_OCPolarity: specifies the OC4 Polarity
  1471. * This parameter can be one of the following values:
  1472. * @arg TIM_OCPolarity_High: Output Compare active high
  1473. * @arg TIM_OCPolarity_Low: Output Compare active low
  1474. * @retval None
  1475. */
  1476. void TIM_OC4PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
  1477. {
  1478. uint16_t tmpccer = 0;
  1479. /* Check the parameters */
  1480. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1481. assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  1482. tmpccer = TIMx->CCER;
  1483. /* Set or Reset the CC4P Bit */
  1484. tmpccer &= (uint16_t)~TIM_CCER_CC4P;
  1485. tmpccer |= (uint16_t)(TIM_OCPolarity << 12);
  1486. /* Write to TIMx CCER register */
  1487. TIMx->CCER = tmpccer;
  1488. }
  1489. /**
  1490. * @brief Enables or disables the TIM Capture Compare Channel x.
  1491. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1492. * @param TIM_Channel: specifies the TIM Channel
  1493. * This parameter can be one of the following values:
  1494. * @arg TIM_Channel_1: TIM Channel 1
  1495. * @arg TIM_Channel_2: TIM Channel 2
  1496. * @arg TIM_Channel_3: TIM Channel 3
  1497. * @arg TIM_Channel_4: TIM Channel 4
  1498. * @param TIM_CCx: specifies the TIM Channel CCxE bit new state.
  1499. * This parameter can be: TIM_CCx_Enable or TIM_CCx_Disable.
  1500. * @retval None
  1501. */
  1502. void TIM_CCxCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCx)
  1503. {
  1504. uint16_t tmp = 0;
  1505. /* Check the parameters */
  1506. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1507. assert_param(IS_TIM_CHANNEL(TIM_Channel));
  1508. assert_param(IS_TIM_CCX(TIM_CCx));
  1509. tmp = CCER_CCE_SET << TIM_Channel;
  1510. /* Reset the CCxE Bit */
  1511. TIMx->CCER &= (uint16_t)~ tmp;
  1512. /* Set or reset the CCxE Bit */
  1513. TIMx->CCER |= (uint16_t)(TIM_CCx << TIM_Channel);
  1514. }
  1515. /**
  1516. * @brief Enables or disables the TIM Capture Compare Channel xN.
  1517. * @param TIMx: where x can be 1 or 8 to select the TIM peripheral.
  1518. * @param TIM_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 TIM_CCxN: 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. void TIM_CCxNCmd(TIM_TypeDef* TIMx, uint16_t TIM_Channel, uint16_t TIM_CCxN)
  1528. {
  1529. uint16_t tmp = 0;
  1530. /* Check the parameters */
  1531. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1532. assert_param(IS_TIM_COMPLEMENTARY_CHANNEL(TIM_Channel));
  1533. assert_param(IS_TIM_CCXN(TIM_CCxN));
  1534. tmp = CCER_CCNE_SET << TIM_Channel;
  1535. /* Reset the CCxNE Bit */
  1536. TIMx->CCER &= (uint16_t) ~tmp;
  1537. /* Set or reset the CCxNE Bit */
  1538. TIMx->CCER |= (uint16_t)(TIM_CCxN << TIM_Channel);
  1539. }
  1540. /**
  1541. * @}
  1542. */
  1543. /** @defgroup TIM_Group3 Input Capture management functions
  1544. * @brief Input Capture management functions
  1545. *
  1546. @verbatim
  1547. ===============================================================================
  1548. Input Capture management functions
  1549. ===============================================================================
  1550. ===================================================================
  1551. TIM Driver: how to use it in Input Capture Mode
  1552. ===================================================================
  1553. To use the Timer in Input Capture mode, the following steps are mandatory:
  1554. 1. Enable TIM clock using RCC_APBxPeriphClockCmd(RCC_APBxPeriph_TIMx, ENABLE) function
  1555. 2. Configure the TIM pins by configuring the corresponding GPIO pins
  1556. 2. Configure the Time base unit as described in the first part of this driver,
  1557. if needed, else the Timer will run with the default configuration:
  1558. - Autoreload value = 0xFFFF
  1559. - Prescaler value = 0x0000
  1560. - Counter mode = Up counting
  1561. - Clock Division = TIM_CKD_DIV1
  1562. 3. Fill the TIM_ICInitStruct with the desired parameters including:
  1563. - TIM Channel: TIM_Channel
  1564. - TIM Input Capture polarity: TIM_ICPolarity
  1565. - TIM Input Capture selection: TIM_ICSelection
  1566. - TIM Input Capture Prescaler: TIM_ICPrescaler
  1567. - TIM Input CApture filter value: TIM_ICFilter
  1568. 4. Call TIM_ICInit(TIMx, &TIM_ICInitStruct) to configure the desired channel with the
  1569. corresponding configuration and to measure only frequency or duty cycle of the input signal,
  1570. or,
  1571. Call TIM_PWMIConfig(TIMx, &TIM_ICInitStruct) to configure the desired channels with the
  1572. corresponding configuration and to measure the frequency and the duty cycle of the input signal
  1573. 5. Enable the NVIC or the DMA to read the measured frequency.
  1574. 6. Enable the corresponding interrupt (or DMA request) to read the Captured value,
  1575. using the function TIM_ITConfig(TIMx, TIM_IT_CCx) (or TIM_DMA_Cmd(TIMx, TIM_DMA_CCx))
  1576. 7. Call the TIM_Cmd(ENABLE) function to enable the TIM counter.
  1577. 8. Use TIM_GetCapturex(TIMx); to read the captured value.
  1578. Note1: All other functions can be used separately to modify, if needed,
  1579. a specific feature of the Timer.
  1580. @endverbatim
  1581. * @{
  1582. */
  1583. /**
  1584. * @brief Initializes the TIM peripheral according to the specified parameters
  1585. * in the TIM_ICInitStruct.
  1586. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1587. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure that contains
  1588. * the configuration information for the specified TIM peripheral.
  1589. * @retval None
  1590. */
  1591. void TIM_ICInit(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
  1592. {
  1593. /* Check the parameters */
  1594. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1595. assert_param(IS_TIM_IC_POLARITY(TIM_ICInitStruct->TIM_ICPolarity));
  1596. assert_param(IS_TIM_IC_SELECTION(TIM_ICInitStruct->TIM_ICSelection));
  1597. assert_param(IS_TIM_IC_PRESCALER(TIM_ICInitStruct->TIM_ICPrescaler));
  1598. assert_param(IS_TIM_IC_FILTER(TIM_ICInitStruct->TIM_ICFilter));
  1599. if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  1600. {
  1601. /* TI1 Configuration */
  1602. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1603. TIM_ICInitStruct->TIM_ICSelection,
  1604. TIM_ICInitStruct->TIM_ICFilter);
  1605. /* Set the Input Capture Prescaler value */
  1606. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1607. }
  1608. else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_2)
  1609. {
  1610. /* TI2 Configuration */
  1611. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1612. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1613. TIM_ICInitStruct->TIM_ICSelection,
  1614. TIM_ICInitStruct->TIM_ICFilter);
  1615. /* Set the Input Capture Prescaler value */
  1616. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1617. }
  1618. else if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_3)
  1619. {
  1620. /* TI3 Configuration */
  1621. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1622. TI3_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1623. TIM_ICInitStruct->TIM_ICSelection,
  1624. TIM_ICInitStruct->TIM_ICFilter);
  1625. /* Set the Input Capture Prescaler value */
  1626. TIM_SetIC3Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1627. }
  1628. else
  1629. {
  1630. /* TI4 Configuration */
  1631. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1632. TI4_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity,
  1633. TIM_ICInitStruct->TIM_ICSelection,
  1634. TIM_ICInitStruct->TIM_ICFilter);
  1635. /* Set the Input Capture Prescaler value */
  1636. TIM_SetIC4Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1637. }
  1638. }
  1639. /**
  1640. * @brief Fills each TIM_ICInitStruct member with its default value.
  1641. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure which will
  1642. * be initialized.
  1643. * @retval None
  1644. */
  1645. void TIM_ICStructInit(TIM_ICInitTypeDef* TIM_ICInitStruct)
  1646. {
  1647. /* Set the default configuration */
  1648. TIM_ICInitStruct->TIM_Channel = TIM_Channel_1;
  1649. TIM_ICInitStruct->TIM_ICPolarity = TIM_ICPolarity_Rising;
  1650. TIM_ICInitStruct->TIM_ICSelection = TIM_ICSelection_DirectTI;
  1651. TIM_ICInitStruct->TIM_ICPrescaler = TIM_ICPSC_DIV1;
  1652. TIM_ICInitStruct->TIM_ICFilter = 0x00;
  1653. }
  1654. /**
  1655. * @brief Configures the TIM peripheral according to the specified parameters
  1656. * in the TIM_ICInitStruct to measure an external PWM signal.
  1657. * @param TIMx: where x can be 1, 2, 3, 4, 5,8, 9 or 12 to select the TIM
  1658. * peripheral.
  1659. * @param TIM_ICInitStruct: pointer to a TIM_ICInitTypeDef structure that contains
  1660. * the configuration information for the specified TIM peripheral.
  1661. * @retval None
  1662. */
  1663. void TIM_PWMIConfig(TIM_TypeDef* TIMx, TIM_ICInitTypeDef* TIM_ICInitStruct)
  1664. {
  1665. uint16_t icoppositepolarity = TIM_ICPolarity_Rising;
  1666. uint16_t icoppositeselection = TIM_ICSelection_DirectTI;
  1667. /* Check the parameters */
  1668. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1669. /* Select the Opposite Input Polarity */
  1670. if (TIM_ICInitStruct->TIM_ICPolarity == TIM_ICPolarity_Rising)
  1671. {
  1672. icoppositepolarity = TIM_ICPolarity_Falling;
  1673. }
  1674. else
  1675. {
  1676. icoppositepolarity = TIM_ICPolarity_Rising;
  1677. }
  1678. /* Select the Opposite Input */
  1679. if (TIM_ICInitStruct->TIM_ICSelection == TIM_ICSelection_DirectTI)
  1680. {
  1681. icoppositeselection = TIM_ICSelection_IndirectTI;
  1682. }
  1683. else
  1684. {
  1685. icoppositeselection = TIM_ICSelection_DirectTI;
  1686. }
  1687. if (TIM_ICInitStruct->TIM_Channel == TIM_Channel_1)
  1688. {
  1689. /* TI1 Configuration */
  1690. TI1_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  1691. TIM_ICInitStruct->TIM_ICFilter);
  1692. /* Set the Input Capture Prescaler value */
  1693. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1694. /* TI2 Configuration */
  1695. TI2_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  1696. /* Set the Input Capture Prescaler value */
  1697. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1698. }
  1699. else
  1700. {
  1701. /* TI2 Configuration */
  1702. TI2_Config(TIMx, TIM_ICInitStruct->TIM_ICPolarity, TIM_ICInitStruct->TIM_ICSelection,
  1703. TIM_ICInitStruct->TIM_ICFilter);
  1704. /* Set the Input Capture Prescaler value */
  1705. TIM_SetIC2Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1706. /* TI1 Configuration */
  1707. TI1_Config(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->TIM_ICFilter);
  1708. /* Set the Input Capture Prescaler value */
  1709. TIM_SetIC1Prescaler(TIMx, TIM_ICInitStruct->TIM_ICPrescaler);
  1710. }
  1711. }
  1712. /**
  1713. * @brief Gets the TIMx Input Capture 1 value.
  1714. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1715. * @retval Capture Compare 1 Register value.
  1716. */
  1717. uint32_t TIM_GetCapture1(TIM_TypeDef* TIMx)
  1718. {
  1719. /* Check the parameters */
  1720. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1721. /* Get the Capture 1 Register value */
  1722. return TIMx->CCR1;
  1723. }
  1724. /**
  1725. * @brief Gets the TIMx Input Capture 2 value.
  1726. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1727. * peripheral.
  1728. * @retval Capture Compare 2 Register value.
  1729. */
  1730. uint32_t TIM_GetCapture2(TIM_TypeDef* TIMx)
  1731. {
  1732. /* Check the parameters */
  1733. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1734. /* Get the Capture 2 Register value */
  1735. return TIMx->CCR2;
  1736. }
  1737. /**
  1738. * @brief Gets the TIMx Input Capture 3 value.
  1739. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1740. * @retval Capture Compare 3 Register value.
  1741. */
  1742. uint32_t TIM_GetCapture3(TIM_TypeDef* TIMx)
  1743. {
  1744. /* Check the parameters */
  1745. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1746. /* Get the Capture 3 Register value */
  1747. return TIMx->CCR3;
  1748. }
  1749. /**
  1750. * @brief Gets the TIMx Input Capture 4 value.
  1751. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1752. * @retval Capture Compare 4 Register value.
  1753. */
  1754. uint32_t TIM_GetCapture4(TIM_TypeDef* TIMx)
  1755. {
  1756. /* Check the parameters */
  1757. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1758. /* Get the Capture 4 Register value */
  1759. return TIMx->CCR4;
  1760. }
  1761. /**
  1762. * @brief Sets the TIMx Input Capture 1 prescaler.
  1763. * @param TIMx: where x can be 1 to 14 except 6 and 7, to select the TIM peripheral.
  1764. * @param TIM_ICPSC: specifies the Input Capture1 prescaler new value.
  1765. * This parameter can be one of the following values:
  1766. * @arg TIM_ICPSC_DIV1: no prescaler
  1767. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1768. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1769. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1770. * @retval None
  1771. */
  1772. void TIM_SetIC1Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1773. {
  1774. /* Check the parameters */
  1775. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1776. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1777. /* Reset the IC1PSC Bits */
  1778. TIMx->CCMR1 &= (uint16_t)~TIM_CCMR1_IC1PSC;
  1779. /* Set the IC1PSC value */
  1780. TIMx->CCMR1 |= TIM_ICPSC;
  1781. }
  1782. /**
  1783. * @brief Sets the TIMx Input Capture 2 prescaler.
  1784. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  1785. * peripheral.
  1786. * @param TIM_ICPSC: specifies the Input Capture2 prescaler new value.
  1787. * This parameter can be one of the following values:
  1788. * @arg TIM_ICPSC_DIV1: no prescaler
  1789. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1790. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1791. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1792. * @retval None
  1793. */
  1794. void TIM_SetIC2Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1795. {
  1796. /* Check the parameters */
  1797. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1798. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1799. /* Reset the IC2PSC Bits */
  1800. TIMx->CCMR1 &= (uint16_t)~TIM_CCMR1_IC2PSC;
  1801. /* Set the IC2PSC value */
  1802. TIMx->CCMR1 |= (uint16_t)(TIM_ICPSC << 8);
  1803. }
  1804. /**
  1805. * @brief Sets the TIMx Input Capture 3 prescaler.
  1806. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1807. * @param TIM_ICPSC: specifies the Input Capture3 prescaler new value.
  1808. * This parameter can be one of the following values:
  1809. * @arg TIM_ICPSC_DIV1: no prescaler
  1810. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1811. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1812. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1813. * @retval None
  1814. */
  1815. void TIM_SetIC3Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1816. {
  1817. /* Check the parameters */
  1818. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1819. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1820. /* Reset the IC3PSC Bits */
  1821. TIMx->CCMR2 &= (uint16_t)~TIM_CCMR2_IC3PSC;
  1822. /* Set the IC3PSC value */
  1823. TIMx->CCMR2 |= TIM_ICPSC;
  1824. }
  1825. /**
  1826. * @brief Sets the TIMx Input Capture 4 prescaler.
  1827. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1828. * @param TIM_ICPSC: specifies the Input Capture4 prescaler new value.
  1829. * This parameter can be one of the following values:
  1830. * @arg TIM_ICPSC_DIV1: no prescaler
  1831. * @arg TIM_ICPSC_DIV2: capture is done once every 2 events
  1832. * @arg TIM_ICPSC_DIV4: capture is done once every 4 events
  1833. * @arg TIM_ICPSC_DIV8: capture is done once every 8 events
  1834. * @retval None
  1835. */
  1836. void TIM_SetIC4Prescaler(TIM_TypeDef* TIMx, uint16_t TIM_ICPSC)
  1837. {
  1838. /* Check the parameters */
  1839. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1840. assert_param(IS_TIM_IC_PRESCALER(TIM_ICPSC));
  1841. /* Reset the IC4PSC Bits */
  1842. TIMx->CCMR2 &= (uint16_t)~TIM_CCMR2_IC4PSC;
  1843. /* Set the IC4PSC value */
  1844. TIMx->CCMR2 |= (uint16_t)(TIM_ICPSC << 8);
  1845. }
  1846. /**
  1847. * @}
  1848. */
  1849. /** @defgroup TIM_Group4 Advanced-control timers (TIM1 and TIM8) specific features
  1850. * @brief Advanced-control timers (TIM1 and TIM8) specific features
  1851. *
  1852. @verbatim
  1853. ===============================================================================
  1854. Advanced-control timers (TIM1 and TIM8) specific features
  1855. ===============================================================================
  1856. ===================================================================
  1857. TIM Driver: how to use the Break feature
  1858. ===================================================================
  1859. After configuring the Timer channel(s) in the appropriate Output Compare mode:
  1860. 1. Fill the TIM_BDTRInitStruct with the desired parameters for the Timer
  1861. Break Polarity, dead time, Lock level, the OSSI/OSSR State and the
  1862. AOE(automatic output enable).
  1863. 2. Call TIM_BDTRConfig(TIMx, &TIM_BDTRInitStruct) to configure the Timer
  1864. 3. Enable the Main Output using TIM_CtrlPWMOutputs(TIM1, ENABLE)
  1865. 4. Once the break even occurs, the Timer's output signals are put in reset
  1866. state or in a known state (according to the configuration made in
  1867. TIM_BDTRConfig() function).
  1868. @endverbatim
  1869. * @{
  1870. */
  1871. /**
  1872. * @brief Configures the Break feature, dead time, Lock level, OSSI/OSSR State
  1873. * and the AOE(automatic output enable).
  1874. * @param TIMx: where x can be 1 or 8 to select the TIM
  1875. * @param TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef structure that
  1876. * contains the BDTR Register configuration information for the TIM peripheral.
  1877. * @retval None
  1878. */
  1879. void TIM_BDTRConfig(TIM_TypeDef* TIMx, TIM_BDTRInitTypeDef *TIM_BDTRInitStruct)
  1880. {
  1881. /* Check the parameters */
  1882. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1883. assert_param(IS_TIM_OSSR_STATE(TIM_BDTRInitStruct->TIM_OSSRState));
  1884. assert_param(IS_TIM_OSSI_STATE(TIM_BDTRInitStruct->TIM_OSSIState));
  1885. assert_param(IS_TIM_LOCK_LEVEL(TIM_BDTRInitStruct->TIM_LOCKLevel));
  1886. assert_param(IS_TIM_BREAK_STATE(TIM_BDTRInitStruct->TIM_Break));
  1887. assert_param(IS_TIM_BREAK_POLARITY(TIM_BDTRInitStruct->TIM_BreakPolarity));
  1888. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(TIM_BDTRInitStruct->TIM_AutomaticOutput));
  1889. /* Set the Lock level, the Break enable Bit and the Polarity, the OSSR State,
  1890. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  1891. TIMx->BDTR = (uint32_t)TIM_BDTRInitStruct->TIM_OSSRState | TIM_BDTRInitStruct->TIM_OSSIState |
  1892. TIM_BDTRInitStruct->TIM_LOCKLevel | TIM_BDTRInitStruct->TIM_DeadTime |
  1893. TIM_BDTRInitStruct->TIM_Break | TIM_BDTRInitStruct->TIM_BreakPolarity |
  1894. TIM_BDTRInitStruct->TIM_AutomaticOutput;
  1895. }
  1896. /**
  1897. * @brief Fills each TIM_BDTRInitStruct member with its default value.
  1898. * @param TIM_BDTRInitStruct: pointer to a TIM_BDTRInitTypeDef structure which
  1899. * will be initialized.
  1900. * @retval None
  1901. */
  1902. void TIM_BDTRStructInit(TIM_BDTRInitTypeDef* TIM_BDTRInitStruct)
  1903. {
  1904. /* Set the default configuration */
  1905. TIM_BDTRInitStruct->TIM_OSSRState = TIM_OSSRState_Disable;
  1906. TIM_BDTRInitStruct->TIM_OSSIState = TIM_OSSIState_Disable;
  1907. TIM_BDTRInitStruct->TIM_LOCKLevel = TIM_LOCKLevel_OFF;
  1908. TIM_BDTRInitStruct->TIM_DeadTime = 0x00;
  1909. TIM_BDTRInitStruct->TIM_Break = TIM_Break_Disable;
  1910. TIM_BDTRInitStruct->TIM_BreakPolarity = TIM_BreakPolarity_Low;
  1911. TIM_BDTRInitStruct->TIM_AutomaticOutput = TIM_AutomaticOutput_Disable;
  1912. }
  1913. /**
  1914. * @brief Enables or disables the TIM peripheral Main Outputs.
  1915. * @param TIMx: where x can be 1 or 8 to select the TIMx peripheral.
  1916. * @param NewState: new state of the TIM peripheral Main Outputs.
  1917. * This parameter can be: ENABLE or DISABLE.
  1918. * @retval None
  1919. */
  1920. void TIM_CtrlPWMOutputs(TIM_TypeDef* TIMx, FunctionalState NewState)
  1921. {
  1922. /* Check the parameters */
  1923. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1924. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1925. if (NewState != DISABLE)
  1926. {
  1927. /* Enable the TIM Main Output */
  1928. TIMx->BDTR |= TIM_BDTR_MOE;
  1929. }
  1930. else
  1931. {
  1932. /* Disable the TIM Main Output */
  1933. TIMx->BDTR &= (uint16_t)~TIM_BDTR_MOE;
  1934. }
  1935. }
  1936. /**
  1937. * @brief Selects the TIM peripheral Commutation event.
  1938. * @param TIMx: where x can be 1 or 8 to select the TIMx peripheral
  1939. * @param NewState: new state of the Commutation event.
  1940. * This parameter can be: ENABLE or DISABLE.
  1941. * @retval None
  1942. */
  1943. void TIM_SelectCOM(TIM_TypeDef* TIMx, FunctionalState NewState)
  1944. {
  1945. /* Check the parameters */
  1946. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1947. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1948. if (NewState != DISABLE)
  1949. {
  1950. /* Set the COM Bit */
  1951. TIMx->CR2 |= TIM_CR2_CCUS;
  1952. }
  1953. else
  1954. {
  1955. /* Reset the COM Bit */
  1956. TIMx->CR2 &= (uint16_t)~TIM_CR2_CCUS;
  1957. }
  1958. }
  1959. /**
  1960. * @brief Sets or Resets the TIM peripheral Capture Compare Preload Control bit.
  1961. * @param TIMx: where x can be 1 or 8 to select the TIMx peripheral
  1962. * @param NewState: new state of the Capture Compare Preload Control bit
  1963. * This parameter can be: ENABLE or DISABLE.
  1964. * @retval None
  1965. */
  1966. void TIM_CCPreloadControl(TIM_TypeDef* TIMx, FunctionalState NewState)
  1967. {
  1968. /* Check the parameters */
  1969. assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1970. assert_param(IS_FUNCTIONAL_STATE(NewState));
  1971. if (NewState != DISABLE)
  1972. {
  1973. /* Set the CCPC Bit */
  1974. TIMx->CR2 |= TIM_CR2_CCPC;
  1975. }
  1976. else
  1977. {
  1978. /* Reset the CCPC Bit */
  1979. TIMx->CR2 &= (uint16_t)~TIM_CR2_CCPC;
  1980. }
  1981. }
  1982. /**
  1983. * @}
  1984. */
  1985. /** @defgroup TIM_Group5 Interrupts DMA and flags management functions
  1986. * @brief Interrupts, DMA and flags management functions
  1987. *
  1988. @verbatim
  1989. ===============================================================================
  1990. Interrupts, DMA and flags management functions
  1991. ===============================================================================
  1992. @endverbatim
  1993. * @{
  1994. */
  1995. /**
  1996. * @brief Enables or disables the specified TIM interrupts.
  1997. * @param TIMx: where x can be 1 to 14 to select the TIMx peripheral.
  1998. * @param TIM_IT: specifies the TIM interrupts sources to be enabled or disabled.
  1999. * This parameter can be any combination of the following values:
  2000. * @arg TIM_IT_Update: TIM update Interrupt source
  2001. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  2002. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  2003. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  2004. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  2005. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  2006. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  2007. * @arg TIM_IT_Break: TIM Break Interrupt source
  2008. *
  2009. * @note For TIM6 and TIM7 only the parameter TIM_IT_Update can be used
  2010. * @note For TIM9 and TIM12 only one of the following parameters can be used: TIM_IT_Update,
  2011. * TIM_IT_CC1, TIM_IT_CC2 or TIM_IT_Trigger.
  2012. * @note For TIM10, TIM11, TIM13 and TIM14 only one of the following parameters can
  2013. * be used: TIM_IT_Update or TIM_IT_CC1
  2014. * @note TIM_IT_COM and TIM_IT_Break can be used only with TIM1 and TIM8
  2015. *
  2016. * @param NewState: new state of the TIM interrupts.
  2017. * This parameter can be: ENABLE or DISABLE.
  2018. * @retval None
  2019. */
  2020. void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState)
  2021. {
  2022. /* Check the parameters */
  2023. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2024. assert_param(IS_TIM_IT(TIM_IT));
  2025. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2026. if (NewState != DISABLE)
  2027. {
  2028. /* Enable the Interrupt sources */
  2029. TIMx->DIER |= TIM_IT;
  2030. }
  2031. else
  2032. {
  2033. /* Disable the Interrupt sources */
  2034. TIMx->DIER &= (uint16_t)~TIM_IT;
  2035. }
  2036. }
  2037. /**
  2038. * @brief Configures the TIMx event to be generate by software.
  2039. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2040. * @param TIM_EventSource: specifies the event source.
  2041. * This parameter can be one or more of the following values:
  2042. * @arg TIM_EventSource_Update: Timer update Event source
  2043. * @arg TIM_EventSource_CC1: Timer Capture Compare 1 Event source
  2044. * @arg TIM_EventSource_CC2: Timer Capture Compare 2 Event source
  2045. * @arg TIM_EventSource_CC3: Timer Capture Compare 3 Event source
  2046. * @arg TIM_EventSource_CC4: Timer Capture Compare 4 Event source
  2047. * @arg TIM_EventSource_COM: Timer COM event source
  2048. * @arg TIM_EventSource_Trigger: Timer Trigger Event source
  2049. * @arg TIM_EventSource_Break: Timer Break event source
  2050. *
  2051. * @note TIM6 and TIM7 can only generate an update event.
  2052. * @note TIM_EventSource_COM and TIM_EventSource_Break are used only with TIM1 and TIM8.
  2053. *
  2054. * @retval None
  2055. */
  2056. void TIM_GenerateEvent(TIM_TypeDef* TIMx, uint16_t TIM_EventSource)
  2057. {
  2058. /* Check the parameters */
  2059. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2060. assert_param(IS_TIM_EVENT_SOURCE(TIM_EventSource));
  2061. /* Set the event sources */
  2062. TIMx->EGR = TIM_EventSource;
  2063. }
  2064. /**
  2065. * @brief Checks whether the specified TIM flag is set or not.
  2066. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2067. * @param TIM_FLAG: specifies the flag to check.
  2068. * This parameter can be one of the following values:
  2069. * @arg TIM_FLAG_Update: TIM update Flag
  2070. * @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  2071. * @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  2072. * @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  2073. * @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  2074. * @arg TIM_FLAG_COM: TIM Commutation Flag
  2075. * @arg TIM_FLAG_Trigger: TIM Trigger Flag
  2076. * @arg TIM_FLAG_Break: TIM Break Flag
  2077. * @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 over capture Flag
  2078. * @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 over capture Flag
  2079. * @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 over capture Flag
  2080. * @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 over capture Flag
  2081. *
  2082. * @note TIM6 and TIM7 can have only one update flag.
  2083. * @note TIM_FLAG_COM and TIM_FLAG_Break are used only with TIM1 and TIM8.
  2084. *
  2085. * @retval The new state of TIM_FLAG (SET or RESET).
  2086. */
  2087. FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
  2088. {
  2089. ITStatus bitstatus = RESET;
  2090. /* Check the parameters */
  2091. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2092. assert_param(IS_TIM_GET_FLAG(TIM_FLAG));
  2093. if ((TIMx->SR & TIM_FLAG) != (uint16_t)RESET)
  2094. {
  2095. bitstatus = SET;
  2096. }
  2097. else
  2098. {
  2099. bitstatus = RESET;
  2100. }
  2101. return bitstatus;
  2102. }
  2103. /**
  2104. * @brief Clears the TIMx's pending flags.
  2105. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2106. * @param TIM_FLAG: specifies the flag bit to clear.
  2107. * This parameter can be any combination of the following values:
  2108. * @arg TIM_FLAG_Update: TIM update Flag
  2109. * @arg TIM_FLAG_CC1: TIM Capture Compare 1 Flag
  2110. * @arg TIM_FLAG_CC2: TIM Capture Compare 2 Flag
  2111. * @arg TIM_FLAG_CC3: TIM Capture Compare 3 Flag
  2112. * @arg TIM_FLAG_CC4: TIM Capture Compare 4 Flag
  2113. * @arg TIM_FLAG_COM: TIM Commutation Flag
  2114. * @arg TIM_FLAG_Trigger: TIM Trigger Flag
  2115. * @arg TIM_FLAG_Break: TIM Break Flag
  2116. * @arg TIM_FLAG_CC1OF: TIM Capture Compare 1 over capture Flag
  2117. * @arg TIM_FLAG_CC2OF: TIM Capture Compare 2 over capture Flag
  2118. * @arg TIM_FLAG_CC3OF: TIM Capture Compare 3 over capture Flag
  2119. * @arg TIM_FLAG_CC4OF: TIM Capture Compare 4 over capture Flag
  2120. *
  2121. * @note TIM6 and TIM7 can have only one update flag.
  2122. * @note TIM_FLAG_COM and TIM_FLAG_Break are used only with TIM1 and TIM8.
  2123. *
  2124. * @retval None
  2125. */
  2126. void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG)
  2127. {
  2128. /* Check the parameters */
  2129. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2130. /* Clear the flags */
  2131. TIMx->SR = (uint16_t)~TIM_FLAG;
  2132. }
  2133. /**
  2134. * @brief Checks whether the TIM interrupt has occurred or not.
  2135. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2136. * @param TIM_IT: specifies the TIM interrupt source to check.
  2137. * This parameter can be one of the following values:
  2138. * @arg TIM_IT_Update: TIM update Interrupt source
  2139. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  2140. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  2141. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  2142. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  2143. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  2144. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  2145. * @arg TIM_IT_Break: TIM Break Interrupt source
  2146. *
  2147. * @note TIM6 and TIM7 can generate only an update interrupt.
  2148. * @note TIM_IT_COM and TIM_IT_Break are used only with TIM1 and TIM8.
  2149. *
  2150. * @retval The new state of the TIM_IT(SET or RESET).
  2151. */
  2152. ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT)
  2153. {
  2154. ITStatus bitstatus = RESET;
  2155. uint16_t itstatus = 0x0, itenable = 0x0;
  2156. /* Check the parameters */
  2157. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2158. assert_param(IS_TIM_GET_IT(TIM_IT));
  2159. itstatus = TIMx->SR & TIM_IT;
  2160. itenable = TIMx->DIER & TIM_IT;
  2161. if ((itstatus != (uint16_t)RESET) && (itenable != (uint16_t)RESET))
  2162. {
  2163. bitstatus = SET;
  2164. }
  2165. else
  2166. {
  2167. bitstatus = RESET;
  2168. }
  2169. return bitstatus;
  2170. }
  2171. /**
  2172. * @brief Clears the TIMx's interrupt pending bits.
  2173. * @param TIMx: where x can be 1 to 14 to select the TIM peripheral.
  2174. * @param TIM_IT: specifies the pending bit to clear.
  2175. * This parameter can be any combination of the following values:
  2176. * @arg TIM_IT_Update: TIM1 update Interrupt source
  2177. * @arg TIM_IT_CC1: TIM Capture Compare 1 Interrupt source
  2178. * @arg TIM_IT_CC2: TIM Capture Compare 2 Interrupt source
  2179. * @arg TIM_IT_CC3: TIM Capture Compare 3 Interrupt source
  2180. * @arg TIM_IT_CC4: TIM Capture Compare 4 Interrupt source
  2181. * @arg TIM_IT_COM: TIM Commutation Interrupt source
  2182. * @arg TIM_IT_Trigger: TIM Trigger Interrupt source
  2183. * @arg TIM_IT_Break: TIM Break Interrupt source
  2184. *
  2185. * @note TIM6 and TIM7 can generate only an update interrupt.
  2186. * @note TIM_IT_COM and TIM_IT_Break are used only with TIM1 and TIM8.
  2187. *
  2188. * @retval None
  2189. */
  2190. void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT)
  2191. {
  2192. /* Check the parameters */
  2193. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2194. /* Clear the IT pending Bit */
  2195. TIMx->SR = (uint16_t)~TIM_IT;
  2196. }
  2197. /**
  2198. * @brief Configures the TIMx's DMA interface.
  2199. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2200. * @param TIM_DMABase: DMA Base address.
  2201. * This parameter can be one of the following values:
  2202. * @arg TIM_DMABase_CR1
  2203. * @arg TIM_DMABase_CR2
  2204. * @arg TIM_DMABase_SMCR
  2205. * @arg TIM_DMABase_DIER
  2206. * @arg TIM1_DMABase_SR
  2207. * @arg TIM_DMABase_EGR
  2208. * @arg TIM_DMABase_CCMR1
  2209. * @arg TIM_DMABase_CCMR2
  2210. * @arg TIM_DMABase_CCER
  2211. * @arg TIM_DMABase_CNT
  2212. * @arg TIM_DMABase_PSC
  2213. * @arg TIM_DMABase_ARR
  2214. * @arg TIM_DMABase_RCR
  2215. * @arg TIM_DMABase_CCR1
  2216. * @arg TIM_DMABase_CCR2
  2217. * @arg TIM_DMABase_CCR3
  2218. * @arg TIM_DMABase_CCR4
  2219. * @arg TIM_DMABase_BDTR
  2220. * @arg TIM_DMABase_DCR
  2221. * @param TIM_DMABurstLength: DMA Burst length. This parameter can be one value
  2222. * between: TIM_DMABurstLength_1Transfer and TIM_DMABurstLength_18Transfers.
  2223. * @retval None
  2224. */
  2225. void TIM_DMAConfig(TIM_TypeDef* TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength)
  2226. {
  2227. /* Check the parameters */
  2228. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2229. assert_param(IS_TIM_DMA_BASE(TIM_DMABase));
  2230. assert_param(IS_TIM_DMA_LENGTH(TIM_DMABurstLength));
  2231. /* Set the DMA Base and the DMA Burst Length */
  2232. TIMx->DCR = TIM_DMABase | TIM_DMABurstLength;
  2233. }
  2234. /**
  2235. * @brief Enables or disables the TIMx's DMA Requests.
  2236. * @param TIMx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the TIM peripheral.
  2237. * @param TIM_DMASource: specifies the DMA Request sources.
  2238. * This parameter can be any combination of the following values:
  2239. * @arg TIM_DMA_Update: TIM update Interrupt source
  2240. * @arg TIM_DMA_CC1: TIM Capture Compare 1 DMA source
  2241. * @arg TIM_DMA_CC2: TIM Capture Compare 2 DMA source
  2242. * @arg TIM_DMA_CC3: TIM Capture Compare 3 DMA source
  2243. * @arg TIM_DMA_CC4: TIM Capture Compare 4 DMA source
  2244. * @arg TIM_DMA_COM: TIM Commutation DMA source
  2245. * @arg TIM_DMA_Trigger: TIM Trigger DMA source
  2246. * @param NewState: new state of the DMA Request sources.
  2247. * This parameter can be: ENABLE or DISABLE.
  2248. * @retval None
  2249. */
  2250. void TIM_DMACmd(TIM_TypeDef* TIMx, uint16_t TIM_DMASource, FunctionalState NewState)
  2251. {
  2252. /* Check the parameters */
  2253. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  2254. assert_param(IS_TIM_DMA_SOURCE(TIM_DMASource));
  2255. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2256. if (NewState != DISABLE)
  2257. {
  2258. /* Enable the DMA sources */
  2259. TIMx->DIER |= TIM_DMASource;
  2260. }
  2261. else
  2262. {
  2263. /* Disable the DMA sources */
  2264. TIMx->DIER &= (uint16_t)~TIM_DMASource;
  2265. }
  2266. }
  2267. /**
  2268. * @brief Selects the TIMx peripheral Capture Compare DMA source.
  2269. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2270. * @param NewState: new state of the Capture Compare DMA source
  2271. * This parameter can be: ENABLE or DISABLE.
  2272. * @retval None
  2273. */
  2274. void TIM_SelectCCDMA(TIM_TypeDef* TIMx, FunctionalState NewState)
  2275. {
  2276. /* Check the parameters */
  2277. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2278. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2279. if (NewState != DISABLE)
  2280. {
  2281. /* Set the CCDS Bit */
  2282. TIMx->CR2 |= TIM_CR2_CCDS;
  2283. }
  2284. else
  2285. {
  2286. /* Reset the CCDS Bit */
  2287. TIMx->CR2 &= (uint16_t)~TIM_CR2_CCDS;
  2288. }
  2289. }
  2290. /**
  2291. * @}
  2292. */
  2293. /** @defgroup TIM_Group6 Clocks management functions
  2294. * @brief Clocks management functions
  2295. *
  2296. @verbatim
  2297. ===============================================================================
  2298. Clocks management functions
  2299. ===============================================================================
  2300. @endverbatim
  2301. * @{
  2302. */
  2303. /**
  2304. * @brief Configures the TIMx internal Clock
  2305. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2306. * peripheral.
  2307. * @retval None
  2308. */
  2309. void TIM_InternalClockConfig(TIM_TypeDef* TIMx)
  2310. {
  2311. /* Check the parameters */
  2312. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2313. /* Disable slave mode to clock the prescaler directly with the internal clock */
  2314. TIMx->SMCR &= (uint16_t)~TIM_SMCR_SMS;
  2315. }
  2316. /**
  2317. * @brief Configures the TIMx Internal Trigger as External Clock
  2318. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2319. * peripheral.
  2320. * @param TIM_InputTriggerSource: Trigger source.
  2321. * This parameter can be one of the following values:
  2322. * @arg TIM_TS_ITR0: Internal Trigger 0
  2323. * @arg TIM_TS_ITR1: Internal Trigger 1
  2324. * @arg TIM_TS_ITR2: Internal Trigger 2
  2325. * @arg TIM_TS_ITR3: Internal Trigger 3
  2326. * @retval None
  2327. */
  2328. void TIM_ITRxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
  2329. {
  2330. /* Check the parameters */
  2331. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2332. assert_param(IS_TIM_INTERNAL_TRIGGER_SELECTION(TIM_InputTriggerSource));
  2333. /* Select the Internal Trigger */
  2334. TIM_SelectInputTrigger(TIMx, TIM_InputTriggerSource);
  2335. /* Select the External clock mode1 */
  2336. TIMx->SMCR |= TIM_SlaveMode_External1;
  2337. }
  2338. /**
  2339. * @brief Configures the TIMx Trigger as External Clock
  2340. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13 or 14
  2341. * to select the TIM peripheral.
  2342. * @param TIM_TIxExternalCLKSource: Trigger source.
  2343. * This parameter can be one of the following values:
  2344. * @arg TIM_TIxExternalCLK1Source_TI1ED: TI1 Edge Detector
  2345. * @arg TIM_TIxExternalCLK1Source_TI1: Filtered Timer Input 1
  2346. * @arg TIM_TIxExternalCLK1Source_TI2: Filtered Timer Input 2
  2347. * @param TIM_ICPolarity: specifies the TIx Polarity.
  2348. * This parameter can be one of the following values:
  2349. * @arg TIM_ICPolarity_Rising
  2350. * @arg TIM_ICPolarity_Falling
  2351. * @param ICFilter: specifies the filter value.
  2352. * This parameter must be a value between 0x0 and 0xF.
  2353. * @retval None
  2354. */
  2355. void TIM_TIxExternalClockConfig(TIM_TypeDef* TIMx, uint16_t TIM_TIxExternalCLKSource,
  2356. uint16_t TIM_ICPolarity, uint16_t ICFilter)
  2357. {
  2358. /* Check the parameters */
  2359. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  2360. assert_param(IS_TIM_IC_POLARITY(TIM_ICPolarity));
  2361. assert_param(IS_TIM_IC_FILTER(ICFilter));
  2362. /* Configure the Timer Input Clock Source */
  2363. if (TIM_TIxExternalCLKSource == TIM_TIxExternalCLK1Source_TI2)
  2364. {
  2365. TI2_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  2366. }
  2367. else
  2368. {
  2369. TI1_Config(TIMx, TIM_ICPolarity, TIM_ICSelection_DirectTI, ICFilter);
  2370. }
  2371. /* Select the Trigger source */
  2372. TIM_SelectInputTrigger(TIMx, TIM_TIxExternalCLKSource);
  2373. /* Select the External clock mode1 */
  2374. TIMx->SMCR |= TIM_SlaveMode_External1;
  2375. }
  2376. /**
  2377. * @brief Configures the External clock Mode1
  2378. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2379. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2380. * This parameter can be one of the following values:
  2381. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2382. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2383. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2384. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2385. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2386. * This parameter can be one of the following values:
  2387. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2388. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2389. * @param ExtTRGFilter: External Trigger Filter.
  2390. * This parameter must be a value between 0x00 and 0x0F
  2391. * @retval None
  2392. */
  2393. void TIM_ETRClockMode1Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2394. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2395. {
  2396. uint16_t tmpsmcr = 0;
  2397. /* Check the parameters */
  2398. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2399. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2400. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2401. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2402. /* Configure the ETR Clock source */
  2403. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  2404. /* Get the TIMx SMCR register value */
  2405. tmpsmcr = TIMx->SMCR;
  2406. /* Reset the SMS Bits */
  2407. tmpsmcr &= (uint16_t)~TIM_SMCR_SMS;
  2408. /* Select the External clock mode1 */
  2409. tmpsmcr |= TIM_SlaveMode_External1;
  2410. /* Select the Trigger selection : ETRF */
  2411. tmpsmcr &= (uint16_t)~TIM_SMCR_TS;
  2412. tmpsmcr |= TIM_TS_ETRF;
  2413. /* Write to TIMx SMCR */
  2414. TIMx->SMCR = tmpsmcr;
  2415. }
  2416. /**
  2417. * @brief Configures the External clock Mode2
  2418. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2419. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2420. * This parameter can be one of the following values:
  2421. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2422. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2423. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2424. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2425. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2426. * This parameter can be one of the following values:
  2427. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2428. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2429. * @param ExtTRGFilter: External Trigger Filter.
  2430. * This parameter must be a value between 0x00 and 0x0F
  2431. * @retval None
  2432. */
  2433. void TIM_ETRClockMode2Config(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2434. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2435. {
  2436. /* Check the parameters */
  2437. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2438. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2439. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2440. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2441. /* Configure the ETR Clock source */
  2442. TIM_ETRConfig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  2443. /* Enable the External clock mode2 */
  2444. TIMx->SMCR |= TIM_SMCR_ECE;
  2445. }
  2446. /**
  2447. * @}
  2448. */
  2449. /** @defgroup TIM_Group7 Synchronization management functions
  2450. * @brief Synchronization management functions
  2451. *
  2452. @verbatim
  2453. ===============================================================================
  2454. Synchronization management functions
  2455. ===============================================================================
  2456. ===================================================================
  2457. TIM Driver: how to use it in synchronization Mode
  2458. ===================================================================
  2459. Case of two/several Timers
  2460. **************************
  2461. 1. Configure the Master Timers using the following functions:
  2462. - void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource);
  2463. - void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_MasterSlaveMode);
  2464. 2. Configure the Slave Timers using the following functions:
  2465. - void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);
  2466. - void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);
  2467. Case of Timers and external trigger(ETR pin)
  2468. ********************************************
  2469. 1. Configure the External trigger using this function:
  2470. - void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler, uint16_t TIM_ExtTRGPolarity,
  2471. uint16_t ExtTRGFilter);
  2472. 2. Configure the Slave Timers using the following functions:
  2473. - void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource);
  2474. - void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);
  2475. @endverbatim
  2476. * @{
  2477. */
  2478. /**
  2479. * @brief Selects the Input Trigger source
  2480. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13 or 14
  2481. * to select the TIM peripheral.
  2482. * @param TIM_InputTriggerSource: The Input Trigger source.
  2483. * This parameter can be one of the following values:
  2484. * @arg TIM_TS_ITR0: Internal Trigger 0
  2485. * @arg TIM_TS_ITR1: Internal Trigger 1
  2486. * @arg TIM_TS_ITR2: Internal Trigger 2
  2487. * @arg TIM_TS_ITR3: Internal Trigger 3
  2488. * @arg TIM_TS_TI1F_ED: TI1 Edge Detector
  2489. * @arg TIM_TS_TI1FP1: Filtered Timer Input 1
  2490. * @arg TIM_TS_TI2FP2: Filtered Timer Input 2
  2491. * @arg TIM_TS_ETRF: External Trigger input
  2492. * @retval None
  2493. */
  2494. void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_InputTriggerSource)
  2495. {
  2496. uint16_t tmpsmcr = 0;
  2497. /* Check the parameters */
  2498. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  2499. assert_param(IS_TIM_TRIGGER_SELECTION(TIM_InputTriggerSource));
  2500. /* Get the TIMx SMCR register value */
  2501. tmpsmcr = TIMx->SMCR;
  2502. /* Reset the TS Bits */
  2503. tmpsmcr &= (uint16_t)~TIM_SMCR_TS;
  2504. /* Set the Input Trigger source */
  2505. tmpsmcr |= TIM_InputTriggerSource;
  2506. /* Write to TIMx SMCR */
  2507. TIMx->SMCR = tmpsmcr;
  2508. }
  2509. /**
  2510. * @brief Selects the TIMx Trigger Output Mode.
  2511. * @param TIMx: where x can be 1, 2, 3, 4, 5, 6, 7 or 8 to select the TIM peripheral.
  2512. *
  2513. * @param TIM_TRGOSource: specifies the Trigger Output source.
  2514. * This parameter can be one of the following values:
  2515. *
  2516. * - For all TIMx
  2517. * @arg TIM_TRGOSource_Reset: The UG bit in the TIM_EGR register is used as the trigger output(TRGO)
  2518. * @arg TIM_TRGOSource_Enable: The Counter Enable CEN is used as the trigger output(TRGO)
  2519. * @arg TIM_TRGOSource_Update: The update event is selected as the trigger output(TRGO)
  2520. *
  2521. * - For all TIMx except TIM6 and TIM7
  2522. * @arg TIM_TRGOSource_OC1: The trigger output sends a positive pulse when the CC1IF flag
  2523. * is to be set, as soon as a capture or compare match occurs(TRGO)
  2524. * @arg TIM_TRGOSource_OC1Ref: OC1REF signal is used as the trigger output(TRGO)
  2525. * @arg TIM_TRGOSource_OC2Ref: OC2REF signal is used as the trigger output(TRGO)
  2526. * @arg TIM_TRGOSource_OC3Ref: OC3REF signal is used as the trigger output(TRGO)
  2527. * @arg TIM_TRGOSource_OC4Ref: OC4REF signal is used as the trigger output(TRGO)
  2528. *
  2529. * @retval None
  2530. */
  2531. void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t TIM_TRGOSource)
  2532. {
  2533. /* Check the parameters */
  2534. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  2535. assert_param(IS_TIM_TRGO_SOURCE(TIM_TRGOSource));
  2536. /* Reset the MMS Bits */
  2537. TIMx->CR2 &= (uint16_t)~TIM_CR2_MMS;
  2538. /* Select the TRGO source */
  2539. TIMx->CR2 |= TIM_TRGOSource;
  2540. }
  2541. /**
  2542. * @brief Selects the TIMx Slave Mode.
  2543. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM peripheral.
  2544. * @param TIM_SlaveMode: specifies the Timer Slave Mode.
  2545. * This parameter can be one of the following values:
  2546. * @arg TIM_SlaveMode_Reset: Rising edge of the selected trigger signal(TRGI) reinitialize
  2547. * the counter and triggers an update of the registers
  2548. * @arg TIM_SlaveMode_Gated: The counter clock is enabled when the trigger signal (TRGI) is high
  2549. * @arg TIM_SlaveMode_Trigger: The counter starts at a rising edge of the trigger TRGI
  2550. * @arg TIM_SlaveMode_External1: Rising edges of the selected trigger (TRGI) clock the counter
  2551. * @retval None
  2552. */
  2553. void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode)
  2554. {
  2555. /* Check the parameters */
  2556. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2557. assert_param(IS_TIM_SLAVE_MODE(TIM_SlaveMode));
  2558. /* Reset the SMS Bits */
  2559. TIMx->SMCR &= (uint16_t)~TIM_SMCR_SMS;
  2560. /* Select the Slave Mode */
  2561. TIMx->SMCR |= TIM_SlaveMode;
  2562. }
  2563. /**
  2564. * @brief Sets or Resets the TIMx Master/Slave Mode.
  2565. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM peripheral.
  2566. * @param TIM_MasterSlaveMode: specifies the Timer Master Slave Mode.
  2567. * This parameter can be one of the following values:
  2568. * @arg TIM_MasterSlaveMode_Enable: synchronization between the current timer
  2569. * and its slaves (through TRGO)
  2570. * @arg TIM_MasterSlaveMode_Disable: No action
  2571. * @retval None
  2572. */
  2573. void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_MasterSlaveMode)
  2574. {
  2575. /* Check the parameters */
  2576. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2577. assert_param(IS_TIM_MSM_STATE(TIM_MasterSlaveMode));
  2578. /* Reset the MSM Bit */
  2579. TIMx->SMCR &= (uint16_t)~TIM_SMCR_MSM;
  2580. /* Set or Reset the MSM Bit */
  2581. TIMx->SMCR |= TIM_MasterSlaveMode;
  2582. }
  2583. /**
  2584. * @brief Configures the TIMx External Trigger (ETR).
  2585. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2586. * @param TIM_ExtTRGPrescaler: The external Trigger Prescaler.
  2587. * This parameter can be one of the following values:
  2588. * @arg TIM_ExtTRGPSC_OFF: ETRP Prescaler OFF.
  2589. * @arg TIM_ExtTRGPSC_DIV2: ETRP frequency divided by 2.
  2590. * @arg TIM_ExtTRGPSC_DIV4: ETRP frequency divided by 4.
  2591. * @arg TIM_ExtTRGPSC_DIV8: ETRP frequency divided by 8.
  2592. * @param TIM_ExtTRGPolarity: The external Trigger Polarity.
  2593. * This parameter can be one of the following values:
  2594. * @arg TIM_ExtTRGPolarity_Inverted: active low or falling edge active.
  2595. * @arg TIM_ExtTRGPolarity_NonInverted: active high or rising edge active.
  2596. * @param ExtTRGFilter: External Trigger Filter.
  2597. * This parameter must be a value between 0x00 and 0x0F
  2598. * @retval None
  2599. */
  2600. void TIM_ETRConfig(TIM_TypeDef* TIMx, uint16_t TIM_ExtTRGPrescaler,
  2601. uint16_t TIM_ExtTRGPolarity, uint16_t ExtTRGFilter)
  2602. {
  2603. uint16_t tmpsmcr = 0;
  2604. /* Check the parameters */
  2605. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2606. assert_param(IS_TIM_EXT_PRESCALER(TIM_ExtTRGPrescaler));
  2607. assert_param(IS_TIM_EXT_POLARITY(TIM_ExtTRGPolarity));
  2608. assert_param(IS_TIM_EXT_FILTER(ExtTRGFilter));
  2609. tmpsmcr = TIMx->SMCR;
  2610. /* Reset the ETR Bits */
  2611. tmpsmcr &= SMCR_ETR_MASK;
  2612. /* Set the Prescaler, the Filter value and the Polarity */
  2613. tmpsmcr |= (uint16_t)(TIM_ExtTRGPrescaler | (uint16_t)(TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << (uint16_t)8)));
  2614. /* Write to TIMx SMCR */
  2615. TIMx->SMCR = tmpsmcr;
  2616. }
  2617. /**
  2618. * @}
  2619. */
  2620. /** @defgroup TIM_Group8 Specific interface management functions
  2621. * @brief Specific interface management functions
  2622. *
  2623. @verbatim
  2624. ===============================================================================
  2625. Specific interface management functions
  2626. ===============================================================================
  2627. @endverbatim
  2628. * @{
  2629. */
  2630. /**
  2631. * @brief Configures the TIMx Encoder Interface.
  2632. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2633. * peripheral.
  2634. * @param TIM_EncoderMode: specifies the TIMx Encoder Mode.
  2635. * This parameter can be one of the following values:
  2636. * @arg TIM_EncoderMode_TI1: Counter counts on TI1FP1 edge depending on TI2FP2 level.
  2637. * @arg TIM_EncoderMode_TI2: Counter counts on TI2FP2 edge depending on TI1FP1 level.
  2638. * @arg TIM_EncoderMode_TI12: Counter counts on both TI1FP1 and TI2FP2 edges depending
  2639. * on the level of the other input.
  2640. * @param TIM_IC1Polarity: specifies the IC1 Polarity
  2641. * This parameter can be one of the following values:
  2642. * @arg TIM_ICPolarity_Falling: IC Falling edge.
  2643. * @arg TIM_ICPolarity_Rising: IC Rising edge.
  2644. * @param TIM_IC2Polarity: specifies the IC2 Polarity
  2645. * This parameter can be one of the following values:
  2646. * @arg TIM_ICPolarity_Falling: IC Falling edge.
  2647. * @arg TIM_ICPolarity_Rising: IC Rising edge.
  2648. * @retval None
  2649. */
  2650. void TIM_EncoderInterfaceConfig(TIM_TypeDef* TIMx, uint16_t TIM_EncoderMode,
  2651. uint16_t TIM_IC1Polarity, uint16_t TIM_IC2Polarity)
  2652. {
  2653. uint16_t tmpsmcr = 0;
  2654. uint16_t tmpccmr1 = 0;
  2655. uint16_t tmpccer = 0;
  2656. /* Check the parameters */
  2657. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2658. assert_param(IS_TIM_ENCODER_MODE(TIM_EncoderMode));
  2659. assert_param(IS_TIM_IC_POLARITY(TIM_IC1Polarity));
  2660. assert_param(IS_TIM_IC_POLARITY(TIM_IC2Polarity));
  2661. /* Get the TIMx SMCR register value */
  2662. tmpsmcr = TIMx->SMCR;
  2663. /* Get the TIMx CCMR1 register value */
  2664. tmpccmr1 = TIMx->CCMR1;
  2665. /* Get the TIMx CCER register value */
  2666. tmpccer = TIMx->CCER;
  2667. /* Set the encoder Mode */
  2668. tmpsmcr &= (uint16_t)~TIM_SMCR_SMS;
  2669. tmpsmcr |= TIM_EncoderMode;
  2670. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  2671. tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR1_CC2S);
  2672. tmpccmr1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0;
  2673. /* Set the TI1 and the TI2 Polarities */
  2674. tmpccer &= ((uint16_t)~TIM_CCER_CC1P) & ((uint16_t)~TIM_CCER_CC2P);
  2675. tmpccer |= (uint16_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
  2676. /* Write to TIMx SMCR */
  2677. TIMx->SMCR = tmpsmcr;
  2678. /* Write to TIMx CCMR1 */
  2679. TIMx->CCMR1 = tmpccmr1;
  2680. /* Write to TIMx CCER */
  2681. TIMx->CCER = tmpccer;
  2682. }
  2683. /**
  2684. * @brief Enables or disables the TIMx's Hall sensor interface.
  2685. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2686. * peripheral.
  2687. * @param NewState: new state of the TIMx Hall sensor interface.
  2688. * This parameter can be: ENABLE or DISABLE.
  2689. * @retval None
  2690. */
  2691. void TIM_SelectHallSensor(TIM_TypeDef* TIMx, FunctionalState NewState)
  2692. {
  2693. /* Check the parameters */
  2694. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  2695. assert_param(IS_FUNCTIONAL_STATE(NewState));
  2696. if (NewState != DISABLE)
  2697. {
  2698. /* Set the TI1S Bit */
  2699. TIMx->CR2 |= TIM_CR2_TI1S;
  2700. }
  2701. else
  2702. {
  2703. /* Reset the TI1S Bit */
  2704. TIMx->CR2 &= (uint16_t)~TIM_CR2_TI1S;
  2705. }
  2706. }
  2707. /**
  2708. * @}
  2709. */
  2710. /** @defgroup TIM_Group9 Specific remapping management function
  2711. * @brief Specific remapping management function
  2712. *
  2713. @verbatim
  2714. ===============================================================================
  2715. Specific remapping management function
  2716. ===============================================================================
  2717. @endverbatim
  2718. * @{
  2719. */
  2720. /**
  2721. * @brief Configures the TIM2, TIM5 and TIM11 Remapping input capabilities.
  2722. * @param TIMx: where x can be 2, 5 or 11 to select the TIM peripheral.
  2723. * @param TIM_Remap: specifies the TIM input remapping source.
  2724. * This parameter can be one of the following values:
  2725. * @arg TIM2_TIM8_TRGO: TIM2 ITR1 input is connected to TIM8 Trigger output(default)
  2726. * @arg TIM2_ETH_PTP: TIM2 ITR1 input is connected to ETH PTP trogger output.
  2727. * @arg TIM2_USBFS_SOF: TIM2 ITR1 input is connected to USB FS SOF.
  2728. * @arg TIM2_USBHS_SOF: TIM2 ITR1 input is connected to USB HS SOF.
  2729. * @arg TIM5_GPIO: TIM5 CH4 input is connected to dedicated Timer pin(default)
  2730. * @arg TIM5_LSI: TIM5 CH4 input is connected to LSI clock.
  2731. * @arg TIM5_LSE: TIM5 CH4 input is connected to LSE clock.
  2732. * @arg TIM5_RTC: TIM5 CH4 input is connected to RTC Output event.
  2733. * @arg TIM11_GPIO: TIM11 CH4 input is connected to dedicated Timer pin(default)
  2734. * @arg TIM11_HSE: TIM11 CH4 input is connected to HSE_RTC clock
  2735. * (HSE divided by a programmable prescaler)
  2736. * @retval None
  2737. */
  2738. void TIM_RemapConfig(TIM_TypeDef* TIMx, uint16_t TIM_Remap)
  2739. {
  2740. /* Check the parameters */
  2741. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2742. assert_param(IS_TIM_REMAP(TIM_Remap));
  2743. /* Set the Timer remapping configuration */
  2744. TIMx->OR = TIM_Remap;
  2745. }
  2746. /**
  2747. * @}
  2748. */
  2749. /**
  2750. * @brief Configure the TI1 as Input.
  2751. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 10, 11, 12, 13 or 14
  2752. * to select the TIM peripheral.
  2753. * @param TIM_ICPolarity : The Input Polarity.
  2754. * This parameter can be one of the following values:
  2755. * @arg TIM_ICPolarity_Rising
  2756. * @arg TIM_ICPolarity_Falling
  2757. * @arg TIM_ICPolarity_BothEdge
  2758. * @param TIM_ICSelection: specifies the input to be used.
  2759. * This parameter can be one of the following values:
  2760. * @arg TIM_ICSelection_DirectTI: TIM Input 1 is selected to be connected to IC1.
  2761. * @arg TIM_ICSelection_IndirectTI: TIM Input 1 is selected to be connected to IC2.
  2762. * @arg TIM_ICSelection_TRC: TIM Input 1 is selected to be connected to TRC.
  2763. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2764. * This parameter must be a value between 0x00 and 0x0F.
  2765. * @retval None
  2766. */
  2767. static void TI1_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2768. uint16_t TIM_ICFilter)
  2769. {
  2770. uint16_t tmpccmr1 = 0, tmpccer = 0;
  2771. /* Disable the Channel 1: Reset the CC1E Bit */
  2772. TIMx->CCER &= (uint16_t)~TIM_CCER_CC1E;
  2773. tmpccmr1 = TIMx->CCMR1;
  2774. tmpccer = TIMx->CCER;
  2775. /* Select the Input and set the filter */
  2776. tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR1_IC1F);
  2777. tmpccmr1 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2778. /* Select the Polarity and set the CC1E Bit */
  2779. tmpccer &= (uint16_t)~(TIM_CCER_CC1P | TIM_CCER_CC1NP);
  2780. tmpccer |= (uint16_t)(TIM_ICPolarity | (uint16_t)TIM_CCER_CC1E);
  2781. /* Write to TIMx CCMR1 and CCER registers */
  2782. TIMx->CCMR1 = tmpccmr1;
  2783. TIMx->CCER = tmpccer;
  2784. }
  2785. /**
  2786. * @brief Configure the TI2 as Input.
  2787. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9 or 12 to select the TIM
  2788. * peripheral.
  2789. * @param TIM_ICPolarity : The Input Polarity.
  2790. * This parameter can be one of the following values:
  2791. * @arg TIM_ICPolarity_Rising
  2792. * @arg TIM_ICPolarity_Falling
  2793. * @arg TIM_ICPolarity_BothEdge
  2794. * @param TIM_ICSelection: specifies the input to be used.
  2795. * This parameter can be one of the following values:
  2796. * @arg TIM_ICSelection_DirectTI: TIM Input 2 is selected to be connected to IC2.
  2797. * @arg TIM_ICSelection_IndirectTI: TIM Input 2 is selected to be connected to IC1.
  2798. * @arg TIM_ICSelection_TRC: TIM Input 2 is selected to be connected to TRC.
  2799. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2800. * This parameter must be a value between 0x00 and 0x0F.
  2801. * @retval None
  2802. */
  2803. static void TI2_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2804. uint16_t TIM_ICFilter)
  2805. {
  2806. uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;
  2807. /* Disable the Channel 2: Reset the CC2E Bit */
  2808. TIMx->CCER &= (uint16_t)~TIM_CCER_CC2E;
  2809. tmpccmr1 = TIMx->CCMR1;
  2810. tmpccer = TIMx->CCER;
  2811. tmp = (uint16_t)(TIM_ICPolarity << 4);
  2812. /* Select the Input and set the filter */
  2813. tmpccmr1 &= ((uint16_t)~TIM_CCMR1_CC2S) & ((uint16_t)~TIM_CCMR1_IC2F);
  2814. tmpccmr1 |= (uint16_t)(TIM_ICFilter << 12);
  2815. tmpccmr1 |= (uint16_t)(TIM_ICSelection << 8);
  2816. /* Select the Polarity and set the CC2E Bit */
  2817. tmpccer &= (uint16_t)~(TIM_CCER_CC2P | TIM_CCER_CC2NP);
  2818. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC2E);
  2819. /* Write to TIMx CCMR1 and CCER registers */
  2820. TIMx->CCMR1 = tmpccmr1 ;
  2821. TIMx->CCER = tmpccer;
  2822. }
  2823. /**
  2824. * @brief Configure the TI3 as Input.
  2825. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2826. * @param TIM_ICPolarity : The Input Polarity.
  2827. * This parameter can be one of the following values:
  2828. * @arg TIM_ICPolarity_Rising
  2829. * @arg TIM_ICPolarity_Falling
  2830. * @arg TIM_ICPolarity_BothEdge
  2831. * @param TIM_ICSelection: specifies the input to be used.
  2832. * This parameter can be one of the following values:
  2833. * @arg TIM_ICSelection_DirectTI: TIM Input 3 is selected to be connected to IC3.
  2834. * @arg TIM_ICSelection_IndirectTI: TIM Input 3 is selected to be connected to IC4.
  2835. * @arg TIM_ICSelection_TRC: TIM Input 3 is selected to be connected to TRC.
  2836. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2837. * This parameter must be a value between 0x00 and 0x0F.
  2838. * @retval None
  2839. */
  2840. static void TI3_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2841. uint16_t TIM_ICFilter)
  2842. {
  2843. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2844. /* Disable the Channel 3: Reset the CC3E Bit */
  2845. TIMx->CCER &= (uint16_t)~TIM_CCER_CC3E;
  2846. tmpccmr2 = TIMx->CCMR2;
  2847. tmpccer = TIMx->CCER;
  2848. tmp = (uint16_t)(TIM_ICPolarity << 8);
  2849. /* Select the Input and set the filter */
  2850. tmpccmr2 &= ((uint16_t)~TIM_CCMR1_CC1S) & ((uint16_t)~TIM_CCMR2_IC3F);
  2851. tmpccmr2 |= (uint16_t)(TIM_ICSelection | (uint16_t)(TIM_ICFilter << (uint16_t)4));
  2852. /* Select the Polarity and set the CC3E Bit */
  2853. tmpccer &= (uint16_t)~(TIM_CCER_CC3P | TIM_CCER_CC3NP);
  2854. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC3E);
  2855. /* Write to TIMx CCMR2 and CCER registers */
  2856. TIMx->CCMR2 = tmpccmr2;
  2857. TIMx->CCER = tmpccer;
  2858. }
  2859. /**
  2860. * @brief Configure the TI4 as Input.
  2861. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2862. * @param TIM_ICPolarity : The Input Polarity.
  2863. * This parameter can be one of the following values:
  2864. * @arg TIM_ICPolarity_Rising
  2865. * @arg TIM_ICPolarity_Falling
  2866. * @arg TIM_ICPolarity_BothEdge
  2867. * @param TIM_ICSelection: specifies the input to be used.
  2868. * This parameter can be one of the following values:
  2869. * @arg TIM_ICSelection_DirectTI: TIM Input 4 is selected to be connected to IC4.
  2870. * @arg TIM_ICSelection_IndirectTI: TIM Input 4 is selected to be connected to IC3.
  2871. * @arg TIM_ICSelection_TRC: TIM Input 4 is selected to be connected to TRC.
  2872. * @param TIM_ICFilter: Specifies the Input Capture Filter.
  2873. * This parameter must be a value between 0x00 and 0x0F.
  2874. * @retval None
  2875. */
  2876. static void TI4_Config(TIM_TypeDef* TIMx, uint16_t TIM_ICPolarity, uint16_t TIM_ICSelection,
  2877. uint16_t TIM_ICFilter)
  2878. {
  2879. uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2880. /* Disable the Channel 4: Reset the CC4E Bit */
  2881. TIMx->CCER &= (uint16_t)~TIM_CCER_CC4E;
  2882. tmpccmr2 = TIMx->CCMR2;
  2883. tmpccer = TIMx->CCER;
  2884. tmp = (uint16_t)(TIM_ICPolarity << 12);
  2885. /* Select the Input and set the filter */
  2886. tmpccmr2 &= ((uint16_t)~TIM_CCMR1_CC2S) & ((uint16_t)~TIM_CCMR1_IC2F);
  2887. tmpccmr2 |= (uint16_t)(TIM_ICSelection << 8);
  2888. tmpccmr2 |= (uint16_t)(TIM_ICFilter << 12);
  2889. /* Select the Polarity and set the CC4E Bit */
  2890. tmpccer &= (uint16_t)~(TIM_CCER_CC4P | TIM_CCER_CC4NP);
  2891. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC4E);
  2892. /* Write to TIMx CCMR2 and CCER registers */
  2893. TIMx->CCMR2 = tmpccmr2;
  2894. TIMx->CCER = tmpccer ;
  2895. }
  2896. /**
  2897. * @}
  2898. */
  2899. /**
  2900. * @}
  2901. */
  2902. /**
  2903. * @}
  2904. */
  2905. /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/