ald_timer.c 119 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_timer.c
  5. * @brief TIMER module driver.
  6. * This is the common part of the TIMER initialization
  7. *
  8. * @version V1.0
  9. * @date 06 Nov 2017
  10. * @author AE Team
  11. * @note
  12. *
  13. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  14. *
  15. *********************************************************************************
  16. */
  17. #include <string.h>
  18. #include "ald_timer.h"
  19. #include "ald_cmu.h"
  20. /** @addtogroup ES32FXXX_ALD
  21. * @{
  22. */
  23. /** @defgroup TIMER TIMER
  24. * @brief TIMER module driver
  25. * @{
  26. */
  27. #ifdef ALD_TIMER
  28. /** @defgroup TIMER_Private_Functions TIMER Private Functions
  29. * @{
  30. */
  31. static void timer_base_set_config(TIMER_TypeDef *TIMERx, timer_base_init_t *init);
  32. static void timer_oc1_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
  33. static void timer_oc2_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
  34. static void timer_oc3_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
  35. static void timer_oc4_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
  36. static void timer_ccx_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state);
  37. static void timer_ccxn_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state);
  38. static void timer_ti1_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  39. timer_ic_select_t sel, uint32_t filter);
  40. static void timer_ti1_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter);
  41. static void timer_ti2_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  42. timer_ic_select_t sel, uint32_t filter);
  43. static void timer_ti2_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter);
  44. static void timer_ti3_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  45. timer_ic_select_t sel, uint32_t filter);
  46. static void timer_ti4_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  47. timer_ic_select_t sel, uint32_t filter);
  48. static void timer_etr_set_config(TIMER_TypeDef* TIMERx, timer_etr_psc_t psc, timer_clock_polarity_t polarity, uint32_t filter);
  49. static void timer_slave_set_config(timer_handle_t *hperh, timer_slave_config_t *config);
  50. #ifdef ALD_DMA
  51. static void timer_dma_oc_cplt(void *arg);
  52. static void timer_dma_capture_cplt(void *arg);
  53. static void timer_dma_period_elapse_cplt(void *arg);
  54. static void timer_dma_error(void *arg);
  55. #endif
  56. /**
  57. * @}
  58. */
  59. /** @defgroup TIMER_Public_Functions TIMER Public Functions
  60. * @{
  61. */
  62. /** @defgroup TIMER_Public_Functions_Group1 TIMER Base functions
  63. * @brief Time Base functions
  64. *
  65. * @verbatim
  66. ==============================================================================
  67. ##### Timer Base functions #####
  68. ==============================================================================
  69. [..]
  70. This section provides functions allowing to:
  71. (+) Initialize and configure the TIMER base.
  72. (+) Reset the TIMER base.
  73. (+) Start the Time Base.
  74. (+) Stop the Time Base.
  75. (+) Start the Time Base and enable interrupt.
  76. (+) Stop the Time Base and disable interrupt.
  77. (+) Start the Time Base and enable DMA transfer.
  78. (+) Stop the Time Base and disable DMA transfer.
  79. @endverbatim
  80. * @{
  81. */
  82. /**
  83. * @brief Initializes the TIMER Time base Unit according to the specified
  84. * parameters in the timer_handle_t and create the associated handle.
  85. * @param hperh: TIMER base handle
  86. * @retval Status, see @ref ald_status_t.
  87. */
  88. ald_status_t timer_base_init(timer_handle_t *hperh)
  89. {
  90. if (hperh == NULL)
  91. return ERROR;
  92. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  93. assert_param(IS_TIMER_COUNTER_MODE(hperh->init.mode));
  94. assert_param(IS_TIMER_CLOCK_DIVISION(hperh->init.clk_div));
  95. if (hperh->state == TIMER_STATE_RESET)
  96. hperh->lock = UNLOCK;
  97. hperh->state = TIMER_STATE_BUSY;
  98. timer_base_set_config(hperh->perh, &hperh->init);
  99. hperh->state = TIMER_STATE_READY;
  100. return OK;
  101. }
  102. /**
  103. * @brief Reset the TIMER base peripheral
  104. * @param hperh: TIMER base handle
  105. * @retval Status, see @ref ald_status_t.
  106. */
  107. void timer_base_reset(timer_handle_t *hperh)
  108. {
  109. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  110. hperh->state = TIMER_STATE_BUSY;
  111. TIMER_DISABLE(hperh);
  112. hperh->state = TIMER_STATE_RESET;
  113. __UNLOCK(hperh);
  114. return;
  115. }
  116. /**
  117. * @brief Starts the TIMER Base generation.
  118. * @param hperh: TIMER handle
  119. * @retval None
  120. */
  121. void timer_base_start(timer_handle_t *hperh)
  122. {
  123. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  124. hperh->state = TIMER_STATE_BUSY;
  125. TIMER_ENABLE(hperh);
  126. hperh->state = TIMER_STATE_READY;
  127. return;
  128. }
  129. /**
  130. * @brief Stops the TIMER Base generation.
  131. * @param hperh: TIMER handle
  132. * @retval None
  133. */
  134. void timer_base_stop(timer_handle_t *hperh)
  135. {
  136. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  137. hperh->state = TIMER_STATE_BUSY;
  138. TIMER_DISABLE(hperh);
  139. hperh->state = TIMER_STATE_READY;
  140. return;
  141. }
  142. /**
  143. * @brief Starts the TIMER Base generation in interrupt mode.
  144. * @param hperh: TIMER handle
  145. * @retval None
  146. */
  147. void timer_base_start_by_it(timer_handle_t *hperh)
  148. {
  149. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  150. timer_interrupt_config(hperh, TIMER_IT_UPDATE, ENABLE);
  151. TIMER_ENABLE(hperh);
  152. return;
  153. }
  154. /**
  155. * @brief Stops the TIMER Base generation in interrupt mode.
  156. * @param hperh: TIMER handle
  157. * @retval None
  158. */
  159. void timer_base_stop_by_it(timer_handle_t *hperh)
  160. {
  161. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  162. timer_interrupt_config(hperh, TIMER_IT_UPDATE, DISABLE);
  163. TIMER_DISABLE(hperh);
  164. return;
  165. }
  166. #ifdef ALD_DMA
  167. /**
  168. * @brief Starts the TIMER Base generation in DMA mode.
  169. * @param hperh: TIMER handle
  170. * @param hdma: Pointer to dma_handle_t.
  171. * @param buf: The source Buffer address.
  172. * @param len: The length of buffer to be transferred from memory to TIMER peripheral
  173. * @param dma_ch: Channel of DMA.
  174. * @retval Status, see @ref ald_status_t.
  175. */
  176. ald_status_t timer_base_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
  177. uint16_t *buf, uint32_t len, uint8_t dma_ch)
  178. {
  179. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  180. if ((hperh->state == TIMER_STATE_BUSY))
  181. return BUSY;
  182. if ((hperh->state == TIMER_STATE_READY)) {
  183. if (((uint32_t)buf == 0 ) || (len == 0))
  184. return ERROR;
  185. }
  186. hperh->state = TIMER_STATE_BUSY;
  187. if (hdma->perh == NULL)
  188. hdma->perh = DMA0;
  189. hdma->cplt_cbk = timer_dma_period_elapse_cplt;
  190. hdma->cplt_arg = (void *)hperh;
  191. hdma->err_cbk = timer_dma_error;
  192. hdma->err_arg = (void *)hperh;
  193. dma_config_struct(&hdma->config);
  194. hdma->config.src = (void *)buf;
  195. hdma->config.dst = (void *)&hperh->perh->AR;
  196. hdma->config.size = len;
  197. hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
  198. hdma->config.src_inc = DMA_DATA_INC_HALFWORD;
  199. hdma->config.dst_inc = DMA_DATA_INC_NONE;
  200. hdma->config.msigsel = DMA_MSIGSEL_TIMER_UPDATE;
  201. hdma->config.channel = dma_ch;
  202. if (hperh->perh == TIMER0)
  203. hdma->config.msel = DMA_MSEL_TIMER0;
  204. else if (hperh->perh == TIMER1)
  205. hdma->config.msel = DMA_MSEL_TIMER1;
  206. else if (hperh->perh == TIMER2)
  207. hdma->config.msel = DMA_MSEL_TIMER2;
  208. else if (hperh->perh == TIMER3)
  209. hdma->config.msel = DMA_MSEL_TIMER3;
  210. else if (hperh->perh == TIMER4)
  211. hdma->config.msel = DMA_MSEL_TIMER4;
  212. else if (hperh->perh == TIMER5)
  213. hdma->config.msel = DMA_MSEL_TIMER5;
  214. else if (hperh->perh == TIMER6)
  215. hdma->config.msel = DMA_MSEL_TIMER6;
  216. else if (hperh->perh == TIMER7)
  217. hdma->config.msel = DMA_MSEL_TIMER7;
  218. else
  219. ;
  220. dma_config_basic(hdma);
  221. timer_dma_req_config(hperh, TIMER_DMA_UPDATE, ENABLE);
  222. TIMER_ENABLE(hperh);
  223. return OK;
  224. }
  225. /**
  226. * @brief Stops the TIMER Base generation in DMA mode.
  227. * @param hperh: TIMER handle
  228. * @retval None
  229. */
  230. void timer_base_stop_by_dma(timer_handle_t *hperh)
  231. {
  232. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  233. timer_dma_req_config(hperh, TIMER_DMA_UPDATE, DISABLE);
  234. TIMER_DISABLE(hperh);
  235. hperh->state = TIMER_STATE_READY;
  236. return;
  237. }
  238. #endif
  239. /**
  240. * @}
  241. */
  242. /** @defgroup TIMER_Public_Functions_Group2 TIMER Output Compare functions
  243. * @brief Time Output Compare functions
  244. *
  245. * @verbatim
  246. ==============================================================================
  247. ##### Time Output Compare functions #####
  248. ==============================================================================
  249. [..]
  250. This section provides functions allowing to:
  251. (+) Initialize and configure the TIMER Output Compare.
  252. (+) Start the Time Output Compare.
  253. (+) Stop the Time Output Compare.
  254. (+) Start the Time Output Compare and enable interrupt.
  255. (+) Stop the Time Output Compare and disable interrupt.
  256. (+) Start the Time Output Compare and enable DMA transfer.
  257. (+) Stop the Time Output Compare and disable DMA transfer.
  258. @endverbatim
  259. * @{
  260. */
  261. /**
  262. * @brief Initializes the TIMER Output Compare according to the specified
  263. * parameters in the timer_handle_t and create the associated handle.
  264. * @param hperh: TIMER handle
  265. * @retval Status, see @ref ald_status_t.
  266. */
  267. ald_status_t timer_oc_init(timer_handle_t *hperh)
  268. {
  269. return timer_base_init(hperh);
  270. }
  271. /**
  272. * @brief Starts the TIMER Output Compare signal generation.
  273. * @param hperh: TIMER handle
  274. * @param ch : TIMER Channel to be enabled
  275. * This parameter can be one of the following values:
  276. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  277. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  278. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  279. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  280. * @retval None
  281. */
  282. void timer_oc_start(timer_handle_t *hperh, timer_channel_t ch)
  283. {
  284. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  285. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  286. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  287. TIMER_MOE_ENABLE(hperh);
  288. TIMER_ENABLE(hperh);
  289. return;
  290. }
  291. /**
  292. * @brief Stops the TIMER Output Compare signal generation.
  293. * @param hperh: TIMER handle
  294. * @param ch: TIMER Channel to be disabled
  295. * This parameter can be one of the following values:
  296. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  297. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  298. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  299. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  300. * @retval None
  301. */
  302. void timer_oc_stop(timer_handle_t *hperh, timer_channel_t ch)
  303. {
  304. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  305. timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
  306. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  307. TIMER_MOE_DISABLE(hperh);
  308. TIMER_DISABLE(hperh);
  309. hperh->state = TIMER_STATE_READY;
  310. return;
  311. }
  312. /**
  313. * @brief Starts the TIMER Output Compare signal generation in interrupt mode.
  314. * @param hperh: TIMER handle
  315. * @param ch: TIMER Channel to be enabled
  316. * This parameter can be one of the following values:
  317. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  318. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  319. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  320. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  321. * @retval None
  322. */
  323. void timer_oc_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  324. {
  325. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  326. switch (ch) {
  327. case TIMER_CHANNEL_1:
  328. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  329. break;
  330. case TIMER_CHANNEL_2:
  331. timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
  332. break;
  333. case TIMER_CHANNEL_3:
  334. timer_interrupt_config(hperh, TIMER_IT_CC3, ENABLE);
  335. break;
  336. case TIMER_CHANNEL_4:
  337. timer_interrupt_config(hperh, TIMER_IT_CC4, ENABLE);
  338. break;
  339. default:
  340. break;
  341. }
  342. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  343. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  344. TIMER_MOE_ENABLE(hperh);
  345. TIMER_ENABLE(hperh);
  346. return;
  347. }
  348. /**
  349. * @brief Stops the TIMER Output Compare signal generation in interrupt mode.
  350. * @param hperh: TIMER handle
  351. * @param ch: TIMER Channel to be disabled
  352. * This parameter can be one of the following values:
  353. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  354. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  355. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  356. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  357. * @retval None
  358. */
  359. void timer_oc_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  360. {
  361. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  362. switch (ch) {
  363. case TIMER_CHANNEL_1:
  364. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  365. break;
  366. case TIMER_CHANNEL_2:
  367. timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
  368. break;
  369. case TIMER_CHANNEL_3:
  370. timer_interrupt_config(hperh, TIMER_IT_CC3, DISABLE);
  371. break;
  372. case TIMER_CHANNEL_4:
  373. timer_interrupt_config(hperh, TIMER_IT_CC4, DISABLE);
  374. break;
  375. default:
  376. break;
  377. }
  378. timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
  379. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  380. TIMER_MOE_DISABLE(hperh);
  381. TIMER_DISABLE(hperh);
  382. hperh->state = TIMER_STATE_READY;
  383. return;
  384. }
  385. #ifdef ALD_DMA
  386. /**
  387. * @brief Starts the TIMER Output Compare signal generation in DMA mode.
  388. * @param hperh: TIMER handle
  389. * @param ch: TIMER Channels to be enabled
  390. * This parameter can be one of the following values:
  391. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  392. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  393. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  394. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  395. * @param hdma: Pointer to dma_handle_t.
  396. * @param buf: The source Buffer address.
  397. * @param len: The length of buffer to be transferred from memory to TIMER peripheral
  398. * @param dma_ch: Channel of DMA.
  399. * @retval Status, see @ref ald_status_t.
  400. */
  401. ald_status_t timer_oc_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
  402. dma_handle_t *hdma, uint16_t *buf, uint32_t len, uint8_t dma_ch)
  403. {
  404. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  405. if ((hperh->state == TIMER_STATE_BUSY))
  406. return BUSY;
  407. if ((hperh->state == TIMER_STATE_READY)) {
  408. if (((uint32_t)buf == 0 ) || (len == 0))
  409. return ERROR;
  410. }
  411. hperh->state = TIMER_STATE_BUSY;
  412. if (hdma->perh == NULL)
  413. hdma->perh = DMA0;
  414. hdma->cplt_cbk = timer_dma_oc_cplt;
  415. hdma->cplt_arg = (void *)hperh;
  416. hdma->err_cbk = timer_dma_error;
  417. hdma->err_arg = (void *)hperh;
  418. dma_config_struct(&hdma->config);
  419. hdma->config.src = (void *)buf;
  420. hdma->config.size = len;
  421. hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
  422. hdma->config.src_inc = DMA_DATA_INC_HALFWORD;
  423. hdma->config.dst_inc = DMA_DATA_INC_NONE;
  424. hdma->config.channel = dma_ch;
  425. if (hperh->perh == TIMER0)
  426. hdma->config.msel = DMA_MSEL_TIMER0;
  427. else if (hperh->perh == TIMER1)
  428. hdma->config.msel = DMA_MSEL_TIMER1;
  429. else if (hperh->perh == TIMER2)
  430. hdma->config.msel = DMA_MSEL_TIMER2;
  431. else if (hperh->perh == TIMER3)
  432. hdma->config.msel = DMA_MSEL_TIMER3;
  433. else if (hperh->perh == TIMER4)
  434. hdma->config.msel = DMA_MSEL_TIMER4;
  435. else if (hperh->perh == TIMER5)
  436. hdma->config.msel = DMA_MSEL_TIMER5;
  437. else if (hperh->perh == TIMER6)
  438. hdma->config.msel = DMA_MSEL_TIMER6;
  439. else if (hperh->perh == TIMER7)
  440. hdma->config.msel = DMA_MSEL_TIMER7;
  441. else
  442. ;//do nothing
  443. switch (ch) {
  444. case TIMER_CHANNEL_1:
  445. hdma->config.dst = (void *)&hperh->perh->CCVAL1;
  446. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
  447. dma_config_basic(hdma);
  448. timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
  449. hperh->ch = TIMER_ACTIVE_CHANNEL_1;
  450. break;
  451. case TIMER_CHANNEL_2:
  452. hdma->config.dst = (void *)&hperh->perh->CCVAL2;
  453. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
  454. dma_config_basic(hdma);
  455. timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
  456. hperh->ch = TIMER_ACTIVE_CHANNEL_2;
  457. break;
  458. case TIMER_CHANNEL_3:
  459. hdma->config.dst = (void *)&hperh->perh->CCVAL3;
  460. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH3;
  461. dma_config_basic(hdma);
  462. timer_dma_req_config(hperh, TIMER_DMA_CC3, ENABLE);
  463. hperh->ch = TIMER_ACTIVE_CHANNEL_3;
  464. break;
  465. case TIMER_CHANNEL_4:
  466. hdma->config.dst = (void *)&hperh->perh->CCVAL4;
  467. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH4;
  468. dma_config_basic(hdma);
  469. timer_dma_req_config(hperh, TIMER_DMA_CC4, ENABLE);
  470. hperh->ch = TIMER_ACTIVE_CHANNEL_4;
  471. break;
  472. default:
  473. break;
  474. }
  475. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  476. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  477. TIMER_MOE_ENABLE(hperh);
  478. TIMER_ENABLE(hperh);
  479. return OK;
  480. }
  481. /**
  482. * @brief Stops the TIMER Output Compare signal generation in DMA mode.
  483. * @param hperh: TIMER handle
  484. * @param ch: TIMER Channels to be disabled
  485. * This parameter can be one of the following values:
  486. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  487. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  488. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  489. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  490. * @retval None
  491. */
  492. void timer_oc_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
  493. {
  494. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  495. switch (ch) {
  496. case TIMER_CHANNEL_1:
  497. timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
  498. break;
  499. case TIMER_CHANNEL_2:
  500. timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
  501. break;
  502. case TIMER_CHANNEL_3:
  503. timer_dma_req_config(hperh, TIMER_DMA_CC3, DISABLE);
  504. break;
  505. case TIMER_CHANNEL_4:
  506. timer_dma_req_config(hperh, TIMER_DMA_CC4, DISABLE);
  507. break;
  508. default:
  509. break;
  510. }
  511. timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
  512. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  513. TIMER_MOE_DISABLE(hperh);
  514. TIMER_DISABLE(hperh);
  515. hperh->state = TIMER_STATE_READY;
  516. return;
  517. }
  518. #endif
  519. /**
  520. * @}
  521. */
  522. /** @defgroup TIMER_Public_Functions_Group3 TIMER PWM functions
  523. * @brief TIMER PWM functions
  524. *
  525. * @verbatim
  526. ==============================================================================
  527. ##### Time PWM functions #####
  528. ==============================================================================
  529. [..]
  530. This section provides functions allowing to:
  531. (+) Initialize and configure the TIMER PWM.
  532. (+) Start the Time PWM.
  533. (+) Stop the Time PWM.
  534. (+) Start the Time PWM and enable interrupt.
  535. (+) Stop the Time PWM and disable interrupt.
  536. (+) Start the Time PWM and enable DMA transfer.
  537. (+) Stop the Time PWM and disable DMA transfer.
  538. @endverbatim
  539. * @{
  540. */
  541. /**
  542. * @brief Initializes the TIMER PWM Time Base according to the specified
  543. * parameters in the timer_handle_t and create the associated handle.
  544. * @param hperh: TIMER handle
  545. * @retval Status, see @ref ald_status_t.
  546. */
  547. ald_status_t timer_pwm_init(timer_handle_t *hperh)
  548. {
  549. return timer_base_init(hperh);
  550. }
  551. /**
  552. * @brief Starts the PWM signal generation.
  553. * @param hperh: TIMER handle
  554. * @param ch: TIMER Channels to be enabled
  555. * This parameter can be one of the following values:
  556. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  557. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  558. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  559. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  560. * @retval None
  561. */
  562. void timer_pwm_start(timer_handle_t *hperh, timer_channel_t ch)
  563. {
  564. timer_oc_start(hperh, ch);
  565. return;
  566. }
  567. /**
  568. * @brief Stops the PWM signal generation.
  569. * @param hperh: TIMER handle
  570. * @param ch: TIMER Channels to be disabled
  571. * This parameter can be one of the following values:
  572. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  573. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  574. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  575. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  576. * @retval None
  577. */
  578. void timer_pwm_stop(timer_handle_t *hperh, timer_channel_t ch)
  579. {
  580. timer_oc_stop(hperh, ch);
  581. return;
  582. }
  583. /**
  584. * @brief Starts the PWM signal generation in interrupt mode.
  585. * @param hperh: TIMER handle
  586. * @param ch: TIMER Channel to be disabled
  587. * This parameter can be one of the following values:
  588. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  589. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  590. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  591. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  592. * @retval None
  593. */
  594. void timer_pwm_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  595. {
  596. timer_oc_start_by_it(hperh, ch);
  597. return;
  598. }
  599. /**
  600. * @brief Stops the PWM signal generation in interrupt mode.
  601. * @param hperh: TIMER handle
  602. * @param ch: TIMER Channels to be disabled
  603. * This parameter can be one of the following values:
  604. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  605. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  606. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  607. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  608. * @retval None
  609. */
  610. void timer_pwm_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  611. {
  612. timer_oc_stop_by_it(hperh, ch);
  613. return;
  614. }
  615. #ifdef ALD_DMA
  616. /**
  617. * @brief Starts the TIMER PWM signal generation in DMA mode.
  618. * @param hperh: TIMER handle
  619. * @param ch: TIMER Channels to be enabled
  620. * This parameter can be one of the following values:
  621. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  622. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  623. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  624. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  625. * @param hdma: Pointer to dma_handle_t.
  626. * @param buf: The source Buffer address.
  627. * @param len: The length of buffer to be transferred from memory to TIMER peripheral
  628. * @param dma_ch: Channel of DMA.
  629. * @retval Status, see @ref ald_status_t.
  630. */
  631. ald_status_t timer_pwm_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
  632. dma_handle_t *hdma, uint16_t *buf, uint32_t len, uint8_t dma_ch)
  633. {
  634. return timer_oc_start_by_dma(hperh, ch, hdma, buf, len, dma_ch);
  635. }
  636. /**
  637. * @brief Stops the TIMER PWM signal generation in DMA mode.
  638. * @param hperh: TIMER handle
  639. * @param ch: TIMER Channels to be disabled
  640. * This parameter can be one of the following values:
  641. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  642. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  643. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  644. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  645. * @retval None
  646. */
  647. void timer_pwm_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
  648. {
  649. timer_oc_stop_by_dma(hperh, ch);
  650. return;
  651. }
  652. #endif
  653. /**
  654. * @brief Set the PWM freq.
  655. * @param hperh: TIMER handle
  656. * @param freq: PWM freq to set
  657. * @retval None
  658. */
  659. void timer_pwm_set_freq(timer_handle_t *hperh, uint16_t freq)
  660. {
  661. uint32_t _arr = cmu_get_pclk1_clock() / (hperh->init.prescaler + 1) / freq - 1;
  662. WRITE_REG(hperh->perh->AR, _arr);
  663. hperh->init.period = _arr;
  664. }
  665. /**
  666. * @brief Set the PWM duty.
  667. * @param hperh: TIMER handle
  668. * @param ch: TIMER Channels to be enabled
  669. * This parameter can be one of the following values:
  670. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  671. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  672. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  673. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  674. * @param duty: PWM duty to set
  675. * @retval None
  676. */
  677. void timer_pwm_set_duty(timer_handle_t *hperh, timer_channel_t ch, uint16_t duty)
  678. {
  679. uint32_t tmp = (hperh->init.period + 1) * duty / 100 - 1;
  680. if (ch == TIMER_CHANNEL_1)
  681. WRITE_REG(hperh->perh->CCVAL1, tmp);
  682. else if (ch == TIMER_CHANNEL_2)
  683. WRITE_REG(hperh->perh->CCVAL2, tmp);
  684. else if (ch == TIMER_CHANNEL_3)
  685. WRITE_REG(hperh->perh->CCVAL3, tmp);
  686. else if (ch == TIMER_CHANNEL_4)
  687. WRITE_REG(hperh->perh->CCVAL4, tmp);
  688. else {
  689. ;/* do nothing */
  690. }
  691. }
  692. /**
  693. * @brief Set capture the PWM.
  694. * @param hperh: TIMER handle
  695. * @param ch: TIMER Channels to be captured the PWM
  696. * This parameter can be one of the following values:
  697. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  698. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  699. * @retval None
  700. */
  701. void timer_pwm_set_input(timer_handle_t *hperh, timer_channel_t ch)
  702. {
  703. assert_param(IS_TIMER_PWM_INPUT_INSTANCE(hperh->perh, ch));
  704. CLEAR_BIT(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK);
  705. switch (ch) {
  706. case TIMER_CHANNEL_1:
  707. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, TIMER_IC_SEL_DIRECT << TIMER_CHMR1_CC1SSEL_POSS);
  708. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, TIMER_IC_SEL_INDIRECT << TIMER_CHMR1_CC2SSEL_POSS);
  709. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1POL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC1POL_POS);
  710. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1NPOL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC1NPOL_POS);
  711. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2POL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC2POL_POS);
  712. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2NPOL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC2NPOL_POS);
  713. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1FP1 << TIMER_SMCON_TSSEL_POSS);
  714. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_RESET << TIMER_SMCON_SMODS_POSS);
  715. break;
  716. case TIMER_CHANNEL_2:
  717. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, TIMER_IC_SEL_INDIRECT << TIMER_CHMR1_CC1SSEL_POSS);
  718. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, TIMER_IC_SEL_DIRECT << TIMER_CHMR1_CC2SSEL_POSS);
  719. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1POL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC1POL_POS);
  720. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1NPOL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC1NPOL_POS);
  721. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2POL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC2POL_POS);
  722. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2NPOL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC2NPOL_POS);
  723. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI2FP2 << TIMER_SMCON_TSSEL_POSS);
  724. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_RESET << TIMER_SMCON_SMODS_POSS);
  725. break;
  726. default:
  727. break;
  728. }
  729. SET_BIT(hperh->perh->CCEP, TIMER_CCEP_CC1EN_MSK);
  730. SET_BIT(hperh->perh->CCEP, TIMER_CCEP_CC2EN_MSK);
  731. return;
  732. }
  733. /**
  734. * @}
  735. */
  736. /** @defgroup TIMER_Public_Functions_Group4 TIMER Input Capture functions
  737. * @brief Time Input Capture functions
  738. *
  739. * @verbatim
  740. ==============================================================================
  741. ##### Time Input Capture functions #####
  742. ==============================================================================
  743. [..]
  744. This section provides functions allowing to:
  745. (+) Initialize and configure the TIMER Input Capture.
  746. (+) Start the Time Input Capture.
  747. (+) Stop the Time Input Capture.
  748. (+) Start the Time Input Capture and enable interrupt.
  749. (+) Stop the Time Input Capture and disable interrupt.
  750. (+) Start the Time Input Capture and enable DMA transfer.
  751. (+) Stop the Time Input Capture and disable DMA transfer.
  752. * @endverbatim
  753. * @{
  754. */
  755. /**
  756. * @brief Initializes the TIMER Input Capture Time base according to the specified
  757. * parameters in the timer_handle_t and create the associated handle.
  758. * @param hperh: TIMER handle
  759. * @retval Status, see @ref ald_status_t.
  760. */
  761. ald_status_t timer_ic_init(timer_handle_t *hperh)
  762. {
  763. return timer_base_init(hperh);
  764. }
  765. /**
  766. * @brief Starts the TIMER Input Capture measurement.
  767. * @param hperh: TIMER handle
  768. * @param ch: TIMER Channels to be enabled
  769. * This parameter can be one of the following values:
  770. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  771. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  772. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  773. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  774. * @retval None
  775. */
  776. void timer_ic_start(timer_handle_t *hperh, timer_channel_t ch)
  777. {
  778. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  779. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  780. TIMER_ENABLE(hperh);
  781. return;
  782. }
  783. /**
  784. * @brief Stops the TIMER Input Capture measurement.
  785. * @param hperh: TIMER handle
  786. * @param ch: TIMER Channels to be disabled
  787. * This parameter can be one of the following values:
  788. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  789. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  790. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  791. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  792. * @retval None
  793. */
  794. void timer_ic_stop(timer_handle_t *hperh, timer_channel_t ch)
  795. {
  796. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  797. timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
  798. TIMER_DISABLE(hperh);
  799. return;
  800. }
  801. /**
  802. * @brief Starts the TIMER Input Capture measurement in interrupt mode.
  803. * @param hperh: TIMER handle
  804. * @param ch: TIMER Channels to be enabled
  805. * This parameter can be one of the following values:
  806. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  807. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  808. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  809. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  810. * @retval None
  811. */
  812. void timer_ic_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  813. {
  814. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  815. switch (ch) {
  816. case TIMER_CHANNEL_1:
  817. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  818. break;
  819. case TIMER_CHANNEL_2:
  820. timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
  821. break;
  822. case TIMER_CHANNEL_3:
  823. timer_interrupt_config(hperh, TIMER_IT_CC3, ENABLE);
  824. break;
  825. case TIMER_CHANNEL_4:
  826. timer_interrupt_config(hperh, TIMER_IT_CC4, ENABLE);
  827. break;
  828. default:
  829. break;
  830. }
  831. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  832. TIMER_ENABLE(hperh);
  833. return;
  834. }
  835. /**
  836. * @brief Stops the TIMER Input Capture measurement in interrupt mode.
  837. * @param hperh: TIMER handle
  838. * @param ch: TIMER Channels to be disabled
  839. * This parameter can be one of the following values:
  840. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  841. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  842. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  843. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  844. * @retval None
  845. */
  846. void timer_ic_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  847. {
  848. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  849. switch (ch) {
  850. case TIMER_CHANNEL_1:
  851. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  852. break;
  853. case TIMER_CHANNEL_2:
  854. timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
  855. break;
  856. case TIMER_CHANNEL_3:
  857. timer_interrupt_config(hperh, TIMER_IT_CC3, DISABLE);
  858. break;
  859. case TIMER_CHANNEL_4:
  860. timer_interrupt_config(hperh, TIMER_IT_CC4, DISABLE);
  861. break;
  862. default:
  863. break;
  864. }
  865. timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
  866. TIMER_DISABLE(hperh);
  867. return;
  868. }
  869. #ifdef ALD_DMA
  870. /**
  871. * @brief Starts the TIMER Input Capture measurement in DMA mode.
  872. * @param hperh: TIMER handle
  873. * @param ch: TIMER Channels to be enabled
  874. * This parameter can be one of the following values:
  875. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  876. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  877. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  878. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  879. * @param hdma: Pointer to dma_handle_t.
  880. * @param buf: The destination Buffer address.
  881. * @param len: The length of buffer to be transferred TIMER peripheral to memory
  882. * @param dma_ch: Channel of DMA.
  883. * @retval Status, see @ref ald_status_t.
  884. */
  885. ald_status_t timer_ic_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
  886. dma_handle_t *hdma, uint16_t *buf, uint32_t len, uint8_t dma_ch)
  887. {
  888. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  889. if ((hperh->state == TIMER_STATE_BUSY))
  890. return BUSY;
  891. if ((hperh->state == TIMER_STATE_READY)) {
  892. if (((uint32_t)buf == 0 ) || (len == 0))
  893. return ERROR;
  894. }
  895. hperh->state = TIMER_STATE_BUSY;
  896. if (hdma->perh == NULL)
  897. hdma->perh = DMA0;
  898. hdma->cplt_cbk = timer_dma_capture_cplt;
  899. hdma->cplt_arg = (void *)hperh;
  900. hdma->err_cbk = timer_dma_error;
  901. hdma->err_arg = (void *)hperh;
  902. dma_config_struct(&hdma->config);
  903. hdma->config.dst = (void *)buf;
  904. hdma->config.size = len;
  905. hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
  906. hdma->config.src_inc = DMA_DATA_INC_NONE;
  907. hdma->config.dst_inc = DMA_DATA_INC_HALFWORD;
  908. hdma->config.channel = dma_ch;
  909. if (hperh->perh == TIMER0)
  910. hdma->config.msel = DMA_MSEL_TIMER0;
  911. else if (hperh->perh == TIMER1)
  912. hdma->config.msel = DMA_MSEL_TIMER1;
  913. else if (hperh->perh == TIMER2)
  914. hdma->config.msel = DMA_MSEL_TIMER2;
  915. else if (hperh->perh == TIMER3)
  916. hdma->config.msel = DMA_MSEL_TIMER3;
  917. else if (hperh->perh == TIMER4)
  918. hdma->config.msel = DMA_MSEL_TIMER4;
  919. else if (hperh->perh == TIMER5)
  920. hdma->config.msel = DMA_MSEL_TIMER5;
  921. else if (hperh->perh == TIMER6)
  922. hdma->config.msel = DMA_MSEL_TIMER6;
  923. else if (hperh->perh == TIMER7)
  924. hdma->config.msel = DMA_MSEL_TIMER7;
  925. else
  926. ;/* do nothing */
  927. switch (ch) {
  928. case TIMER_CHANNEL_1:
  929. hdma->config.src = (void *)&hperh->perh->CCVAL1;
  930. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
  931. dma_config_basic(hdma);
  932. timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
  933. hperh->ch = TIMER_ACTIVE_CHANNEL_1;
  934. break;
  935. case TIMER_CHANNEL_2:
  936. hdma->config.src = (void *)&hperh->perh->CCVAL2;
  937. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
  938. dma_config_basic(hdma);
  939. timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
  940. hperh->ch = TIMER_ACTIVE_CHANNEL_2;
  941. break;
  942. case TIMER_CHANNEL_3:
  943. hdma->config.src = (void *)&hperh->perh->CCVAL3;
  944. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH3;
  945. dma_config_basic(hdma);
  946. timer_dma_req_config(hperh, TIMER_DMA_CC3, ENABLE);
  947. hperh->ch = TIMER_ACTIVE_CHANNEL_3;
  948. break;
  949. case TIMER_CHANNEL_4:
  950. hdma->config.src = (void *)&hperh->perh->CCVAL4;
  951. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH4;
  952. dma_config_basic(hdma);
  953. timer_dma_req_config(hperh, TIMER_DMA_CC4, ENABLE);
  954. hperh->ch = TIMER_ACTIVE_CHANNEL_4;
  955. break;
  956. default:
  957. break;
  958. }
  959. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  960. TIMER_ENABLE(hperh);
  961. return OK;
  962. }
  963. /**
  964. * @brief Stops the TIMER Input Capture measurement in DMA mode.
  965. * @param hperh: TIMER handle
  966. * @param ch: TIMER Channels to be disabled
  967. * This parameter can be one of the following values:
  968. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  969. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  970. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  971. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  972. * @retval None
  973. */
  974. void timer_ic_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
  975. {
  976. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  977. switch (ch) {
  978. case TIMER_CHANNEL_1:
  979. timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
  980. break;
  981. case TIMER_CHANNEL_2:
  982. timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
  983. break;
  984. case TIMER_CHANNEL_3:
  985. timer_dma_req_config(hperh, TIMER_DMA_CC3, DISABLE);
  986. break;
  987. case TIMER_CHANNEL_4:
  988. timer_dma_req_config(hperh, TIMER_DMA_CC4, DISABLE);
  989. break;
  990. default:
  991. break;
  992. }
  993. timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
  994. TIMER_DISABLE(hperh);
  995. hperh->state = TIMER_STATE_READY;
  996. return;
  997. }
  998. #endif
  999. /**
  1000. * @}
  1001. */
  1002. /** @defgroup TIMER_Public_Functions_Group5 TIMER One Pulse functions
  1003. * @brief Time One Pulse functions
  1004. *
  1005. * @verbatim
  1006. ==============================================================================
  1007. ##### Time One Pulse functions #####
  1008. ==============================================================================
  1009. [..]
  1010. This section provides functions allowing to:
  1011. (+) Initialize and configure the TIMER One Pulse.
  1012. (+) Start the Time One Pulse.
  1013. (+) Stop the Time One Pulse.
  1014. (+) Start the Time One Pulse and enable interrupt.
  1015. (+) Stop the Time One Pulse and disable interrupt.
  1016. (+) Start the Time One Pulse and enable DMA transfer.
  1017. (+) Stop the Time One Pulse and disable DMA transfer.
  1018. * @endverbatim
  1019. * @{
  1020. */
  1021. /**
  1022. * @brief Initializes the TIMER One Pulse Time Base according to the specified
  1023. * parameters in the timer_handle_t and create the associated handle.
  1024. * @param hperh: TIMER handle
  1025. * @param mode: Select the One pulse mode.
  1026. * This parameter can be one of the following values:
  1027. * @arg TIMER_OP_MODE_SINGLE: Only one pulse will be generated.
  1028. * @arg TIMER_OP_MODE_REPEAT: Repetitive pulses wil be generated.
  1029. * @retval Status, see @ref ald_status_t.
  1030. */
  1031. ald_status_t timer_one_pulse_init(timer_handle_t *hperh, timer_op_mode_t mode)
  1032. {
  1033. if (hperh == NULL)
  1034. return ERROR;
  1035. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  1036. assert_param(IS_TIMER_COUNTER_MODE(hperh->init.mode));
  1037. assert_param(IS_TIMER_CLOCK_DIVISION(hperh->init.clk_div));
  1038. assert_param(IS_TIMER_OP_MODE(mode));
  1039. if (hperh->state == TIMER_STATE_RESET)
  1040. hperh->lock = UNLOCK;
  1041. hperh->state = TIMER_STATE_BUSY;
  1042. timer_base_set_config(hperh->perh, &hperh->init);
  1043. MODIFY_REG(hperh->perh->CON1, TIMER_CON1_SPMEN_MSK, mode << TIMER_CON1_SPMEN_POS);
  1044. hperh->state = TIMER_STATE_READY;
  1045. return OK;
  1046. }
  1047. /**
  1048. * @brief Starts the TIMER One Pulse signal generation.
  1049. * @param hperh: TIMER One Pulse handle
  1050. * @param ch: TIMER Channels to be enabled
  1051. * This parameter can be one of the following values:
  1052. * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
  1053. * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
  1054. * @retval None
  1055. */
  1056. void timer_one_pulse_start(timer_handle_t *hperh, timer_op_output_channel_t ch)
  1057. {
  1058. /* Enable the Capture compare and the Input Capture channels
  1059. * (in the OPM Mode the two possible channels that can be used are TIMER_CHANNEL_1 and TIMER_CHANNEL_2)
  1060. * if TIMER_CHANNEL_1 is used as output, the TIMER_CHANNEL_2 will be used as input and
  1061. * if TIMER_CHANNEL_1 is used as input, the TIMER_CHANNEL_2 will be used as output
  1062. * in all combinations, the TIMER_CHANNEL_1 and TIMER_CHANNEL_2 should be enabled together
  1063. */
  1064. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1065. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1066. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  1067. TIMER_MOE_ENABLE(hperh);
  1068. return;
  1069. }
  1070. /**
  1071. * @brief Stops the TIMER One Pulse signal generation.
  1072. * @param hperh: TIMER handle
  1073. * @param ch: TIMER Channels to be enabled
  1074. * This parameter can be one of the following values:
  1075. * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
  1076. * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
  1077. * @retval None
  1078. */
  1079. void timer_one_pulse_stop(timer_handle_t *hperh, timer_op_output_channel_t ch)
  1080. {
  1081. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1082. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1083. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  1084. TIMER_MOE_DISABLE(hperh);
  1085. TIMER_DISABLE(hperh);
  1086. return;
  1087. }
  1088. /**
  1089. * @brief Starts the TIMER One Pulse signal generation in interrupt mode.
  1090. * @param hperh: TIMER handle
  1091. * @param ch: TIMER Channels to be enabled
  1092. * This parameter can be one of the following values:
  1093. * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
  1094. * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
  1095. * @retval None
  1096. */
  1097. void timer_one_pulse_start_by_it(timer_handle_t *hperh, timer_op_output_channel_t ch)
  1098. {
  1099. /* Enable the Capture compare and the Input Capture channels
  1100. * (in the OPM Mode the two possible channels that can be used are TIMER_CHANNEL_1 and TIMER_CHANNEL_2)
  1101. * if TIMER_CHANNEL_1 is used as output, the TIMER_CHANNEL_2 will be used as input and
  1102. * if TIMER_CHANNEL_1 is used as input, the TIMER_CHANNEL_2 will be used as output
  1103. * in all combinations, the TIMER_CHANNEL_1 and TIMER_CHANNEL_2 should be enabled together
  1104. */
  1105. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  1106. timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
  1107. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1108. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1109. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  1110. TIMER_MOE_ENABLE(hperh);
  1111. return;
  1112. }
  1113. /**
  1114. * @brief Stops the TIMER One Pulse signal generation in interrupt mode.
  1115. * @param hperh : TIMER handle
  1116. * @param ch: TIMER Channels to be enabled
  1117. * This parameter can be one of the following values:
  1118. * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
  1119. * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
  1120. * @retval None
  1121. */
  1122. void timer_one_pulse_stop_by_it(timer_handle_t *hperh, timer_op_output_channel_t ch)
  1123. {
  1124. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  1125. timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
  1126. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1127. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1128. if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
  1129. TIMER_MOE_DISABLE(hperh);
  1130. TIMER_DISABLE(hperh);
  1131. return;
  1132. }
  1133. /**
  1134. * @}
  1135. */
  1136. /** @defgroup TIMER_Public_Functions_Group6 TIMER Encoder functions
  1137. * @brief TIMER Encoder functions
  1138. *
  1139. * @verbatim
  1140. ==============================================================================
  1141. ##### Time Encoder functions #####
  1142. ==============================================================================
  1143. [..]
  1144. This section provides functions allowing to:
  1145. (+) Initialize and configure the TIMER Encoder.
  1146. (+) Start the Time Encoder.
  1147. (+) Stop the Time Encoder.
  1148. (+) Start the Time Encoder and enable interrupt.
  1149. (+) Stop the Time Encoder and disable interrupt.
  1150. (+) Start the Time Encoder and enable DMA transfer.
  1151. (+) Stop the Time Encoder and disable DMA transfer.
  1152. * @endverbatim
  1153. * @{
  1154. */
  1155. /**
  1156. * @brief Initializes the TIMER Encoder Interface and create the associated handle.
  1157. * @param hperh: TIMER handle
  1158. * @param config: TIMER Encoder Interface configuration structure
  1159. * @retval Status, see @ref ald_status_t.
  1160. */
  1161. ald_status_t timer_encoder_init(timer_handle_t *hperh, timer_encoder_init_t *config)
  1162. {
  1163. if (hperh == NULL)
  1164. return ERROR;
  1165. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1166. assert_param(IS_TIMER_ENCODER_MODE(config->mode));
  1167. assert_param(IS_TIMER_IC_POLARITY(config->ic1_polarity));
  1168. assert_param(IS_TIMER_IC_POLARITY(config->ic2_polarity));
  1169. assert_param(IS_TIMER_IC_SELECT(config->ic1_sel));
  1170. assert_param(IS_TIMER_IC_SELECT(config->ic2_sel));
  1171. assert_param(IS_TIMER_IC_PSC(config->ic1_psc));
  1172. assert_param(IS_TIMER_IC_PSC(config->ic2_psc));
  1173. assert_param(IS_TIMER_IC_FILTER(config->ic1_filter));
  1174. assert_param(IS_TIMER_IC_FILTER(config->ic2_filter));
  1175. if (hperh->state == TIMER_STATE_RESET)
  1176. hperh->lock = UNLOCK;
  1177. hperh->state = TIMER_STATE_BUSY;
  1178. CLEAR_BIT(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK);
  1179. timer_base_set_config(hperh->perh, &hperh->init);
  1180. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, config->mode << TIMER_SMCON_SMODS_POSS);
  1181. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, config->ic1_sel << TIMER_CHMR1_CC1SSEL_POSS);
  1182. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, config->ic2_sel << TIMER_CHMR1_CC2SSEL_POSS);
  1183. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK, config->ic1_psc << TIMER_CHMR1_IC1PRES_POSS);
  1184. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC2PRES_MSK, config->ic2_psc << TIMER_CHMR1_IC2PRES_POSS);
  1185. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_I1FLT_MSK, config->ic1_filter << TIMER_CHMR1_I1FLT_POSS);
  1186. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_I2FLT_MSK, config->ic2_filter << TIMER_CHMR1_I2FLT_POSS);
  1187. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1POL_MSK, (config->ic1_polarity & 0x1) << TIMER_CCEP_CC1POL_POS);
  1188. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1NPOL_MSK, ((config->ic1_polarity >> 1) & 0x1) << TIMER_CCEP_CC1NPOL_POS);
  1189. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2POL_MSK, (config->ic2_polarity & 0x1) << TIMER_CCEP_CC2POL_POS);
  1190. MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2NPOL_MSK, ((config->ic2_polarity >> 1) & 0x1) << TIMER_CCEP_CC2NPOL_POS);
  1191. hperh->state = TIMER_STATE_READY;
  1192. return OK;
  1193. }
  1194. /**
  1195. * @brief Starts the TIMER Encoder Interface.
  1196. * @param hperh: TIMER handle
  1197. * @param ch: TIMER Channels to be enabled
  1198. * This parameter can be one of the following values:
  1199. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1200. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1201. * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
  1202. * @retval None
  1203. */
  1204. void timer_encoder_start(timer_handle_t *hperh, timer_channel_t ch)
  1205. {
  1206. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1207. switch (ch) {
  1208. case TIMER_CHANNEL_1:
  1209. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1210. break;
  1211. case TIMER_CHANNEL_2:
  1212. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1213. break;
  1214. default:
  1215. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1216. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1217. break;
  1218. }
  1219. TIMER_ENABLE(hperh);
  1220. return;
  1221. }
  1222. /**
  1223. * @brief Stops the TIMER Encoder Interface.
  1224. * @param hperh: TIMER handle
  1225. * @param ch: TIMER Channels to be enabled
  1226. * This parameter can be one of the following values:
  1227. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1228. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1229. * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
  1230. * @retval None
  1231. */
  1232. void timer_encoder_stop(timer_handle_t *hperh, timer_channel_t ch)
  1233. {
  1234. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1235. switch (ch) {
  1236. case TIMER_CHANNEL_1:
  1237. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1238. break;
  1239. case TIMER_CHANNEL_2:
  1240. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1241. break;
  1242. default:
  1243. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1244. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1245. break;
  1246. }
  1247. TIMER_DISABLE(hperh);
  1248. return;
  1249. }
  1250. /**
  1251. * @brief Starts the TIMER Encoder Interface in interrupt mode.
  1252. * @param hperh: TIMER handle
  1253. * @param ch: TIMER Channels to be enabled
  1254. * This parameter can be one of the following values:
  1255. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1256. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1257. * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
  1258. * @retval None
  1259. */
  1260. void timer_encoder_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  1261. {
  1262. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1263. switch (ch) {
  1264. case TIMER_CHANNEL_1:
  1265. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1266. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  1267. break;
  1268. case TIMER_CHANNEL_2:
  1269. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1270. timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
  1271. break;
  1272. default:
  1273. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1274. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1275. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  1276. timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
  1277. break;
  1278. }
  1279. TIMER_ENABLE(hperh);
  1280. return;
  1281. }
  1282. /**
  1283. * @brief Stops the TIMER Encoder Interface in interrupt mode.
  1284. * @param hperh: TIMER handle
  1285. * @param ch: TIMER Channels to be enabled
  1286. * This parameter can be one of the following values:
  1287. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1288. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1289. * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
  1290. * @retval None
  1291. */
  1292. void timer_encoder_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  1293. {
  1294. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1295. switch (ch) {
  1296. case TIMER_CHANNEL_1:
  1297. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1298. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  1299. break;
  1300. case TIMER_CHANNEL_2:
  1301. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1302. timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
  1303. break;
  1304. default:
  1305. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1306. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1307. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  1308. timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
  1309. break;
  1310. }
  1311. TIMER_DISABLE(hperh);
  1312. hperh->state = TIMER_STATE_READY;
  1313. return;
  1314. }
  1315. #ifdef ALD_DMA
  1316. /**
  1317. * @brief Starts the TIMER Encoder Interface in DMA mode.
  1318. * @param hperh: TIMER handle
  1319. * @param ch: TIMER Channels to be enabled
  1320. * This parameter can be one of the following values:
  1321. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1322. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1323. * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
  1324. * @param hdma1: Pointer to dma_handle_t.
  1325. * @param hdma2: Pointer to dma_handle_t.
  1326. * @param buf1: The destination Buffer address. Reading data from CCR1.
  1327. * @param buf2: The destination Buffer address. Reading data from CCR2.
  1328. * @param len: The length of buffer to be transferred TIMER peripheral to memory
  1329. * @param dma_ch1: Channel of DMA.
  1330. * @param dma_ch2: Channel of DMA.
  1331. * @retval Status, see @ref ald_status_t.
  1332. */
  1333. ald_status_t timer_encoder_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
  1334. dma_handle_t *hdma1, dma_handle_t *hdma2, uint16_t *buf1,
  1335. uint16_t *buf2, uint32_t len, uint8_t dma_ch1, uint8_t dma_ch2)
  1336. {
  1337. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1338. if ((hperh->state == TIMER_STATE_BUSY))
  1339. return BUSY;
  1340. if ((hperh->state == TIMER_STATE_READY)) {
  1341. if (((uint32_t)buf1 == 0) || ((uint32_t)buf2 == 0) || (len == 0))
  1342. return ERROR;
  1343. }
  1344. if (hdma1->perh == NULL)
  1345. hdma1->perh = DMA0;
  1346. if (hdma2->perh == NULL)
  1347. hdma2->perh = DMA0;
  1348. hperh->state = TIMER_STATE_BUSY;
  1349. hdma1->cplt_cbk = timer_dma_capture_cplt;
  1350. hdma1->cplt_arg = (void *)hperh;
  1351. hdma1->err_cbk = timer_dma_error;
  1352. hdma1->err_arg = (void *)hperh;
  1353. dma_config_struct(&hdma1->config);
  1354. hdma1->config.size = len;
  1355. hdma1->config.data_width = DMA_DATA_SIZE_HALFWORD;
  1356. hdma1->config.src_inc = DMA_DATA_INC_NONE;
  1357. hdma1->config.dst_inc = DMA_DATA_INC_HALFWORD;
  1358. if (hperh->perh == TIMER0)
  1359. hdma1->config.msel = DMA_MSEL_TIMER0;
  1360. else if (hperh->perh == TIMER1)
  1361. hdma1->config.msel = DMA_MSEL_TIMER1;
  1362. else if (hperh->perh == TIMER2)
  1363. hdma1->config.msel = DMA_MSEL_TIMER2;
  1364. else if (hperh->perh == TIMER3)
  1365. hdma1->config.msel = DMA_MSEL_TIMER3;
  1366. else if (hperh->perh == TIMER4)
  1367. hdma1->config.msel = DMA_MSEL_TIMER4;
  1368. else if (hperh->perh == TIMER5)
  1369. hdma1->config.msel = DMA_MSEL_TIMER5;
  1370. else if (hperh->perh == TIMER6)
  1371. hdma1->config.msel = DMA_MSEL_TIMER6;
  1372. else if (hperh->perh == TIMER7)
  1373. hdma1->config.msel = DMA_MSEL_TIMER7;
  1374. else
  1375. ;/* do nothing */
  1376. switch (ch) {
  1377. case TIMER_CHANNEL_1:
  1378. hdma1->config.src = (void *)&hperh->perh->CCVAL1;
  1379. hdma1->config.dst = (void *)buf1;
  1380. hdma1->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
  1381. hdma1->config.channel = dma_ch1;
  1382. dma_config_basic(hdma1);
  1383. timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
  1384. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1385. TIMER_ENABLE(hperh);
  1386. break;
  1387. case TIMER_CHANNEL_2:
  1388. hdma1->config.src = (void *)&hperh->perh->CCVAL2;
  1389. hdma1->config.dst = (void *)buf2;
  1390. hdma1->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
  1391. hdma1->config.channel = dma_ch2;
  1392. dma_config_basic(hdma1);
  1393. timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
  1394. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1395. TIMER_ENABLE(hperh);
  1396. break;
  1397. default:
  1398. hdma2->cplt_cbk = timer_dma_capture_cplt;
  1399. hdma2->cplt_arg = (void *)hperh;
  1400. hdma2->err_cbk = timer_dma_error;
  1401. hdma2->err_arg = (void *)hperh;
  1402. memcpy(&hdma2->config, &hdma1->config, sizeof(dma_config_t));
  1403. hdma1->config.src = (void *)&hperh->perh->CCVAL1;
  1404. hdma1->config.dst = (void *)buf1;
  1405. hdma1->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
  1406. hdma1->config.channel = dma_ch1;
  1407. dma_config_basic(hdma1);
  1408. timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
  1409. hdma2->config.src = (void *)&hperh->perh->CCVAL2;
  1410. hdma2->config.dst = (void *)buf2;
  1411. hdma2->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
  1412. hdma2->config.channel = dma_ch2;
  1413. dma_config_basic(hdma2);
  1414. timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
  1415. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1416. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
  1417. TIMER_ENABLE(hperh);
  1418. break;
  1419. }
  1420. return OK;
  1421. }
  1422. /**
  1423. * @brief Stops the TIMER Encoder Interface in DMA mode.
  1424. * @param hperh: TIMER handle
  1425. * @param ch: TIMER Channels to be disabled
  1426. * This parameter can be one of the following values:
  1427. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1428. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1429. * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
  1430. * @retval None
  1431. */
  1432. void timer_encoder_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
  1433. {
  1434. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  1435. switch (ch) {
  1436. case TIMER_CHANNEL_1:
  1437. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1438. timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
  1439. break;
  1440. case TIMER_CHANNEL_2:
  1441. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1442. timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
  1443. break;
  1444. default:
  1445. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1446. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
  1447. timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
  1448. timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
  1449. break;
  1450. }
  1451. TIMER_DISABLE(hperh);
  1452. hperh->state = TIMER_STATE_READY;
  1453. return;
  1454. }
  1455. #endif
  1456. /**
  1457. * @}
  1458. */
  1459. /** @defgroup TIMER_Public_Functions_Group7 TIMER Hall Sensor functions
  1460. * @brief TIMER Hall Sensor functions
  1461. *
  1462. * @verbatim
  1463. ==============================================================================
  1464. ##### Time Hall Sensor functions #####
  1465. ==============================================================================
  1466. [..]
  1467. This section provides functions allowing to:
  1468. (+) Initialize and configure the TIMER hall sensor.
  1469. (+) Start the hall sensor.
  1470. (+) Stop the hall sensor.
  1471. (+) Start the hall sensor and enable interrupt.
  1472. (+) Stop the hall sensor and disable interrupt.
  1473. (+) Start the hall sensor and enable DMA transfer.
  1474. (+) Stop the hal sensor and disable DMA transfer.
  1475. * @endverbatim
  1476. * @{
  1477. */
  1478. /**
  1479. * @brief Initializes the TIMER Encoder Interface and create the associated handle.
  1480. * @param hperh: TIMER handle
  1481. * @param config: TIMER Encoder Interface configuration structure
  1482. * @retval Status, see @ref ald_status_t.
  1483. */
  1484. ald_status_t timer_hall_sensor_init(timer_handle_t *hperh, timer_hall_sensor_init_t *config)
  1485. {
  1486. timer_oc_init_t oc;
  1487. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1488. assert_param(IS_TIMER_COUNTER_MODE(hperh->init.mode));
  1489. assert_param(IS_TIMER_CLOCK_DIVISION(hperh->init.clk_div));
  1490. assert_param(IS_TIMER_IC_POLARITY(config->polarity));
  1491. assert_param(IS_TIMER_IC_PSC(config->psc));
  1492. assert_param(IS_TIMER_IC_FILTER(config->filter));
  1493. if (hperh->state == TIMER_STATE_RESET)
  1494. hperh->lock = UNLOCK;
  1495. hperh->state = TIMER_STATE_READY;
  1496. timer_base_set_config(hperh->perh, &hperh->init);
  1497. timer_ti1_set_config(hperh->perh, config->polarity, TIMER_IC_SEL_TRC, config->filter);
  1498. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK, config->psc << TIMER_CHMR1_IC1PRES_POSS);
  1499. SET_BIT(hperh->perh->CON2, TIMER_CON2_I1FSEL_MSK);
  1500. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1F_ED << TIMER_SMCON_TSSEL_POSS);
  1501. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_RESET << TIMER_SMCON_SMODS_POSS);
  1502. oc.oc_mode = TIMER_OC_MODE_PWM2;
  1503. oc.pulse = config->delay;
  1504. oc.oc_polarity = TIMER_OC_POLARITY_HIGH;
  1505. oc.ocn_polarity = TIMER_OCN_POLARITY_HIGH;
  1506. oc.oc_fast_en = DISABLE;
  1507. oc.oc_idle = TIMER_OC_IDLE_RESET;
  1508. oc.ocn_idle = TIMER_OCN_IDLE_RESET;
  1509. timer_oc2_set_config(hperh->perh, &oc);
  1510. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_TRGO_OC2REF << TIMER_SMCON_SMODS_POSS);
  1511. return OK;
  1512. }
  1513. /**
  1514. * @brief Starts the TIMER hall sensor interface.
  1515. * @param hperh: TIMER handle
  1516. * @retval None
  1517. */
  1518. void timer_hall_sensor_start(timer_handle_t *hperh)
  1519. {
  1520. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1521. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1522. TIMER_ENABLE(hperh);
  1523. return;
  1524. }
  1525. /**
  1526. * @brief Stops the TIMER hall sensor interface.
  1527. * @param hperh: TIMER handle
  1528. * @retval None
  1529. */
  1530. void timer_hall_sensor_stop(timer_handle_t *hperh)
  1531. {
  1532. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1533. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1534. TIMER_DISABLE(hperh);
  1535. return;
  1536. }
  1537. /**
  1538. * @brief Starts the TIMER hall sensor interface in interrupt mode.
  1539. * @param hperh: TIMER handle
  1540. * @retval None
  1541. */
  1542. void timer_hall_sensor_start_by_it(timer_handle_t *hperh)
  1543. {
  1544. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1545. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  1546. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1547. TIMER_ENABLE(hperh);
  1548. return;
  1549. }
  1550. /**
  1551. * @brief Stops the TIMER hall sensor interface in interrupt mode.
  1552. * @param hperh: TIMER handle
  1553. * @retval None
  1554. */
  1555. void timer_hall_sensor_stop_by_it(timer_handle_t *hperh)
  1556. {
  1557. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1558. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1559. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  1560. TIMER_DISABLE(hperh);
  1561. return;
  1562. }
  1563. #ifdef ALD_DMA
  1564. /**
  1565. * @brief Starts the TIMER hall sensor interface in DMA mode.
  1566. * @param hperh: TIMER handle
  1567. * @param hdma: Pointer to dma_handle_t.
  1568. * @param buf: The destination Buffer address. Reading data from CCR1.
  1569. * @param len: The length of buffer to be transferred TIMER peripheral to memory
  1570. * @param dma_ch: Channel of DMA.
  1571. * @retval Status, see @ref ald_status_t.
  1572. */
  1573. ald_status_t timer_hall_sensor_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
  1574. uint16_t *buf, uint32_t len, uint8_t dma_ch)
  1575. {
  1576. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1577. if ((hperh->state == TIMER_STATE_BUSY))
  1578. return BUSY;
  1579. if ((hperh->state == TIMER_STATE_READY)) {
  1580. if (((uint32_t)buf == 0) || (len == 0))
  1581. return ERROR;
  1582. }
  1583. if (hdma->perh == NULL)
  1584. hdma->perh = DMA0;
  1585. hperh->state = TIMER_STATE_BUSY;
  1586. hdma->cplt_cbk = timer_dma_capture_cplt;
  1587. hdma->cplt_arg = (void *)hperh;
  1588. hdma->err_cbk = timer_dma_error;
  1589. hdma->err_arg = (void *)hperh;
  1590. dma_config_struct(&hdma->config);
  1591. hdma->config.size = len;
  1592. hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
  1593. hdma->config.src_inc = DMA_DATA_INC_NONE;
  1594. hdma->config.dst_inc = DMA_DATA_INC_HALFWORD;
  1595. if (hperh->perh == TIMER0)
  1596. hdma->config.msel = DMA_MSEL_TIMER0;
  1597. else if (hperh->perh == TIMER6)
  1598. hdma->config.msel = DMA_MSEL_TIMER6;
  1599. else
  1600. ;/* do nothing */
  1601. hdma->config.src = (void *)&hperh->perh->CCVAL1;
  1602. hdma->config.dst = (void *)buf;
  1603. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
  1604. hdma->config.channel = dma_ch;
  1605. dma_config_basic(hdma);
  1606. timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
  1607. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
  1608. TIMER_ENABLE(hperh);
  1609. return OK;
  1610. }
  1611. /**
  1612. * @brief Stops the TIMER hall sensor interface in DMA mode.
  1613. * @param hperh: TIMER handle
  1614. * @retval None
  1615. */
  1616. void timer_hall_sensor_stop_by_dma(timer_handle_t *hperh)
  1617. {
  1618. assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
  1619. timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
  1620. timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
  1621. TIMER_DISABLE(hperh);
  1622. return;
  1623. }
  1624. #endif
  1625. /**
  1626. * @}
  1627. */
  1628. /** @defgroup TIMER_Public_Functions_Group8 TIMER complementary output compare functions
  1629. * @brief TIMER complementary output compare functions
  1630. *
  1631. * @verbatim
  1632. ==============================================================================
  1633. ##### Time complementary output compare functions #####
  1634. ==============================================================================
  1635. [..]
  1636. This section provides functions allowing to:
  1637. (+) Start the Time complementary output compare.
  1638. (+) Stop the Time complementary output compare.
  1639. (+) Start the Time complementary output compare and enable interrupt.
  1640. (+) Stop the Time complementary output compare and disable interrupt.
  1641. (+) Start the Time complementary output compare and enable DMA transfer.
  1642. (+) Stop the Time complementary output compare and disable DMA transfer.
  1643. * @endverbatim
  1644. * @{
  1645. */
  1646. /**
  1647. * @brief Starts the TIMER output compare signal generation on the complementary output.
  1648. * @param hperh: TIMER handle
  1649. * @param ch: TIMER Channels to be enabled
  1650. * This parameter can be one of the following values:
  1651. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1652. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1653. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1654. * @retval None
  1655. */
  1656. void timer_ocn_start(timer_handle_t *hperh, timer_channel_t ch)
  1657. {
  1658. assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
  1659. timer_ccxn_channel_cmd(hperh->perh, ch, ENABLE);
  1660. TIMER_MOE_ENABLE(hperh);
  1661. TIMER_ENABLE(hperh);
  1662. return;
  1663. }
  1664. /**
  1665. * @brief Stops the TIMER output compare signal generation on the complementary output.
  1666. * @param hperh: TIMER handle
  1667. * @param ch: TIMER Channels to be disabled
  1668. * This parameter can be one of the following values:
  1669. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1670. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1671. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1672. * @retval None
  1673. */
  1674. void timer_ocn_stop(timer_handle_t *hperh, timer_channel_t ch)
  1675. {
  1676. assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
  1677. timer_ccxn_channel_cmd(hperh->perh, ch, DISABLE);
  1678. TIMER_MOE_DISABLE(hperh);
  1679. TIMER_DISABLE(hperh);
  1680. return;
  1681. }
  1682. /**
  1683. * @brief Starts the TIMER output compare signal generation on the complementary output.
  1684. * in interrupt mode
  1685. * @param hperh: TIMER handle
  1686. * @param ch: TIMER Channels to be enabled
  1687. * This parameter can be one of the following values:
  1688. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1689. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1690. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1691. * @retval None
  1692. */
  1693. void timer_ocn_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  1694. {
  1695. assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
  1696. switch (ch) {
  1697. case TIMER_CHANNEL_1:
  1698. timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
  1699. break;
  1700. case TIMER_CHANNEL_2:
  1701. timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
  1702. break;
  1703. case TIMER_CHANNEL_3:
  1704. timer_interrupt_config(hperh, TIMER_IT_CC3, ENABLE);
  1705. break;
  1706. default:
  1707. break;
  1708. }
  1709. timer_interrupt_config(hperh, TIMER_IT_BREAK, ENABLE);
  1710. timer_ccxn_channel_cmd(hperh->perh, ch, ENABLE);
  1711. TIMER_MOE_ENABLE(hperh);
  1712. TIMER_ENABLE(hperh);
  1713. return;
  1714. }
  1715. /**
  1716. * @brief Stops the TIMER output compare signal generation on the complementary output.
  1717. * in interrupt mode
  1718. * @param hperh: TIMER handle
  1719. * @param ch: TIMER Channels to be disabled
  1720. * This parameter can be one of the following values:
  1721. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1722. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1723. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1724. * @retval None
  1725. */
  1726. void timer_ocn_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  1727. {
  1728. assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
  1729. switch (ch) {
  1730. case TIMER_CHANNEL_1:
  1731. timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
  1732. break;
  1733. case TIMER_CHANNEL_2:
  1734. timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
  1735. break;
  1736. case TIMER_CHANNEL_3:
  1737. timer_interrupt_config(hperh, TIMER_IT_CC3, DISABLE);
  1738. break;
  1739. default:
  1740. break;
  1741. }
  1742. if ((!(READ_BIT(hperh->perh->CCEP, TIMER_CCEP_CC1NEN_MSK)))
  1743. && (!(READ_BIT(hperh->perh->CCEP, TIMER_CCEP_CC2NEN_MSK)))
  1744. && (!(READ_BIT(hperh->perh->CCEP, TIMER_CCEP_CC3NEN_MSK)))) {
  1745. timer_interrupt_config(hperh, TIMER_IT_BREAK, DISABLE);
  1746. }
  1747. timer_ccxn_channel_cmd(hperh->perh, ch, DISABLE);
  1748. TIMER_MOE_DISABLE(hperh);
  1749. TIMER_DISABLE(hperh);
  1750. return;
  1751. }
  1752. #ifdef ALD_DMA
  1753. /**
  1754. * @brief Starts the TIMER output compare signal generation on the complementary output.
  1755. * in DMA mode
  1756. * @param hperh: TIMER handle
  1757. * @param ch: TIMER Channels to be enabled
  1758. * This parameter can be one of the following values:
  1759. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1760. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1761. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1762. * @param hdma: Pointer to dma_handle_t.
  1763. * @param buf: The destination Buffer address. Reading data from CCRx.
  1764. * @param len: The length of buffer to be transferred TIMER peripheral to memory
  1765. * @param dma_ch: Channel of DMA.
  1766. * @retval None
  1767. */
  1768. ald_status_t timer_ocn_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
  1769. timer_channel_t ch, uint16_t *buf, uint32_t len, uint8_t dma_ch)
  1770. {
  1771. assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
  1772. if ((hperh->state == TIMER_STATE_BUSY))
  1773. return BUSY;
  1774. if ((hperh->state == TIMER_STATE_READY)) {
  1775. if (((uint32_t)buf == 0 ) || (len == 0))
  1776. return ERROR;
  1777. }
  1778. hperh->state = TIMER_STATE_BUSY;
  1779. if (hdma->perh == NULL)
  1780. hdma->perh = DMA0;
  1781. hdma->cplt_cbk = timer_dma_oc_cplt;
  1782. hdma->cplt_arg = (void *)hperh;
  1783. hdma->err_cbk = timer_dma_error;
  1784. hdma->err_arg = (void *)hperh;
  1785. dma_config_struct(&hdma->config);
  1786. hdma->config.src = (void *)buf;
  1787. hdma->config.size = len;
  1788. hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
  1789. hdma->config.src_inc = DMA_DATA_INC_HALFWORD;
  1790. hdma->config.dst_inc = DMA_DATA_INC_NONE;
  1791. hdma->config.channel = dma_ch;
  1792. hdma->config.msel = DMA_MSEL_TIMER0;
  1793. switch (ch) {
  1794. case TIMER_CHANNEL_1:
  1795. hdma->config.dst = (void *)&hperh->perh->CCVAL1;
  1796. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
  1797. dma_config_basic(hdma);
  1798. timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
  1799. hperh->ch = TIMER_ACTIVE_CHANNEL_1;
  1800. break;
  1801. case TIMER_CHANNEL_2:
  1802. hdma->config.dst = (void *)&hperh->perh->CCVAL2;
  1803. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
  1804. dma_config_basic(hdma);
  1805. timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
  1806. hperh->ch = TIMER_ACTIVE_CHANNEL_2;
  1807. break;
  1808. case TIMER_CHANNEL_3:
  1809. hdma->config.dst = (void *)&hperh->perh->CCVAL3;
  1810. hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH3;
  1811. dma_config_basic(hdma);
  1812. timer_dma_req_config(hperh, TIMER_DMA_CC3, ENABLE);
  1813. hperh->ch = TIMER_ACTIVE_CHANNEL_3;
  1814. break;
  1815. default:
  1816. break;
  1817. }
  1818. timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
  1819. TIMER_MOE_ENABLE(hperh);
  1820. TIMER_ENABLE(hperh);
  1821. return OK;
  1822. }
  1823. /**
  1824. * @brief Starts the TIMER output compare signal generation on the complementary output.
  1825. * in DMA mode
  1826. * @param hperh: TIMER handle
  1827. * @param ch: TIMER Channels to be disabled
  1828. * This parameter can be one of the following values:
  1829. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1830. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1831. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1832. * @retval None
  1833. */
  1834. void timer_ocn_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
  1835. {
  1836. assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
  1837. switch (ch) {
  1838. case TIMER_CHANNEL_1:
  1839. timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
  1840. break;
  1841. case TIMER_CHANNEL_2:
  1842. timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
  1843. break;
  1844. case TIMER_CHANNEL_3:
  1845. timer_dma_req_config(hperh, TIMER_DMA_CC3, DISABLE);
  1846. break;
  1847. default:
  1848. break;
  1849. }
  1850. timer_ccxn_channel_cmd(hperh->perh, ch, DISABLE);
  1851. TIMER_MOE_DISABLE(hperh);
  1852. TIMER_DISABLE(hperh);
  1853. return;
  1854. }
  1855. #endif
  1856. /**
  1857. * @}
  1858. */
  1859. /** @defgroup TIMER_Public_Functions_Group9 TIMER complementary PWM functions
  1860. * @brief TIMER complementary PWM functions
  1861. *
  1862. * @verbatim
  1863. ==============================================================================
  1864. ##### Time complementary PWM functions #####
  1865. ==============================================================================
  1866. [..]
  1867. This section provides functions allowing to:
  1868. (+) Start the Time complementary PWM.
  1869. (+) Stop the Time complementary PWM.
  1870. (+) Start the Time complementary PWM and enable interrupt.
  1871. (+) Stop the Time complementary PWM and disable interrupt.
  1872. (+) Start the Time complementary PWM and enable DMA transfer.
  1873. (+) Stop the Time complementary PWM and disable DMA transfer.
  1874. * @endverbatim
  1875. * @{
  1876. */
  1877. /**
  1878. * @brief Starts the TIMER PWM signal generation on the complementary output.
  1879. * @param hperh: TIMER handle
  1880. * @param ch: TIMER Channels to be enabled
  1881. * This parameter can be one of the following values:
  1882. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1883. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1884. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1885. * @retval None
  1886. */
  1887. void timer_pwmn_start(timer_handle_t *hperh, timer_channel_t ch)
  1888. {
  1889. timer_ocn_start(hperh, ch);
  1890. }
  1891. /**
  1892. * @brief Stops the TIMER PWM signal generation on the complementary output.
  1893. * @param hperh: TIMER handle
  1894. * @param ch: TIMER Channels to be disabled
  1895. * This parameter can be one of the following values:
  1896. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1897. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1898. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1899. * @retval None
  1900. */
  1901. void timer_pwmn_stop(timer_handle_t *hperh, timer_channel_t ch)
  1902. {
  1903. timer_ocn_stop(hperh, ch);
  1904. }
  1905. /**
  1906. * @brief Starts the TIMER PWM signal generation on the complementary output.
  1907. * in interrupt mode
  1908. * @param hperh: TIMER handle
  1909. * @param ch: TIMER Channels to be enabled
  1910. * This parameter can be one of the following values:
  1911. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1912. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1913. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1914. * @retval None
  1915. */
  1916. void timer_pwmn_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  1917. {
  1918. timer_ocn_start_by_it(hperh, ch);
  1919. }
  1920. /**
  1921. * @brief Stops the TIMER PWM signal generation on the complementary output.
  1922. * in interrupt mode
  1923. * @param hperh: TIMER handle
  1924. * @param ch: TIMER Channels to be disabled
  1925. * This parameter can be one of the following values:
  1926. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1927. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1928. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1929. * @retval None
  1930. */
  1931. void timer_pwmn_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  1932. {
  1933. timer_ocn_stop_by_it(hperh, ch);
  1934. }
  1935. #ifdef ALD_DMA
  1936. /**
  1937. * @brief Starts the TIMER PWM signal generation on the complementary output.
  1938. * in DMA mode
  1939. * @param hperh: TIMER handle
  1940. * @param ch: TIMER Channels to be enabled
  1941. * This parameter can be one of the following values:
  1942. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1943. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1944. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1945. * @param hdma: Pointer to dma_handle_t.
  1946. * @param buf: The destination Buffer address. Reading data from CCRx.
  1947. * @param len: The length of buffer to be transferred TIMER peripheral to memory
  1948. * @param dma_ch: Channel of DMA.
  1949. * @retval None
  1950. */
  1951. ald_status_t timer_pwmn_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
  1952. timer_channel_t ch, uint16_t *buf, uint32_t len, uint8_t dma_ch)
  1953. {
  1954. return timer_ocn_start_by_dma(hperh, hdma, ch, buf, len, dma_ch);
  1955. }
  1956. /**
  1957. * @brief Starts the TIMER PWM signal generation on the complementary output.
  1958. * in DMA mode
  1959. * @param hperh: TIMER handle
  1960. * @param ch: TIMER Channels to be disabled
  1961. * This parameter can be one of the following values:
  1962. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1963. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1964. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  1965. * @retval None
  1966. */
  1967. void timer_pwmn_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
  1968. {
  1969. timer_ocn_stop_by_dma(hperh, ch);
  1970. }
  1971. #endif
  1972. /**
  1973. * @}
  1974. */
  1975. /** @defgroup TIMER_Public_Functions_Group10 TIMER complementary one pulse functions
  1976. * @brief TIMER complementary one pulse functions
  1977. *
  1978. * @verbatim
  1979. ==============================================================================
  1980. ##### Time complementary one pulse functions #####
  1981. ==============================================================================
  1982. [..]
  1983. This section provides functions allowing to:
  1984. (+) Start the Time complementary one pulse.
  1985. (+) Stop the Time complementary one pulse.
  1986. (+) Start the Time complementary one pulse and enable interrupt.
  1987. (+) Stop the Time complementary one pulse and disable interrupt.
  1988. * @endverbatim
  1989. * @{
  1990. */
  1991. /**
  1992. * @brief Starts the TIMER one pulse signal generation on the complementary output.
  1993. * @param hperh: TIMER handle
  1994. * @param ch: TIMER Channels to be enabled
  1995. * This parameter can be one of the following values:
  1996. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  1997. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  1998. * @retval None
  1999. */
  2000. void timer_one_pulse_n_start(timer_handle_t *hperh, timer_channel_t ch)
  2001. {
  2002. timer_ocn_start(hperh, ch);
  2003. }
  2004. /**
  2005. * @brief Stops the TIMER one pulse signal generation on the complementary output.
  2006. * @param hperh: TIMER handle
  2007. * @param ch: TIMER Channels to be disabled
  2008. * This parameter can be one of the following values:
  2009. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2010. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2011. * @retval None
  2012. */
  2013. void timer_one_pulse_n_stop(timer_handle_t *hperh, timer_channel_t ch)
  2014. {
  2015. timer_ocn_stop(hperh, ch);
  2016. }
  2017. /**
  2018. * @brief Starts the TIMER one pulse signal generation on the complementary output.
  2019. * in interrupt mode
  2020. * @param hperh: TIMER handle
  2021. * @param ch: TIMER Channels to be enabled
  2022. * This parameter can be one of the following values:
  2023. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2024. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2025. * @retval None
  2026. */
  2027. void timer_one_pulse_n_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
  2028. {
  2029. timer_ocn_start_by_it(hperh, ch);
  2030. }
  2031. /**
  2032. * @brief Stops the TIMER one pulse signal generation on the complementary output.
  2033. * in interrupt mode
  2034. * @param hperh: TIMER handle
  2035. * @param ch: TIMER Channels to be disabled
  2036. * This parameter can be one of the following values:
  2037. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2038. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2039. * @retval None
  2040. */
  2041. void timer_one_pulse_n_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
  2042. {
  2043. timer_ocn_stop_by_it(hperh, ch);
  2044. }
  2045. /**
  2046. * @}
  2047. */
  2048. /** @defgroup TIMER_Public_Functions_Group11 Peripheral Control functions
  2049. * @brief Peripheral Control functions
  2050. *
  2051. * @verbatim
  2052. ==============================================================================
  2053. ##### Peripheral Control functions #####
  2054. ==============================================================================
  2055. [..]
  2056. This section provides functions allowing to:
  2057. (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
  2058. (+) Configure External Clock source.
  2059. (+) Configure Complementary channels, break features and dead timere.
  2060. (+) Configure Master and the Slave synchronization.
  2061. (+) Handle TIMER interrupt.
  2062. (+) Get TIMER compare register's vale.
  2063. (+) Configure TIMER interrupt ENABLE/DISABLE.
  2064. (+) Get TIMER interrupt source status.
  2065. (+) Get TIMER interrupt flag status.
  2066. (+) Clear TIMER interrupt flag.
  2067. @endverbatim
  2068. * @{
  2069. */
  2070. /**
  2071. * @brief Initializes the TIMER Output Compare Channels according to the specified
  2072. * parameters in the timer_oc_init_t.
  2073. * @param hperh: TIMER handle
  2074. * @param config: TIMER Output Compare configuration structure
  2075. * @param ch: TIMER Channels to be enabled
  2076. * This parameter can be one of the following values:
  2077. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2078. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2079. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  2080. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  2081. * @retval Status, see @ref ald_status_t.
  2082. */
  2083. ald_status_t timer_oc_config_channel(timer_handle_t *hperh, timer_oc_init_t* config, timer_channel_t ch)
  2084. {
  2085. assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
  2086. assert_param(IS_TIMER_OC_MODE(config->oc_mode));
  2087. assert_param(IS_TIMER_OC_POLARITY(config->oc_polarity));
  2088. __LOCK(hperh);
  2089. hperh->state = TIMER_STATE_BUSY;
  2090. switch (ch) {
  2091. case TIMER_CHANNEL_1:
  2092. timer_oc1_set_config(hperh->perh, config);
  2093. break;
  2094. case TIMER_CHANNEL_2:
  2095. timer_oc2_set_config(hperh->perh, config);
  2096. break;
  2097. case TIMER_CHANNEL_3:
  2098. timer_oc3_set_config(hperh->perh, config);
  2099. break;
  2100. case TIMER_CHANNEL_4:
  2101. timer_oc4_set_config(hperh->perh, config);
  2102. break;
  2103. default:
  2104. break;
  2105. }
  2106. hperh->state = TIMER_STATE_READY;
  2107. __UNLOCK(hperh);
  2108. return OK;
  2109. }
  2110. /**
  2111. * @brief Initializes the TIMER Input Capture Channels according to the specified
  2112. * parameters in the timer_ic_init_t.
  2113. * @param hperh: TIMER handle
  2114. * @param config: TIMER Input Capture configuration structure
  2115. * @param ch: TIMER Channels to be enabled
  2116. * This parameter can be one of the following values:
  2117. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2118. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2119. * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
  2120. * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
  2121. * @retval Status, see @ref ald_status_t.
  2122. */
  2123. ald_status_t timer_ic_config_channel(timer_handle_t *hperh, timer_ic_init_t* config, timer_channel_t ch)
  2124. {
  2125. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  2126. assert_param(IS_TIMER_IC_POLARITY(config->polarity));
  2127. assert_param(IS_TIMER_IC_SELECT(config->sel));
  2128. assert_param(IS_TIMER_IC_PSC(config->psc));
  2129. assert_param(IS_TIMER_IC_FILTER(config->filter));
  2130. __LOCK(hperh);
  2131. hperh->state = TIMER_STATE_BUSY;
  2132. switch (ch) {
  2133. case TIMER_CHANNEL_1:
  2134. timer_ti1_set_config(hperh->perh, config->polarity, config->sel, config->filter);
  2135. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK, config->psc << TIMER_CHMR1_IC1PRES_POSS);
  2136. break;
  2137. case TIMER_CHANNEL_2:
  2138. timer_ti2_set_config(hperh->perh, config->polarity, config->sel, config->filter);
  2139. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC2PRES_MSK, config->psc << TIMER_CHMR1_IC2PRES_POSS);
  2140. break;
  2141. case TIMER_CHANNEL_3:
  2142. timer_ti3_set_config(hperh->perh, config->polarity, config->sel, config->filter);
  2143. MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_IC3PRES_MSK, config->psc << TIMER_CHMR2_IC3PRES_POSS);
  2144. break;
  2145. case TIMER_CHANNEL_4:
  2146. timer_ti4_set_config(hperh->perh, config->polarity, config->sel, config->filter);
  2147. MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_IC4PRES_MSK, config->psc << TIMER_CHMR2_IC4PRES_POSS);
  2148. break;
  2149. default:
  2150. break;
  2151. }
  2152. hperh->state = TIMER_STATE_READY;
  2153. __UNLOCK(hperh);
  2154. return OK;
  2155. }
  2156. /**
  2157. * @brief Initializes the TIMER One Pulse Channels according to the specified
  2158. * parameters in the timer_one_pulse_init_t.
  2159. * @param hperh: TIMER handle
  2160. * @param config: TIMER One Pulse configuration structure
  2161. * @param ch_out: TIMER Channels to be enabled
  2162. * This parameter can be one of the following values:
  2163. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2164. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2165. * @param ch_in: TIMER Channels to be enabled
  2166. * This parameter can be one of the following values:
  2167. * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
  2168. * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
  2169. * @retval Status, see @ref ald_status_t.
  2170. */
  2171. ald_status_t timer_one_pulse_config_channel(timer_handle_t *hperh, timer_one_pulse_init_t *config,
  2172. timer_channel_t ch_out, timer_channel_t ch_in)
  2173. {
  2174. timer_oc_init_t tmp;
  2175. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  2176. assert_param(IS_TIMER_OC_MODE(config->mode));
  2177. assert_param(IS_TIMER_OC_POLARITY(config->oc_polarity));
  2178. assert_param(IS_TIMER_OCN_POLARITY(config->ocn_polarity));
  2179. assert_param(IS_TIMER_OCIDLE_STATE(config->oc_idle));
  2180. assert_param(IS_TIMER_OCNIDLE_STATE(config->ocn_idle));
  2181. assert_param(IS_TIMER_IC_POLARITY(config->polarity));
  2182. assert_param(IS_TIMER_IC_SELECT(config->sel));
  2183. assert_param(IS_TIMER_IC_FILTER(config->filter));
  2184. if (ch_out == ch_in)
  2185. return ERROR;
  2186. __LOCK(hperh);
  2187. hperh->state = TIMER_STATE_BUSY;
  2188. tmp.oc_mode = config->mode;
  2189. tmp.pulse = config->pulse;
  2190. tmp.oc_polarity = config->oc_polarity;
  2191. tmp.ocn_polarity = config->ocn_polarity;
  2192. tmp.oc_idle = config->oc_idle;
  2193. tmp.ocn_idle = config->ocn_idle;
  2194. switch (ch_out) {
  2195. case TIMER_CHANNEL_1:
  2196. timer_oc1_set_config(hperh->perh, &tmp);
  2197. break;
  2198. case TIMER_CHANNEL_2:
  2199. timer_oc2_set_config(hperh->perh, &tmp);
  2200. break;
  2201. default:
  2202. break;
  2203. }
  2204. switch (ch_in) {
  2205. case TIMER_CHANNEL_1:
  2206. timer_ti1_set_config(hperh->perh, config->polarity, config->sel, config->filter);
  2207. CLEAR_BIT(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK);
  2208. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1FP1 << TIMER_SMCON_TSSEL_POSS);
  2209. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_TRIG << TIMER_SMCON_SMODS_POSS);
  2210. break;
  2211. case TIMER_CHANNEL_2:
  2212. timer_ti2_set_config(hperh->perh, config->polarity, config->sel, config->filter);
  2213. CLEAR_BIT(hperh->perh->CHMR1, TIMER_CHMR1_IC2PRES_MSK);
  2214. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI2FP2 << TIMER_SMCON_TSSEL_POSS);
  2215. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_TRIG << TIMER_SMCON_SMODS_POSS);
  2216. break;
  2217. default:
  2218. break;
  2219. }
  2220. hperh->state = TIMER_STATE_READY;
  2221. __UNLOCK(hperh);
  2222. return OK;
  2223. }
  2224. /**
  2225. * @brief Configures the OCRef clear feature
  2226. * @param hperh: TIMER handle
  2227. * @param config: pointer to a TIMER_ClearInputConfigTypeDef structure that
  2228. * contains the OCREF clear feature and parameters for the TIMER peripheral.
  2229. * @param ch: specifies the TIMER Channel
  2230. * This parameter can be one of the following values:
  2231. * @arg TIMER_CHANNEL_1: TIMER Channel 1
  2232. * @arg TIMER_CHANNEL_2: TIMER Channel 2
  2233. * @arg TIMER_CHANNEL_3: TIMER Channel 3
  2234. * @arg TIMER_CHANNEL_4: TIMER Channel 4
  2235. * @retval Status, see @ref ald_status_t.
  2236. */
  2237. ald_status_t timer_config_oc_ref_clear(timer_handle_t *hperh, timer_clear_input_config_t *config, timer_channel_t ch)
  2238. {
  2239. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  2240. assert_param(IS_FUNC_STATE(config->state));
  2241. assert_param(IS_TIMER_CLEAR_INPUT_SOURCE(config->source));
  2242. assert_param(IS_TIMER_CLEAR_INPUT_POLARITY(config->polarity));
  2243. assert_param(IS_TIMER_ETR_PSC(config->psc));
  2244. assert_param(IS_TIMER_IC_FILTER(config->filter));
  2245. if (config->source == TIMER_INPUT_NONE) {
  2246. timer_etr_set_config(hperh->perh, TIMER_ETR_PSC_DIV1, TIMER_CLK_POLARITY_NO_INV, 0);
  2247. }
  2248. else {
  2249. timer_etr_set_config(hperh->perh, config->psc,
  2250. (timer_clock_polarity_t)config->polarity, config->filter);
  2251. }
  2252. switch (ch) {
  2253. case TIMER_CHANNEL_1:
  2254. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH1OCLREN_MSK, config->state << TIMER_CHMR1_CH1OCLREN_POS);
  2255. break;
  2256. case TIMER_CHANNEL_2:
  2257. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH2OCLREN_MSK, config->state << TIMER_CHMR1_CH2OCLREN_POS);
  2258. break;
  2259. case TIMER_CHANNEL_3:
  2260. assert_param(IS_TIMER_CC4_INSTANCE(hperh->perh));
  2261. MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH3OCLREN_MSK, config->state << TIMER_CHMR2_CH3OCLREN_POS);
  2262. break;
  2263. case TIMER_CHANNEL_4:
  2264. assert_param(IS_TIMER_CC4_INSTANCE(hperh->perh));
  2265. MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH4OCLREN_MSK, config->state << TIMER_CHMR2_CH4OCLREN_POS);
  2266. break;
  2267. default:
  2268. break;
  2269. }
  2270. return OK;
  2271. }
  2272. /**
  2273. * @brief Configures the clock source to be used
  2274. * @param hperh: TIMER handle
  2275. * @param config: pointer to a timer_clock_config_t structure that
  2276. * contains the clock source information for the TIMER peripheral.
  2277. * @retval Status, see @ref ald_status_t.
  2278. */
  2279. ald_status_t timer_config_clock_source(timer_handle_t *hperh, timer_clock_config_t *config)
  2280. {
  2281. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2282. assert_param(IS_TIMER_CLOCK_SOURCE(config->source));
  2283. assert_param(IS_TIMER_CLOCK_POLARITY(config->polarity));
  2284. assert_param(IS_TIMER_ETR_PSC(config->psc));
  2285. assert_param(IS_TIMER_IC_FILTER(config->filter));
  2286. __LOCK(hperh);
  2287. hperh->state = TIMER_STATE_BUSY;
  2288. WRITE_REG(hperh->perh->SMCON, 0x0);
  2289. switch (config->source) {
  2290. case TIMER_SRC_INTER:
  2291. CLEAR_BIT(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK);
  2292. break;
  2293. case TIMER_SRC_ETRMODE1:
  2294. timer_etr_set_config(hperh->perh, config->psc, config->polarity, config->filter);
  2295. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ETRF << TIMER_SMCON_TSSEL_POSS);
  2296. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2297. break;
  2298. case TIMER_SRC_ETRMODE2:
  2299. timer_etr_set_config(hperh->perh, config->psc, config->polarity, config->filter);
  2300. SET_BIT(hperh->perh->SMCON, TIMER_SMCON_ECM2EN_MSK);
  2301. break;
  2302. case TIMER_SRC_TI1:
  2303. timer_ti1_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
  2304. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1FP1 << TIMER_SMCON_TSSEL_POSS);
  2305. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2306. break;
  2307. case TIMER_SRC_TI2:
  2308. timer_ti2_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
  2309. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI2FP2 << TIMER_SMCON_TSSEL_POSS);
  2310. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2311. break;
  2312. case TIMER_SRC_TI1ED:
  2313. timer_ti1_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
  2314. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1F_ED << TIMER_SMCON_TSSEL_POSS);
  2315. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2316. break;
  2317. case TIMER_SRC_ITR0:
  2318. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR0 << TIMER_SMCON_TSSEL_POSS);
  2319. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2320. break;
  2321. case TIMER_SRC_ITR1:
  2322. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR1 << TIMER_SMCON_TSSEL_POSS);
  2323. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2324. break;
  2325. case TIMER_SRC_ITR2:
  2326. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR2 << TIMER_SMCON_TSSEL_POSS);
  2327. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2328. break;
  2329. case TIMER_SRC_ITR3:
  2330. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR3 << TIMER_SMCON_TSSEL_POSS);
  2331. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
  2332. break;
  2333. default:
  2334. break;
  2335. }
  2336. hperh->state = TIMER_STATE_READY;
  2337. __UNLOCK(hperh);
  2338. return OK;
  2339. }
  2340. /**
  2341. * @brief Selects the signal connected to the TI1 input: direct from CH1_input
  2342. * or a XOR combination between CH1_input, CH2_input & CH3_input
  2343. * @param hperh: TIMER handle.
  2344. * @param ti1_select: Indicate whether or not channel 1 is connected to the
  2345. * output of a XOR gate.
  2346. * This parameter can be one of the following values:
  2347. * @arg 0: The TIMERx_CH1 pin is connected to TI1 input
  2348. * @arg 1: The TIMERx_CH1, CH2 and CH3
  2349. * pins are connected to the TI1 input (XOR combination)
  2350. * @retval Status, see @ref ald_status_t.
  2351. */
  2352. ald_status_t timer_config_ti1_input(timer_handle_t *hperh, uint32_t ti1_select)
  2353. {
  2354. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2355. MODIFY_REG(hperh->perh->CON2, TIMER_CON2_I1FSEL_MSK, ti1_select << TIMER_CON2_I1FSEL_POS);
  2356. return OK;
  2357. }
  2358. /**
  2359. * @brief Configures the TIMER in Slave mode
  2360. * @param hperh: TIMER handle.
  2361. * @param config: pointer to a timer_slave_config_t structure that
  2362. * contains the selected trigger (internal trigger input, filtered
  2363. * timerer input or external trigger input) and the Slave
  2364. * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
  2365. * @retval Status, see @ref ald_status_t.
  2366. */
  2367. ald_status_t timer_slave_config_sync(timer_handle_t *hperh, timer_slave_config_t *config)
  2368. {
  2369. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2370. assert_param(IS_TIMER_SLAVE_MODE(config->mode));
  2371. assert_param(IS_TIMER_TS(config->input));
  2372. assert_param(IS_TIMER_CLOCK_POLARITY(config->polarity));
  2373. assert_param(IS_TIMER_ETR_PSC(config->psc));
  2374. assert_param(IS_TIMER_IC_FILTER(config->filter));
  2375. __LOCK(hperh);
  2376. hperh->state = TIMER_STATE_BUSY;
  2377. timer_slave_set_config(hperh, config);
  2378. timer_interrupt_config(hperh, TIMER_IT_TRIGGER, DISABLE);
  2379. timer_dma_req_config(hperh, TIMER_DMA_TRIGGER, DISABLE);
  2380. hperh->state = TIMER_STATE_READY;
  2381. __UNLOCK(hperh);
  2382. return OK;
  2383. }
  2384. /**
  2385. * @brief Configures the TIMER in Slave mode in interrupt mode
  2386. * @param hperh: TIMER handle.
  2387. * @param config: pointer to a timer_slave_config_t structure that
  2388. * contains the selected trigger (internal trigger input, filtered
  2389. * timerer input or external trigger input) and the ) and the Slave
  2390. * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
  2391. * @retval Status, see @ref ald_status_t.
  2392. */
  2393. ald_status_t timer_slave_config_sync_by_it(timer_handle_t *hperh, timer_slave_config_t *config)
  2394. {
  2395. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2396. assert_param(IS_TIMER_SLAVE_MODE(config->mode));
  2397. assert_param(IS_TIMER_TS(config->input));
  2398. assert_param(IS_TIMER_CLOCK_POLARITY(config->polarity));
  2399. assert_param(IS_TIMER_ETR_PSC(config->psc));
  2400. assert_param(IS_TIMER_IC_FILTER(config->filter));
  2401. __LOCK(hperh);
  2402. hperh->state = TIMER_STATE_BUSY;
  2403. timer_slave_set_config(hperh, config);
  2404. timer_interrupt_config(hperh, TIMER_IT_TRIGGER, ENABLE);
  2405. timer_dma_req_config(hperh, TIMER_DMA_TRIGGER, DISABLE);
  2406. hperh->state = TIMER_STATE_READY;
  2407. __UNLOCK(hperh);
  2408. return OK;
  2409. }
  2410. /**
  2411. * @brief Generate a software event
  2412. * @param hperh: TIMER handle
  2413. * @param event: specifies the event source.
  2414. * @retval Status, see @ref ald_status_t.
  2415. */
  2416. ald_status_t timer_generate_event(timer_handle_t *hperh, timer_event_source_t event)
  2417. {
  2418. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2419. assert_param(IS_TIMER_EVENT_SOURCE(event));
  2420. __LOCK(hperh);
  2421. hperh->state = TIMER_STATE_BUSY;
  2422. WRITE_REG(hperh->perh->SGE, event);
  2423. hperh->state = TIMER_STATE_READY;
  2424. __UNLOCK(hperh);
  2425. return OK;
  2426. }
  2427. /**
  2428. * @brief Read the captured value from Capture Compare unit
  2429. * @param hperh: TIMER handle.
  2430. * @param ch: TIMER Channels to be enabled
  2431. * This parameter can be one of the following values:
  2432. * @arg TIMER_CHANNEL_1 : TIMER Channel 1 selected
  2433. * @arg TIMER_CHANNEL_2 : TIMER Channel 2 selected
  2434. * @arg TIMER_CHANNEL_3 : TIMER Channel 3 selected
  2435. * @arg TIMER_CHANNEL_4 : TIMER Channel 4 selected
  2436. * @retval Captured value
  2437. */
  2438. uint32_t timer_read_capture_value(timer_handle_t *hperh, timer_channel_t ch)
  2439. {
  2440. uint32_t tmp;
  2441. __LOCK(hperh);
  2442. hperh->state = TIMER_STATE_BUSY;
  2443. switch (ch) {
  2444. case TIMER_CHANNEL_1:
  2445. tmp = hperh->perh->CCVAL1;
  2446. break;
  2447. case TIMER_CHANNEL_2:
  2448. tmp = hperh->perh->CCVAL2;
  2449. break;
  2450. case TIMER_CHANNEL_3:
  2451. tmp = hperh->perh->CCVAL3;
  2452. break;
  2453. case TIMER_CHANNEL_4:
  2454. tmp = hperh->perh->CCVAL4;
  2455. break;
  2456. default:
  2457. break;
  2458. }
  2459. hperh->state = TIMER_STATE_READY;
  2460. __UNLOCK(hperh);
  2461. return tmp;
  2462. }
  2463. /**
  2464. * @brief Sets TIMER output mode.
  2465. * @param hperh: TIMER handle.
  2466. * @param mode: TIMER output mode.
  2467. * @param ch: TIMER Channels.
  2468. * This parameter can be one of the following values:
  2469. * @arg TIMER_CHANNEL_1 : TIMER Channel 1 selected
  2470. * @arg TIMER_CHANNEL_2 : TIMER Channel 2 selected
  2471. * @arg TIMER_CHANNEL_3 : TIMER Channel 3 selected
  2472. * @arg TIMER_CHANNEL_4 : TIMER Channel 4 selected
  2473. * @retval None
  2474. */
  2475. void timer_set_output_mode(timer_handle_t *hperh, timer_oc_mode_t mode, timer_channel_t ch)
  2476. {
  2477. assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
  2478. assert_param(IS_TIMER_OC_MODE(mode));
  2479. assert_param(IS_TIMER_CHANNELS(ch));
  2480. switch (ch) {
  2481. case TIMER_CHANNEL_1:
  2482. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH1OMOD_MSK, mode << TIMER_CHMR1_CH1OMOD_POSS);
  2483. break;
  2484. case TIMER_CHANNEL_2:
  2485. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH2OMOD_MSK, mode << TIMER_CHMR1_CH2OMOD_POSS);
  2486. break;
  2487. case TIMER_CHANNEL_3:
  2488. MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH3OMOD_MSK, mode << TIMER_CHMR2_CH3OMOD_POSS);
  2489. break;
  2490. case TIMER_CHANNEL_4:
  2491. MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH4OMOD_MSK, mode << TIMER_CHMR2_CH4OMOD_POSS);
  2492. break;
  2493. default:
  2494. break;
  2495. }
  2496. return;
  2497. }
  2498. /**
  2499. * @brief Configure the channel in commutation event.
  2500. * @param hperh: TIMER handel
  2501. * @param config: Parameters of the channel.
  2502. * @retval None
  2503. */
  2504. void timer_com_change_config(timer_handle_t *hperh, timer_com_channel_config_t *config)
  2505. {
  2506. uint32_t cm1, cm2, cce;
  2507. assert_param(IS_TIMER_COM_EVENT_INSTANCE(hperh->perh));
  2508. assert_param(IS_FUNC_STATE(config->ch[0].en));
  2509. assert_param(IS_FUNC_STATE(config->ch[0].n_en));
  2510. assert_param(IS_TIMER_OC_MODE(config->ch[0].mode));
  2511. assert_param(IS_FUNC_STATE(config->ch[1].en));
  2512. assert_param(IS_FUNC_STATE(config->ch[1].n_en));
  2513. assert_param(IS_TIMER_OC_MODE(config->ch[1].mode));
  2514. assert_param(IS_FUNC_STATE(config->ch[2].en));
  2515. assert_param(IS_FUNC_STATE(config->ch[2].n_en));
  2516. assert_param(IS_TIMER_OC_MODE(config->ch[2].mode));
  2517. TIMER_MOE_DISABLE(hperh);
  2518. TIMER_DISABLE(hperh);
  2519. cm1 = hperh->perh->CHMR1;
  2520. cm2 = hperh->perh->CHMR2;
  2521. cce = hperh->perh->CCEP;
  2522. MODIFY_REG(cm1, (0x7 << 4), (config->ch[0].mode << 4));
  2523. MODIFY_REG(cm1, (0x7 << 12), (config->ch[1].mode << 12));
  2524. MODIFY_REG(cm2, (0x7 << 4), (config->ch[2].mode << 4));
  2525. MODIFY_REG(cce, (0x1 << 0), (config->ch[0].en << 0));
  2526. MODIFY_REG(cce, (0x1 << 2), (config->ch[0].n_en << 2));
  2527. MODIFY_REG(cce, (0x1 << 4), (config->ch[1].en << 4));
  2528. MODIFY_REG(cce, (0x1 << 6), (config->ch[1].n_en << 6));
  2529. MODIFY_REG(cce, (0x1 << 8), (config->ch[2].en << 8));
  2530. MODIFY_REG(cce, (0x1 << 10), (config->ch[2].n_en << 10));
  2531. WRITE_REG(hperh->perh->CHMR1, cm1);
  2532. WRITE_REG(hperh->perh->CHMR2, cm2);
  2533. WRITE_REG(hperh->perh->CCEP, cce);
  2534. TIMER_MOE_ENABLE(hperh);
  2535. TIMER_ENABLE(hperh);
  2536. return;
  2537. }
  2538. /**
  2539. * @brief Configure the TIMER commutation event sequence.
  2540. * @param hperh: TIMER handel
  2541. * @param ts: the internal trigger corresponding to the timerer interfacing
  2542. * with the hall sensor.
  2543. * This parameter can be one of the following values:
  2544. * @arg TIMER_TS_ITR0
  2545. * @arg TIMER_TS_ITR1
  2546. * @arg TIMER_TS_ITR2
  2547. * @arg TIMER_TS_ITR3
  2548. * @param trgi: the commutation event source.
  2549. * This parameter can be one of the following values:
  2550. * @arg ENABLE: Commutation event source is TRGI
  2551. * @arg DISABLE: Commutation event source is set by software using the COMG bit
  2552. * @retval None
  2553. */
  2554. void timer_com_event_config(timer_handle_t *hperh, timer_ts_t ts, type_func_t trgi)
  2555. {
  2556. assert_param(IS_TIMER_COM_EVENT_INSTANCE(hperh->perh));
  2557. assert_param(IS_TIMER_TS(ts));
  2558. assert_param(IS_FUNC_STATE(trgi));
  2559. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, ts << TIMER_SMCON_TSSEL_POSS);
  2560. SET_BIT(hperh->perh->CON2, TIMER_CON2_CCPCEN_MSK);
  2561. MODIFY_REG(hperh->perh->CON2, TIMER_CON2_CCUSEL_MSK, trgi << TIMER_CON2_CCUSEL_POS);
  2562. return;
  2563. }
  2564. /**
  2565. * @brief Configure the TIMER commutation event sequence with interrupt.
  2566. * @param hperh: TIMER handel
  2567. * @param ts: the internal trigger corresponding to the timerer interfacing
  2568. * with the hall sensor.
  2569. * This parameter can be one of the following values:
  2570. * @arg TIMER_TS_ITR0
  2571. * @arg TIMER_TS_ITR1
  2572. * @arg TIMER_TS_ITR2
  2573. * @arg TIMER_TS_ITR3
  2574. * @param trgi: the commutation event source.
  2575. * This parameter can be one of the following values:
  2576. * @arg ENABLE: Commutation event source is TRGI
  2577. * @arg DISABLE: Commutation event source is set by software using the COMG bit
  2578. * @retval None
  2579. */
  2580. void timer_com_event_config_it(timer_handle_t *hperh, timer_ts_t ts, type_func_t trgi)
  2581. {
  2582. timer_com_event_config(hperh, ts, trgi);
  2583. timer_interrupt_config(hperh, TIMER_IT_COM, ENABLE);
  2584. }
  2585. /**
  2586. * @brief Configure the break, dead timere, lock level state.
  2587. * @param hperh: TIMER handle
  2588. * @param config: Pointer to the timer_break_dead_timere_t structure.
  2589. * @retval None
  2590. */
  2591. void timer_break_dead_time_config(timer_handle_t *hperh, timer_break_dead_time_t *config)
  2592. {
  2593. uint32_t tmp;
  2594. assert_param(IS_TIMER_BREAK_INSTANCE(hperh->perh));
  2595. assert_param(IS_FUNC_STATE(config->off_run));
  2596. assert_param(IS_FUNC_STATE(config->off_idle));
  2597. assert_param(IS_TIMER_CLOCK_LEVEL(config->lock_level));
  2598. assert_param(IS_TIMER_DEAD_TIMERE(config->dead_time));
  2599. assert_param(IS_FUNC_STATE(config->break_state));
  2600. assert_param(IS_TIMER_BREAK_POLARITY(config->polarity));
  2601. assert_param(IS_FUNC_STATE(config->auto_out));
  2602. tmp = READ_REG(hperh->perh->BDCFG);
  2603. MODIFY_REG(tmp, TIMER_BDCFG_OFFSSR_MSK, config->off_run << TIMER_BDCFG_OFFSSR_POS);
  2604. MODIFY_REG(tmp, TIMER_BDCFG_OFFSSI_MSK, config->off_idle << TIMER_BDCFG_OFFSSI_POS);
  2605. MODIFY_REG(tmp, TIMER_BDCFG_LOCKLVL_MSK, config->lock_level << TIMER_BDCFG_LOCKLVL_POSS);
  2606. MODIFY_REG(tmp, TIMER_BDCFG_DT_MSK, config->dead_time << TIMER_BDCFG_DT_POSS);
  2607. MODIFY_REG(tmp, TIMER_BDCFG_BRKEN_MSK, config->break_state << TIMER_BDCFG_BRKEN_POS);
  2608. MODIFY_REG(tmp, TIMER_BDCFG_BRKP_MSK, config->polarity << TIMER_BDCFG_BRKP_POS);
  2609. MODIFY_REG(tmp, TIMER_BDCFG_AOEN_MSK, config->auto_out << TIMER_BDCFG_AOEN_POS);
  2610. WRITE_REG(hperh->perh->BDCFG, tmp);
  2611. hperh->state = TIMER_STATE_READY;
  2612. return;
  2613. }
  2614. /**
  2615. * @brief Configure the master mode
  2616. * @param hperh: TIMER handle
  2617. * @param config: Pointer to the timer_master_config_t structure.
  2618. * @retval None
  2619. */
  2620. void timer_master_sync_config(timer_handle_t *hperh, timer_master_config_t *config)
  2621. {
  2622. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2623. assert_param(IS_TIMER_MASTER_MODE_SEL(config->sel));
  2624. assert_param(IS_FUNC_STATE(config->master_en));
  2625. hperh->state = TIMER_STATE_BUSY;
  2626. MODIFY_REG(hperh->perh->CON2, TIMER_CON2_TRGOSEL_MSK, config->sel << TIMER_CON2_TRGOSEL_POSS);
  2627. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_MSCFG_MSK, config->master_en << TIMER_SMCON_MSCFG_POS);
  2628. hperh->state = TIMER_STATE_READY;
  2629. return;
  2630. }
  2631. /**
  2632. * @brief This function handles TIMER interrupts requests.
  2633. * @param hperh: TIMER handle
  2634. * @retval None
  2635. */
  2636. void timer_irq_handle(timer_handle_t *hperh)
  2637. {
  2638. uint32_t reg = hperh->perh->IFM;
  2639. /* Capture or compare 1 event */
  2640. if (READ_BIT(reg, TIMER_FLAG_CC1)) {
  2641. timer_clear_flag_status(hperh, TIMER_FLAG_CC1);
  2642. hperh->ch = TIMER_ACTIVE_CHANNEL_1;
  2643. /* Input capture event */
  2644. if (READ_BIT(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK)) {
  2645. if (hperh->capture_cbk)
  2646. hperh->capture_cbk(hperh);
  2647. }
  2648. else { /* Output compare event */
  2649. if (hperh->delay_elapse_cbk)
  2650. hperh->delay_elapse_cbk(hperh);
  2651. if (hperh->pwm_pulse_finish_cbk)
  2652. hperh->pwm_pulse_finish_cbk(hperh);
  2653. }
  2654. hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
  2655. }
  2656. /* Capture or compare 2 event */
  2657. if (READ_BIT(reg, TIMER_FLAG_CC2)) {
  2658. timer_clear_flag_status(hperh, TIMER_FLAG_CC2);
  2659. hperh->ch = TIMER_ACTIVE_CHANNEL_2;
  2660. /* Input capture event */
  2661. if (READ_BIT(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK)) {
  2662. if (hperh->capture_cbk)
  2663. hperh->capture_cbk(hperh);
  2664. }
  2665. else { /* Output compare event */
  2666. if (hperh->delay_elapse_cbk)
  2667. hperh->delay_elapse_cbk(hperh);
  2668. if (hperh->pwm_pulse_finish_cbk)
  2669. hperh->pwm_pulse_finish_cbk(hperh);
  2670. }
  2671. hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
  2672. }
  2673. /* Capture or compare 3 event */
  2674. if (READ_BIT(reg, TIMER_FLAG_CC3)) {
  2675. timer_clear_flag_status(hperh, TIMER_FLAG_CC3);
  2676. hperh->ch = TIMER_ACTIVE_CHANNEL_3;
  2677. /* Input capture event */
  2678. if (READ_BIT(hperh->perh->CHMR2, TIMER_CHMR2_CC3SSEL_MSK)) {
  2679. if (hperh->capture_cbk)
  2680. hperh->capture_cbk(hperh);
  2681. }
  2682. else { /* Output compare event */
  2683. if (hperh->delay_elapse_cbk)
  2684. hperh->delay_elapse_cbk(hperh);
  2685. if (hperh->pwm_pulse_finish_cbk)
  2686. hperh->pwm_pulse_finish_cbk(hperh);
  2687. }
  2688. hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
  2689. }
  2690. /* Capture or compare 4 event */
  2691. if (READ_BIT(reg, TIMER_FLAG_CC4)) {
  2692. timer_clear_flag_status(hperh, TIMER_FLAG_CC4);
  2693. hperh->ch = TIMER_ACTIVE_CHANNEL_4;
  2694. /* Input capture event */
  2695. if (READ_BIT(hperh->perh->CHMR2, TIMER_CHMR2_CC4SSEL_MSK)) {
  2696. if (hperh->capture_cbk)
  2697. hperh->capture_cbk(hperh);
  2698. }
  2699. else { /* Output compare event */
  2700. if (hperh->delay_elapse_cbk)
  2701. hperh->delay_elapse_cbk(hperh);
  2702. if (hperh->pwm_pulse_finish_cbk)
  2703. hperh->pwm_pulse_finish_cbk(hperh);
  2704. }
  2705. hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
  2706. }
  2707. /* TIMER Update event */
  2708. if (READ_BIT(reg, TIMER_FLAG_UPDATE)) {
  2709. timer_clear_flag_status(hperh, TIMER_FLAG_UPDATE);
  2710. if (hperh->period_elapse_cbk)
  2711. hperh->period_elapse_cbk(hperh);
  2712. }
  2713. /* TIMER Break input event */
  2714. if (READ_BIT(reg, TIMER_FLAG_BREAK)) {
  2715. timer_clear_flag_status(hperh, TIMER_FLAG_BREAK);
  2716. if (hperh->break_cbk)
  2717. hperh->break_cbk(hperh);
  2718. }
  2719. /* TIMER Trigger detection event */
  2720. if (READ_BIT(reg, TIMER_FLAG_TRIGGER)) {
  2721. timer_clear_flag_status(hperh, TIMER_FLAG_TRIGGER);
  2722. if (hperh->trigger_cbk)
  2723. hperh->trigger_cbk(hperh);
  2724. }
  2725. /* TIMER commutation event */
  2726. if (READ_BIT(reg, TIMER_FLAG_COM)) {
  2727. timer_clear_flag_status(hperh, TIMER_FLAG_COM);
  2728. if (hperh->com_cbk)
  2729. hperh->com_cbk(hperh);
  2730. }
  2731. return;
  2732. }
  2733. /**
  2734. * @brief Configure DMA request source.
  2735. * @param hperh: TIMER handle
  2736. * @param req: DMA request source.
  2737. * @param state: New state of the specified DMA request.
  2738. * @retval None
  2739. */
  2740. void timer_dma_req_config(timer_handle_t *hperh, timer_dma_req_t req, type_func_t state)
  2741. {
  2742. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2743. assert_param(IS_TIMER_DMA_REQ(req));
  2744. assert_param(IS_FUNC_STATE(state));
  2745. if (state == ENABLE)
  2746. SET_BIT(hperh->perh->DIER, req);
  2747. else
  2748. CLEAR_BIT(hperh->perh->DIER, req);
  2749. return;
  2750. }
  2751. /**
  2752. * @brief Enable/disable the specified TIMER interrupts.
  2753. * @param hperh: Pointer to a timer_handle_t structure.
  2754. * @param it: Specifies the timer interrupt sources to be enabled or disabled.
  2755. * This parameter can be one of the @ref timer_it_t.
  2756. * @param state: New state of the specified TIMER interrupts.
  2757. * This parameter can be:
  2758. * @arg ENABLE
  2759. * @arg DISABLE
  2760. * @retval None
  2761. */
  2762. void timer_interrupt_config(timer_handle_t *hperh, timer_it_t it, type_func_t state)
  2763. {
  2764. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2765. assert_param(IS_TIMER_IT(it));
  2766. assert_param(IS_FUNC_STATE(state));
  2767. if (state == ENABLE)
  2768. SET_BIT(hperh->perh->DIER, it);
  2769. else
  2770. CLEAR_BIT(hperh->perh->DIER, it);
  2771. return;
  2772. }
  2773. /**
  2774. * @brief Get the status of TIMER interrupt source.
  2775. * @param hperh: Pointer to a timer_handle_t structure.
  2776. * @param it: Specifies the TIMER interrupt source.
  2777. * This parameter can be one of the @ref timer_it_t.
  2778. * @retval Status:
  2779. * - 0: RESET
  2780. * - 1: SET
  2781. */
  2782. it_status_t timer_get_it_status(timer_handle_t *hperh, timer_it_t it)
  2783. {
  2784. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2785. assert_param(IS_TIMER_IT(it));
  2786. if (hperh->perh->DIVS & it)
  2787. return SET;
  2788. return RESET;
  2789. }
  2790. /**
  2791. * @brief Get the status of TIMER interrupt flag.
  2792. * @param hperh: Pointer to a timer_handle_t structure.
  2793. * @param flag: Specifies the TIMER interrupt flag.
  2794. * This parameter can be one of the @ref timer_flag_t.
  2795. * @retval Status:
  2796. * - 0: RESET
  2797. * - 1: SET
  2798. */
  2799. flag_status_t timer_get_flag_status(timer_handle_t *hperh, timer_flag_t flag)
  2800. {
  2801. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2802. assert_param(IS_TIMER_FLAG(flag));
  2803. if (hperh->perh->RIF & flag)
  2804. return SET;
  2805. return RESET;
  2806. }
  2807. /**
  2808. * @brief Clear the TIMER interrupt flag.
  2809. * @param hperh: Pointer to a uart_handle_t structure.
  2810. * @param flag: Specifies the TIMER interrupt flag.
  2811. * This parameter can be one of the @ref timer_flag_t.
  2812. * @retval None
  2813. */
  2814. void timer_clear_flag_status(timer_handle_t *hperh, timer_flag_t flag)
  2815. {
  2816. assert_param(IS_TIMER_INSTANCE(hperh->perh));
  2817. assert_param(IS_TIMER_FLAG(flag));
  2818. hperh->perh->ICR = flag;
  2819. return;
  2820. }
  2821. /**
  2822. * @}
  2823. */
  2824. /** @defgroup TIMER_Public_Functions_Group12 Peripheral State functions
  2825. * @brief Peripheral State functions
  2826. *
  2827. * @verbatim
  2828. ==============================================================================
  2829. ##### Peripheral State functions #####
  2830. ==============================================================================
  2831. [..]
  2832. This subsection permit to get in run-timere the status of the peripheral
  2833. and the data flow.
  2834. @endverbatim
  2835. * @{
  2836. */
  2837. /**
  2838. * @brief Return the TIMER Base state
  2839. * @param hperh: TIMER handle
  2840. * @retval TIMER peripheral state
  2841. */
  2842. timer_state_t timer_get_state(timer_handle_t *hperh)
  2843. {
  2844. return hperh->state;
  2845. }
  2846. /**
  2847. * @}
  2848. */
  2849. /**
  2850. * @}
  2851. */
  2852. /** @addtogroup TIMER_Private_Functions
  2853. * @{
  2854. */
  2855. #ifdef ALD_DMA
  2856. /**
  2857. * @brief TIMER DMA out compare complete callback.
  2858. * @param arg: pointer to TIMER handle.
  2859. * @retval None
  2860. */
  2861. void timer_dma_oc_cplt(void *arg)
  2862. {
  2863. timer_handle_t *hperh = (timer_handle_t *)arg;
  2864. if (hperh->delay_elapse_cbk)
  2865. hperh->delay_elapse_cbk(hperh);
  2866. if (hperh->pwm_pulse_finish_cbk)
  2867. hperh->pwm_pulse_finish_cbk(hperh);
  2868. hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
  2869. return;
  2870. }
  2871. /**
  2872. * @brief TIMER DMA Capture complete callback.
  2873. * @param arg: pointer to TIMER handle.
  2874. * @retval None
  2875. */
  2876. void timer_dma_capture_cplt(void *arg)
  2877. {
  2878. timer_handle_t *hperh = (timer_handle_t *)arg;
  2879. if (hperh->capture_cbk)
  2880. hperh->capture_cbk(hperh);
  2881. hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
  2882. return;
  2883. }
  2884. /**
  2885. * @brief TIMER DMA Period Elapse complete callback.
  2886. * @param arg: pointer to TIMER handle.
  2887. * @retval None
  2888. */
  2889. void timer_dma_period_elapse_cplt(void *arg)
  2890. {
  2891. timer_handle_t *hperh = (timer_handle_t *)arg;
  2892. if (hperh->period_elapse_cbk)
  2893. hperh->period_elapse_cbk(hperh);
  2894. hperh->state = TIMER_STATE_READY;
  2895. return;
  2896. }
  2897. /**
  2898. * @brief TIMER DMA error callback
  2899. * @param arg: pointer to TIMER handle.
  2900. * @retval None
  2901. */
  2902. void timer_dma_error(void *arg)
  2903. {
  2904. timer_handle_t *hperh = (timer_handle_t *)arg;
  2905. hperh->state = TIMER_STATE_READY;
  2906. if (hperh->error_cbk)
  2907. hperh->error_cbk(hperh);
  2908. return;
  2909. }
  2910. #endif
  2911. /**
  2912. * @brief Time Base configuration
  2913. * @param TIMERx: TIMER periheral
  2914. * @param init: TIMER Base configuration structure
  2915. * @retval None
  2916. */
  2917. static void timer_base_set_config(TIMER_TypeDef *TIMERx, timer_base_init_t *init)
  2918. {
  2919. assert_param(IS_TIMER_COUNTER_MODE(init->mode));
  2920. assert_param(IS_TIMER_CLOCK_DIVISION(init->clk_div));
  2921. if (init->mode == TIMER_CNT_MODE_UP || init->mode == TIMER_CNT_MODE_DOWN) {
  2922. CLEAR_BIT(TIMERx->CON1, TIMER_CON1_CMSEL_MSK);
  2923. MODIFY_REG(TIMERx->CON1, TIMER_CON1_DIRSEL_MSK, init->mode << TIMER_CON1_DIRSEL_POS);
  2924. }
  2925. else {
  2926. MODIFY_REG(TIMERx->CON1, TIMER_CON1_CMSEL_MSK, (init->mode - 1) << TIMER_CON1_CMSEL_POSS);
  2927. }
  2928. if (IS_TIMER_CLOCK_DIVISION_INSTANCE(TIMERx))
  2929. MODIFY_REG(TIMERx->CON1, TIMER_CON1_DFCKSEL_MSK, init->clk_div << TIMER_CON1_DFCKSEL_POSS);
  2930. WRITE_REG(TIMERx->AR, init->period);
  2931. WRITE_REG(TIMERx->PRES, init->prescaler);
  2932. if (IS_TIMER_REPETITION_COUNTER_INSTANCE(TIMERx))
  2933. WRITE_REG(TIMERx->REPAR, init->re_cnt);
  2934. return;
  2935. }
  2936. /**
  2937. * @brief Time Ouput Compare 1 configuration
  2938. * @param TIMERx: Select the TIMER peripheral
  2939. * @param oc_config: The ouput configuration structure
  2940. * @retval None
  2941. */
  2942. static void timer_oc1_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
  2943. {
  2944. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC1EN_MSK);
  2945. CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CH1OMOD_MSK);
  2946. CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CC1SSEL_MSK);
  2947. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CH1OMOD_MSK, oc_config->oc_mode << TIMER_CHMR1_CH1OMOD_POSS);
  2948. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC1POL_POS);
  2949. if (IS_TIMER_CCXN_INSTANCE(TIMERx, TIMER_CHANNEL_1)) {
  2950. assert_param(IS_TIMER_OCN_POLARITY(oc_config->ocn_polarity));
  2951. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NPOL_MSK, oc_config->ocn_polarity << TIMER_CCEP_CC1NPOL_POS);
  2952. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC1NEN_MSK);
  2953. }
  2954. if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
  2955. assert_param(IS_TIMER_OCNIDLE_STATE(oc_config->ocn_idle));
  2956. assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
  2957. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS1_MSK, oc_config->oc_idle << TIMER_CON2_OISS1_POS);
  2958. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS1N_MSK, oc_config->ocn_idle << TIMER_CON2_OISS1N_POS);
  2959. }
  2960. WRITE_REG(TIMERx->CCVAL1, oc_config->pulse);
  2961. }
  2962. /**
  2963. * @brief Time Ouput Compare 2 configuration
  2964. * @param TIMERx: Select the TIMER peripheral
  2965. * @param oc_config: The ouput configuration structure
  2966. * @retval None
  2967. */
  2968. static void timer_oc2_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
  2969. {
  2970. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC2EN_MSK);
  2971. CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CH2OMOD_MSK);
  2972. CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CC2SSEL_MSK);
  2973. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CH2OMOD_MSK, oc_config->oc_mode << TIMER_CHMR1_CH2OMOD_POSS);
  2974. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC2POL_POS);
  2975. if (IS_TIMER_CCXN_INSTANCE(TIMERx, TIMER_CHANNEL_2)) {
  2976. assert_param(IS_TIMER_OCN_POLARITY(oc_config->ocn_polarity));
  2977. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NPOL_MSK, oc_config->ocn_polarity << TIMER_CCEP_CC2NPOL_POS);
  2978. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC2NEN_MSK);
  2979. }
  2980. if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
  2981. assert_param(IS_TIMER_OCNIDLE_STATE(oc_config->ocn_idle));
  2982. assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
  2983. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS2_MSK, oc_config->oc_idle << TIMER_CON2_OISS2_POS);
  2984. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS2N_MSK, oc_config->ocn_idle << TIMER_CON2_OISS2N_POS);
  2985. }
  2986. WRITE_REG(TIMERx->CCVAL2, oc_config->pulse);
  2987. }
  2988. /**
  2989. * @brief Time Ouput Compare 3 configuration
  2990. * @param TIMERx: Select the TIMER peripheral
  2991. * @param oc_config: The ouput configuration structure
  2992. * @retval None
  2993. */
  2994. static void timer_oc3_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
  2995. {
  2996. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC3EN_MSK);
  2997. CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CH3OMOD_MSK);
  2998. CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CC3SSEL_MSK);
  2999. MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CH3OMOD_MSK, oc_config->oc_mode << TIMER_CHMR2_CH3OMOD_POSS);
  3000. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC3POL_POS);
  3001. if (IS_TIMER_CCXN_INSTANCE(TIMERx, TIMER_CHANNEL_3)) {
  3002. assert_param(IS_TIMER_OCN_POLARITY(oc_config->ocn_polarity));
  3003. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3NPOL_MSK, oc_config->ocn_polarity << TIMER_CCEP_CC3NPOL_POS);
  3004. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC3NEN_MSK);
  3005. }
  3006. if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
  3007. assert_param(IS_TIMER_OCNIDLE_STATE(oc_config->ocn_idle));
  3008. assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
  3009. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS3_MSK, oc_config->oc_idle << TIMER_CON2_OISS3_POS);
  3010. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS3N_MSK, oc_config->ocn_idle << TIMER_CON2_OISS3N_POS);
  3011. }
  3012. WRITE_REG(TIMERx->CCVAL3, oc_config->pulse);
  3013. }
  3014. /**
  3015. * @brief Time Ouput Compare 4 configuration
  3016. * @param TIMERx: Select the TIMER peripheral
  3017. * @param oc_config: The ouput configuration structure
  3018. * @retval None
  3019. */
  3020. static void timer_oc4_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
  3021. {
  3022. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC4EN_MSK);
  3023. CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CH4OMOD_MSK);
  3024. CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CC4SSEL_MSK);
  3025. MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CH4OMOD_MSK, oc_config->oc_mode << TIMER_CHMR2_CH4OMOD_POSS);
  3026. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC4POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC4POL_POS);
  3027. if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
  3028. assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
  3029. MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS4_MSK, oc_config->oc_idle << TIMER_CON2_OISS4_POS);
  3030. }
  3031. WRITE_REG(TIMERx->CCVAL4, oc_config->pulse);
  3032. }
  3033. /**
  3034. * @brief Enables or disables the TIMER Capture Compare Channel x.
  3035. * @param TIMERx: Select the TIMER peripheral
  3036. * @param ch: specifies the TIMER Channel
  3037. * This parameter can be one of the following values:
  3038. * @arg TIMER_CHANNEL_1: TIMER Channel 1
  3039. * @arg TIMER_CHANNEL_2: TIMER Channel 2
  3040. * @arg TIMER_CHANNEL_3: TIMER Channel 3
  3041. * @arg TIMER_CHANNEL_4: TIMER Channel 4
  3042. * @param state: specifies the TIMER Channel CCxE bit new state.
  3043. * @retval None
  3044. */
  3045. static void timer_ccx_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state)
  3046. {
  3047. assert_param(IS_TIMER_CC2_INSTANCE(TIMERx));
  3048. assert_param(IS_TIMER_CHANNELS(ch));
  3049. switch (ch) {
  3050. case TIMER_CHANNEL_1:
  3051. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1EN_MSK, state << TIMER_CCEP_CC1EN_POS);
  3052. break;
  3053. case TIMER_CHANNEL_2:
  3054. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2EN_MSK, state << TIMER_CCEP_CC2EN_POS);
  3055. break;
  3056. case TIMER_CHANNEL_3:
  3057. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3EN_MSK, state << TIMER_CCEP_CC3EN_POS);
  3058. break;
  3059. case TIMER_CHANNEL_4:
  3060. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC4EN_MSK, state << TIMER_CCEP_CC4EN_POS);
  3061. break;
  3062. default:
  3063. break;
  3064. }
  3065. }
  3066. /**
  3067. * @brief Enables or disables the TIMER Capture Compare Channel xN.
  3068. * @param TIMERx: Select the TIMER peripheral
  3069. * @param ch: specifies the TIMER Channel
  3070. * This parameter can be one of the following values:
  3071. * @arg TIMER_CHANNEL_1: TIMER Channel 1
  3072. * @arg TIMER_CHANNEL_2: TIMER Channel 2
  3073. * @arg TIMER_CHANNEL_3: TIMER Channel 3
  3074. * @param state: specifies the TIMER Channel CCxNE bit new state.
  3075. * @retval None
  3076. */
  3077. static void timer_ccxn_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state)
  3078. {
  3079. switch (ch) {
  3080. case TIMER_CHANNEL_1:
  3081. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NEN_MSK, state << TIMER_CCEP_CC1NEN_POS);
  3082. break;
  3083. case TIMER_CHANNEL_2:
  3084. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NEN_MSK, state << TIMER_CCEP_CC2NEN_POS);
  3085. break;
  3086. case TIMER_CHANNEL_3:
  3087. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3NEN_MSK, state << TIMER_CCEP_CC3NEN_POS);
  3088. break;
  3089. default:
  3090. break;
  3091. }
  3092. }
  3093. /**
  3094. * @brief Configure the TI1 as Input.
  3095. * @param TIMERx: Select the TIMER peripheral.
  3096. * @param polarity: The Input Polarity.
  3097. * @param sel: specifies the input to be used.
  3098. * @param filter: Specifies the Input Capture Filter.
  3099. * This parameter must be a value between 0x00 and 0x0F.
  3100. * @retval None
  3101. */
  3102. static void timer_ti1_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  3103. timer_ic_select_t sel, uint32_t filter)
  3104. {
  3105. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC1EN_MSK);
  3106. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, sel << TIMER_CHMR1_CC1SSEL_POSS);
  3107. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I1FLT_MSK, filter << TIMER_CHMR1_I1FLT_POSS);
  3108. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC1POL_POS);
  3109. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC1NPOL_POS);
  3110. return;
  3111. }
  3112. /**
  3113. * @brief Configure the Polarity and Filter for TI1.
  3114. * @param TIMERx: Select the TIMER peripheral.
  3115. * @param polarity: The Input Polarity.
  3116. * @param filter: Specifies the Input Capture Filter.
  3117. * This parameter must be a value between 0x00 and 0x0F.
  3118. * @retval None
  3119. */
  3120. static void timer_ti1_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter)
  3121. {
  3122. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I1FLT_MSK, filter << TIMER_CHMR1_I1FLT_POSS);
  3123. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC1POL_POS);
  3124. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC1NPOL_POS);
  3125. return;
  3126. }
  3127. /**
  3128. * @brief Configure the TI2 as Input.
  3129. * @param TIMERx: Select the TIMER peripheral.
  3130. * @param polarity: The Input Polarity.
  3131. * @param sel: specifies the input to be used.
  3132. * @param filter: Specifies the Input Capture Filter.
  3133. * This parameter must be a value between 0x00 and 0x0F.
  3134. * @retval None
  3135. */
  3136. static void timer_ti2_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  3137. timer_ic_select_t sel, uint32_t filter)
  3138. {
  3139. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC2EN_MSK);
  3140. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, sel << TIMER_CHMR1_CC2SSEL_POSS);
  3141. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I2FLT_MSK, filter << TIMER_CHMR1_I2FLT_POSS);
  3142. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC2POL_POS);
  3143. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC2NPOL_POS);
  3144. return;
  3145. }
  3146. /**
  3147. * @brief Configure the Polarity and Filter for TI2.
  3148. * @param TIMERx: Select the TIMER peripheral.
  3149. * @param polarity: The Input Polarity.
  3150. * @param filter: Specifies the Input Capture Filter.
  3151. * This parameter must be a value between 0x00 and 0x0F.
  3152. * @retval None
  3153. */
  3154. static void timer_ti2_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter)
  3155. {
  3156. MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I2FLT_MSK, filter << TIMER_CHMR1_I2FLT_POSS);
  3157. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC2POL_POS);
  3158. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC2NPOL_POS);
  3159. return;
  3160. }
  3161. /**
  3162. * @brief Configure the TI3 as Input.
  3163. * @param TIMERx: Select the TIMER peripheral.
  3164. * @param polarity: The Input Polarity.
  3165. * @param sel: specifies the input to be used.
  3166. * @param filter: Specifies the Input Capture Filter.
  3167. * This parameter must be a value between 0x00 and 0x0F.
  3168. * @retval None
  3169. */
  3170. static void timer_ti3_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  3171. timer_ic_select_t sel, uint32_t filter)
  3172. {
  3173. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC3EN_MSK);
  3174. MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CC3SSEL_MSK, sel << TIMER_CHMR2_CC3SSEL_POSS);
  3175. MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_I3FLT_MSK, filter << TIMER_CHMR2_I3FLT_POSS);
  3176. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC3POL_POS);
  3177. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC3NPOL_POS);
  3178. return;
  3179. }
  3180. /**
  3181. * @brief Configure the TI4 as Input.
  3182. * @param TIMERx: Select the TIMER peripheral.
  3183. * @param polarity: The Input Polarity.
  3184. * @param sel: specifies the input to be used.
  3185. * @param filter: Specifies the Input Capture Filter.
  3186. * This parameter must be a value between 0x00 and 0x0F.
  3187. * @retval None
  3188. */
  3189. static void timer_ti4_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
  3190. timer_ic_select_t sel, uint32_t filter)
  3191. {
  3192. CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC4EN_MSK);
  3193. MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CC4SSEL_MSK, sel << TIMER_CHMR2_CC4SSEL_POSS);
  3194. MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_I4FLT_MSK, filter << TIMER_CHMR2_I4FLT_POSS);
  3195. MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC4POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC4POL_POS);
  3196. return;
  3197. }
  3198. /**
  3199. * @brief Configures the TIMERx External Trigger (ETR).
  3200. * @param TIMERx: Select the TIMER peripheral
  3201. * @param psc: The external Trigger Prescaler.
  3202. * @param polarity: The external Trigger Polarity.
  3203. * @param filter: External Trigger Filter.
  3204. * This parameter must be a value between 0x00 and 0x0F
  3205. * @retval None
  3206. */
  3207. static void timer_etr_set_config(TIMER_TypeDef* TIMERx, timer_etr_psc_t psc, timer_clock_polarity_t polarity, uint32_t filter)
  3208. {
  3209. MODIFY_REG(TIMERx->SMCON, TIMER_SMCON_ETFLT_MSK, filter << TIMER_SMCON_ETFLT_POSS);
  3210. MODIFY_REG(TIMERx->SMCON, TIMER_SMCON_ETPSEL_MSK, psc << TIMER_SMCON_ETPSEL_POSS);
  3211. CLEAR_BIT(TIMERx->SMCON, TIMER_SMCON_ECM2EN_MSK);
  3212. MODIFY_REG(TIMERx->SMCON, TIMER_SMCON_ETPOL_MSK, polarity << TIMER_SMCON_ETPOL_POS);
  3213. return;
  3214. }
  3215. /**
  3216. * @brief Time Slave configuration
  3217. * @param hperh: pointer to a timer_handle_t structure that contains
  3218. * the configuration information for TIMER module.
  3219. * @param config: The slave configuration structure
  3220. * @retval None
  3221. */
  3222. static void timer_slave_set_config(timer_handle_t *hperh, timer_slave_config_t *config)
  3223. {
  3224. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, config->input << TIMER_SMCON_TSSEL_POSS);
  3225. MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, config->mode << TIMER_SMCON_SMODS_POSS);
  3226. switch (config->input) {
  3227. case TIMER_TS_ETRF:
  3228. timer_etr_set_config(hperh->perh, config->psc, config->polarity, config->filter);
  3229. break;
  3230. case TIMER_TS_TI1F_ED:
  3231. CLEAR_BIT(hperh->perh->CCEP, TIMER_CCEP_CC1EN_MSK);
  3232. MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_I1FLT_MSK, config->filter << TIMER_CHMR1_I1FLT_POSS);
  3233. break;
  3234. case TIMER_TS_TI1FP1:
  3235. timer_ti1_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
  3236. break;
  3237. case TIMER_TS_TI2FP2:
  3238. timer_ti2_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
  3239. break;
  3240. default:
  3241. break;
  3242. }
  3243. }
  3244. /**
  3245. * @}
  3246. */
  3247. #endif /* ALD_TIMER */
  3248. /**
  3249. * @}
  3250. */
  3251. /**
  3252. * @}
  3253. */