n32g45x_tim.c 119 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_tim.c
  29. * @author Nations
  30. * @version v1.0.4
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #include "n32g45x_tim.h"
  35. #include "n32g45x_rcc.h"
  36. /** @addtogroup N32G45X_StdPeriph_Driver
  37. * @{
  38. */
  39. /** @addtogroup TIM
  40. * @brief TIM driver modules
  41. * @{
  42. */
  43. /** @addtogroup TIM_Private_TypesDefinitions
  44. * @{
  45. */
  46. /**
  47. * @}
  48. */
  49. /** @addtogroup TIM_Private_Defines
  50. * @{
  51. */
  52. /* ---------------------- TIM registers bit mask ------------------------ */
  53. #define SMCTRL_ETR_MASK ((uint16_t)0x00FF)
  54. #define CAPCMPMOD_OFFSET ((uint16_t)0x0018)
  55. #define CAPCMPEN_CCE_SET ((uint16_t)0x0001)
  56. #define CAPCMPEN_CCNE_SET ((uint16_t)0x0004)
  57. /**
  58. * @}
  59. */
  60. /** @addtogroup TIM_Private_Macros
  61. * @{
  62. */
  63. /**
  64. * @}
  65. */
  66. /** @addtogroup TIM_Private_Variables
  67. * @{
  68. */
  69. /**
  70. * @}
  71. */
  72. /** @addtogroup TIM_Private_FunctionPrototypes
  73. * @{
  74. */
  75. static void ConfigTI1(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter);
  76. static void ConfigTI2(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter);
  77. static void ConfigTI3(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter);
  78. static void ConfigTI4(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter);
  79. /**
  80. * @}
  81. */
  82. /** @addtogroup TIM_Private_Macros
  83. * @{
  84. */
  85. /**
  86. * @}
  87. */
  88. /** @addtogroup TIM_Private_Variables
  89. * @{
  90. */
  91. /**
  92. * @}
  93. */
  94. /** @addtogroup TIM_Private_FunctionPrototypes
  95. * @{
  96. */
  97. /**
  98. * @}
  99. */
  100. /** @addtogroup TIM_Private_Functions
  101. * @{
  102. */
  103. /**
  104. * @brief Deinitializes the TIMx peripheral registers to their default reset values.
  105. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  106. */
  107. void TIM_DeInit(TIM_Module* TIMx)
  108. {
  109. /* Check the parameters */
  110. assert_param(IsTimAllModule(TIMx));
  111. if (TIMx == TIM1)
  112. {
  113. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_TIM1, ENABLE);
  114. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_TIM1, DISABLE);
  115. }
  116. else if (TIMx == TIM2)
  117. {
  118. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM2, ENABLE);
  119. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM2, DISABLE);
  120. }
  121. else if (TIMx == TIM3)
  122. {
  123. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM3, ENABLE);
  124. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM3, DISABLE);
  125. }
  126. else if (TIMx == TIM4)
  127. {
  128. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM4, ENABLE);
  129. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM4, DISABLE);
  130. }
  131. else if (TIMx == TIM5)
  132. {
  133. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM5, ENABLE);
  134. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM5, DISABLE);
  135. }
  136. else if (TIMx == TIM6)
  137. {
  138. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM6, ENABLE);
  139. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM6, DISABLE);
  140. }
  141. else if (TIMx == TIM7)
  142. {
  143. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM7, ENABLE);
  144. RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_TIM7, DISABLE);
  145. }
  146. else if (TIMx == TIM8)
  147. {
  148. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_TIM8, ENABLE);
  149. RCC_EnableAPB2PeriphReset(RCC_APB2_PERIPH_TIM8, DISABLE);
  150. }
  151. }
  152. /**
  153. * @brief Initializes the TIMx Time Base Unit peripheral according to
  154. * the specified parameters in the TIM_TimeBaseInitStruct.
  155. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  156. * @param TIM_TimeBaseInitStruct pointer to a TIM_TimeBaseInitType
  157. * structure that contains the configuration information for the
  158. * specified TIM peripheral.
  159. */
  160. void TIM_InitTimeBase(TIM_Module* TIMx, TIM_TimeBaseInitType* TIM_TimeBaseInitStruct)
  161. {
  162. uint32_t tmpcr1 = 0;
  163. /* Check the parameters */
  164. assert_param(IsTimAllModule(TIMx));
  165. assert_param(IsTimCntMode(TIM_TimeBaseInitStruct->CntMode));
  166. assert_param(IsTimClkDiv(TIM_TimeBaseInitStruct->ClkDiv));
  167. tmpcr1 = TIMx->CTRL1;
  168. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  169. {
  170. /* Select the Counter Mode */
  171. tmpcr1 &= (uint32_t)(~((uint32_t)(TIM_CTRL1_DIR | TIM_CTRL1_CAMSEL)));
  172. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->CntMode;
  173. }
  174. if ((TIMx != TIM6) && (TIMx != TIM7))
  175. {
  176. /* Set the clock division */
  177. tmpcr1 &= (uint32_t)(~((uint32_t)TIM_CTRL1_CLKD));
  178. tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->ClkDiv;
  179. }
  180. TIMx->CTRL1 = tmpcr1;
  181. /* Set the Autoreload value */
  182. TIMx->AR = TIM_TimeBaseInitStruct->Period;
  183. /* Set the Prescaler value */
  184. TIMx->PSC = TIM_TimeBaseInitStruct->Prescaler;
  185. if ((TIMx == TIM1) || (TIMx == TIM8))
  186. {
  187. /* Set the Repetition Counter value */
  188. TIMx->REPCNT = TIM_TimeBaseInitStruct->RepetCnt;
  189. }
  190. /* Generate an update event to reload the Prescaler and the Repetition counter
  191. values immediately */
  192. TIMx->EVTGEN = TIM_PSC_RELOAD_MODE_IMMEDIATE;
  193. /*channel input from comp or iom*/
  194. tmpcr1 = TIMx->CTRL1;
  195. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  196. {
  197. if (TIM_TimeBaseInitStruct->CapCh1FromCompEn)
  198. tmpcr1 |= (0x01L << 11);
  199. else
  200. tmpcr1 &= ~(0x01L << 11);
  201. }
  202. if ((TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  203. {
  204. if (TIM_TimeBaseInitStruct->CapCh2FromCompEn)
  205. tmpcr1 |= (0x01L << 12);
  206. else
  207. tmpcr1 &= ~(0x01L << 12);
  208. if (TIM_TimeBaseInitStruct->CapCh3FromCompEn)
  209. tmpcr1 |= (0x01L << 13);
  210. else
  211. tmpcr1 &= ~(0x01L << 13);
  212. }
  213. if ((TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4))
  214. {
  215. if (TIM_TimeBaseInitStruct->CapCh4FromCompEn)
  216. tmpcr1 |= (0x01L << 14);
  217. else
  218. tmpcr1 &= ~(0x01L << 14);
  219. }
  220. /*etr input from comp or iom*/
  221. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4))
  222. {
  223. if (TIM_TimeBaseInitStruct->CapEtrClrFromCompEn)
  224. tmpcr1 |= (0x01L << 15);
  225. else
  226. tmpcr1 &= ~(0x01L << 15);
  227. }
  228. TIMx->CTRL1 = tmpcr1;
  229. /*sel etr from iom or tsc*/
  230. tmpcr1 = TIMx->CTRL2;
  231. if ((TIMx == TIM2) || (TIMx == TIM4))
  232. {
  233. if (TIM_TimeBaseInitStruct->CapEtrSelFromTscEn)
  234. tmpcr1 |= (0x01L << 8);
  235. else
  236. tmpcr1 &= ~(0x01L << 8);
  237. }
  238. TIMx->CTRL2 = tmpcr1;
  239. }
  240. /**
  241. * @brief Initializes the TIMx Channel1 according to the specified
  242. * parameters in the TIM_OCInitStruct.
  243. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  244. * @param TIM_OCInitStruct pointer to a OCInitType structure
  245. * that contains the configuration information for the specified TIM peripheral.
  246. */
  247. void TIM_InitOc1(TIM_Module* TIMx, OCInitType* TIM_OCInitStruct)
  248. {
  249. uint16_t tmpccmrx = 0;
  250. uint32_t tmpccer = 0, tmpcr2 = 0;
  251. /* Check the parameters */
  252. assert_param(IsTimList8Module(TIMx));
  253. assert_param(IsTimOcMode(TIM_OCInitStruct->OcMode));
  254. assert_param(IsTimOutputState(TIM_OCInitStruct->OutputState));
  255. assert_param(IsTimOcPolarity(TIM_OCInitStruct->OcPolarity));
  256. /* Disable the Channel 1: Reset the CC1E Bit */
  257. TIMx->CCEN &= (uint32_t)(~(uint32_t)TIM_CCEN_CC1EN);
  258. /* Get the TIMx CCEN register value */
  259. tmpccer = TIMx->CCEN;
  260. /* Get the TIMx CTRL2 register value */
  261. tmpcr2 = TIMx->CTRL2;
  262. /* Get the TIMx CCMOD1 register value */
  263. tmpccmrx = TIMx->CCMOD1;
  264. /* Reset the Output Compare Mode Bits */
  265. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD1_OC1M));
  266. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD1_CC1SEL));
  267. /* Select the Output Compare Mode */
  268. tmpccmrx |= TIM_OCInitStruct->OcMode;
  269. /* Reset the Output Polarity level */
  270. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC1P));
  271. /* Set the Output Compare Polarity */
  272. tmpccer |= TIM_OCInitStruct->OcPolarity;
  273. /* Set the Output State */
  274. tmpccer |= TIM_OCInitStruct->OutputState;
  275. if ((TIMx == TIM1) || (TIMx == TIM8))
  276. {
  277. assert_param(IsTimOutputNState(TIM_OCInitStruct->OutputNState));
  278. assert_param(IsTimOcnPolarity(TIM_OCInitStruct->OcNPolarity));
  279. assert_param(IsTimOcnIdleState(TIM_OCInitStruct->OcNIdleState));
  280. assert_param(IsTimOcIdleState(TIM_OCInitStruct->OcIdleState));
  281. /* Reset the Output N Polarity level */
  282. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC1NP));
  283. /* Set the Output N Polarity */
  284. tmpccer |= TIM_OCInitStruct->OcNPolarity;
  285. /* Reset the Output N State */
  286. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC1NEN));
  287. /* Set the Output N State */
  288. tmpccer |= TIM_OCInitStruct->OutputNState;
  289. /* Reset the Output Compare and Output Compare N IDLE State */
  290. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI1));
  291. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI1N));
  292. /* Set the Output Idle state */
  293. tmpcr2 |= TIM_OCInitStruct->OcIdleState;
  294. /* Set the Output N Idle state */
  295. tmpcr2 |= TIM_OCInitStruct->OcNIdleState;
  296. }
  297. /* Write to TIMx CTRL2 */
  298. TIMx->CTRL2 = tmpcr2;
  299. /* Write to TIMx CCMOD1 */
  300. TIMx->CCMOD1 = tmpccmrx;
  301. /* Set the Capture Compare Register value */
  302. TIMx->CCDAT1 = TIM_OCInitStruct->Pulse;
  303. /* Write to TIMx CCEN */
  304. TIMx->CCEN = tmpccer;
  305. }
  306. /**
  307. * @brief Initializes the TIMx Channel2 according to the specified
  308. * parameters in the TIM_OCInitStruct.
  309. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select
  310. * the TIM peripheral.
  311. * @param TIM_OCInitStruct pointer to a OCInitType structure
  312. * that contains the configuration information for the specified TIM peripheral.
  313. */
  314. void TIM_InitOc2(TIM_Module* TIMx, OCInitType* TIM_OCInitStruct)
  315. {
  316. uint16_t tmpccmrx = 0;
  317. uint32_t tmpccer = 0, tmpcr2 = 0;
  318. /* Check the parameters */
  319. assert_param(IsTimList6Module(TIMx));
  320. assert_param(IsTimOcMode(TIM_OCInitStruct->OcMode));
  321. assert_param(IsTimOutputState(TIM_OCInitStruct->OutputState));
  322. assert_param(IsTimOcPolarity(TIM_OCInitStruct->OcPolarity));
  323. /* Disable the Channel 2: Reset the CC2E Bit */
  324. TIMx->CCEN &= (uint32_t)(~((uint32_t)TIM_CCEN_CC2EN));
  325. /* Get the TIMx CCEN register value */
  326. tmpccer = TIMx->CCEN;
  327. /* Get the TIMx CTRL2 register value */
  328. tmpcr2 = TIMx->CTRL2;
  329. /* Get the TIMx CCMOD1 register value */
  330. tmpccmrx = TIMx->CCMOD1;
  331. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  332. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD1_OC2M));
  333. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD1_CC2SEL));
  334. /* Select the Output Compare Mode */
  335. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->OcMode << 8);
  336. /* Reset the Output Polarity level */
  337. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC2P));
  338. /* Set the Output Compare Polarity */
  339. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcPolarity << 4);
  340. /* Set the Output State */
  341. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputState << 4);
  342. if ((TIMx == TIM1) || (TIMx == TIM8))
  343. {
  344. assert_param(IsTimOutputNState(TIM_OCInitStruct->OutputNState));
  345. assert_param(IsTimOcnPolarity(TIM_OCInitStruct->OcNPolarity));
  346. assert_param(IsTimOcnIdleState(TIM_OCInitStruct->OcNIdleState));
  347. assert_param(IsTimOcIdleState(TIM_OCInitStruct->OcIdleState));
  348. /* Reset the Output N Polarity level */
  349. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC2NP));
  350. /* Set the Output N Polarity */
  351. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcNPolarity << 4);
  352. /* Reset the Output N State */
  353. tmpccer &= (uint32_t)(~((uint16_t)TIM_CCEN_CC2NEN));
  354. /* Set the Output N State */
  355. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputNState << 4);
  356. /* Reset the Output Compare and Output Compare N IDLE State */
  357. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI2));
  358. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI2N));
  359. /* Set the Output Idle state */
  360. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcIdleState << 2);
  361. /* Set the Output N Idle state */
  362. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcNIdleState << 2);
  363. }
  364. /* Write to TIMx CTRL2 */
  365. TIMx->CTRL2 = tmpcr2;
  366. /* Write to TIMx CCMOD1 */
  367. TIMx->CCMOD1 = tmpccmrx;
  368. /* Set the Capture Compare Register value */
  369. TIMx->CCDAT2 = TIM_OCInitStruct->Pulse;
  370. /* Write to TIMx CCEN */
  371. TIMx->CCEN = tmpccer;
  372. }
  373. /**
  374. * @brief Initializes the TIMx Channel3 according to the specified
  375. * parameters in the TIM_OCInitStruct.
  376. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  377. * @param TIM_OCInitStruct pointer to a OCInitType structure
  378. * that contains the configuration information for the specified TIM peripheral.
  379. */
  380. void TIM_InitOc3(TIM_Module* TIMx, OCInitType* TIM_OCInitStruct)
  381. {
  382. uint16_t tmpccmrx = 0;
  383. uint32_t tmpccer = 0, tmpcr2 = 0;
  384. /* Check the parameters */
  385. assert_param(IsTimList3Module(TIMx));
  386. assert_param(IsTimOcMode(TIM_OCInitStruct->OcMode));
  387. assert_param(IsTimOutputState(TIM_OCInitStruct->OutputState));
  388. assert_param(IsTimOcPolarity(TIM_OCInitStruct->OcPolarity));
  389. /* Disable the Channel 2: Reset the CC2E Bit */
  390. TIMx->CCEN &= (uint32_t)(~((uint32_t)TIM_CCEN_CC3EN));
  391. /* Get the TIMx CCEN register value */
  392. tmpccer = TIMx->CCEN;
  393. /* Get the TIMx CTRL2 register value */
  394. tmpcr2 = TIMx->CTRL2;
  395. /* Get the TIMx CCMOD2 register value */
  396. tmpccmrx = TIMx->CCMOD2;
  397. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  398. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD2_OC3MD));
  399. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD2_CC3SEL));
  400. /* Select the Output Compare Mode */
  401. tmpccmrx |= TIM_OCInitStruct->OcMode;
  402. /* Reset the Output Polarity level */
  403. tmpccer &= (uint32_t)(~((uint16_t)TIM_CCEN_CC3P));
  404. /* Set the Output Compare Polarity */
  405. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcPolarity << 8);
  406. /* Set the Output State */
  407. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputState << 8);
  408. if ((TIMx == TIM1) || (TIMx == TIM8))
  409. {
  410. assert_param(IsTimOutputNState(TIM_OCInitStruct->OutputNState));
  411. assert_param(IsTimOcnPolarity(TIM_OCInitStruct->OcNPolarity));
  412. assert_param(IsTimOcnIdleState(TIM_OCInitStruct->OcNIdleState));
  413. assert_param(IsTimOcIdleState(TIM_OCInitStruct->OcIdleState));
  414. /* Reset the Output N Polarity level */
  415. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC3NP));
  416. /* Set the Output N Polarity */
  417. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcNPolarity << 8);
  418. /* Reset the Output N State */
  419. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC3NEN));
  420. /* Set the Output N State */
  421. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputNState << 8);
  422. /* Reset the Output Compare and Output Compare N IDLE State */
  423. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI3));
  424. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI3N));
  425. /* Set the Output Idle state */
  426. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcIdleState << 4);
  427. /* Set the Output N Idle state */
  428. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcNIdleState << 4);
  429. }
  430. /* Write to TIMx CTRL2 */
  431. TIMx->CTRL2 = tmpcr2;
  432. /* Write to TIMx CCMOD2 */
  433. TIMx->CCMOD2 = tmpccmrx;
  434. /* Set the Capture Compare Register value */
  435. TIMx->CCDAT3 = TIM_OCInitStruct->Pulse;
  436. /* Write to TIMx CCEN */
  437. TIMx->CCEN = tmpccer;
  438. }
  439. /**
  440. * @brief Initializes the TIMx Channel4 according to the specified
  441. * parameters in the TIM_OCInitStruct.
  442. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  443. * @param TIM_OCInitStruct pointer to a OCInitType structure
  444. * that contains the configuration information for the specified TIM peripheral.
  445. */
  446. void TIM_InitOc4(TIM_Module* TIMx, OCInitType* TIM_OCInitStruct)
  447. {
  448. uint16_t tmpccmrx = 0;
  449. uint32_t tmpccer = 0, tmpcr2 = 0;
  450. /* Check the parameters */
  451. assert_param(IsTimList3Module(TIMx));
  452. assert_param(IsTimOcMode(TIM_OCInitStruct->OcMode));
  453. assert_param(IsTimOutputState(TIM_OCInitStruct->OutputState));
  454. assert_param(IsTimOcPolarity(TIM_OCInitStruct->OcPolarity));
  455. /* Disable the Channel 2: Reset the CC4E Bit */
  456. TIMx->CCEN &= (uint32_t)(~((uint32_t)TIM_CCEN_CC4EN));
  457. /* Get the TIMx CCEN register value */
  458. tmpccer = TIMx->CCEN;
  459. /* Get the TIMx CTRL2 register value */
  460. tmpcr2 = TIMx->CTRL2;
  461. /* Get the TIMx CCMOD2 register value */
  462. tmpccmrx = TIMx->CCMOD2;
  463. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  464. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD2_OC4MD));
  465. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD2_CC4SEL));
  466. /* Select the Output Compare Mode */
  467. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->OcMode << 8);
  468. /* Reset the Output Polarity level */
  469. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC4P));
  470. /* Set the Output Compare Polarity */
  471. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcPolarity << 12);
  472. /* Set the Output State */
  473. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputState << 12);
  474. if ((TIMx == TIM1) || (TIMx == TIM8))
  475. {
  476. assert_param(IsTimOcIdleState(TIM_OCInitStruct->OcIdleState));
  477. /* Reset the Output Compare IDLE State */
  478. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI4));
  479. /* Set the Output Idle state */
  480. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcIdleState << 6);
  481. }
  482. /* Write to TIMx CTRL2 */
  483. TIMx->CTRL2 = tmpcr2;
  484. /* Write to TIMx CCMOD2 */
  485. TIMx->CCMOD2 = tmpccmrx;
  486. /* Set the Capture Compare Register value */
  487. TIMx->CCDAT4 = TIM_OCInitStruct->Pulse;
  488. /* Write to TIMx CCEN */
  489. TIMx->CCEN = tmpccer;
  490. }
  491. /**
  492. * @brief Initializes the TIMx Channel5 according to the specified
  493. * parameters in the TIM_OCInitStruct.
  494. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  495. * @param TIM_OCInitStruct pointer to a OCInitType structure
  496. * that contains the configuration information for the specified TIM peripheral.
  497. */
  498. void TIM_InitOc5(TIM_Module* TIMx, OCInitType* TIM_OCInitStruct)
  499. {
  500. uint16_t tmpccmrx = 0;
  501. uint32_t tmpccer = 0, tmpcr2 = 0;
  502. /* Check the parameters */
  503. assert_param(IsTimList1Module(TIMx));
  504. assert_param(IsTimOcMode(TIM_OCInitStruct->OcMode));
  505. assert_param(IsTimOutputState(TIM_OCInitStruct->OutputState));
  506. assert_param(IsTimOcPolarity(TIM_OCInitStruct->OcPolarity));
  507. /* Disable the Channel 5: Reset the CC5E Bit */
  508. TIMx->CCEN &= (uint32_t)(~((uint32_t)TIM_CCEN_CC5EN));
  509. /* Get the TIMx CCEN register value */
  510. tmpccer = TIMx->CCEN;
  511. /* Get the TIMx CTRL2 register value */
  512. tmpcr2 = TIMx->CTRL2;
  513. /* Get the TIMx CCMOD3 register value */
  514. tmpccmrx = TIMx->CCMOD3;
  515. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  516. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD3_OC5MD));
  517. /* Select the Output Compare Mode */
  518. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->OcMode);
  519. /* Reset the Output Polarity level */
  520. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC5P));
  521. /* Set the Output Compare Polarity */
  522. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcPolarity << 16);
  523. /* Set the Output State */
  524. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputState << 16);
  525. if ((TIMx == TIM1) || (TIMx == TIM8))
  526. {
  527. assert_param(IsTimOcIdleState(TIM_OCInitStruct->OcIdleState));
  528. /* Reset the Output Compare IDLE State */
  529. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI5));
  530. /* Set the Output Idle state */
  531. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcIdleState << 8);
  532. }
  533. /* Write to TIMx CTRL2 */
  534. TIMx->CTRL2 = tmpcr2;
  535. /* Write to TIMx CCMOD3 */
  536. TIMx->CCMOD3 = tmpccmrx;
  537. /* Set the Capture Compare Register value */
  538. TIMx->CCDAT5 = TIM_OCInitStruct->Pulse;
  539. /* Write to TIMx CCEN */
  540. TIMx->CCEN = tmpccer;
  541. }
  542. /**
  543. * @brief Initializes the TIMx Channel6 according to the specified
  544. * parameters in the TIM_OCInitStruct.
  545. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  546. * @param TIM_OCInitStruct pointer to a OCInitType structure
  547. * that contains the configuration information for the specified TIM peripheral.
  548. */
  549. void TIM_InitOc6(TIM_Module* TIMx, OCInitType* TIM_OCInitStruct)
  550. {
  551. uint16_t tmpccmrx = 0;
  552. uint32_t tmpccer = 0, tmpcr2 = 0;
  553. /* Check the parameters */
  554. assert_param(IsTimList1Module(TIMx));
  555. assert_param(IsTimOcMode(TIM_OCInitStruct->OcMode));
  556. assert_param(IsTimOutputState(TIM_OCInitStruct->OutputState));
  557. assert_param(IsTimOcPolarity(TIM_OCInitStruct->OcPolarity));
  558. /* Disable the Channel 6: Reset the CC6E Bit */
  559. TIMx->CCEN &= (uint32_t)(~((uint32_t)TIM_CCEN_CC6EN));
  560. /* Get the TIMx CCEN register value */
  561. tmpccer = TIMx->CCEN;
  562. /* Get the TIMx CTRL2 register value */
  563. tmpcr2 = TIMx->CTRL2;
  564. /* Get the TIMx CCMOD3 register value */
  565. tmpccmrx = TIMx->CCMOD3;
  566. /* Reset the Output Compare mode and Capture/Compare selection Bits */
  567. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMOD3_OC6MD));
  568. /* Select the Output Compare Mode */
  569. tmpccmrx |= (uint16_t)(TIM_OCInitStruct->OcMode << 8);
  570. /* Reset the Output Polarity level */
  571. tmpccer &= (uint32_t)(~((uint32_t)TIM_CCEN_CC6P));
  572. /* Set the Output Compare Polarity */
  573. tmpccer |= (uint32_t)(TIM_OCInitStruct->OcPolarity << 20);
  574. /* Set the Output State */
  575. tmpccer |= (uint32_t)(TIM_OCInitStruct->OutputState << 20);
  576. if ((TIMx == TIM1) || (TIMx == TIM8))
  577. {
  578. assert_param(IsTimOcIdleState(TIM_OCInitStruct->OcIdleState));
  579. /* Reset the Output Compare IDLE State */
  580. tmpcr2 &= (uint32_t)(~((uint32_t)TIM_CTRL2_OI6));
  581. /* Set the Output Idle state */
  582. tmpcr2 |= (uint32_t)(TIM_OCInitStruct->OcIdleState << 10);
  583. }
  584. /* Write to TIMx CTRL2 */
  585. TIMx->CTRL2 = tmpcr2;
  586. /* Write to TIMx CCMOD3 */
  587. TIMx->CCMOD3 = tmpccmrx;
  588. /* Set the Capture Compare Register value */
  589. TIMx->CCDAT6 = TIM_OCInitStruct->Pulse;
  590. /* Write to TIMx CCEN */
  591. TIMx->CCEN = tmpccer;
  592. }
  593. /**
  594. * @brief Initializes the TIM peripheral according to the specified
  595. * parameters in the TIM_ICInitStruct.
  596. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  597. * @param TIM_ICInitStruct pointer to a TIM_ICInitType structure
  598. * that contains the configuration information for the specified TIM peripheral.
  599. */
  600. void TIM_ICInit(TIM_Module* TIMx, TIM_ICInitType* TIM_ICInitStruct)
  601. {
  602. /* Check the parameters */
  603. assert_param(IsTimCh(TIM_ICInitStruct->Channel));
  604. assert_param(IsTimIcSelection(TIM_ICInitStruct->IcSelection));
  605. assert_param(IsTimIcPrescaler(TIM_ICInitStruct->IcPrescaler));
  606. assert_param(IsTimInCapFilter(TIM_ICInitStruct->IcFilter));
  607. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  608. {
  609. assert_param(IsTimIcPalaritySingleEdge(TIM_ICInitStruct->IcPolarity));
  610. }
  611. else
  612. {
  613. assert_param(IsTimIcPolarityAnyEdge(TIM_ICInitStruct->IcPolarity));
  614. }
  615. if (TIM_ICInitStruct->Channel == TIM_CH_1)
  616. {
  617. assert_param(IsTimList8Module(TIMx));
  618. /* TI1 Configuration */
  619. ConfigTI1(TIMx, TIM_ICInitStruct->IcPolarity, TIM_ICInitStruct->IcSelection, TIM_ICInitStruct->IcFilter);
  620. /* Set the Input Capture Prescaler value */
  621. TIM_SetInCap1Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  622. }
  623. else if (TIM_ICInitStruct->Channel == TIM_CH_2)
  624. {
  625. assert_param(IsTimList6Module(TIMx));
  626. /* TI2 Configuration */
  627. ConfigTI2(TIMx, TIM_ICInitStruct->IcPolarity, TIM_ICInitStruct->IcSelection, TIM_ICInitStruct->IcFilter);
  628. /* Set the Input Capture Prescaler value */
  629. TIM_SetInCap2Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  630. }
  631. else if (TIM_ICInitStruct->Channel == TIM_CH_3)
  632. {
  633. assert_param(IsTimList3Module(TIMx));
  634. /* TI3 Configuration */
  635. ConfigTI3(TIMx, TIM_ICInitStruct->IcPolarity, TIM_ICInitStruct->IcSelection, TIM_ICInitStruct->IcFilter);
  636. /* Set the Input Capture Prescaler value */
  637. TIM_SetInCap3Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  638. }
  639. else
  640. {
  641. assert_param(IsTimList3Module(TIMx));
  642. /* TI4 Configuration */
  643. ConfigTI4(TIMx, TIM_ICInitStruct->IcPolarity, TIM_ICInitStruct->IcSelection, TIM_ICInitStruct->IcFilter);
  644. /* Set the Input Capture Prescaler value */
  645. TIM_SetInCap4Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  646. }
  647. }
  648. /**
  649. * @brief Configures the TIM peripheral according to the specified
  650. * parameters in the TIM_ICInitStruct to measure an external PWM signal.
  651. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  652. * @param TIM_ICInitStruct pointer to a TIM_ICInitType structure
  653. * that contains the configuration information for the specified TIM peripheral.
  654. */
  655. void TIM_ConfigPwmIc(TIM_Module* TIMx, TIM_ICInitType* TIM_ICInitStruct)
  656. {
  657. uint16_t icoppositepolarity = TIM_IC_POLARITY_RISING;
  658. uint16_t icoppositeselection = TIM_IC_SELECTION_DIRECTTI;
  659. /* Check the parameters */
  660. assert_param(IsTimList6Module(TIMx));
  661. /* Select the Opposite Input Polarity */
  662. if (TIM_ICInitStruct->IcPolarity == TIM_IC_POLARITY_RISING)
  663. {
  664. icoppositepolarity = TIM_IC_POLARITY_FALLING;
  665. }
  666. else
  667. {
  668. icoppositepolarity = TIM_IC_POLARITY_RISING;
  669. }
  670. /* Select the Opposite Input */
  671. if (TIM_ICInitStruct->IcSelection == TIM_IC_SELECTION_DIRECTTI)
  672. {
  673. icoppositeselection = TIM_IC_SELECTION_INDIRECTTI;
  674. }
  675. else
  676. {
  677. icoppositeselection = TIM_IC_SELECTION_DIRECTTI;
  678. }
  679. if (TIM_ICInitStruct->Channel == TIM_CH_1)
  680. {
  681. /* TI1 Configuration */
  682. ConfigTI1(TIMx, TIM_ICInitStruct->IcPolarity, TIM_ICInitStruct->IcSelection, TIM_ICInitStruct->IcFilter);
  683. /* Set the Input Capture Prescaler value */
  684. TIM_SetInCap1Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  685. /* TI2 Configuration */
  686. ConfigTI2(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->IcFilter);
  687. /* Set the Input Capture Prescaler value */
  688. TIM_SetInCap2Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  689. }
  690. else
  691. {
  692. /* TI2 Configuration */
  693. ConfigTI2(TIMx, TIM_ICInitStruct->IcPolarity, TIM_ICInitStruct->IcSelection, TIM_ICInitStruct->IcFilter);
  694. /* Set the Input Capture Prescaler value */
  695. TIM_SetInCap2Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  696. /* TI1 Configuration */
  697. ConfigTI1(TIMx, icoppositepolarity, icoppositeselection, TIM_ICInitStruct->IcFilter);
  698. /* Set the Input Capture Prescaler value */
  699. TIM_SetInCap1Prescaler(TIMx, TIM_ICInitStruct->IcPrescaler);
  700. }
  701. }
  702. /**
  703. * @brief Configures the: Break feature, dead time, Lock level, the OSSI,
  704. * the OSSR State and the AOE(automatic output enable).
  705. * @param TIMx where x can be 1 or 8 to select the TIM
  706. * @param TIM_BDTRInitStruct pointer to a TIM_BDTRInitType structure that
  707. * contains the BKDT Register configuration information for the TIM peripheral.
  708. */
  709. void TIM_ConfigBkdt(TIM_Module* TIMx, TIM_BDTRInitType* TIM_BDTRInitStruct)
  710. {
  711. uint32_t tmp;
  712. /* Check the parameters */
  713. assert_param(IsTimList2Module(TIMx));
  714. assert_param(IsTimOssrState(TIM_BDTRInitStruct->OssrState));
  715. assert_param(IsTimOssiState(TIM_BDTRInitStruct->OssiState));
  716. assert_param(IsTimLockLevel(TIM_BDTRInitStruct->LockLevel));
  717. assert_param(IsTimBreakInState(TIM_BDTRInitStruct->Break));
  718. assert_param(IsTimBreakPalarity(TIM_BDTRInitStruct->BreakPolarity));
  719. assert_param(IsTimAutoOutputState(TIM_BDTRInitStruct->AutomaticOutput));
  720. /* Set the Lock level, the Break enable Bit and the Ploarity, the OSSR State,
  721. the OSSI State, the dead time value and the Automatic Output Enable Bit */
  722. TIMx->BKDT = (uint32_t)TIM_BDTRInitStruct->OssrState | TIM_BDTRInitStruct->OssiState | TIM_BDTRInitStruct->LockLevel
  723. | TIM_BDTRInitStruct->DeadTime | TIM_BDTRInitStruct->Break | TIM_BDTRInitStruct->BreakPolarity
  724. | TIM_BDTRInitStruct->AutomaticOutput;
  725. /*cofigure other break in*/
  726. tmp = TIMx->CTRL1;
  727. /*IOMBKPEN 0 meaning iom as break enable*/
  728. if (TIM_BDTRInitStruct->IomBreakEn)
  729. tmp &= ~(0x01L << 10);
  730. else
  731. tmp |= (0x01L << 10);
  732. if (TIM_BDTRInitStruct->LockUpBreakEn)
  733. tmp |= (0x01L << 16);
  734. else
  735. tmp &= ~(0x01L << 16);
  736. if (TIM_BDTRInitStruct->PvdBreakEn)
  737. tmp |= (0x01L << 17);
  738. else
  739. tmp &= ~(0x01L << 17);
  740. TIMx->CTRL1 = tmp;
  741. }
  742. /**
  743. * @brief Fills each TIM_TimeBaseInitStruct member with its default value.
  744. * @param TIM_TimeBaseInitStruct pointer to a TIM_TimeBaseInitType
  745. * structure which will be initialized.
  746. */
  747. void TIM_InitTimBaseStruct(TIM_TimeBaseInitType* TIM_TimeBaseInitStruct)
  748. {
  749. /* Set the default configuration */
  750. TIM_TimeBaseInitStruct->Period = 0xFFFF;
  751. TIM_TimeBaseInitStruct->Prescaler = 0x0000;
  752. TIM_TimeBaseInitStruct->ClkDiv = TIM_CLK_DIV1;
  753. TIM_TimeBaseInitStruct->CntMode = TIM_CNT_MODE_UP;
  754. TIM_TimeBaseInitStruct->RepetCnt = 0x0000;
  755. TIM_TimeBaseInitStruct->CapCh1FromCompEn = false;
  756. TIM_TimeBaseInitStruct->CapCh2FromCompEn = false;
  757. TIM_TimeBaseInitStruct->CapCh3FromCompEn = false;
  758. TIM_TimeBaseInitStruct->CapCh4FromCompEn = false;
  759. TIM_TimeBaseInitStruct->CapEtrClrFromCompEn = false;
  760. TIM_TimeBaseInitStruct->CapEtrSelFromTscEn = false;
  761. }
  762. /**
  763. * @brief Fills each TIM_OCInitStruct member with its default value.
  764. * @param TIM_OCInitStruct pointer to a OCInitType structure which will
  765. * be initialized.
  766. */
  767. void TIM_InitOcStruct(OCInitType* TIM_OCInitStruct)
  768. {
  769. /* Set the default configuration */
  770. TIM_OCInitStruct->OcMode = TIM_OCMODE_TIMING;
  771. TIM_OCInitStruct->OutputState = TIM_OUTPUT_STATE_DISABLE;
  772. TIM_OCInitStruct->OutputNState = TIM_OUTPUT_NSTATE_DISABLE;
  773. TIM_OCInitStruct->Pulse = 0x0000;
  774. TIM_OCInitStruct->OcPolarity = TIM_OC_POLARITY_HIGH;
  775. TIM_OCInitStruct->OcNPolarity = TIM_OC_POLARITY_HIGH;
  776. TIM_OCInitStruct->OcIdleState = TIM_OC_IDLE_STATE_RESET;
  777. TIM_OCInitStruct->OcNIdleState = TIM_OCN_IDLE_STATE_RESET;
  778. }
  779. /**
  780. * @brief Fills each TIM_ICInitStruct member with its default value.
  781. * @param TIM_ICInitStruct pointer to a TIM_ICInitType structure which will
  782. * be initialized.
  783. */
  784. void TIM_InitIcStruct(TIM_ICInitType* TIM_ICInitStruct)
  785. {
  786. /* Set the default configuration */
  787. TIM_ICInitStruct->Channel = TIM_CH_1;
  788. TIM_ICInitStruct->IcPolarity = TIM_IC_POLARITY_RISING;
  789. TIM_ICInitStruct->IcSelection = TIM_IC_SELECTION_DIRECTTI;
  790. TIM_ICInitStruct->IcPrescaler = TIM_IC_PSC_DIV1;
  791. TIM_ICInitStruct->IcFilter = 0x00;
  792. }
  793. /**
  794. * @brief Fills each TIM_BDTRInitStruct member with its default value.
  795. * @param TIM_BDTRInitStruct pointer to a TIM_BDTRInitType structure which
  796. * will be initialized.
  797. */
  798. void TIM_InitBkdtStruct(TIM_BDTRInitType* TIM_BDTRInitStruct)
  799. {
  800. /* Set the default configuration */
  801. TIM_BDTRInitStruct->OssrState = TIM_OSSR_STATE_DISABLE;
  802. TIM_BDTRInitStruct->OssiState = TIM_OSSI_STATE_DISABLE;
  803. TIM_BDTRInitStruct->LockLevel = TIM_LOCK_LEVEL_OFF;
  804. TIM_BDTRInitStruct->DeadTime = 0x00;
  805. TIM_BDTRInitStruct->Break = TIM_BREAK_IN_DISABLE;
  806. TIM_BDTRInitStruct->BreakPolarity = TIM_BREAK_POLARITY_LOW;
  807. TIM_BDTRInitStruct->AutomaticOutput = TIM_AUTO_OUTPUT_DISABLE;
  808. }
  809. /**
  810. * @brief Enables or disables the specified TIM peripheral.
  811. * @param TIMx where x can be 1 to 8 to select the TIMx peripheral.
  812. * @param Cmd new state of the TIMx peripheral.
  813. * This parameter can be: ENABLE or DISABLE.
  814. */
  815. void TIM_Enable(TIM_Module* TIMx, FunctionalState Cmd)
  816. {
  817. /* Check the parameters */
  818. assert_param(IsTimAllModule(TIMx));
  819. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  820. if (Cmd != DISABLE)
  821. {
  822. /* Enable the TIM Counter */
  823. TIMx->CTRL1 |= TIM_CTRL1_CNTEN;
  824. }
  825. else
  826. {
  827. /* Disable the TIM Counter */
  828. TIMx->CTRL1 &= (uint32_t)(~((uint32_t)TIM_CTRL1_CNTEN));
  829. }
  830. }
  831. /**
  832. * @brief Enables or disables the TIM peripheral Main Outputs.
  833. * @param TIMx where x can be 1, 8 to select the TIMx peripheral.
  834. * @param Cmd new state of the TIM peripheral Main Outputs.
  835. * This parameter can be: ENABLE or DISABLE.
  836. */
  837. void TIM_EnableCtrlPwmOutputs(TIM_Module* TIMx, FunctionalState Cmd)
  838. {
  839. /* Check the parameters */
  840. assert_param(IsTimList2Module(TIMx));
  841. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  842. if (Cmd != DISABLE)
  843. {
  844. /* Enable the TIM Main Output */
  845. TIMx->BKDT |= TIM_BKDT_MOEN;
  846. }
  847. else
  848. {
  849. /* Disable the TIM Main Output */
  850. TIMx->BKDT &= (uint16_t)(~((uint16_t)TIM_BKDT_MOEN));
  851. }
  852. }
  853. /**
  854. * @brief Enables or disables the specified TIM interrupts.
  855. * @param TIMx where x can be 1 to 8 to select the TIMx peripheral.
  856. * @param TIM_IT specifies the TIM interrupts sources to be enabled or disabled.
  857. * This parameter can be any combination of the following values:
  858. * @arg TIM_INT_UPDATE TIM update Interrupt source
  859. * @arg TIM_INT_CC1 TIM Capture Compare 1 Interrupt source
  860. * @arg TIM_INT_CC2 TIM Capture Compare 2 Interrupt source
  861. * @arg TIM_INT_CC3 TIM Capture Compare 3 Interrupt source
  862. * @arg TIM_INT_CC4 TIM Capture Compare 4 Interrupt source
  863. * @arg TIM_INT_COM TIM Commutation Interrupt source
  864. * @arg TIM_INT_TRIG TIM Trigger Interrupt source
  865. * @arg TIM_INT_BREAK TIM Break Interrupt source
  866. * @note
  867. * - TIM6 and TIM7 can only generate an update interrupt.
  868. * - TIM_INT_BREAK is used only with TIM1, TIM8.
  869. * - TIM_INT_COM is used only with TIM1, TIM8.
  870. * @param Cmd new state of the TIM interrupts.
  871. * This parameter can be: ENABLE or DISABLE.
  872. */
  873. void TIM_ConfigInt(TIM_Module* TIMx, uint16_t TIM_IT, FunctionalState Cmd)
  874. {
  875. /* Check the parameters */
  876. assert_param(IsTimAllModule(TIMx));
  877. assert_param(IsTimInt(TIM_IT));
  878. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  879. if (Cmd != DISABLE)
  880. {
  881. /* Enable the Interrupt sources */
  882. TIMx->DINTEN |= TIM_IT;
  883. }
  884. else
  885. {
  886. /* Disable the Interrupt sources */
  887. TIMx->DINTEN &= (uint16_t)~TIM_IT;
  888. }
  889. }
  890. /**
  891. * @brief Configures the TIMx event to be generate by software.
  892. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  893. * @param TIM_EventSource specifies the event source.
  894. * This parameter can be one or more of the following values:
  895. * @arg TIM_EVT_SRC_UPDATE Timer update Event source
  896. * @arg TIM_EVT_SRC_CC1 Timer Capture Compare 1 Event source
  897. * @arg TIM_EVT_SRC_CC2 Timer Capture Compare 2 Event source
  898. * @arg TIM_EVT_SRC_CC3 Timer Capture Compare 3 Event source
  899. * @arg TIM_EVT_SRC_CC4 Timer Capture Compare 4 Event source
  900. * @arg TIM_EVT_SRC_COM Timer COM event source
  901. * @arg TIM_EVT_SRC_TRIG Timer Trigger Event source
  902. * @arg TIM_EVT_SRC_BREAK Timer Break event source
  903. * @note
  904. * - TIM6 and TIM7 can only generate an update event.
  905. * - TIM_EVT_SRC_COM and TIM_EVT_SRC_BREAK are used only with TIM1 and TIM8.
  906. */
  907. void TIM_GenerateEvent(TIM_Module* TIMx, uint16_t TIM_EventSource)
  908. {
  909. /* Check the parameters */
  910. assert_param(IsTimAllModule(TIMx));
  911. assert_param(IsTimEvtSrc(TIM_EventSource));
  912. /* Set the event sources */
  913. TIMx->EVTGEN = TIM_EventSource;
  914. }
  915. /**
  916. * @brief Configures the TIMx's DMA interface.
  917. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select
  918. * the TIM peripheral.
  919. * @param TIM_DMABase DMA Base address.
  920. * This parameter can be one of the following values:
  921. * @arg TIM_DMABase_CR, TIM_DMABASE_CTRL2, TIM_DMABASE_SMCTRL,
  922. * TIM_DMABASE_DMAINTEN, TIM1_DMABase_SR, TIM_DMABASE_EVTGEN,
  923. * TIM_DMABASE_CAPCMPMOD1, TIM_DMABASE_CAPCMPMOD2, TIM_DMABASE_CAPCMPEN,
  924. * TIM_DMABASE_CNT, TIM_DMABASE_PSC, TIM_DMABASE_AR,
  925. * TIM_DMABASE_REPCNT, TIM_DMABASE_CAPCMPDAT1, TIM_DMABASE_CAPCMPDAT2,
  926. * TIM_DMABASE_CAPCMPDAT3, TIM_DMABASE_CAPCMPDAT4, TIM_DMABASE_BKDT,
  927. * TIM_DMABASE_DMACTRL.
  928. * @param TIM_DMABurstLength DMA Burst length.
  929. * This parameter can be one value between:
  930. * TIM_DMABURST_LENGTH_1TRANSFER and TIM_DMABURST_LENGTH_18TRANSFERS.
  931. */
  932. void TIM_ConfigDma(TIM_Module* TIMx, uint16_t TIM_DMABase, uint16_t TIM_DMABurstLength)
  933. {
  934. /* Check the parameters */
  935. assert_param(IsTimList4Module(TIMx));
  936. assert_param(IsTimDmaBase(TIM_DMABase));
  937. assert_param(IsTimDmaLength(TIM_DMABurstLength));
  938. /* Set the DMA Base and the DMA Burst Length */
  939. TIMx->DCTRL = TIM_DMABase | TIM_DMABurstLength;
  940. }
  941. /**
  942. * @brief Enables or disables the TIMx's DMA Requests.
  943. * @param TIMx where x can be 1, 2, 3, 4, 5, 6, 7, 8
  944. * to select the TIM peripheral.
  945. * @param TIM_DMASource specifies the DMA Request sources.
  946. * This parameter can be any combination of the following values:
  947. * @arg TIM_DMA_UPDATE TIM update Interrupt source
  948. * @arg TIM_DMA_CC1 TIM Capture Compare 1 DMA source
  949. * @arg TIM_DMA_CC2 TIM Capture Compare 2 DMA source
  950. * @arg TIM_DMA_CC3 TIM Capture Compare 3 DMA source
  951. * @arg TIM_DMA_CC4 TIM Capture Compare 4 DMA source
  952. * @arg TIM_DMA_COM TIM Commutation DMA source
  953. * @arg TIM_DMA_TRIG TIM Trigger DMA source
  954. * @param Cmd new state of the DMA Request sources.
  955. * This parameter can be: ENABLE or DISABLE.
  956. */
  957. void TIM_EnableDma(TIM_Module* TIMx, uint16_t TIM_DMASource, FunctionalState Cmd)
  958. {
  959. /* Check the parameters */
  960. assert_param(IsTimList9Module(TIMx));
  961. assert_param(IsTimDmaSrc(TIM_DMASource));
  962. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  963. if (Cmd != DISABLE)
  964. {
  965. /* Enable the DMA sources */
  966. TIMx->DINTEN |= TIM_DMASource;
  967. }
  968. else
  969. {
  970. /* Disable the DMA sources */
  971. TIMx->DINTEN &= (uint16_t)~TIM_DMASource;
  972. }
  973. }
  974. /**
  975. * @brief Configures the TIMx internal Clock
  976. * @param TIMx where x can be 1, 2, 3, 4, 5, 8
  977. * to select the TIM peripheral.
  978. */
  979. void TIM_ConfigInternalClk(TIM_Module* TIMx)
  980. {
  981. /* Check the parameters */
  982. assert_param(IsTimList6Module(TIMx));
  983. /* Disable slave mode to clock the prescaler directly with the internal clock */
  984. TIMx->SMCTRL &= (uint16_t)(~((uint16_t)TIM_SMCTRL_SMSEL));
  985. }
  986. /**
  987. * @brief Configures the TIMx Internal Trigger as External Clock
  988. * @param TIMx where x can be 1, 2, 3, 4, 5 to select the TIM peripheral.
  989. * @param TIM_InputTriggerSource Trigger source.
  990. * This parameter can be one of the following values:
  991. * @arg TIM_TRIG_SEL_IN_TR0 Internal Trigger 0
  992. * @arg TIM_TRIG_SEL_IN_TR1 Internal Trigger 1
  993. * @arg TIM_TRIG_SEL_IN_TR2 Internal Trigger 2
  994. * @arg TIM_TRIG_SEL_IN_TR3 Internal Trigger 3
  995. */
  996. void TIM_ConfigInternalTrigToExt(TIM_Module* TIMx, uint16_t TIM_InputTriggerSource)
  997. {
  998. /* Check the parameters */
  999. assert_param(IsTimList6Module(TIMx));
  1000. assert_param(IsTimInterTrigSel(TIM_InputTriggerSource));
  1001. /* Select the Internal Trigger */
  1002. TIM_SelectInputTrig(TIMx, TIM_InputTriggerSource);
  1003. /* Select the External clock mode1 */
  1004. TIMx->SMCTRL |= TIM_SLAVE_MODE_EXT1;
  1005. }
  1006. /**
  1007. * @brief Configures the TIMx Trigger as External Clock
  1008. * @param TIMx where x can be 1, 2, 3, 4, 5 to select the TIM peripheral.
  1009. * @param TIM_TIxExternalCLKSource Trigger source.
  1010. * This parameter can be one of the following values:
  1011. * @arg TIM_EXT_CLK_SRC_TI1ED TI1 Edge Detector
  1012. * @arg TIM_EXT_CLK_SRC_TI1 Filtered Timer Input 1
  1013. * @arg TIM_EXT_CLK_SRC_TI2 Filtered Timer Input 2
  1014. * @param IcPolarity specifies the TIx Polarity.
  1015. * This parameter can be one of the following values:
  1016. * @arg TIM_IC_POLARITY_RISING
  1017. * @arg TIM_IC_POLARITY_FALLING
  1018. * @param ICFilter specifies the filter value.
  1019. * This parameter must be a value between 0x0 and 0xF.
  1020. */
  1021. void TIM_ConfigExtTrigAsClk(TIM_Module* TIMx, uint16_t TIM_TIxExternalCLKSource, uint16_t IcPolarity, uint16_t ICFilter)
  1022. {
  1023. /* Check the parameters */
  1024. assert_param(IsTimList6Module(TIMx));
  1025. assert_param(IsTimExtClkSrc(TIM_TIxExternalCLKSource));
  1026. assert_param(IsTimIcPalaritySingleEdge(IcPolarity));
  1027. assert_param(IsTimInCapFilter(ICFilter));
  1028. /* Configure the Timer Input Clock Source */
  1029. if (TIM_TIxExternalCLKSource == TIM_EXT_CLK_SRC_TI2)
  1030. {
  1031. ConfigTI2(TIMx, IcPolarity, TIM_IC_SELECTION_DIRECTTI, ICFilter);
  1032. }
  1033. else
  1034. {
  1035. ConfigTI1(TIMx, IcPolarity, TIM_IC_SELECTION_DIRECTTI, ICFilter);
  1036. }
  1037. /* Select the Trigger source */
  1038. TIM_SelectInputTrig(TIMx, TIM_TIxExternalCLKSource);
  1039. /* Select the External clock mode1 */
  1040. TIMx->SMCTRL |= TIM_SLAVE_MODE_EXT1;
  1041. }
  1042. /**
  1043. * @brief Configures the External clock Mode1
  1044. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1045. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  1046. * This parameter can be one of the following values:
  1047. * @arg TIM_EXT_TRG_PSC_OFF ETRP Prescaler OFF.
  1048. * @arg TIM_EXT_TRG_PSC_DIV2 ETRP frequency divided by 2.
  1049. * @arg TIM_EXT_TRG_PSC_DIV4 ETRP frequency divided by 4.
  1050. * @arg TIM_EXT_TRG_PSC_DIV8 ETRP frequency divided by 8.
  1051. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  1052. * This parameter can be one of the following values:
  1053. * @arg TIM_EXT_TRIG_POLARITY_INVERTED active low or falling edge active.
  1054. * @arg TIM_EXT_TRIG_POLARITY_NONINVERTED active high or rising edge active.
  1055. * @param ExtTRGFilter External Trigger Filter.
  1056. * This parameter must be a value between 0x00 and 0x0F
  1057. */
  1058. void TIM_ConfigExtClkMode1(TIM_Module* TIMx,
  1059. uint16_t TIM_ExtTRGPrescaler,
  1060. uint16_t TIM_ExtTRGPolarity,
  1061. uint16_t ExtTRGFilter)
  1062. {
  1063. uint16_t tmpsmcr = 0;
  1064. /* Check the parameters */
  1065. assert_param(IsTimList3Module(TIMx));
  1066. assert_param(IsTimExtPreDiv(TIM_ExtTRGPrescaler));
  1067. assert_param(IsTimExtTrigPolarity(TIM_ExtTRGPolarity));
  1068. assert_param(IsTimExtTrigFilter(ExtTRGFilter));
  1069. /* Configure the ETR Clock source */
  1070. TIM_ConfigExtTrig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  1071. /* Get the TIMx SMCTRL register value */
  1072. tmpsmcr = TIMx->SMCTRL;
  1073. /* Reset the SMS Bits */
  1074. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCTRL_SMSEL));
  1075. /* Select the External clock mode1 */
  1076. tmpsmcr |= TIM_SLAVE_MODE_EXT1;
  1077. /* Select the Trigger selection : ETRF */
  1078. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCTRL_TSEL));
  1079. tmpsmcr |= TIM_TRIG_SEL_ETRF;
  1080. /* Write to TIMx SMCTRL */
  1081. TIMx->SMCTRL = tmpsmcr;
  1082. }
  1083. /**
  1084. * @brief Configures the External clock Mode2
  1085. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1086. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  1087. * This parameter can be one of the following values:
  1088. * @arg TIM_EXT_TRG_PSC_OFF ETRP Prescaler OFF.
  1089. * @arg TIM_EXT_TRG_PSC_DIV2 ETRP frequency divided by 2.
  1090. * @arg TIM_EXT_TRG_PSC_DIV4 ETRP frequency divided by 4.
  1091. * @arg TIM_EXT_TRG_PSC_DIV8 ETRP frequency divided by 8.
  1092. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  1093. * This parameter can be one of the following values:
  1094. * @arg TIM_EXT_TRIG_POLARITY_INVERTED active low or falling edge active.
  1095. * @arg TIM_EXT_TRIG_POLARITY_NONINVERTED active high or rising edge active.
  1096. * @param ExtTRGFilter External Trigger Filter.
  1097. * This parameter must be a value between 0x00 and 0x0F
  1098. */
  1099. void TIM_ConfigExtClkMode2(TIM_Module* TIMx,
  1100. uint16_t TIM_ExtTRGPrescaler,
  1101. uint16_t TIM_ExtTRGPolarity,
  1102. uint16_t ExtTRGFilter)
  1103. {
  1104. /* Check the parameters */
  1105. assert_param(IsTimList3Module(TIMx));
  1106. assert_param(IsTimExtPreDiv(TIM_ExtTRGPrescaler));
  1107. assert_param(IsTimExtTrigPolarity(TIM_ExtTRGPolarity));
  1108. assert_param(IsTimExtTrigFilter(ExtTRGFilter));
  1109. /* Configure the ETR Clock source */
  1110. TIM_ConfigExtTrig(TIMx, TIM_ExtTRGPrescaler, TIM_ExtTRGPolarity, ExtTRGFilter);
  1111. /* Enable the External clock mode2 */
  1112. TIMx->SMCTRL |= TIM_SMCTRL_EXCEN;
  1113. }
  1114. /**
  1115. * @brief Configures the TIMx External Trigger (ETR).
  1116. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1117. * @param TIM_ExtTRGPrescaler The external Trigger Prescaler.
  1118. * This parameter can be one of the following values:
  1119. * @arg TIM_EXT_TRG_PSC_OFF ETRP Prescaler OFF.
  1120. * @arg TIM_EXT_TRG_PSC_DIV2 ETRP frequency divided by 2.
  1121. * @arg TIM_EXT_TRG_PSC_DIV4 ETRP frequency divided by 4.
  1122. * @arg TIM_EXT_TRG_PSC_DIV8 ETRP frequency divided by 8.
  1123. * @param TIM_ExtTRGPolarity The external Trigger Polarity.
  1124. * This parameter can be one of the following values:
  1125. * @arg TIM_EXT_TRIG_POLARITY_INVERTED active low or falling edge active.
  1126. * @arg TIM_EXT_TRIG_POLARITY_NONINVERTED active high or rising edge active.
  1127. * @param ExtTRGFilter External Trigger Filter.
  1128. * This parameter must be a value between 0x00 and 0x0F
  1129. */
  1130. void TIM_ConfigExtTrig(TIM_Module* TIMx,
  1131. uint16_t TIM_ExtTRGPrescaler,
  1132. uint16_t TIM_ExtTRGPolarity,
  1133. uint16_t ExtTRGFilter)
  1134. {
  1135. uint16_t tmpsmcr = 0;
  1136. /* Check the parameters */
  1137. assert_param(IsTimList3Module(TIMx));
  1138. assert_param(IsTimExtPreDiv(TIM_ExtTRGPrescaler));
  1139. assert_param(IsTimExtTrigPolarity(TIM_ExtTRGPolarity));
  1140. assert_param(IsTimExtTrigFilter(ExtTRGFilter));
  1141. tmpsmcr = TIMx->SMCTRL;
  1142. /* Reset the ETR Bits */
  1143. tmpsmcr &= SMCTRL_ETR_MASK;
  1144. /* Set the Prescaler, the Filter value and the Polarity */
  1145. tmpsmcr |=
  1146. (uint16_t)(TIM_ExtTRGPrescaler | (uint16_t)(TIM_ExtTRGPolarity | (uint16_t)(ExtTRGFilter << (uint16_t)8)));
  1147. /* Write to TIMx SMCTRL */
  1148. TIMx->SMCTRL = tmpsmcr;
  1149. }
  1150. /**
  1151. * @brief Configures the TIMx Prescaler.
  1152. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  1153. * @param Prescaler specifies the Prescaler Register value
  1154. * @param TIM_PSCReloadMode specifies the TIM Prescaler Reload mode
  1155. * This parameter can be one of the following values:
  1156. * @arg TIM_PSC_RELOAD_MODE_UPDATE The Prescaler is loaded at the update event.
  1157. * @arg TIM_PSC_RELOAD_MODE_IMMEDIATE The Prescaler is loaded immediately.
  1158. */
  1159. void TIM_ConfigPrescaler(TIM_Module* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode)
  1160. {
  1161. /* Check the parameters */
  1162. assert_param(IsTimAllModule(TIMx));
  1163. assert_param(IsTimPscReloadMode(TIM_PSCReloadMode));
  1164. /* Set the Prescaler value */
  1165. TIMx->PSC = Prescaler;
  1166. /* Set or reset the UG Bit */
  1167. TIMx->EVTGEN = TIM_PSCReloadMode;
  1168. }
  1169. /**
  1170. * @brief Specifies the TIMx Counter Mode to be used.
  1171. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1172. * @param CntMode specifies the Counter Mode to be used
  1173. * This parameter can be one of the following values:
  1174. * @arg TIM_CNT_MODE_UP TIM Up Counting Mode
  1175. * @arg TIM_CNT_MODE_DOWN TIM Down Counting Mode
  1176. * @arg TIM_CNT_MODE_CENTER_ALIGN1 TIM Center Aligned Mode1
  1177. * @arg TIM_CNT_MODE_CENTER_ALIGN2 TIM Center Aligned Mode2
  1178. * @arg TIM_CNT_MODE_CENTER_ALIGN3 TIM Center Aligned Mode3
  1179. */
  1180. void TIM_ConfigCntMode(TIM_Module* TIMx, uint16_t CntMode)
  1181. {
  1182. uint32_t tmpcr1 = 0;
  1183. /* Check the parameters */
  1184. assert_param(IsTimList3Module(TIMx));
  1185. assert_param(IsTimCntMode(CntMode));
  1186. tmpcr1 = TIMx->CTRL1;
  1187. /* Reset the CMS and DIR Bits */
  1188. tmpcr1 &= (uint32_t)(~((uint32_t)(TIM_CTRL1_DIR | TIM_CTRL1_CAMSEL)));
  1189. /* Set the Counter Mode */
  1190. tmpcr1 |= CntMode;
  1191. /* Write to TIMx CTRL1 register */
  1192. TIMx->CTRL1 = tmpcr1;
  1193. }
  1194. /**
  1195. * @brief Selects the Input Trigger source
  1196. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  1197. * @param TIM_InputTriggerSource The Input Trigger source.
  1198. * This parameter can be one of the following values:
  1199. * @arg TIM_TRIG_SEL_IN_TR0 Internal Trigger 0
  1200. * @arg TIM_TRIG_SEL_IN_TR1 Internal Trigger 1
  1201. * @arg TIM_TRIG_SEL_IN_TR2 Internal Trigger 2
  1202. * @arg TIM_TRIG_SEL_IN_TR3 Internal Trigger 3
  1203. * @arg TIM_TRIG_SEL_TI1F_ED TI1 Edge Detector
  1204. * @arg TIM_TRIG_SEL_TI1FP1 Filtered Timer Input 1
  1205. * @arg TIM_TRIG_SEL_TI2FP2 Filtered Timer Input 2
  1206. * @arg TIM_TRIG_SEL_ETRF External Trigger input
  1207. */
  1208. void TIM_SelectInputTrig(TIM_Module* TIMx, uint16_t TIM_InputTriggerSource)
  1209. {
  1210. uint16_t tmpsmcr = 0;
  1211. /* Check the parameters */
  1212. assert_param(IsTimList6Module(TIMx));
  1213. assert_param(IsTimTrigSel(TIM_InputTriggerSource));
  1214. /* Get the TIMx SMCTRL register value */
  1215. tmpsmcr = TIMx->SMCTRL;
  1216. /* Reset the TS Bits */
  1217. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCTRL_TSEL));
  1218. /* Set the Input Trigger source */
  1219. tmpsmcr |= TIM_InputTriggerSource;
  1220. /* Write to TIMx SMCTRL */
  1221. TIMx->SMCTRL = tmpsmcr;
  1222. }
  1223. /**
  1224. * @brief Configures the TIMx Encoder Interface.
  1225. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1226. * @param TIM_EncoderMode specifies the TIMx Encoder Mode.
  1227. * This parameter can be one of the following values:
  1228. * @arg TIM_ENCODE_MODE_TI1 Counter counts on TI1FP1 edge depending on TI2FP2 level.
  1229. * @arg TIM_ENCODE_MODE_TI2 Counter counts on TI2FP2 edge depending on TI1FP1 level.
  1230. * @arg TIM_ENCODE_MODE_TI12 Counter counts on both TI1FP1 and TI2FP2 edges depending
  1231. * on the level of the other input.
  1232. * @param TIM_IC1Polarity specifies the IC1 Polarity
  1233. * This parameter can be one of the following values:
  1234. * @arg TIM_IC_POLARITY_FALLING IC Falling edge.
  1235. * @arg TIM_IC_POLARITY_RISING IC Rising edge.
  1236. * @param TIM_IC2Polarity specifies the IC2 Polarity
  1237. * This parameter can be one of the following values:
  1238. * @arg TIM_IC_POLARITY_FALLING IC Falling edge.
  1239. * @arg TIM_IC_POLARITY_RISING IC Rising edge.
  1240. */
  1241. void TIM_ConfigEncoderInterface(TIM_Module* TIMx,
  1242. uint16_t TIM_EncoderMode,
  1243. uint16_t TIM_IC1Polarity,
  1244. uint16_t TIM_IC2Polarity)
  1245. {
  1246. uint16_t tmpsmcr = 0;
  1247. uint16_t tmpccmr1 = 0;
  1248. uint32_t tmpccer = 0;
  1249. /* Check the parameters */
  1250. assert_param(IsTimList5Module(TIMx));
  1251. assert_param(IsTimEncodeMode(TIM_EncoderMode));
  1252. assert_param(IsTimIcPalaritySingleEdge(TIM_IC1Polarity));
  1253. assert_param(IsTimIcPalaritySingleEdge(TIM_IC2Polarity));
  1254. /* Get the TIMx SMCTRL register value */
  1255. tmpsmcr = TIMx->SMCTRL;
  1256. /* Get the TIMx CCMOD1 register value */
  1257. tmpccmr1 = TIMx->CCMOD1;
  1258. /* Get the TIMx CCEN register value */
  1259. tmpccer = TIMx->CCEN;
  1260. /* Set the encoder Mode */
  1261. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCTRL_SMSEL));
  1262. tmpsmcr |= TIM_EncoderMode;
  1263. /* Select the Capture Compare 1 and the Capture Compare 2 as input */
  1264. tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CCMOD1_CC1SEL)) & (uint16_t)(~((uint16_t)TIM_CCMOD1_CC2SEL)));
  1265. tmpccmr1 |= TIM_CCMOD1_CC1SEL_0 | TIM_CCMOD1_CC2SEL_0;
  1266. /* Set the TI1 and the TI2 Polarities */
  1267. tmpccer &= (uint32_t)(((uint32_t) ~((uint32_t)TIM_CCEN_CC1P)) & ((uint32_t) ~((uint32_t)TIM_CCEN_CC2P)));
  1268. tmpccer |= (uint32_t)(TIM_IC1Polarity | (uint16_t)(TIM_IC2Polarity << (uint16_t)4));
  1269. /* Write to TIMx SMCTRL */
  1270. TIMx->SMCTRL = tmpsmcr;
  1271. /* Write to TIMx CCMOD1 */
  1272. TIMx->CCMOD1 = tmpccmr1;
  1273. /* Write to TIMx CCEN */
  1274. TIMx->CCEN = tmpccer;
  1275. }
  1276. /**
  1277. * @brief Forces the TIMx output 1 waveform to active or inactive level.
  1278. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  1279. * @param TIM_ForcedAction specifies the forced Action to be set to the output waveform.
  1280. * This parameter can be one of the following values:
  1281. * @arg TIM_FORCED_ACTION_ACTIVE Force active level on OC1REF
  1282. * @arg TIM_FORCED_ACTION_INACTIVE Force inactive level on OC1REF.
  1283. */
  1284. void TIM_ConfigForcedOc1(TIM_Module* TIMx, uint16_t TIM_ForcedAction)
  1285. {
  1286. uint16_t tmpccmr1 = 0;
  1287. /* Check the parameters */
  1288. assert_param(IsTimList8Module(TIMx));
  1289. assert_param(IsTimForceActive(TIM_ForcedAction));
  1290. tmpccmr1 = TIMx->CCMOD1;
  1291. /* Reset the OC1M Bits */
  1292. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC1M);
  1293. /* Configure The Forced output Mode */
  1294. tmpccmr1 |= TIM_ForcedAction;
  1295. /* Write to TIMx CCMOD1 register */
  1296. TIMx->CCMOD1 = tmpccmr1;
  1297. }
  1298. /**
  1299. * @brief Forces the TIMx output 2 waveform to active or inactive level.
  1300. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  1301. * @param TIM_ForcedAction specifies the forced Action to be set to the output waveform.
  1302. * This parameter can be one of the following values:
  1303. * @arg TIM_FORCED_ACTION_ACTIVE Force active level on OC2REF
  1304. * @arg TIM_FORCED_ACTION_INACTIVE Force inactive level on OC2REF.
  1305. */
  1306. void TIM_ConfigForcedOc2(TIM_Module* TIMx, uint16_t TIM_ForcedAction)
  1307. {
  1308. uint16_t tmpccmr1 = 0;
  1309. /* Check the parameters */
  1310. assert_param(IsTimList6Module(TIMx));
  1311. assert_param(IsTimForceActive(TIM_ForcedAction));
  1312. tmpccmr1 = TIMx->CCMOD1;
  1313. /* Reset the OC2M Bits */
  1314. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC2M);
  1315. /* Configure The Forced output Mode */
  1316. tmpccmr1 |= (uint16_t)(TIM_ForcedAction << 8);
  1317. /* Write to TIMx CCMOD1 register */
  1318. TIMx->CCMOD1 = tmpccmr1;
  1319. }
  1320. /**
  1321. * @brief Forces the TIMx output 3 waveform to active or inactive level.
  1322. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1323. * @param TIM_ForcedAction specifies the forced Action to be set to the output waveform.
  1324. * This parameter can be one of the following values:
  1325. * @arg TIM_FORCED_ACTION_ACTIVE Force active level on OC3REF
  1326. * @arg TIM_FORCED_ACTION_INACTIVE Force inactive level on OC3REF.
  1327. */
  1328. void TIM_ConfigForcedOc3(TIM_Module* TIMx, uint16_t TIM_ForcedAction)
  1329. {
  1330. uint16_t tmpccmr2 = 0;
  1331. /* Check the parameters */
  1332. assert_param(IsTimList3Module(TIMx));
  1333. assert_param(IsTimForceActive(TIM_ForcedAction));
  1334. tmpccmr2 = TIMx->CCMOD2;
  1335. /* Reset the OC1M Bits */
  1336. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC3MD);
  1337. /* Configure The Forced output Mode */
  1338. tmpccmr2 |= TIM_ForcedAction;
  1339. /* Write to TIMx CCMOD2 register */
  1340. TIMx->CCMOD2 = tmpccmr2;
  1341. }
  1342. /**
  1343. * @brief Forces the TIMx output 4 waveform to active or inactive level.
  1344. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1345. * @param TIM_ForcedAction specifies the forced Action to be set to the output waveform.
  1346. * This parameter can be one of the following values:
  1347. * @arg TIM_FORCED_ACTION_ACTIVE Force active level on OC4REF
  1348. * @arg TIM_FORCED_ACTION_INACTIVE Force inactive level on OC4REF.
  1349. */
  1350. void TIM_ConfigForcedOc4(TIM_Module* TIMx, uint16_t TIM_ForcedAction)
  1351. {
  1352. uint16_t tmpccmr2 = 0;
  1353. /* Check the parameters */
  1354. assert_param(IsTimList3Module(TIMx));
  1355. assert_param(IsTimForceActive(TIM_ForcedAction));
  1356. tmpccmr2 = TIMx->CCMOD2;
  1357. /* Reset the OC2M Bits */
  1358. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC4MD);
  1359. /* Configure The Forced output Mode */
  1360. tmpccmr2 |= (uint16_t)(TIM_ForcedAction << 8);
  1361. /* Write to TIMx CCMOD2 register */
  1362. TIMx->CCMOD2 = tmpccmr2;
  1363. }
  1364. /**
  1365. * @brief Forces the TIMx output 5 waveform to active or inactive level.
  1366. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1367. * @param TIM_ForcedAction specifies the forced Action to be set to the output waveform.
  1368. * This parameter can be one of the following values:
  1369. * @arg TIM_FORCED_ACTION_ACTIVE Force active level on OC5REF
  1370. * @arg TIM_FORCED_ACTION_INACTIVE Force inactive level on OC5REF.
  1371. */
  1372. void TIM_ConfigForcedOc5(TIM_Module* TIMx, uint16_t TIM_ForcedAction)
  1373. {
  1374. uint16_t tmpccmr3 = 0;
  1375. /* Check the parameters */
  1376. assert_param(IsTimList1Module(TIMx));
  1377. assert_param(IsTimForceActive(TIM_ForcedAction));
  1378. tmpccmr3 = TIMx->CCMOD3;
  1379. /* Reset the OC2M Bits */
  1380. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC5MD);
  1381. /* Configure The Forced output Mode */
  1382. tmpccmr3 |= (uint16_t)(TIM_ForcedAction);
  1383. /* Write to TIMx CCMOD2 register */
  1384. TIMx->CCMOD3 = tmpccmr3;
  1385. }
  1386. /**
  1387. * @brief Forces the TIMx output 6 waveform to active or inactive level.
  1388. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1389. * @param TIM_ForcedAction specifies the forced Action to be set to the output waveform.
  1390. * This parameter can be one of the following values:
  1391. * @arg TIM_FORCED_ACTION_ACTIVE Force active level on OC6REF
  1392. * @arg TIM_FORCED_ACTION_INACTIVE Force inactive level on OC6REF.
  1393. */
  1394. void TIM_ConfigForcedOc6(TIM_Module* TIMx, uint16_t TIM_ForcedAction)
  1395. {
  1396. uint16_t tmpccmr3 = 0;
  1397. /* Check the parameters */
  1398. assert_param(IsTimList1Module(TIMx));
  1399. assert_param(IsTimForceActive(TIM_ForcedAction));
  1400. tmpccmr3 = TIMx->CCMOD3;
  1401. /* Reset the OC2M Bits */
  1402. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC6MD);
  1403. /* Configure The Forced output Mode */
  1404. tmpccmr3 |= (uint16_t)(TIM_ForcedAction << 8);
  1405. /* Write to TIMx CCMOD2 register */
  1406. TIMx->CCMOD3 = tmpccmr3;
  1407. }
  1408. /**
  1409. * @brief Enables or disables TIMx peripheral Preload register on AR.
  1410. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  1411. * @param Cmd new state of the TIMx peripheral Preload register
  1412. * This parameter can be: ENABLE or DISABLE.
  1413. */
  1414. void TIM_ConfigArPreload(TIM_Module* TIMx, FunctionalState Cmd)
  1415. {
  1416. /* Check the parameters */
  1417. assert_param(IsTimAllModule(TIMx));
  1418. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1419. if (Cmd != DISABLE)
  1420. {
  1421. /* Set the AR Preload Bit */
  1422. TIMx->CTRL1 |= TIM_CTRL1_ARPEN;
  1423. }
  1424. else
  1425. {
  1426. /* Reset the AR Preload Bit */
  1427. TIMx->CTRL1 &= (uint32_t) ~((uint32_t)TIM_CTRL1_ARPEN);
  1428. }
  1429. }
  1430. /**
  1431. * @brief Selects the TIM peripheral Commutation event.
  1432. * @param TIMx where x can be 1, 8 to select the TIMx peripheral
  1433. * @param Cmd new state of the Commutation event.
  1434. * This parameter can be: ENABLE or DISABLE.
  1435. */
  1436. void TIM_SelectComEvt(TIM_Module* TIMx, FunctionalState Cmd)
  1437. {
  1438. /* Check the parameters */
  1439. assert_param(IsTimList2Module(TIMx));
  1440. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1441. if (Cmd != DISABLE)
  1442. {
  1443. /* Set the COM Bit */
  1444. TIMx->CTRL2 |= TIM_CTRL2_CCUSEL;
  1445. }
  1446. else
  1447. {
  1448. /* Reset the COM Bit */
  1449. TIMx->CTRL2 &= (uint32_t) ~((uint32_t)TIM_CTRL2_CCUSEL);
  1450. }
  1451. }
  1452. /**
  1453. * @brief Selects the TIMx peripheral Capture Compare DMA source.
  1454. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select
  1455. * the TIM peripheral.
  1456. * @param Cmd new state of the Capture Compare DMA source
  1457. * This parameter can be: ENABLE or DISABLE.
  1458. */
  1459. void TIM_SelectCapCmpDmaSrc(TIM_Module* TIMx, FunctionalState Cmd)
  1460. {
  1461. /* Check the parameters */
  1462. assert_param(IsTimList4Module(TIMx));
  1463. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1464. if (Cmd != DISABLE)
  1465. {
  1466. /* Set the CCDS Bit */
  1467. TIMx->CTRL2 |= TIM_CTRL2_CCDSEL;
  1468. }
  1469. else
  1470. {
  1471. /* Reset the CCDS Bit */
  1472. TIMx->CTRL2 &= (uint32_t) ~((uint32_t)TIM_CTRL2_CCDSEL);
  1473. }
  1474. }
  1475. /**
  1476. * @brief Sets or Resets the TIM peripheral Capture Compare Preload Control bit.
  1477. * @param TIMx where x can be 1, 2, 3, 4, 5, 8
  1478. * to select the TIMx peripheral
  1479. * @param Cmd new state of the Capture Compare Preload Control bit
  1480. * This parameter can be: ENABLE or DISABLE.
  1481. */
  1482. void TIM_EnableCapCmpPreloadControl(TIM_Module* TIMx, FunctionalState Cmd)
  1483. {
  1484. /* Check the parameters */
  1485. assert_param(IsTimList5Module(TIMx));
  1486. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  1487. if (Cmd != DISABLE)
  1488. {
  1489. /* Set the CCPC Bit */
  1490. TIMx->CTRL2 |= TIM_CTRL2_CCPCTL;
  1491. }
  1492. else
  1493. {
  1494. /* Reset the CCPC Bit */
  1495. TIMx->CTRL2 &= (uint32_t) ~((uint32_t)TIM_CTRL2_CCPCTL);
  1496. }
  1497. }
  1498. /**
  1499. * @brief Enables or disables the TIMx peripheral Preload register on CCDAT1.
  1500. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  1501. * @param TIM_OCPreload new state of the TIMx peripheral Preload register
  1502. * This parameter can be one of the following values:
  1503. * @arg TIM_OC_PRE_LOAD_ENABLE
  1504. * @arg TIM_OC_PRE_LOAD_DISABLE
  1505. */
  1506. void TIM_ConfigOc1Preload(TIM_Module* TIMx, uint16_t TIM_OCPreload)
  1507. {
  1508. uint16_t tmpccmr1 = 0;
  1509. /* Check the parameters */
  1510. assert_param(IsTimList8Module(TIMx));
  1511. assert_param(IsTimOcPreLoadState(TIM_OCPreload));
  1512. tmpccmr1 = TIMx->CCMOD1;
  1513. /* Reset the OC1PE Bit */
  1514. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC1PEN);
  1515. /* Enable or Disable the Output Compare Preload feature */
  1516. tmpccmr1 |= TIM_OCPreload;
  1517. /* Write to TIMx CCMOD1 register */
  1518. TIMx->CCMOD1 = tmpccmr1;
  1519. }
  1520. /**
  1521. * @brief Enables or disables the TIMx peripheral Preload register on CCDAT2.
  1522. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select
  1523. * the TIM peripheral.
  1524. * @param TIM_OCPreload new state of the TIMx peripheral Preload register
  1525. * This parameter can be one of the following values:
  1526. * @arg TIM_OC_PRE_LOAD_ENABLE
  1527. * @arg TIM_OC_PRE_LOAD_DISABLE
  1528. */
  1529. void TIM_ConfigOc2Preload(TIM_Module* TIMx, uint16_t TIM_OCPreload)
  1530. {
  1531. uint16_t tmpccmr1 = 0;
  1532. /* Check the parameters */
  1533. assert_param(IsTimList6Module(TIMx));
  1534. assert_param(IsTimOcPreLoadState(TIM_OCPreload));
  1535. tmpccmr1 = TIMx->CCMOD1;
  1536. /* Reset the OC2PE Bit */
  1537. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC2PEN);
  1538. /* Enable or Disable the Output Compare Preload feature */
  1539. tmpccmr1 |= (uint16_t)(TIM_OCPreload << 8);
  1540. /* Write to TIMx CCMOD1 register */
  1541. TIMx->CCMOD1 = tmpccmr1;
  1542. }
  1543. /**
  1544. * @brief Enables or disables the TIMx peripheral Preload register on CCDAT3.
  1545. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1546. * @param TIM_OCPreload new state of the TIMx peripheral Preload register
  1547. * This parameter can be one of the following values:
  1548. * @arg TIM_OC_PRE_LOAD_ENABLE
  1549. * @arg TIM_OC_PRE_LOAD_DISABLE
  1550. */
  1551. void TIM_ConfigOc3Preload(TIM_Module* TIMx, uint16_t TIM_OCPreload)
  1552. {
  1553. uint16_t tmpccmr2 = 0;
  1554. /* Check the parameters */
  1555. assert_param(IsTimList3Module(TIMx));
  1556. assert_param(IsTimOcPreLoadState(TIM_OCPreload));
  1557. tmpccmr2 = TIMx->CCMOD2;
  1558. /* Reset the OC3PE Bit */
  1559. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC3PEN);
  1560. /* Enable or Disable the Output Compare Preload feature */
  1561. tmpccmr2 |= TIM_OCPreload;
  1562. /* Write to TIMx CCMOD2 register */
  1563. TIMx->CCMOD2 = tmpccmr2;
  1564. }
  1565. /**
  1566. * @brief Enables or disables the TIMx peripheral Preload register on CCDAT4.
  1567. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1568. * @param TIM_OCPreload new state of the TIMx peripheral Preload register
  1569. * This parameter can be one of the following values:
  1570. * @arg TIM_OC_PRE_LOAD_ENABLE
  1571. * @arg TIM_OC_PRE_LOAD_DISABLE
  1572. */
  1573. void TIM_ConfigOc4Preload(TIM_Module* TIMx, uint16_t TIM_OCPreload)
  1574. {
  1575. uint16_t tmpccmr2 = 0;
  1576. /* Check the parameters */
  1577. assert_param(IsTimList3Module(TIMx));
  1578. assert_param(IsTimOcPreLoadState(TIM_OCPreload));
  1579. tmpccmr2 = TIMx->CCMOD2;
  1580. /* Reset the OC4PE Bit */
  1581. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC4PEN);
  1582. /* Enable or Disable the Output Compare Preload feature */
  1583. tmpccmr2 |= (uint16_t)(TIM_OCPreload << 8);
  1584. /* Write to TIMx CCMOD2 register */
  1585. TIMx->CCMOD2 = tmpccmr2;
  1586. }
  1587. /**
  1588. * @brief Enables or disables the TIMx peripheral Preload register on CCDAT5.
  1589. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1590. * @param TIM_OCPreload new state of the TIMx peripheral Preload register
  1591. * This parameter can be one of the following values:
  1592. * @arg TIM_OC_PRE_LOAD_ENABLE
  1593. * @arg TIM_OC_PRE_LOAD_DISABLE
  1594. */
  1595. void TIM_ConfigOc5Preload(TIM_Module* TIMx, uint16_t TIM_OCPreload)
  1596. {
  1597. uint16_t tmpccmr3 = 0;
  1598. /* Check the parameters */
  1599. assert_param(IsTimList1Module(TIMx));
  1600. assert_param(IsTimOcPreLoadState(TIM_OCPreload));
  1601. tmpccmr3 = TIMx->CCMOD3;
  1602. /* Reset the OC5PE Bit */
  1603. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC5PEN);
  1604. /* Enable or Disable the Output Compare Preload feature */
  1605. tmpccmr3 |= (uint16_t)(TIM_OCPreload);
  1606. /* Write to TIMx CCMOD3 register */
  1607. TIMx->CCMOD3 = tmpccmr3;
  1608. }
  1609. /**
  1610. * @brief Enables or disables the TIMx peripheral Preload register on CCDAT6.
  1611. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1612. * @param TIM_OCPreload new state of the TIMx peripheral Preload register
  1613. * This parameter can be one of the following values:
  1614. * @arg TIM_OC_PRE_LOAD_ENABLE
  1615. * @arg TIM_OC_PRE_LOAD_DISABLE
  1616. */
  1617. void TIM_ConfigOc6Preload(TIM_Module* TIMx, uint16_t TIM_OCPreload)
  1618. {
  1619. uint16_t tmpccmr3 = 0;
  1620. /* Check the parameters */
  1621. assert_param(IsTimList1Module(TIMx));
  1622. assert_param(IsTimOcPreLoadState(TIM_OCPreload));
  1623. tmpccmr3 = TIMx->CCMOD3;
  1624. /* Reset the OC6PE Bit */
  1625. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC6PEN);
  1626. /* Enable or Disable the Output Compare Preload feature */
  1627. tmpccmr3 |= (uint16_t)(TIM_OCPreload << 8);
  1628. /* Write to TIMx CCMOD3 register */
  1629. TIMx->CCMOD3 = tmpccmr3;
  1630. }
  1631. /**
  1632. * @brief Configures the TIMx Output Compare 1 Fast feature.
  1633. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  1634. * @param TIM_OCFast new state of the Output Compare Fast Enable Bit.
  1635. * This parameter can be one of the following values:
  1636. * @arg TIM_OC_FAST_ENABLE TIM output compare fast enable
  1637. * @arg TIM_OC_FAST_DISABLE TIM output compare fast disable
  1638. */
  1639. void TIM_ConfigOc1Fast(TIM_Module* TIMx, uint16_t TIM_OCFast)
  1640. {
  1641. uint16_t tmpccmr1 = 0;
  1642. /* Check the parameters */
  1643. assert_param(IsTimList8Module(TIMx));
  1644. assert_param(IsTimOcFastState(TIM_OCFast));
  1645. /* Get the TIMx CCMOD1 register value */
  1646. tmpccmr1 = TIMx->CCMOD1;
  1647. /* Reset the OC1FE Bit */
  1648. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC1FEN);
  1649. /* Enable or Disable the Output Compare Fast Bit */
  1650. tmpccmr1 |= TIM_OCFast;
  1651. /* Write to TIMx CCMOD1 */
  1652. TIMx->CCMOD1 = tmpccmr1;
  1653. }
  1654. /**
  1655. * @brief Configures the TIMx Output Compare 2 Fast feature.
  1656. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select
  1657. * the TIM peripheral.
  1658. * @param TIM_OCFast new state of the Output Compare Fast Enable Bit.
  1659. * This parameter can be one of the following values:
  1660. * @arg TIM_OC_FAST_ENABLE TIM output compare fast enable
  1661. * @arg TIM_OC_FAST_DISABLE TIM output compare fast disable
  1662. */
  1663. void TIM_ConfigOc2Fast(TIM_Module* TIMx, uint16_t TIM_OCFast)
  1664. {
  1665. uint16_t tmpccmr1 = 0;
  1666. /* Check the parameters */
  1667. assert_param(IsTimList6Module(TIMx));
  1668. assert_param(IsTimOcFastState(TIM_OCFast));
  1669. /* Get the TIMx CCMOD1 register value */
  1670. tmpccmr1 = TIMx->CCMOD1;
  1671. /* Reset the OC2FE Bit */
  1672. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC2FEN);
  1673. /* Enable or Disable the Output Compare Fast Bit */
  1674. tmpccmr1 |= (uint16_t)(TIM_OCFast << 8);
  1675. /* Write to TIMx CCMOD1 */
  1676. TIMx->CCMOD1 = tmpccmr1;
  1677. }
  1678. /**
  1679. * @brief Configures the TIMx Output Compare 3 Fast feature.
  1680. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1681. * @param TIM_OCFast new state of the Output Compare Fast Enable Bit.
  1682. * This parameter can be one of the following values:
  1683. * @arg TIM_OC_FAST_ENABLE TIM output compare fast enable
  1684. * @arg TIM_OC_FAST_DISABLE TIM output compare fast disable
  1685. */
  1686. void TIM_ConfigOc3Fast(TIM_Module* TIMx, uint16_t TIM_OCFast)
  1687. {
  1688. uint16_t tmpccmr2 = 0;
  1689. /* Check the parameters */
  1690. assert_param(IsTimList3Module(TIMx));
  1691. assert_param(IsTimOcFastState(TIM_OCFast));
  1692. /* Get the TIMx CCMOD2 register value */
  1693. tmpccmr2 = TIMx->CCMOD2;
  1694. /* Reset the OC3FE Bit */
  1695. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC3FEN);
  1696. /* Enable or Disable the Output Compare Fast Bit */
  1697. tmpccmr2 |= TIM_OCFast;
  1698. /* Write to TIMx CCMOD2 */
  1699. TIMx->CCMOD2 = tmpccmr2;
  1700. }
  1701. /**
  1702. * @brief Configures the TIMx Output Compare 4 Fast feature.
  1703. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1704. * @param TIM_OCFast new state of the Output Compare Fast Enable Bit.
  1705. * This parameter can be one of the following values:
  1706. * @arg TIM_OC_FAST_ENABLE TIM output compare fast enable
  1707. * @arg TIM_OC_FAST_DISABLE TIM output compare fast disable
  1708. */
  1709. void TIM_ConfigOc4Fast(TIM_Module* TIMx, uint16_t TIM_OCFast)
  1710. {
  1711. uint16_t tmpccmr2 = 0;
  1712. /* Check the parameters */
  1713. assert_param(IsTimList3Module(TIMx));
  1714. assert_param(IsTimOcFastState(TIM_OCFast));
  1715. /* Get the TIMx CCMOD2 register value */
  1716. tmpccmr2 = TIMx->CCMOD2;
  1717. /* Reset the OC4FE Bit */
  1718. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC4FEN);
  1719. /* Enable or Disable the Output Compare Fast Bit */
  1720. tmpccmr2 |= (uint16_t)(TIM_OCFast << 8);
  1721. /* Write to TIMx CCMOD2 */
  1722. TIMx->CCMOD2 = tmpccmr2;
  1723. }
  1724. /**
  1725. * @brief Configures the TIMx Output Compare 5 Fast feature.
  1726. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1727. * @param TIM_OCFast new state of the Output Compare Fast Enable Bit.
  1728. * This parameter can be one of the following values:
  1729. * @arg TIM_OC_FAST_ENABLE TIM output compare fast enable
  1730. * @arg TIM_OC_FAST_DISABLE TIM output compare fast disable
  1731. */
  1732. void TIM_ConfigOc5Fast(TIM_Module* TIMx, uint16_t TIM_OCFast)
  1733. {
  1734. uint16_t tmpccmr3 = 0;
  1735. /* Check the parameters */
  1736. assert_param(IsTimList1Module(TIMx));
  1737. assert_param(IsTimOcFastState(TIM_OCFast));
  1738. /* Get the TIMx CCMOD2 register value */
  1739. tmpccmr3 = TIMx->CCMOD3;
  1740. /* Reset the OC4FE Bit */
  1741. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC5FEN);
  1742. /* Enable or Disable the Output Compare Fast Bit */
  1743. tmpccmr3 |= (uint16_t)(TIM_OCFast);
  1744. /* Write to TIMx CCMOD3 */
  1745. TIMx->CCMOD3 = tmpccmr3;
  1746. }
  1747. /**
  1748. * @brief Configures the TIMx Output Compare 6 Fast feature.
  1749. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1750. * @param TIM_OCFast new state of the Output Compare Fast Enable Bit.
  1751. * This parameter can be one of the following values:
  1752. * @arg TIM_OC_FAST_ENABLE TIM output compare fast enable
  1753. * @arg TIM_OC_FAST_DISABLE TIM output compare fast disable
  1754. */
  1755. void TIM_ConfigOc6Fast(TIM_Module* TIMx, uint16_t TIM_OCFast)
  1756. {
  1757. uint16_t tmpccmr3 = 0;
  1758. /* Check the parameters */
  1759. assert_param(IsTimList1Module(TIMx));
  1760. assert_param(IsTimOcFastState(TIM_OCFast));
  1761. /* Get the TIMx CCMOD2 register value */
  1762. tmpccmr3 = TIMx->CCMOD3;
  1763. /* Reset the OC4FE Bit */
  1764. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC6FEN);
  1765. /* Enable or Disable the Output Compare Fast Bit */
  1766. tmpccmr3 |= (uint16_t)(TIM_OCFast << 8);
  1767. /* Write to TIMx CCMOD3 */
  1768. TIMx->CCMOD3 = tmpccmr3;
  1769. }
  1770. /**
  1771. * @brief Clears or safeguards the OCREF1 signal on an external event
  1772. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1773. * @param TIM_OCClear new state of the Output Compare Clear Enable Bit.
  1774. * This parameter can be one of the following values:
  1775. * @arg TIM_OC_CLR_ENABLE TIM Output clear enable
  1776. * @arg TIM_OC_CLR_DISABLE TIM Output clear disable
  1777. */
  1778. void TIM_ClrOc1Ref(TIM_Module* TIMx, uint16_t TIM_OCClear)
  1779. {
  1780. uint16_t tmpccmr1 = 0;
  1781. /* Check the parameters */
  1782. assert_param(IsTimList3Module(TIMx));
  1783. assert_param(IsTimOcClrState(TIM_OCClear));
  1784. tmpccmr1 = TIMx->CCMOD1;
  1785. /* Reset the OC1CE Bit */
  1786. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC1CEN);
  1787. /* Enable or Disable the Output Compare Clear Bit */
  1788. tmpccmr1 |= TIM_OCClear;
  1789. /* Write to TIMx CCMOD1 register */
  1790. TIMx->CCMOD1 = tmpccmr1;
  1791. }
  1792. /**
  1793. * @brief Clears or safeguards the OCREF2 signal on an external event
  1794. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1795. * @param TIM_OCClear new state of the Output Compare Clear Enable Bit.
  1796. * This parameter can be one of the following values:
  1797. * @arg TIM_OC_CLR_ENABLE TIM Output clear enable
  1798. * @arg TIM_OC_CLR_DISABLE TIM Output clear disable
  1799. */
  1800. void TIM_ClrOc2Ref(TIM_Module* TIMx, uint16_t TIM_OCClear)
  1801. {
  1802. uint16_t tmpccmr1 = 0;
  1803. /* Check the parameters */
  1804. assert_param(IsTimList3Module(TIMx));
  1805. assert_param(IsTimOcClrState(TIM_OCClear));
  1806. tmpccmr1 = TIMx->CCMOD1;
  1807. /* Reset the OC2CE Bit */
  1808. tmpccmr1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_OC2CEN);
  1809. /* Enable or Disable the Output Compare Clear Bit */
  1810. tmpccmr1 |= (uint16_t)(TIM_OCClear << 8);
  1811. /* Write to TIMx CCMOD1 register */
  1812. TIMx->CCMOD1 = tmpccmr1;
  1813. }
  1814. /**
  1815. * @brief Clears or safeguards the OCREF3 signal on an external event
  1816. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1817. * @param TIM_OCClear new state of the Output Compare Clear Enable Bit.
  1818. * This parameter can be one of the following values:
  1819. * @arg TIM_OC_CLR_ENABLE TIM Output clear enable
  1820. * @arg TIM_OC_CLR_DISABLE TIM Output clear disable
  1821. */
  1822. void TIM_ClrOc3Ref(TIM_Module* TIMx, uint16_t TIM_OCClear)
  1823. {
  1824. uint16_t tmpccmr2 = 0;
  1825. /* Check the parameters */
  1826. assert_param(IsTimList3Module(TIMx));
  1827. assert_param(IsTimOcClrState(TIM_OCClear));
  1828. tmpccmr2 = TIMx->CCMOD2;
  1829. /* Reset the OC3CE Bit */
  1830. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC3CEN);
  1831. /* Enable or Disable the Output Compare Clear Bit */
  1832. tmpccmr2 |= TIM_OCClear;
  1833. /* Write to TIMx CCMOD2 register */
  1834. TIMx->CCMOD2 = tmpccmr2;
  1835. }
  1836. /**
  1837. * @brief Clears or safeguards the OCREF4 signal on an external event
  1838. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1839. * @param TIM_OCClear new state of the Output Compare Clear Enable Bit.
  1840. * This parameter can be one of the following values:
  1841. * @arg TIM_OC_CLR_ENABLE TIM Output clear enable
  1842. * @arg TIM_OC_CLR_DISABLE TIM Output clear disable
  1843. */
  1844. void TIM_ClrOc4Ref(TIM_Module* TIMx, uint16_t TIM_OCClear)
  1845. {
  1846. uint16_t tmpccmr2 = 0;
  1847. /* Check the parameters */
  1848. assert_param(IsTimList3Module(TIMx));
  1849. assert_param(IsTimOcClrState(TIM_OCClear));
  1850. tmpccmr2 = TIMx->CCMOD2;
  1851. /* Reset the OC4CE Bit */
  1852. tmpccmr2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_OC4CEN);
  1853. /* Enable or Disable the Output Compare Clear Bit */
  1854. tmpccmr2 |= (uint16_t)(TIM_OCClear << 8);
  1855. /* Write to TIMx CCMOD2 register */
  1856. TIMx->CCMOD2 = tmpccmr2;
  1857. }
  1858. /**
  1859. * @brief Clears or safeguards the OCREF5 signal on an external event
  1860. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1861. * @param TIM_OCClear new state of the Output Compare Clear Enable Bit.
  1862. * This parameter can be one of the following values:
  1863. * @arg TIM_OC_CLR_ENABLE TIM Output clear enable
  1864. * @arg TIM_OC_CLR_DISABLE TIM Output clear disable
  1865. */
  1866. void TIM_ClrOc5Ref(TIM_Module* TIMx, uint16_t TIM_OCClear)
  1867. {
  1868. uint16_t tmpccmr3 = 0;
  1869. /* Check the parameters */
  1870. assert_param(IsTimList1Module(TIMx));
  1871. assert_param(IsTimOcClrState(TIM_OCClear));
  1872. tmpccmr3 = TIMx->CCMOD3;
  1873. /* Reset the OC4CE Bit */
  1874. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC5CEN);
  1875. /* Enable or Disable the Output Compare Clear Bit */
  1876. tmpccmr3 |= (uint16_t)(TIM_OCClear);
  1877. /* Write to TIMx CCMOD3 register */
  1878. TIMx->CCMOD3 = tmpccmr3;
  1879. }
  1880. /**
  1881. * @brief Clears or safeguards the OCREF6 signal on an external event
  1882. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1883. * @param TIM_OCClear new state of the Output Compare Clear Enable Bit.
  1884. * This parameter can be one of the following values:
  1885. * @arg TIM_OC_CLR_ENABLE TIM Output clear enable
  1886. * @arg TIM_OC_CLR_DISABLE TIM Output clear disable
  1887. */
  1888. void TIM_ClrOc6Ref(TIM_Module* TIMx, uint16_t TIM_OCClear)
  1889. {
  1890. uint16_t tmpccmr3 = 0;
  1891. /* Check the parameters */
  1892. assert_param(IsTimList1Module(TIMx));
  1893. assert_param(IsTimOcClrState(TIM_OCClear));
  1894. tmpccmr3 = TIMx->CCMOD3;
  1895. /* Reset the OC4CE Bit */
  1896. tmpccmr3 &= (uint16_t) ~((uint16_t)TIM_CCMOD3_OC6CEN);
  1897. /* Enable or Disable the Output Compare Clear Bit */
  1898. tmpccmr3 |= (uint16_t)(TIM_OCClear << 8);
  1899. /* Write to TIMx CCMOD3 register */
  1900. TIMx->CCMOD3 = tmpccmr3;
  1901. }
  1902. /**
  1903. * @brief Configures the TIMx channel 1 polarity.
  1904. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  1905. * @param OcPolarity specifies the OC1 Polarity
  1906. * This parameter can be one of the following values:
  1907. * @arg TIM_OC_POLARITY_HIGH Output Compare active high
  1908. * @arg TIM_OC_POLARITY_LOW Output Compare active low
  1909. */
  1910. void TIM_ConfigOc1Polarity(TIM_Module* TIMx, uint16_t OcPolarity)
  1911. {
  1912. uint32_t tmpccer = 0;
  1913. /* Check the parameters */
  1914. assert_param(IsTimList8Module(TIMx));
  1915. assert_param(IsTimOcPolarity(OcPolarity));
  1916. tmpccer = TIMx->CCEN;
  1917. /* Set or Reset the CC1P Bit */
  1918. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC1P);
  1919. tmpccer |= OcPolarity;
  1920. /* Write to TIMx CCEN register */
  1921. TIMx->CCEN = tmpccer;
  1922. }
  1923. /**
  1924. * @brief Configures the TIMx Channel 1N polarity.
  1925. * @param TIMx where x can be 1, 8 to select the TIM peripheral.
  1926. * @param OcNPolarity specifies the OC1N Polarity
  1927. * This parameter can be one of the following values:
  1928. * @arg TIM_OCN_POLARITY_HIGH Output Compare active high
  1929. * @arg TIM_OCN_POLARITY_LOW Output Compare active low
  1930. */
  1931. void TIM_ConfigOc1NPolarity(TIM_Module* TIMx, uint16_t OcNPolarity)
  1932. {
  1933. uint32_t tmpccer = 0;
  1934. /* Check the parameters */
  1935. assert_param(IsTimList2Module(TIMx));
  1936. assert_param(IsTimOcnPolarity(OcNPolarity));
  1937. tmpccer = TIMx->CCEN;
  1938. /* Set or Reset the CC1NP Bit */
  1939. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC1NP);
  1940. tmpccer |= OcNPolarity;
  1941. /* Write to TIMx CCEN register */
  1942. TIMx->CCEN = tmpccer;
  1943. }
  1944. /**
  1945. * @brief Configures the TIMx channel 2 polarity.
  1946. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  1947. * @param OcPolarity specifies the OC2 Polarity
  1948. * This parameter can be one of the following values:
  1949. * @arg TIM_OC_POLARITY_HIGH Output Compare active high
  1950. * @arg TIM_OC_POLARITY_LOW Output Compare active low
  1951. */
  1952. void TIM_ConfigOc2Polarity(TIM_Module* TIMx, uint16_t OcPolarity)
  1953. {
  1954. uint32_t tmpccer = 0;
  1955. /* Check the parameters */
  1956. assert_param(IsTimList6Module(TIMx));
  1957. assert_param(IsTimOcPolarity(OcPolarity));
  1958. tmpccer = TIMx->CCEN;
  1959. /* Set or Reset the CC2P Bit */
  1960. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC2P);
  1961. tmpccer |= (uint32_t)(OcPolarity << 4);
  1962. /* Write to TIMx CCEN register */
  1963. TIMx->CCEN = tmpccer;
  1964. }
  1965. /**
  1966. * @brief Configures the TIMx Channel 2N polarity.
  1967. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  1968. * @param OcNPolarity specifies the OC2N Polarity
  1969. * This parameter can be one of the following values:
  1970. * @arg TIM_OCN_POLARITY_HIGH Output Compare active high
  1971. * @arg TIM_OCN_POLARITY_LOW Output Compare active low
  1972. */
  1973. void TIM_ConfigOc2NPolarity(TIM_Module* TIMx, uint16_t OcNPolarity)
  1974. {
  1975. uint32_t tmpccer = 0;
  1976. /* Check the parameters */
  1977. assert_param(IsTimList1Module(TIMx));
  1978. assert_param(IsTimOcnPolarity(OcNPolarity));
  1979. tmpccer = TIMx->CCEN;
  1980. /* Set or Reset the CC2NP Bit */
  1981. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC2NP);
  1982. tmpccer |= (uint32_t)(OcNPolarity << 4);
  1983. /* Write to TIMx CCEN register */
  1984. TIMx->CCEN = tmpccer;
  1985. }
  1986. /**
  1987. * @brief Configures the TIMx channel 3 polarity.
  1988. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  1989. * @param OcPolarity specifies the OC3 Polarity
  1990. * This parameter can be one of the following values:
  1991. * @arg TIM_OC_POLARITY_HIGH Output Compare active high
  1992. * @arg TIM_OC_POLARITY_LOW Output Compare active low
  1993. */
  1994. void TIM_ConfigOc3Polarity(TIM_Module* TIMx, uint16_t OcPolarity)
  1995. {
  1996. uint32_t tmpccer = 0;
  1997. /* Check the parameters */
  1998. assert_param(IsTimList3Module(TIMx));
  1999. assert_param(IsTimOcPolarity(OcPolarity));
  2000. tmpccer = TIMx->CCEN;
  2001. /* Set or Reset the CC3P Bit */
  2002. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC3P);
  2003. tmpccer |= (uint32_t)(OcPolarity << 8);
  2004. /* Write to TIMx CCEN register */
  2005. TIMx->CCEN = tmpccer;
  2006. }
  2007. /**
  2008. * @brief Configures the TIMx Channel 3N polarity.
  2009. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  2010. * @param OcNPolarity specifies the OC3N Polarity
  2011. * This parameter can be one of the following values:
  2012. * @arg TIM_OCN_POLARITY_HIGH Output Compare active high
  2013. * @arg TIM_OCN_POLARITY_LOW Output Compare active low
  2014. */
  2015. void TIM_ConfigOc3NPolarity(TIM_Module* TIMx, uint16_t OcNPolarity)
  2016. {
  2017. uint32_t tmpccer = 0;
  2018. /* Check the parameters */
  2019. assert_param(IsTimList1Module(TIMx));
  2020. assert_param(IsTimOcnPolarity(OcNPolarity));
  2021. tmpccer = TIMx->CCEN;
  2022. /* Set or Reset the CC3NP Bit */
  2023. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC3NP);
  2024. tmpccer |= (uint32_t)(OcNPolarity << 8);
  2025. /* Write to TIMx CCEN register */
  2026. TIMx->CCEN = tmpccer;
  2027. }
  2028. /**
  2029. * @brief Configures the TIMx channel 4 polarity.
  2030. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2031. * @param OcPolarity specifies the OC4 Polarity
  2032. * This parameter can be one of the following values:
  2033. * @arg TIM_OC_POLARITY_HIGH Output Compare active high
  2034. * @arg TIM_OC_POLARITY_LOW Output Compare active low
  2035. */
  2036. void TIM_ConfigOc4Polarity(TIM_Module* TIMx, uint16_t OcPolarity)
  2037. {
  2038. uint32_t tmpccer = 0;
  2039. /* Check the parameters */
  2040. assert_param(IsTimList3Module(TIMx));
  2041. assert_param(IsTimOcPolarity(OcPolarity));
  2042. tmpccer = TIMx->CCEN;
  2043. /* Set or Reset the CC4P Bit */
  2044. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC4P);
  2045. tmpccer |= (uint32_t)(OcPolarity << 12);
  2046. /* Write to TIMx CCEN register */
  2047. TIMx->CCEN = tmpccer;
  2048. }
  2049. /**
  2050. * @brief Configures the TIMx channel 5 polarity.
  2051. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  2052. * @param OcPolarity specifies the OC5 Polarity
  2053. * This parameter can be one of the following values:
  2054. * @arg TIM_OC_POLARITY_HIGH Output Compare active high
  2055. * @arg TIM_OC_POLARITY_LOW Output Compare active low
  2056. */
  2057. void TIM_ConfigOc5Polarity(TIM_Module* TIMx, uint16_t OcPolarity)
  2058. {
  2059. uint32_t tmpccer = 0;
  2060. /* Check the parameters */
  2061. assert_param(IsTimList1Module(TIMx));
  2062. assert_param(IsTimOcPolarity(OcPolarity));
  2063. tmpccer = TIMx->CCEN;
  2064. /* Set or Reset the CC5P Bit */
  2065. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC5P);
  2066. tmpccer |= (uint32_t)(OcPolarity << 16);
  2067. /* Write to TIMx CCEN register */
  2068. TIMx->CCEN = tmpccer;
  2069. }
  2070. /**
  2071. * @brief Configures the TIMx channel 6 polarity.
  2072. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  2073. * @param OcPolarity specifies the OC6 Polarity
  2074. * This parameter can be one of the following values:
  2075. * @arg TIM_OC_POLARITY_HIGH Output Compare active high
  2076. * @arg TIM_OC_POLARITY_LOW Output Compare active low
  2077. */
  2078. void TIM_ConfigOc6Polarity(TIM_Module* TIMx, uint16_t OcPolarity)
  2079. {
  2080. uint32_t tmpccer = 0;
  2081. /* Check the parameters */
  2082. assert_param(IsTimList1Module(TIMx));
  2083. assert_param(IsTimOcPolarity(OcPolarity));
  2084. tmpccer = TIMx->CCEN;
  2085. /* Set or Reset the CC6P Bit */
  2086. tmpccer &= (uint32_t) ~((uint32_t)TIM_CCEN_CC6P);
  2087. tmpccer |= (uint32_t)(OcPolarity << 20);
  2088. /* Write to TIMx CCEN register */
  2089. TIMx->CCEN = tmpccer;
  2090. }
  2091. /**
  2092. * @brief Enables or disables the TIM Capture Compare Channel x.
  2093. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  2094. * @param Channel specifies the TIM Channel
  2095. * This parameter can be one of the following values:
  2096. * @arg TIM_CH_1 TIM Channel 1
  2097. * @arg TIM_CH_2 TIM Channel 2
  2098. * @arg TIM_CH_3 TIM Channel 3
  2099. * @arg TIM_CH_4 TIM Channel 4
  2100. * @param TIM_CCx specifies the TIM Channel CCxE bit new state.
  2101. * This parameter can be: TIM_CAP_CMP_ENABLE or TIM_CAP_CMP_DISABLE.
  2102. */
  2103. void TIM_EnableCapCmpCh(TIM_Module* TIMx, uint16_t Channel, uint32_t TIM_CCx)
  2104. {
  2105. uint16_t tmp = 0;
  2106. /* Check the parameters */
  2107. assert_param(IsTimList8Module(TIMx));
  2108. assert_param(IsTimCh(Channel));
  2109. assert_param(IsTimCapCmpState(TIM_CCx));
  2110. tmp = CAPCMPEN_CCE_SET << Channel;
  2111. /* Reset the CCxE Bit */
  2112. TIMx->CCEN &= (uint32_t)~tmp;
  2113. /* Set or reset the CCxE Bit */
  2114. TIMx->CCEN |= (uint32_t)(TIM_CCx << Channel);
  2115. }
  2116. /**
  2117. * @brief Enables or disables the TIM Capture Compare Channel xN.
  2118. * @param TIMx where x can be 1, 8 to select the TIM peripheral.
  2119. * @param Channel specifies the TIM Channel
  2120. * This parameter can be one of the following values:
  2121. * @arg TIM_CH_1 TIM Channel 1
  2122. * @arg TIM_CH_2 TIM Channel 2
  2123. * @arg TIM_CH_3 TIM Channel 3
  2124. * @param TIM_CCxN specifies the TIM Channel CCxNE bit new state.
  2125. * This parameter can be: TIM_CAP_CMP_N_ENABLE or TIM_CAP_CMP_N_DISABLE.
  2126. */
  2127. void TIM_EnableCapCmpChN(TIM_Module* TIMx, uint16_t Channel, uint32_t TIM_CCxN)
  2128. {
  2129. uint16_t tmp = 0;
  2130. /* Check the parameters */
  2131. assert_param(IsTimList2Module(TIMx));
  2132. assert_param(IsTimComplementaryCh(Channel));
  2133. assert_param(IsTimCapCmpNState(TIM_CCxN));
  2134. tmp = CAPCMPEN_CCNE_SET << Channel;
  2135. /* Reset the CCxNE Bit */
  2136. TIMx->CCEN &= (uint32_t)~tmp;
  2137. /* Set or reset the CCxNE Bit */
  2138. TIMx->CCEN |= (uint32_t)(TIM_CCxN << Channel);
  2139. }
  2140. /**
  2141. * @brief Selects the TIM Output Compare Mode.
  2142. * @note This function disables the selected channel before changing the Output
  2143. * Compare Mode.
  2144. * User has to enable this channel using TIM_EnableCapCmpCh and TIM_EnableCapCmpChN functions.
  2145. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  2146. * @param Channel specifies the TIM Channel
  2147. * This parameter can be one of the following values:
  2148. * @arg TIM_CH_1 TIM Channel 1
  2149. * @arg TIM_CH_2 TIM Channel 2
  2150. * @arg TIM_CH_3 TIM Channel 3
  2151. * @arg TIM_CH_4 TIM Channel 4
  2152. * @param OcMode specifies the TIM Output Compare Mode.
  2153. * This parameter can be one of the following values:
  2154. * @arg TIM_OCMODE_TIMING
  2155. * @arg TIM_OCMODE_ACTIVE
  2156. * @arg TIM_OCMODE_TOGGLE
  2157. * @arg TIM_OCMODE_PWM1
  2158. * @arg TIM_OCMODE_PWM2
  2159. * @arg TIM_FORCED_ACTION_ACTIVE
  2160. * @arg TIM_FORCED_ACTION_INACTIVE
  2161. */
  2162. void TIM_SelectOcMode(TIM_Module* TIMx, uint16_t Channel, uint16_t OcMode)
  2163. {
  2164. uint32_t tmp = 0;
  2165. uint16_t tmp1 = 0;
  2166. /* Check the parameters */
  2167. assert_param(IsTimList8Module(TIMx));
  2168. assert_param(IsTimCh(Channel));
  2169. assert_param(IsTimOc(OcMode));
  2170. tmp = (uint32_t)TIMx;
  2171. tmp += CAPCMPMOD_OFFSET;
  2172. tmp1 = CAPCMPEN_CCE_SET << (uint16_t)Channel;
  2173. /* Disable the Channel: Reset the CCxE Bit */
  2174. TIMx->CCEN &= (uint16_t)~tmp1;
  2175. if ((Channel == TIM_CH_1) || (Channel == TIM_CH_3))
  2176. {
  2177. tmp += (Channel >> 1);
  2178. /* Reset the OCxM bits in the CCMRx register */
  2179. *(__IO uint32_t*)tmp &= (uint32_t) ~((uint32_t)TIM_CCMOD1_OC1M);
  2180. /* Configure the OCxM bits in the CCMRx register */
  2181. *(__IO uint32_t*)tmp |= OcMode;
  2182. }
  2183. else
  2184. {
  2185. tmp += (uint16_t)(Channel - (uint16_t)4) >> (uint16_t)1;
  2186. /* Reset the OCxM bits in the CCMRx register */
  2187. *(__IO uint32_t*)tmp &= (uint32_t) ~((uint32_t)TIM_CCMOD1_OC2M);
  2188. /* Configure the OCxM bits in the CCMRx register */
  2189. *(__IO uint32_t*)tmp |= (uint16_t)(OcMode << 8);
  2190. }
  2191. }
  2192. /**
  2193. * @brief Enables or Disables the TIMx Update event.
  2194. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2195. * @param Cmd new state of the TIMx UDIS bit
  2196. * This parameter can be: ENABLE or DISABLE.
  2197. */
  2198. void TIM_EnableUpdateEvt(TIM_Module* TIMx, FunctionalState Cmd)
  2199. {
  2200. /* Check the parameters */
  2201. assert_param(IsTimAllModule(TIMx));
  2202. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2203. if (Cmd != DISABLE)
  2204. {
  2205. /* Set the Update Disable Bit */
  2206. TIMx->CTRL1 |= TIM_CTRL1_UPDIS;
  2207. }
  2208. else
  2209. {
  2210. /* Reset the Update Disable Bit */
  2211. TIMx->CTRL1 &= (uint32_t) ~((uint32_t)TIM_CTRL1_UPDIS);
  2212. }
  2213. }
  2214. /**
  2215. * @brief Configures the TIMx Update Request Interrupt source.
  2216. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2217. * @param TIM_UpdateSource specifies the Update source.
  2218. * This parameter can be one of the following values:
  2219. * @arg TIM_UPDATE_SRC_REGULAr Source of update is the counter overflow/underflow
  2220. or the setting of UG bit, or an update generation
  2221. through the slave mode controller.
  2222. * @arg TIM_UPDATE_SRC_GLOBAL Source of update is counter overflow/underflow.
  2223. */
  2224. void TIM_ConfigUpdateRequestIntSrc(TIM_Module* TIMx, uint16_t TIM_UpdateSource)
  2225. {
  2226. /* Check the parameters */
  2227. assert_param(IsTimAllModule(TIMx));
  2228. assert_param(IsTimUpdateSrc(TIM_UpdateSource));
  2229. if (TIM_UpdateSource != TIM_UPDATE_SRC_GLOBAL)
  2230. {
  2231. /* Set the URS Bit */
  2232. TIMx->CTRL1 |= TIM_CTRL1_UPRS;
  2233. }
  2234. else
  2235. {
  2236. /* Reset the URS Bit */
  2237. TIMx->CTRL1 &= (uint32_t) ~((uint32_t)TIM_CTRL1_UPRS);
  2238. }
  2239. }
  2240. /**
  2241. * @brief Enables or disables the TIMx's Hall sensor interface.
  2242. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2243. * @param Cmd new state of the TIMx Hall sensor interface.
  2244. * This parameter can be: ENABLE or DISABLE.
  2245. */
  2246. void TIM_SelectHallSensor(TIM_Module* TIMx, FunctionalState Cmd)
  2247. {
  2248. /* Check the parameters */
  2249. assert_param(IsTimList6Module(TIMx));
  2250. assert_param(IS_FUNCTIONAL_STATE(Cmd));
  2251. if (Cmd != DISABLE)
  2252. {
  2253. /* Set the TI1S Bit */
  2254. TIMx->CTRL2 |= TIM_CTRL2_TI1SEL;
  2255. }
  2256. else
  2257. {
  2258. /* Reset the TI1S Bit */
  2259. TIMx->CTRL2 &= (uint32_t) ~((uint32_t)TIM_CTRL2_TI1SEL);
  2260. }
  2261. }
  2262. /**
  2263. * @brief Selects the TIMx's One Pulse Mode.
  2264. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2265. * @param TIM_OPMode specifies the OPM Mode to be used.
  2266. * This parameter can be one of the following values:
  2267. * @arg TIM_OPMODE_SINGLE
  2268. * @arg TIM_OPMODE_REPET
  2269. */
  2270. void TIM_SelectOnePulseMode(TIM_Module* TIMx, uint16_t TIM_OPMode)
  2271. {
  2272. /* Check the parameters */
  2273. assert_param(IsTimAllModule(TIMx));
  2274. assert_param(IsTimOpMOde(TIM_OPMode));
  2275. /* Reset the OPM Bit */
  2276. TIMx->CTRL1 &= (uint32_t) ~((uint32_t)TIM_CTRL1_ONEPM);
  2277. /* Configure the OPM Mode */
  2278. TIMx->CTRL1 |= TIM_OPMode;
  2279. }
  2280. /**
  2281. * @brief Selects the TIMx Trigger Output Mode.
  2282. * @param TIMx where x can be 1, 2, 3, 4, 5, 6, 7, 8 to select the TIM peripheral.
  2283. * @param TIM_TRGOSource specifies the Trigger Output source.
  2284. * This paramter can be one of the following values:
  2285. *
  2286. * - For all TIMx
  2287. * @arg TIM_TRGO_SRC_RESET The UG bit in the TIM_EVTGEN register is used as the trigger output (TRGO).
  2288. * @arg TIM_TRGO_SRC_ENABLE The Counter Enable CEN is used as the trigger output (TRGO).
  2289. * @arg TIM_TRGO_SRC_UPDATE The update event is selected as the trigger output (TRGO).
  2290. *
  2291. * - For all TIMx except TIM6 and TIM7
  2292. * @arg TIM_TRGO_SRC_OC1 The trigger output sends a positive pulse when the CC1IF flag
  2293. * is to be set, as soon as a capture or compare match occurs (TRGO).
  2294. * @arg TIM_TRGO_SRC_OC1REF OC1REF signal is used as the trigger output (TRGO).
  2295. * @arg TIM_TRGO_SRC_OC2REF OC2REF signal is used as the trigger output (TRGO).
  2296. * @arg TIM_TRGO_SRC_OC3REF OC3REF signal is used as the trigger output (TRGO).
  2297. * @arg TIM_TRGO_SRC_OC4REF OC4REF signal is used as the trigger output (TRGO).
  2298. *
  2299. */
  2300. void TIM_SelectOutputTrig(TIM_Module* TIMx, uint16_t TIM_TRGOSource)
  2301. {
  2302. /* Check the parameters */
  2303. assert_param(IsTimList7Module(TIMx));
  2304. assert_param(IsTimTrgoSrc(TIM_TRGOSource));
  2305. /* Reset the MMS Bits */
  2306. TIMx->CTRL2 &= (uint32_t) ~((uint32_t)TIM_CTRL2_MMSEL);
  2307. /* Select the TRGO source */
  2308. TIMx->CTRL2 |= TIM_TRGOSource;
  2309. }
  2310. /**
  2311. * @brief Selects the TIMx Ext Source.
  2312. * @param Source: 0:selection external ETR(from IOM) signal.1:selection internal ETR(fromo TSC) signal.
  2313. * @retval None
  2314. */
  2315. void TIM_SelectExtSignalSource(TIM_Module* TIMx, uint16_t ExtSigalSource)
  2316. {
  2317. /* Select Ext Signal Source Bits */
  2318. TIMx->CTRL2 &= (uint32_t)~((uint32_t)TIM_ETR_Seletct_innerTsc);
  2319. /* Select the TRGO source */
  2320. TIMx->CTRL2 |= ExtSigalSource;
  2321. }
  2322. /**
  2323. * @brief Selects the TIMx Slave Mode.
  2324. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  2325. * @param TIM_SlaveMode specifies the Timer Slave Mode.
  2326. * This parameter can be one of the following values:
  2327. * @arg TIM_SLAVE_MODE_RESET Rising edge of the selected trigger signal (TRGI) re-initializes
  2328. * the counter and triggers an update of the registers.
  2329. * @arg TIM_SLAVE_MODE_GATED The counter clock is enabled when the trigger signal (TRGI) is high.
  2330. * @arg TIM_SLAVE_MODE_TRIG The counter starts at a rising edge of the trigger TRGI.
  2331. * @arg TIM_SLAVE_MODE_EXT1 Rising edges of the selected trigger (TRGI) clock the counter.
  2332. */
  2333. void TIM_SelectSlaveMode(TIM_Module* TIMx, uint16_t TIM_SlaveMode)
  2334. {
  2335. /* Check the parameters */
  2336. assert_param(IsTimList6Module(TIMx));
  2337. assert_param(IsTimSlaveMode(TIM_SlaveMode));
  2338. /* Reset the SMS Bits */
  2339. TIMx->SMCTRL &= (uint16_t) ~((uint16_t)TIM_SMCTRL_SMSEL);
  2340. /* Select the Slave Mode */
  2341. TIMx->SMCTRL |= TIM_SlaveMode;
  2342. }
  2343. /**
  2344. * @brief Sets or Resets the TIMx Master/Slave Mode.
  2345. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  2346. * @param TIM_MasterSlaveMode specifies the Timer Master Slave Mode.
  2347. * This parameter can be one of the following values:
  2348. * @arg TIM_MASTER_SLAVE_MODE_ENABLE synchronization between the current timer
  2349. * and its slaves (through TRGO).
  2350. * @arg TIM_MASTER_SLAVE_MODE_DISABLE No action
  2351. */
  2352. void TIM_SelectMasterSlaveMode(TIM_Module* TIMx, uint16_t TIM_MasterSlaveMode)
  2353. {
  2354. /* Check the parameters */
  2355. assert_param(IsTimList6Module(TIMx));
  2356. assert_param(IsTimMasterSlaveMode(TIM_MasterSlaveMode));
  2357. /* Reset the MSM Bit */
  2358. TIMx->SMCTRL &= (uint16_t) ~((uint16_t)TIM_SMCTRL_MSMD);
  2359. /* Set or Reset the MSM Bit */
  2360. TIMx->SMCTRL |= TIM_MasterSlaveMode;
  2361. }
  2362. /**
  2363. * @brief Sets the TIMx Counter Register value
  2364. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2365. * @param Counter specifies the Counter register new value.
  2366. */
  2367. void TIM_SetCnt(TIM_Module* TIMx, uint16_t Counter)
  2368. {
  2369. /* Check the parameters */
  2370. assert_param(IsTimAllModule(TIMx));
  2371. /* Set the Counter Register value */
  2372. TIMx->CNT = Counter;
  2373. }
  2374. /**
  2375. * @brief Sets the TIMx Autoreload Register value
  2376. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2377. * @param Autoreload specifies the Autoreload register new value.
  2378. */
  2379. void TIM_SetAutoReload(TIM_Module* TIMx, uint16_t Autoreload)
  2380. {
  2381. /* Check the parameters */
  2382. assert_param(IsTimAllModule(TIMx));
  2383. /* Set the Autoreload Register value */
  2384. TIMx->AR = Autoreload;
  2385. }
  2386. /**
  2387. * @brief Sets the TIMx Capture Compare1 Register value
  2388. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  2389. * @param Compare1 specifies the Capture Compare1 register new value.
  2390. */
  2391. void TIM_SetCmp1(TIM_Module* TIMx, uint16_t Compare1)
  2392. {
  2393. /* Check the parameters */
  2394. assert_param(IsTimList8Module(TIMx));
  2395. /* Set the Capture Compare1 Register value */
  2396. TIMx->CCDAT1 = Compare1;
  2397. }
  2398. /**
  2399. * @brief Sets the TIMx Capture Compare2 Register value
  2400. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  2401. * @param Compare2 specifies the Capture Compare2 register new value.
  2402. */
  2403. void TIM_SetCmp2(TIM_Module* TIMx, uint16_t Compare2)
  2404. {
  2405. /* Check the parameters */
  2406. assert_param(IsTimList6Module(TIMx));
  2407. /* Set the Capture Compare2 Register value */
  2408. TIMx->CCDAT2 = Compare2;
  2409. }
  2410. /**
  2411. * @brief Sets the TIMx Capture Compare3 Register value
  2412. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2413. * @param Compare3 specifies the Capture Compare3 register new value.
  2414. */
  2415. void TIM_SetCmp3(TIM_Module* TIMx, uint16_t Compare3)
  2416. {
  2417. /* Check the parameters */
  2418. assert_param(IsTimList3Module(TIMx));
  2419. /* Set the Capture Compare3 Register value */
  2420. TIMx->CCDAT3 = Compare3;
  2421. }
  2422. /**
  2423. * @brief Sets the TIMx Capture Compare4 Register value
  2424. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2425. * @param Compare4 specifies the Capture Compare4 register new value.
  2426. */
  2427. void TIM_SetCmp4(TIM_Module* TIMx, uint16_t Compare4)
  2428. {
  2429. /* Check the parameters */
  2430. assert_param(IsTimList3Module(TIMx));
  2431. /* Set the Capture Compare4 Register value */
  2432. TIMx->CCDAT4 = Compare4;
  2433. }
  2434. /**
  2435. * @brief Sets the TIMx Capture Compare5 Register value
  2436. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  2437. * @param Compare5 specifies the Capture Compare5 register new value.
  2438. */
  2439. void TIM_SetCmp5(TIM_Module* TIMx, uint16_t Compare5)
  2440. {
  2441. /* Check the parameters */
  2442. assert_param(IsTimList1Module(TIMx));
  2443. /* Set the Capture Compare4 Register value */
  2444. TIMx->CCDAT5 = Compare5;
  2445. }
  2446. /**
  2447. * @brief Sets the TIMx Capture Compare4 Register value
  2448. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  2449. * @param Compare6 specifies the Capture Compare6 register new value.
  2450. */
  2451. void TIM_SetCmp6(TIM_Module* TIMx, uint16_t Compare6)
  2452. {
  2453. /* Check the parameters */
  2454. assert_param(IsTimList1Module(TIMx));
  2455. /* Set the Capture Compare4 Register value */
  2456. TIMx->CCDAT6 = Compare6;
  2457. }
  2458. /**
  2459. * @brief Sets the TIMx Input Capture 1 prescaler.
  2460. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  2461. * @param TIM_ICPSC specifies the Input Capture1 prescaler new value.
  2462. * This parameter can be one of the following values:
  2463. * @arg TIM_IC_PSC_DIV1 no prescaler
  2464. * @arg TIM_IC_PSC_DIV2 capture is done once every 2 events
  2465. * @arg TIM_IC_PSC_DIV4 capture is done once every 4 events
  2466. * @arg TIM_IC_PSC_DIV8 capture is done once every 8 events
  2467. */
  2468. void TIM_SetInCap1Prescaler(TIM_Module* TIMx, uint16_t TIM_ICPSC)
  2469. {
  2470. /* Check the parameters */
  2471. assert_param(IsTimList8Module(TIMx));
  2472. assert_param(IsTimIcPrescaler(TIM_ICPSC));
  2473. /* Reset the IC1PSC Bits */
  2474. TIMx->CCMOD1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_IC1PSC);
  2475. /* Set the IC1PSC value */
  2476. TIMx->CCMOD1 |= TIM_ICPSC;
  2477. }
  2478. /**
  2479. * @brief Sets the TIMx Input Capture 2 prescaler.
  2480. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  2481. * @param TIM_ICPSC specifies the Input Capture2 prescaler new value.
  2482. * This parameter can be one of the following values:
  2483. * @arg TIM_IC_PSC_DIV1 no prescaler
  2484. * @arg TIM_IC_PSC_DIV2 capture is done once every 2 events
  2485. * @arg TIM_IC_PSC_DIV4 capture is done once every 4 events
  2486. * @arg TIM_IC_PSC_DIV8 capture is done once every 8 events
  2487. */
  2488. void TIM_SetInCap2Prescaler(TIM_Module* TIMx, uint16_t TIM_ICPSC)
  2489. {
  2490. /* Check the parameters */
  2491. assert_param(IsTimList6Module(TIMx));
  2492. assert_param(IsTimIcPrescaler(TIM_ICPSC));
  2493. /* Reset the IC2PSC Bits */
  2494. TIMx->CCMOD1 &= (uint16_t) ~((uint16_t)TIM_CCMOD1_IC2PSC);
  2495. /* Set the IC2PSC value */
  2496. TIMx->CCMOD1 |= (uint16_t)(TIM_ICPSC << 8);
  2497. }
  2498. /**
  2499. * @brief Sets the TIMx Input Capture 3 prescaler.
  2500. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2501. * @param TIM_ICPSC specifies the Input Capture3 prescaler new value.
  2502. * This parameter can be one of the following values:
  2503. * @arg TIM_IC_PSC_DIV1 no prescaler
  2504. * @arg TIM_IC_PSC_DIV2 capture is done once every 2 events
  2505. * @arg TIM_IC_PSC_DIV4 capture is done once every 4 events
  2506. * @arg TIM_IC_PSC_DIV8 capture is done once every 8 events
  2507. */
  2508. void TIM_SetInCap3Prescaler(TIM_Module* TIMx, uint16_t TIM_ICPSC)
  2509. {
  2510. /* Check the parameters */
  2511. assert_param(IsTimList3Module(TIMx));
  2512. assert_param(IsTimIcPrescaler(TIM_ICPSC));
  2513. /* Reset the IC3PSC Bits */
  2514. TIMx->CCMOD2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_IC3PSC);
  2515. /* Set the IC3PSC value */
  2516. TIMx->CCMOD2 |= TIM_ICPSC;
  2517. }
  2518. /**
  2519. * @brief Sets the TIMx Input Capture 4 prescaler.
  2520. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2521. * @param TIM_ICPSC specifies the Input Capture4 prescaler new value.
  2522. * This parameter can be one of the following values:
  2523. * @arg TIM_IC_PSC_DIV1 no prescaler
  2524. * @arg TIM_IC_PSC_DIV2 capture is done once every 2 events
  2525. * @arg TIM_IC_PSC_DIV4 capture is done once every 4 events
  2526. * @arg TIM_IC_PSC_DIV8 capture is done once every 8 events
  2527. */
  2528. void TIM_SetInCap4Prescaler(TIM_Module* TIMx, uint16_t TIM_ICPSC)
  2529. {
  2530. /* Check the parameters */
  2531. assert_param(IsTimList3Module(TIMx));
  2532. assert_param(IsTimIcPrescaler(TIM_ICPSC));
  2533. /* Reset the IC4PSC Bits */
  2534. TIMx->CCMOD2 &= (uint16_t) ~((uint16_t)TIM_CCMOD2_IC4PSC);
  2535. /* Set the IC4PSC value */
  2536. TIMx->CCMOD2 |= (uint16_t)(TIM_ICPSC << 8);
  2537. }
  2538. /**
  2539. * @brief Sets the TIMx Clock Division value.
  2540. * @param TIMx where x can be 1 to 8 except 6 and 7 to select
  2541. * the TIM peripheral.
  2542. * @param TIM_CKD specifies the clock division value.
  2543. * This parameter can be one of the following value:
  2544. * @arg TIM_CLK_DIV1 TDTS = Tck_tim
  2545. * @arg TIM_CLK_DIV2 TDTS = 2*Tck_tim
  2546. * @arg TIM_CLK_DIV4 TDTS = 4*Tck_tim
  2547. */
  2548. void TIM_SetClkDiv(TIM_Module* TIMx, uint16_t TIM_CKD)
  2549. {
  2550. /* Check the parameters */
  2551. assert_param(IsTimList8Module(TIMx));
  2552. assert_param(IsTimClkDiv(TIM_CKD));
  2553. /* Reset the CKD Bits */
  2554. TIMx->CTRL1 &= (uint32_t) ~((uint32_t)TIM_CTRL1_CLKD);
  2555. /* Set the CKD value */
  2556. TIMx->CTRL1 |= TIM_CKD;
  2557. }
  2558. /**
  2559. * @brief Gets the TIMx Input Capture 1 value.
  2560. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  2561. * @return Capture Compare 1 Register value.
  2562. */
  2563. uint16_t TIM_GetCap1(TIM_Module* TIMx)
  2564. {
  2565. /* Check the parameters */
  2566. assert_param(IsTimList8Module(TIMx));
  2567. /* Get the Capture 1 Register value */
  2568. return TIMx->CCDAT1;
  2569. }
  2570. /**
  2571. * @brief Gets the TIMx Input Capture 2 value.
  2572. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  2573. * @return Capture Compare 2 Register value.
  2574. */
  2575. uint16_t TIM_GetCap2(TIM_Module* TIMx)
  2576. {
  2577. /* Check the parameters */
  2578. assert_param(IsTimList6Module(TIMx));
  2579. /* Get the Capture 2 Register value */
  2580. return TIMx->CCDAT2;
  2581. }
  2582. /**
  2583. * @brief Gets the TIMx Input Capture 3 value.
  2584. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2585. * @return Capture Compare 3 Register value.
  2586. */
  2587. uint16_t TIM_GetCap3(TIM_Module* TIMx)
  2588. {
  2589. /* Check the parameters */
  2590. assert_param(IsTimList3Module(TIMx));
  2591. /* Get the Capture 3 Register value */
  2592. return TIMx->CCDAT3;
  2593. }
  2594. /**
  2595. * @brief Gets the TIMx Input Capture 4 value.
  2596. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2597. * @return Capture Compare 4 Register value.
  2598. */
  2599. uint16_t TIM_GetCap4(TIM_Module* TIMx)
  2600. {
  2601. /* Check the parameters */
  2602. assert_param(IsTimList3Module(TIMx));
  2603. /* Get the Capture 4 Register value */
  2604. return TIMx->CCDAT4;
  2605. }
  2606. /**
  2607. * @brief Gets the TIMx Input Capture 5 value.
  2608. * @param TIMx where x can be 1 8 to select the TIM peripheral.
  2609. * @return Capture Compare 5 Register value.
  2610. */
  2611. uint16_t TIM_GetCap5(TIM_Module* TIMx)
  2612. {
  2613. /* Check the parameters */
  2614. assert_param(IsTimList1Module(TIMx));
  2615. /* Get the Capture 5 Register value */
  2616. return TIMx->CCDAT5;
  2617. }
  2618. /**
  2619. * @brief Gets the TIMx Input Capture 6 value.
  2620. * @param TIMx where x can be 1 or 8 to select the TIM peripheral.
  2621. * @return Capture Compare 6 Register value.
  2622. */
  2623. uint16_t TIM_GetCap6(TIM_Module* TIMx)
  2624. {
  2625. /* Check the parameters */
  2626. assert_param(IsTimList1Module(TIMx));
  2627. /* Get the Capture 6 Register value */
  2628. return TIMx->CCDAT6;
  2629. }
  2630. /**
  2631. * @brief Gets the TIMx Counter value.
  2632. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2633. * @return Counter Register value.
  2634. */
  2635. uint16_t TIM_GetCnt(TIM_Module* TIMx)
  2636. {
  2637. /* Check the parameters */
  2638. assert_param(IsTimAllModule(TIMx));
  2639. /* Get the Counter Register value */
  2640. return TIMx->CNT;
  2641. }
  2642. /**
  2643. * @brief Gets the TIMx Prescaler value.
  2644. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2645. * @return Prescaler Register value.
  2646. */
  2647. uint16_t TIM_GetPrescaler(TIM_Module* TIMx)
  2648. {
  2649. /* Check the parameters */
  2650. assert_param(IsTimAllModule(TIMx));
  2651. /* Get the Prescaler Register value */
  2652. return TIMx->PSC;
  2653. }
  2654. /**
  2655. * @brief Gets the TIMx Prescaler value.
  2656. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2657. * @return Prescaler Register value.
  2658. */
  2659. uint16_t TIM_GetAutoReload(TIM_Module* TIMx)
  2660. {
  2661. /* Check the parameters */
  2662. assert_param(IsTimAllModule(TIMx));
  2663. /* Get the Prescaler Register value */
  2664. return TIMx->AR;
  2665. }
  2666. /**
  2667. * @brief Checks whether the specified TIM flag is set or not.
  2668. * @param TIMx where x can be 1 to 5 , 8 to select the TIM peripheral.
  2669. * @param TIM_CCEN specifies the Bit to check.
  2670. * This parameter can be one of the following values:
  2671. * @arg TIM_CC1EN CC1EN Bit
  2672. * @arg TIM_CC1NEN CC1NEN Bit
  2673. * @arg TIM_CC2EN CC2EN Bit
  2674. * @arg TIM_CC2NEN CC2NEN Bit
  2675. * @arg TIM_CC3EN CC3EN Bit
  2676. * @arg TIM_CC3NEN CC3NEN Bit
  2677. * @arg TIM_CC4EN CC4EN Bit
  2678. * @arg TIM_CC5EN CC5EN Bit
  2679. * @arg TIM_CC6EN CC6EN Bit
  2680. * @note
  2681. * - TIM_CC1NEN TIM_CC2NEN TIM_CC3NEN is used only with TIM1, TIM8.
  2682. * @return The new state of TIM_FLAG (SET or RESET).
  2683. */
  2684. FlagStatus TIM_GetCCENStatus(TIM_Module* TIMx, uint32_t TIM_CCEN)
  2685. {
  2686. INTStatus bitstatus = RESET;
  2687. /* Check the parameters */
  2688. assert_param(IsTimList3Module(TIMx));
  2689. if(TIMx==TIM1 || TIMx==TIM8)
  2690. {
  2691. assert_param(IsAdvancedTimCCENFlag(TIM_CCEN));
  2692. if ((TIMx->CCEN & TIM_CCEN) != (uint32_t)RESET)
  2693. {
  2694. bitstatus = SET;
  2695. }
  2696. else
  2697. {
  2698. bitstatus = RESET;
  2699. }
  2700. }
  2701. else if(TIMx==TIM2 || TIMx==TIM3 || TIMx==TIM4 || TIMx==TIM5 )
  2702. {
  2703. assert_param(IsGeneralTimCCENFlag(TIM_CCEN));
  2704. if ((TIMx->CCEN & TIM_CCEN) != (uint32_t)RESET)
  2705. {
  2706. bitstatus = SET;
  2707. }
  2708. else
  2709. {
  2710. bitstatus = RESET;
  2711. }
  2712. }
  2713. return bitstatus;
  2714. }
  2715. /**
  2716. * @brief Checks whether the specified TIM flag is set or not.
  2717. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2718. * @param TIM_FLAG specifies the flag to check.
  2719. * This parameter can be one of the following values:
  2720. * @arg TIM_FLAG_UPDATE TIM update Flag
  2721. * @arg TIM_FLAG_CC1 TIM Capture Compare 1 Flag
  2722. * @arg TIM_FLAG_CC2 TIM Capture Compare 2 Flag
  2723. * @arg TIM_FLAG_CC3 TIM Capture Compare 3 Flag
  2724. * @arg TIM_FLAG_CC4 TIM Capture Compare 4 Flag
  2725. * @arg TIM_FLAG_COM TIM Commutation Flag
  2726. * @arg TIM_FLAG_TRIG TIM Trigger Flag
  2727. * @arg TIM_FLAG_BREAK TIM Break Flag
  2728. * @arg TIM_FLAG_CC1OF TIM Capture Compare 1 overcapture Flag
  2729. * @arg TIM_FLAG_CC2OF TIM Capture Compare 2 overcapture Flag
  2730. * @arg TIM_FLAG_CC3OF TIM Capture Compare 3 overcapture Flag
  2731. * @arg TIM_FLAG_CC4OF TIM Capture Compare 4 overcapture Flag
  2732. * @arg TIM_FLAG_CC5 TIM Capture Compare 5 Flag
  2733. * @arg TIM_FLAG_CC6 TIM Capture Compare 6 Flag
  2734. * @note
  2735. * - TIM6 and TIM7 can have only one update flag.
  2736. * - TIM_FLAG_BREAK is used only with TIM1, TIM8.
  2737. * - TIM_FLAG_COM is used only with TIM1, TIM8.
  2738. * @return The new state of TIM_FLAG (SET or RESET).
  2739. */
  2740. FlagStatus TIM_GetFlagStatus(TIM_Module* TIMx, uint32_t TIM_FLAG)
  2741. {
  2742. INTStatus bitstatus = RESET;
  2743. /* Check the parameters */
  2744. assert_param(IsTimAllModule(TIMx));
  2745. assert_param(IsTimGetFlag(TIM_FLAG));
  2746. if ((TIMx->STS & TIM_FLAG) != (uint32_t)RESET)
  2747. {
  2748. bitstatus = SET;
  2749. }
  2750. else
  2751. {
  2752. bitstatus = RESET;
  2753. }
  2754. return bitstatus;
  2755. }
  2756. /**
  2757. * @brief Clears the TIMx's pending flags.
  2758. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2759. * @param TIM_FLAG specifies the flag bit to clear.
  2760. * This parameter can be any combination of the following values:
  2761. * @arg TIM_FLAG_UPDATE TIM update Flag
  2762. * @arg TIM_FLAG_CC1 TIM Capture Compare 1 Flag
  2763. * @arg TIM_FLAG_CC2 TIM Capture Compare 2 Flag
  2764. * @arg TIM_FLAG_CC3 TIM Capture Compare 3 Flag
  2765. * @arg TIM_FLAG_CC4 TIM Capture Compare 4 Flag
  2766. * @arg TIM_FLAG_COM TIM Commutation Flag
  2767. * @arg TIM_FLAG_TRIG TIM Trigger Flag
  2768. * @arg TIM_FLAG_BREAK TIM Break Flag
  2769. * @arg TIM_FLAG_CC1OF TIM Capture Compare 1 overcapture Flag
  2770. * @arg TIM_FLAG_CC2OF TIM Capture Compare 2 overcapture Flag
  2771. * @arg TIM_FLAG_CC3OF TIM Capture Compare 3 overcapture Flag
  2772. * @arg TIM_FLAG_CC4OF TIM Capture Compare 4 overcapture Flag
  2773. * @note
  2774. * - TIM6 and TIM7 can have only one update flag.
  2775. * - TIM_FLAG_BREAK is used only with TIM1, TIM8.
  2776. * - TIM_FLAG_COM is used only with TIM1, TIM8.
  2777. */
  2778. void TIM_ClearFlag(TIM_Module* TIMx, uint32_t TIM_FLAG)
  2779. {
  2780. /* Check the parameters */
  2781. assert_param(IsTimAllModule(TIMx));
  2782. assert_param(IsTimClrFlag(TIM_FLAG));
  2783. /* Clear the flags */
  2784. TIMx->STS = (uint32_t)~TIM_FLAG;
  2785. }
  2786. /**
  2787. * @brief Checks whether the TIM interrupt has occurred or not.
  2788. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2789. * @param TIM_IT specifies the TIM interrupt source to check.
  2790. * This parameter can be one of the following values:
  2791. * @arg TIM_INT_UPDATE TIM update Interrupt source
  2792. * @arg TIM_INT_CC1 TIM Capture Compare 1 Interrupt source
  2793. * @arg TIM_INT_CC2 TIM Capture Compare 2 Interrupt source
  2794. * @arg TIM_INT_CC3 TIM Capture Compare 3 Interrupt source
  2795. * @arg TIM_INT_CC4 TIM Capture Compare 4 Interrupt source
  2796. * @arg TIM_INT_COM TIM Commutation Interrupt source
  2797. * @arg TIM_INT_TRIG TIM Trigger Interrupt source
  2798. * @arg TIM_INT_BREAK TIM Break Interrupt source
  2799. * @note
  2800. * - TIM6 and TIM7 can generate only an update interrupt.
  2801. * - TIM_INT_BREAK is used only with TIM1, TIM8.
  2802. * - TIM_INT_COM is used only with TIM1, TIM8.
  2803. * @return The new state of the TIM_IT(SET or RESET).
  2804. */
  2805. INTStatus TIM_GetIntStatus(TIM_Module* TIMx, uint32_t TIM_IT)
  2806. {
  2807. INTStatus bitstatus = RESET;
  2808. uint32_t itstatus = 0x0, itenable = 0x0;
  2809. /* Check the parameters */
  2810. assert_param(IsTimAllModule(TIMx));
  2811. assert_param(IsTimGetInt(TIM_IT));
  2812. itstatus = TIMx->STS & TIM_IT;
  2813. itenable = TIMx->DINTEN & TIM_IT;
  2814. if ((itstatus != (uint32_t)RESET) && (itenable != (uint32_t)RESET))
  2815. {
  2816. bitstatus = SET;
  2817. }
  2818. else
  2819. {
  2820. bitstatus = RESET;
  2821. }
  2822. return bitstatus;
  2823. }
  2824. /**
  2825. * @brief Clears the TIMx's interrupt pending bits.
  2826. * @param TIMx where x can be 1 to 8 to select the TIM peripheral.
  2827. * @param TIM_IT specifies the pending bit to clear.
  2828. * This parameter can be any combination of the following values:
  2829. * @arg TIM_INT_UPDATE TIM1 update Interrupt source
  2830. * @arg TIM_INT_CC1 TIM Capture Compare 1 Interrupt source
  2831. * @arg TIM_INT_CC2 TIM Capture Compare 2 Interrupt source
  2832. * @arg TIM_INT_CC3 TIM Capture Compare 3 Interrupt source
  2833. * @arg TIM_INT_CC4 TIM Capture Compare 4 Interrupt source
  2834. * @arg TIM_INT_COM TIM Commutation Interrupt source
  2835. * @arg TIM_INT_TRIG TIM Trigger Interrupt source
  2836. * @arg TIM_INT_BREAK TIM Break Interrupt source
  2837. * @note
  2838. * - TIM6 and TIM7 can generate only an update interrupt.
  2839. * - TIM_INT_BREAK is used only with TIM1, TIM8.
  2840. * - TIM_INT_COM is used only with TIM1, TIM8.
  2841. */
  2842. void TIM_ClrIntPendingBit(TIM_Module* TIMx, uint32_t TIM_IT)
  2843. {
  2844. /* Check the parameters */
  2845. assert_param(IsTimAllModule(TIMx));
  2846. assert_param(IsTimInt(TIM_IT));
  2847. /* Clear the IT pending Bit */
  2848. TIMx->STS = (uint32_t)~TIM_IT;
  2849. }
  2850. /**
  2851. * @brief Configure the TI1 as Input.
  2852. * @param TIMx where x can be 1 to 8 except 6 and 7 to select the TIM peripheral.
  2853. * @param IcPolarity The Input Polarity.
  2854. * This parameter can be one of the following values:
  2855. * @arg TIM_IC_POLARITY_RISING
  2856. * @arg TIM_IC_POLARITY_FALLING
  2857. * @param IcSelection specifies the input to be used.
  2858. * This parameter can be one of the following values:
  2859. * @arg TIM_IC_SELECTION_DIRECTTI TIM Input 1 is selected to be connected to IC1.
  2860. * @arg TIM_IC_SELECTION_INDIRECTTI TIM Input 1 is selected to be connected to IC2.
  2861. * @arg TIM_IC_SELECTION_TRC TIM Input 1 is selected to be connected to TRC.
  2862. * @param IcFilter Specifies the Input Capture Filter.
  2863. * This parameter must be a value between 0x00 and 0x0F.
  2864. */
  2865. static void ConfigTI1(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter)
  2866. {
  2867. uint16_t tmpccmr1 = 0;
  2868. uint32_t tmpccer = 0;
  2869. /* Disable the Channel 1: Reset the CC1E Bit */
  2870. TIMx->CCEN &= (uint32_t) ~((uint32_t)TIM_CCEN_CC1EN);
  2871. tmpccmr1 = TIMx->CCMOD1;
  2872. tmpccer = TIMx->CCEN;
  2873. /* Select the Input and set the filter */
  2874. tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CCMOD1_CC1SEL)) & ((uint16_t) ~((uint16_t)TIM_CCMOD1_IC1F)));
  2875. tmpccmr1 |= (uint16_t)(IcSelection | (uint16_t)(IcFilter << (uint16_t)4));
  2876. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2877. {
  2878. /* Select the Polarity and set the CC1E Bit */
  2879. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC1P));
  2880. tmpccer |= (uint32_t)(IcPolarity | (uint32_t)TIM_CCEN_CC1EN);
  2881. }
  2882. else
  2883. {
  2884. /* Select the Polarity and set the CC1E Bit */
  2885. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC1P | TIM_CCEN_CC1NP));
  2886. tmpccer |= (uint32_t)(IcPolarity | (uint32_t)TIM_CCEN_CC1EN);
  2887. }
  2888. /* Write to TIMx CCMOD1 and CCEN registers */
  2889. TIMx->CCMOD1 = tmpccmr1;
  2890. TIMx->CCEN = tmpccer;
  2891. }
  2892. /**
  2893. * @brief Configure the TI2 as Input.
  2894. * @param TIMx where x can be 1, 2, 3, 4, 5, 8 to select the TIM peripheral.
  2895. * @param IcPolarity The Input Polarity.
  2896. * This parameter can be one of the following values:
  2897. * @arg TIM_IC_POLARITY_RISING
  2898. * @arg TIM_IC_POLARITY_FALLING
  2899. * @param IcSelection specifies the input to be used.
  2900. * This parameter can be one of the following values:
  2901. * @arg TIM_IC_SELECTION_DIRECTTI TIM Input 2 is selected to be connected to IC2.
  2902. * @arg TIM_IC_SELECTION_INDIRECTTI TIM Input 2 is selected to be connected to IC1.
  2903. * @arg TIM_IC_SELECTION_TRC TIM Input 2 is selected to be connected to TRC.
  2904. * @param IcFilter Specifies the Input Capture Filter.
  2905. * This parameter must be a value between 0x00 and 0x0F.
  2906. */
  2907. static void ConfigTI2(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter)
  2908. {
  2909. uint16_t tmpccmr1 = 0;
  2910. uint32_t tmpccer = 0, tmp = 0;
  2911. /* Disable the Channel 2: Reset the CC2E Bit */
  2912. TIMx->CCEN &= (uint32_t) ~((uint32_t)TIM_CCEN_CC2EN);
  2913. tmpccmr1 = TIMx->CCMOD1;
  2914. tmpccer = TIMx->CCEN;
  2915. tmp = (uint32_t)(IcPolarity << 4);
  2916. /* Select the Input and set the filter */
  2917. tmpccmr1 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CCMOD1_CC2SEL)) & ((uint16_t) ~((uint16_t)TIM_CCMOD1_IC2F)));
  2918. tmpccmr1 |= (uint16_t)(IcFilter << 12);
  2919. tmpccmr1 |= (uint16_t)(IcSelection << 8);
  2920. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2921. {
  2922. /* Select the Polarity and set the CC2E Bit */
  2923. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC2P));
  2924. tmpccer |= (uint32_t)(tmp | (uint32_t)TIM_CCEN_CC2EN);
  2925. }
  2926. else
  2927. {
  2928. /* Select the Polarity and set the CC2E Bit */
  2929. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC2P | TIM_CCEN_CC2NP));
  2930. tmpccer |= (uint32_t)(IcPolarity | (uint32_t)TIM_CCEN_CC2EN);
  2931. }
  2932. /* Write to TIMx CCMOD1 and CCEN registers */
  2933. TIMx->CCMOD1 = tmpccmr1;
  2934. TIMx->CCEN = tmpccer;
  2935. }
  2936. /**
  2937. * @brief Configure the TI3 as Input.
  2938. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2939. * @param IcPolarity The Input Polarity.
  2940. * This parameter can be one of the following values:
  2941. * @arg TIM_IC_POLARITY_RISING
  2942. * @arg TIM_IC_POLARITY_FALLING
  2943. * @param IcSelection specifies the input to be used.
  2944. * This parameter can be one of the following values:
  2945. * @arg TIM_IC_SELECTION_DIRECTTI TIM Input 3 is selected to be connected to IC3.
  2946. * @arg TIM_IC_SELECTION_INDIRECTTI TIM Input 3 is selected to be connected to IC4.
  2947. * @arg TIM_IC_SELECTION_TRC TIM Input 3 is selected to be connected to TRC.
  2948. * @param IcFilter Specifies the Input Capture Filter.
  2949. * This parameter must be a value between 0x00 and 0x0F.
  2950. */
  2951. static void ConfigTI3(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter)
  2952. {
  2953. uint16_t tmpccmr2 = 0;
  2954. uint32_t tmpccer = 0, tmp = 0;
  2955. /* Disable the Channel 3: Reset the CC3E Bit */
  2956. TIMx->CCEN &= (uint32_t) ~((uint32_t)TIM_CCEN_CC3EN);
  2957. tmpccmr2 = TIMx->CCMOD2;
  2958. tmpccer = TIMx->CCEN;
  2959. tmp = (uint32_t)(IcPolarity << 8);
  2960. /* Select the Input and set the filter */
  2961. tmpccmr2 &= (uint16_t)(((uint16_t) ~((uint16_t)TIM_CCMOD2_CC3SEL)) & ((uint16_t) ~((uint16_t)TIM_CCMOD2_IC3F)));
  2962. tmpccmr2 |= (uint16_t)(IcSelection | (uint16_t)(IcFilter << (uint16_t)4));
  2963. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  2964. {
  2965. /* Select the Polarity and set the CC3E Bit */
  2966. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC3P));
  2967. tmpccer |= (uint32_t)(tmp | (uint32_t)TIM_CCEN_CC3EN);
  2968. }
  2969. else
  2970. {
  2971. /* Select the Polarity and set the CC3E Bit */
  2972. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC3P | TIM_CCEN_CC3NP));
  2973. tmpccer |= (uint32_t)(IcPolarity | (uint32_t)TIM_CCEN_CC3EN);
  2974. }
  2975. /* Write to TIMx CCMOD2 and CCEN registers */
  2976. TIMx->CCMOD2 = tmpccmr2;
  2977. TIMx->CCEN = tmpccer;
  2978. }
  2979. /**
  2980. * @brief Configure the TI4 as Input.
  2981. * @param TIMx where x can be 1, 2, 3, 4, 5 or 8 to select the TIM peripheral.
  2982. * @param IcPolarity The Input Polarity.
  2983. * This parameter can be one of the following values:
  2984. * @arg TIM_IC_POLARITY_RISING
  2985. * @arg TIM_IC_POLARITY_FALLING
  2986. * @param IcSelection specifies the input to be used.
  2987. * This parameter can be one of the following values:
  2988. * @arg TIM_IC_SELECTION_DIRECTTI TIM Input 4 is selected to be connected to IC4.
  2989. * @arg TIM_IC_SELECTION_INDIRECTTI TIM Input 4 is selected to be connected to IC3.
  2990. * @arg TIM_IC_SELECTION_TRC TIM Input 4 is selected to be connected to TRC.
  2991. * @param IcFilter Specifies the Input Capture Filter.
  2992. * This parameter must be a value between 0x00 and 0x0F.
  2993. */
  2994. static void ConfigTI4(TIM_Module* TIMx, uint16_t IcPolarity, uint16_t IcSelection, uint16_t IcFilter)
  2995. {
  2996. uint16_t tmpccmr2 = 0;
  2997. uint32_t tmpccer = 0, tmp = 0;
  2998. /* Disable the Channel 4: Reset the CC4E Bit */
  2999. TIMx->CCEN &= (uint32_t) ~((uint32_t)TIM_CCEN_CC4EN);
  3000. tmpccmr2 = TIMx->CCMOD2;
  3001. tmpccer = TIMx->CCEN;
  3002. tmp = (uint32_t)(IcPolarity << 12);
  3003. /* Select the Input and set the filter */
  3004. tmpccmr2 &= (uint16_t)((uint16_t)(~(uint16_t)TIM_CCMOD2_CC4SEL) & ((uint16_t) ~((uint16_t)TIM_CCMOD2_IC4F)));
  3005. tmpccmr2 |= (uint16_t)(IcSelection << 8);
  3006. tmpccmr2 |= (uint16_t)(IcFilter << 12);
  3007. if ((TIMx == TIM1) || (TIMx == TIM8) || (TIMx == TIM2) || (TIMx == TIM3) || (TIMx == TIM4) || (TIMx == TIM5))
  3008. {
  3009. /* Select the Polarity and set the CC4E Bit */
  3010. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC4P));
  3011. tmpccer |= (uint32_t)(tmp | (uint32_t)TIM_CCEN_CC4EN);
  3012. }
  3013. else
  3014. {
  3015. /* Select the Polarity and set the CC4E Bit */
  3016. tmpccer &= (uint32_t) ~((uint32_t)(TIM_CCEN_CC4P));
  3017. tmpccer |= (uint32_t)(IcPolarity | (uint32_t)TIM_CCEN_CC4EN);
  3018. }
  3019. /* Write to TIMx CCMOD2 and CCEN registers */
  3020. TIMx->CCMOD2 = tmpccmr2;
  3021. TIMx->CCEN = tmpccer;
  3022. }
  3023. /**
  3024. * @}
  3025. */
  3026. /**
  3027. * @}
  3028. */
  3029. /**
  3030. * @}
  3031. */