stm32f0xx_tim.c 130 KB

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