gd32f30x_timer.c 74 KB

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