gd32f30x_timer.c 83 KB

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