| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709 |
- /**
- *********************************************************************************
- *
- * @file ald_timer.c
- * @brief TIMER module driver.
- * This is the common part of the TIMER initialization
- *
- * @version V1.0
- * @date 06 Nov 2017
- * @author AE Team
- * @note
- *
- * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
- *
- *********************************************************************************
- */
- #include <string.h>
- #include "ald_timer.h"
- #include "ald_cmu.h"
- /** @addtogroup ES32FXXX_ALD
- * @{
- */
- /** @defgroup TIMER TIMER
- * @brief TIMER module driver
- * @{
- */
- #ifdef ALD_TIMER
- /** @defgroup TIMER_Private_Functions TIMER Private Functions
- * @{
- */
- static void timer_base_set_config(TIMER_TypeDef *TIMERx, timer_base_init_t *init);
- static void timer_oc1_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
- static void timer_oc2_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
- static void timer_oc3_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
- static void timer_oc4_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config);
- static void timer_ccx_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state);
- static void timer_ccxn_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state);
- static void timer_ti1_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter);
- static void timer_ti1_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter);
- static void timer_ti2_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter);
- static void timer_ti2_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter);
- static void timer_ti3_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter);
- static void timer_ti4_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter);
- static void timer_etr_set_config(TIMER_TypeDef* TIMERx, timer_etr_psc_t psc, timer_clock_polarity_t polarity, uint32_t filter);
- static void timer_slave_set_config(timer_handle_t *hperh, timer_slave_config_t *config);
- #ifdef ALD_DMA
- static void timer_dma_oc_cplt(void *arg);
- static void timer_dma_capture_cplt(void *arg);
- static void timer_dma_period_elapse_cplt(void *arg);
- static void timer_dma_error(void *arg);
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions TIMER Public Functions
- * @{
- */
- /** @defgroup TIMER_Public_Functions_Group1 TIMER Base functions
- * @brief Time Base functions
- *
- * @verbatim
- ==============================================================================
- ##### Timer Base functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER base.
- (+) Reset the TIMER base.
- (+) Start the Time Base.
- (+) Stop the Time Base.
- (+) Start the Time Base and enable interrupt.
- (+) Stop the Time Base and disable interrupt.
- (+) Start the Time Base and enable DMA transfer.
- (+) Stop the Time Base and disable DMA transfer.
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER Time base Unit according to the specified
- * parameters in the timer_handle_t and create the associated handle.
- * @param hperh: TIMER base handle
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_base_init(timer_handle_t *hperh)
- {
- if (hperh == NULL)
- return ERROR;
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_COUNTER_MODE(hperh->init.mode));
- assert_param(IS_TIMER_CLOCK_DIVISION(hperh->init.clk_div));
- if (hperh->state == TIMER_STATE_RESET)
- hperh->lock = UNLOCK;
- hperh->state = TIMER_STATE_BUSY;
- timer_base_set_config(hperh->perh, &hperh->init);
- hperh->state = TIMER_STATE_READY;
- return OK;
- }
- /**
- * @brief Reset the TIMER base peripheral
- * @param hperh: TIMER base handle
- * @retval Status, see @ref ald_status_t.
- */
- void timer_base_reset(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- hperh->state = TIMER_STATE_BUSY;
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_RESET;
- __UNLOCK(hperh);
- return;
- }
- /**
- * @brief Starts the TIMER Base generation.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_base_start(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- hperh->state = TIMER_STATE_BUSY;
- TIMER_ENABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- /**
- * @brief Stops the TIMER Base generation.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_base_stop(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- hperh->state = TIMER_STATE_BUSY;
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- /**
- * @brief Starts the TIMER Base generation in interrupt mode.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_base_start_by_it(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- timer_interrupt_config(hperh, TIMER_IT_UPDATE, ENABLE);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Base generation in interrupt mode.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_base_stop_by_it(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- timer_interrupt_config(hperh, TIMER_IT_UPDATE, DISABLE);
- TIMER_DISABLE(hperh);
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER Base generation in DMA mode.
- * @param hperh: TIMER handle
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The source Buffer address.
- * @param len: The length of buffer to be transferred from memory to TIMER peripheral
- * @param dma_ch: Channel of DMA.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_base_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
- uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- if ((hperh->state == TIMER_STATE_BUSY))
- return BUSY;
- if ((hperh->state == TIMER_STATE_READY)) {
- if (((uint32_t)buf == 0 ) || (len == 0))
- return ERROR;
- }
- hperh->state = TIMER_STATE_BUSY;
- if (hdma->perh == NULL)
- hdma->perh = DMA0;
- hdma->cplt_cbk = timer_dma_period_elapse_cplt;
- hdma->cplt_arg = (void *)hperh;
- hdma->err_cbk = timer_dma_error;
- hdma->err_arg = (void *)hperh;
- dma_config_struct(&hdma->config);
- hdma->config.src = (void *)buf;
- hdma->config.dst = (void *)&hperh->perh->AR;
- hdma->config.size = len;
- hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
- hdma->config.src_inc = DMA_DATA_INC_HALFWORD;
- hdma->config.dst_inc = DMA_DATA_INC_NONE;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_UPDATE;
- hdma->config.channel = dma_ch;
- if (hperh->perh == TIMER0)
- hdma->config.msel = DMA_MSEL_TIMER0;
- else if (hperh->perh == TIMER1)
- hdma->config.msel = DMA_MSEL_TIMER1;
- else if (hperh->perh == TIMER2)
- hdma->config.msel = DMA_MSEL_TIMER2;
- else if (hperh->perh == TIMER3)
- hdma->config.msel = DMA_MSEL_TIMER3;
- else if (hperh->perh == TIMER4)
- hdma->config.msel = DMA_MSEL_TIMER4;
- else if (hperh->perh == TIMER5)
- hdma->config.msel = DMA_MSEL_TIMER5;
- else if (hperh->perh == TIMER6)
- hdma->config.msel = DMA_MSEL_TIMER6;
- else if (hperh->perh == TIMER7)
- hdma->config.msel = DMA_MSEL_TIMER7;
- else
- ;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_UPDATE, ENABLE);
- TIMER_ENABLE(hperh);
- return OK;
- }
- /**
- * @brief Stops the TIMER Base generation in DMA mode.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_base_stop_by_dma(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- timer_dma_req_config(hperh, TIMER_DMA_UPDATE, DISABLE);
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group2 TIMER Output Compare functions
- * @brief Time Output Compare functions
- *
- * @verbatim
- ==============================================================================
- ##### Time Output Compare functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER Output Compare.
- (+) Start the Time Output Compare.
- (+) Stop the Time Output Compare.
- (+) Start the Time Output Compare and enable interrupt.
- (+) Stop the Time Output Compare and disable interrupt.
- (+) Start the Time Output Compare and enable DMA transfer.
- (+) Stop the Time Output Compare and disable DMA transfer.
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER Output Compare according to the specified
- * parameters in the timer_handle_t and create the associated handle.
- * @param hperh: TIMER handle
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_oc_init(timer_handle_t *hperh)
- {
- return timer_base_init(hperh);
- }
- /**
- * @brief Starts the TIMER Output Compare signal generation.
- * @param hperh: TIMER handle
- * @param ch : TIMER Channel to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_oc_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Output Compare signal generation.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channel to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_oc_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- /**
- * @brief Starts the TIMER Output Compare signal generation in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channel to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_oc_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_interrupt_config(hperh, TIMER_IT_CC3, ENABLE);
- break;
- case TIMER_CHANNEL_4:
- timer_interrupt_config(hperh, TIMER_IT_CC4, ENABLE);
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Output Compare signal generation in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channel to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_oc_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_interrupt_config(hperh, TIMER_IT_CC3, DISABLE);
- break;
- case TIMER_CHANNEL_4:
- timer_interrupt_config(hperh, TIMER_IT_CC4, DISABLE);
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER Output Compare signal generation in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The source Buffer address.
- * @param len: The length of buffer to be transferred from memory to TIMER peripheral
- * @param dma_ch: Channel of DMA.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_oc_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
- dma_handle_t *hdma, uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- if ((hperh->state == TIMER_STATE_BUSY))
- return BUSY;
- if ((hperh->state == TIMER_STATE_READY)) {
- if (((uint32_t)buf == 0 ) || (len == 0))
- return ERROR;
- }
- hperh->state = TIMER_STATE_BUSY;
- if (hdma->perh == NULL)
- hdma->perh = DMA0;
- hdma->cplt_cbk = timer_dma_oc_cplt;
- hdma->cplt_arg = (void *)hperh;
- hdma->err_cbk = timer_dma_error;
- hdma->err_arg = (void *)hperh;
- dma_config_struct(&hdma->config);
- hdma->config.src = (void *)buf;
- hdma->config.size = len;
- hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
- hdma->config.src_inc = DMA_DATA_INC_HALFWORD;
- hdma->config.dst_inc = DMA_DATA_INC_NONE;
- hdma->config.channel = dma_ch;
- if (hperh->perh == TIMER0)
- hdma->config.msel = DMA_MSEL_TIMER0;
- else if (hperh->perh == TIMER1)
- hdma->config.msel = DMA_MSEL_TIMER1;
- else if (hperh->perh == TIMER2)
- hdma->config.msel = DMA_MSEL_TIMER2;
- else if (hperh->perh == TIMER3)
- hdma->config.msel = DMA_MSEL_TIMER3;
- else if (hperh->perh == TIMER4)
- hdma->config.msel = DMA_MSEL_TIMER4;
- else if (hperh->perh == TIMER5)
- hdma->config.msel = DMA_MSEL_TIMER5;
- else if (hperh->perh == TIMER6)
- hdma->config.msel = DMA_MSEL_TIMER6;
- else if (hperh->perh == TIMER7)
- hdma->config.msel = DMA_MSEL_TIMER7;
- else
- ;//do nothing
- switch (ch) {
- case TIMER_CHANNEL_1:
- hdma->config.dst = (void *)&hperh->perh->CCVAL1;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_1;
- break;
- case TIMER_CHANNEL_2:
- hdma->config.dst = (void *)&hperh->perh->CCVAL2;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_2;
- break;
- case TIMER_CHANNEL_3:
- hdma->config.dst = (void *)&hperh->perh->CCVAL3;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH3;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC3, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_3;
- break;
- case TIMER_CHANNEL_4:
- hdma->config.dst = (void *)&hperh->perh->CCVAL4;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH4;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC4, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_4;
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return OK;
- }
- /**
- * @brief Stops the TIMER Output Compare signal generation in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_oc_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_dma_req_config(hperh, TIMER_DMA_CC3, DISABLE);
- break;
- case TIMER_CHANNEL_4:
- timer_dma_req_config(hperh, TIMER_DMA_CC4, DISABLE);
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group3 TIMER PWM functions
- * @brief TIMER PWM functions
- *
- * @verbatim
- ==============================================================================
- ##### Time PWM functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER PWM.
- (+) Start the Time PWM.
- (+) Stop the Time PWM.
- (+) Start the Time PWM and enable interrupt.
- (+) Stop the Time PWM and disable interrupt.
- (+) Start the Time PWM and enable DMA transfer.
- (+) Stop the Time PWM and disable DMA transfer.
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER PWM Time Base according to the specified
- * parameters in the timer_handle_t and create the associated handle.
- * @param hperh: TIMER handle
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_pwm_init(timer_handle_t *hperh)
- {
- return timer_base_init(hperh);
- }
- /**
- * @brief Starts the PWM signal generation.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_pwm_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_oc_start(hperh, ch);
- return;
- }
- /**
- * @brief Stops the PWM signal generation.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_pwm_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_oc_stop(hperh, ch);
- return;
- }
- /**
- * @brief Starts the PWM signal generation in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channel to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_pwm_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_oc_start_by_it(hperh, ch);
- return;
- }
- /**
- * @brief Stops the PWM signal generation in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_pwm_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_oc_stop_by_it(hperh, ch);
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER PWM signal generation in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The source Buffer address.
- * @param len: The length of buffer to be transferred from memory to TIMER peripheral
- * @param dma_ch: Channel of DMA.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_pwm_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
- dma_handle_t *hdma, uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- return timer_oc_start_by_dma(hperh, ch, hdma, buf, len, dma_ch);
- }
- /**
- * @brief Stops the TIMER PWM signal generation in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_pwm_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_oc_stop_by_dma(hperh, ch);
- return;
- }
- #endif
- /**
- * @brief Set the PWM freq.
- * @param hperh: TIMER handle
- * @param freq: PWM freq to set
- * @retval None
- */
- void timer_pwm_set_freq(timer_handle_t *hperh, uint16_t freq)
- {
- uint32_t _arr = cmu_get_pclk1_clock() / (hperh->init.prescaler + 1) / freq - 1;
- WRITE_REG(hperh->perh->AR, _arr);
- hperh->init.period = _arr;
- }
- /**
- * @brief Set the PWM duty.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @param duty: PWM duty to set
- * @retval None
- */
- void timer_pwm_set_duty(timer_handle_t *hperh, timer_channel_t ch, uint16_t duty)
- {
- uint32_t tmp = (hperh->init.period + 1) * duty / 100 - 1;
- if (ch == TIMER_CHANNEL_1)
- WRITE_REG(hperh->perh->CCVAL1, tmp);
- else if (ch == TIMER_CHANNEL_2)
- WRITE_REG(hperh->perh->CCVAL2, tmp);
- else if (ch == TIMER_CHANNEL_3)
- WRITE_REG(hperh->perh->CCVAL3, tmp);
- else if (ch == TIMER_CHANNEL_4)
- WRITE_REG(hperh->perh->CCVAL4, tmp);
- else {
- ;/* do nothing */
- }
- }
- /**
- * @brief Set capture the PWM.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be captured the PWM
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_pwm_set_input(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_PWM_INPUT_INSTANCE(hperh->perh, ch));
- CLEAR_BIT(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK);
- switch (ch) {
- case TIMER_CHANNEL_1:
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, TIMER_IC_SEL_DIRECT << TIMER_CHMR1_CC1SSEL_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, TIMER_IC_SEL_INDIRECT << TIMER_CHMR1_CC2SSEL_POSS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1POL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC1POL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1NPOL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC1NPOL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2POL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC2POL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2NPOL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC2NPOL_POS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1FP1 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_RESET << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_CHANNEL_2:
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, TIMER_IC_SEL_INDIRECT << TIMER_CHMR1_CC1SSEL_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, TIMER_IC_SEL_DIRECT << TIMER_CHMR1_CC2SSEL_POSS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1POL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC1POL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1NPOL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC1NPOL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2POL_MSK, TIMER_IC_POLARITY_FALL << TIMER_CCEP_CC2POL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2NPOL_MSK, TIMER_IC_POLARITY_RISE << TIMER_CCEP_CC2NPOL_POS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI2FP2 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_RESET << TIMER_SMCON_SMODS_POSS);
- break;
- default:
- break;
- }
- SET_BIT(hperh->perh->CCEP, TIMER_CCEP_CC1EN_MSK);
- SET_BIT(hperh->perh->CCEP, TIMER_CCEP_CC2EN_MSK);
- return;
- }
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group4 TIMER Input Capture functions
- * @brief Time Input Capture functions
- *
- * @verbatim
- ==============================================================================
- ##### Time Input Capture functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER Input Capture.
- (+) Start the Time Input Capture.
- (+) Stop the Time Input Capture.
- (+) Start the Time Input Capture and enable interrupt.
- (+) Stop the Time Input Capture and disable interrupt.
- (+) Start the Time Input Capture and enable DMA transfer.
- (+) Stop the Time Input Capture and disable DMA transfer.
- * @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER Input Capture Time base according to the specified
- * parameters in the timer_handle_t and create the associated handle.
- * @param hperh: TIMER handle
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_ic_init(timer_handle_t *hperh)
- {
- return timer_base_init(hperh);
- }
- /**
- * @brief Starts the TIMER Input Capture measurement.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_ic_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Input Capture measurement.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_ic_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
- TIMER_DISABLE(hperh);
- return;
- }
- /**
- * @brief Starts the TIMER Input Capture measurement in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_ic_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_interrupt_config(hperh, TIMER_IT_CC3, ENABLE);
- break;
- case TIMER_CHANNEL_4:
- timer_interrupt_config(hperh, TIMER_IT_CC4, ENABLE);
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Input Capture measurement in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_ic_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_interrupt_config(hperh, TIMER_IT_CC3, DISABLE);
- break;
- case TIMER_CHANNEL_4:
- timer_interrupt_config(hperh, TIMER_IT_CC4, DISABLE);
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
- TIMER_DISABLE(hperh);
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER Input Capture measurement in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The destination Buffer address.
- * @param len: The length of buffer to be transferred TIMER peripheral to memory
- * @param dma_ch: Channel of DMA.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_ic_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
- dma_handle_t *hdma, uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- if ((hperh->state == TIMER_STATE_BUSY))
- return BUSY;
- if ((hperh->state == TIMER_STATE_READY)) {
- if (((uint32_t)buf == 0 ) || (len == 0))
- return ERROR;
- }
- hperh->state = TIMER_STATE_BUSY;
- if (hdma->perh == NULL)
- hdma->perh = DMA0;
- hdma->cplt_cbk = timer_dma_capture_cplt;
- hdma->cplt_arg = (void *)hperh;
- hdma->err_cbk = timer_dma_error;
- hdma->err_arg = (void *)hperh;
- dma_config_struct(&hdma->config);
- hdma->config.dst = (void *)buf;
- hdma->config.size = len;
- hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
- hdma->config.src_inc = DMA_DATA_INC_NONE;
- hdma->config.dst_inc = DMA_DATA_INC_HALFWORD;
- hdma->config.channel = dma_ch;
- if (hperh->perh == TIMER0)
- hdma->config.msel = DMA_MSEL_TIMER0;
- else if (hperh->perh == TIMER1)
- hdma->config.msel = DMA_MSEL_TIMER1;
- else if (hperh->perh == TIMER2)
- hdma->config.msel = DMA_MSEL_TIMER2;
- else if (hperh->perh == TIMER3)
- hdma->config.msel = DMA_MSEL_TIMER3;
- else if (hperh->perh == TIMER4)
- hdma->config.msel = DMA_MSEL_TIMER4;
- else if (hperh->perh == TIMER5)
- hdma->config.msel = DMA_MSEL_TIMER5;
- else if (hperh->perh == TIMER6)
- hdma->config.msel = DMA_MSEL_TIMER6;
- else if (hperh->perh == TIMER7)
- hdma->config.msel = DMA_MSEL_TIMER7;
- else
- ;/* do nothing */
- switch (ch) {
- case TIMER_CHANNEL_1:
- hdma->config.src = (void *)&hperh->perh->CCVAL1;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_1;
- break;
- case TIMER_CHANNEL_2:
- hdma->config.src = (void *)&hperh->perh->CCVAL2;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_2;
- break;
- case TIMER_CHANNEL_3:
- hdma->config.src = (void *)&hperh->perh->CCVAL3;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH3;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC3, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_3;
- break;
- case TIMER_CHANNEL_4:
- hdma->config.src = (void *)&hperh->perh->CCVAL4;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH4;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC4, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_4;
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- TIMER_ENABLE(hperh);
- return OK;
- }
- /**
- * @brief Stops the TIMER Input Capture measurement in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval None
- */
- void timer_ic_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_dma_req_config(hperh, TIMER_DMA_CC3, DISABLE);
- break;
- case TIMER_CHANNEL_4:
- timer_dma_req_config(hperh, TIMER_DMA_CC4, DISABLE);
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, DISABLE);
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group5 TIMER One Pulse functions
- * @brief Time One Pulse functions
- *
- * @verbatim
- ==============================================================================
- ##### Time One Pulse functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER One Pulse.
- (+) Start the Time One Pulse.
- (+) Stop the Time One Pulse.
- (+) Start the Time One Pulse and enable interrupt.
- (+) Stop the Time One Pulse and disable interrupt.
- (+) Start the Time One Pulse and enable DMA transfer.
- (+) Stop the Time One Pulse and disable DMA transfer.
- * @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER One Pulse Time Base according to the specified
- * parameters in the timer_handle_t and create the associated handle.
- * @param hperh: TIMER handle
- * @param mode: Select the One pulse mode.
- * This parameter can be one of the following values:
- * @arg TIMER_OP_MODE_SINGLE: Only one pulse will be generated.
- * @arg TIMER_OP_MODE_REPEAT: Repetitive pulses wil be generated.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_one_pulse_init(timer_handle_t *hperh, timer_op_mode_t mode)
- {
- if (hperh == NULL)
- return ERROR;
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_COUNTER_MODE(hperh->init.mode));
- assert_param(IS_TIMER_CLOCK_DIVISION(hperh->init.clk_div));
- assert_param(IS_TIMER_OP_MODE(mode));
- if (hperh->state == TIMER_STATE_RESET)
- hperh->lock = UNLOCK;
- hperh->state = TIMER_STATE_BUSY;
- timer_base_set_config(hperh->perh, &hperh->init);
- MODIFY_REG(hperh->perh->CON1, TIMER_CON1_SPMEN_MSK, mode << TIMER_CON1_SPMEN_POS);
- hperh->state = TIMER_STATE_READY;
- return OK;
- }
- /**
- * @brief Starts the TIMER One Pulse signal generation.
- * @param hperh: TIMER One Pulse handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_start(timer_handle_t *hperh, timer_op_output_channel_t ch)
- {
- /* Enable the Capture compare and the Input Capture channels
- * (in the OPM Mode the two possible channels that can be used are TIMER_CHANNEL_1 and TIMER_CHANNEL_2)
- * if TIMER_CHANNEL_1 is used as output, the TIMER_CHANNEL_2 will be used as input and
- * if TIMER_CHANNEL_1 is used as input, the TIMER_CHANNEL_2 will be used as output
- * in all combinations, the TIMER_CHANNEL_1 and TIMER_CHANNEL_2 should be enabled together
- */
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER One Pulse signal generation.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_stop(timer_handle_t *hperh, timer_op_output_channel_t ch)
- {
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- return;
- }
- /**
- * @brief Starts the TIMER One Pulse signal generation in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_start_by_it(timer_handle_t *hperh, timer_op_output_channel_t ch)
- {
- /* Enable the Capture compare and the Input Capture channels
- * (in the OPM Mode the two possible channels that can be used are TIMER_CHANNEL_1 and TIMER_CHANNEL_2)
- * if TIMER_CHANNEL_1 is used as output, the TIMER_CHANNEL_2 will be used as input and
- * if TIMER_CHANNEL_1 is used as input, the TIMER_CHANNEL_2 will be used as output
- * in all combinations, the TIMER_CHANNEL_1 and TIMER_CHANNEL_2 should be enabled together
- */
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER One Pulse signal generation in interrupt mode.
- * @param hperh : TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_OP_OUTPUT_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_OP_OUTPUT_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_stop_by_it(timer_handle_t *hperh, timer_op_output_channel_t ch)
- {
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- if (IS_TIMER_BREAK_INSTANCE(hperh->perh) != RESET)
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- return;
- }
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group6 TIMER Encoder functions
- * @brief TIMER Encoder functions
- *
- * @verbatim
- ==============================================================================
- ##### Time Encoder functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER Encoder.
- (+) Start the Time Encoder.
- (+) Stop the Time Encoder.
- (+) Start the Time Encoder and enable interrupt.
- (+) Stop the Time Encoder and disable interrupt.
- (+) Start the Time Encoder and enable DMA transfer.
- (+) Stop the Time Encoder and disable DMA transfer.
- * @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER Encoder Interface and create the associated handle.
- * @param hperh: TIMER handle
- * @param config: TIMER Encoder Interface configuration structure
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_encoder_init(timer_handle_t *hperh, timer_encoder_init_t *config)
- {
- if (hperh == NULL)
- return ERROR;
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_ENCODER_MODE(config->mode));
- assert_param(IS_TIMER_IC_POLARITY(config->ic1_polarity));
- assert_param(IS_TIMER_IC_POLARITY(config->ic2_polarity));
- assert_param(IS_TIMER_IC_SELECT(config->ic1_sel));
- assert_param(IS_TIMER_IC_SELECT(config->ic2_sel));
- assert_param(IS_TIMER_IC_PSC(config->ic1_psc));
- assert_param(IS_TIMER_IC_PSC(config->ic2_psc));
- assert_param(IS_TIMER_IC_FILTER(config->ic1_filter));
- assert_param(IS_TIMER_IC_FILTER(config->ic2_filter));
- if (hperh->state == TIMER_STATE_RESET)
- hperh->lock = UNLOCK;
- hperh->state = TIMER_STATE_BUSY;
- CLEAR_BIT(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK);
- timer_base_set_config(hperh->perh, &hperh->init);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, config->mode << TIMER_SMCON_SMODS_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, config->ic1_sel << TIMER_CHMR1_CC1SSEL_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, config->ic2_sel << TIMER_CHMR1_CC2SSEL_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK, config->ic1_psc << TIMER_CHMR1_IC1PRES_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC2PRES_MSK, config->ic2_psc << TIMER_CHMR1_IC2PRES_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_I1FLT_MSK, config->ic1_filter << TIMER_CHMR1_I1FLT_POSS);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_I2FLT_MSK, config->ic2_filter << TIMER_CHMR1_I2FLT_POSS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1POL_MSK, (config->ic1_polarity & 0x1) << TIMER_CCEP_CC1POL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC1NPOL_MSK, ((config->ic1_polarity >> 1) & 0x1) << TIMER_CCEP_CC1NPOL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2POL_MSK, (config->ic2_polarity & 0x1) << TIMER_CCEP_CC2POL_POS);
- MODIFY_REG(hperh->perh->CCEP, TIMER_CCEP_CC2NPOL_MSK, ((config->ic2_polarity >> 1) & 0x1) << TIMER_CCEP_CC2NPOL_POS);
- hperh->state = TIMER_STATE_READY;
- return OK;
- }
- /**
- * @brief Starts the TIMER Encoder Interface.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
- * @retval None
- */
- void timer_encoder_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- break;
- default:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- break;
- }
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Encoder Interface.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
- * @retval None
- */
- void timer_encoder_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- break;
- default:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- break;
- }
- TIMER_DISABLE(hperh);
- return;
- }
- /**
- * @brief Starts the TIMER Encoder Interface in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
- * @retval None
- */
- void timer_encoder_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
- break;
- default:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
- break;
- }
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER Encoder Interface in interrupt mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
- * @retval None
- */
- void timer_encoder_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
- break;
- default:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
- break;
- }
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER Encoder Interface in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
- * @param hdma1: Pointer to dma_handle_t.
- * @param hdma2: Pointer to dma_handle_t.
- * @param buf1: The destination Buffer address. Reading data from CCR1.
- * @param buf2: The destination Buffer address. Reading data from CCR2.
- * @param len: The length of buffer to be transferred TIMER peripheral to memory
- * @param dma_ch1: Channel of DMA.
- * @param dma_ch2: Channel of DMA.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_encoder_start_by_dma(timer_handle_t *hperh, timer_channel_t ch,
- dma_handle_t *hdma1, dma_handle_t *hdma2, uint16_t *buf1,
- uint16_t *buf2, uint32_t len, uint8_t dma_ch1, uint8_t dma_ch2)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- if ((hperh->state == TIMER_STATE_BUSY))
- return BUSY;
- if ((hperh->state == TIMER_STATE_READY)) {
- if (((uint32_t)buf1 == 0) || ((uint32_t)buf2 == 0) || (len == 0))
- return ERROR;
- }
- if (hdma1->perh == NULL)
- hdma1->perh = DMA0;
- if (hdma2->perh == NULL)
- hdma2->perh = DMA0;
- hperh->state = TIMER_STATE_BUSY;
- hdma1->cplt_cbk = timer_dma_capture_cplt;
- hdma1->cplt_arg = (void *)hperh;
- hdma1->err_cbk = timer_dma_error;
- hdma1->err_arg = (void *)hperh;
- dma_config_struct(&hdma1->config);
- hdma1->config.size = len;
- hdma1->config.data_width = DMA_DATA_SIZE_HALFWORD;
- hdma1->config.src_inc = DMA_DATA_INC_NONE;
- hdma1->config.dst_inc = DMA_DATA_INC_HALFWORD;
- if (hperh->perh == TIMER0)
- hdma1->config.msel = DMA_MSEL_TIMER0;
- else if (hperh->perh == TIMER1)
- hdma1->config.msel = DMA_MSEL_TIMER1;
- else if (hperh->perh == TIMER2)
- hdma1->config.msel = DMA_MSEL_TIMER2;
- else if (hperh->perh == TIMER3)
- hdma1->config.msel = DMA_MSEL_TIMER3;
- else if (hperh->perh == TIMER4)
- hdma1->config.msel = DMA_MSEL_TIMER4;
- else if (hperh->perh == TIMER5)
- hdma1->config.msel = DMA_MSEL_TIMER5;
- else if (hperh->perh == TIMER6)
- hdma1->config.msel = DMA_MSEL_TIMER6;
- else if (hperh->perh == TIMER7)
- hdma1->config.msel = DMA_MSEL_TIMER7;
- else
- ;/* do nothing */
- switch (ch) {
- case TIMER_CHANNEL_1:
- hdma1->config.src = (void *)&hperh->perh->CCVAL1;
- hdma1->config.dst = (void *)buf1;
- hdma1->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
- hdma1->config.channel = dma_ch1;
- dma_config_basic(hdma1);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- TIMER_ENABLE(hperh);
- break;
- case TIMER_CHANNEL_2:
- hdma1->config.src = (void *)&hperh->perh->CCVAL2;
- hdma1->config.dst = (void *)buf2;
- hdma1->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
- hdma1->config.channel = dma_ch2;
- dma_config_basic(hdma1);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- TIMER_ENABLE(hperh);
- break;
- default:
- hdma2->cplt_cbk = timer_dma_capture_cplt;
- hdma2->cplt_arg = (void *)hperh;
- hdma2->err_cbk = timer_dma_error;
- hdma2->err_arg = (void *)hperh;
- memcpy(&hdma2->config, &hdma1->config, sizeof(dma_config_t));
- hdma1->config.src = (void *)&hperh->perh->CCVAL1;
- hdma1->config.dst = (void *)buf1;
- hdma1->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
- hdma1->config.channel = dma_ch1;
- dma_config_basic(hdma1);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
- hdma2->config.src = (void *)&hperh->perh->CCVAL2;
- hdma2->config.dst = (void *)buf2;
- hdma2->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
- hdma2->config.channel = dma_ch2;
- dma_config_basic(hdma2);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, ENABLE);
- TIMER_ENABLE(hperh);
- break;
- }
- return OK;
- }
- /**
- * @brief Stops the TIMER Encoder Interface in DMA mode.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_ALL: TIMER Channel 1 and TIMER Channel 2 are selected
- * @retval None
- */
- void timer_encoder_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
- break;
- default:
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_2, DISABLE);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
- break;
- }
- TIMER_DISABLE(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group7 TIMER Hall Sensor functions
- * @brief TIMER Hall Sensor functions
- *
- * @verbatim
- ==============================================================================
- ##### Time Hall Sensor functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Initialize and configure the TIMER hall sensor.
- (+) Start the hall sensor.
- (+) Stop the hall sensor.
- (+) Start the hall sensor and enable interrupt.
- (+) Stop the hall sensor and disable interrupt.
- (+) Start the hall sensor and enable DMA transfer.
- (+) Stop the hal sensor and disable DMA transfer.
- * @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER Encoder Interface and create the associated handle.
- * @param hperh: TIMER handle
- * @param config: TIMER Encoder Interface configuration structure
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_hall_sensor_init(timer_handle_t *hperh, timer_hall_sensor_init_t *config)
- {
- timer_oc_init_t oc;
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_COUNTER_MODE(hperh->init.mode));
- assert_param(IS_TIMER_CLOCK_DIVISION(hperh->init.clk_div));
- assert_param(IS_TIMER_IC_POLARITY(config->polarity));
- assert_param(IS_TIMER_IC_PSC(config->psc));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- if (hperh->state == TIMER_STATE_RESET)
- hperh->lock = UNLOCK;
- hperh->state = TIMER_STATE_READY;
- timer_base_set_config(hperh->perh, &hperh->init);
- timer_ti1_set_config(hperh->perh, config->polarity, TIMER_IC_SEL_TRC, config->filter);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK, config->psc << TIMER_CHMR1_IC1PRES_POSS);
- SET_BIT(hperh->perh->CON2, TIMER_CON2_I1FSEL_MSK);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1F_ED << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_RESET << TIMER_SMCON_SMODS_POSS);
- oc.oc_mode = TIMER_OC_MODE_PWM2;
- oc.pulse = config->delay;
- oc.oc_polarity = TIMER_OC_POLARITY_HIGH;
- oc.ocn_polarity = TIMER_OCN_POLARITY_HIGH;
- oc.oc_fast_en = DISABLE;
- oc.oc_idle = TIMER_OC_IDLE_RESET;
- oc.ocn_idle = TIMER_OCN_IDLE_RESET;
- timer_oc2_set_config(hperh->perh, &oc);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_TRGO_OC2REF << TIMER_SMCON_SMODS_POSS);
- return OK;
- }
- /**
- * @brief Starts the TIMER hall sensor interface.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_hall_sensor_start(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER hall sensor interface.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_hall_sensor_stop(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- TIMER_DISABLE(hperh);
- return;
- }
- /**
- * @brief Starts the TIMER hall sensor interface in interrupt mode.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_hall_sensor_start_by_it(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER hall sensor interface in interrupt mode.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_hall_sensor_stop_by_it(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- TIMER_DISABLE(hperh);
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER hall sensor interface in DMA mode.
- * @param hperh: TIMER handle
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The destination Buffer address. Reading data from CCR1.
- * @param len: The length of buffer to be transferred TIMER peripheral to memory
- * @param dma_ch: Channel of DMA.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_hall_sensor_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
- uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- if ((hperh->state == TIMER_STATE_BUSY))
- return BUSY;
- if ((hperh->state == TIMER_STATE_READY)) {
- if (((uint32_t)buf == 0) || (len == 0))
- return ERROR;
- }
- if (hdma->perh == NULL)
- hdma->perh = DMA0;
- hperh->state = TIMER_STATE_BUSY;
- hdma->cplt_cbk = timer_dma_capture_cplt;
- hdma->cplt_arg = (void *)hperh;
- hdma->err_cbk = timer_dma_error;
- hdma->err_arg = (void *)hperh;
- dma_config_struct(&hdma->config);
- hdma->config.size = len;
- hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
- hdma->config.src_inc = DMA_DATA_INC_NONE;
- hdma->config.dst_inc = DMA_DATA_INC_HALFWORD;
- if (hperh->perh == TIMER0)
- hdma->config.msel = DMA_MSEL_TIMER0;
- else if (hperh->perh == TIMER6)
- hdma->config.msel = DMA_MSEL_TIMER6;
- else
- ;/* do nothing */
- hdma->config.src = (void *)&hperh->perh->CCVAL1;
- hdma->config.dst = (void *)buf;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
- hdma->config.channel = dma_ch;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, ENABLE);
- TIMER_ENABLE(hperh);
- return OK;
- }
- /**
- * @brief Stops the TIMER hall sensor interface in DMA mode.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_hall_sensor_stop_by_dma(timer_handle_t *hperh)
- {
- assert_param(IS_TIMER_XOR_INSTANCE(hperh->perh));
- timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
- timer_ccx_channel_cmd(hperh->perh, TIMER_CHANNEL_1, DISABLE);
- TIMER_DISABLE(hperh);
- return;
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group8 TIMER complementary output compare functions
- * @brief TIMER complementary output compare functions
- *
- * @verbatim
- ==============================================================================
- ##### Time complementary output compare functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Start the Time complementary output compare.
- (+) Stop the Time complementary output compare.
- (+) Start the Time complementary output compare and enable interrupt.
- (+) Stop the Time complementary output compare and disable interrupt.
- (+) Start the Time complementary output compare and enable DMA transfer.
- (+) Stop the Time complementary output compare and disable DMA transfer.
- * @endverbatim
- * @{
- */
- /**
- * @brief Starts the TIMER output compare signal generation on the complementary output.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_ocn_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
- timer_ccxn_channel_cmd(hperh->perh, ch, ENABLE);
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER output compare signal generation on the complementary output.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_ocn_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
- timer_ccxn_channel_cmd(hperh->perh, ch, DISABLE);
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- return;
- }
- /**
- * @brief Starts the TIMER output compare signal generation on the complementary output.
- * in interrupt mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_ocn_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_interrupt_config(hperh, TIMER_IT_CC1, ENABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_interrupt_config(hperh, TIMER_IT_CC2, ENABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_interrupt_config(hperh, TIMER_IT_CC3, ENABLE);
- break;
- default:
- break;
- }
- timer_interrupt_config(hperh, TIMER_IT_BREAK, ENABLE);
- timer_ccxn_channel_cmd(hperh->perh, ch, ENABLE);
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Stops the TIMER output compare signal generation on the complementary output.
- * in interrupt mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_ocn_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_interrupt_config(hperh, TIMER_IT_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_interrupt_config(hperh, TIMER_IT_CC2, DISABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_interrupt_config(hperh, TIMER_IT_CC3, DISABLE);
- break;
- default:
- break;
- }
- if ((!(READ_BIT(hperh->perh->CCEP, TIMER_CCEP_CC1NEN_MSK)))
- && (!(READ_BIT(hperh->perh->CCEP, TIMER_CCEP_CC2NEN_MSK)))
- && (!(READ_BIT(hperh->perh->CCEP, TIMER_CCEP_CC3NEN_MSK)))) {
- timer_interrupt_config(hperh, TIMER_IT_BREAK, DISABLE);
- }
- timer_ccxn_channel_cmd(hperh->perh, ch, DISABLE);
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- return;
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER output compare signal generation on the complementary output.
- * in DMA mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The destination Buffer address. Reading data from CCRx.
- * @param len: The length of buffer to be transferred TIMER peripheral to memory
- * @param dma_ch: Channel of DMA.
- * @retval None
- */
- ald_status_t timer_ocn_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
- timer_channel_t ch, uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
- if ((hperh->state == TIMER_STATE_BUSY))
- return BUSY;
- if ((hperh->state == TIMER_STATE_READY)) {
- if (((uint32_t)buf == 0 ) || (len == 0))
- return ERROR;
- }
- hperh->state = TIMER_STATE_BUSY;
- if (hdma->perh == NULL)
- hdma->perh = DMA0;
- hdma->cplt_cbk = timer_dma_oc_cplt;
- hdma->cplt_arg = (void *)hperh;
- hdma->err_cbk = timer_dma_error;
- hdma->err_arg = (void *)hperh;
- dma_config_struct(&hdma->config);
- hdma->config.src = (void *)buf;
- hdma->config.size = len;
- hdma->config.data_width = DMA_DATA_SIZE_HALFWORD;
- hdma->config.src_inc = DMA_DATA_INC_HALFWORD;
- hdma->config.dst_inc = DMA_DATA_INC_NONE;
- hdma->config.channel = dma_ch;
- hdma->config.msel = DMA_MSEL_TIMER0;
- switch (ch) {
- case TIMER_CHANNEL_1:
- hdma->config.dst = (void *)&hperh->perh->CCVAL1;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH1;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC1, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_1;
- break;
- case TIMER_CHANNEL_2:
- hdma->config.dst = (void *)&hperh->perh->CCVAL2;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH2;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC2, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_2;
- break;
- case TIMER_CHANNEL_3:
- hdma->config.dst = (void *)&hperh->perh->CCVAL3;
- hdma->config.msigsel = DMA_MSIGSEL_TIMER_CH3;
- dma_config_basic(hdma);
- timer_dma_req_config(hperh, TIMER_DMA_CC3, ENABLE);
- hperh->ch = TIMER_ACTIVE_CHANNEL_3;
- break;
- default:
- break;
- }
- timer_ccx_channel_cmd(hperh->perh, ch, ENABLE);
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return OK;
- }
- /**
- * @brief Starts the TIMER output compare signal generation on the complementary output.
- * in DMA mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_ocn_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCXN_INSTANCE(hperh->perh, ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_dma_req_config(hperh, TIMER_DMA_CC1, DISABLE);
- break;
- case TIMER_CHANNEL_2:
- timer_dma_req_config(hperh, TIMER_DMA_CC2, DISABLE);
- break;
- case TIMER_CHANNEL_3:
- timer_dma_req_config(hperh, TIMER_DMA_CC3, DISABLE);
- break;
- default:
- break;
- }
- timer_ccxn_channel_cmd(hperh->perh, ch, DISABLE);
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- return;
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group9 TIMER complementary PWM functions
- * @brief TIMER complementary PWM functions
- *
- * @verbatim
- ==============================================================================
- ##### Time complementary PWM functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Start the Time complementary PWM.
- (+) Stop the Time complementary PWM.
- (+) Start the Time complementary PWM and enable interrupt.
- (+) Stop the Time complementary PWM and disable interrupt.
- (+) Start the Time complementary PWM and enable DMA transfer.
- (+) Stop the Time complementary PWM and disable DMA transfer.
- * @endverbatim
- * @{
- */
- /**
- * @brief Starts the TIMER PWM signal generation on the complementary output.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_pwmn_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_start(hperh, ch);
- }
- /**
- * @brief Stops the TIMER PWM signal generation on the complementary output.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_pwmn_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_stop(hperh, ch);
- }
- /**
- * @brief Starts the TIMER PWM signal generation on the complementary output.
- * in interrupt mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_pwmn_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_start_by_it(hperh, ch);
- }
- /**
- * @brief Stops the TIMER PWM signal generation on the complementary output.
- * in interrupt mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_pwmn_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_stop_by_it(hperh, ch);
- }
- #ifdef ALD_DMA
- /**
- * @brief Starts the TIMER PWM signal generation on the complementary output.
- * in DMA mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @param hdma: Pointer to dma_handle_t.
- * @param buf: The destination Buffer address. Reading data from CCRx.
- * @param len: The length of buffer to be transferred TIMER peripheral to memory
- * @param dma_ch: Channel of DMA.
- * @retval None
- */
- ald_status_t timer_pwmn_start_by_dma(timer_handle_t *hperh, dma_handle_t *hdma,
- timer_channel_t ch, uint16_t *buf, uint32_t len, uint8_t dma_ch)
- {
- return timer_ocn_start_by_dma(hperh, hdma, ch, buf, len, dma_ch);
- }
- /**
- * @brief Starts the TIMER PWM signal generation on the complementary output.
- * in DMA mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @retval None
- */
- void timer_pwmn_stop_by_dma(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_stop_by_dma(hperh, ch);
- }
- #endif
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group10 TIMER complementary one pulse functions
- * @brief TIMER complementary one pulse functions
- *
- * @verbatim
- ==============================================================================
- ##### Time complementary one pulse functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Start the Time complementary one pulse.
- (+) Stop the Time complementary one pulse.
- (+) Start the Time complementary one pulse and enable interrupt.
- (+) Stop the Time complementary one pulse and disable interrupt.
- * @endverbatim
- * @{
- */
- /**
- * @brief Starts the TIMER one pulse signal generation on the complementary output.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_n_start(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_start(hperh, ch);
- }
- /**
- * @brief Stops the TIMER one pulse signal generation on the complementary output.
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_n_stop(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_stop(hperh, ch);
- }
- /**
- * @brief Starts the TIMER one pulse signal generation on the complementary output.
- * in interrupt mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_n_start_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_start_by_it(hperh, ch);
- }
- /**
- * @brief Stops the TIMER one pulse signal generation on the complementary output.
- * in interrupt mode
- * @param hperh: TIMER handle
- * @param ch: TIMER Channels to be disabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @retval None
- */
- void timer_one_pulse_n_stop_by_it(timer_handle_t *hperh, timer_channel_t ch)
- {
- timer_ocn_stop_by_it(hperh, ch);
- }
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group11 Peripheral Control functions
- * @brief Peripheral Control functions
- *
- * @verbatim
- ==============================================================================
- ##### Peripheral Control functions #####
- ==============================================================================
- [..]
- This section provides functions allowing to:
- (+) Configure The Input Output channels for OC, PWM, IC or One Pulse mode.
- (+) Configure External Clock source.
- (+) Configure Complementary channels, break features and dead timere.
- (+) Configure Master and the Slave synchronization.
- (+) Handle TIMER interrupt.
- (+) Get TIMER compare register's vale.
- (+) Configure TIMER interrupt ENABLE/DISABLE.
- (+) Get TIMER interrupt source status.
- (+) Get TIMER interrupt flag status.
- (+) Clear TIMER interrupt flag.
- @endverbatim
- * @{
- */
- /**
- * @brief Initializes the TIMER Output Compare Channels according to the specified
- * parameters in the timer_oc_init_t.
- * @param hperh: TIMER handle
- * @param config: TIMER Output Compare configuration structure
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_oc_config_channel(timer_handle_t *hperh, timer_oc_init_t* config, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CCX_INSTANCE(hperh->perh, ch));
- assert_param(IS_TIMER_OC_MODE(config->oc_mode));
- assert_param(IS_TIMER_OC_POLARITY(config->oc_polarity));
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_oc1_set_config(hperh->perh, config);
- break;
- case TIMER_CHANNEL_2:
- timer_oc2_set_config(hperh->perh, config);
- break;
- case TIMER_CHANNEL_3:
- timer_oc3_set_config(hperh->perh, config);
- break;
- case TIMER_CHANNEL_4:
- timer_oc4_set_config(hperh->perh, config);
- break;
- default:
- break;
- }
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Initializes the TIMER Input Capture Channels according to the specified
- * parameters in the timer_ic_init_t.
- * @param hperh: TIMER handle
- * @param config: TIMER Input Capture configuration structure
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3: TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4: TIMER Channel 4 selected
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_ic_config_channel(timer_handle_t *hperh, timer_ic_init_t* config, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_IC_POLARITY(config->polarity));
- assert_param(IS_TIMER_IC_SELECT(config->sel));
- assert_param(IS_TIMER_IC_PSC(config->psc));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- switch (ch) {
- case TIMER_CHANNEL_1:
- timer_ti1_set_config(hperh->perh, config->polarity, config->sel, config->filter);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK, config->psc << TIMER_CHMR1_IC1PRES_POSS);
- break;
- case TIMER_CHANNEL_2:
- timer_ti2_set_config(hperh->perh, config->polarity, config->sel, config->filter);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_IC2PRES_MSK, config->psc << TIMER_CHMR1_IC2PRES_POSS);
- break;
- case TIMER_CHANNEL_3:
- timer_ti3_set_config(hperh->perh, config->polarity, config->sel, config->filter);
- MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_IC3PRES_MSK, config->psc << TIMER_CHMR2_IC3PRES_POSS);
- break;
- case TIMER_CHANNEL_4:
- timer_ti4_set_config(hperh->perh, config->polarity, config->sel, config->filter);
- MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_IC4PRES_MSK, config->psc << TIMER_CHMR2_IC4PRES_POSS);
- break;
- default:
- break;
- }
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Initializes the TIMER One Pulse Channels according to the specified
- * parameters in the timer_one_pulse_init_t.
- * @param hperh: TIMER handle
- * @param config: TIMER One Pulse configuration structure
- * @param ch_out: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @param ch_in: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2: TIMER Channel 2 selected
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_one_pulse_config_channel(timer_handle_t *hperh, timer_one_pulse_init_t *config,
- timer_channel_t ch_out, timer_channel_t ch_in)
- {
- timer_oc_init_t tmp;
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_OC_MODE(config->mode));
- assert_param(IS_TIMER_OC_POLARITY(config->oc_polarity));
- assert_param(IS_TIMER_OCN_POLARITY(config->ocn_polarity));
- assert_param(IS_TIMER_OCIDLE_STATE(config->oc_idle));
- assert_param(IS_TIMER_OCNIDLE_STATE(config->ocn_idle));
- assert_param(IS_TIMER_IC_POLARITY(config->polarity));
- assert_param(IS_TIMER_IC_SELECT(config->sel));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- if (ch_out == ch_in)
- return ERROR;
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- tmp.oc_mode = config->mode;
- tmp.pulse = config->pulse;
- tmp.oc_polarity = config->oc_polarity;
- tmp.ocn_polarity = config->ocn_polarity;
- tmp.oc_idle = config->oc_idle;
- tmp.ocn_idle = config->ocn_idle;
- switch (ch_out) {
- case TIMER_CHANNEL_1:
- timer_oc1_set_config(hperh->perh, &tmp);
- break;
- case TIMER_CHANNEL_2:
- timer_oc2_set_config(hperh->perh, &tmp);
- break;
- default:
- break;
- }
- switch (ch_in) {
- case TIMER_CHANNEL_1:
- timer_ti1_set_config(hperh->perh, config->polarity, config->sel, config->filter);
- CLEAR_BIT(hperh->perh->CHMR1, TIMER_CHMR1_IC1PRES_MSK);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1FP1 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_TRIG << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_CHANNEL_2:
- timer_ti2_set_config(hperh->perh, config->polarity, config->sel, config->filter);
- CLEAR_BIT(hperh->perh->CHMR1, TIMER_CHMR1_IC2PRES_MSK);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI2FP2 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_TRIG << TIMER_SMCON_SMODS_POSS);
- break;
- default:
- break;
- }
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Configures the OCRef clear feature
- * @param hperh: TIMER handle
- * @param config: pointer to a TIMER_ClearInputConfigTypeDef structure that
- * contains the OCREF clear feature and parameters for the TIMER peripheral.
- * @param ch: specifies the TIMER Channel
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1
- * @arg TIMER_CHANNEL_2: TIMER Channel 2
- * @arg TIMER_CHANNEL_3: TIMER Channel 3
- * @arg TIMER_CHANNEL_4: TIMER Channel 4
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_config_oc_ref_clear(timer_handle_t *hperh, timer_clear_input_config_t *config, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- assert_param(IS_FUNC_STATE(config->state));
- assert_param(IS_TIMER_CLEAR_INPUT_SOURCE(config->source));
- assert_param(IS_TIMER_CLEAR_INPUT_POLARITY(config->polarity));
- assert_param(IS_TIMER_ETR_PSC(config->psc));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- if (config->source == TIMER_INPUT_NONE) {
- timer_etr_set_config(hperh->perh, TIMER_ETR_PSC_DIV1, TIMER_CLK_POLARITY_NO_INV, 0);
- }
- else {
- timer_etr_set_config(hperh->perh, config->psc,
- (timer_clock_polarity_t)config->polarity, config->filter);
- }
- switch (ch) {
- case TIMER_CHANNEL_1:
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH1OCLREN_MSK, config->state << TIMER_CHMR1_CH1OCLREN_POS);
- break;
- case TIMER_CHANNEL_2:
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH2OCLREN_MSK, config->state << TIMER_CHMR1_CH2OCLREN_POS);
- break;
- case TIMER_CHANNEL_3:
- assert_param(IS_TIMER_CC4_INSTANCE(hperh->perh));
- MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH3OCLREN_MSK, config->state << TIMER_CHMR2_CH3OCLREN_POS);
- break;
- case TIMER_CHANNEL_4:
- assert_param(IS_TIMER_CC4_INSTANCE(hperh->perh));
- MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH4OCLREN_MSK, config->state << TIMER_CHMR2_CH4OCLREN_POS);
- break;
- default:
- break;
- }
- return OK;
- }
- /**
- * @brief Configures the clock source to be used
- * @param hperh: TIMER handle
- * @param config: pointer to a timer_clock_config_t structure that
- * contains the clock source information for the TIMER peripheral.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_config_clock_source(timer_handle_t *hperh, timer_clock_config_t *config)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_CLOCK_SOURCE(config->source));
- assert_param(IS_TIMER_CLOCK_POLARITY(config->polarity));
- assert_param(IS_TIMER_ETR_PSC(config->psc));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- WRITE_REG(hperh->perh->SMCON, 0x0);
- switch (config->source) {
- case TIMER_SRC_INTER:
- CLEAR_BIT(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK);
- break;
- case TIMER_SRC_ETRMODE1:
- timer_etr_set_config(hperh->perh, config->psc, config->polarity, config->filter);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ETRF << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_ETRMODE2:
- timer_etr_set_config(hperh->perh, config->psc, config->polarity, config->filter);
- SET_BIT(hperh->perh->SMCON, TIMER_SMCON_ECM2EN_MSK);
- break;
- case TIMER_SRC_TI1:
- timer_ti1_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1FP1 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_TI2:
- timer_ti2_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI2FP2 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_TI1ED:
- timer_ti1_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_TI1F_ED << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_ITR0:
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR0 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_ITR1:
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR1 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_ITR2:
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR2 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- case TIMER_SRC_ITR3:
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, TIMER_TS_ITR3 << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, TIMER_MODE_EXTERNAL1 << TIMER_SMCON_SMODS_POSS);
- break;
- default:
- break;
- }
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Selects the signal connected to the TI1 input: direct from CH1_input
- * or a XOR combination between CH1_input, CH2_input & CH3_input
- * @param hperh: TIMER handle.
- * @param ti1_select: Indicate whether or not channel 1 is connected to the
- * output of a XOR gate.
- * This parameter can be one of the following values:
- * @arg 0: The TIMERx_CH1 pin is connected to TI1 input
- * @arg 1: The TIMERx_CH1, CH2 and CH3
- * pins are connected to the TI1 input (XOR combination)
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_config_ti1_input(timer_handle_t *hperh, uint32_t ti1_select)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- MODIFY_REG(hperh->perh->CON2, TIMER_CON2_I1FSEL_MSK, ti1_select << TIMER_CON2_I1FSEL_POS);
- return OK;
- }
- /**
- * @brief Configures the TIMER in Slave mode
- * @param hperh: TIMER handle.
- * @param config: pointer to a timer_slave_config_t structure that
- * contains the selected trigger (internal trigger input, filtered
- * timerer input or external trigger input) and the Slave
- * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_slave_config_sync(timer_handle_t *hperh, timer_slave_config_t *config)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_SLAVE_MODE(config->mode));
- assert_param(IS_TIMER_TS(config->input));
- assert_param(IS_TIMER_CLOCK_POLARITY(config->polarity));
- assert_param(IS_TIMER_ETR_PSC(config->psc));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- timer_slave_set_config(hperh, config);
- timer_interrupt_config(hperh, TIMER_IT_TRIGGER, DISABLE);
- timer_dma_req_config(hperh, TIMER_DMA_TRIGGER, DISABLE);
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Configures the TIMER in Slave mode in interrupt mode
- * @param hperh: TIMER handle.
- * @param config: pointer to a timer_slave_config_t structure that
- * contains the selected trigger (internal trigger input, filtered
- * timerer input or external trigger input) and the ) and the Slave
- * mode (Disable, Reset, Gated, Trigger, External clock mode 1).
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_slave_config_sync_by_it(timer_handle_t *hperh, timer_slave_config_t *config)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_SLAVE_MODE(config->mode));
- assert_param(IS_TIMER_TS(config->input));
- assert_param(IS_TIMER_CLOCK_POLARITY(config->polarity));
- assert_param(IS_TIMER_ETR_PSC(config->psc));
- assert_param(IS_TIMER_IC_FILTER(config->filter));
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- timer_slave_set_config(hperh, config);
- timer_interrupt_config(hperh, TIMER_IT_TRIGGER, ENABLE);
- timer_dma_req_config(hperh, TIMER_DMA_TRIGGER, DISABLE);
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Generate a software event
- * @param hperh: TIMER handle
- * @param event: specifies the event source.
- * @retval Status, see @ref ald_status_t.
- */
- ald_status_t timer_generate_event(timer_handle_t *hperh, timer_event_source_t event)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_EVENT_SOURCE(event));
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- WRITE_REG(hperh->perh->SGE, event);
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return OK;
- }
- /**
- * @brief Read the captured value from Capture Compare unit
- * @param hperh: TIMER handle.
- * @param ch: TIMER Channels to be enabled
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1 : TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2 : TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3 : TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4 : TIMER Channel 4 selected
- * @retval Captured value
- */
- uint32_t timer_read_capture_value(timer_handle_t *hperh, timer_channel_t ch)
- {
- uint32_t tmp;
- __LOCK(hperh);
- hperh->state = TIMER_STATE_BUSY;
- switch (ch) {
- case TIMER_CHANNEL_1:
- tmp = hperh->perh->CCVAL1;
- break;
- case TIMER_CHANNEL_2:
- tmp = hperh->perh->CCVAL2;
- break;
- case TIMER_CHANNEL_3:
- tmp = hperh->perh->CCVAL3;
- break;
- case TIMER_CHANNEL_4:
- tmp = hperh->perh->CCVAL4;
- break;
- default:
- break;
- }
- hperh->state = TIMER_STATE_READY;
- __UNLOCK(hperh);
- return tmp;
- }
- /**
- * @brief Sets TIMER output mode.
- * @param hperh: TIMER handle.
- * @param mode: TIMER output mode.
- * @param ch: TIMER Channels.
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1 : TIMER Channel 1 selected
- * @arg TIMER_CHANNEL_2 : TIMER Channel 2 selected
- * @arg TIMER_CHANNEL_3 : TIMER Channel 3 selected
- * @arg TIMER_CHANNEL_4 : TIMER Channel 4 selected
- * @retval None
- */
- void timer_set_output_mode(timer_handle_t *hperh, timer_oc_mode_t mode, timer_channel_t ch)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_OC_MODE(mode));
- assert_param(IS_TIMER_CHANNELS(ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH1OMOD_MSK, mode << TIMER_CHMR1_CH1OMOD_POSS);
- break;
- case TIMER_CHANNEL_2:
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_CH2OMOD_MSK, mode << TIMER_CHMR1_CH2OMOD_POSS);
- break;
- case TIMER_CHANNEL_3:
- MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH3OMOD_MSK, mode << TIMER_CHMR2_CH3OMOD_POSS);
- break;
- case TIMER_CHANNEL_4:
- MODIFY_REG(hperh->perh->CHMR2, TIMER_CHMR2_CH4OMOD_MSK, mode << TIMER_CHMR2_CH4OMOD_POSS);
- break;
- default:
- break;
- }
- return;
- }
- /**
- * @brief Configure the channel in commutation event.
- * @param hperh: TIMER handel
- * @param config: Parameters of the channel.
- * @retval None
- */
- void timer_com_change_config(timer_handle_t *hperh, timer_com_channel_config_t *config)
- {
- uint32_t cm1, cm2, cce;
- assert_param(IS_TIMER_COM_EVENT_INSTANCE(hperh->perh));
- assert_param(IS_FUNC_STATE(config->ch[0].en));
- assert_param(IS_FUNC_STATE(config->ch[0].n_en));
- assert_param(IS_TIMER_OC_MODE(config->ch[0].mode));
- assert_param(IS_FUNC_STATE(config->ch[1].en));
- assert_param(IS_FUNC_STATE(config->ch[1].n_en));
- assert_param(IS_TIMER_OC_MODE(config->ch[1].mode));
- assert_param(IS_FUNC_STATE(config->ch[2].en));
- assert_param(IS_FUNC_STATE(config->ch[2].n_en));
- assert_param(IS_TIMER_OC_MODE(config->ch[2].mode));
- TIMER_MOE_DISABLE(hperh);
- TIMER_DISABLE(hperh);
- cm1 = hperh->perh->CHMR1;
- cm2 = hperh->perh->CHMR2;
- cce = hperh->perh->CCEP;
- MODIFY_REG(cm1, (0x7 << 4), (config->ch[0].mode << 4));
- MODIFY_REG(cm1, (0x7 << 12), (config->ch[1].mode << 12));
- MODIFY_REG(cm2, (0x7 << 4), (config->ch[2].mode << 4));
- MODIFY_REG(cce, (0x1 << 0), (config->ch[0].en << 0));
- MODIFY_REG(cce, (0x1 << 2), (config->ch[0].n_en << 2));
- MODIFY_REG(cce, (0x1 << 4), (config->ch[1].en << 4));
- MODIFY_REG(cce, (0x1 << 6), (config->ch[1].n_en << 6));
- MODIFY_REG(cce, (0x1 << 8), (config->ch[2].en << 8));
- MODIFY_REG(cce, (0x1 << 10), (config->ch[2].n_en << 10));
- WRITE_REG(hperh->perh->CHMR1, cm1);
- WRITE_REG(hperh->perh->CHMR2, cm2);
- WRITE_REG(hperh->perh->CCEP, cce);
- TIMER_MOE_ENABLE(hperh);
- TIMER_ENABLE(hperh);
- return;
- }
- /**
- * @brief Configure the TIMER commutation event sequence.
- * @param hperh: TIMER handel
- * @param ts: the internal trigger corresponding to the timerer interfacing
- * with the hall sensor.
- * This parameter can be one of the following values:
- * @arg TIMER_TS_ITR0
- * @arg TIMER_TS_ITR1
- * @arg TIMER_TS_ITR2
- * @arg TIMER_TS_ITR3
- * @param trgi: the commutation event source.
- * This parameter can be one of the following values:
- * @arg ENABLE: Commutation event source is TRGI
- * @arg DISABLE: Commutation event source is set by software using the COMG bit
- * @retval None
- */
- void timer_com_event_config(timer_handle_t *hperh, timer_ts_t ts, type_func_t trgi)
- {
- assert_param(IS_TIMER_COM_EVENT_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_TS(ts));
- assert_param(IS_FUNC_STATE(trgi));
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, ts << TIMER_SMCON_TSSEL_POSS);
- SET_BIT(hperh->perh->CON2, TIMER_CON2_CCPCEN_MSK);
- MODIFY_REG(hperh->perh->CON2, TIMER_CON2_CCUSEL_MSK, trgi << TIMER_CON2_CCUSEL_POS);
- return;
- }
- /**
- * @brief Configure the TIMER commutation event sequence with interrupt.
- * @param hperh: TIMER handel
- * @param ts: the internal trigger corresponding to the timerer interfacing
- * with the hall sensor.
- * This parameter can be one of the following values:
- * @arg TIMER_TS_ITR0
- * @arg TIMER_TS_ITR1
- * @arg TIMER_TS_ITR2
- * @arg TIMER_TS_ITR3
- * @param trgi: the commutation event source.
- * This parameter can be one of the following values:
- * @arg ENABLE: Commutation event source is TRGI
- * @arg DISABLE: Commutation event source is set by software using the COMG bit
- * @retval None
- */
- void timer_com_event_config_it(timer_handle_t *hperh, timer_ts_t ts, type_func_t trgi)
- {
- timer_com_event_config(hperh, ts, trgi);
- timer_interrupt_config(hperh, TIMER_IT_COM, ENABLE);
- }
- /**
- * @brief Configure the break, dead timere, lock level state.
- * @param hperh: TIMER handle
- * @param config: Pointer to the timer_break_dead_timere_t structure.
- * @retval None
- */
- void timer_break_dead_time_config(timer_handle_t *hperh, timer_break_dead_time_t *config)
- {
- uint32_t tmp;
- assert_param(IS_TIMER_BREAK_INSTANCE(hperh->perh));
- assert_param(IS_FUNC_STATE(config->off_run));
- assert_param(IS_FUNC_STATE(config->off_idle));
- assert_param(IS_TIMER_CLOCK_LEVEL(config->lock_level));
- assert_param(IS_TIMER_DEAD_TIMERE(config->dead_time));
- assert_param(IS_FUNC_STATE(config->break_state));
- assert_param(IS_TIMER_BREAK_POLARITY(config->polarity));
- assert_param(IS_FUNC_STATE(config->auto_out));
- tmp = READ_REG(hperh->perh->BDCFG);
- MODIFY_REG(tmp, TIMER_BDCFG_OFFSSR_MSK, config->off_run << TIMER_BDCFG_OFFSSR_POS);
- MODIFY_REG(tmp, TIMER_BDCFG_OFFSSI_MSK, config->off_idle << TIMER_BDCFG_OFFSSI_POS);
- MODIFY_REG(tmp, TIMER_BDCFG_LOCKLVL_MSK, config->lock_level << TIMER_BDCFG_LOCKLVL_POSS);
- MODIFY_REG(tmp, TIMER_BDCFG_DT_MSK, config->dead_time << TIMER_BDCFG_DT_POSS);
- MODIFY_REG(tmp, TIMER_BDCFG_BRKEN_MSK, config->break_state << TIMER_BDCFG_BRKEN_POS);
- MODIFY_REG(tmp, TIMER_BDCFG_BRKP_MSK, config->polarity << TIMER_BDCFG_BRKP_POS);
- MODIFY_REG(tmp, TIMER_BDCFG_AOEN_MSK, config->auto_out << TIMER_BDCFG_AOEN_POS);
- WRITE_REG(hperh->perh->BDCFG, tmp);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- /**
- * @brief Configure the master mode
- * @param hperh: TIMER handle
- * @param config: Pointer to the timer_master_config_t structure.
- * @retval None
- */
- void timer_master_sync_config(timer_handle_t *hperh, timer_master_config_t *config)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_MASTER_MODE_SEL(config->sel));
- assert_param(IS_FUNC_STATE(config->master_en));
- hperh->state = TIMER_STATE_BUSY;
- MODIFY_REG(hperh->perh->CON2, TIMER_CON2_TRGOSEL_MSK, config->sel << TIMER_CON2_TRGOSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_MSCFG_MSK, config->master_en << TIMER_SMCON_MSCFG_POS);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- /**
- * @brief This function handles TIMER interrupts requests.
- * @param hperh: TIMER handle
- * @retval None
- */
- void timer_irq_handle(timer_handle_t *hperh)
- {
- uint32_t reg = hperh->perh->IFM;
- /* Capture or compare 1 event */
- if (READ_BIT(reg, TIMER_FLAG_CC1)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_CC1);
- hperh->ch = TIMER_ACTIVE_CHANNEL_1;
- /* Input capture event */
- if (READ_BIT(hperh->perh->CHMR1, TIMER_CHMR1_CC1SSEL_MSK)) {
- if (hperh->capture_cbk)
- hperh->capture_cbk(hperh);
- }
- else { /* Output compare event */
- if (hperh->delay_elapse_cbk)
- hperh->delay_elapse_cbk(hperh);
- if (hperh->pwm_pulse_finish_cbk)
- hperh->pwm_pulse_finish_cbk(hperh);
- }
- hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
- }
- /* Capture or compare 2 event */
- if (READ_BIT(reg, TIMER_FLAG_CC2)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_CC2);
- hperh->ch = TIMER_ACTIVE_CHANNEL_2;
- /* Input capture event */
- if (READ_BIT(hperh->perh->CHMR1, TIMER_CHMR1_CC2SSEL_MSK)) {
- if (hperh->capture_cbk)
- hperh->capture_cbk(hperh);
- }
- else { /* Output compare event */
- if (hperh->delay_elapse_cbk)
- hperh->delay_elapse_cbk(hperh);
- if (hperh->pwm_pulse_finish_cbk)
- hperh->pwm_pulse_finish_cbk(hperh);
- }
- hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
- }
- /* Capture or compare 3 event */
- if (READ_BIT(reg, TIMER_FLAG_CC3)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_CC3);
- hperh->ch = TIMER_ACTIVE_CHANNEL_3;
- /* Input capture event */
- if (READ_BIT(hperh->perh->CHMR2, TIMER_CHMR2_CC3SSEL_MSK)) {
- if (hperh->capture_cbk)
- hperh->capture_cbk(hperh);
- }
- else { /* Output compare event */
- if (hperh->delay_elapse_cbk)
- hperh->delay_elapse_cbk(hperh);
- if (hperh->pwm_pulse_finish_cbk)
- hperh->pwm_pulse_finish_cbk(hperh);
- }
- hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
- }
- /* Capture or compare 4 event */
- if (READ_BIT(reg, TIMER_FLAG_CC4)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_CC4);
- hperh->ch = TIMER_ACTIVE_CHANNEL_4;
- /* Input capture event */
- if (READ_BIT(hperh->perh->CHMR2, TIMER_CHMR2_CC4SSEL_MSK)) {
- if (hperh->capture_cbk)
- hperh->capture_cbk(hperh);
- }
- else { /* Output compare event */
- if (hperh->delay_elapse_cbk)
- hperh->delay_elapse_cbk(hperh);
- if (hperh->pwm_pulse_finish_cbk)
- hperh->pwm_pulse_finish_cbk(hperh);
- }
- hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
- }
- /* TIMER Update event */
- if (READ_BIT(reg, TIMER_FLAG_UPDATE)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_UPDATE);
- if (hperh->period_elapse_cbk)
- hperh->period_elapse_cbk(hperh);
- }
- /* TIMER Break input event */
- if (READ_BIT(reg, TIMER_FLAG_BREAK)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_BREAK);
- if (hperh->break_cbk)
- hperh->break_cbk(hperh);
- }
- /* TIMER Trigger detection event */
- if (READ_BIT(reg, TIMER_FLAG_TRIGGER)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_TRIGGER);
- if (hperh->trigger_cbk)
- hperh->trigger_cbk(hperh);
- }
- /* TIMER commutation event */
- if (READ_BIT(reg, TIMER_FLAG_COM)) {
- timer_clear_flag_status(hperh, TIMER_FLAG_COM);
- if (hperh->com_cbk)
- hperh->com_cbk(hperh);
- }
- return;
- }
- /**
- * @brief Configure DMA request source.
- * @param hperh: TIMER handle
- * @param req: DMA request source.
- * @param state: New state of the specified DMA request.
- * @retval None
- */
- void timer_dma_req_config(timer_handle_t *hperh, timer_dma_req_t req, type_func_t state)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_DMA_REQ(req));
- assert_param(IS_FUNC_STATE(state));
- if (state == ENABLE)
- SET_BIT(hperh->perh->DIER, req);
- else
- CLEAR_BIT(hperh->perh->DIER, req);
- return;
- }
- /**
- * @brief Enable/disable the specified TIMER interrupts.
- * @param hperh: Pointer to a timer_handle_t structure.
- * @param it: Specifies the timer interrupt sources to be enabled or disabled.
- * This parameter can be one of the @ref timer_it_t.
- * @param state: New state of the specified TIMER interrupts.
- * This parameter can be:
- * @arg ENABLE
- * @arg DISABLE
- * @retval None
- */
- void timer_interrupt_config(timer_handle_t *hperh, timer_it_t it, type_func_t state)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_IT(it));
- assert_param(IS_FUNC_STATE(state));
- if (state == ENABLE)
- SET_BIT(hperh->perh->DIER, it);
- else
- CLEAR_BIT(hperh->perh->DIER, it);
- return;
- }
- /**
- * @brief Get the status of TIMER interrupt source.
- * @param hperh: Pointer to a timer_handle_t structure.
- * @param it: Specifies the TIMER interrupt source.
- * This parameter can be one of the @ref timer_it_t.
- * @retval Status:
- * - 0: RESET
- * - 1: SET
- */
- it_status_t timer_get_it_status(timer_handle_t *hperh, timer_it_t it)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_IT(it));
- if (hperh->perh->DIVS & it)
- return SET;
- return RESET;
- }
- /**
- * @brief Get the status of TIMER interrupt flag.
- * @param hperh: Pointer to a timer_handle_t structure.
- * @param flag: Specifies the TIMER interrupt flag.
- * This parameter can be one of the @ref timer_flag_t.
- * @retval Status:
- * - 0: RESET
- * - 1: SET
- */
- flag_status_t timer_get_flag_status(timer_handle_t *hperh, timer_flag_t flag)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_FLAG(flag));
- if (hperh->perh->RIF & flag)
- return SET;
- return RESET;
- }
- /**
- * @brief Clear the TIMER interrupt flag.
- * @param hperh: Pointer to a uart_handle_t structure.
- * @param flag: Specifies the TIMER interrupt flag.
- * This parameter can be one of the @ref timer_flag_t.
- * @retval None
- */
- void timer_clear_flag_status(timer_handle_t *hperh, timer_flag_t flag)
- {
- assert_param(IS_TIMER_INSTANCE(hperh->perh));
- assert_param(IS_TIMER_FLAG(flag));
- hperh->perh->ICR = flag;
- return;
- }
- /**
- * @}
- */
- /** @defgroup TIMER_Public_Functions_Group12 Peripheral State functions
- * @brief Peripheral State functions
- *
- * @verbatim
- ==============================================================================
- ##### Peripheral State functions #####
- ==============================================================================
- [..]
- This subsection permit to get in run-timere the status of the peripheral
- and the data flow.
- @endverbatim
- * @{
- */
- /**
- * @brief Return the TIMER Base state
- * @param hperh: TIMER handle
- * @retval TIMER peripheral state
- */
- timer_state_t timer_get_state(timer_handle_t *hperh)
- {
- return hperh->state;
- }
- /**
- * @}
- */
- /**
- * @}
- */
- /** @addtogroup TIMER_Private_Functions
- * @{
- */
- #ifdef ALD_DMA
- /**
- * @brief TIMER DMA out compare complete callback.
- * @param arg: pointer to TIMER handle.
- * @retval None
- */
- void timer_dma_oc_cplt(void *arg)
- {
- timer_handle_t *hperh = (timer_handle_t *)arg;
- if (hperh->delay_elapse_cbk)
- hperh->delay_elapse_cbk(hperh);
- if (hperh->pwm_pulse_finish_cbk)
- hperh->pwm_pulse_finish_cbk(hperh);
- hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
- return;
- }
- /**
- * @brief TIMER DMA Capture complete callback.
- * @param arg: pointer to TIMER handle.
- * @retval None
- */
- void timer_dma_capture_cplt(void *arg)
- {
- timer_handle_t *hperh = (timer_handle_t *)arg;
- if (hperh->capture_cbk)
- hperh->capture_cbk(hperh);
- hperh->ch = TIMER_ACTIVE_CHANNEL_CLEARED;
- return;
- }
- /**
- * @brief TIMER DMA Period Elapse complete callback.
- * @param arg: pointer to TIMER handle.
- * @retval None
- */
- void timer_dma_period_elapse_cplt(void *arg)
- {
- timer_handle_t *hperh = (timer_handle_t *)arg;
- if (hperh->period_elapse_cbk)
- hperh->period_elapse_cbk(hperh);
- hperh->state = TIMER_STATE_READY;
- return;
- }
- /**
- * @brief TIMER DMA error callback
- * @param arg: pointer to TIMER handle.
- * @retval None
- */
- void timer_dma_error(void *arg)
- {
- timer_handle_t *hperh = (timer_handle_t *)arg;
- hperh->state = TIMER_STATE_READY;
- if (hperh->error_cbk)
- hperh->error_cbk(hperh);
- return;
- }
- #endif
- /**
- * @brief Time Base configuration
- * @param TIMERx: TIMER periheral
- * @param init: TIMER Base configuration structure
- * @retval None
- */
- static void timer_base_set_config(TIMER_TypeDef *TIMERx, timer_base_init_t *init)
- {
- assert_param(IS_TIMER_COUNTER_MODE(init->mode));
- assert_param(IS_TIMER_CLOCK_DIVISION(init->clk_div));
- if (init->mode == TIMER_CNT_MODE_UP || init->mode == TIMER_CNT_MODE_DOWN) {
- CLEAR_BIT(TIMERx->CON1, TIMER_CON1_CMSEL_MSK);
- MODIFY_REG(TIMERx->CON1, TIMER_CON1_DIRSEL_MSK, init->mode << TIMER_CON1_DIRSEL_POS);
- }
- else {
- MODIFY_REG(TIMERx->CON1, TIMER_CON1_CMSEL_MSK, (init->mode - 1) << TIMER_CON1_CMSEL_POSS);
- }
- if (IS_TIMER_CLOCK_DIVISION_INSTANCE(TIMERx))
- MODIFY_REG(TIMERx->CON1, TIMER_CON1_DFCKSEL_MSK, init->clk_div << TIMER_CON1_DFCKSEL_POSS);
- WRITE_REG(TIMERx->AR, init->period);
- WRITE_REG(TIMERx->PRES, init->prescaler);
- if (IS_TIMER_REPETITION_COUNTER_INSTANCE(TIMERx))
- WRITE_REG(TIMERx->REPAR, init->re_cnt);
- return;
- }
- /**
- * @brief Time Ouput Compare 1 configuration
- * @param TIMERx: Select the TIMER peripheral
- * @param oc_config: The ouput configuration structure
- * @retval None
- */
- static void timer_oc1_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC1EN_MSK);
- CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CH1OMOD_MSK);
- CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CC1SSEL_MSK);
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CH1OMOD_MSK, oc_config->oc_mode << TIMER_CHMR1_CH1OMOD_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC1POL_POS);
- if (IS_TIMER_CCXN_INSTANCE(TIMERx, TIMER_CHANNEL_1)) {
- assert_param(IS_TIMER_OCN_POLARITY(oc_config->ocn_polarity));
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NPOL_MSK, oc_config->ocn_polarity << TIMER_CCEP_CC1NPOL_POS);
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC1NEN_MSK);
- }
- if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
- assert_param(IS_TIMER_OCNIDLE_STATE(oc_config->ocn_idle));
- assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS1_MSK, oc_config->oc_idle << TIMER_CON2_OISS1_POS);
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS1N_MSK, oc_config->ocn_idle << TIMER_CON2_OISS1N_POS);
- }
- WRITE_REG(TIMERx->CCVAL1, oc_config->pulse);
- }
- /**
- * @brief Time Ouput Compare 2 configuration
- * @param TIMERx: Select the TIMER peripheral
- * @param oc_config: The ouput configuration structure
- * @retval None
- */
- static void timer_oc2_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC2EN_MSK);
- CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CH2OMOD_MSK);
- CLEAR_BIT(TIMERx->CHMR1, TIMER_CHMR1_CC2SSEL_MSK);
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CH2OMOD_MSK, oc_config->oc_mode << TIMER_CHMR1_CH2OMOD_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC2POL_POS);
- if (IS_TIMER_CCXN_INSTANCE(TIMERx, TIMER_CHANNEL_2)) {
- assert_param(IS_TIMER_OCN_POLARITY(oc_config->ocn_polarity));
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NPOL_MSK, oc_config->ocn_polarity << TIMER_CCEP_CC2NPOL_POS);
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC2NEN_MSK);
- }
- if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
- assert_param(IS_TIMER_OCNIDLE_STATE(oc_config->ocn_idle));
- assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS2_MSK, oc_config->oc_idle << TIMER_CON2_OISS2_POS);
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS2N_MSK, oc_config->ocn_idle << TIMER_CON2_OISS2N_POS);
- }
- WRITE_REG(TIMERx->CCVAL2, oc_config->pulse);
- }
- /**
- * @brief Time Ouput Compare 3 configuration
- * @param TIMERx: Select the TIMER peripheral
- * @param oc_config: The ouput configuration structure
- * @retval None
- */
- static void timer_oc3_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC3EN_MSK);
- CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CH3OMOD_MSK);
- CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CC3SSEL_MSK);
- MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CH3OMOD_MSK, oc_config->oc_mode << TIMER_CHMR2_CH3OMOD_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC3POL_POS);
- if (IS_TIMER_CCXN_INSTANCE(TIMERx, TIMER_CHANNEL_3)) {
- assert_param(IS_TIMER_OCN_POLARITY(oc_config->ocn_polarity));
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3NPOL_MSK, oc_config->ocn_polarity << TIMER_CCEP_CC3NPOL_POS);
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC3NEN_MSK);
- }
- if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
- assert_param(IS_TIMER_OCNIDLE_STATE(oc_config->ocn_idle));
- assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS3_MSK, oc_config->oc_idle << TIMER_CON2_OISS3_POS);
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS3N_MSK, oc_config->ocn_idle << TIMER_CON2_OISS3N_POS);
- }
- WRITE_REG(TIMERx->CCVAL3, oc_config->pulse);
- }
- /**
- * @brief Time Ouput Compare 4 configuration
- * @param TIMERx: Select the TIMER peripheral
- * @param oc_config: The ouput configuration structure
- * @retval None
- */
- static void timer_oc4_set_config(TIMER_TypeDef *TIMERx, timer_oc_init_t *oc_config)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC4EN_MSK);
- CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CH4OMOD_MSK);
- CLEAR_BIT(TIMERx->CHMR2, TIMER_CHMR2_CC4SSEL_MSK);
- MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CH4OMOD_MSK, oc_config->oc_mode << TIMER_CHMR2_CH4OMOD_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC4POL_MSK, oc_config->oc_polarity << TIMER_CCEP_CC4POL_POS);
- if (IS_TIMER_BREAK_INSTANCE(TIMERx)) {
- assert_param(IS_TIMER_OCIDLE_STATE(oc_config->oc_idle));
- MODIFY_REG(TIMERx->CON2, TIMER_CON2_OISS4_MSK, oc_config->oc_idle << TIMER_CON2_OISS4_POS);
- }
- WRITE_REG(TIMERx->CCVAL4, oc_config->pulse);
- }
- /**
- * @brief Enables or disables the TIMER Capture Compare Channel x.
- * @param TIMERx: Select the TIMER peripheral
- * @param ch: specifies the TIMER Channel
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1
- * @arg TIMER_CHANNEL_2: TIMER Channel 2
- * @arg TIMER_CHANNEL_3: TIMER Channel 3
- * @arg TIMER_CHANNEL_4: TIMER Channel 4
- * @param state: specifies the TIMER Channel CCxE bit new state.
- * @retval None
- */
- static void timer_ccx_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state)
- {
- assert_param(IS_TIMER_CC2_INSTANCE(TIMERx));
- assert_param(IS_TIMER_CHANNELS(ch));
- switch (ch) {
- case TIMER_CHANNEL_1:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1EN_MSK, state << TIMER_CCEP_CC1EN_POS);
- break;
- case TIMER_CHANNEL_2:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2EN_MSK, state << TIMER_CCEP_CC2EN_POS);
- break;
- case TIMER_CHANNEL_3:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3EN_MSK, state << TIMER_CCEP_CC3EN_POS);
- break;
- case TIMER_CHANNEL_4:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC4EN_MSK, state << TIMER_CCEP_CC4EN_POS);
- break;
- default:
- break;
- }
- }
- /**
- * @brief Enables or disables the TIMER Capture Compare Channel xN.
- * @param TIMERx: Select the TIMER peripheral
- * @param ch: specifies the TIMER Channel
- * This parameter can be one of the following values:
- * @arg TIMER_CHANNEL_1: TIMER Channel 1
- * @arg TIMER_CHANNEL_2: TIMER Channel 2
- * @arg TIMER_CHANNEL_3: TIMER Channel 3
- * @param state: specifies the TIMER Channel CCxNE bit new state.
- * @retval None
- */
- static void timer_ccxn_channel_cmd(TIMER_TypeDef* TIMERx, timer_channel_t ch, type_func_t state)
- {
- switch (ch) {
- case TIMER_CHANNEL_1:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NEN_MSK, state << TIMER_CCEP_CC1NEN_POS);
- break;
- case TIMER_CHANNEL_2:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NEN_MSK, state << TIMER_CCEP_CC2NEN_POS);
- break;
- case TIMER_CHANNEL_3:
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3NEN_MSK, state << TIMER_CCEP_CC3NEN_POS);
- break;
- default:
- break;
- }
- }
- /**
- * @brief Configure the TI1 as Input.
- * @param TIMERx: Select the TIMER peripheral.
- * @param polarity: The Input Polarity.
- * @param sel: specifies the input to be used.
- * @param filter: Specifies the Input Capture Filter.
- * This parameter must be a value between 0x00 and 0x0F.
- * @retval None
- */
- static void timer_ti1_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC1EN_MSK);
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CC1SSEL_MSK, sel << TIMER_CHMR1_CC1SSEL_POSS);
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I1FLT_MSK, filter << TIMER_CHMR1_I1FLT_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC1POL_POS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC1NPOL_POS);
- return;
- }
- /**
- * @brief Configure the Polarity and Filter for TI1.
- * @param TIMERx: Select the TIMER peripheral.
- * @param polarity: The Input Polarity.
- * @param filter: Specifies the Input Capture Filter.
- * This parameter must be a value between 0x00 and 0x0F.
- * @retval None
- */
- static void timer_ti1_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter)
- {
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I1FLT_MSK, filter << TIMER_CHMR1_I1FLT_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC1POL_POS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC1NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC1NPOL_POS);
- return;
- }
- /**
- * @brief Configure the TI2 as Input.
- * @param TIMERx: Select the TIMER peripheral.
- * @param polarity: The Input Polarity.
- * @param sel: specifies the input to be used.
- * @param filter: Specifies the Input Capture Filter.
- * This parameter must be a value between 0x00 and 0x0F.
- * @retval None
- */
- static void timer_ti2_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC2EN_MSK);
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_CC2SSEL_MSK, sel << TIMER_CHMR1_CC2SSEL_POSS);
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I2FLT_MSK, filter << TIMER_CHMR1_I2FLT_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC2POL_POS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC2NPOL_POS);
- return;
- }
- /**
- * @brief Configure the Polarity and Filter for TI2.
- * @param TIMERx: Select the TIMER peripheral.
- * @param polarity: The Input Polarity.
- * @param filter: Specifies the Input Capture Filter.
- * This parameter must be a value between 0x00 and 0x0F.
- * @retval None
- */
- static void timer_ti2_set_config_stage(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity, uint32_t filter)
- {
- MODIFY_REG(TIMERx->CHMR1, TIMER_CHMR1_I2FLT_MSK, filter << TIMER_CHMR1_I2FLT_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC2POL_POS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC2NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC2NPOL_POS);
- return;
- }
- /**
- * @brief Configure the TI3 as Input.
- * @param TIMERx: Select the TIMER peripheral.
- * @param polarity: The Input Polarity.
- * @param sel: specifies the input to be used.
- * @param filter: Specifies the Input Capture Filter.
- * This parameter must be a value between 0x00 and 0x0F.
- * @retval None
- */
- static void timer_ti3_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC3EN_MSK);
- MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CC3SSEL_MSK, sel << TIMER_CHMR2_CC3SSEL_POSS);
- MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_I3FLT_MSK, filter << TIMER_CHMR2_I3FLT_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC3POL_POS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC3NPOL_MSK, ((polarity >> 1) & 0x1) << TIMER_CCEP_CC3NPOL_POS);
- return;
- }
- /**
- * @brief Configure the TI4 as Input.
- * @param TIMERx: Select the TIMER peripheral.
- * @param polarity: The Input Polarity.
- * @param sel: specifies the input to be used.
- * @param filter: Specifies the Input Capture Filter.
- * This parameter must be a value between 0x00 and 0x0F.
- * @retval None
- */
- static void timer_ti4_set_config(TIMER_TypeDef *TIMERx, timer_ic_polarity_t polarity,
- timer_ic_select_t sel, uint32_t filter)
- {
- CLEAR_BIT(TIMERx->CCEP, TIMER_CCEP_CC4EN_MSK);
- MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_CC4SSEL_MSK, sel << TIMER_CHMR2_CC4SSEL_POSS);
- MODIFY_REG(TIMERx->CHMR2, TIMER_CHMR2_I4FLT_MSK, filter << TIMER_CHMR2_I4FLT_POSS);
- MODIFY_REG(TIMERx->CCEP, TIMER_CCEP_CC4POL_MSK, (polarity & 0x1) << TIMER_CCEP_CC4POL_POS);
- return;
- }
- /**
- * @brief Configures the TIMERx External Trigger (ETR).
- * @param TIMERx: Select the TIMER peripheral
- * @param psc: The external Trigger Prescaler.
- * @param polarity: The external Trigger Polarity.
- * @param filter: External Trigger Filter.
- * This parameter must be a value between 0x00 and 0x0F
- * @retval None
- */
- static void timer_etr_set_config(TIMER_TypeDef* TIMERx, timer_etr_psc_t psc, timer_clock_polarity_t polarity, uint32_t filter)
- {
- MODIFY_REG(TIMERx->SMCON, TIMER_SMCON_ETFLT_MSK, filter << TIMER_SMCON_ETFLT_POSS);
- MODIFY_REG(TIMERx->SMCON, TIMER_SMCON_ETPSEL_MSK, psc << TIMER_SMCON_ETPSEL_POSS);
- CLEAR_BIT(TIMERx->SMCON, TIMER_SMCON_ECM2EN_MSK);
- MODIFY_REG(TIMERx->SMCON, TIMER_SMCON_ETPOL_MSK, polarity << TIMER_SMCON_ETPOL_POS);
- return;
- }
- /**
- * @brief Time Slave configuration
- * @param hperh: pointer to a timer_handle_t structure that contains
- * the configuration information for TIMER module.
- * @param config: The slave configuration structure
- * @retval None
- */
- static void timer_slave_set_config(timer_handle_t *hperh, timer_slave_config_t *config)
- {
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_TSSEL_MSK, config->input << TIMER_SMCON_TSSEL_POSS);
- MODIFY_REG(hperh->perh->SMCON, TIMER_SMCON_SMODS_MSK, config->mode << TIMER_SMCON_SMODS_POSS);
- switch (config->input) {
- case TIMER_TS_ETRF:
- timer_etr_set_config(hperh->perh, config->psc, config->polarity, config->filter);
- break;
- case TIMER_TS_TI1F_ED:
- CLEAR_BIT(hperh->perh->CCEP, TIMER_CCEP_CC1EN_MSK);
- MODIFY_REG(hperh->perh->CHMR1, TIMER_CHMR1_I1FLT_MSK, config->filter << TIMER_CHMR1_I1FLT_POSS);
- break;
- case TIMER_TS_TI1FP1:
- timer_ti1_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
- break;
- case TIMER_TS_TI2FP2:
- timer_ti2_set_config_stage(hperh->perh, (timer_ic_polarity_t)config->polarity, config->filter);
- break;
- default:
- break;
- }
- }
- /**
- * @}
- */
- #endif /* ALD_TIMER */
- /**
- * @}
- */
- /**
- * @}
- */
|