driver_adv_timer.c 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825
  1. #include "driver_adv_timer.h"
  2. /* ---------------------- TIM registers bit mask ------------------------ */
  3. #define SMCR_ETR_MASK ((uint16_t)0x00FF)
  4. #define CCMR_OFFSET ((uint16_t)0x0018)
  5. #define CCER_CCE_SET ((uint16_t)0x0001)
  6. #define CCER_CCNE_SET ((uint16_t)0x0004)
  7. /**
  8. * @}
  9. */
  10. /** @defgroup tim_private_macros
  11. * @{
  12. */
  13. /**
  14. * @}
  15. */
  16. /** @defgroup tim_private_variables
  17. * @{
  18. */
  19. /**
  20. * @}
  21. */
  22. /** @defgroup tim_private_functionprototypes
  23. * @{
  24. */
  25. static void ti1_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection,
  26. uint16_t tim_icfilter);
  27. static void ti2_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection,
  28. uint16_t tim_icfilter);
  29. //static void ti3_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection,
  30. // uint16_t tim_icfilter);
  31. //static void ti4_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection,
  32. // uint16_t tim_icfilter);
  33. /**
  34. * @}
  35. */
  36. /** @defgroup tim_private_macros
  37. * @{
  38. */
  39. /**
  40. * @}
  41. */
  42. /** @defgroup tim_private_variables
  43. * @{
  44. */
  45. /**
  46. * @}
  47. */
  48. /** @defgroup tim_private_functionprototypes
  49. * @{
  50. */
  51. /**
  52. * @}
  53. */
  54. /** @defgroup tim_private_functions
  55. * @{
  56. */
  57. /**
  58. * @brief deinitializes the TIMx peripheral registers to their default reset values.
  59. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  60. * @retval none
  61. */
  62. /**
  63. * @brief initializes the TIMx time base unit peripheral according to
  64. * the specified parameters in the tim_timebaseinitstruct.
  65. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  66. * @param tim_timebaseinitstruct: pointer to a tim_timebaseinittypedef
  67. * structure that contains the configuration information for the
  68. * specified tim peripheral.
  69. * @retval none
  70. */
  71. void tim_timebaseinit(tim_typedef* TIMx, tim_timebaseinittypedef* tim_timebaseinitstruct)
  72. {
  73. uint16_t tmpcr1 = 0;
  74. /* check the parameters */
  75. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  76. assert_param(IS_TIM_COUNTER_MODE(tim_timebaseinitstruct->tim_countermode));
  77. assert_param(IS_TIM_CKD_DIV(tim_timebaseinitstruct->tim_clockdivision));
  78. tmpcr1 = TIMx->cr1;
  79. if(TIMx == TIM1)
  80. {
  81. /* select the counter mode */
  82. tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS)));
  83. tmpcr1 |= (uint32_t)tim_timebaseinitstruct->tim_countermode;
  84. }
  85. // if((TIMx != tim6) && (TIMx != tim7))
  86. // {
  87. // /* set the clock division */
  88. tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CR1_CKD));
  89. tmpcr1 |= (uint32_t)tim_timebaseinitstruct->tim_clockdivision;
  90. // }
  91. TIMx->cr1 = tmpcr1;
  92. /* set the autoreload value */
  93. TIMx->arr = tim_timebaseinitstruct->tim_period ;
  94. /* set the prescaler value */
  95. TIMx->psc = tim_timebaseinitstruct->tim_prescaler;
  96. if (TIMx == TIM1)
  97. {
  98. /* set the repetition counter value */
  99. TIMx->rcr = tim_timebaseinitstruct->tim_repetitioncounter;
  100. }
  101. /* generate an update event to reload the prescaler and the repetition counter
  102. values immediately */
  103. TIMx->egr = TIM_PSCRELOADMODE_IMMEDIATE;
  104. }
  105. /**
  106. * @brief initializes the TIMx channel1 according to the specified
  107. * parameters in the tim_ocinitstruct.
  108. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  109. * @param tim_ocinitstruct: pointer to a tim_ocinittypedef structure
  110. * that contains the configuration information for the specified tim peripheral.
  111. * @retval none
  112. */
  113. void tim_oc1init(tim_typedef* TIMx, tim_ocinittypedef* tim_ocinitstruct)
  114. {
  115. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  116. /* check the parameters */
  117. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  118. assert_param(IS_TIM_OC_MODE(tim_ocinitstruct->tim_ocmode));
  119. assert_param(IS_TIM_OUTPUT_STATE(tim_ocinitstruct->tim_outputstate));
  120. assert_param(IS_TIM_OC_POLARITY(tim_ocinitstruct->tim_ocpolarity));
  121. /* DISABLE the channel 1: reset the cc1e bit */
  122. TIMx->ccer &= (uint16_t)(~(uint16_t)TIM_CCER_CC1E);
  123. /* get the TIMx ccer register value */
  124. tmpccer = TIMx->ccer;
  125. /* get the TIMx cr2 register value */
  126. tmpcr2 = TIMx->cr2;
  127. /* get the TIMx ccmr1 register value */
  128. tmpccmrx = TIMx->ccmr1;
  129. /* reset the output compare mode bits */
  130. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC1M));
  131. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC1S)); // 预留 ■
  132. /* select the output compare mode */
  133. tmpccmrx |= tim_ocinitstruct->tim_ocmode;
  134. /* reset the output polarity level */
  135. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1P));
  136. /* set the output compare polarity */
  137. tmpccer |= tim_ocinitstruct->tim_ocpolarity;
  138. /* set the output state */
  139. tmpccer |= tim_ocinitstruct->tim_outputstate;
  140. if(TIMx == TIM1)
  141. {
  142. assert_param(IS_TIM_OUTPUTN_STATE(tim_ocinitstruct->tim_outputnstate));
  143. assert_param(IS_TIM_OCN_POLARITY(tim_ocinitstruct->tim_ocnpolarity));
  144. assert_param(IS_TIM_OCNIDLE_STATE(tim_ocinitstruct->tim_ocnidlestate));
  145. assert_param(IS_TIM_OCIDLE_STATE(tim_ocinitstruct->tim_ocidlestate));
  146. /* reset the output n polarity level */
  147. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NP));
  148. /* set the output n polarity */
  149. tmpccer |= tim_ocinitstruct->tim_ocnpolarity;
  150. /* reset the output n state */
  151. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NE));
  152. /* set the output n state */
  153. tmpccer |= tim_ocinitstruct->tim_outputnstate;
  154. /* reset the output compare and output compare n idle state */
  155. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS1)); // 预留 ■
  156. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS1N)); // 预留 ■
  157. /* set the output idle state */
  158. tmpcr2 |= tim_ocinitstruct->tim_ocidlestate;
  159. /* set the output n idle state */
  160. tmpcr2 |= tim_ocinitstruct->tim_ocnidlestate;
  161. }
  162. /* write to TIMx cr2 */
  163. TIMx->cr2 = tmpcr2;
  164. /* write to TIMx ccmr1 */
  165. TIMx->ccmr1 = tmpccmrx;
  166. /* set the capture compare register value */
  167. TIMx->ccr1 = tim_ocinitstruct->tim_pulse;
  168. /* write to TIMx ccer */
  169. TIMx->ccer = tmpccer;
  170. }
  171. /**
  172. * @brief initializes the TIMx channel2 according to the specified
  173. * parameters in the tim_ocinitstruct.
  174. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select
  175. * the tim peripheral.
  176. * @param tim_ocinitstruct: pointer to a tim_ocinittypedef structure
  177. * that contains the configuration information for the specified tim peripheral.
  178. * @retval none
  179. */
  180. void tim_oc2init(tim_typedef* TIMx, tim_ocinittypedef* tim_ocinitstruct)
  181. {
  182. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  183. /* check the parameters */
  184. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  185. assert_param(IS_TIM_OC_MODE(tim_ocinitstruct->tim_ocmode));
  186. assert_param(IS_TIM_OUTPUT_STATE(tim_ocinitstruct->tim_outputstate));
  187. assert_param(IS_TIM_OC_POLARITY(tim_ocinitstruct->tim_ocpolarity));
  188. /* DISABLE the channel 2: reset the cc2e bit */
  189. TIMx->ccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2E));
  190. /* get the TIMx ccer register value */
  191. tmpccer = TIMx->ccer;
  192. /* get the TIMx cr2 register value */
  193. tmpcr2 = TIMx->cr2;
  194. /* get the TIMx ccmr1 register value */
  195. tmpccmrx = TIMx->ccmr1;
  196. /* reset the output compare mode and capture/compare selection bits */
  197. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC2M));
  198. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S)); // 预留 ■
  199. /* select the output compare mode */
  200. tmpccmrx |= (uint16_t)(tim_ocinitstruct->tim_ocmode << 8);
  201. /* reset the output polarity level */
  202. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2P));
  203. /* set the output compare polarity */
  204. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_ocpolarity << 4);
  205. /* set the output state */
  206. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_outputstate << 4);
  207. if(TIMx == TIM1)
  208. {
  209. assert_param(IS_TIM_OUTPUTN_STATE(tim_ocinitstruct->tim_outputnstate));
  210. assert_param(IS_TIM_OCN_POLARITY(tim_ocinitstruct->tim_ocnpolarity));
  211. assert_param(IS_TIM_OCNIDLE_STATE(tim_ocinitstruct->tim_ocnidlestate));
  212. assert_param(IS_TIM_OCIDLE_STATE(tim_ocinitstruct->tim_ocidlestate));
  213. /* reset the output n polarity level */
  214. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2NP));
  215. /* set the output n polarity */
  216. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_ocnpolarity << 4);
  217. /* reset the output n state */
  218. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC2NE));
  219. /* set the output n state */
  220. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_outputnstate << 4);
  221. /* reset the output compare and output compare n idle state */
  222. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS2));
  223. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS2N));
  224. /* set the output idle state */
  225. tmpcr2 |= (uint16_t)(tim_ocinitstruct->tim_ocidlestate << 2);
  226. /* set the output n idle state */
  227. tmpcr2 |= (uint16_t)(tim_ocinitstruct->tim_ocnidlestate << 2);
  228. }
  229. /* write to TIMx cr2 */
  230. TIMx->cr2 = tmpcr2;
  231. /* write to TIMx ccmr1 */
  232. TIMx->ccmr1 = tmpccmrx;
  233. /* set the capture compare register value */
  234. TIMx->ccr2 = tim_ocinitstruct->tim_pulse;
  235. /* write to TIMx ccer */
  236. TIMx->ccer = tmpccer;
  237. }
  238. /**
  239. * @brief initializes the TIMx channel3 according to the specified
  240. * parameters in the tim_ocinitstruct.
  241. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  242. * @param tim_ocinitstruct: pointer to a tim_ocinittypedef structure
  243. * that contains the configuration information for the specified tim peripheral.
  244. * @retval none
  245. */
  246. void tim_oc3init(tim_typedef* TIMx, tim_ocinittypedef* tim_ocinitstruct)
  247. {
  248. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  249. /* check the parameters */
  250. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  251. assert_param(IS_TIM_OC_MODE(tim_ocinitstruct->tim_ocmode));
  252. assert_param(IS_TIM_OUTPUT_STATE(tim_ocinitstruct->tim_outputstate));
  253. assert_param(IS_TIM_OC_POLARITY(tim_ocinitstruct->tim_ocpolarity));
  254. /* DISABLE the channel 2: reset the cc2e bit */
  255. TIMx->ccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3E));
  256. /* get the TIMx ccer register value */
  257. tmpccer = TIMx->ccer;
  258. /* get the TIMx cr2 register value */
  259. tmpcr2 = TIMx->cr2;
  260. /* get the TIMx ccmr2 register value */
  261. tmpccmrx = TIMx->ccmr2;
  262. /* reset the output compare mode and capture/compare selection bits */
  263. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_OC3M));
  264. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_CC3S));// 预留 ■
  265. /* select the output compare mode */
  266. tmpccmrx |= tim_ocinitstruct->tim_ocmode;
  267. /* reset the output polarity level */
  268. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3P));
  269. /* set the output compare polarity */
  270. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_ocpolarity << 8);
  271. /* set the output state */
  272. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_outputstate << 8);
  273. if(TIMx == TIM1)
  274. {
  275. assert_param(IS_TIM_OUTPUTN_STATE(tim_ocinitstruct->tim_outputnstate));
  276. assert_param(IS_TIM_OCN_POLARITY(tim_ocinitstruct->tim_ocnpolarity));
  277. assert_param(IS_TIM_OCNIDLE_STATE(tim_ocinitstruct->tim_ocnidlestate));
  278. assert_param(IS_TIM_OCIDLE_STATE(tim_ocinitstruct->tim_ocidlestate));
  279. /* reset the output n polarity level */
  280. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3NP));
  281. /* set the output n polarity */
  282. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_ocnpolarity << 8);
  283. /* reset the output n state */
  284. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC3NE));
  285. /* set the output n state */
  286. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_outputnstate << 8);
  287. /* reset the output compare and output compare n idle state */
  288. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS3));
  289. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS3N));
  290. /* set the output idle state */
  291. tmpcr2 |= (uint16_t)(tim_ocinitstruct->tim_ocidlestate << 4);
  292. /* set the output n idle state */
  293. tmpcr2 |= (uint16_t)(tim_ocinitstruct->tim_ocnidlestate << 4);
  294. }
  295. /* write to TIMx cr2 */
  296. TIMx->cr2 = tmpcr2;
  297. /* write to TIMx ccmr2 */
  298. TIMx->ccmr2 = tmpccmrx;
  299. /* set the capture compare register value */
  300. TIMx->ccr3 = tim_ocinitstruct->tim_pulse;
  301. /* write to TIMx ccer */
  302. TIMx->ccer = tmpccer;
  303. }
  304. /**
  305. * @brief initializes the TIMx channel4 according to the specified
  306. * parameters in the tim_ocinitstruct.
  307. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  308. * @param tim_ocinitstruct: pointer to a tim_ocinittypedef structure
  309. * that contains the configuration information for the specified tim peripheral.
  310. * @retval none
  311. */
  312. void tim_oc4init(tim_typedef* TIMx, tim_ocinittypedef* tim_ocinitstruct)
  313. {
  314. uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
  315. /* check the parameters */
  316. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  317. assert_param(IS_TIM_OC_MODE(tim_ocinitstruct->tim_ocmode));
  318. assert_param(IS_TIM_OUTPUT_STATE(tim_ocinitstruct->tim_outputstate));
  319. assert_param(IS_TIM_OC_POLARITY(tim_ocinitstruct->tim_ocpolarity));
  320. /* DISABLE the channel 2: reset the cc4e bit */
  321. TIMx->ccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC4E));
  322. /* get the TIMx ccer register value */
  323. tmpccer = TIMx->ccer;
  324. /* get the TIMx cr2 register value */
  325. tmpcr2 = TIMx->cr2;
  326. /* get the TIMx ccmr2 register value */
  327. tmpccmrx = TIMx->ccmr2;
  328. /* reset the output compare mode and capture/compare selection bits */
  329. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_OC4M));
  330. tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR2_CC4S)); // 预留 ■
  331. /* select the output compare mode */
  332. tmpccmrx |= (uint16_t)(tim_ocinitstruct->tim_ocmode << 8);
  333. /* reset the output polarity level */
  334. tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC4P));
  335. /* set the output compare polarity */
  336. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_ocpolarity << 12);
  337. /* set the output state */
  338. tmpccer |= (uint16_t)(tim_ocinitstruct->tim_outputstate << 12);
  339. if(TIMx == TIM1)
  340. {
  341. assert_param(IS_TIM_OCIDLE_STATE(tim_ocinitstruct->tim_ocidlestate));
  342. /* reset the output compare idle state */
  343. tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS4));
  344. /* set the output idle state */
  345. tmpcr2 |= (uint16_t)(tim_ocinitstruct->tim_ocidlestate << 6);
  346. }
  347. /* write to TIMx cr2 */
  348. TIMx->cr2 = tmpcr2;
  349. /* write to TIMx ccmr2 */
  350. TIMx->ccmr2 = tmpccmrx;
  351. /* set the capture compare register value */
  352. TIMx->ccr4 = tim_ocinitstruct->tim_pulse;
  353. /* write to TIMx ccer */
  354. TIMx->ccer = tmpccer;
  355. }
  356. /**
  357. * @brief initializes the tim peripheral according to the specified
  358. * parameters in the tim_icinitstruct.
  359. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  360. * @param tim_icinitstruct: pointer to a tim_icinittypedef structure
  361. * that contains the configuration information for the specified tim peripheral.
  362. * @retval none
  363. */
  364. //void tim_icinit(tim_typedef* TIMx, tim_icinittypedef* tim_icinitstruct) // 预留
  365. //{
  366. // /* check the parameters */
  367. // assert_param(IS_TIM_CHANNEL(tim_icinitstruct->tim_channel));
  368. // assert_param(IS_TIM_IC_SELECTION(tim_icinitstruct->tim_icselection));
  369. // assert_param(IS_TIM_IC_PRESCALER(tim_icinitstruct->tim_icprescaler));
  370. // assert_param(IS_TIM_IC_FILTER(tim_icinitstruct->tim_icfilter));
  371. //
  372. // if(TIMx == TIM1)
  373. // {
  374. // assert_param(IS_TIM_IC_POLARITY(tim_icinitstruct->tim_icpolarity));
  375. // }
  376. // else
  377. // {
  378. // assert_param(IS_TIM_IC_POLARITY_LITE(tim_icinitstruct->tim_icpolarity));
  379. // }
  380. // if (tim_icinitstruct->tim_channel == TIM_CHANNEL_1)
  381. // {
  382. // assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  383. // /* ti1 configuration */
  384. // ti1_config(TIMx, tim_icinitstruct->tim_icpolarity,
  385. // tim_icinitstruct->tim_icselection,
  386. // tim_icinitstruct->tim_icfilter);
  387. // /* set the input capture prescaler value */
  388. // tim_setic1prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  389. // }
  390. // else if (tim_icinitstruct->tim_channel == TIM_CHANNEL_2)
  391. // {
  392. // assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  393. // /* ti2 configuration */
  394. // ti2_config(TIMx, tim_icinitstruct->tim_icpolarity,
  395. // tim_icinitstruct->tim_icselection,
  396. // tim_icinitstruct->tim_icfilter);
  397. // /* set the input capture prescaler value */
  398. // tim_setic2prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  399. // }
  400. // else if (tim_icinitstruct->tim_channel == TIM_CHANNEL_3)
  401. // {
  402. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  403. // /* ti3 configuration */
  404. // ti3_config(TIMx, tim_icinitstruct->tim_icpolarity,
  405. // tim_icinitstruct->tim_icselection,
  406. // tim_icinitstruct->tim_icfilter);
  407. // /* set the input capture prescaler value */
  408. // tim_setic3prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  409. // }
  410. // else
  411. // {
  412. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  413. // /* ti4 configuration */
  414. // ti4_config(TIMx, tim_icinitstruct->tim_icpolarity,
  415. // tim_icinitstruct->tim_icselection,
  416. // tim_icinitstruct->tim_icfilter);
  417. // /* set the input capture prescaler value */
  418. // tim_setic4prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  419. // }
  420. //}
  421. /**
  422. * @brief configures the tim peripheral according to the specified
  423. * parameters in the tim_icinitstruct to measure an external pwm signal.
  424. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  425. * @param tim_icinitstruct: pointer to a tim_icinittypedef structure
  426. * that contains the configuration information for the specified tim peripheral.
  427. * @retval none
  428. */
  429. //void tim_pwmiconfig(tim_typedef* TIMx, tim_icinittypedef* tim_icinitstruct) // 预留
  430. //{
  431. // uint16_t icoppositepolarity = TIM_ICPOLARITY_RISING;
  432. // uint16_t icoppositeselection = TIM_ICSELECTION_DIRECTTI;
  433. // /* check the parameters */
  434. // assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  435. // /* select the opposite input polarity */
  436. // if (tim_icinitstruct->tim_icpolarity == TIM_ICPOLARITY_RISING)
  437. // {
  438. // icoppositepolarity = TIM_ICPOLARITY_FALLING;
  439. // }
  440. // else
  441. // {
  442. // icoppositepolarity = TIM_ICPOLARITY_RISING;
  443. // }
  444. // /* select the opposite input */
  445. // if (tim_icinitstruct->tim_icselection == TIM_ICSELECTION_DIRECTTI)
  446. // {
  447. // icoppositeselection = TIM_ICSELECTION_INDIRECTTI;
  448. // }
  449. // else
  450. // {
  451. // icoppositeselection = TIM_ICSELECTION_DIRECTTI;
  452. // }
  453. // if (tim_icinitstruct->tim_channel == TIM_CHANNEL_1)
  454. // {
  455. // /* ti1 configuration */
  456. // ti1_config(TIMx, tim_icinitstruct->tim_icpolarity, tim_icinitstruct->tim_icselection,
  457. // tim_icinitstruct->tim_icfilter);
  458. // /* set the input capture prescaler value */
  459. // tim_setic1prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  460. // /* ti2 configuration */
  461. // ti2_config(TIMx, icoppositepolarity, icoppositeselection, tim_icinitstruct->tim_icfilter);
  462. // /* set the input capture prescaler value */
  463. // tim_setic2prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  464. // }
  465. // else
  466. // {
  467. // /* ti2 configuration */
  468. // ti2_config(TIMx, tim_icinitstruct->tim_icpolarity, tim_icinitstruct->tim_icselection,
  469. // tim_icinitstruct->tim_icfilter);
  470. // /* set the input capture prescaler value */
  471. // tim_setic2prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  472. // /* ti1 configuration */
  473. // ti1_config(TIMx, icoppositepolarity, icoppositeselection, tim_icinitstruct->tim_icfilter);
  474. // /* set the input capture prescaler value */
  475. // tim_setic1prescaler(TIMx, tim_icinitstruct->tim_icprescaler);
  476. // }
  477. //}
  478. /**
  479. * @brief configures the: break feature, dead time, lock level, the ossi,
  480. * the ossr state and the aoe(automatic output enable).
  481. * @param TIMx: where x can be 1 or 8 to select the tim
  482. * @param tim_bdtrinitstruct: pointer to a tim_bdtrinittypedef structure that
  483. * contains the bdtr register configuration information for the tim peripheral.
  484. * @retval none
  485. */
  486. void tim_bdtrconfig(tim_typedef* TIMx, tim_bdtrinittypedef *tim_bdtrinitstruct)
  487. {
  488. /* check the parameters */
  489. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  490. assert_param(IS_TIM_OSSR_STATE(tim_bdtrinitstruct->tim_ossrstate));
  491. assert_param(IS_TIM_OSSI_STATE(tim_bdtrinitstruct->tim_ossistate));
  492. assert_param(IS_TIM_LOCK_LEVEL(tim_bdtrinitstruct->tim_locklevel));
  493. assert_param(IS_TIM_BREAK_STATE(tim_bdtrinitstruct->tim_break));
  494. assert_param(IS_TIM_BREAK_POLARITY(tim_bdtrinitstruct->tim_breakpolarity));
  495. assert_param(IS_TIM_AUTOMATIC_OUTPUT_STATE(tim_bdtrinitstruct->tim_automaticoutput));
  496. /* set the lock level, the break enable bit and the ploarity, the ossr state,
  497. the ossi state, the dead time value and the automatic output enable bit */
  498. TIMx->bdtr = (uint32_t)tim_bdtrinitstruct->tim_ossrstate | tim_bdtrinitstruct->tim_ossistate |
  499. tim_bdtrinitstruct->tim_locklevel | tim_bdtrinitstruct->tim_deadtime |
  500. tim_bdtrinitstruct->tim_break | tim_bdtrinitstruct->tim_breakpolarity |
  501. tim_bdtrinitstruct->tim_automaticoutput;
  502. }
  503. /**
  504. * @brief fills each tim_timebaseinitstruct member with its default value.
  505. * @param tim_timebaseinitstruct : pointer to a tim_timebaseinittypedef
  506. * structure which will be initialized.
  507. * @retval none
  508. */
  509. void tim_timebasestructinit(tim_timebaseinittypedef* tim_timebaseinitstruct)
  510. {
  511. /* set the default configuration */
  512. tim_timebaseinitstruct->tim_period = 0xffff;
  513. tim_timebaseinitstruct->tim_prescaler = 0x0000;
  514. tim_timebaseinitstruct->tim_clockdivision = TIM_CKD_DIV1;
  515. tim_timebaseinitstruct->tim_countermode = TIM_COUNTERMODE_UP;
  516. tim_timebaseinitstruct->tim_repetitioncounter = 0x0000;
  517. }
  518. /**
  519. * @brief fills each tim_ocinitstruct member with its default value.
  520. * @param tim_ocinitstruct : pointer to a tim_ocinittypedef structure which will
  521. * be initialized.
  522. * @retval none
  523. */
  524. void tim_ocstructinit(tim_ocinittypedef* tim_ocinitstruct)
  525. {
  526. /* set the default configuration */
  527. tim_ocinitstruct->tim_ocmode = TIM_OCMODE_TIMING;
  528. tim_ocinitstruct->tim_outputstate = TIM_OUTPUTSTATE_DISABLE;
  529. tim_ocinitstruct->tim_outputnstate = TIM_OUTPUTNSTATE_DISABLE;
  530. tim_ocinitstruct->tim_pulse = 0x0000;
  531. tim_ocinitstruct->tim_ocpolarity = TIM_OCPOLARITY_HIGH;
  532. tim_ocinitstruct->tim_ocnpolarity = TIM_OCNPOLARITY_HIGH;
  533. tim_ocinitstruct->tim_ocidlestate = TIM_OCIDLESTATE_RESET;
  534. tim_ocinitstruct->tim_ocnidlestate = TIM_OCNIDLESTATE_RESET;
  535. }
  536. /**
  537. * @brief fills each tim_icinitstruct member with its default value.
  538. * @param tim_icinitstruct: pointer to a tim_icinittypedef structure which will
  539. * be initialized.
  540. * @retval none
  541. */
  542. //void tim_icstructinit(tim_icinittypedef* tim_icinitstruct) // 预留
  543. //{
  544. // /* set the default configuration */
  545. // tim_icinitstruct->tim_channel = TIM_CHANNEL_1;
  546. // tim_icinitstruct->tim_icpolarity = TIM_ICPOLARITY_RISING;
  547. // tim_icinitstruct->tim_icselection = TIM_ICSELECTION_DIRECTTI;
  548. // tim_icinitstruct->tim_icprescaler = TIM_ICPSC_DIV1;
  549. // tim_icinitstruct->tim_icfilter = 0x00;
  550. //}
  551. /**
  552. * @brief fills each tim_bdtrinitstruct member with its default value.
  553. * @param tim_bdtrinitstruct: pointer to a tim_bdtrinittypedef structure which
  554. * will be initialized.
  555. * @retval none
  556. */
  557. void tim_bdtrstructinit(tim_bdtrinittypedef* tim_bdtrinitstruct)
  558. {
  559. /* set the default configuration */
  560. tim_bdtrinitstruct->tim_ossrstate = TIM_OSSRSTATE_DISABLE;
  561. tim_bdtrinitstruct->tim_ossistate = TIM_OSSISTATE_DISABLE;
  562. tim_bdtrinitstruct->tim_locklevel = TIM_LOCKLEVEL_OFF;
  563. tim_bdtrinitstruct->tim_deadtime = 0x00;
  564. tim_bdtrinitstruct->tim_break = TIM_BREAK_DISABLE;
  565. tim_bdtrinitstruct->tim_breakpolarity = TIM_BREAKPOLARITY_LOW;
  566. tim_bdtrinitstruct->tim_automaticoutput = TIM_AUTOMATICOUTPUT_DISABLE;
  567. }
  568. /**
  569. * @brief enables or DISABLEs the specified tim peripheral.
  570. * @param TIMx: where x can be 1 to 17 to select the TIMx peripheral.
  571. * @param newstate: new state of the TIMx peripheral.
  572. * this parameter can be: enable or DISABLE.
  573. * @retval none
  574. */
  575. AT(.com_text.tim.cmd)
  576. void tim_cmd(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  577. {
  578. /* check the parameters */
  579. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  580. assert_param(IS_FUNCTIONAL_STATE(newstate));
  581. if (newstate != DISABLE)
  582. {
  583. /* enable the tim counter */
  584. TIMx->cr1 |= TIM_CR1_CEN;
  585. }
  586. else
  587. {
  588. /* DISABLE the tim counter */
  589. TIMx->cr1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN));
  590. }
  591. }
  592. /**
  593. * @brief enables or DISABLEs the tim peripheral main outputs.
  594. * @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the TIMx peripheral.
  595. * @param newstate: new state of the tim peripheral main outputs.
  596. * this parameter can be: enable or DISABLE.
  597. * @retval none
  598. */
  599. AT(.com_text.tim.ctrlpwmoutputs)
  600. void tim_ctrlpwmoutputs(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  601. {
  602. /* check the parameters */
  603. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  604. assert_param(IS_FUNCTIONAL_STATE(newstate));
  605. if (newstate != DISABLE)
  606. {
  607. /* enable the tim main output */
  608. TIMx->bdtr |= TIM_BDTR_MOE;
  609. }
  610. else
  611. {
  612. /* DISABLE the tim main output */
  613. TIMx->bdtr &= (uint32_t)(~((uint32_t)TIM_BDTR_MOE));
  614. }
  615. }
  616. /**
  617. * @brief enables or DISABLEs the specified tim interrupts.
  618. * @param TIMx: where x can be 1 to 17 to select the TIMx peripheral.
  619. * @param tim_it: specifies the tim interrupts sources to be enabled or DISABLEd.
  620. * this parameter can be any combination of the following values:
  621. * @arg tim_it_update: tim update interrupt source
  622. * @arg tim_it_cc1: tim capture compare 1 interrupt source
  623. * @arg tim_it_cc2: tim capture compare 2 interrupt source
  624. * @arg tim_it_cc3: tim capture compare 3 interrupt source
  625. * @arg tim_it_cc4: tim capture compare 4 interrupt source
  626. * @arg tim_it_com: tim commutation interrupt source
  627. * @arg tim_it_trigger: tim trigger interrupt source
  628. * @arg tim_it_break: tim break interrupt source
  629. * @note
  630. * - tim6 and tim7 can only generate an update interrupt.
  631. * - tim9, TIM12 and TIM15 can have only tim_it_update, tim_it_cc1,
  632. * tim_it_cc2 or tim_it_trigger.
  633. * - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have tim_it_update or tim_it_cc1.
  634. * - tim_it_break is used only with TIM1, tim8 and TIM15.
  635. * - tim_it_com is used only with TIM1, tim8, TIM15, TIM16 and TIM17.
  636. * @param newstate: new state of the tim interrupts.
  637. * this parameter can be: enable or DISABLE.
  638. * @retval none
  639. */
  640. void tim_itconfig(tim_typedef* TIMx, uint16_t tim_it, FUNCTIONAL_STATE newstate)
  641. {
  642. /* check the parameters */
  643. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  644. assert_param(IS_TIM_IT(tim_it));
  645. assert_param(IS_FUNCTIONAL_STATE(newstate));
  646. if (newstate != DISABLE)
  647. {
  648. /* enable the interrupt sources */
  649. TIMx->dier |= tim_it;
  650. }
  651. else
  652. {
  653. /* DISABLE the interrupt sources */
  654. TIMx->dier &= (uint16_t)~tim_it;
  655. }
  656. }
  657. /**
  658. * @brief configures the TIMx event to be generate by software.
  659. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  660. * @param tim_eventsource: specifies the event source.
  661. * this parameter can be one or more of the following values:
  662. * @arg tim_eventsource_update: timer update event source
  663. * @arg tim_eventsource_cc1: timer capture compare 1 event source
  664. * @arg tim_eventsource_cc2: timer capture compare 2 event source
  665. * @arg tim_eventsource_cc3: timer capture compare 3 event source
  666. * @arg tim_eventsource_cc4: timer capture compare 4 event source
  667. * @arg tim_eventsource_com: timer com event source
  668. * @arg tim_eventsource_trigger: timer trigger event source
  669. * @arg tim_eventsource_break: timer break event source
  670. * @note
  671. * - tim6 and tim7 can only generate an update event.
  672. * - tim_eventsource_com and tim_eventsource_break are used only with TIM1 and tim8.
  673. * @retval none
  674. */
  675. AT(.com_periph.tim.generateevent)
  676. void tim_generateevent(tim_typedef* TIMx, uint16_t tim_eventsource)
  677. {
  678. /* check the parameters */
  679. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  680. assert_param(IS_TIM_EVENT_SOURCE(tim_eventsource));
  681. /* set the event sources */
  682. TIMx->egr = tim_eventsource;
  683. }
  684. ///**
  685. // * @brief configures the TIMx's dma interface.
  686. // * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 15, 16 or 17 to select
  687. // * the tim peripheral.
  688. // * @param tim_dmabase: dma base address.
  689. // * this parameter can be one of the following values:
  690. // * @arg tim_dmabase_cr, tim_dmabase_CR2, tim_dmabase_smcr,
  691. // * tim_dmabase_dier, TIM1_dmabase_sr, tim_dmabase_egr,
  692. // * tim_dmabase_CCMR1, tim_dmabase_ccmr2, tim_dmabase_CCER,
  693. // * tim_dmabase_cnt, tim_dmabase_psc, tim_dmabase_arr,
  694. // * tim_dmabase_rcr, tim_dmabase_CCR1, tim_dmabase_CCR2,
  695. // * tim_dmabase_ccr3, tim_dmabase_ccr4, tim_dmabase_bdtr,
  696. // * tim_dmabase_dcr.
  697. // * @param tim_dmaburstlength: dma burst length.
  698. // * this parameter can be one value between:
  699. // * tim_dmaburstlength_1transfer and tim_dmaburstlength_18transfers.
  700. // * @retval none
  701. // */
  702. //void tim_dmaconfig(tim_typedef* TIMx, uint16_t tim_dmabase, uint16_t tim_dmaburstlength)
  703. //{
  704. // /* check the parameters */
  705. // assert_param(is_tim_list4_periph(TIMx));
  706. // assert_param(is_tim_dma_base(tim_dmabase));
  707. // assert_param(is_tim_dma_length(tim_dmaburstlength));
  708. // /* set the dma base and the dma burst length */
  709. // TIMx->dcr = tim_dmabase | tim_dmaburstlength;
  710. //}
  711. /**
  712. * @brief enables or DISABLEs the TIMx's dma requests.
  713. * @param TIMx: where x can be 1, 2, 3, 4, 5, 6, 7, 8, 15, 16 or 17
  714. * to select the tim peripheral.
  715. * @param tim_dmasource: specifies the dma request sources.
  716. * this parameter can be any combination of the following values:
  717. * @arg tim_dma_update: tim update interrupt source
  718. * @arg tim_dma_cc1: tim capture compare 1 dma source
  719. * @arg tim_dma_cc2: tim capture compare 2 dma source
  720. * @arg tim_dma_cc3: tim capture compare 3 dma source
  721. * @arg tim_dma_cc4: tim capture compare 4 dma source
  722. * @arg tim_dma_com: tim commutation dma source
  723. * @arg tim_dma_trigger: tim trigger dma source
  724. * @param newstate: new state of the dma request sources.
  725. * this parameter can be: enable or DISABLE.
  726. * @retval none
  727. */
  728. //void tim_dmacmd(tim_typedef* TIMx, uint16_t tim_dmasource, FUNCTIONAL_STATE newstate)
  729. //{
  730. // /* check the parameters */
  731. // assert_param(IS_TIM_LIST9_PERIPH(TIMx));
  732. // assert_param(IS_TIM_DMA_SOURCE(tim_dmasource));
  733. // assert_param(IS_FUNCTIONAL_STATE(newstate));
  734. //
  735. // if (newstate != DISABLE)
  736. // {
  737. // /* enable the dma sources */
  738. // TIMx->dier |= tim_dmasource;
  739. // }
  740. // else
  741. // {
  742. // /* DISABLE the dma sources */
  743. // TIMx->dier &= (uint16_t)~tim_dmasource;
  744. // }
  745. //}
  746. /**
  747. * @brief configures the TIMx internal clock
  748. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15
  749. * to select the tim peripheral.
  750. * @retval none
  751. */
  752. AT(.com_periph.tim.internalclock)
  753. void tim_internalclockconfig(tim_typedef* TIMx)
  754. {
  755. /* check the parameters */
  756. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  757. /* DISABLE slave mode to clock the prescaler directly with the internal clock */
  758. TIMx->smcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  759. }
  760. /**
  761. * @brief configures the TIMx internal trigger as external clock
  762. * @param TIMx: where x can be 1, 2, 3, 4, 5, 9, 12 or 15 to select the tim peripheral.
  763. * @param tim_itrsource: trigger source.
  764. * this parameter can be one of the following values:
  765. * @param tim_ts_itr0: internal trigger 0
  766. * @param tim_ts_itr1: internal trigger 1
  767. * @param tim_ts_itr2: internal trigger 2
  768. * @param tim_ts_itr3: internal trigger 3
  769. * @retval none
  770. */
  771. AT(.com_periph.tim.itrxexternalclock)
  772. void tim_itrxexternalclockconfig(tim_typedef* TIMx, uint16_t tim_inputtriggersource)
  773. {
  774. /* check the parameters */
  775. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  776. assert_param(IS_TIM_INTERNAL_TRIGGER_SELECTION(tim_inputtriggersource));
  777. /* select the internal trigger */
  778. tim_selectinputtrigger(TIMx, tim_inputtriggersource);
  779. /* select the external clock mode1 */
  780. TIMx->smcr |= TIM_SLAVEMODE_EXTERNAL1;
  781. }
  782. /**
  783. * @brief configures the TIMx trigger as external clock
  784. * @param TIMx: where x can be 1, 2, 3, 4, 5, 9, 12 or 15 to select the tim peripheral.
  785. * @param tim_tixexternalclksource: trigger source.
  786. * this parameter can be one of the following values:
  787. * @arg tim_tixexternalclk1source_ti1ed: ti1 edge detector
  788. * @arg tim_tixexternalclk1source_ti1: filtered timer input 1
  789. * @arg TIM_TIXEXTERNALCLK1SOURCE_TI2: filtered timer input 2
  790. * @param tim_icpolarity: specifies the tix polarity.
  791. * this parameter can be one of the following values:
  792. * @arg TIM_ICPOLARITY_RISING
  793. * @arg TIM_ICPOLARITY_FALLING
  794. * @param icfilter : specifies the filter value.
  795. * this parameter must be a value between 0x0 and 0xf.
  796. * @retval none
  797. */
  798. AT(.com_periph.tim.tixexternalclock)
  799. void tim_tixexternalclockconfig(tim_typedef* TIMx, uint16_t tim_tixexternalclksource,
  800. uint16_t tim_icpolarity, uint16_t icfilter)
  801. {
  802. /* check the parameters */
  803. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  804. assert_param(IS_TIM_TIXCLK_SOURCE(tim_tixexternalclksource));
  805. assert_param(IS_TIM_IC_POLARITY(tim_icpolarity));
  806. assert_param(IS_TIM_IC_FILTER(icfilter));
  807. /* configure the timer input clock source */
  808. if (tim_tixexternalclksource == TIM_TIXEXTERNALCLK1SOURCE_TI2)
  809. {
  810. ti2_config(TIMx, tim_icpolarity, TIM_ICSELECTION_DIRECTTI, icfilter);
  811. }
  812. else
  813. {
  814. ti1_config(TIMx, tim_icpolarity, TIM_ICSELECTION_DIRECTTI, icfilter);
  815. }
  816. /* select the trigger source */
  817. tim_selectinputtrigger(TIMx, tim_tixexternalclksource);
  818. /* select the external clock mode1 */
  819. TIMx->smcr |= TIM_SLAVEMODE_EXTERNAL1;
  820. }
  821. /**
  822. * @brief configures the external clock mode1
  823. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  824. * @param tim_exttrgprescaler: the external trigger prescaler.
  825. * this parameter can be one of the following values:
  826. * @arg tim_exttrgpsc_off: etrp prescaler off.
  827. * @arg tim_exttrgpsc_div2: etrp frequency divided by 2.
  828. * @arg tim_exttrgpsc_div4: etrp frequency divided by 4.
  829. * @arg tim_exttrgpsc_div8: etrp frequency divided by 8.
  830. * @param tim_exttrgpolarity: the external trigger polarity.
  831. * this parameter can be one of the following values:
  832. * @arg tim_exttrgpolarity_inverted: active low or falling edge active.
  833. * @arg tim_exttrgpolarity_noninverted: active high or rising edge active.
  834. * @param exttrgfilter: external trigger filter.
  835. * this parameter must be a value between 0x00 and 0x0f
  836. * @retval none
  837. */
  838. AT(.com_periph.etrclockmode1)
  839. void tim_etrclockmode1config(tim_typedef* TIMx, uint16_t tim_exttrgprescaler, uint16_t tim_exttrgpolarity,
  840. uint16_t exttrgfilter)
  841. {
  842. uint16_t tmpsmcr = 0;
  843. /* check the parameters */
  844. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  845. assert_param(IS_TIM_EXT_PRESCALER(tim_exttrgprescaler));
  846. assert_param(IS_TIM_EXT_POLARITY(tim_exttrgpolarity));
  847. assert_param(IS_TIM_EXT_FILTER(exttrgfilter));
  848. /* configure the etr clock source */
  849. tim_etrconfig(TIMx, tim_exttrgprescaler, tim_exttrgpolarity, exttrgfilter);
  850. /* get the TIMx smcr register value */
  851. tmpsmcr = TIMx->smcr;
  852. /* reset the sms bits */
  853. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  854. /* select the external clock mode1 */
  855. tmpsmcr |= TIM_SLAVEMODE_EXTERNAL1;
  856. /* select the trigger selection : etrf */
  857. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_TS));
  858. tmpsmcr |= TIM_TS_ETRF;
  859. /* write to TIMx smcr */
  860. TIMx->smcr = tmpsmcr;
  861. }
  862. /**
  863. * @brief configures the external clock mode2
  864. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  865. * @param tim_exttrgprescaler: the external trigger prescaler.
  866. * this parameter can be one of the following values:
  867. * @arg tim_exttrgpsc_off: etrp prescaler off.
  868. * @arg tim_exttrgpsc_div2: etrp frequency divided by 2.
  869. * @arg tim_exttrgpsc_div4: etrp frequency divided by 4.
  870. * @arg tim_exttrgpsc_div8: etrp frequency divided by 8.
  871. * @param tim_exttrgpolarity: the external trigger polarity.
  872. * this parameter can be one of the following values:
  873. * @arg tim_exttrgpolarity_inverted: active low or falling edge active.
  874. * @arg tim_exttrgpolarity_noninverted: active high or rising edge active.
  875. * @param exttrgfilter: external trigger filter.
  876. * this parameter must be a value between 0x00 and 0x0f
  877. * @retval none
  878. */
  879. AT(.com_periph.tim.etrclockmode2)
  880. void tim_etrclockmode2config(tim_typedef* TIMx, uint16_t tim_exttrgprescaler,
  881. uint16_t tim_exttrgpolarity, uint16_t exttrgfilter)
  882. {
  883. /* check the parameters */
  884. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  885. assert_param(IS_TIM_EXT_PRESCALER(tim_exttrgprescaler));
  886. assert_param(IS_TIM_EXT_POLARITY(tim_exttrgpolarity));
  887. assert_param(IS_TIM_EXT_FILTER(exttrgfilter));
  888. /* configure the etr clock source */
  889. tim_etrconfig(TIMx, tim_exttrgprescaler, tim_exttrgpolarity, exttrgfilter);
  890. /* enable the external clock mode2 */
  891. TIMx->smcr |= TIM_SMCR_ECE;
  892. }
  893. /**
  894. * @brief configures the TIMx external trigger (etr).
  895. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  896. * @param tim_exttrgprescaler: the external trigger prescaler.
  897. * this parameter can be one of the following values:
  898. * @arg tim_exttrgpsc_off: etrp prescaler off.
  899. * @arg tim_exttrgpsc_div2: etrp frequency divided by 2.
  900. * @arg tim_exttrgpsc_div4: etrp frequency divided by 4.
  901. * @arg tim_exttrgpsc_div8: etrp frequency divided by 8.
  902. * @param tim_exttrgpolarity: the external trigger polarity.
  903. * this parameter can be one of the following values:
  904. * @arg tim_exttrgpolarity_inverted: active low or falling edge active.
  905. * @arg tim_exttrgpolarity_noninverted: active high or rising edge active.
  906. * @param exttrgfilter: external trigger filter.
  907. * this parameter must be a value between 0x00 and 0x0f
  908. * @retval none
  909. */
  910. AT(.com_periph.tim.etrconfig)
  911. void tim_etrconfig(tim_typedef* TIMx, uint16_t tim_exttrgprescaler, uint16_t tim_exttrgpolarity,
  912. uint16_t exttrgfilter)
  913. {
  914. uint16_t tmpsmcr = 0;
  915. /* check the parameters */
  916. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  917. assert_param(IS_TIM_EXT_PRESCALER(tim_exttrgprescaler));
  918. assert_param(IS_TIM_EXT_POLARITY(tim_exttrgpolarity));
  919. assert_param(IS_TIM_EXT_FILTER(exttrgfilter));
  920. tmpsmcr = TIMx->smcr;
  921. /* reset the etr bits */
  922. tmpsmcr &= SMCR_ETR_MASK;
  923. /* set the PRESCALER, the filter value and the polarity */
  924. tmpsmcr |= (uint16_t)(tim_exttrgprescaler | (uint16_t)(tim_exttrgpolarity | (uint16_t)(exttrgfilter << (uint16_t)8)));
  925. /* write to TIMx smcr */
  926. TIMx->smcr = tmpsmcr;
  927. }
  928. /**
  929. * @brief configures the TIMx prescaler.
  930. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  931. * @param prescaler: specifies the prescaler register value
  932. * @param tim_pscreloadmode: specifies the tim prescaler reload mode
  933. * this parameter can be one of the following values:
  934. * @arg tim_pscreloadmode_update: the prescaler is loaded at the update event.
  935. * @arg TIM_PSCRELOADMODE_IMMEDIATE: the prescaler is loaded immediately.
  936. * @retval none
  937. */
  938. AT(.com_periph.tim.prescaler)
  939. void tim_prescalerconfig(tim_typedef* TIMx, uint16_t prescaler, uint16_t tim_pscreloadmode)
  940. {
  941. /* check the parameters */
  942. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  943. assert_param(IS_TIM_PRESCALER_RELOAD(tim_pscreloadmode));
  944. /* set the prescaler value */
  945. TIMx->psc = prescaler;
  946. /* set or reset the ug bit */
  947. TIMx->egr = tim_pscreloadmode;
  948. }
  949. /**
  950. * @brief specifies the TIMx counter mode to be used.
  951. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  952. * @param tim_countermode: specifies the counter mode to be used
  953. * this parameter can be one of the following values:
  954. * @arg TIM_COUNTERMODE_UP: tim up counting mode
  955. * @arg tim_countermode_down: tim down counting mode
  956. * @arg tim_countermode_centeraligned1: tim center aligned mode1
  957. * @arg tim_countermode_centeraligned2: tim center aligned mode2
  958. * @arg tim_countermode_centeraligned3: tim center aligned mode3
  959. * @retval none
  960. */
  961. AT(.com_periph.tim.countermode)
  962. void tim_countermodeconfig(tim_typedef* TIMx, uint16_t tim_countermode)
  963. {
  964. uint16_t tmpcr1 = 0;
  965. /* check the parameters */
  966. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  967. assert_param(IS_TIM_COUNTER_MODE(tim_countermode));
  968. tmpcr1 = TIMx->cr1;
  969. /* reset the cms and dir bits */
  970. tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS)));
  971. /* set the counter mode */
  972. tmpcr1 |= tim_countermode;
  973. /* write to TIMx cr1 register */
  974. TIMx->cr1 = tmpcr1;
  975. }
  976. /**
  977. * @brief selects the input trigger source
  978. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  979. * @param tim_inputtriggersource: the input trigger source.
  980. * this parameter can be one of the following values:
  981. * @arg tim_ts_itr0: internal trigger 0
  982. * @arg tim_ts_itr1: internal trigger 1
  983. * @arg tim_ts_itr2: internal trigger 2
  984. * @arg tim_ts_itr3: internal trigger 3
  985. * @arg tim_ts_ti1f_ed: ti1 edge detector
  986. * @arg tim_ts_ti1fp1: filtered timer input 1
  987. * @arg tim_ts_ti2fp2: filtered timer input 2
  988. * @arg TIM_TS_ETRF: external trigger input
  989. * @retval none
  990. */
  991. AT(.com_periph.tim.selectinputtrigger)
  992. void tim_selectinputtrigger(tim_typedef* TIMx, uint16_t tim_inputtriggersource)
  993. {
  994. uint16_t tmpsmcr = 0;
  995. /* check the parameters */
  996. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  997. assert_param(IS_TIM_TRIGGER_SELECTION(tim_inputtriggersource));
  998. /* get the TIMx smcr register value */
  999. tmpsmcr = TIMx->smcr;
  1000. /* reset the ts bits */
  1001. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_TS));
  1002. /* set the input trigger source */
  1003. tmpsmcr |= tim_inputtriggersource;
  1004. /* write to TIMx smcr */
  1005. TIMx->smcr = tmpsmcr;
  1006. }
  1007. /**
  1008. * @brief configures the TIMx encoder interface.
  1009. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1010. * @param tim_encodermode: specifies the TIMx encoder mode.
  1011. * this parameter can be one of the following values:
  1012. * @arg tim_encodermode_ti1: counter counts on ti1fp1 edge depending on ti2fp2 level.
  1013. * @arg tim_encodermode_ti2: counter counts on ti2fp2 edge depending on ti1fp1 level.
  1014. * @arg tim_encodermode_ti12: counter counts on both ti1fp1 and ti2fp2 edges depending
  1015. * on the level of the other input.
  1016. * @param tim_ic1polarity: specifies the ic1 polarity
  1017. * this parameter can be one of the following values:
  1018. * @arg TIM_ICPOLARITY_FALLING: ic falling edge.
  1019. * @arg TIM_ICPOLARITY_RISING: ic rising edge.
  1020. * @param tim_ic2polarity: specifies the ic2 polarity
  1021. * this parameter can be one of the following values:
  1022. * @arg TIM_ICPOLARITY_FALLING: ic falling edge.
  1023. * @arg TIM_ICPOLARITY_RISING: ic rising edge.
  1024. * @retval none
  1025. */
  1026. void tim_encoderinterfaceconfig(tim_typedef* TIMx, uint16_t tim_encodermode,
  1027. uint16_t tim_ic1polarity, uint16_t tim_ic2polarity)
  1028. {
  1029. uint16_t tmpsmcr = 0;
  1030. uint16_t tmpccmr1 = 0;
  1031. uint16_t tmpccer = 0;
  1032. /* check the parameters */
  1033. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  1034. assert_param(IS_TIM_ENCODER_MODE(tim_encodermode));
  1035. assert_param(IS_TIM_IC_POLARITY(tim_ic1polarity));
  1036. assert_param(IS_TIM_IC_POLARITY(tim_ic2polarity));
  1037. /* get the TIMx smcr register value */
  1038. tmpsmcr = TIMx->smcr;
  1039. /* get the TIMx ccmr1 register value */
  1040. tmpccmr1 = TIMx->ccmr1;
  1041. /* get the TIMx ccer register value */
  1042. tmpccer = TIMx->ccer;
  1043. /* set the encoder mode */
  1044. tmpsmcr &= (uint16_t)(~((uint16_t)TIM_SMCR_SMS));
  1045. tmpsmcr |= tim_encodermode;
  1046. /* select the capture compare 1 and the capture compare 2 as input */
  1047. tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC1S)) & (uint16_t)(~((uint16_t)TIM_CCMR1_CC2S))); // 预留 ■
  1048. tmpccmr1 |= TIM_CCMR1_CC1S_0 | TIM_CCMR1_CC2S_0; // 预留 ■
  1049. /* set the ti1 and the ti2 polarities */
  1050. tmpccer &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCER_CC1P)) & ((uint16_t)~((uint16_t)TIM_CCER_CC2P)));
  1051. tmpccer |= (uint16_t)(tim_ic1polarity | (uint16_t)(tim_ic2polarity << (uint16_t)4));
  1052. /* write to TIMx smcr */
  1053. TIMx->smcr = tmpsmcr;
  1054. /* write to TIMx ccmr1 */
  1055. TIMx->ccmr1 = tmpccmr1;
  1056. /* write to TIMx ccer */
  1057. TIMx->ccer = tmpccer;
  1058. }
  1059. /**
  1060. * @brief forces the TIMx output 1 waveform to active or inactive level.
  1061. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  1062. * @param tim_forcedaction: specifies the forced action to be set to the output waveform.
  1063. * this parameter can be one of the following values:
  1064. * @arg tim_forcedaction_active: force active level on oc1ref
  1065. * @arg tim_forcedaction_inactive: force inactive level on oc1ref.
  1066. * @retval none
  1067. */
  1068. AT(.com_periph.tim.forcedoc1)
  1069. void tim_forcedoc1config(tim_typedef* TIMx, uint16_t tim_forcedaction)
  1070. {
  1071. uint16_t tmpccmr1 = 0;
  1072. /* check the parameters */
  1073. assert_param(is_tim_list8_periph(TIMx));
  1074. assert_param(IS_TIM_FORCED_ACTION(tim_forcedaction));
  1075. tmpccmr1 = TIMx->ccmr1;
  1076. /* reset the oc1m bits */
  1077. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1M);
  1078. /* configure the forced output mode */
  1079. tmpccmr1 |= tim_forcedaction;
  1080. /* write to TIMx ccmr1 register */
  1081. TIMx->ccmr1 = tmpccmr1;
  1082. }
  1083. /**
  1084. * @brief forces the TIMx output 2 waveform to active or inactive level.
  1085. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  1086. * @param tim_forcedaction: specifies the forced action to be set to the output waveform.
  1087. * this parameter can be one of the following values:
  1088. * @arg tim_forcedaction_active: force active level on oc2ref
  1089. * @arg tim_forcedaction_inactive: force inactive level on oc2ref.
  1090. * @retval none
  1091. */
  1092. AT(.com_periph.tim.forcedoc2)
  1093. void tim_forcedoc2config(tim_typedef* TIMx, uint16_t tim_forcedaction)
  1094. {
  1095. uint16_t tmpccmr1 = 0;
  1096. /* check the parameters */
  1097. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1098. assert_param(IS_TIM_FORCED_ACTION(tim_forcedaction));
  1099. tmpccmr1 = TIMx->ccmr1;
  1100. /* reset the oc2m bits */
  1101. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2M);
  1102. /* configure the forced output mode */
  1103. tmpccmr1 |= (uint16_t)(tim_forcedaction << 8);
  1104. /* write to TIMx ccmr1 register */
  1105. TIMx->ccmr1 = tmpccmr1;
  1106. }
  1107. /**
  1108. * @brief forces the TIMx output 3 waveform to active or inactive level.
  1109. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1110. * @param tim_forcedaction: specifies the forced action to be set to the output waveform.
  1111. * this parameter can be one of the following values:
  1112. * @arg tim_forcedaction_active: force active level on oc3ref
  1113. * @arg tim_forcedaction_inactive: force inactive level on oc3ref.
  1114. * @retval none
  1115. */
  1116. AT(.com_periph.tim.forcedoc3)
  1117. void tim_forcedoc3config(tim_typedef* TIMx, uint16_t tim_forcedaction)
  1118. {
  1119. uint16_t tmpccmr2 = 0;
  1120. /* check the parameters */
  1121. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1122. assert_param(IS_TIM_FORCED_ACTION(tim_forcedaction));
  1123. tmpccmr2 = TIMx->ccmr2;
  1124. /* reset the oc1m bits */
  1125. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3M);
  1126. /* configure the forced output mode */
  1127. tmpccmr2 |= tim_forcedaction;
  1128. /* write to TIMx ccmr2 register */
  1129. TIMx->ccmr2 = tmpccmr2;
  1130. }
  1131. /**
  1132. * @brief forces the TIMx output 4 waveform to active or inactive level.
  1133. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1134. * @param tim_forcedaction: specifies the forced action to be set to the output waveform.
  1135. * this parameter can be one of the following values:
  1136. * @arg tim_forcedaction_active: force active level on oc4ref
  1137. * @arg tim_forcedaction_inactive: force inactive level on oc4ref.
  1138. * @retval none
  1139. */
  1140. AT(.com_periph.tim.forcedoc4)
  1141. void tim_forcedoc4config(tim_typedef* TIMx, uint16_t tim_forcedaction)
  1142. {
  1143. uint16_t tmpccmr2 = 0;
  1144. /* check the parameters */
  1145. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1146. assert_param(IS_TIM_FORCED_ACTION(tim_forcedaction));
  1147. tmpccmr2 = TIMx->ccmr2;
  1148. /* reset the oc2m bits */
  1149. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4M);
  1150. /* configure the forced output mode */
  1151. tmpccmr2 |= (uint16_t)(tim_forcedaction << 8);
  1152. /* write to TIMx ccmr2 register */
  1153. TIMx->ccmr2 = tmpccmr2;
  1154. }
  1155. /**
  1156. * @brief enables or DISABLEs TIMx peripheral preload register on arr.
  1157. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  1158. * @param newstate: new state of the TIMx peripheral preload register
  1159. * this parameter can be: enable or DISABLE.
  1160. * @retval none
  1161. */
  1162. AT(.com_periph.tim.arrpreload)
  1163. void tim_arrpreloadconfig(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  1164. {
  1165. /* check the parameters */
  1166. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1167. assert_param(is_FUNCTIONAL_STATE(newstate));
  1168. if (newstate != DISABLE)
  1169. {
  1170. /* set the arr preload bit */
  1171. TIMx->cr1 |= TIM_CR1_ARPE;
  1172. }
  1173. else
  1174. {
  1175. /* reset the arr preload bit */
  1176. TIMx->cr1 &= (uint16_t)~((uint16_t)TIM_CR1_ARPE);
  1177. }
  1178. }
  1179. /**
  1180. * @brief selects the tim peripheral commutation event.
  1181. * @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the TIMx peripheral
  1182. * @param newstate: new state of the commutation event.
  1183. * this parameter can be: enable or DISABLE.
  1184. * @retval none
  1185. */
  1186. AT(.com_periph.tim.selectcom)
  1187. void tim_selectcom(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  1188. {
  1189. /* check the parameters */
  1190. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1191. assert_param(is_FUNCTIONAL_STATE(newstate));
  1192. if (newstate != DISABLE)
  1193. {
  1194. /* set the com bit */
  1195. TIMx->cr2 |= TIM_CR2_CCUS;
  1196. }
  1197. else
  1198. {
  1199. /* reset the com bit */
  1200. TIMx->cr2 &= (uint16_t)~((uint16_t)TIM_CR2_CCUS);
  1201. }
  1202. }
  1203. /**
  1204. * @brief selects the TIMx peripheral capture compare dma source.
  1205. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 15, 16 or 17 to select
  1206. * the tim peripheral.
  1207. * @param newstate: new state of the capture compare dma source
  1208. * this parameter can be: enable or DISABLE.
  1209. * @retval none
  1210. */
  1211. //void tim_selectccdma(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)预留
  1212. //{
  1213. // /* check the parameters */
  1214. // assert_param(IS_TIM_LIST4_PERIPH(TIMx));
  1215. // assert_param(is_FUNCTIONAL_STATE(newstate));
  1216. // if (newstate != DISABLE)
  1217. // {
  1218. // /* set the ccds bit */
  1219. // TIMx->cr2 |= TIM_CR2_CCDS; // 预留 ■
  1220. // }
  1221. // else
  1222. // {
  1223. // /* reset the ccds bit */
  1224. // TIMx->cr2 &= (uint16_t)~((uint16_t)TIM_CR2_CCDS); // 预留 ■
  1225. // }
  1226. //}
  1227. /**
  1228. * @brief sets or resets the tim peripheral capture compare preload control bit.
  1229. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8 or 15
  1230. * to select the TIMx peripheral
  1231. * @param newstate: new state of the capture compare preload control bit
  1232. * this parameter can be: enable or DISABLE.
  1233. * @retval none
  1234. */
  1235. AT(.com_periph.tim.ccpreloadcontrol)
  1236. void tim_ccpreloadcontrol(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  1237. {
  1238. /* check the parameters */
  1239. assert_param(IS_TIM_LIST5_PERIPH(TIMx));
  1240. assert_param(is_FUNCTIONAL_STATE(newstate));
  1241. if (newstate != DISABLE)
  1242. {
  1243. /* set the ccpc bit */
  1244. TIMx->cr2 |= TIM_CR2_CCPC;
  1245. }
  1246. else
  1247. {
  1248. /* reset the ccpc bit */
  1249. TIMx->cr2 &= (uint16_t)~((uint16_t)TIM_CR2_CCPC);
  1250. }
  1251. }
  1252. /**
  1253. * @brief enables or DISABLEs the TIMx peripheral preload register on ccr1.
  1254. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  1255. * @param tim_ocpreload: new state of the TIMx peripheral preload register
  1256. * this parameter can be one of the following values:
  1257. * @arg tim_ocpreload_enable
  1258. * @arg tim_ocpreload_DISABLE
  1259. * @retval none
  1260. */
  1261. AT(.com_periph.tim.oc1preload)
  1262. void tim_oc1preloadconfig(tim_typedef* TIMx, uint16_t tim_ocpreload)
  1263. {
  1264. uint16_t tmpccmr1 = 0;
  1265. /* check the parameters */
  1266. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  1267. assert_param(IS_TIM_OCPRELOAD_STATE(tim_ocpreload));
  1268. tmpccmr1 = TIMx->ccmr1;
  1269. /* reset the oc1pe bit */
  1270. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1PE);
  1271. /* enable or DISABLE the output compare preload feature */
  1272. tmpccmr1 |= tim_ocpreload;
  1273. /* write to TIMx ccmr1 register */
  1274. TIMx->ccmr1 = tmpccmr1;
  1275. }
  1276. /**
  1277. * @brief enables or DISABLEs the TIMx peripheral preload register on ccr2.
  1278. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select
  1279. * the tim peripheral.
  1280. * @param tim_ocpreload: new state of the TIMx peripheral preload register
  1281. * this parameter can be one of the following values:
  1282. * @arg tim_ocpreload_enable
  1283. * @arg tim_ocpreload_DISABLE
  1284. * @retval none
  1285. */
  1286. AT(.com_periph.tim.oc2preload)
  1287. void tim_oc2preloadconfig(tim_typedef* TIMx, uint16_t tim_ocpreload)
  1288. {
  1289. uint16_t tmpccmr1 = 0;
  1290. /* check the parameters */
  1291. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1292. assert_param(IS_TIM_OCPRELOAD_STATE(tim_ocpreload));
  1293. tmpccmr1 = TIMx->ccmr1;
  1294. /* reset the oc2pe bit */
  1295. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2PE);
  1296. /* enable or DISABLE the output compare preload feature */
  1297. tmpccmr1 |= (uint16_t)(tim_ocpreload << 8);
  1298. /* write to TIMx ccmr1 register */
  1299. TIMx->ccmr1 = tmpccmr1;
  1300. }
  1301. /**
  1302. * @brief enables or DISABLEs the TIMx peripheral preload register on ccr3.
  1303. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1304. * @param tim_ocpreload: new state of the TIMx peripheral preload register
  1305. * this parameter can be one of the following values:
  1306. * @arg tim_ocpreload_enable
  1307. * @arg tim_ocpreload_DISABLE
  1308. * @retval none
  1309. */
  1310. AT(.com_periph.tim.oc3preload)
  1311. void tim_oc3preloadconfig(tim_typedef* TIMx, uint16_t tim_ocpreload)
  1312. {
  1313. uint16_t tmpccmr2 = 0;
  1314. /* check the parameters */
  1315. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1316. // assert_param(IS_TIM_OCPRELOAD_STATE(tim_ocpreload));
  1317. tmpccmr2 = TIMx->ccmr2;
  1318. /* reset the oc3pe bit */
  1319. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3PE);
  1320. /* enable or DISABLE the output compare preload feature */
  1321. tmpccmr2 |= tim_ocpreload;
  1322. /* write to TIMx ccmr2 register */
  1323. TIMx->ccmr2 = tmpccmr2;
  1324. }
  1325. /**
  1326. * @brief enables or DISABLEs the TIMx peripheral preload register on ccr4.
  1327. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1328. * @param tim_ocpreload: new state of the TIMx peripheral preload register
  1329. * this parameter can be one of the following values:
  1330. * @arg tim_ocpreload_enable
  1331. * @arg tim_ocpreload_DISABLE
  1332. * @retval none
  1333. */
  1334. AT(.com_periph.tim.oc4preload)
  1335. void tim_oc4preloadconfig(tim_typedef* TIMx, uint16_t tim_ocpreload)
  1336. {
  1337. uint16_t tmpccmr2 = 0;
  1338. /* check the parameters */
  1339. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1340. // assert_param(IS_TIM_OCPRELOAD_STATE(tim_ocpreload));
  1341. tmpccmr2 = TIMx->ccmr2;
  1342. /* reset the oc4pe bit */
  1343. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4PE);
  1344. /* enable or DISABLE the output compare preload feature */
  1345. tmpccmr2 |= (uint16_t)(tim_ocpreload << 8);
  1346. /* write to TIMx ccmr2 register */
  1347. TIMx->ccmr2 = tmpccmr2;
  1348. }
  1349. /**
  1350. * @brief configures the TIMx output compare 1 fast feature.
  1351. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  1352. * @param tim_ocfast: new state of the output compare fast enable bit.
  1353. * this parameter can be one of the following values:
  1354. * @arg tim_ocfast_enable: tim output compare fast enable
  1355. * @arg tim_ocfast_DISABLE: tim output compare fast DISABLE
  1356. * @retval none
  1357. */
  1358. //void tim_oc1fastconfig(tim_typedef* TIMx, uint16_t tim_ocfast) 预留
  1359. //{
  1360. // uint16_t tmpccmr1 = 0;
  1361. // /* check the parameters */
  1362. // assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  1363. // assert_param(IS_TIM_OCFAST_STATE(tim_ocfast));
  1364. // /* get the TIMx ccmr1 register value */
  1365. // tmpccmr1 = TIMx->ccmr1;
  1366. // /* reset the oc1fe bit */
  1367. // tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1FE); // 预留 ■
  1368. // /* enable or DISABLE the output compare fast bit */
  1369. // tmpccmr1 |= tim_ocfast;
  1370. // /* write to TIMx ccmr1 */
  1371. // TIMx->ccmr1 = tmpccmr1;
  1372. //}
  1373. /**
  1374. * @brief configures the TIMx output compare 2 fast feature.
  1375. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select
  1376. * the tim peripheral.
  1377. * @param tim_ocfast: new state of the output compare fast enable bit.
  1378. * this parameter can be one of the following values:
  1379. * @arg tim_ocfast_enable: tim output compare fast enable
  1380. * @arg tim_ocfast_DISABLE: tim output compare fast DISABLE
  1381. * @retval none
  1382. */
  1383. //void tim_oc2fastconfig(tim_typedef* TIMx, uint16_t tim_ocfast) 预留
  1384. //{
  1385. // uint16_t tmpccmr1 = 0;
  1386. // /* check the parameters */
  1387. //// assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1388. //// assert_param(IS_TIM_OCFAST_STATE(tim_ocfast));
  1389. // /* get the TIMx ccmr1 register value */
  1390. // tmpccmr1 = TIMx->ccmr1;
  1391. // /* reset the oc2fe bit */
  1392. // tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2FE); // 预留 ■
  1393. // /* enable or DISABLE the output compare fast bit */
  1394. // tmpccmr1 |= (uint16_t)(tim_ocfast << 8);
  1395. // /* write to TIMx ccmr1 */
  1396. // TIMx->ccmr1 = tmpccmr1;
  1397. //}
  1398. /**
  1399. * @brief configures the TIMx output compare 3 fast feature.
  1400. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1401. * @param tim_ocfast: new state of the output compare fast enable bit.
  1402. * this parameter can be one of the following values:
  1403. * @arg tim_ocfast_enable: tim output compare fast enable
  1404. * @arg tim_ocfast_DISABLE: tim output compare fast DISABLE
  1405. * @retval none
  1406. */
  1407. //void tim_oc3fastconfig(tim_typedef* TIMx, uint16_t tim_ocfast) 预留
  1408. //{
  1409. // uint16_t tmpccmr2 = 0;
  1410. // /* check the parameters */
  1411. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1412. // assert_param(IS_TIM_OCFAST_STATE(tim_ocfast));
  1413. // /* get the TIMx ccmr2 register value */
  1414. // tmpccmr2 = TIMx->ccmr2;
  1415. // /* reset the oc3fe bit */
  1416. // tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3FE); // 预留 ■
  1417. // /* enable or DISABLE the output compare fast bit */
  1418. // tmpccmr2 |= tim_ocfast;
  1419. // /* write to TIMx ccmr2 */
  1420. // TIMx->ccmr2 = tmpccmr2;
  1421. //}
  1422. /**
  1423. * @brief configures the TIMx output compare 4 fast feature.
  1424. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1425. * @param tim_ocfast: new state of the output compare fast enable bit.
  1426. * this parameter can be one of the following values:
  1427. * @arg tim_ocfast_enable: tim output compare fast enable
  1428. * @arg tim_ocfast_DISABLE: tim output compare fast DISABLE
  1429. * @retval none
  1430. */
  1431. //void tim_oc4fastconfig(tim_typedef* TIMx, uint16_t tim_ocfast) 预留
  1432. //{
  1433. // uint16_t tmpccmr2 = 0;
  1434. // /* check the parameters */
  1435. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1436. // assert_param(IS_TIM_OCFAST_STATE(tim_ocfast));
  1437. // /* get the TIMx ccmr2 register value */
  1438. // tmpccmr2 = TIMx->ccmr2;
  1439. // /* reset the oc4fe bit */
  1440. // tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4FE); // 预留 ■
  1441. // /* enable or DISABLE the output compare fast bit */
  1442. // tmpccmr2 |= (uint16_t)(tim_ocfast << 8);
  1443. // /* write to TIMx ccmr2 */
  1444. // TIMx->ccmr2 = tmpccmr2;
  1445. //}
  1446. /**
  1447. * @brief clears or safeguards the ocref1 signal on an external event
  1448. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1449. * @param tim_occlear: new state of the output compare clear enable bit.
  1450. * this parameter can be one of the following values:
  1451. * @arg tim_occlear_enable: tim output clear enable
  1452. * @arg tim_occlear_DISABLE: tim output clear DISABLE
  1453. * @retval none
  1454. */
  1455. AT(.com_periph.tim.clearoc1)
  1456. void tim_clearoc1ref(tim_typedef* TIMx, uint16_t tim_occlear)
  1457. {
  1458. uint16_t tmpccmr1 = 0;
  1459. /* check the parameters */
  1460. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1461. assert_param(IS_TIM_OCCLEAR_STATE(tim_occlear));
  1462. tmpccmr1 = TIMx->ccmr1;
  1463. /* reset the oc1ce bit */
  1464. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1CE);
  1465. /* enable or DISABLE the output compare clear bit */
  1466. tmpccmr1 |= tim_occlear;
  1467. /* write to TIMx ccmr1 register */
  1468. TIMx->ccmr1 = tmpccmr1;
  1469. }
  1470. /**
  1471. * @brief clears or safeguards the ocref2 signal on an external event
  1472. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1473. * @param tim_occlear: new state of the output compare clear enable bit.
  1474. * this parameter can be one of the following values:
  1475. * @arg tim_occlear_enable: tim output clear enable
  1476. * @arg tim_occlear_DISABLE: tim output clear DISABLE
  1477. * @retval none
  1478. */
  1479. AT(.com_periph.tim.clearoc2)
  1480. void tim_clearoc2ref(tim_typedef* TIMx, uint16_t tim_occlear)
  1481. {
  1482. uint16_t tmpccmr1 = 0;
  1483. /* check the parameters */
  1484. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1485. assert_param(IS_TIM_OCCLEAR_STATE(tim_occlear));
  1486. tmpccmr1 = TIMx->ccmr1;
  1487. /* reset the oc2ce bit */
  1488. tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC2CE);
  1489. /* enable or DISABLE the output compare clear bit */
  1490. tmpccmr1 |= (uint16_t)(tim_occlear << 8);
  1491. /* write to TIMx ccmr1 register */
  1492. TIMx->ccmr1 = tmpccmr1;
  1493. }
  1494. /**
  1495. * @brief clears or safeguards the ocref3 signal on an external event
  1496. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1497. * @param tim_occlear: new state of the output compare clear enable bit.
  1498. * this parameter can be one of the following values:
  1499. * @arg tim_occlear_enable: tim output clear enable
  1500. * @arg tim_occlear_DISABLE: tim output clear DISABLE
  1501. * @retval none
  1502. */
  1503. AT(.com_periph.tim.clearoc3)
  1504. void tim_clearoc3ref(tim_typedef* TIMx, uint16_t tim_occlear)
  1505. {
  1506. uint16_t tmpccmr2 = 0;
  1507. /* check the parameters */
  1508. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1509. assert_param(IS_TIM_OCCLEAR_STATE(tim_occlear));
  1510. tmpccmr2 = TIMx->ccmr2;
  1511. /* reset the oc3ce bit */
  1512. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC3CE);
  1513. /* enable or DISABLE the output compare clear bit */
  1514. tmpccmr2 |= tim_occlear;
  1515. /* write to TIMx ccmr2 register */
  1516. TIMx->ccmr2 = tmpccmr2;
  1517. }
  1518. /**
  1519. * @brief clears or safeguards the ocref4 signal on an external event
  1520. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1521. * @param tim_occlear: new state of the output compare clear enable bit.
  1522. * this parameter can be one of the following values:
  1523. * @arg tim_occlear_enable: tim output clear enable
  1524. * @arg tim_occlear_DISABLE: tim output clear DISABLE
  1525. * @retval none
  1526. */
  1527. AT(.com_periph.tim.clearoc4)
  1528. void tim_clearoc4ref(tim_typedef* TIMx, uint16_t tim_occlear)
  1529. {
  1530. uint16_t tmpccmr2 = 0;
  1531. /* check the parameters */
  1532. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1533. assert_param(IS_TIM_OCCLEAR_STATE(tim_occlear));
  1534. tmpccmr2 = TIMx->ccmr2;
  1535. /* reset the oc4ce bit */
  1536. tmpccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_OC4CE);
  1537. /* enable or DISABLE the output compare clear bit */
  1538. tmpccmr2 |= (uint16_t)(tim_occlear << 8);
  1539. /* write to TIMx ccmr2 register */
  1540. TIMx->ccmr2 = tmpccmr2;
  1541. }
  1542. /**
  1543. * @brief configures the TIMx channel 1 polarity.
  1544. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  1545. * @param tim_ocpolarity: specifies the oc1 polarity
  1546. * this parameter can be one of the following values:
  1547. * @arg TIM_OCPOLARITY_HIGH: output compare active high
  1548. * @arg tim_ocpolarity_low: output compare active low
  1549. * @retval none
  1550. */
  1551. AT(.com_periph.tim.oc1polarity)
  1552. void tim_oc1polarityconfig(tim_typedef* TIMx, uint16_t tim_ocpolarity)
  1553. {
  1554. uint16_t tmpccer = 0;
  1555. /* check the parameters */
  1556. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  1557. assert_param(IS_TIM_OC_POLARITY(tim_ocpolarity));
  1558. tmpccer = TIMx->ccer;
  1559. /* set or reset the cc1p bit */
  1560. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1P);
  1561. tmpccer |= tim_ocpolarity;
  1562. /* write to TIMx ccer register */
  1563. TIMx->ccer = tmpccer;
  1564. }
  1565. /**
  1566. * @brief configures the TIMx channel 1n polarity.
  1567. * @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the tim peripheral.
  1568. * @param tim_ocnpolarity: specifies the oc1n polarity
  1569. * this parameter can be one of the following values:
  1570. * @arg tim_ocnpolarity_high: output compare active high
  1571. * @arg tim_ocnpolarity_low: output compare active low
  1572. * @retval none
  1573. */
  1574. AT(.com_periph.oc1npolarity)
  1575. void tim_oc1npolarityconfig(tim_typedef* TIMx, uint16_t tim_ocnpolarity)
  1576. {
  1577. uint16_t tmpccer = 0;
  1578. /* check the parameters */
  1579. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1580. assert_param(IS_TIM_OCN_POLARITY(tim_ocnpolarity));
  1581. tmpccer = TIMx->ccer;
  1582. /* set or reset the cc1np bit */
  1583. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1NP);
  1584. tmpccer |= tim_ocnpolarity;
  1585. /* write to TIMx ccer register */
  1586. TIMx->ccer = tmpccer;
  1587. }
  1588. /**
  1589. * @brief configures the TIMx channel 2 polarity.
  1590. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  1591. * @param tim_ocpolarity: specifies the oc2 polarity
  1592. * this parameter can be one of the following values:
  1593. * @arg TIM_OCPOLARITY_HIGH: output compare active high
  1594. * @arg tim_ocpolarity_low: output compare active low
  1595. * @retval none
  1596. */
  1597. AT(.com_periph.oc2polarity)
  1598. void tim_oc2polarityconfig(tim_typedef* TIMx, uint16_t tim_ocpolarity)
  1599. {
  1600. uint16_t tmpccer = 0;
  1601. /* check the parameters */
  1602. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1603. assert_param(IS_TIM_OC_POLARITY(tim_ocpolarity));
  1604. tmpccer = TIMx->ccer;
  1605. /* set or reset the cc2p bit */
  1606. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC2P);
  1607. tmpccer |= (uint16_t)(tim_ocpolarity << 4);
  1608. /* write to TIMx ccer register */
  1609. TIMx->ccer = tmpccer;
  1610. }
  1611. /**
  1612. * @brief configures the TIMx channel 2n polarity.
  1613. * @param TIMx: where x can be 1 or 8 to select the tim peripheral.
  1614. * @param tim_ocnpolarity: specifies the oc2n polarity
  1615. * this parameter can be one of the following values:
  1616. * @arg tim_ocnpolarity_high: output compare active high
  1617. * @arg tim_ocnpolarity_low: output compare active low
  1618. * @retval none
  1619. */
  1620. AT(.com_periph.oc2npolarity)
  1621. void tim_oc2npolarityconfig(tim_typedef* TIMx, uint16_t tim_ocnpolarity)
  1622. {
  1623. uint16_t tmpccer = 0;
  1624. /* check the parameters */
  1625. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1626. assert_param(IS_TIM_OCN_POLARITY(tim_ocnpolarity));
  1627. tmpccer = TIMx->ccer;
  1628. /* set or reset the cc2np bit */
  1629. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC2NP);
  1630. tmpccer |= (uint16_t)(tim_ocnpolarity << 4);
  1631. /* write to TIMx ccer register */
  1632. TIMx->ccer = tmpccer;
  1633. }
  1634. /**
  1635. * @brief configures the TIMx channel 3 polarity.
  1636. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1637. * @param tim_ocpolarity: specifies the oc3 polarity
  1638. * this parameter can be one of the following values:
  1639. * @arg TIM_OCPOLARITY_HIGH: output compare active high
  1640. * @arg tim_ocpolarity_low: output compare active low
  1641. * @retval none
  1642. */
  1643. AT(.com_periph.oc3polarity)
  1644. void tim_oc3polarityconfig(tim_typedef* TIMx, uint16_t tim_ocpolarity)
  1645. {
  1646. uint16_t tmpccer = 0;
  1647. /* check the parameters */
  1648. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1649. assert_param(IS_TIM_OC_POLARITY(tim_ocpolarity));
  1650. tmpccer = TIMx->ccer;
  1651. /* set or reset the cc3p bit */
  1652. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC3P);
  1653. tmpccer |= (uint16_t)(tim_ocpolarity << 8);
  1654. /* write to TIMx ccer register */
  1655. TIMx->ccer = tmpccer;
  1656. }
  1657. /**
  1658. * @brief configures the TIMx channel 3n polarity.
  1659. * @param TIMx: where x can be 1 or 8 to select the tim peripheral.
  1660. * @param tim_ocnpolarity: specifies the oc3n polarity
  1661. * this parameter can be one of the following values:
  1662. * @arg tim_ocnpolarity_high: output compare active high
  1663. * @arg tim_ocnpolarity_low: output compare active low
  1664. * @retval none
  1665. */
  1666. AT(.com_periph.oc3npolarity)
  1667. void tim_oc3npolarityconfig(tim_typedef* TIMx, uint16_t tim_ocnpolarity)
  1668. {
  1669. uint16_t tmpccer = 0;
  1670. /* check the parameters */
  1671. assert_param(IS_TIM_LIST1_PERIPH(TIMx));
  1672. assert_param(IS_TIM_OCN_POLARITY(tim_ocnpolarity));
  1673. tmpccer = TIMx->ccer;
  1674. /* set or reset the cc3np bit */
  1675. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC3NP);
  1676. tmpccer |= (uint16_t)(tim_ocnpolarity << 8);
  1677. /* write to TIMx ccer register */
  1678. TIMx->ccer = tmpccer;
  1679. }
  1680. /**
  1681. * @brief configures the TIMx channel 4 polarity.
  1682. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1683. * @param tim_ocpolarity: specifies the oc4 polarity
  1684. * this parameter can be one of the following values:
  1685. * @arg TIM_OCPOLARITY_HIGH: output compare active high
  1686. * @arg tim_ocpolarity_low: output compare active low
  1687. * @retval none
  1688. */
  1689. AT(.com_periph.oc4polarity)
  1690. void tim_oc4polarityconfig(tim_typedef* TIMx, uint16_t tim_ocpolarity)
  1691. {
  1692. uint16_t tmpccer = 0;
  1693. /* check the parameters */
  1694. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  1695. assert_param(IS_TIM_OC_POLARITY(tim_ocpolarity));
  1696. tmpccer = TIMx->ccer;
  1697. /* set or reset the cc4p bit */
  1698. tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC4P);
  1699. tmpccer |= (uint16_t)(tim_ocpolarity << 12);
  1700. /* write to TIMx ccer register */
  1701. TIMx->ccer = tmpccer;
  1702. }
  1703. /**
  1704. * @brief enables or DISABLEs the tim capture compare channel x.
  1705. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  1706. * @param tim_channel: specifies the tim channel
  1707. * this parameter can be one of the following values:
  1708. * @arg TIM_CHANNEL_1: tim channel 1
  1709. * @arg TIM_CHANNEL_2: tim channel 2
  1710. * @arg TIM_CHANNEL_3: tim channel 3
  1711. * @arg TIM_CHANNEL_4: tim channel 4
  1712. * @param tim_ccx: specifies the tim channel ccxe bit new state.
  1713. * this parameter can be: tim_ccx_enable or tim_ccx_DISABLE.
  1714. * @retval none
  1715. */
  1716. AT(.com_periph.ccxcmd)
  1717. void tim_ccxcmd(tim_typedef* TIMx, uint16_t tim_channel, uint16_t tim_ccx)
  1718. {
  1719. uint16_t tmp = 0;
  1720. /* check the parameters */
  1721. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  1722. assert_param(IS_TIM_CHANNEL(tim_channel));
  1723. assert_param(IS_TIM_CCX(tim_ccx));
  1724. tmp = CCER_CCE_SET << tim_channel;
  1725. /* reset the ccxe bit */
  1726. TIMx->ccer &= (uint16_t)~ tmp;
  1727. /* set or reset the ccxe bit */
  1728. TIMx->ccer |= (uint16_t)(tim_ccx << tim_channel);
  1729. }
  1730. /**
  1731. * @brief enables or DISABLEs the tim capture compare channel xn.
  1732. * @param TIMx: where x can be 1, 8, 15, 16 or 17 to select the tim peripheral.
  1733. * @param tim_channel: specifies the tim channel
  1734. * this parameter can be one of the following values:
  1735. * @arg TIM_CHANNEL_1: tim channel 1
  1736. * @arg TIM_CHANNEL_2: tim channel 2
  1737. * @arg TIM_CHANNEL_3: tim channel 3
  1738. * @param tim_ccxn: specifies the tim channel ccxne bit new state.
  1739. * this parameter can be: tim_ccxn_enable or tim_ccxn_DISABLE.
  1740. * @retval none
  1741. */
  1742. AT(.com_periph.ccxncmd)
  1743. void tim_ccxncmd(tim_typedef* TIMx, uint16_t tim_channel, uint16_t tim_ccxn)
  1744. {
  1745. uint16_t tmp = 0;
  1746. /* check the parameters */
  1747. assert_param(IS_TIM_LIST2_PERIPH(TIMx));
  1748. assert_param(IS_TIM_COMPLEMENTARY_CHANNEL(tim_channel));
  1749. assert_param(IS_TIM_CCXN(tim_ccxn));
  1750. tmp = CCER_CCNE_SET << tim_channel;
  1751. /* reset the ccxne bit */
  1752. TIMx->ccer &= (uint16_t) ~tmp;
  1753. /* set or reset the ccxne bit */
  1754. TIMx->ccer |= (uint16_t)(tim_ccxn << tim_channel);
  1755. }
  1756. /**
  1757. * @brief selects the tim output compare mode.
  1758. * @note this function DISABLEs the selected channel before changing the output
  1759. * compare mode.
  1760. * user has to enable this channel using tim_ccxcmd and tim_ccxncmd functions.
  1761. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  1762. * @param tim_channel: specifies the tim channel
  1763. * this parameter can be one of the following values:
  1764. * @arg TIM_CHANNEL_1: tim channel 1
  1765. * @arg TIM_CHANNEL_2: tim channel 2
  1766. * @arg TIM_CHANNEL_3: tim channel 3
  1767. * @arg TIM_CHANNEL_4: tim channel 4
  1768. * @param tim_ocmode: specifies the tim output compare mode.
  1769. * this parameter can be one of the following values:
  1770. * @arg TIM_OCMODE_TIMING
  1771. * @arg tim_ocmode_active
  1772. * @arg tim_ocmode_toggle
  1773. * @arg tim_ocmode_low
  1774. * @arg tim_ocmode_high
  1775. * @arg tim_ocmode_pwm1
  1776. * @arg tim_ocmode_pwm2
  1777. * @arg tim_forcedaction_active
  1778. * @arg tim_forcedaction_inactive
  1779. * @retval none
  1780. */
  1781. AT(.com_periph.selectocxm)
  1782. void tim_selectocxm(tim_typedef* TIMx, uint16_t tim_channel, uint16_t tim_ocmode)
  1783. {
  1784. uint32_t tmp = 0;
  1785. uint16_t tmp1 = 0;
  1786. /* check the parameters */
  1787. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  1788. assert_param(IS_TIM_CHANNEL(tim_channel));
  1789. assert_param(IS_TIM_OCM(tim_ocmode));
  1790. tmp = (uint32_t) TIMx;
  1791. tmp += CCMR_OFFSET;
  1792. tmp1 = CCER_CCE_SET << (uint16_t)tim_channel;
  1793. /* DISABLE the channel: reset the ccxe bit */
  1794. TIMx->ccer &= (uint16_t) ~tmp1;
  1795. if((tim_channel == TIM_CHANNEL_1) ||(tim_channel == TIM_CHANNEL_3))
  1796. {
  1797. tmp += (tim_channel>>1);
  1798. /* reset the ocxm bits in the ccmrx register */
  1799. *(__IO uint32_t *) tmp &= (uint32_t)~((uint32_t)TIM_CCMR1_OC1M);
  1800. /* configure the ocxm bits in the ccmrx register */
  1801. *(__IO uint32_t *) tmp |= tim_ocmode;
  1802. }
  1803. else
  1804. {
  1805. tmp += (uint16_t)(tim_channel - (uint16_t)4)>> (uint16_t)1;
  1806. /* reset the ocxm bits in the ccmrx register */
  1807. *(__IO uint32_t *) tmp &= (uint32_t)~((uint32_t)TIM_CCMR1_OC2M);
  1808. /* configure the ocxm bits in the ccmrx register */
  1809. *(__IO uint32_t *) tmp |= (uint16_t)(tim_ocmode << 8);
  1810. }
  1811. }
  1812. /**
  1813. * @brief enables or DISABLEs the TIMx update event.
  1814. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  1815. * @param newstate: new state of the TIMx udis bit
  1816. * this parameter can be: enable or DISABLE.
  1817. * @retval none
  1818. */
  1819. AT(.com_periph.tim.updatedisable)
  1820. void tim_updatedisableconfig(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  1821. {
  1822. /* check the parameters */
  1823. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1824. assert_param(IS_FUNCTIONAL_STATE(newstate));
  1825. if (newstate != DISABLE)
  1826. {
  1827. /* set the update DISABLE bit */
  1828. TIMx->cr1 |= TIM_CR1_UDIS;
  1829. }
  1830. else
  1831. {
  1832. /* reset the update DISABLE bit */
  1833. TIMx->cr1 &= (uint16_t)~((uint16_t)TIM_CR1_UDIS);
  1834. }
  1835. }
  1836. /**
  1837. * @brief configures the TIMx update request interrupt source.
  1838. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  1839. * @param tim_updatesource: specifies the update source.
  1840. * this parameter can be one of the following values:
  1841. * @arg tim_updatesource_regular: source of update is the counter overflow/underflow
  1842. or the setting of ug bit, or an update generation
  1843. through the slave mode controller.
  1844. * @arg tim_updatesource_global: source of update is counter overflow/underflow.
  1845. * @retval none
  1846. */
  1847. AT(.com_periph.tim.updaterequest)
  1848. void tim_updaterequestconfig(tim_typedef* TIMx, uint16_t tim_updatesource)
  1849. {
  1850. /* check the parameters */
  1851. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1852. assert_param(IS_TIM_UPDATE_SOURCE(tim_updatesource));
  1853. if (tim_updatesource != TIM_UPDATESOURCE_GLOBAL)
  1854. {
  1855. /* set the urs bit */
  1856. TIMx->cr1 |= TIM_CR1_URS;
  1857. }
  1858. else
  1859. {
  1860. /* reset the urs bit */
  1861. TIMx->cr1 &= (uint16_t)~((uint16_t)TIM_CR1_URS);
  1862. }
  1863. }
  1864. /**
  1865. * @brief enables or DISABLEs the TIMx's hall sensor interface.
  1866. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  1867. * @param newstate: new state of the TIMx hall sensor interface.
  1868. * this parameter can be: enable or DISABLE.
  1869. * @retval none
  1870. */
  1871. //void tim_selecthallsensor(tim_typedef* TIMx, FUNCTIONAL_STATE newstate) 预留
  1872. //{
  1873. // /* check the parameters */
  1874. // assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1875. // assert_param(IS_FUNCTIONAL_STATE(newstate));
  1876. // if (newstate != DISABLE)
  1877. // {
  1878. // /* set the ti1s bit */
  1879. // TIMx->cr2 |= TIM_CR2_TI1S; // 预留 ■
  1880. // }
  1881. // else
  1882. // {
  1883. // /* reset the ti1s bit */
  1884. // TIMx->cr2 &= (uint16_t)~((uint16_t)TIM_CR2_TI1S); // 预留 ■
  1885. // }
  1886. //}
  1887. /**
  1888. * @brief selects the TIMx's one pulse mode.
  1889. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  1890. * @param tim_opmode: specifies the opm mode to be used.
  1891. * this parameter can be one of the following values:
  1892. * @arg tim_opmode_single
  1893. * @arg tim_opmode_repetitive
  1894. * @retval none
  1895. */
  1896. AT(.com_periph.selectonepulsemode)
  1897. void tim_selectonepulsemode(tim_typedef* TIMx, uint16_t tim_opmode)
  1898. {
  1899. /* check the parameters */
  1900. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1901. assert_param(IS_TIM_OPM_MODE(tim_opmode));
  1902. /* reset the opm bit */
  1903. TIMx->cr1 &= (uint16_t)~((uint16_t)TIM_CR1_OPM);
  1904. /* configure the opm mode */
  1905. TIMx->cr1 |= tim_opmode;
  1906. }
  1907. /**
  1908. * @brief selects the TIMx trigger output mode.
  1909. * @param TIMx: where x can be 1, 2, 3, 4, 5, 6, 7, 8, 9, 12 or 15 to select the tim peripheral.
  1910. * @param tim_trgosource: specifies the trigger output source.
  1911. * this paramter can be one of the following values:
  1912. *
  1913. * - for all TIMx
  1914. * @arg tim_trgosource_reset: the ug bit in the tim_egr register is used as the trigger output (trgo).
  1915. * @arg tim_trgosource_enable: the counter enable cen is used as the trigger output (trgo).
  1916. * @arg tim_trgosource_update: the update event is selected as the trigger output (trgo).
  1917. *
  1918. * - for all TIMx except tim6 and tim7
  1919. * @arg tim_trgosource_oc1: the trigger output sends a positive pulse when the cc1if flag
  1920. * is to be set, as soon as a capture or compare match occurs (trgo).
  1921. * @arg tim_trgosource_oc1ref: oc1ref signal is used as the trigger output (trgo).
  1922. * @arg tim_trgosource_oc2ref: oc2ref signal is used as the trigger output (trgo).
  1923. * @arg tim_trgosource_oc3ref: oc3ref signal is used as the trigger output (trgo).
  1924. * @arg tim_trgosource_oc4ref: oc4ref signal is used as the trigger output (trgo).
  1925. *
  1926. * @retval none
  1927. */
  1928. AT(.com_periph.tim.selectoutputtrigger)
  1929. void tim_selectoutputtrigger(tim_typedef* TIMx, uint16_t tim_trgosource)
  1930. {
  1931. /* check the parameters */
  1932. assert_param(IS_TIM_LIST7_PERIPH(TIMx));
  1933. assert_param(IS_TIM_TRGO_SOURCE(tim_trgosource));
  1934. /* reset the mms bits */
  1935. TIMx->cr2 &= (uint16_t)~((uint16_t)TIM_CR2_MMS);
  1936. /* select the trgo source */
  1937. TIMx->cr2 |= tim_trgosource;
  1938. }
  1939. /**
  1940. * @brief selects the TIMx slave mode.
  1941. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  1942. * @param tim_slavemode: specifies the timer slave mode.
  1943. * this parameter can be one of the following values:
  1944. * @arg tim_slavemode_reset: rising edge of the selected trigger signal (trgi) re-initializes
  1945. * the counter and triggers an update of the registers.
  1946. * @arg tim_slavemode_gated: the counter clock is enabled when the trigger signal (trgi) is high.
  1947. * @arg tim_slavemode_trigger: the counter starts at a rising edge of the trigger trgi.
  1948. * @arg TIM_SLAVEMODE_EXTERNAL1: rising edges of the selected trigger (trgi) clock the counter.
  1949. * @retval none
  1950. */
  1951. AT(.com_periph.tim.selectslavemode)
  1952. void tim_selectslavemode(tim_typedef* TIMx, uint16_t tim_slavemode)
  1953. {
  1954. /* check the parameters */
  1955. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1956. assert_param(IS_TIM_SLAVE_MODE(tim_slavemode));
  1957. /* reset the sms bits */
  1958. TIMx->smcr &= (uint16_t)~((uint16_t)TIM_SMCR_SMS);
  1959. /* select the slave mode */
  1960. TIMx->smcr |= tim_slavemode;
  1961. }
  1962. /**
  1963. * @brief sets or resets the TIMx master/slave mode.
  1964. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  1965. * @param tim_masterslavemode: specifies the timer master slave mode.
  1966. * this parameter can be one of the following values:
  1967. * @arg tim_masterslavemode_enable: synchronization between the current timer
  1968. * and its slaves (through trgo).
  1969. * @arg tim_masterslavemode_DISABLE: no action
  1970. * @retval none
  1971. */
  1972. //void tim_selectmasterslavemode(tim_typedef* TIMx, uint16_t tim_masterslavemode) 预留
  1973. //{
  1974. // /* check the parameters */
  1975. // assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  1976. // assert_param(IS_TIM_MSM_STATE(tim_masterslavemode));
  1977. // /* reset the msm bit */
  1978. // TIMx->smcr &= (uint16_t)~((uint16_t)TIM_SMCR_MSM); // 预留 ■
  1979. //
  1980. // /* set or reset the msm bit */
  1981. // TIMx->smcr |= tim_masterslavemode;
  1982. //}
  1983. /**
  1984. * @brief sets the TIMx counter register value
  1985. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  1986. * @param counter: specifies the counter register new value.
  1987. * @retval none
  1988. */
  1989. AT(.com_periph.tim.setcounter)
  1990. void tim_setcounter(tim_typedef* TIMx, uint16_t counter)
  1991. {
  1992. /* check the parameters */
  1993. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  1994. /* set the counter register value */
  1995. TIMx->cnt = counter;
  1996. }
  1997. /**
  1998. * @brief sets the TIMx autoreload register value
  1999. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2000. * @param autoreload: specifies the autoreload register new value.
  2001. * @retval none
  2002. */
  2003. AT(.com_periph.tim.setautoreload)
  2004. void tim_setautoreload(tim_typedef* TIMx, uint16_t autoreload)
  2005. {
  2006. /* check the parameters */
  2007. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2008. /* set the autoreload register value */
  2009. TIMx->arr = autoreload;
  2010. }
  2011. /**
  2012. * @brief sets the TIMx capture compare1 register value
  2013. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  2014. * @param compare1: specifies the capture compare1 register new value.
  2015. * @retval none
  2016. */
  2017. AT(.com_periph.setcompare1)
  2018. void tim_setcompare1(tim_typedef* TIMx, uint16_t compare1)
  2019. {
  2020. /* check the parameters */
  2021. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  2022. /* set the capture compare1 register value */
  2023. TIMx->ccr1 = compare1;
  2024. }
  2025. /**
  2026. * @brief sets the TIMx capture compare2 register value
  2027. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  2028. * @param compare2: specifies the capture compare2 register new value.
  2029. * @retval none
  2030. */
  2031. AT(.com_periph.setcompare2)
  2032. void tim_setcompare2(tim_typedef* TIMx, uint16_t compare2)
  2033. {
  2034. /* check the parameters */
  2035. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2036. /* set the capture compare2 register value */
  2037. TIMx->ccr2 = compare2;
  2038. }
  2039. /**
  2040. * @brief sets the TIMx capture compare3 register value
  2041. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2042. * @param compare3: specifies the capture compare3 register new value.
  2043. * @retval none
  2044. */
  2045. AT(.com_periph.setcompare3)
  2046. void tim_setcompare3(tim_typedef* TIMx, uint16_t compare3)
  2047. {
  2048. /* check the parameters */
  2049. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2050. /* set the capture compare3 register value */
  2051. TIMx->ccr3 = compare3;
  2052. }
  2053. /**
  2054. * @brief sets the TIMx capture compare4 register value
  2055. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2056. * @param compare4: specifies the capture compare4 register new value.
  2057. * @retval none
  2058. */
  2059. AT(.com_periph.setcompare4)
  2060. void tim_setcompare4(tim_typedef* TIMx, uint16_t compare4)
  2061. {
  2062. /* check the parameters */
  2063. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2064. /* set the capture compare4 register value */
  2065. TIMx->ccr4 = compare4;
  2066. }
  2067. /**
  2068. * @brief sets the TIMx input capture 1 prescaler.
  2069. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  2070. * @param tim_icpsc: specifies the input capture1 prescaler new value.
  2071. * this parameter can be one of the following values:
  2072. * @arg TIM_ICPSC_DIV1: no prescaler
  2073. * @arg tim_icpsc_div2: capture is done once every 2 events
  2074. * @arg tim_icpsc_div4: capture is done once every 4 events
  2075. * @arg tim_icpsc_div8: capture is done once every 8 events
  2076. * @retval none
  2077. */
  2078. AT(.com_periph.setic1prescaler)
  2079. void tim_setic1prescaler(tim_typedef* TIMx, uint16_t tim_icpsc)
  2080. {
  2081. /* check the parameters */
  2082. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  2083. assert_param(IS_TIM_IC_PRESCALER(tim_icpsc));
  2084. /* reset the ic1psc bits */
  2085. TIMx->ccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_IC1PSC); // 预留 ■
  2086. /* set the ic1psc value */
  2087. TIMx->ccmr1 |= tim_icpsc;
  2088. }
  2089. /**
  2090. * @brief sets the TIMx input capture 2 prescaler.
  2091. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  2092. * @param tim_icpsc: specifies the input capture2 prescaler new value.
  2093. * this parameter can be one of the following values:
  2094. * @arg TIM_ICPSC_DIV1: no prescaler
  2095. * @arg tim_icpsc_div2: capture is done once every 2 events
  2096. * @arg tim_icpsc_div4: capture is done once every 4 events
  2097. * @arg tim_icpsc_div8: capture is done once every 8 events
  2098. * @retval none
  2099. */
  2100. AT(.com_periph.setic2prescaler)
  2101. void tim_setic2prescaler(tim_typedef* TIMx, uint16_t tim_icpsc)
  2102. {
  2103. /* check the parameters */
  2104. assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2105. assert_param(IS_TIM_IC_PRESCALER(tim_icpsc));
  2106. /* reset the ic2psc bits */
  2107. TIMx->ccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_IC2PSC); // 预留 ■
  2108. /* set the ic2psc value */
  2109. TIMx->ccmr1 |= (uint16_t)(tim_icpsc << 8);
  2110. }
  2111. /**
  2112. * @brief sets the TIMx input capture 3 prescaler.
  2113. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2114. * @param tim_icpsc: specifies the input capture3 prescaler new value.
  2115. * this parameter can be one of the following values:
  2116. * @arg TIM_ICPSC_DIV1: no prescaler
  2117. * @arg tim_icpsc_div2: capture is done once every 2 events
  2118. * @arg tim_icpsc_div4: capture is done once every 4 events
  2119. * @arg tim_icpsc_div8: capture is done once every 8 events
  2120. * @retval none
  2121. */
  2122. AT(.com_periph.setic3prescaler)
  2123. void tim_setic3prescaler(tim_typedef* TIMx, uint16_t tim_icpsc)
  2124. {
  2125. /* check the parameters */
  2126. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2127. assert_param(IS_TIM_IC_PRESCALER(tim_icpsc));
  2128. /* reset the ic3psc bits */
  2129. TIMx->ccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_IC3PSC); // 预留 ■
  2130. /* set the ic3psc value */
  2131. TIMx->ccmr2 |= tim_icpsc;
  2132. }
  2133. /**
  2134. * @brief sets the TIMx input capture 4 prescaler.
  2135. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2136. * @param tim_icpsc: specifies the input capture4 prescaler new value.
  2137. * this parameter can be one of the following values:
  2138. * @arg TIM_ICPSC_DIV1: no prescaler
  2139. * @arg tim_icpsc_div2: capture is done once every 2 events
  2140. * @arg tim_icpsc_div4: capture is done once every 4 events
  2141. * @arg tim_icpsc_div8: capture is done once every 8 events
  2142. * @retval none
  2143. */
  2144. AT(.com_periph.setic4prescaler)
  2145. void tim_setic4prescaler(tim_typedef* TIMx, uint16_t tim_icpsc)
  2146. {
  2147. /* check the parameters */
  2148. assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2149. assert_param(IS_TIM_IC_PRESCALER(tim_icpsc));
  2150. /* reset the ic4psc bits */
  2151. TIMx->ccmr2 &= (uint16_t)~((uint16_t)TIM_CCMR2_IC4PSC); // 预留 ■
  2152. /* set the ic4psc value */
  2153. TIMx->ccmr2 |= (uint16_t)(tim_icpsc << 8);
  2154. }
  2155. /**
  2156. * @brief sets the TIMx clock division value.
  2157. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select
  2158. * the tim peripheral.
  2159. * @param tim_ckd: specifies the clock division value.
  2160. * this parameter can be one of the following value:
  2161. * @arg TIM_CKD_DIV1: tdts = tck_tim
  2162. * @arg tim_ckd_div2: tdts = 2*tck_tim
  2163. * @arg tim_ckd_div4: tdts = 4*tck_tim
  2164. * @retval none
  2165. */
  2166. AT(.com_periph.tim.setclockdivision)
  2167. void tim_setclockdivision(tim_typedef* TIMx, uint16_t tim_ckd)
  2168. {
  2169. /* check the parameters */
  2170. assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  2171. assert_param(IS_TIM_CKD_DIV(tim_ckd));
  2172. /* reset the ckd bits */
  2173. TIMx->cr1 &= (uint16_t)~((uint16_t)TIM_CR1_CKD);
  2174. /* set the ckd value */
  2175. TIMx->cr1 |= tim_ckd;
  2176. }
  2177. /**
  2178. * @brief gets the TIMx input capture 1 value.
  2179. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  2180. * @retval capture compare 1 register value.
  2181. */
  2182. //uint16_t tim_getcapture1(tim_typedef* TIMx)
  2183. //{
  2184. // /* check the parameters */
  2185. // assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  2186. // /* get the capture 1 register value */
  2187. // return TIMx->ccr1;
  2188. //}
  2189. /**
  2190. * @brief gets the TIMx input capture 2 value.
  2191. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  2192. * @retval capture compare 2 register value.
  2193. */
  2194. //uint16_t tim_getcapture2(tim_typedef* TIMx)
  2195. //{
  2196. // /* check the parameters */
  2197. // assert_param(IS_TIM_LIST6_PERIPH(TIMx));
  2198. // /* get the capture 2 register value */
  2199. // return TIMx->ccr2;
  2200. //}
  2201. /**
  2202. * @brief gets the TIMx input capture 3 value.
  2203. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2204. * @retval capture compare 3 register value.
  2205. */
  2206. //uint16_t tim_getcapture3(tim_typedef* TIMx)
  2207. //{
  2208. // /* check the parameters */
  2209. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2210. // /* get the capture 3 register value */
  2211. // return TIMx->ccr3;
  2212. //}
  2213. /**
  2214. * @brief gets the TIMx input capture 4 value.
  2215. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2216. * @retval capture compare 4 register value.
  2217. */
  2218. //uint16_t tim_getcapture4(tim_typedef* TIMx)
  2219. //{
  2220. // /* check the parameters */
  2221. // assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  2222. // /* get the capture 4 register value */
  2223. // return TIMx->ccr4;
  2224. //}
  2225. /**
  2226. * @brief gets the TIMx counter value.
  2227. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2228. * @retval counter register value.
  2229. */
  2230. AT(.com_periph.tim.getcounter)
  2231. uint16_t tim_getcounter(tim_typedef* TIMx)
  2232. {
  2233. /* check the parameters */
  2234. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2235. /* get the counter register value */
  2236. return TIMx->cnt;
  2237. }
  2238. /**
  2239. * @brief gets the TIMx prescaler value.
  2240. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2241. * @retval prescaler register value.
  2242. */
  2243. AT(.com_periph.tim.getprescaler)
  2244. uint16_t tim_getprescaler(tim_typedef* TIMx)
  2245. {
  2246. /* check the parameters */
  2247. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2248. /* get the prescaler register value */
  2249. return TIMx->psc;
  2250. }
  2251. /**
  2252. * @brief checks whether the specified tim flag is set or not.
  2253. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2254. * @param tim_flag: specifies the flag to check.
  2255. * this parameter can be one of the following values:
  2256. * @arg tim_flag_update: tim update flag
  2257. * @arg tim_flag_cc1: tim capture compare 1 flag
  2258. * @arg tim_flag_cc2: tim capture compare 2 flag
  2259. * @arg tim_flag_cc3: tim capture compare 3 flag
  2260. * @arg tim_flag_cc4: tim capture compare 4 flag
  2261. * @arg tim_flag_com: tim commutation flag
  2262. * @arg tim_flag_trigger: tim trigger flag
  2263. * @arg tim_flag_break: tim break flag
  2264. * @arg tim_flag_cc1of: tim capture compare 1 overcapture flag
  2265. * @arg tim_flag_cc2of: tim capture compare 2 overcapture flag
  2266. * @arg tim_flag_cc3of: tim capture compare 3 overcapture flag
  2267. * @arg tim_flag_cc4of: tim capture compare 4 overcapture flag
  2268. * @note
  2269. * - tim6 and tim7 can have only one update flag.
  2270. * - tim9, TIM12 and TIM15 can have only tim_flag_update, tim_flag_cc1,
  2271. * tim_flag_cc2 or tim_flag_trigger.
  2272. * - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have tim_flag_update or tim_flag_cc1.
  2273. * - tim_flag_break is used only with TIM1, tim8 and TIM15.
  2274. * - tim_flag_com is used only with TIM1, tim8, TIM15, TIM16 and TIM17.
  2275. * @retval the new state of tim_flag (set or reset).
  2276. */
  2277. AT(.com_periph.getflagstatus)
  2278. FLAG_STATE tim_getflagstatus(tim_typedef* TIMx, uint16_t tim_flag)
  2279. {
  2280. IT_STATE bitstatus = RESET;
  2281. /* check the parameters */
  2282. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2283. assert_param(IS_TIM_GET_FLAG(tim_flag));
  2284. if ((TIMx->sr & tim_flag) != (uint16_t)RESET)
  2285. {
  2286. bitstatus = SET;
  2287. }
  2288. else
  2289. {
  2290. bitstatus = RESET;
  2291. }
  2292. return bitstatus;
  2293. }
  2294. /**
  2295. * @brief clears the TIMx's pending flags.
  2296. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2297. * @param tim_flag: specifies the flag bit to clear.
  2298. * this parameter can be any combination of the following values:
  2299. * @arg tim_flag_update: tim update flag
  2300. * @arg tim_flag_cc1: tim capture compare 1 flag
  2301. * @arg tim_flag_cc2: tim capture compare 2 flag
  2302. * @arg tim_flag_cc3: tim capture compare 3 flag
  2303. * @arg tim_flag_cc4: tim capture compare 4 flag
  2304. * @arg tim_flag_com: tim commutation flag
  2305. * @arg tim_flag_trigger: tim trigger flag
  2306. * @arg tim_flag_break: tim break flag
  2307. * @arg tim_flag_cc1of: tim capture compare 1 overcapture flag
  2308. * @arg tim_flag_cc2of: tim capture compare 2 overcapture flag
  2309. * @arg tim_flag_cc3of: tim capture compare 3 overcapture flag
  2310. * @arg tim_flag_cc4of: tim capture compare 4 overcapture flag
  2311. * @note
  2312. * - tim6 and tim7 can have only one update flag.
  2313. * - tim9, TIM12 and TIM15 can have only tim_flag_update, tim_flag_cc1,
  2314. * tim_flag_cc2 or tim_flag_trigger.
  2315. * - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have tim_flag_update or tim_flag_cc1.
  2316. * - tim_flag_break is used only with TIM1, tim8 and TIM15.
  2317. * - tim_flag_com is used only with TIM1, tim8, TIM15, TIM16 and TIM17.
  2318. * @retval none
  2319. */
  2320. AT(.com_periph.clearflag)
  2321. void tim_clearflag(tim_typedef* TIMx, uint16_t tim_flag)
  2322. {
  2323. /* check the parameters */
  2324. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2325. assert_param(IS_TIM_CLEAR_FLAG(tim_flag));
  2326. /* clear the flags */
  2327. TIMx->sr = (uint16_t)tim_flag;
  2328. }
  2329. /**
  2330. * @brief checks whether the tim interrupt has occurred or not.
  2331. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2332. * @param tim_it: specifies the tim interrupt source to check.
  2333. * this parameter can be one of the following values:
  2334. * @arg tim_it_update: tim update interrupt source
  2335. * @arg tim_it_cc1: tim capture compare 1 interrupt source
  2336. * @arg tim_it_cc2: tim capture compare 2 interrupt source
  2337. * @arg tim_it_cc3: tim capture compare 3 interrupt source
  2338. * @arg tim_it_cc4: tim capture compare 4 interrupt source
  2339. * @arg tim_it_com: tim commutation interrupt source
  2340. * @arg tim_it_trigger: tim trigger interrupt source
  2341. * @arg tim_it_break: tim break interrupt source
  2342. * @note
  2343. * - tim6 and tim7 can generate only an update interrupt.
  2344. * - tim9, TIM12 and TIM15 can have only tim_it_update, tim_it_cc1,
  2345. * tim_it_cc2 or tim_it_trigger.
  2346. * - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have tim_it_update or tim_it_cc1.
  2347. * - tim_it_break is used only with TIM1, tim8 and TIM15.
  2348. * - tim_it_com is used only with TIM1, tim8, TIM15, TIM16 and TIM17.
  2349. * @retval the new state of the tim_it(set or reset).
  2350. */
  2351. AT(.com_periph.getitstatus)
  2352. IT_STATE tim_getitstatus(tim_typedef* TIMx, uint16_t tim_it)
  2353. {
  2354. IT_STATE bitstatus = RESET;
  2355. uint16_t itstatus = 0x0, itenable = 0x0;
  2356. /* check the parameters */
  2357. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2358. assert_param(IS_TIM_GET_IT(tim_it));
  2359. itstatus = TIMx->sr & tim_it;
  2360. itenable = TIMx->dier & tim_it;
  2361. if ((itstatus != (uint16_t)RESET) && (itenable != (uint16_t)RESET))
  2362. {
  2363. bitstatus = SET;
  2364. }
  2365. else
  2366. {
  2367. bitstatus = RESET;
  2368. }
  2369. return bitstatus;
  2370. }
  2371. /**
  2372. * @brief clears the TIMx's interrupt pending bits.
  2373. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2374. * @param tim_it: specifies the pending bit to clear.
  2375. * this parameter can be any combination of the following values:
  2376. * @arg tim_it_update: TIM1 update interrupt source
  2377. * @arg tim_it_cc1: tim capture compare 1 interrupt source
  2378. * @arg tim_it_cc2: tim capture compare 2 interrupt source
  2379. * @arg tim_it_cc3: tim capture compare 3 interrupt source
  2380. * @arg tim_it_cc4: tim capture compare 4 interrupt source
  2381. * @arg tim_it_com: tim commutation interrupt source
  2382. * @arg tim_it_trigger: tim trigger interrupt source
  2383. * @arg tim_it_break: tim break interrupt source
  2384. * @note
  2385. * - tim6 and tim7 can generate only an update interrupt.
  2386. * - tim9, TIM12 and TIM15 can have only tim_it_update, tim_it_cc1,
  2387. * tim_it_cc2 or tim_it_trigger.
  2388. * - TIM10, TIM11, TIM13, TIM14, TIM16 and TIM17 can have tim_it_update or tim_it_cc1.
  2389. * - tim_it_break is used only with TIM1, tim8 and TIM15.
  2390. * - tim_it_com is used only with TIM1, tim8, TIM15, TIM16 and TIM17.
  2391. * @retval none
  2392. */
  2393. AT(.com_periph.clearitpendingbit)
  2394. void tim_clearitpendingbit(tim_typedef* TIMx, uint16_t tim_it)
  2395. {
  2396. /* check the parameters */
  2397. assert_param(IS_TIM_ALL_PERIPH(TIMx));
  2398. assert_param(IS_TIM_IT(tim_it));
  2399. /* clear the it pending bit */
  2400. TIMx->sr = (uint16_t)tim_it;
  2401. }
  2402. /**
  2403. * @brief configure the ti1 as input.
  2404. * @param TIMx: where x can be 1 to 17 except 6 and 7 to select the tim peripheral.
  2405. * @param tim_icpolarity : the input polarity.
  2406. * this parameter can be one of the following values:
  2407. * @arg TIM_ICPOLARITY_RISING
  2408. * @arg TIM_ICPOLARITY_FALLING
  2409. * @param tim_icselection: specifies the input to be used.
  2410. * this parameter can be one of the following values:
  2411. * @arg TIM_ICSELECTION_DIRECTTI: tim input 1 is selected to be connected to ic1.
  2412. * @arg TIM_ICSELECTION_INDIRECTTI: tim input 1 is selected to be connected to ic2.
  2413. * @arg tim_icselection_trc: tim input 1 is selected to be connected to trc.
  2414. * @param tim_icfilter: specifies the input capture filter.
  2415. * this parameter must be a value between 0x00 and 0x0f.
  2416. * @retval none
  2417. */
  2418. static void ti1_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection,
  2419. uint16_t tim_icfilter)
  2420. {
  2421. uint16_t tmpccmr1 = 0, tmpccer = 0;
  2422. /* DISABLE the channel 1: reset the cc1e bit */
  2423. TIMx->ccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1E);
  2424. tmpccmr1 = TIMx->ccmr1;
  2425. tmpccer = TIMx->ccer;
  2426. /* select the input and set the filter */
  2427. tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC1S)) & ((uint16_t)~((uint16_t)TIM_CCMR1_IC1F))); // 预留 ■
  2428. tmpccmr1 |= (uint16_t)(tim_icselection | (uint16_t)(tim_icfilter << (uint16_t)4));
  2429. if(TIMx == TIM1)
  2430. {
  2431. /* select the polarity and set the cc1e bit */
  2432. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC1P));
  2433. tmpccer |= (uint16_t)(tim_icpolarity | (uint16_t)TIM_CCER_CC1E);
  2434. }
  2435. else
  2436. {
  2437. /* select the polarity and set the cc1e bit */
  2438. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC1P | TIM_CCER_CC1NP));
  2439. tmpccer |= (uint16_t)(tim_icpolarity | (uint16_t)TIM_CCER_CC1E);
  2440. }
  2441. /* write to TIMx ccmr1 and ccer registers */
  2442. TIMx->ccmr1 = tmpccmr1;
  2443. TIMx->ccer = tmpccer;
  2444. }
  2445. /**
  2446. * @brief configure the ti2 as input.
  2447. * @param TIMx: where x can be 1, 2, 3, 4, 5, 8, 9, 12 or 15 to select the tim peripheral.
  2448. * @param tim_icpolarity : the input polarity.
  2449. * this parameter can be one of the following values:
  2450. * @arg TIM_ICPOLARITY_RISING
  2451. * @arg TIM_ICPOLARITY_FALLING
  2452. * @param tim_icselection: specifies the input to be used.
  2453. * this parameter can be one of the following values:
  2454. * @arg TIM_ICSELECTION_DIRECTTI: tim input 2 is selected to be connected to ic2.
  2455. * @arg TIM_ICSELECTION_INDIRECTTI: tim input 2 is selected to be connected to ic1.
  2456. * @arg tim_icselection_trc: tim input 2 is selected to be connected to trc.
  2457. * @param tim_icfilter: specifies the input capture filter.
  2458. * this parameter must be a value between 0x00 and 0x0f.
  2459. * @retval none
  2460. */
  2461. static void ti2_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection,
  2462. uint16_t tim_icfilter)
  2463. {
  2464. uint16_t tmpccmr1 = 0, tmpccer = 0, tmp = 0;
  2465. /* DISABLE the channel 2: reset the cc2e bit */
  2466. TIMx->ccer &= (uint16_t)~((uint16_t)TIM_CCER_CC2E);
  2467. tmpccmr1 = TIMx->ccmr1;
  2468. tmpccer = TIMx->ccer;
  2469. tmp = (uint16_t)(tim_icpolarity << 4);
  2470. /* select the input and set the filter */
  2471. tmpccmr1 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR1_CC2S)) & ((uint16_t)~((uint16_t)TIM_CCMR1_IC2F))); // 预留 ■
  2472. tmpccmr1 |= (uint16_t)(tim_icfilter << 12);
  2473. tmpccmr1 |= (uint16_t)(tim_icselection << 8);
  2474. if(TIMx == TIM1)
  2475. {
  2476. /* select the polarity and set the cc2e bit */
  2477. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC2P));
  2478. tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC2E);
  2479. }
  2480. else
  2481. {
  2482. /* select the polarity and set the cc2e bit */
  2483. tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC2P | TIM_CCER_CC2NP));
  2484. tmpccer |= (uint16_t)(tim_icpolarity | (uint16_t)TIM_CCER_CC2E);
  2485. }
  2486. /* write to TIMx ccmr1 and ccer registers */
  2487. TIMx->ccmr1 = tmpccmr1 ;
  2488. TIMx->ccer = tmpccer;
  2489. }
  2490. /**
  2491. * @brief configure the ti3 as input.
  2492. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2493. * @param tim_icpolarity : the input polarity.
  2494. * this parameter can be one of the following values:
  2495. * @arg TIM_ICPOLARITY_RISING
  2496. * @arg TIM_ICPOLARITY_FALLING
  2497. * @param tim_icselection: specifies the input to be used.
  2498. * this parameter can be one of the following values:
  2499. * @arg TIM_ICSELECTION_DIRECTTI: tim input 3 is selected to be connected to ic3.
  2500. * @arg TIM_ICSELECTION_INDIRECTTI: tim input 3 is selected to be connected to ic4.
  2501. * @arg tim_icselection_trc: tim input 3 is selected to be connected to trc.
  2502. * @param tim_icfilter: specifies the input capture filter.
  2503. * this parameter must be a value between 0x00 and 0x0f.
  2504. * @retval none
  2505. */
  2506. //static void ti3_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection, 预留
  2507. // uint16_t tim_icfilter)
  2508. //{
  2509. // uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2510. // /* DISABLE the channel 3: reset the cc3e bit */
  2511. // TIMx->ccer &= (uint16_t)~((uint16_t)TIM_CCER_CC3E);
  2512. // tmpccmr2 = TIMx->ccmr2;
  2513. // tmpccer = TIMx->ccer;
  2514. // tmp = (uint16_t)(tim_icpolarity << 8);
  2515. // /* select the input and set the filter */
  2516. // tmpccmr2 &= (uint16_t)(((uint16_t)~((uint16_t)TIM_CCMR2_CC3S)) & ((uint16_t)~((uint16_t)TIM_CCMR2_IC3F))); // 预留 ■
  2517. // tmpccmr2 |= (uint16_t)(tim_icselection | (uint16_t)(tim_icfilter << (uint16_t)4));
  2518. //
  2519. // if(TIMx == TIM1)
  2520. // {
  2521. // /* select the polarity and set the cc3e bit */
  2522. // tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC3P));
  2523. // tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC3E);
  2524. // }
  2525. // else
  2526. // {
  2527. // /* select the polarity and set the cc3e bit */
  2528. // tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC3P | TIM_CCER_CC3NP));
  2529. // tmpccer |= (uint16_t)(tim_icpolarity | (uint16_t)TIM_CCER_CC3E);
  2530. // }
  2531. //
  2532. // /* write to TIMx ccmr2 and ccer registers */
  2533. // TIMx->ccmr2 = tmpccmr2;
  2534. // TIMx->ccer = tmpccer;
  2535. //}
  2536. /**
  2537. * @brief configure the ti4 as input.
  2538. * @param TIMx: where x can be 1, 2, 3, 4, 5 or 8 to select the tim peripheral.
  2539. * @param tim_icpolarity : the input polarity.
  2540. * this parameter can be one of the following values:
  2541. * @arg TIM_ICPOLARITY_RISING
  2542. * @arg TIM_ICPOLARITY_FALLING
  2543. * @param tim_icselection: specifies the input to be used.
  2544. * this parameter can be one of the following values:
  2545. * @arg TIM_ICSELECTION_DIRECTTI: tim input 4 is selected to be connected to ic4.
  2546. * @arg TIM_ICSELECTION_INDIRECTTI: tim input 4 is selected to be connected to ic3.
  2547. * @arg tim_icselection_trc: tim input 4 is selected to be connected to trc.
  2548. * @param tim_icfilter: specifies the input capture filter.
  2549. * this parameter must be a value between 0x00 and 0x0f.
  2550. * @retval none
  2551. */
  2552. //static void ti4_config(tim_typedef* TIMx, uint16_t tim_icpolarity, uint16_t tim_icselection, 预留
  2553. // uint16_t tim_icfilter)
  2554. //{
  2555. // uint16_t tmpccmr2 = 0, tmpccer = 0, tmp = 0;
  2556. //
  2557. // /* DISABLE the channel 4: reset the cc4e bit */
  2558. // TIMx->ccer &= (uint16_t)~((uint16_t)TIM_CCER_CC4E);
  2559. // tmpccmr2 = TIMx->ccmr2;
  2560. // tmpccer = TIMx->ccer;
  2561. // tmp = (uint16_t)(tim_icpolarity << 12);
  2562. // /* select the input and set the filter */
  2563. // tmpccmr2 &= (uint16_t)((uint16_t)(~(uint16_t)TIM_CCMR2_CC4S) & ((uint16_t)~((uint16_t)TIM_CCMR2_IC4F))); // 预留 ■
  2564. // tmpccmr2 |= (uint16_t)(tim_icselection << 8);
  2565. // tmpccmr2 |= (uint16_t)(tim_icfilter << 12);
  2566. //
  2567. // if(TIMx == TIM1)
  2568. // {
  2569. // /* select the polarity and set the cc4e bit */
  2570. // tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC4P));
  2571. // tmpccer |= (uint16_t)(tmp | (uint16_t)TIM_CCER_CC4E);
  2572. // }
  2573. // else
  2574. // {
  2575. // /* select the polarity and set the cc4e bit */
  2576. // tmpccer &= (uint16_t)~((uint16_t)(TIM_CCER_CC3P | TIM_CCER_CC4NP)); // 预留 ■
  2577. // tmpccer |= (uint16_t)(tim_icpolarity | (uint16_t)TIM_CCER_CC4E);
  2578. // }
  2579. // /* write to TIMx ccmr2 and ccer registers */
  2580. // TIMx->ccmr2 = tmpccmr2;
  2581. // TIMx->ccer = tmpccer;
  2582. //}
  2583. /**
  2584. * @brief enables or DISABLEs the TIMx saradc trgo enable or DISABLE.
  2585. * @param TIMx: where x can be 1 to 17 to select the tim peripheral.
  2586. * @param newstate: new state of the TIMx saradc trgo bit
  2587. * this parameter can be: enable or DISABLE.
  2588. * @retval none
  2589. */
  2590. AT(.com_periph.tim.trgosaradc)
  2591. void tim_trgosaradc_config(tim_typedef* TIMx, FUNCTIONAL_STATE newstate)
  2592. {
  2593. if(newstate != DISABLE)
  2594. {
  2595. /* set the update DISABLE bit */
  2596. TIMx->sarad |= TIM_SARAD_TRGOEN;
  2597. }
  2598. else
  2599. {
  2600. /* reset the update DISABLE bit */
  2601. TIMx->sarad &= (uint32_t)~((uint32_t)TIM_SARAD_TRGOEN);
  2602. }
  2603. }