gd32f4xx_timer.c 77 KB

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