gd32f3x0_timer.c 86 KB

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