gd32f4xx_timer.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  1. /*!
  2. \file gd32f4xx_timer.c
  3. \brief TIMER driver
  4. \version 2024-12-20, V3.3.1, firmware for GD32F4xx
  5. */
  6. /*
  7. Copyright (c) 2024, GigaDevice Semiconductor Inc.
  8. Redistribution and use in source and binary forms, with or without modification,
  9. are permitted provided that the following conditions are met:
  10. 1. Redistributions of source code must retain the above copyright notice, this
  11. list of conditions and the following disclaimer.
  12. 2. Redistributions in binary form must reproduce the above copyright notice,
  13. this list of conditions and the following disclaimer in the documentation
  14. and/or other materials provided with the distribution.
  15. 3. Neither the name of the copyright holder nor the names of its contributors
  16. may be used to endorse or promote products derived from this software without
  17. specific prior written permission.
  18. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  22. INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  23. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  24. PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  25. WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  27. OF SUCH DAMAGE.
  28. */
  29. #include "gd32f4xx_timer.h"
  30. /*!
  31. \brief deinit a TIMER
  32. \param[in] timer_periph: TIMERx(x=0..13)
  33. \param[out] none
  34. \retval none
  35. */
  36. void timer_deinit(uint32_t timer_periph)
  37. {
  38. switch(timer_periph) {
  39. case TIMER0:
  40. /* reset TIMER0 */
  41. rcu_periph_reset_enable(RCU_TIMER0RST);
  42. rcu_periph_reset_disable(RCU_TIMER0RST);
  43. break;
  44. case TIMER1:
  45. /* reset TIMER1 */
  46. rcu_periph_reset_enable(RCU_TIMER1RST);
  47. rcu_periph_reset_disable(RCU_TIMER1RST);
  48. break;
  49. case TIMER2:
  50. /* reset TIMER2 */
  51. rcu_periph_reset_enable(RCU_TIMER2RST);
  52. rcu_periph_reset_disable(RCU_TIMER2RST);
  53. break;
  54. case TIMER3:
  55. /* reset TIMER3 */
  56. rcu_periph_reset_enable(RCU_TIMER3RST);
  57. rcu_periph_reset_disable(RCU_TIMER3RST);
  58. break;
  59. case TIMER4:
  60. /* reset TIMER4 */
  61. rcu_periph_reset_enable(RCU_TIMER4RST);
  62. rcu_periph_reset_disable(RCU_TIMER4RST);
  63. break;
  64. case TIMER5:
  65. /* reset TIMER5 */
  66. rcu_periph_reset_enable(RCU_TIMER5RST);
  67. rcu_periph_reset_disable(RCU_TIMER5RST);
  68. break;
  69. case TIMER6:
  70. /* reset TIMER6 */
  71. rcu_periph_reset_enable(RCU_TIMER6RST);
  72. rcu_periph_reset_disable(RCU_TIMER6RST);
  73. break;
  74. case TIMER7:
  75. /* reset TIMER7 */
  76. rcu_periph_reset_enable(RCU_TIMER7RST);
  77. rcu_periph_reset_disable(RCU_TIMER7RST);
  78. break;
  79. case TIMER8:
  80. /* reset TIMER8 */
  81. rcu_periph_reset_enable(RCU_TIMER8RST);
  82. rcu_periph_reset_disable(RCU_TIMER8RST);
  83. break;
  84. case TIMER9:
  85. /* reset TIMER9 */
  86. rcu_periph_reset_enable(RCU_TIMER9RST);
  87. rcu_periph_reset_disable(RCU_TIMER9RST);
  88. break;
  89. case TIMER10:
  90. /* reset TIMER10 */
  91. rcu_periph_reset_enable(RCU_TIMER10RST);
  92. rcu_periph_reset_disable(RCU_TIMER10RST);
  93. break;
  94. case TIMER11:
  95. /* reset TIMER11 */
  96. rcu_periph_reset_enable(RCU_TIMER11RST);
  97. rcu_periph_reset_disable(RCU_TIMER11RST);
  98. break;
  99. case TIMER12:
  100. /* reset TIMER12 */
  101. rcu_periph_reset_enable(RCU_TIMER12RST);
  102. rcu_periph_reset_disable(RCU_TIMER12RST);
  103. break;
  104. case TIMER13:
  105. /* reset TIMER13 */
  106. rcu_periph_reset_enable(RCU_TIMER13RST);
  107. rcu_periph_reset_disable(RCU_TIMER13RST);
  108. break;
  109. default:
  110. break;
  111. }
  112. }
  113. /*!
  114. \brief initialize TIMER init parameter struct with a default value
  115. \param[in] initpara: init parameter struct
  116. \param[out] none
  117. \retval none
  118. */
  119. void timer_struct_para_init(timer_parameter_struct *initpara)
  120. {
  121. /* initialize the init parameter struct member with the default value */
  122. initpara->prescaler = 0U;
  123. initpara->alignedmode = TIMER_COUNTER_EDGE;
  124. initpara->counterdirection = TIMER_COUNTER_UP;
  125. initpara->period = 65535U;
  126. initpara->clockdivision = TIMER_CKDIV_DIV1;
  127. initpara->repetitioncounter = 0U;
  128. }
  129. /*!
  130. \brief initialize TIMER counter
  131. \param[in] timer_periph: TIMERx(x=0..13)
  132. \param[in] initpara: init parameter struct
  133. prescaler: prescaler value of the counter clock,0~65535
  134. alignedmode: TIMER_COUNTER_EDGE,TIMER_COUNTER_CENTER_DOWN,TIMER_COUNTER_CENTER_UP,TIMER_COUNTER_CENTER_BOTH
  135. counterdirection: TIMER_COUNTER_UP,TIMER_COUNTER_DOWN
  136. period: counter auto reload value,(TIMER1,TIMER4,32 bit)
  137. clockdivision: TIMER_CKDIV_DIV1,TIMER_CKDIV_DIV2,TIMER_CKDIV_DIV4
  138. repetitioncounter: counter repetition value,0~255
  139. \param[out] none
  140. \retval none
  141. */
  142. void timer_init(uint32_t timer_periph, timer_parameter_struct *initpara)
  143. {
  144. /* configure the counter prescaler value */
  145. TIMER_PSC(timer_periph) = (uint16_t)initpara->prescaler;
  146. /* configure the counter direction and aligned mode */
  147. if((TIMER0 == timer_periph) || (TIMER1 == timer_periph) || (TIMER2 == timer_periph)
  148. || (TIMER3 == timer_periph) || (TIMER4 == timer_periph) || (TIMER7 == timer_periph)) {
  149. TIMER_CTL0(timer_periph) &= ~(uint32_t)(TIMER_CTL0_DIR | TIMER_CTL0_CAM);
  150. TIMER_CTL0(timer_periph) |= (uint32_t)initpara->alignedmode;
  151. TIMER_CTL0(timer_periph) |= (uint32_t)initpara->counterdirection;
  152. }
  153. /* configure the autoreload value */
  154. TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
  155. if((TIMER5 != timer_periph) && (TIMER6 != timer_periph)) {
  156. /* reset the CKDIV bit */
  157. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CKDIV;
  158. TIMER_CTL0(timer_periph) |= (uint32_t)initpara->clockdivision;
  159. }
  160. if((TIMER0 == timer_periph) || (TIMER7 == timer_periph)) {
  161. /* configure the repetition counter value */
  162. TIMER_CREP(timer_periph) = (uint32_t)initpara->repetitioncounter;
  163. }
  164. /* generate an update event */
  165. TIMER_SWEVG(timer_periph) |= (uint32_t)TIMER_SWEVG_UPG;
  166. }
  167. /*!
  168. \brief enable a TIMER
  169. \param[in] timer_periph: TIMERx(x=0..13)
  170. \param[out] none
  171. \retval none
  172. */
  173. void timer_enable(uint32_t timer_periph)
  174. {
  175. TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_CEN;
  176. }
  177. /*!
  178. \brief disable a TIMER
  179. \param[in] timer_periph: TIMERx(x=0..13)
  180. \param[out] none
  181. \retval none
  182. */
  183. void timer_disable(uint32_t timer_periph)
  184. {
  185. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CEN;
  186. }
  187. /*!
  188. \brief enable the auto reload shadow function
  189. \param[in] timer_periph: TIMERx(x=0..13)
  190. \param[out] none
  191. \retval none
  192. */
  193. void timer_auto_reload_shadow_enable(uint32_t timer_periph)
  194. {
  195. TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_ARSE;
  196. }
  197. /*!
  198. \brief disable the auto reload shadow function
  199. \param[in] timer_periph: TIMERx(x=0..13)
  200. \param[out] none
  201. \retval none
  202. */
  203. void timer_auto_reload_shadow_disable(uint32_t timer_periph)
  204. {
  205. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_ARSE;
  206. }
  207. /*!
  208. \brief enable the update event
  209. \param[in] timer_periph: TIMERx(x=0..13)
  210. \param[out] none
  211. \retval none
  212. */
  213. void timer_update_event_enable(uint32_t timer_periph)
  214. {
  215. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_UPDIS;
  216. }
  217. /*!
  218. \brief disable the update event
  219. \param[in] timer_periph: TIMERx(x=0..13)
  220. \param[out] none
  221. \retval none
  222. */
  223. void timer_update_event_disable(uint32_t timer_periph)
  224. {
  225. TIMER_CTL0(timer_periph) |= (uint32_t) TIMER_CTL0_UPDIS;
  226. }
  227. /*!
  228. \brief set TIMER counter alignment mode
  229. \param[in] timer_periph: TIMERx(x=0..4,7)
  230. \param[in] aligned:
  231. only one parameter can be selected which is shown as below:
  232. \arg TIMER_COUNTER_EDGE: edge-aligned mode
  233. \arg TIMER_COUNTER_CENTER_DOWN: center-aligned and counting down assert mode
  234. \arg TIMER_COUNTER_CENTER_UP: center-aligned and counting up assert mode
  235. \arg TIMER_COUNTER_CENTER_BOTH: center-aligned and counting up/down assert mode
  236. \param[out] none
  237. \retval none
  238. */
  239. void timer_counter_alignment(uint32_t timer_periph, uint16_t aligned)
  240. {
  241. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CAM;
  242. TIMER_CTL0(timer_periph) |= (uint32_t)aligned;
  243. }
  244. /*!
  245. \brief set TIMER counter up direction
  246. \param[in] timer_periph: TIMERx(x=0..4,7)
  247. \param[out] none
  248. \retval none
  249. */
  250. void timer_counter_up_direction(uint32_t timer_periph)
  251. {
  252. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_DIR;
  253. }
  254. /*!
  255. \brief set TIMER counter down direction
  256. \param[in] timer_periph: TIMERx(x=0..4,7)
  257. \param[out] none
  258. \retval none
  259. */
  260. void timer_counter_down_direction(uint32_t timer_periph)
  261. {
  262. TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_DIR;
  263. }
  264. /*!
  265. \brief configure TIMER prescaler
  266. \param[in] timer_periph: TIMERx(x=0..13)
  267. \param[in] prescaler: prescaler value,0~65535
  268. \param[in] pscreload: prescaler reload mode
  269. only one parameter can be selected which is shown as below:
  270. \arg TIMER_PSC_RELOAD_NOW: the prescaler is loaded right now
  271. \arg TIMER_PSC_RELOAD_UPDATE: the prescaler is loaded at the next update event
  272. \param[out] none
  273. \retval none
  274. */
  275. void timer_prescaler_config(uint32_t timer_periph, uint16_t prescaler, uint8_t pscreload)
  276. {
  277. TIMER_PSC(timer_periph) = (uint32_t)prescaler;
  278. if(TIMER_PSC_RELOAD_NOW == pscreload) {
  279. TIMER_SWEVG(timer_periph) |= (uint32_t)TIMER_SWEVG_UPG;
  280. }
  281. }
  282. /*!
  283. \brief configure TIMER repetition register value
  284. \param[in] timer_periph: TIMERx(x=0,7)
  285. \param[in] repetition: the counter repetition value,0~255
  286. \param[out] none
  287. \retval none
  288. */
  289. void timer_repetition_value_config(uint32_t timer_periph, uint16_t repetition)
  290. {
  291. TIMER_CREP(timer_periph) = (uint32_t)repetition;
  292. }
  293. /*!
  294. \brief configure TIMER autoreload register value
  295. \param[in] timer_periph: TIMERx(x=0..13)
  296. \param[in] autoreload: the counter auto-reload value
  297. \param[out] none
  298. \retval none
  299. */
  300. void timer_autoreload_value_config(uint32_t timer_periph, uint32_t autoreload)
  301. {
  302. TIMER_CAR(timer_periph) = (uint32_t)autoreload;
  303. }
  304. /*!
  305. \brief configure TIMER counter register value
  306. \param[in] timer_periph: TIMERx(x=0..13)
  307. \param[in] counter: the counter value,0~65535
  308. \param[out] none
  309. \retval none
  310. */
  311. void timer_counter_value_config(uint32_t timer_periph, uint32_t counter)
  312. {
  313. TIMER_CNT(timer_periph) = (uint32_t)counter;
  314. }
  315. /*!
  316. \brief read TIMER counter value
  317. \param[in] timer_periph: TIMERx(x=0..13)
  318. \param[out] none
  319. \retval counter value
  320. */
  321. uint32_t timer_counter_read(uint32_t timer_periph)
  322. {
  323. uint32_t count_value = 0U;
  324. count_value = TIMER_CNT(timer_periph);
  325. return (count_value);
  326. }
  327. /*!
  328. \brief read TIMER prescaler value
  329. \param[in] timer_periph: TIMERx(x=0..13)
  330. \param[out] none
  331. \retval prescaler register value
  332. */
  333. uint16_t timer_prescaler_read(uint32_t timer_periph)
  334. {
  335. uint16_t prescaler_value = 0U;
  336. prescaler_value = (uint16_t)(TIMER_PSC(timer_periph));
  337. return (prescaler_value);
  338. }
  339. /*!
  340. \brief configure TIMER single pulse mode
  341. \param[in] timer_periph: TIMERx(x=0..8,11)
  342. \param[in] spmode:
  343. only one parameter can be selected which is shown as below:
  344. \arg TIMER_SP_MODE_SINGLE: single pulse mode
  345. \arg TIMER_SP_MODE_REPETITIVE: repetitive pulse mode
  346. \param[out] none
  347. \retval none
  348. */
  349. void timer_single_pulse_mode_config(uint32_t timer_periph, uint32_t spmode)
  350. {
  351. if(TIMER_SP_MODE_SINGLE == spmode) {
  352. TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_SPM;
  353. } else if(TIMER_SP_MODE_REPETITIVE == spmode) {
  354. TIMER_CTL0(timer_periph) &= ~((uint32_t)TIMER_CTL0_SPM);
  355. } else {
  356. /* illegal parameters */
  357. }
  358. }
  359. /*!
  360. \brief configure TIMER update source
  361. \param[in] timer_periph: TIMERx(x=0..13)
  362. \param[in] update:
  363. only one parameter can be selected which is shown as below:
  364. \arg TIMER_UPDATE_SRC_GLOBAL: update generate by setting of UPG bit or the counter overflow/underflow,or the slave mode controller trigger
  365. \arg TIMER_UPDATE_SRC_REGULAR: update generate only by counter overflow/underflow
  366. \param[out] none
  367. \retval none
  368. */
  369. void timer_update_source_config(uint32_t timer_periph, uint32_t update)
  370. {
  371. if(TIMER_UPDATE_SRC_REGULAR == update) {
  372. TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_UPS;
  373. } else if(TIMER_UPDATE_SRC_GLOBAL == update) {
  374. TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_UPS;
  375. } else {
  376. /* illegal parameters */
  377. }
  378. }
  379. /*!
  380. \brief enable the TIMER DMA
  381. \param[in] timer_periph: please refer to the following parameters
  382. \param[in] dma: specify which DMA to enable
  383. one or more parameters can be selected which is shown as below:
  384. \arg TIMER_DMA_UPD: update DMA,TIMERx(x=0..7)
  385. \arg TIMER_DMA_CH0D: channel 0 DMA request,TIMERx(x=0..4,7)
  386. \arg TIMER_DMA_CH1D: channel 1 DMA request,TIMERx(x=0..4,7)
  387. \arg TIMER_DMA_CH2D: channel 2 DMA request,TIMERx(x=0..4,7)
  388. \arg TIMER_DMA_CH3D: channel 3 DMA request,TIMERx(x=0..4,7)
  389. \arg TIMER_DMA_CMTD: commutation DMA request,TIMERx(x=0,7)
  390. \arg TIMER_DMA_TRGD: trigger DMA request,TIMERx(x=0..4,7)
  391. \param[out] none
  392. \retval none
  393. */
  394. void timer_dma_enable(uint32_t timer_periph, uint16_t dma)
  395. {
  396. TIMER_DMAINTEN(timer_periph) |= (uint32_t) dma;
  397. }
  398. /*!
  399. \brief disable the TIMER DMA
  400. \param[in] timer_periph: please refer to the following parameters
  401. \param[in] dma: specify which DMA to disable
  402. one or more parameters can be selected which are shown as below:
  403. \arg TIMER_DMA_UPD: update DMA,TIMERx(x=0..7)
  404. \arg TIMER_DMA_CH0D: channel 0 DMA request,TIMERx(x=0..4,7)
  405. \arg TIMER_DMA_CH1D: channel 1 DMA request,TIMERx(x=0..4,7)
  406. \arg TIMER_DMA_CH2D: channel 2 DMA request,TIMERx(x=0..4,7)
  407. \arg TIMER_DMA_CH3D: channel 3 DMA request,TIMERx(x=0..4,7)
  408. \arg TIMER_DMA_CMTD: commutation DMA request ,TIMERx(x=0,7)
  409. \arg TIMER_DMA_TRGD: trigger DMA request,TIMERx(x=0..4,7)
  410. \param[out] none
  411. \retval none
  412. */
  413. void timer_dma_disable(uint32_t timer_periph, uint16_t dma)
  414. {
  415. TIMER_DMAINTEN(timer_periph) &= (~(uint32_t)(dma));
  416. }
  417. /*!
  418. \brief channel DMA request source selection
  419. \param[in] timer_periph: TIMERx(x=0..4,7)
  420. \param[in] dma_request: channel DMA request source selection
  421. only one parameter can be selected which is shown as below:
  422. \arg TIMER_DMAREQUEST_CHANNELEVENT: DMA request of channel y is sent when channel y event occurs
  423. \arg TIMER_DMAREQUEST_UPDATEEVENT: DMA request of channel y is sent when update event occurs
  424. \param[out] none
  425. \retval none
  426. */
  427. void timer_channel_dma_request_source_select(uint32_t timer_periph, uint8_t dma_request)
  428. {
  429. if(TIMER_DMAREQUEST_UPDATEEVENT == dma_request) {
  430. TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_DMAS;
  431. } else if(TIMER_DMAREQUEST_CHANNELEVENT == dma_request) {
  432. TIMER_CTL1(timer_periph) &= ~(uint32_t)TIMER_CTL1_DMAS;
  433. } else {
  434. /* illegal parameters */
  435. }
  436. }
  437. /*!
  438. \brief configure the TIMER DMA transfer
  439. \param[in] timer_periph: please refer to the following parameters
  440. \param[in] dma_baseaddr:
  441. only one parameter can be selected which is shown as below:
  442. \arg TIMER_DMACFG_DMATA_CTL0: DMA transfer address is TIMER_CTL0,TIMERx(x=0..4,7)
  443. \arg TIMER_DMACFG_DMATA_CTL1: DMA transfer address is TIMER_CTL1,TIMERx(x=0..4,7)
  444. \arg TIMER_DMACFG_DMATA_SMCFG: DMA transfer address is TIMER_SMCFG,TIMERx(x=0..4,7)
  445. \arg TIMER_DMACFG_DMATA_DMAINTEN: DMA transfer address is TIMER_DMAINTEN,TIMERx(x=0..4,7)
  446. \arg TIMER_DMACFG_DMATA_INTF: DMA transfer address is TIMER_INTF,TIMERx(x=0..4,7)
  447. \arg TIMER_DMACFG_DMATA_SWEVG: DMA transfer address is TIMER_SWEVG,TIMERx(x=0..4,7)
  448. \arg TIMER_DMACFG_DMATA_CHCTL0: DMA transfer address is TIMER_CHCTL0,TIMERx(x=0..4,7)
  449. \arg TIMER_DMACFG_DMATA_CHCTL1: DMA transfer address is TIMER_CHCTL1,TIMERx(x=0..4,7)
  450. \arg TIMER_DMACFG_DMATA_CHCTL2: DMA transfer address is TIMER_CHCTL2,TIMERx(x=0..4,7)
  451. \arg TIMER_DMACFG_DMATA_CNT: DMA transfer address is TIMER_CNT,TIMERx(x=0..4,7)
  452. \arg TIMER_DMACFG_DMATA_PSC: DMA transfer address is TIMER_PSC,TIMERx(x=0..4,7)
  453. \arg TIMER_DMACFG_DMATA_CAR: DMA transfer address is TIMER_CAR,TIMERx(x=0..4,7)
  454. \arg TIMER_DMACFG_DMATA_CREP: DMA transfer address is TIMER_CREP,TIMERx(x=0,7)
  455. \arg TIMER_DMACFG_DMATA_CH0CV: DMA transfer address is TIMER_CH0CV,TIMERx(x=0..4,7)
  456. \arg TIMER_DMACFG_DMATA_CH1CV: DMA transfer address is TIMER_CH1CV,TIMERx(x=0..4,7)
  457. \arg TIMER_DMACFG_DMATA_CH2CV: DMA transfer address is TIMER_CH2CV,TIMERx(x=0..4,7)
  458. \arg TIMER_DMACFG_DMATA_CH3CV: DMA transfer address is TIMER_CH3CV,TIMERx(x=0..4,7)
  459. \arg TIMER_DMACFG_DMATA_CCHP: DMA transfer address is TIMER_CCHP,TIMERx(x=0..4,7)
  460. \arg TIMER_DMACFG_DMATA_DMACFG: DMA transfer address is TIMER_DMACFG,TIMERx(x=0..4,7)
  461. \arg TIMER_DMACFG_DMATA_DMATB: DMA transfer address is TIMER_DMATB,TIMERx(x=0..4,7)
  462. \param[in] dma_lenth:
  463. only one parameter can be selected which is shown as below:
  464. \arg TIMER_DMACFG_DMATC_xTRANSFER(x=1..18): DMA transfer x time
  465. \param[out] none
  466. \retval none
  467. */
  468. void timer_dma_transfer_config(uint32_t timer_periph, uint32_t dma_baseaddr, uint32_t dma_lenth)
  469. {
  470. TIMER_DMACFG(timer_periph) &= (~(uint32_t)(TIMER_DMACFG_DMATA | TIMER_DMACFG_DMATC));
  471. TIMER_DMACFG(timer_periph) |= (uint32_t)(dma_baseaddr | dma_lenth);
  472. }
  473. /*!
  474. \brief software generate events
  475. \param[in] timer_periph: please refer to the following parameters
  476. \param[in] event: the timer software event generation sources
  477. one or more parameters can be selected which are shown as below:
  478. \arg TIMER_EVENT_SRC_UPG: update event,TIMERx(x=0..13)
  479. \arg TIMER_EVENT_SRC_CH0G: channel 0 capture or compare event generation,TIMERx(x=0..4,7..13)
  480. \arg TIMER_EVENT_SRC_CH1G: channel 1 capture or compare event generation,TIMERx(x=0..4,7,8,11)
  481. \arg TIMER_EVENT_SRC_CH2G: channel 2 capture or compare event generation,TIMERx(x=0..4,7)
  482. \arg TIMER_EVENT_SRC_CH3G: channel 3 capture or compare event generation,TIMERx(x=0..4,7)
  483. \arg TIMER_EVENT_SRC_CMTG: channel commutation event generation,TIMERx(x=0,7)
  484. \arg TIMER_EVENT_SRC_TRGG: trigger event generation,TIMERx(x=0..4,7,8,11)
  485. \arg TIMER_EVENT_SRC_BRKG: break event generation,TIMERx(x=0,7)
  486. \param[out] none
  487. \retval none
  488. */
  489. void timer_event_software_generate(uint32_t timer_periph, uint16_t event)
  490. {
  491. TIMER_SWEVG(timer_periph) |= (uint32_t)event;
  492. }
  493. /*!
  494. \brief initialize TIMER break parameter struct
  495. \param[in] breakpara: TIMER break parameter struct
  496. \param[out] none
  497. \retval none
  498. */
  499. void timer_break_struct_para_init(timer_break_parameter_struct *breakpara)
  500. {
  501. /* initialize the break parameter struct member with the default value */
  502. breakpara->runoffstate = TIMER_ROS_STATE_DISABLE;
  503. breakpara->ideloffstate = TIMER_IOS_STATE_DISABLE;
  504. breakpara->deadtime = 0U;
  505. breakpara->breakpolarity = TIMER_BREAK_POLARITY_LOW;
  506. breakpara->outputautostate = TIMER_OUTAUTO_DISABLE;
  507. breakpara->protectmode = TIMER_CCHP_PROT_OFF;
  508. breakpara->breakstate = TIMER_BREAK_DISABLE;
  509. }
  510. /*!
  511. \brief configure TIMER break function
  512. \param[in] timer_periph: TIMERx(x=0,7)
  513. \param[in] breakpara: TIMER break parameter struct
  514. runoffstate: TIMER_ROS_STATE_ENABLE,TIMER_ROS_STATE_DISABLE
  515. ideloffstate: TIMER_IOS_STATE_ENABLE,TIMER_IOS_STATE_DISABLE
  516. deadtime: 0~255
  517. breakpolarity: TIMER_BREAK_POLARITY_LOW,TIMER_BREAK_POLARITY_HIGH
  518. outputautostate: TIMER_OUTAUTO_ENABLE,TIMER_OUTAUTO_DISABLE
  519. protectmode: TIMER_CCHP_PROT_OFF,TIMER_CCHP_PROT_0,TIMER_CCHP_PROT_1,TIMER_CCHP_PROT_2
  520. breakstate: TIMER_BREAK_ENABLE,TIMER_BREAK_DISABLE
  521. \param[out] none
  522. \retval none
  523. */
  524. void timer_break_config(uint32_t timer_periph, timer_break_parameter_struct *breakpara)
  525. {
  526. TIMER_CCHP(timer_periph) = (uint32_t)(((uint32_t)(breakpara->runoffstate)) |
  527. ((uint32_t)(breakpara->ideloffstate)) |
  528. ((uint32_t)(breakpara->deadtime)) |
  529. ((uint32_t)(breakpara->breakpolarity)) |
  530. ((uint32_t)(breakpara->outputautostate)) |
  531. ((uint32_t)(breakpara->protectmode)) |
  532. ((uint32_t)(breakpara->breakstate))) ;
  533. }
  534. /*!
  535. \brief enable TIMER break function
  536. \param[in] timer_periph: TIMERx(x=0,7)
  537. \param[out] none
  538. \retval none
  539. */
  540. void timer_break_enable(uint32_t timer_periph)
  541. {
  542. TIMER_CCHP(timer_periph) |= (uint32_t)TIMER_CCHP_BRKEN;
  543. }
  544. /*!
  545. \brief disable TIMER break function
  546. \param[in] timer_periph: TIMERx(x=0,7)
  547. \param[out] none
  548. \retval none
  549. */
  550. void timer_break_disable(uint32_t timer_periph)
  551. {
  552. TIMER_CCHP(timer_periph) &= ~(uint32_t)TIMER_CCHP_BRKEN;
  553. }
  554. /*!
  555. \brief enable TIMER output automatic function
  556. \param[in] timer_periph: TIMERx(x=0,7)
  557. \param[out] none
  558. \retval none
  559. */
  560. void timer_automatic_output_enable(uint32_t timer_periph)
  561. {
  562. TIMER_CCHP(timer_periph) |= (uint32_t)TIMER_CCHP_OAEN;
  563. }
  564. /*!
  565. \brief disable TIMER output automatic function
  566. \param[in] timer_periph: TIMERx(x=0,7)
  567. \param[out] none
  568. \retval none
  569. */
  570. void timer_automatic_output_disable(uint32_t timer_periph)
  571. {
  572. TIMER_CCHP(timer_periph) &= ~(uint32_t)TIMER_CCHP_OAEN;
  573. }
  574. /*!
  575. \brief configure TIMER primary output function
  576. \param[in] timer_periph: TIMERx(x=0,7)
  577. \param[in] newvalue: ENABLE or DISABLE
  578. \param[out] none
  579. \retval none
  580. */
  581. void timer_primary_output_config(uint32_t timer_periph, ControlStatus newvalue)
  582. {
  583. if(ENABLE == newvalue) {
  584. TIMER_CCHP(timer_periph) |= (uint32_t)TIMER_CCHP_POEN;
  585. } else {
  586. TIMER_CCHP(timer_periph) &= (~(uint32_t)TIMER_CCHP_POEN);
  587. }
  588. }
  589. /*!
  590. \brief enable or disable channel capture/compare control shadow register
  591. \param[in] timer_periph: TIMERx(x=0,7)
  592. \param[in] newvalue: ENABLE or DISABLE
  593. \param[out] none
  594. \retval none
  595. */
  596. void timer_channel_control_shadow_config(uint32_t timer_periph, ControlStatus newvalue)
  597. {
  598. if(ENABLE == newvalue) {
  599. TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_CCSE;
  600. } else {
  601. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_CCSE);
  602. }
  603. }
  604. /*!
  605. \brief configure TIMER channel control shadow register update control
  606. \param[in] timer_periph: TIMERx(x=0,7)
  607. \param[in] ccuctl: channel control shadow register update control
  608. only one parameter can be selected which is shown as below:
  609. \arg TIMER_UPDATECTL_CCU: the shadow registers update by when CMTG bit is set
  610. \arg TIMER_UPDATECTL_CCUTRI: the shadow registers update by when CMTG bit is set or an rising edge of TRGI occurs
  611. \param[out] none
  612. \retval none
  613. */
  614. void timer_channel_control_shadow_update_config(uint32_t timer_periph, uint8_t ccuctl)
  615. {
  616. if(TIMER_UPDATECTL_CCU == ccuctl) {
  617. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_CCUC);
  618. } else if(TIMER_UPDATECTL_CCUTRI == ccuctl) {
  619. TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_CCUC;
  620. } else {
  621. /* illegal parameters */
  622. }
  623. }
  624. /*!
  625. \brief initialize TIMER channel output parameter struct with a default value
  626. \param[in] ocpara: TIMER channel n output parameter struct
  627. \param[out] none
  628. \retval none
  629. */
  630. void timer_channel_output_struct_para_init(timer_oc_parameter_struct *ocpara)
  631. {
  632. /* initialize the channel output parameter struct member with the default value */
  633. ocpara->outputstate = (uint16_t)TIMER_CCX_DISABLE;
  634. ocpara->outputnstate = TIMER_CCXN_DISABLE;
  635. ocpara->ocpolarity = TIMER_OC_POLARITY_HIGH;
  636. ocpara->ocnpolarity = TIMER_OCN_POLARITY_HIGH;
  637. ocpara->ocidlestate = TIMER_OC_IDLE_STATE_LOW;
  638. ocpara->ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
  639. }
  640. /*!
  641. \brief configure TIMER channel output function
  642. \param[in] timer_periph: please refer to the following parameters
  643. \param[in] channel:
  644. only one parameter can be selected which is shown as below:
  645. \arg TIMER_CH_0: TIMER channel 0(TIMERx(x=0..4,7..13))
  646. \arg TIMER_CH_1: TIMER channel 1(TIMERx(x=0..4,7,8,11))
  647. \arg TIMER_CH_2: TIMER channel 2(TIMERx(x=0..4,7))
  648. \arg TIMER_CH_3: TIMER channel 3(TIMERx(x=0..4,7))
  649. \param[in] ocpara: TIMER channeln output parameter struct
  650. outputstate: TIMER_CCX_ENABLE,TIMER_CCX_DISABLE
  651. outputnstate: TIMER_CCXN_ENABLE,TIMER_CCXN_DISABLE
  652. ocpolarity: TIMER_OC_POLARITY_HIGH,TIMER_OC_POLARITY_LOW
  653. ocnpolarity: TIMER_OCN_POLARITY_HIGH,TIMER_OCN_POLARITY_LOW
  654. ocidlestate: TIMER_OC_IDLE_STATE_LOW,TIMER_OC_IDLE_STATE_HIGH
  655. ocnidlestate: TIMER_OCN_IDLE_STATE_LOW,TIMER_OCN_IDLE_STATE_HIGH
  656. \param[out] none
  657. \retval none
  658. */
  659. void timer_channel_output_config(uint32_t timer_periph, uint16_t channel, timer_oc_parameter_struct *ocpara)
  660. {
  661. switch(channel) {
  662. /* configure TIMER_CH_0 */
  663. case TIMER_CH_0:
  664. /* reset the CH0EN bit */
  665. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
  666. TIMER_CHCTL0(timer_periph) &= ~(uint32_t)TIMER_CHCTL0_CH0MS;
  667. /* set the CH0EN bit */
  668. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->outputstate;
  669. /* reset the CH0P bit */
  670. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0P);
  671. /* set the CH0P bit */
  672. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->ocpolarity;
  673. if((TIMER0 == timer_periph) || (TIMER7 == timer_periph)) {
  674. /* reset the CH0NEN bit */
  675. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NEN);
  676. /* set the CH0NEN bit */
  677. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->outputnstate;
  678. /* reset the CH0NP bit */
  679. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NP);
  680. /* set the CH0NP bit */
  681. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->ocnpolarity;
  682. /* reset the ISO0 bit */
  683. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO0);
  684. /* set the ISO0 bit */
  685. TIMER_CTL1(timer_periph) |= (uint32_t)ocpara->ocidlestate;
  686. /* reset the ISO0N bit */
  687. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO0N);
  688. /* set the ISO0N bit */
  689. TIMER_CTL1(timer_periph) |= (uint32_t)ocpara->ocnidlestate;
  690. }
  691. break;
  692. /* configure TIMER_CH_1 */
  693. case TIMER_CH_1:
  694. /* reset the CH1EN bit */
  695. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
  696. TIMER_CHCTL0(timer_periph) &= ~(uint32_t)TIMER_CHCTL0_CH1MS;
  697. /* set the CH1EN bit */
  698. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpara->outputstate << 4U);
  699. /* reset the CH1P bit */
  700. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1P);
  701. /* set the CH1P bit */
  702. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocpolarity) << 4U);
  703. if((TIMER0 == timer_periph) || (TIMER7 == timer_periph)) {
  704. /* reset the CH1NEN bit */
  705. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NEN);
  706. /* set the CH1NEN bit */
  707. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->outputnstate) << 4U);
  708. /* reset the CH1NP bit */
  709. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NP);
  710. /* set the CH1NP bit */
  711. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnpolarity) << 4U);
  712. /* reset the ISO1 bit */
  713. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO1);
  714. /* set the ISO1 bit */
  715. TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 2U);
  716. /* reset the ISO1N bit */
  717. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO1N);
  718. /* set the ISO1N bit */
  719. TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnidlestate) << 2U);
  720. }
  721. break;
  722. /* configure TIMER_CH_2 */
  723. case TIMER_CH_2:
  724. /* reset the CH2EN bit */
  725. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2EN);
  726. TIMER_CHCTL1(timer_periph) &= ~(uint32_t)TIMER_CHCTL1_CH2MS;
  727. /* set the CH2EN bit */
  728. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpara->outputstate << 8U);
  729. /* reset the CH2P bit */
  730. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2P);
  731. /* set the CH2P bit */
  732. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocpolarity) << 8U);
  733. if((TIMER0 == timer_periph) || (TIMER7 == timer_periph)) {
  734. /* reset the CH2NEN bit */
  735. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NEN);
  736. /* set the CH2NEN bit */
  737. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->outputnstate) << 8U);
  738. /* reset the CH2NP bit */
  739. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NP);
  740. /* set the CH2NP bit */
  741. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnpolarity) << 8U);
  742. /* reset the ISO2 bit */
  743. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO2);
  744. /* set the ISO2 bit */
  745. TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 4U);
  746. /* reset the ISO2N bit */
  747. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO2N);
  748. /* set the ISO2N bit */
  749. TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnidlestate) << 4U);
  750. }
  751. break;
  752. /* configure TIMER_CH_3 */
  753. case TIMER_CH_3:
  754. /* reset the CH3EN bit */
  755. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3EN);
  756. TIMER_CHCTL1(timer_periph) &= ~(uint32_t)TIMER_CHCTL1_CH3MS;
  757. /* set the CH3EN bit */
  758. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpara->outputstate << 12U);
  759. /* reset the CH3P bit */
  760. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3P);
  761. /* set the CH3P bit */
  762. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocpolarity) << 12U);
  763. if((TIMER0 == timer_periph) || (TIMER7 == timer_periph)) {
  764. /* reset the ISO3 bit */
  765. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO3);
  766. /* set the ISO3 bit */
  767. TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 6U);
  768. }
  769. break;
  770. default:
  771. break;
  772. }
  773. }
  774. /*!
  775. \brief configure TIMER channel output compare mode
  776. \param[in] timer_periph: please refer to the following parameters
  777. \param[in] channel:
  778. only one parameter can be selected which is shown as below:
  779. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  780. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  781. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  782. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  783. \param[in] ocmode: channel output compare mode
  784. only one parameter can be selected which is shown as below:
  785. \arg TIMER_OC_MODE_TIMING: timing mode
  786. \arg TIMER_OC_MODE_ACTIVE: active mode
  787. \arg TIMER_OC_MODE_INACTIVE: inactive mode
  788. \arg TIMER_OC_MODE_TOGGLE: toggle mode
  789. \arg TIMER_OC_MODE_LOW: force low mode
  790. \arg TIMER_OC_MODE_HIGH: force high mode
  791. \arg TIMER_OC_MODE_PWM0: PWM0 mode
  792. \arg TIMER_OC_MODE_PWM1: PWM1 mode
  793. \param[out] none
  794. \retval none
  795. */
  796. void timer_channel_output_mode_config(uint32_t timer_periph, uint16_t channel, uint16_t ocmode)
  797. {
  798. switch(channel) {
  799. /* configure TIMER_CH_0 */
  800. case TIMER_CH_0:
  801. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMCTL);
  802. TIMER_CHCTL0(timer_periph) |= (uint32_t)ocmode;
  803. break;
  804. /* configure TIMER_CH_1 */
  805. case TIMER_CH_1:
  806. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMCTL);
  807. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(ocmode) << 8U);
  808. break;
  809. /* configure TIMER_CH_2 */
  810. case TIMER_CH_2:
  811. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMCTL);
  812. TIMER_CHCTL1(timer_periph) |= (uint32_t)ocmode;
  813. break;
  814. /* configure TIMER_CH_3 */
  815. case TIMER_CH_3:
  816. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMCTL);
  817. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(ocmode) << 8U);
  818. break;
  819. default:
  820. break;
  821. }
  822. }
  823. /*!
  824. \brief configure TIMER channel output pulse value
  825. \param[in] timer_periph: please refer to the following parameters
  826. \param[in] channel:
  827. only one parameter can be selected which is shown as below:
  828. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  829. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  830. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  831. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  832. \param[in] pulse: channel output pulse value,0~65535
  833. \param[out] none
  834. \retval none
  835. */
  836. void timer_channel_output_pulse_value_config(uint32_t timer_periph, uint16_t channel, uint32_t pulse)
  837. {
  838. switch(channel) {
  839. /* configure TIMER_CH_0 */
  840. case TIMER_CH_0:
  841. TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
  842. break;
  843. /* configure TIMER_CH_1 */
  844. case TIMER_CH_1:
  845. TIMER_CH1CV(timer_periph) = (uint32_t)pulse;
  846. break;
  847. /* configure TIMER_CH_2 */
  848. case TIMER_CH_2:
  849. TIMER_CH2CV(timer_periph) = (uint32_t)pulse;
  850. break;
  851. /* configure TIMER_CH_3 */
  852. case TIMER_CH_3:
  853. TIMER_CH3CV(timer_periph) = (uint32_t)pulse;
  854. break;
  855. default:
  856. break;
  857. }
  858. }
  859. /*!
  860. \brief configure TIMER channel output shadow function
  861. \param[in] timer_periph: please refer to the following parameters
  862. \param[in] channel:
  863. only one parameter can be selected which is shown as below:
  864. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  865. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  866. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  867. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  868. \param[in] ocshadow: channel output shadow state
  869. only one parameter can be selected which is shown as below:
  870. \arg TIMER_OC_SHADOW_ENABLE: channel output shadow state enable
  871. \arg TIMER_OC_SHADOW_DISABLE: channel output shadow state disable
  872. \param[out] none
  873. \retval none
  874. */
  875. void timer_channel_output_shadow_config(uint32_t timer_periph, uint16_t channel, uint16_t ocshadow)
  876. {
  877. switch(channel) {
  878. /* configure TIMER_CH_0 */
  879. case TIMER_CH_0:
  880. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMSEN);
  881. TIMER_CHCTL0(timer_periph) |= (uint32_t)ocshadow;
  882. break;
  883. /* configure TIMER_CH_1 */
  884. case TIMER_CH_1:
  885. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMSEN);
  886. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(ocshadow) << 8U);
  887. break;
  888. /* configure TIMER_CH_2 */
  889. case TIMER_CH_2:
  890. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMSEN);
  891. TIMER_CHCTL1(timer_periph) |= (uint32_t)ocshadow;
  892. break;
  893. /* configure TIMER_CH_3 */
  894. case TIMER_CH_3:
  895. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMSEN);
  896. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(ocshadow) << 8U);
  897. break;
  898. default:
  899. break;
  900. }
  901. }
  902. /*!
  903. \brief configure TIMER channel output fast function
  904. \param[in] timer_periph: please refer to the following parameters
  905. \param[in] channel:
  906. only one parameter can be selected which is shown as below:
  907. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  908. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  909. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  910. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  911. \param[in] ocfast: channel output fast function
  912. only one parameter can be selected which is shown as below:
  913. \arg TIMER_OC_FAST_ENABLE: channel output fast function enable
  914. \arg TIMER_OC_FAST_DISABLE: channel output fast function disable
  915. \param[out] none
  916. \retval none
  917. */
  918. void timer_channel_output_fast_config(uint32_t timer_periph, uint16_t channel, uint16_t ocfast)
  919. {
  920. switch(channel) {
  921. /* configure TIMER_CH_0 */
  922. case TIMER_CH_0:
  923. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMFEN);
  924. TIMER_CHCTL0(timer_periph) |= (uint32_t)ocfast;
  925. break;
  926. /* configure TIMER_CH_1 */
  927. case TIMER_CH_1:
  928. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMFEN);
  929. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)ocfast << 8U);
  930. break;
  931. /* configure TIMER_CH_2 */
  932. case TIMER_CH_2:
  933. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMFEN);
  934. TIMER_CHCTL1(timer_periph) |= (uint32_t)ocfast;
  935. break;
  936. /* configure TIMER_CH_3 */
  937. case TIMER_CH_3:
  938. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMFEN);
  939. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)ocfast << 8U);
  940. break;
  941. default:
  942. break;
  943. }
  944. }
  945. /*!
  946. \brief configure TIMER channel output clear function
  947. \param[in] timer_periph: please refer to the following parameters
  948. \param[in] channel:
  949. only one parameter can be selected which is shown as below:
  950. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7))
  951. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7))
  952. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  953. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  954. \param[in] occlear: channel output clear function
  955. only one parameter can be selected which is shown as below:
  956. \arg TIMER_OC_CLEAR_ENABLE: channel output clear function enable
  957. \arg TIMER_OC_CLEAR_DISABLE: channel output clear function disable
  958. \param[out] none
  959. \retval none
  960. */
  961. void timer_channel_output_clear_config(uint32_t timer_periph, uint16_t channel, uint16_t occlear)
  962. {
  963. switch(channel) {
  964. /* configure TIMER_CH_0 */
  965. case TIMER_CH_0:
  966. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMCEN);
  967. TIMER_CHCTL0(timer_periph) |= (uint32_t)occlear;
  968. break;
  969. /* configure TIMER_CH_1 */
  970. case TIMER_CH_1:
  971. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMCEN);
  972. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)occlear << 8U);
  973. break;
  974. /* configure TIMER_CH_2 */
  975. case TIMER_CH_2:
  976. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMCEN);
  977. TIMER_CHCTL1(timer_periph) |= (uint32_t)occlear;
  978. break;
  979. /* configure TIMER_CH_3 */
  980. case TIMER_CH_3:
  981. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMCEN);
  982. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)occlear << 8U);
  983. break;
  984. default:
  985. break;
  986. }
  987. }
  988. /*!
  989. \brief configure TIMER channel output polarity
  990. \param[in] timer_periph: please refer to the following parameters
  991. \param[in] channel:
  992. only one parameter can be selected which is shown as below:
  993. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  994. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  995. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  996. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  997. \param[in] ocpolarity: channel output polarity
  998. only one parameter can be selected which is shown as below:
  999. \arg TIMER_OC_POLARITY_HIGH: channel output polarity is high
  1000. \arg TIMER_OC_POLARITY_LOW: channel output polarity is low
  1001. \param[out] none
  1002. \retval none
  1003. */
  1004. void timer_channel_output_polarity_config(uint32_t timer_periph, uint16_t channel, uint16_t ocpolarity)
  1005. {
  1006. switch(channel) {
  1007. /* configure TIMER_CH_0 */
  1008. case TIMER_CH_0:
  1009. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0P);
  1010. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpolarity;
  1011. break;
  1012. /* configure TIMER_CH_1 */
  1013. case TIMER_CH_1:
  1014. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1P);
  1015. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpolarity << 4U);
  1016. break;
  1017. /* configure TIMER_CH_2 */
  1018. case TIMER_CH_2:
  1019. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2P);
  1020. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpolarity << 8U);
  1021. break;
  1022. /* configure TIMER_CH_3 */
  1023. case TIMER_CH_3:
  1024. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3P);
  1025. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpolarity << 12U);
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. }
  1031. /*!
  1032. \brief configure TIMER channel complementary output polarity
  1033. \param[in] timer_periph: please refer to the following parameters
  1034. \param[in] channel:
  1035. only one parameter can be selected which is shown as below:
  1036. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  1037. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  1038. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  1039. \param[in] ocnpolarity: channel complementary output polarity
  1040. only one parameter can be selected which is shown as below:
  1041. \arg TIMER_OCN_POLARITY_HIGH: channel complementary output polarity is high
  1042. \arg TIMER_OCN_POLARITY_LOW: channel complementary output polarity is low
  1043. \param[out] none
  1044. \retval none
  1045. */
  1046. void timer_channel_complementary_output_polarity_config(uint32_t timer_periph, uint16_t channel, uint16_t ocnpolarity)
  1047. {
  1048. switch(channel) {
  1049. /* configure TIMER_CH_0 */
  1050. case TIMER_CH_0:
  1051. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NP);
  1052. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocnpolarity;
  1053. break;
  1054. /* configure TIMER_CH_1 */
  1055. case TIMER_CH_1:
  1056. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NP);
  1057. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnpolarity << 4U);
  1058. break;
  1059. /* configure TIMER_CH_2 */
  1060. case TIMER_CH_2:
  1061. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NP);
  1062. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnpolarity << 8U);
  1063. break;
  1064. default:
  1065. break;
  1066. }
  1067. }
  1068. /*!
  1069. \brief configure TIMER channel enable state
  1070. \param[in] timer_periph: please refer to the following parameters
  1071. \param[in] channel:
  1072. only one parameter can be selected which is shown as below:
  1073. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  1074. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  1075. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  1076. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  1077. \param[in] state: TIMER channel enable state
  1078. only one parameter can be selected which is shown as below:
  1079. \arg TIMER_CCX_ENABLE: channel enable
  1080. \arg TIMER_CCX_DISABLE: channel disable
  1081. \param[out] none
  1082. \retval none
  1083. */
  1084. void timer_channel_output_state_config(uint32_t timer_periph, uint16_t channel, uint32_t state)
  1085. {
  1086. switch(channel) {
  1087. /* configure TIMER_CH_0 */
  1088. case TIMER_CH_0:
  1089. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
  1090. TIMER_CHCTL2(timer_periph) |= (uint32_t)state;
  1091. break;
  1092. /* configure TIMER_CH_1 */
  1093. case TIMER_CH_1:
  1094. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
  1095. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)state << 4U);
  1096. break;
  1097. /* configure TIMER_CH_2 */
  1098. case TIMER_CH_2:
  1099. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2EN);
  1100. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)state << 8U);
  1101. break;
  1102. /* configure TIMER_CH_3 */
  1103. case TIMER_CH_3:
  1104. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3EN);
  1105. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)state << 12U);
  1106. break;
  1107. default:
  1108. break;
  1109. }
  1110. }
  1111. /*!
  1112. \brief configure TIMER channel complementary output enable state
  1113. \param[in] timer_periph: TIMERx(x=0,7)
  1114. \param[in] channel:
  1115. only one parameter can be selected which is shown as below:
  1116. \arg TIMER_CH_0: TIMER channel0
  1117. \arg TIMER_CH_1: TIMER channel1
  1118. \arg TIMER_CH_2: TIMER channel2
  1119. \param[in] ocnstate: TIMER channel complementary output enable state
  1120. only one parameter can be selected which is shown as below:
  1121. \arg TIMER_CCXN_ENABLE: channel complementary enable
  1122. \arg TIMER_CCXN_DISABLE: channel complementary disable
  1123. \param[out] none
  1124. \retval none
  1125. */
  1126. void timer_channel_complementary_output_state_config(uint32_t timer_periph, uint16_t channel, uint16_t ocnstate)
  1127. {
  1128. switch(channel) {
  1129. /* configure TIMER_CH_0 */
  1130. case TIMER_CH_0:
  1131. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NEN);
  1132. TIMER_CHCTL2(timer_periph) |= (uint32_t)ocnstate;
  1133. break;
  1134. /* configure TIMER_CH_1 */
  1135. case TIMER_CH_1:
  1136. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NEN);
  1137. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnstate << 4U);
  1138. break;
  1139. /* configure TIMER_CH_2 */
  1140. case TIMER_CH_2:
  1141. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NEN);
  1142. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnstate << 8U);
  1143. break;
  1144. default:
  1145. break;
  1146. }
  1147. }
  1148. /*!
  1149. \brief initialize TIMER channel input parameter struct
  1150. \param[in] icpara: TIMER channel intput parameter struct
  1151. \param[out] none
  1152. \retval none
  1153. */
  1154. void timer_channel_input_struct_para_init(timer_ic_parameter_struct *icpara)
  1155. {
  1156. /* initialize the channel input parameter struct member with the default value */
  1157. icpara->icpolarity = TIMER_IC_POLARITY_RISING;
  1158. icpara->icselection = TIMER_IC_SELECTION_DIRECTTI;
  1159. icpara->icprescaler = TIMER_IC_PSC_DIV1;
  1160. icpara->icfilter = 0U;
  1161. }
  1162. /*!
  1163. \brief configure TIMER input capture parameter
  1164. \param[in] timer_periph: please refer to the following parameters
  1165. \param[in] channel:
  1166. only one parameter can be selected which is shown as below:
  1167. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  1168. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  1169. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  1170. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  1171. \param[in] icpara: TIMER channel intput parameter struct
  1172. icpolarity: TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_FALLING,TIMER_IC_POLARITY_BOTH_EDGE
  1173. icselection: TIMER_IC_SELECTION_DIRECTTI,TIMER_IC_SELECTION_INDIRECTTI,TIMER_IC_SELECTION_ITS
  1174. icprescaler: TIMER_IC_PSC_DIV1,TIMER_IC_PSC_DIV2,TIMER_IC_PSC_DIV4,TIMER_IC_PSC_DIV8
  1175. icfilter: 0~15
  1176. \param[out] none
  1177. \retval none
  1178. */
  1179. void timer_input_capture_config(uint32_t timer_periph, uint16_t channel, timer_ic_parameter_struct *icpara)
  1180. {
  1181. switch(channel) {
  1182. /* configure TIMER_CH_0 */
  1183. case TIMER_CH_0:
  1184. /* reset the CH0EN bit */
  1185. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
  1186. /* reset the CH0P and CH0NP bits */
  1187. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P | TIMER_CHCTL2_CH0NP));
  1188. TIMER_CHCTL2(timer_periph) |= (uint32_t)(icpara->icpolarity);
  1189. /* reset the CH0MS bit */
  1190. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
  1191. TIMER_CHCTL0(timer_periph) |= (uint32_t)(icpara->icselection);
  1192. /* reset the CH0CAPFLT bit */
  1193. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
  1194. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 4U);
  1195. /* set the CH0EN bit */
  1196. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
  1197. break;
  1198. /* configure TIMER_CH_1 */
  1199. case TIMER_CH_1:
  1200. /* reset the CH1EN bit */
  1201. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
  1202. /* reset the CH1P and CH1NP bits */
  1203. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P | TIMER_CHCTL2_CH1NP));
  1204. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpara->icpolarity) << 4U);
  1205. /* reset the CH1MS bit */
  1206. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
  1207. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpara->icselection) << 8U);
  1208. /* reset the CH1CAPFLT bit */
  1209. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
  1210. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 12U);
  1211. /* set the CH1EN bit */
  1212. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
  1213. break;
  1214. /* configure TIMER_CH_2 */
  1215. case TIMER_CH_2:
  1216. /* reset the CH2EN bit */
  1217. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2EN);
  1218. /* reset the CH2P and CH2NP bits */
  1219. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH2P | TIMER_CHCTL2_CH2NP));
  1220. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpara->icpolarity) << 8U);
  1221. /* reset the CH2MS bit */
  1222. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2MS);
  1223. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icselection));
  1224. /* reset the CH2CAPFLT bit */
  1225. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2CAPFLT);
  1226. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 4U);
  1227. /* set the CH2EN bit */
  1228. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH2EN;
  1229. break;
  1230. /* configure TIMER_CH_3 */
  1231. case TIMER_CH_3:
  1232. /* reset the CH3EN bit */
  1233. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3EN);
  1234. /* reset the CH3P bits */
  1235. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH3P));
  1236. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpara->icpolarity) << 12U);
  1237. /* reset the CH3MS bit */
  1238. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3MS);
  1239. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icselection) << 8U);
  1240. /* reset the CH3CAPFLT bit */
  1241. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3CAPFLT);
  1242. TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 12U);
  1243. /* set the CH3EN bit */
  1244. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH3EN;
  1245. break;
  1246. default:
  1247. break;
  1248. }
  1249. /* configure TIMER channel input capture prescaler value */
  1250. timer_channel_input_capture_prescaler_config(timer_periph, channel, (uint16_t)(icpara->icprescaler));
  1251. }
  1252. /*!
  1253. \brief configure TIMER channel input capture prescaler value
  1254. \param[in] timer_periph: please refer to the following parameters
  1255. \param[in] channel:
  1256. only one parameter can be selected which is shown as below:
  1257. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  1258. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  1259. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  1260. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  1261. \param[in] prescaler: channel input capture prescaler value
  1262. only one parameter can be selected which is shown as below:
  1263. \arg TIMER_IC_PSC_DIV1: no prescaler
  1264. \arg TIMER_IC_PSC_DIV2: divided by 2
  1265. \arg TIMER_IC_PSC_DIV4: divided by 4
  1266. \arg TIMER_IC_PSC_DIV8: divided by 8
  1267. \param[out] none
  1268. \retval none
  1269. */
  1270. void timer_channel_input_capture_prescaler_config(uint32_t timer_periph, uint16_t channel, uint16_t prescaler)
  1271. {
  1272. switch(channel) {
  1273. /* configure TIMER_CH_0 */
  1274. case TIMER_CH_0:
  1275. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPPSC);
  1276. TIMER_CHCTL0(timer_periph) |= (uint32_t)prescaler;
  1277. break;
  1278. /* configure TIMER_CH_1 */
  1279. case TIMER_CH_1:
  1280. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPPSC);
  1281. TIMER_CHCTL0(timer_periph) |= ((uint32_t)prescaler << 8U);
  1282. break;
  1283. /* configure TIMER_CH_2 */
  1284. case TIMER_CH_2:
  1285. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2CAPPSC);
  1286. TIMER_CHCTL1(timer_periph) |= (uint32_t)prescaler;
  1287. break;
  1288. /* configure TIMER_CH_3 */
  1289. case TIMER_CH_3:
  1290. TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3CAPPSC);
  1291. TIMER_CHCTL1(timer_periph) |= ((uint32_t)prescaler << 8U);
  1292. break;
  1293. default:
  1294. break;
  1295. }
  1296. }
  1297. /*!
  1298. \brief read TIMER channel capture compare register value
  1299. \param[in] timer_periph: please refer to the following parameters
  1300. \param[in] channel:
  1301. only one parameter can be selected which is shown as below:
  1302. \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..4,7..13))
  1303. \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..4,7,8,11))
  1304. \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..4,7))
  1305. \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..4,7))
  1306. \param[out] none
  1307. \retval channel capture compare register value
  1308. */
  1309. uint32_t timer_channel_capture_value_register_read(uint32_t timer_periph, uint16_t channel)
  1310. {
  1311. uint32_t count_value = 0U;
  1312. switch(channel) {
  1313. /* read TIMER channel 0 capture compare register value */
  1314. case TIMER_CH_0:
  1315. count_value = TIMER_CH0CV(timer_periph);
  1316. break;
  1317. /* read TIMER channel 1 capture compare register value */
  1318. case TIMER_CH_1:
  1319. count_value = TIMER_CH1CV(timer_periph);
  1320. break;
  1321. /* read TIMER channel 2 capture compare register value */
  1322. case TIMER_CH_2:
  1323. count_value = TIMER_CH2CV(timer_periph);
  1324. break;
  1325. /* read TIMER channel 3 capture compare register value */
  1326. case TIMER_CH_3:
  1327. count_value = TIMER_CH3CV(timer_periph);
  1328. break;
  1329. default:
  1330. break;
  1331. }
  1332. return (count_value);
  1333. }
  1334. /*!
  1335. \brief configure TIMER input pwm capture function
  1336. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1337. \param[in] channel:
  1338. only one parameter can be selected which is shown as below:
  1339. \arg TIMER_CH_0: TIMER channel0
  1340. \arg TIMER_CH_1: TIMER channel1
  1341. \param[in] icpwm:TIMER channel intput pwm parameter struct
  1342. icpolarity: TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_FALLING
  1343. icselection: TIMER_IC_SELECTION_DIRECTTI,TIMER_IC_SELECTION_INDIRECTTI
  1344. icprescaler: TIMER_IC_PSC_DIV1,TIMER_IC_PSC_DIV2,TIMER_IC_PSC_DIV4,TIMER_IC_PSC_DIV8
  1345. icfilter: 0~15
  1346. \param[out] none
  1347. \retval none
  1348. */
  1349. void timer_input_pwm_capture_config(uint32_t timer_periph, uint16_t channel, timer_ic_parameter_struct *icpwm)
  1350. {
  1351. uint16_t icpolarity = 0x0U;
  1352. uint16_t icselection = 0x0U;
  1353. /* Set channel input polarity */
  1354. if(TIMER_IC_POLARITY_RISING == icpwm->icpolarity) {
  1355. icpolarity = TIMER_IC_POLARITY_FALLING;
  1356. } else {
  1357. icpolarity = TIMER_IC_POLARITY_RISING;
  1358. }
  1359. /* Set channel input mode selection */
  1360. if(TIMER_IC_SELECTION_DIRECTTI == icpwm->icselection) {
  1361. icselection = TIMER_IC_SELECTION_INDIRECTTI;
  1362. } else {
  1363. icselection = TIMER_IC_SELECTION_DIRECTTI;
  1364. }
  1365. if(TIMER_CH_0 == channel) {
  1366. /* reset the CH0EN bit */
  1367. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
  1368. /* reset the CH0P and CH0NP bits */
  1369. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P | TIMER_CHCTL2_CH0NP));
  1370. /* set the CH0P and CH0NP bits */
  1371. TIMER_CHCTL2(timer_periph) |= (uint32_t)(icpwm->icpolarity);
  1372. /* reset the CH0MS bit */
  1373. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
  1374. /* set the CH0MS bit */
  1375. TIMER_CHCTL0(timer_periph) |= (uint32_t)(icpwm->icselection);
  1376. /* reset the CH0CAPFLT bit */
  1377. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
  1378. /* set the CH0CAPFLT bit */
  1379. TIMER_CHCTL0(timer_periph) |= ((uint32_t)(icpwm->icfilter) << 4U);
  1380. /* set the CH0EN bit */
  1381. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
  1382. /* configure TIMER channel input capture prescaler value */
  1383. timer_channel_input_capture_prescaler_config(timer_periph, TIMER_CH_0, (uint16_t)(icpwm->icprescaler));
  1384. /* reset the CH1EN bit */
  1385. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
  1386. /* reset the CH1P and CH1NP bits */
  1387. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P | TIMER_CHCTL2_CH1NP));
  1388. /* set the CH1P and CH1NP bits */
  1389. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)icpolarity << 4U);
  1390. /* reset the CH1MS bit */
  1391. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
  1392. /* set the CH1MS bit */
  1393. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)icselection << 8U);
  1394. /* reset the CH1CAPFLT bit */
  1395. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
  1396. /* set the CH1CAPFLT bit */
  1397. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icfilter) << 12U);
  1398. /* set the CH1EN bit */
  1399. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
  1400. /* configure TIMER channel input capture prescaler value */
  1401. timer_channel_input_capture_prescaler_config(timer_periph, TIMER_CH_1, (uint16_t)(icpwm->icprescaler));
  1402. } else {
  1403. /* reset the CH1EN bit */
  1404. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
  1405. /* reset the CH1P and CH1NP bits */
  1406. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P | TIMER_CHCTL2_CH1NP));
  1407. /* set the CH1P and CH1NP bits */
  1408. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icpolarity) << 4U);
  1409. /* reset the CH1MS bit */
  1410. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
  1411. /* set the CH1MS bit */
  1412. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icselection) << 8U);
  1413. /* reset the CH1CAPFLT bit */
  1414. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
  1415. /* set the CH1CAPFLT bit */
  1416. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icfilter) << 12U);
  1417. /* set the CH1EN bit */
  1418. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
  1419. /* configure TIMER channel input capture prescaler value */
  1420. timer_channel_input_capture_prescaler_config(timer_periph, TIMER_CH_1, (uint16_t)(icpwm->icprescaler));
  1421. /* reset the CH0EN bit */
  1422. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
  1423. /* reset the CH0P and CH0NP bits */
  1424. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P | TIMER_CHCTL2_CH0NP));
  1425. /* set the CH0P and CH0NP bits */
  1426. TIMER_CHCTL2(timer_periph) |= (uint32_t)icpolarity;
  1427. /* reset the CH0MS bit */
  1428. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
  1429. /* set the CH0MS bit */
  1430. TIMER_CHCTL0(timer_periph) |= (uint32_t)icselection;
  1431. /* reset the CH0CAPFLT bit */
  1432. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
  1433. /* set the CH0CAPFLT bit */
  1434. TIMER_CHCTL0(timer_periph) |= ((uint32_t)(icpwm->icfilter) << 4U);
  1435. /* set the CH0EN bit */
  1436. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
  1437. /* configure TIMER channel input capture prescaler value */
  1438. timer_channel_input_capture_prescaler_config(timer_periph, TIMER_CH_0, (uint16_t)(icpwm->icprescaler));
  1439. }
  1440. }
  1441. /*!
  1442. \brief configure TIMER hall sensor mode
  1443. \param[in] timer_periph: TIMERx(x=0..4,7)
  1444. \param[in] hallmode:
  1445. only one parameter can be selected which is shown as below:
  1446. \arg TIMER_HALLINTERFACE_ENABLE: TIMER hall sensor mode enable
  1447. \arg TIMER_HALLINTERFACE_DISABLE: TIMER hall sensor mode disable
  1448. \param[out] none
  1449. \retval none
  1450. */
  1451. void timer_hall_mode_config(uint32_t timer_periph, uint32_t hallmode)
  1452. {
  1453. if(TIMER_HALLINTERFACE_ENABLE == hallmode) {
  1454. TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_TI0S;
  1455. } else if(TIMER_HALLINTERFACE_DISABLE == hallmode) {
  1456. TIMER_CTL1(timer_periph) &= ~(uint32_t)TIMER_CTL1_TI0S;
  1457. } else {
  1458. /* illegal parameters */
  1459. }
  1460. }
  1461. /*!
  1462. \brief select TIMER input trigger source
  1463. \param[in] timer_periph: please refer to the following parameters
  1464. \param[in] intrigger:
  1465. only one parameter can be selected which is shown as below:
  1466. \arg TIMER_SMCFG_TRGSEL_ITI0: internal trigger 0(TIMERx(x=0..4,7,8,11))
  1467. \arg TIMER_SMCFG_TRGSEL_ITI1: internal trigger 1(TIMERx(x=0..4,7,8,11))
  1468. \arg TIMER_SMCFG_TRGSEL_ITI2: internal trigger 2(TIMERx(x=0..4,7,8,11))
  1469. \arg TIMER_SMCFG_TRGSEL_ITI3: internal trigger 3(TIMERx(x=0..4,7,8,11))
  1470. \arg TIMER_SMCFG_TRGSEL_CI0F_ED: TI0 edge detector(TIMERx(x=0..4,7,8,11))
  1471. \arg TIMER_SMCFG_TRGSEL_CI0FE0: filtered TIMER input 0(TIMERx(x=0..4,7,8,11))
  1472. \arg TIMER_SMCFG_TRGSEL_CI1FE1: filtered TIMER input 1(TIMERx(x=0..4,7,8,11))
  1473. \arg TIMER_SMCFG_TRGSEL_ETIFP: external trigger(TIMERx(x=0..4,7,8,11))
  1474. \param[out] none
  1475. \retval none
  1476. */
  1477. void timer_input_trigger_source_select(uint32_t timer_periph, uint32_t intrigger)
  1478. {
  1479. TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_TRGS);
  1480. TIMER_SMCFG(timer_periph) |= (uint32_t)intrigger;
  1481. }
  1482. /*!
  1483. \brief select TIMER master mode output trigger source
  1484. \param[in] timer_periph: TIMERx(x=0..7)
  1485. \param[in] outrigger:
  1486. only one parameter can be selected which is shown as below:
  1487. \arg TIMER_TRI_OUT_SRC_RESET: the UPG bit as trigger output(TIMERx(x=0..7,9,10,12,13))
  1488. \arg TIMER_TRI_OUT_SRC_ENABLE: the counter enable signal TIMER_CTL0_CEN as trigger output(TIMERx(x=0..7,9,10,12,13))
  1489. \arg TIMER_TRI_OUT_SRC_UPDATE: update event as trigger output(TIMERx(x=0..7,9,10,12,13))
  1490. \arg TIMER_TRI_OUT_SRC_CH0: a capture or a compare match occurred in channal0 as trigger output TRGO(TIMERx(x=0..4,7,9,10,12,13))
  1491. \arg TIMER_TRI_OUT_SRC_O0CPRE: O0CPRE as trigger output(TIMERx(x=0..4,7,9,10,12,13))
  1492. \arg TIMER_TRI_OUT_SRC_O1CPRE: O1CPRE as trigger output(TIMERx(x=0..4,7))
  1493. \arg TIMER_TRI_OUT_SRC_O2CPRE: O2CPRE as trigger output(TIMERx(x=0..4,7))
  1494. \arg TIMER_TRI_OUT_SRC_O3CPRE: O3CPRE as trigger output(TIMERx(x=0..4,7))
  1495. \param[out] none
  1496. \retval none
  1497. */
  1498. void timer_master_output_trigger_source_select(uint32_t timer_periph, uint32_t outrigger)
  1499. {
  1500. TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_MMC);
  1501. TIMER_CTL1(timer_periph) |= (uint32_t)outrigger;
  1502. }
  1503. /*!
  1504. \brief select TIMER slave mode
  1505. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1506. \param[in] slavemode:
  1507. only one parameter can be selected which is shown as below:
  1508. \arg TIMER_SLAVE_MODE_DISABLE: slave mode disable(TIMERx(x=0..4,7,8,11))
  1509. \arg TIMER_QUAD_DECODER_MODE0: quadrature decoder mode 0(TIMERx(x=0..4,7))
  1510. \arg TIMER_QUAD_DECODER_MODE1: quadrature decoder mode 1(TIMERx(x=0..4,7))
  1511. \arg TIMER_QUAD_DECODER_MODE2: quadrature decoder mode 2(TIMERx(x=0..4,7))
  1512. \arg TIMER_SLAVE_MODE_RESTART: restart mode(TIMERx(x=0..4,7,8,11))
  1513. \arg TIMER_SLAVE_MODE_PAUSE: pause mode(TIMERx(x=0..4,7,8,11))
  1514. \arg TIMER_SLAVE_MODE_EVENT: event mode(TIMERx(x=0..4,7,8,11))
  1515. \arg TIMER_SLAVE_MODE_EXTERNAL0: external clock mode 0.(TIMERx(x=0..4,7,8,11))
  1516. \param[out] none
  1517. \retval none
  1518. */
  1519. void timer_slave_mode_select(uint32_t timer_periph, uint32_t slavemode)
  1520. {
  1521. TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_SMC);
  1522. TIMER_SMCFG(timer_periph) |= (uint32_t)slavemode;
  1523. }
  1524. /*!
  1525. \brief configure TIMER master slave mode
  1526. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1527. \param[in] masterslave:
  1528. only one parameter can be selected which is shown as below:
  1529. \arg TIMER_MASTER_SLAVE_MODE_ENABLE: master slave mode enable
  1530. \arg TIMER_MASTER_SLAVE_MODE_DISABLE: master slave mode disable
  1531. \param[out] none
  1532. \retval none
  1533. */
  1534. void timer_master_slave_mode_config(uint32_t timer_periph, uint32_t masterslave)
  1535. {
  1536. if(TIMER_MASTER_SLAVE_MODE_ENABLE == masterslave) {
  1537. TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SMCFG_MSM;
  1538. } else if(TIMER_MASTER_SLAVE_MODE_DISABLE == masterslave) {
  1539. TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_MSM;
  1540. } else {
  1541. /* illegal parameters */
  1542. }
  1543. }
  1544. /*!
  1545. \brief configure TIMER external trigger input
  1546. \param[in] timer_periph: TIMERx(x=0..4,7)
  1547. \param[in] extprescaler:
  1548. only one parameter can be selected which is shown as below:
  1549. \arg TIMER_EXT_TRI_PSC_OFF: no divided
  1550. \arg TIMER_EXT_TRI_PSC_DIV2: divided by 2
  1551. \arg TIMER_EXT_TRI_PSC_DIV4: divided by 4
  1552. \arg TIMER_EXT_TRI_PSC_DIV8: divided by 8
  1553. \param[in] extpolarity:
  1554. only one parameter can be selected which is shown as below:
  1555. \arg TIMER_ETP_FALLING: active low or falling edge active
  1556. \arg TIMER_ETP_RISING: active high or rising edge active
  1557. \param[in] extfilter: a value between 0 and 15
  1558. \param[out] none
  1559. \retval none
  1560. */
  1561. void timer_external_trigger_config(uint32_t timer_periph, uint32_t extprescaler,
  1562. uint32_t extpolarity, uint32_t extfilter)
  1563. {
  1564. TIMER_SMCFG(timer_periph) &= (~(uint32_t)(TIMER_SMCFG_ETP | TIMER_SMCFG_ETPSC | TIMER_SMCFG_ETFC));
  1565. TIMER_SMCFG(timer_periph) |= (uint32_t)(extprescaler | extpolarity);
  1566. TIMER_SMCFG(timer_periph) |= (uint32_t)(extfilter << 8U);
  1567. }
  1568. /*!
  1569. \brief configure TIMER quadrature decoder mode
  1570. \param[in] timer_periph: TIMERx(x=0..4,7)
  1571. \param[in] decomode:
  1572. only one parameter can be selected which is shown as below:
  1573. \arg TIMER_QUAD_DECODER_MODE0: counter counts on CI0FE0 edge depending on CI1FE1 level
  1574. \arg TIMER_QUAD_DECODER_MODE1: counter counts on CI1FE1 edge depending on CI0FE0 level
  1575. \arg TIMER_QUAD_DECODER_MODE2: counter counts on both CI0FE0 and CI1FE1 edges depending on the level of the other input
  1576. \param[in] ic0polarity:
  1577. only one parameter can be selected which is shown as below:
  1578. \arg TIMER_IC_POLARITY_RISING: capture rising edge
  1579. \arg TIMER_IC_POLARITY_FALLING: capture falling edge
  1580. \param[in] ic1polarity:
  1581. only one parameter can be selected which is shown as below:
  1582. \arg TIMER_IC_POLARITY_RISING: capture rising edge
  1583. \arg TIMER_IC_POLARITY_FALLING: capture falling edge
  1584. \param[out] none
  1585. \retval none
  1586. */
  1587. void timer_quadrature_decoder_mode_config(uint32_t timer_periph, uint32_t decomode,
  1588. uint16_t ic0polarity, uint16_t ic1polarity)
  1589. {
  1590. TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_SMC);
  1591. TIMER_SMCFG(timer_periph) |= (uint32_t)decomode;
  1592. TIMER_CHCTL0(timer_periph) &= (uint32_t)(((~(uint32_t)TIMER_CHCTL0_CH0MS)) & ((~(uint32_t)TIMER_CHCTL0_CH1MS)));
  1593. TIMER_CHCTL0(timer_periph) |= (uint32_t)(TIMER_IC_SELECTION_DIRECTTI | ((uint32_t)TIMER_IC_SELECTION_DIRECTTI << 8U));
  1594. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P | TIMER_CHCTL2_CH0NP));
  1595. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P | TIMER_CHCTL2_CH1NP));
  1596. TIMER_CHCTL2(timer_periph) |= ((uint32_t)ic0polarity | ((uint32_t)ic1polarity << 4U));
  1597. }
  1598. /*!
  1599. \brief configure TIMER internal clock mode
  1600. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1601. \param[out] none
  1602. \retval none
  1603. */
  1604. void timer_internal_clock_config(uint32_t timer_periph)
  1605. {
  1606. TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_SMC;
  1607. }
  1608. /*!
  1609. \brief configure TIMER the internal trigger as external clock input
  1610. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1611. \param[in] intrigger:
  1612. only one parameter can be selected which is shown as below:
  1613. \arg TIMER_SMCFG_TRGSEL_ITI0: internal trigger 0
  1614. \arg TIMER_SMCFG_TRGSEL_ITI1: internal trigger 1
  1615. \arg TIMER_SMCFG_TRGSEL_ITI2: internal trigger 2
  1616. \arg TIMER_SMCFG_TRGSEL_ITI3: internal trigger 3
  1617. \param[out] none
  1618. \retval none
  1619. */
  1620. void timer_internal_trigger_as_external_clock_config(uint32_t timer_periph, uint32_t intrigger)
  1621. {
  1622. timer_input_trigger_source_select(timer_periph, intrigger);
  1623. TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_SMC;
  1624. TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SLAVE_MODE_EXTERNAL0;
  1625. }
  1626. /*!
  1627. \brief configure TIMER the external trigger as external clock input
  1628. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1629. \param[in] extrigger:
  1630. only one parameter can be selected which is shown as below:
  1631. \arg TIMER_SMCFG_TRGSEL_CI0F_ED: TI0 edge detector
  1632. \arg TIMER_SMCFG_TRGSEL_CI0FE0: filtered TIMER input 0
  1633. \arg TIMER_SMCFG_TRGSEL_CI1FE1: filtered TIMER input 1
  1634. \param[in] extpolarity:
  1635. only one parameter can be selected which is shown as below:
  1636. \arg TIMER_IC_POLARITY_RISING: active high or rising edge active
  1637. \arg TIMER_IC_POLARITY_FALLING: active low or falling edge active
  1638. \param[in] extfilter: a value between 0 and 15
  1639. \param[out] none
  1640. \retval none
  1641. */
  1642. void timer_external_trigger_as_external_clock_config(uint32_t timer_periph, uint32_t extrigger,
  1643. uint16_t extpolarity, uint32_t extfilter)
  1644. {
  1645. if(TIMER_SMCFG_TRGSEL_CI1FE1 == extrigger) {
  1646. /* reset the CH1EN bit */
  1647. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
  1648. /* reset the CH1NP bit */
  1649. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P | TIMER_CHCTL2_CH1NP));
  1650. /* set the CH1NP bit */
  1651. TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)extpolarity << 4U);
  1652. /* reset the CH1MS bit */
  1653. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
  1654. /* set the CH1MS bit */
  1655. TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)TIMER_IC_SELECTION_DIRECTTI << 8U);
  1656. /* reset the CH1CAPFLT bit */
  1657. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
  1658. /* set the CH1CAPFLT bit */
  1659. TIMER_CHCTL0(timer_periph) |= (uint32_t)(extfilter << 12U);
  1660. /* set the CH1EN bit */
  1661. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
  1662. } else {
  1663. /* reset the CH0EN bit */
  1664. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
  1665. /* reset the CH0P and CH0NP bits */
  1666. TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P | TIMER_CHCTL2_CH0NP));
  1667. /* set the CH0P and CH0NP bits */
  1668. TIMER_CHCTL2(timer_periph) |= (uint32_t)extpolarity;
  1669. /* reset the CH0MS bit */
  1670. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
  1671. /* set the CH0MS bit */
  1672. TIMER_CHCTL0(timer_periph) |= (uint32_t)TIMER_IC_SELECTION_DIRECTTI;
  1673. /* reset the CH0CAPFLT bit */
  1674. TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
  1675. /* reset the CH0CAPFLT bit */
  1676. TIMER_CHCTL0(timer_periph) |= (uint32_t)(extfilter << 4U);
  1677. /* set the CH0EN bit */
  1678. TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
  1679. }
  1680. /* select TIMER input trigger source */
  1681. timer_input_trigger_source_select(timer_periph, extrigger);
  1682. /* reset the SMC bit */
  1683. TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_SMC);
  1684. /* set the SMC bit */
  1685. TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SLAVE_MODE_EXTERNAL0;
  1686. }
  1687. /*!
  1688. \brief configure TIMER the external clock mode0
  1689. \param[in] timer_periph: TIMERx(x=0..4,7,8,11)
  1690. \param[in] extprescaler:
  1691. only one parameter can be selected which is shown as below:
  1692. \arg TIMER_EXT_TRI_PSC_OFF: no divided
  1693. \arg TIMER_EXT_TRI_PSC_DIV2: divided by 2
  1694. \arg TIMER_EXT_TRI_PSC_DIV4: divided by 4
  1695. \arg TIMER_EXT_TRI_PSC_DIV8: divided by 8
  1696. \param[in] extpolarity:
  1697. only one parameter can be selected which is shown as below:
  1698. \arg TIMER_ETP_FALLING: active low or falling edge active
  1699. \arg TIMER_ETP_RISING: active high or rising edge active
  1700. \param[in] extfilter: a value between 0 and 15
  1701. \param[out] none
  1702. \retval none
  1703. */
  1704. void timer_external_clock_mode0_config(uint32_t timer_periph, uint32_t extprescaler,
  1705. uint32_t extpolarity, uint32_t extfilter)
  1706. {
  1707. /* configure TIMER external trigger input */
  1708. timer_external_trigger_config(timer_periph, extprescaler, extpolarity, extfilter);
  1709. /* reset the SMC bit,TRGS bit */
  1710. TIMER_SMCFG(timer_periph) &= (~(uint32_t)(TIMER_SMCFG_SMC | TIMER_SMCFG_TRGS));
  1711. /* set the SMC bit,TRGS bit */
  1712. TIMER_SMCFG(timer_periph) |= (uint32_t)(TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_ETIFP);
  1713. }
  1714. /*!
  1715. \brief configure TIMER the external clock mode1
  1716. \param[in] timer_periph: TIMERx(x=0..4,7)
  1717. \param[in] extprescaler:
  1718. only one parameter can be selected which is shown as below:
  1719. \arg TIMER_EXT_TRI_PSC_OFF: no divided
  1720. \arg TIMER_EXT_TRI_PSC_DIV2: divided by 2
  1721. \arg TIMER_EXT_TRI_PSC_DIV4: divided by 4
  1722. \arg TIMER_EXT_TRI_PSC_DIV8: divided by 8
  1723. \param[in] extpolarity:
  1724. only one parameter can be selected which is shown as below:
  1725. \arg TIMER_ETP_FALLING: active low or falling edge active
  1726. \arg TIMER_ETP_RISING: active high or rising edge active
  1727. \param[in] extfilter: a value between 0 and 15
  1728. \param[out] none
  1729. \retval none
  1730. */
  1731. void timer_external_clock_mode1_config(uint32_t timer_periph, uint32_t extprescaler,
  1732. uint32_t extpolarity, uint32_t extfilter)
  1733. {
  1734. /* configure TIMER external trigger input */
  1735. timer_external_trigger_config(timer_periph, extprescaler, extpolarity, extfilter);
  1736. TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SMCFG_SMC1;
  1737. }
  1738. /*!
  1739. \brief disable TIMER the external clock mode1
  1740. \param[in] timer_periph: TIMERx(x=0..4,7)
  1741. \param[out] none
  1742. \retval none
  1743. */
  1744. void timer_external_clock_mode1_disable(uint32_t timer_periph)
  1745. {
  1746. TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_SMC1;
  1747. }
  1748. /*!
  1749. \brief configure TIMER channel remap function
  1750. \param[in] timer_periph: TIMERx(x=1,4,10)
  1751. \param[in] remap:
  1752. only one parameter can be selected which is shown as below:
  1753. \arg TIMER1_ITI1_RMP_TIMER7_TRGO: timer1 internal trigger input1 remap to TIMER7_TRGO
  1754. \arg TIMER1_ITI1_RMP_ETHERNET_PTP: timer1 internal trigger input1 remap to ethernet PTP
  1755. \arg TIMER1_ITI1_RMP_USB_FS_SOF: timer1 internal trigger input1 remap to USB FS SOF
  1756. \arg TIMER1_ITI1_RMP_USB_HS_SOF: timer1 internal trigger input1 remap to USB HS SOF
  1757. \arg TIMER4_CI3_RMP_GPIO: timer4 channel 3 input remap to GPIO pin
  1758. \arg TIMER4_CI3_RMP_IRC32K: timer4 channel 3 input remap to IRC32K
  1759. \arg TIMER4_CI3_RMP_LXTAL: timer4 channel 3 input remap to LXTAL
  1760. \arg TIMER4_CI3_RMP_RTC_WAKEUP_INT: timer4 channel 3 input remap to RTC wakeup interrupt
  1761. \arg TIMER10_ITI1_RMP_GPIO: timer10 internal trigger input1 remap based on GPIO setting
  1762. \arg TIMER10_ITI1_RMP_RTC_HXTAL_DIV: timer10 internal trigger input1 remap HXTAL _DIV(clock used for RTC which is HXTAL clock divided by RTCDIV bits in RCU_CFG0 register)
  1763. \param[out] none
  1764. \retval none
  1765. */
  1766. void timer_channel_remap_config(uint32_t timer_periph, uint32_t remap)
  1767. {
  1768. TIMER_IRMP(timer_periph) = (uint32_t)remap;
  1769. }
  1770. /*!
  1771. \brief configure TIMER write CHxVAL register selection
  1772. \param[in] timer_periph: TIMERx(x=0,1,2,13,14,15,16)
  1773. \param[in] ccsel:
  1774. only one parameter can be selected which is shown as below:
  1775. \arg TIMER_CHVSEL_DISABLE: no effect
  1776. \arg TIMER_CHVSEL_ENABLE: when write the CHxVAL register, if the write value is same as the CHxVAL value, the write access is ignored
  1777. \param[out] none
  1778. \retval none
  1779. */
  1780. void timer_write_chxval_register_config(uint32_t timer_periph, uint16_t ccsel)
  1781. {
  1782. if(TIMER_CHVSEL_ENABLE == ccsel) {
  1783. TIMER_CFG(timer_periph) |= (uint32_t)TIMER_CFG_CHVSEL;
  1784. } else if(TIMER_CHVSEL_DISABLE == ccsel) {
  1785. TIMER_CFG(timer_periph) &= ~(uint32_t)TIMER_CFG_CHVSEL;
  1786. } else {
  1787. /* illegal parameters */
  1788. }
  1789. }
  1790. /*!
  1791. \brief configure TIMER output value selection
  1792. \param[in] timer_periph: TIMERx(x=0,7)
  1793. \param[in] outsel:
  1794. only one parameter can be selected which is shown as below:
  1795. \arg TIMER_OUTSEL_DISABLE: no effect
  1796. \arg TIMER_OUTSEL_ENABLE: if POEN and IOS is 0, the output disabled
  1797. \param[out] none
  1798. \retval none
  1799. */
  1800. void timer_output_value_selection_config(uint32_t timer_periph, uint16_t outsel)
  1801. {
  1802. if(TIMER_OUTSEL_ENABLE == outsel) {
  1803. TIMER_CFG(timer_periph) |= (uint32_t)TIMER_CFG_OUTSEL;
  1804. } else if(TIMER_OUTSEL_DISABLE == outsel) {
  1805. TIMER_CFG(timer_periph) &= ~(uint32_t)TIMER_CFG_OUTSEL;
  1806. } else {
  1807. /* illegal parameters */
  1808. }
  1809. }
  1810. /*!
  1811. \brief get TIMER flags
  1812. \param[in] timer_periph: please refer to the following parameters
  1813. \param[in] flag: the timer interrupt flags
  1814. only one parameter can be selected which is shown as below:
  1815. \arg TIMER_FLAG_UP: update flag,TIMERx(x=0..13)
  1816. \arg TIMER_FLAG_CH0: channel 0 flag,TIMERx(x=0..4,7..13)
  1817. \arg TIMER_FLAG_CH1: channel 1 flag,TIMERx(x=0..4,7,8,11)
  1818. \arg TIMER_FLAG_CH2: channel 2 flag,TIMERx(x=0..4,7)
  1819. \arg TIMER_FLAG_CH3: channel 3 flag,TIMERx(x=0..4,7)
  1820. \arg TIMER_FLAG_CMT: channel control update flag,TIMERx(x=0,7)
  1821. \arg TIMER_FLAG_TRG: trigger flag,TIMERx(x=0,7,8,11)
  1822. \arg TIMER_FLAG_BRK: break flag,TIMERx(x=0,7)
  1823. \arg TIMER_FLAG_CH0O: channel 0 overcapture flag,TIMERx(x=0..4,7..11)
  1824. \arg TIMER_FLAG_CH1O: channel 1 overcapture flag,TIMERx(x=0..4,7,8,11)
  1825. \arg TIMER_FLAG_CH2O: channel 2 overcapture flag,TIMERx(x=0..4,7)
  1826. \arg TIMER_FLAG_CH3O: channel 3 overcapture flag,TIMERx(x=0..4,7)
  1827. \param[out] none
  1828. \retval FlagStatus: SET or RESET
  1829. */
  1830. FlagStatus timer_flag_get(uint32_t timer_periph, uint32_t flag)
  1831. {
  1832. if(RESET != (TIMER_INTF(timer_periph) & flag)) {
  1833. return SET;
  1834. } else {
  1835. return RESET;
  1836. }
  1837. }
  1838. /*!
  1839. \brief clear TIMER flags
  1840. \param[in] timer_periph: please refer to the following parameters
  1841. \param[in] flag: the timer interrupt flags
  1842. only one parameter can be selected which is shown as below:
  1843. \arg TIMER_FLAG_UP: update flag,TIMERx(x=0..13)
  1844. \arg TIMER_FLAG_CH0: channel 0 flag,TIMERx(x=0..4,7..13)
  1845. \arg TIMER_FLAG_CH1: channel 1 flag,TIMERx(x=0..4,7,8,11)
  1846. \arg TIMER_FLAG_CH2: channel 2 flag,TIMERx(x=0..4,7)
  1847. \arg TIMER_FLAG_CH3: channel 3 flag,TIMERx(x=0..4,7)
  1848. \arg TIMER_FLAG_CMT: channel control update flag,TIMERx(x=0,7)
  1849. \arg TIMER_FLAG_TRG: trigger flag,TIMERx(x=0,7,8,11)
  1850. \arg TIMER_FLAG_BRK: break flag,TIMERx(x=0,7)
  1851. \arg TIMER_FLAG_CH0O: channel 0 overcapture flag,TIMERx(x=0..4,7..11)
  1852. \arg TIMER_FLAG_CH1O: channel 1 overcapture flag,TIMERx(x=0..4,7,8,11)
  1853. \arg TIMER_FLAG_CH2O: channel 2 overcapture flag,TIMERx(x=0..4,7)
  1854. \arg TIMER_FLAG_CH3O: channel 3 overcapture flag,TIMERx(x=0..4,7)
  1855. \param[out] none
  1856. \retval none
  1857. */
  1858. void timer_flag_clear(uint32_t timer_periph, uint32_t flag)
  1859. {
  1860. TIMER_INTF(timer_periph) = (~(uint32_t)flag);
  1861. }
  1862. /*!
  1863. \brief enable the TIMER interrupt
  1864. \param[in] timer_periph: please refer to the following parameters
  1865. \param[in] interrupt: timer interrupt enable source
  1866. only one parameter can be selected which is shown as below:
  1867. \arg TIMER_INT_UP: update interrupt enable, TIMERx(x=0..13)
  1868. \arg TIMER_INT_CH0: channel 0 interrupt enable, TIMERx(x=0..4,7..13)
  1869. \arg TIMER_INT_CH1: channel 1 interrupt enable, TIMERx(x=0..4,7,8,11)
  1870. \arg TIMER_INT_CH2: channel 2 interrupt enable, TIMERx(x=0..4,7)
  1871. \arg TIMER_INT_CH3: channel 3 interrupt enable , TIMERx(x=0..4,7)
  1872. \arg TIMER_INT_CMT: commutation interrupt enable, TIMERx(x=0,7)
  1873. \arg TIMER_INT_TRG: trigger interrupt enable, TIMERx(x=0..4,7,8,11)
  1874. \arg TIMER_INT_BRK: break interrupt enable, TIMERx(x=0,7)
  1875. \param[out] none
  1876. \retval none
  1877. */
  1878. void timer_interrupt_enable(uint32_t timer_periph, uint32_t interrupt)
  1879. {
  1880. TIMER_DMAINTEN(timer_periph) |= (uint32_t) interrupt;
  1881. }
  1882. /*!
  1883. \brief disable the TIMER interrupt
  1884. \param[in] timer_periph: please refer to the following parameters
  1885. \param[in] interrupt: timer interrupt source enable
  1886. only one parameter can be selected which is shown as below:
  1887. \arg TIMER_INT_UP: update interrupt enable, TIMERx(x=0..13)
  1888. \arg TIMER_INT_CH0: channel 0 interrupt enable, TIMERx(x=0..4,7..13)
  1889. \arg TIMER_INT_CH1: channel 1 interrupt enable, TIMERx(x=0..4,7,8,11)
  1890. \arg TIMER_INT_CH2: channel 2 interrupt enable, TIMERx(x=0..4,7)
  1891. \arg TIMER_INT_CH3: channel 3 interrupt enable , TIMERx(x=0..4,7)
  1892. \arg TIMER_INT_CMT: commutation interrupt enable, TIMERx(x=0,7)
  1893. \arg TIMER_INT_TRG: trigger interrupt enable, TIMERx(x=0..4,7,8,11)
  1894. \arg TIMER_INT_BRK: break interrupt enable, TIMERx(x=0,7)
  1895. \param[out] none
  1896. \retval none
  1897. */
  1898. void timer_interrupt_disable(uint32_t timer_periph, uint32_t interrupt)
  1899. {
  1900. TIMER_DMAINTEN(timer_periph) &= (~(uint32_t)interrupt);
  1901. }
  1902. /*!
  1903. \brief get timer interrupt flag
  1904. \param[in] timer_periph: please refer to the following parameters
  1905. \param[in] interrupt: the timer interrupt bits
  1906. only one parameter can be selected which is shown as below:
  1907. \arg TIMER_INT_FLAG_UP: update interrupt flag,TIMERx(x=0..13)
  1908. \arg TIMER_INT_FLAG_CH0: channel 0 interrupt flag,TIMERx(x=0..4,7..13)
  1909. \arg TIMER_INT_FLAG_CH1: channel 1 interrupt flag,TIMERx(x=0..4,7,8,11)
  1910. \arg TIMER_INT_FLAG_CH2: channel 2 interrupt flag,TIMERx(x=0..4,7)
  1911. \arg TIMER_INT_FLAG_CH3: channel 3 interrupt flag,TIMERx(x=0..4,7)
  1912. \arg TIMER_INT_FLAG_CMT: channel commutation interrupt flag,TIMERx(x=0,7)
  1913. \arg TIMER_INT_FLAG_TRG: trigger interrupt flag,TIMERx(x=0,7,8,11)
  1914. \arg TIMER_INT_FLAG_BRK: break interrupt flag,TIMERx(x=0,7)
  1915. \param[out] none
  1916. \retval FlagStatus: SET or RESET
  1917. */
  1918. FlagStatus timer_interrupt_flag_get(uint32_t timer_periph, uint32_t interrupt)
  1919. {
  1920. uint32_t val;
  1921. val = (TIMER_DMAINTEN(timer_periph) & interrupt);
  1922. if((RESET != (TIMER_INTF(timer_periph) & interrupt)) && (RESET != val)) {
  1923. return SET;
  1924. } else {
  1925. return RESET;
  1926. }
  1927. }
  1928. /*!
  1929. \brief clear TIMER interrupt flag
  1930. \param[in] timer_periph: please refer to the following parameters
  1931. \param[in] interrupt: the timer interrupt bits
  1932. only one parameter can be selected which is shown as below:
  1933. \arg TIMER_INT_FLAG_UP: update interrupt flag,TIMERx(x=0..13)
  1934. \arg TIMER_INT_FLAG_CH0: channel 0 interrupt flag,TIMERx(x=0..4,7..13)
  1935. \arg TIMER_INT_FLAG_CH1: channel 1 interrupt flag,TIMERx(x=0..4,7,8,11)
  1936. \arg TIMER_INT_FLAG_CH2: channel 2 interrupt flag,TIMERx(x=0..4,7)
  1937. \arg TIMER_INT_FLAG_CH3: channel 3 interrupt flag,TIMERx(x=0..4,7)
  1938. \arg TIMER_INT_FLAG_CMT: channel commutation interrupt flag,TIMERx(x=0,7)
  1939. \arg TIMER_INT_FLAG_TRG: trigger interrupt flag,TIMERx(x=0,7,8,11)
  1940. \arg TIMER_INT_FLAG_BRK: break interrupt flag,TIMERx(x=0,7)
  1941. \param[out] none
  1942. \retval none
  1943. */
  1944. void timer_interrupt_flag_clear(uint32_t timer_periph, uint32_t interrupt)
  1945. {
  1946. TIMER_INTF(timer_periph) = (~(uint32_t)interrupt);
  1947. }