ald_timer.c 129 KB

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