gd32f3x0_timer.c 77 KB

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