gd32f30x_timer.c 84 KB

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