gd32vf103_timer.c 81 KB

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