gd32f10x_timer.c 83 KB

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