FlexCAN_Ip.c 181 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813
  1. /*==================================================================================================
  2. * Project : RTD AUTOSAR 4.4
  3. * Platform : CORTEXM
  4. * Peripheral : FLEXCAN
  5. * Dependencies :
  6. *
  7. * Autosar Version : 4.4.0
  8. * Autosar Revision : ASR_REL_4_4_REV_0000
  9. * Autosar Conf.Variant :
  10. * SW Version : 1.0.0
  11. * Build Version : S32K1_RTD_1_0_0_HF01_D2109_ASR_REL_4_4_REV_0000_20210907
  12. *
  13. * (c) Copyright 2020-2021 NXP Semiconductors
  14. * All Rights Reserved.
  15. *
  16. * NXP Confidential. This software is owned or controlled by NXP and may only be
  17. * used strictly in accordance with the applicable license terms. By expressly
  18. * accepting such terms or by downloading, installing, activating and/or otherwise
  19. * using the software, you are agreeing that you have read, and that you agree to
  20. * comply with and are bound by, such license terms. If you do not agree to be
  21. * bound by the applicable license terms, then you may not retain, install,
  22. * activate or otherwise use the software.
  23. ==================================================================================================*/
  24. /**
  25. * @file FlexCAN_Ip.c
  26. * @brief FlexCAN Driver File
  27. * @addtogroup FlexCAN
  28. * @{
  29. */
  30. #ifdef __cplusplus
  31. extern "C"{
  32. #endif
  33. /*==================================================================================================
  34. * INCLUDE FILES
  35. * 1) system and project includes
  36. * 2) needed interfaces from external units
  37. * 3) internal and external interfaces from this unit
  38. ==================================================================================================*/
  39. #include "FlexCAN_Ip.h"
  40. #include "FlexCAN_Ip_Irq.h"
  41. #include "FlexCAN_Ip_HwAccess.h"
  42. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  43. #include "Dma_Ip.h"
  44. #endif
  45. #if (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE)
  46. #define USER_MODE_REG_PROT_ENABLED (STD_ON)
  47. #include "RegLockMacros.h"
  48. #endif /* (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE) */
  49. #include "SchM_Can.h"
  50. #if ((defined (MCAL_ENABLE_FAULT_INJECTION)) || (defined (ERR_IPV_FLEXCAN_E050246)) || (defined (ERR_IPV_FLEXCAN_E050630)))
  51. #include "Mcal.h"
  52. #endif
  53. /*==================================================================================================
  54. * SOURCE FILE VERSION INFORMATION
  55. ==================================================================================================*/
  56. #define FLEXCAN_IP_VENDOR_ID_C 43
  57. #define FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C 4
  58. #define FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C 4
  59. #define FLEXCAN_IP_AR_RELEASE_REVISION_VERSION_C 0
  60. #define FLEXCAN_IP_SW_MAJOR_VERSION_C 1
  61. #define FLEXCAN_IP_SW_MINOR_VERSION_C 0
  62. #define FLEXCAN_IP_SW_PATCH_VERSION_C 0
  63. /*==================================================================================================
  64. * FILE VERSION CHECKS
  65. ==================================================================================================*/
  66. /* Check if current file and FlexCAN_Ip.h header file are of the same vendor */
  67. #if (FLEXCAN_IP_VENDOR_ID_C != FLEXCAN_IP_VENDOR_ID_H)
  68. #error "FlexCAN_Ip.c and FlexCAN_Ip.h have different vendor ids"
  69. #endif
  70. /* Check if current file and FlexCAN_Ip.h header file are of the same Autosar version */
  71. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_H) || \
  72. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_H) || \
  73. (FLEXCAN_IP_AR_RELEASE_REVISION_VERSION_C != FLEXCAN_IP_AR_RELEASE_REVISION_VERSION_H) \
  74. )
  75. #error "AutoSar Version Numbers of FlexCAN_Ip.c and FlexCAN_Ip.h are different"
  76. #endif
  77. /* Check if current file and FlexCAN_Ip.h header file are of the same Software version */
  78. #if ((FLEXCAN_IP_SW_MAJOR_VERSION_C != FLEXCAN_IP_SW_MAJOR_VERSION_H) || \
  79. (FLEXCAN_IP_SW_MINOR_VERSION_C != FLEXCAN_IP_SW_MINOR_VERSION_H) || \
  80. (FLEXCAN_IP_SW_PATCH_VERSION_C != FLEXCAN_IP_SW_PATCH_VERSION_H) \
  81. )
  82. #error "Software Version Numbers of FlexCAN_Ip.c and FlexCAN_Ip.h are different"
  83. #endif
  84. /* Check if current file and FlexCAN_Ip_Irq.h header file are of the same vendor */
  85. #if (FLEXCAN_IP_VENDOR_ID_C != FLEXCAN_IP_IRQ_VENDOR_ID_H)
  86. #error "FlexCAN_Ip.c and FlexCAN_Ip_Irq.h have different vendor ids"
  87. #endif
  88. /* Check if current file and FlexCAN_Ip_Irq.h header file are of the same Autosar version */
  89. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != FLEXCAN_IP_IRQ_AR_RELEASE_MAJOR_VERSION_H) || \
  90. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != FLEXCAN_IP_IRQ_AR_RELEASE_MINOR_VERSION_H) || \
  91. (FLEXCAN_IP_AR_RELEASE_REVISION_VERSION_C != FLEXCAN_IP_IRQ_AR_RELEASE_REVISION_VERSION_H) \
  92. )
  93. #error "AutoSar Version Numbers of FlexCAN_Ip.c and FlexCAN_Ip_Irq.h are different"
  94. #endif
  95. /* Check if current file and FlexCAN_Ip_Irq.h header file are of the same Software version */
  96. #if ((FLEXCAN_IP_SW_MAJOR_VERSION_C != FLEXCAN_IP_IRQ_SW_MAJOR_VERSION_H) || \
  97. (FLEXCAN_IP_SW_MINOR_VERSION_C != FLEXCAN_IP_IRQ_SW_MINOR_VERSION_H) || \
  98. (FLEXCAN_IP_SW_PATCH_VERSION_C != FLEXCAN_IP_IRQ_SW_PATCH_VERSION_H) \
  99. )
  100. #error "Software Version Numbers of FlexCAN_Ip.c and FlexCAN_Ip_Irq.h are different"
  101. #endif
  102. /* Check if current file and FlexCAN_Ip_HwAccess.h header file are of the same vendor */
  103. #if (FLEXCAN_IP_VENDOR_ID_C != FLEXCAN_IP_HWACCESS_VENDOR_ID_H)
  104. #error "FlexCAN_Ip.c and FlexCAN_Ip_HwAccess.h have different vendor ids"
  105. #endif
  106. /* Check if current file and FlexCAN_Ip_HwAccess.h header file are of the same Autosar version */
  107. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != FLEXCAN_IP_HWACCESS_AR_RELEASE_MAJOR_VERSION_H) || \
  108. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != FLEXCAN_IP_HWACCESS_AR_RELEASE_MINOR_VERSION_H) || \
  109. (FLEXCAN_IP_AR_RELEASE_REVISION_VERSION_C != FLEXCAN_IP_HWACCESS_AR_RELEASE_REVISION_VERSION_H) \
  110. )
  111. #error "AutoSar Version Numbers of FlexCAN_Ip.c and FlexCAN_Ip_HwAccess.h are different"
  112. #endif
  113. /* Check if current file and FlexCAN_Ip_HwAccess.h header file are of the same Software version */
  114. #if ((FLEXCAN_IP_SW_MAJOR_VERSION_C != FLEXCAN_IP_HWACCESS_SW_MAJOR_VERSION_H) || \
  115. (FLEXCAN_IP_SW_MINOR_VERSION_C != FLEXCAN_IP_HWACCESS_SW_MINOR_VERSION_H) || \
  116. (FLEXCAN_IP_SW_PATCH_VERSION_C != FLEXCAN_IP_HWACCESS_SW_PATCH_VERSION_H) \
  117. )
  118. #error "Software Version Numbers of FlexCAN_Ip.c and FlexCAN_Ip_HwAccess.h are different"
  119. #endif
  120. #ifndef DISABLE_MCAL_INTERMODULE_ASR_CHECK
  121. /* Check if current file and SchM_Can header file are of the same version */
  122. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != SCHM_CAN_AR_RELEASE_MAJOR_VERSION) || \
  123. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != SCHM_CAN_AR_RELEASE_MINOR_VERSION) \
  124. )
  125. #error "AUTOSAR Version Numbers of FlexCAN_Ip.c and SchM_Can.h are different"
  126. #endif
  127. /* Checks against current file and Dma_Ip.h */
  128. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  129. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != DMA_IP_AR_RELEASE_MAJOR_VERSION_H) || \
  130. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != DMA_IP_AR_RELEASE_MINOR_VERSION_H) \
  131. )
  132. #error "AUTOSAR Version Numbers of FlexCAN_Ip.c and Dma_Ip.h are different"
  133. #endif
  134. #endif
  135. /* Checks against current file and RegLockMacros.h */
  136. #if (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE)
  137. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != REGLOCKMACROS_AR_RELEASE_MAJOR_VERSION) || \
  138. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != REGLOCKMACROS_AR_RELEASE_MINOR_VERSION) \
  139. )
  140. #error "AUTOSAR Version Numbers of FlexCAN_Ip.c and RegLockMacros.h are different"
  141. #endif
  142. #endif /* (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE) */
  143. /* Check if current file and Mcal.h header file are of the same Software version */
  144. #ifdef MCAL_ENABLE_FAULT_INJECTION
  145. #if ((FLEXCAN_IP_AR_RELEASE_MAJOR_VERSION_C != MCAL_AR_RELEASE_MAJOR_VERSION) || \
  146. (FLEXCAN_IP_AR_RELEASE_MINOR_VERSION_C != MCAL_AR_RELEASE_MINOR_VERSION) \
  147. )
  148. #error "AUTOSAR Version Numbers of FlexCAN_Ip.c and Mcal.h are different"
  149. #endif
  150. #endif
  151. #endif
  152. /*==================================================================================================
  153. * LOCAL TYPEDEFS (STRUCTURES, UNIONS, ENUMS)
  154. ==================================================================================================*/
  155. /*==================================================================================================
  156. * LOCAL MACROS
  157. ==================================================================================================*/
  158. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  159. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  160. #define FLEXCAN_IP_ENHANCE_TRASNFER_DIMENSION_LIST (13U)
  161. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  162. #define FLEXCAN_IP_TRASNFER_DIMENSION_LIST (10U)
  163. #endif /*(FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)*/
  164. /*==================================================================================================
  165. * LOCAL CONSTANTS
  166. ==================================================================================================*/
  167. /*==================================================================================================
  168. * GLOBAL CONSTANTS
  169. ==================================================================================================*/
  170. /*==================================================================================================
  171. * GLOBAL VARIABLES
  172. ==================================================================================================*/
  173. #define CAN_START_SEC_CONST_UNSPECIFIED
  174. #include "Can_MemMap.h"
  175. /* Table of base addresses for CAN instances. */
  176. static FLEXCAN_Type * const Flexcan_Ip_apxBase[] = IP_FLEXCAN_BASE_PTRS;
  177. #define CAN_STOP_SEC_CONST_UNSPECIFIED
  178. #include "Can_MemMap.h"
  179. #if (FLEXCAN_IP_FEATURE_NO_CACHE_NEEDED == STD_ON)
  180. #define CAN_START_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  181. #else
  182. #define CAN_START_SEC_VAR_CLEARED_UNSPECIFIED
  183. #endif /* (FLEXCAN_IP_FEATURE_NO_CACHE_NEEDED == STD_ON) */
  184. #include "Can_MemMap.h"
  185. /* Pointer to runtime state structure.*/
  186. static Flexcan_Ip_StateType * Flexcan_Ip_apxState[FLEXCAN_INSTANCE_COUNT];
  187. #if (FLEXCAN_IP_FEATURE_NO_CACHE_NEEDED == STD_ON)
  188. #define CAN_STOP_SEC_VAR_CLEARED_UNSPECIFIED_NO_CACHEABLE
  189. #else
  190. #define CAN_STOP_SEC_VAR_CLEARED_UNSPECIFIED
  191. #endif /* (FLEXCAN_IP_FEATURE_NO_CACHE_NEEDED == STD_ON) */
  192. #include "Can_MemMap.h"
  193. #define CAN_START_SEC_CODE
  194. #include "Can_MemMap.h"
  195. /*==================================================================================================
  196. * LOCAL FUNCTION PROTOTYPES
  197. ==================================================================================================*/
  198. /* Init legacy, enhanced fifo if requested, if no fifo request, return success. */
  199. static Flexcan_Ip_StatusType FlexCAN_InitRxFifo(FLEXCAN_Type * pBase, const Flexcan_Ip_ConfigType * Flexcan_Ip_pData);
  200. /* Init basically controller , this function is called in FlexCAN_Ip_Init only */
  201. static Flexcan_Ip_StatusType FlexCAN_InitController(FLEXCAN_Type * pBase, const Flexcan_Ip_ConfigType * Flexcan_Ip_pData);
  202. static Flexcan_Ip_StatusType FlexCAN_InitCtroll(FLEXCAN_Type * pBase, const Flexcan_Ip_ConfigType * Flexcan_Ip_pData);
  203. static Flexcan_Ip_StatusType FlexCAN_StartRxMessageBufferData(uint8 instance,
  204. uint8 mb_idx,
  205. Flexcan_Ip_MsgBuffType * data,
  206. boolean isPolling
  207. );
  208. static Flexcan_Ip_StatusType FlexCAN_StartSendData(uint8 Flexcan_Ip_u8Instance,
  209. uint8 mb_idx,
  210. const Flexcan_Ip_DataInfoType * tx_info,
  211. uint32 msg_id,
  212. const uint8 * mb_data
  213. );
  214. static Flexcan_Ip_StatusType FlexCAN_StartRxMessageFifoData(uint8 instance, Flexcan_Ip_MsgBuffType * data);
  215. static Flexcan_Ip_StatusType FlexCAN_ProccessLegacyRxFIFO(uint8 u8Instance, uint32 u32TimeoutMs);
  216. static void FlexCAN_IRQHandlerRxMB(uint8 instance, uint32 mb_idx);
  217. static void FlexCAN_IRQHandlerTxMB(uint8 u8Instance, uint32 u32MbIdx);
  218. static inline void FlexCAN_IRQHandlerRxFIFO(uint8 instance, uint32 mb_idx);
  219. static inline void FlexCAN_ProcessSpuriousInterruptMB(uint8 instance, uint32 startMbIdx, uint32 endMbIdx);
  220. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  221. static void DMA_Can_Callback(uint8 instance);
  222. #endif
  223. #if (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE)
  224. static void FlexCAN_SetUserAccessAllowed(const FLEXCAN_Type * pBase);
  225. static void FlexCAN_ClrUserAccessAllowed(const FLEXCAN_Type * pBase);
  226. #endif
  227. static Flexcan_Ip_StatusType FlexCAN_AbortTxTransfer(uint8 u8Instance, uint8 mb_idx);
  228. static void FlexCAN_AbortRxTransfer(uint8 u8Instance, uint8 mb_idx);
  229. static void FlexCAN_CompleteRxMessageFifoData(uint8 instance);
  230. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  231. static void FlexCAN_IRQHandlerEnhancedRxFIFO(uint8 instance, uint32 intType);
  232. static inline boolean FlexCAN_ProcessIRQHandlerEnhancedRxFIFO(uint8 u8Instance, boolean bIsSpuriousIntPrevious);
  233. static void FlexCAN_CompleteRxMessageEnhancedFifoData(uint8 instance);
  234. static Flexcan_Ip_StatusType FlexCAN_StartRxMessageEnhancedFifoData(uint8 instance, Flexcan_Ip_MsgBuffType * data);
  235. static Flexcan_Ip_StatusType FlexCAN_ProccessEnhancedRxFifo(uint8 u8Instance, uint32 u32TimeoutMs);
  236. #endif
  237. /*==================================================================================================
  238. * LOCAL FUNCTIONS
  239. ==================================================================================================*/
  240. /*FUNCTION**********************************************************************
  241. *
  242. * Function Name : FlexCAN_InitRxFifo
  243. * Description : Initialize fifo and dma if requested.
  244. *
  245. * This is not a public API as it is called from other driver functions.
  246. *END**************************************************************************/
  247. static Flexcan_Ip_StatusType FlexCAN_InitRxFifo(FLEXCAN_Type * pBase, const Flexcan_Ip_ConfigType * Flexcan_Ip_pData)
  248. {
  249. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  250. /* Enable RxFIFO feature, if requested. This might fail if the FD mode is enabled. */
  251. if (Flexcan_Ip_pData->is_rx_fifo_needed)
  252. {
  253. eResult = FlexCAN_EnableRxFifo(pBase, (uint32)Flexcan_Ip_pData->num_id_filters);
  254. }
  255. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  256. /* Enable Enhanced RxFIFO feature, if requested.
  257. * This might fail if the current CAN instance does not support Enhaneced RxFIFO or the Rx FIFO is enabled. */
  258. if ((FLEXCAN_STATUS_SUCCESS == eResult) && (Flexcan_Ip_pData->is_enhanced_rx_fifo_needed))
  259. {
  260. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  261. if (FLEXCAN_RXFIFO_USING_DMA == Flexcan_Ip_pData->transfer_type)
  262. {
  263. eResult = FlexCAN_EnableEnhancedRxFifo(pBase,
  264. (uint32)Flexcan_Ip_pData->num_enhanced_std_id_filters,
  265. (uint32)Flexcan_Ip_pData->num_enhanced_ext_id_filters,
  266. (uint32)0U
  267. ); /* for dma, each a frame received -> a minor loop */
  268. }
  269. else
  270. #endif /* FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  271. {
  272. eResult = FlexCAN_EnableEnhancedRxFifo(pBase,
  273. (uint32)Flexcan_Ip_pData->num_enhanced_std_id_filters,
  274. (uint32)Flexcan_Ip_pData->num_enhanced_ext_id_filters,
  275. (uint32)Flexcan_Ip_pData->num_enhanced_watermark
  276. );
  277. }
  278. }
  279. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  280. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  281. if (FLEXCAN_STATUS_SUCCESS == eResult)
  282. {
  283. /* Enable DMA support for RxFIFO transfer, if requested. */
  284. if (FLEXCAN_RXFIFO_USING_DMA == Flexcan_Ip_pData->transfer_type)
  285. {
  286. if (((pBase->MCR & FLEXCAN_MCR_RFEN_MASK) >> FLEXCAN_MCR_RFEN_SHIFT) != 0U)
  287. {
  288. FlexCAN_SetRxFifoDMA(pBase, TRUE);
  289. }
  290. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  291. else if (FlexCAN_IsEnhancedRxFifoAvailable(pBase))
  292. {
  293. if (FlexCAN_IsEnhancedRxFifoEnabled(pBase))
  294. {
  295. FlexCAN_SetRxFifoDMA(pBase, TRUE);
  296. FlexCAN_ConfigEnhancedRxFifoDMA(pBase, 20U); /* always transfer 80 bytes (DMALW = 19)*/
  297. }
  298. else
  299. {
  300. eResult = FLEXCAN_STATUS_ERROR;
  301. }
  302. }
  303. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  304. else
  305. {
  306. eResult = FLEXCAN_STATUS_ERROR;
  307. }
  308. }
  309. else
  310. {
  311. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  312. if (FlexCAN_IsEnhancedRxFifoAvailable(pBase))
  313. {
  314. /* Clear Enhanced Rx FIFO status.*/
  315. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(pBase, FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE);
  316. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(pBase, FLEXCAN_IP_ENHANCED_RXFIFO_WATERMARK);
  317. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(pBase, FLEXCAN_IP_ENHANCED_RXFIFO_OVERFLOW);
  318. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(pBase, FLEXCAN_IP_ENHANCED_RXFIFO_UNDERFLOW);
  319. /* Clear the Enhanced RX FIFO engine */
  320. FlexCAN_ClearEnhancedRxFifoEngine(pBase);
  321. }
  322. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  323. FlexCAN_SetRxFifoDMA(pBase, FALSE);
  324. }
  325. }
  326. #endif /* FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  327. return eResult;
  328. }
  329. /*FUNCTION**********************************************************************
  330. *
  331. * Function Name : FlexCAN_InitCtroll
  332. * Description : Initialize basically controller.
  333. *
  334. * This is not a public API as it is called from other driver functions.
  335. *END**************************************************************************/
  336. static Flexcan_Ip_StatusType FlexCAN_InitCtroll(FLEXCAN_Type * pBase, const Flexcan_Ip_ConfigType * Flexcan_Ip_pData)
  337. {
  338. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  339. /* Disable the self reception feature if FlexCAN is not in loopback mode. */
  340. if (Flexcan_Ip_pData->flexcanMode != FLEXCAN_LOOPBACK_MODE)
  341. {
  342. FlexCAN_SetSelfReception(pBase, FALSE);
  343. }
  344. /* Init legacy fifo, enhanced fifo if requested. */
  345. eResult = FlexCAN_InitRxFifo(pBase, Flexcan_Ip_pData);
  346. if (eResult != FLEXCAN_STATUS_SUCCESS)
  347. {
  348. /* To enter Disable Mode requires FreezMode first */
  349. (void)FlexCAN_EnterFreezeMode(pBase);
  350. (void)FlexCAN_Disable(pBase);
  351. }
  352. else
  353. {
  354. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  355. /* Set payload size. */
  356. FlexCAN_SetPayloadSize(pBase, &Flexcan_Ip_pData->payload);
  357. #endif /* FLEXCAN_IP_FEATURE_HAS_FD */
  358. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  359. eResult = FlexCAN_SetMaxMsgBuffNum(pBase, Flexcan_Ip_pData->max_num_mb);
  360. if (eResult != FLEXCAN_STATUS_SUCCESS)
  361. {
  362. /* To enter Disable Mode requires FreezMode first */
  363. (void)FlexCAN_EnterFreezeMode(pBase);
  364. (void)FlexCAN_Disable(pBase);
  365. }
  366. #else
  367. (void)FlexCAN_SetMaxMsgBuffNum(pBase, Flexcan_Ip_pData->max_num_mb);
  368. #endif /* FLEXCAN_IP_DEV_ERROR_DETECT */
  369. }
  370. return eResult;
  371. }
  372. /*FUNCTION**********************************************************************
  373. *
  374. * Function Name : FlexCAN_InitController
  375. * Description : Initialize basically controller.
  376. *
  377. * This is not a public API as it is called from other driver functions.
  378. *END**************************************************************************/
  379. static Flexcan_Ip_StatusType FlexCAN_InitController(FLEXCAN_Type * pBase, const Flexcan_Ip_ConfigType * Flexcan_Ip_pData)
  380. {
  381. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  382. if (FlexCAN_IsEnabled(pBase))
  383. {
  384. /* To enter Disable Mode requires FreezMode first */
  385. eResult = FlexCAN_EnterFreezeMode(pBase);
  386. if (FLEXCAN_STATUS_SUCCESS == eResult)
  387. {
  388. eResult = FlexCAN_Disable(pBase);
  389. }
  390. }
  391. if (FLEXCAN_STATUS_SUCCESS == eResult)
  392. {
  393. #if (FLEXCAN_IP_FEATURE_HAS_PE_CLKSRC_SELECT == STD_ON)
  394. /* Select a source clock for the FlexCAN engine */
  395. FlexCAN_SetClkSrc(pBase, Flexcan_Ip_pData->is_pe_clock);
  396. #endif
  397. /* Enable FlexCAN Module need to perform SoftReset & ClearRam */
  398. pBase->MCR &= ~FLEXCAN_MCR_MDIS_MASK;
  399. /* Initialize FLEXCAN device */
  400. eResult = FlexCAN_Init(pBase);
  401. if (eResult != FLEXCAN_STATUS_SUCCESS)
  402. {
  403. /* To enter Disable Mode requires FreezMode first */
  404. (void)FlexCAN_EnterFreezeMode(pBase);
  405. (void)FlexCAN_Disable(pBase);
  406. }
  407. else
  408. {
  409. #if (FLEXCAN_IP_FEATURE_HAS_MEM_ERR_DET == STD_ON)
  410. /* Disable the Protection again because is enabled by soft reset */
  411. FlexCAN_DisableMemErrorDetection(pBase);
  412. #endif
  413. #if defined(CAN_FEATURE_S32K1XX)
  414. if (TRUE == FlexCAN_IsFDAvailable(pBase))
  415. {
  416. #endif /* defined(CAN_FEATURE_S32K1XX) */
  417. /* Enable/Disable FD and check FD was set as expected. Setting FD as enabled
  418. * might fail if the current CAN instance does not support FD. */
  419. FlexCAN_SetFDEnabled(pBase, Flexcan_Ip_pData->fd_enable, Flexcan_Ip_pData->bitRateSwitch);
  420. /* No more required, I don't expect any other NPIs with and without Interfaces with FD support
  421. * if (FLEXCAN_IsFDEnabled(base) != Flexcan_Ip_pData->fd_enable)
  422. {
  423. return FLEXCAN_STATUS_ERROR;
  424. }*/
  425. #if defined(CAN_FEATURE_S32K1XX)
  426. }
  427. #endif /* defined(CAN_FEATURE_S32K1XX) */
  428. /* configure depends on controller options. */
  429. FlexCAN_ConfigCtrlOptions(pBase, Flexcan_Ip_pData->ctrlOptions);
  430. eResult = FlexCAN_InitCtroll(pBase, Flexcan_Ip_pData);
  431. }
  432. }
  433. return eResult;
  434. }
  435. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  436. /*FUNCTION**********************************************************************
  437. *
  438. * Function Name : FLEXCAN_CompleteRxMessageEnhancedFifoData
  439. * Description : Finish up a receive by completing the process of receiving
  440. * data and disabling the interrupt.
  441. * This is not a public API as it is called from other driver functions.
  442. *
  443. *END**************************************************************************/
  444. static void FlexCAN_CompleteRxMessageEnhancedFifoData(uint8 instance)
  445. {
  446. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  447. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  448. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  449. uint32 u32MbCnt = 0U;
  450. uint8 j;
  451. Flexcan_Ip_MsgBuffType * fifo_message = NULL_PTR;
  452. uint32 * msgData_32 = NULL_PTR;
  453. uint8 flexcan_mb_dlc_value = 0U;
  454. uint8 can_dlc_payload = 0U;
  455. uint8 can_real_payload = 0U;
  456. #endif /* FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  457. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  458. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  459. DevAssert(FlexCAN_IsEnhancedRxFifoAvailable(base));
  460. #endif /* (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON) */
  461. if (FALSE == state->enhancedFifoOutput.isPolling)
  462. {
  463. /* Reset to default value to avoid re-enable when calling Ip_EnableInterrupt */
  464. state->enhancedFifoOutput.isPolling = TRUE;
  465. /* Disable Enhanced RX FIFO interrupts*/
  466. FlexCAN_SetEnhancedRxFifoIntAll(base, FALSE);
  467. }
  468. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  469. else if (FLEXCAN_RXFIFO_USING_DMA == state->transferType)
  470. {
  471. Dma_Ip_LogicChannelStatusType dmaStatus;
  472. (void)Dma_Ip_GetLogicChannelStatus(state->rxFifoDMAChannel, &dmaStatus);
  473. if (DMA_IP_CH_ERROR_STATE == dmaStatus.ChStateValue)
  474. {
  475. (void)Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_CLEAR_ERROR);
  476. state->enhancedFifoOutput.state = FLEXCAN_MB_DMA_ERROR;
  477. }
  478. (void)Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  479. if (state->enhancedFifoOutput.state != FLEXCAN_MB_DMA_ERROR)
  480. {
  481. fifo_message = state->enhancedFifoOutput.pMBmessage;
  482. msgData_32 = (uint32 *)fifo_message->data;
  483. for (u32MbCnt = 0U; u32MbCnt < state->u32NumOfMbTransferByDMA; u32MbCnt++)
  484. {
  485. /* Adjust the ID if it is not extended */
  486. if (0U == ((fifo_message->cs) & FLEXCAN_IP_CS_IDE_MASK))
  487. {
  488. fifo_message->msgId = fifo_message->msgId >> FLEXCAN_IP_ID_STD_SHIFT;
  489. }
  490. /* Extract the data length */
  491. flexcan_mb_dlc_value = (uint8)((fifo_message->cs & FLEXCAN_IP_CS_DLC_MASK) >> FLEXCAN_IP_CS_DLC_SHIFT);
  492. can_dlc_payload = FlexCAN_ComputePayloadSize(flexcan_mb_dlc_value);
  493. can_real_payload = 0U;
  494. /* Extract the IDHIT and Time Stamp */
  495. if ((fifo_message->cs & FLEXCAN_IP_CS_RTR_MASK) != 0U)
  496. {
  497. can_real_payload = 0U;
  498. }
  499. else
  500. {
  501. can_real_payload = can_dlc_payload;
  502. }
  503. uint8 idhit_offset = (can_real_payload >> 2U) + (((can_real_payload % 4U) != 0U) ? 1U : 0U);
  504. #if (FLEXCAN_IP_FEATURE_HAS_HR_TIMER == STD_ON)
  505. /* Extract the Time Stamp */
  506. if (FLEXCAN_IsHRTimeStampEnabled(base))
  507. {
  508. fifo_message->time_stamp = (uint32)(msgData_32[idhit_offset + 1U]);
  509. }
  510. else
  511. #endif /* FLEXCAN_IP_FEATURE_HAS_HR_TIMER */
  512. {
  513. fifo_message->time_stamp = (uint32)((fifo_message->cs & FLEXCAN_IP_CS_TIME_STAMP_MASK) >> FLEXCAN_IP_CS_TIME_STAMP_SHIFT);
  514. }
  515. /* Extract the IDHIT */
  516. fifo_message->id_hit = (uint8)(((msgData_32[idhit_offset]) & FLEXCAN_IP_ENHANCED_IDHIT_MASK) >> FLEXCAN_IP_ENHANCED_IDHIT_SHIFT);
  517. /* Extract the dataLen */
  518. fifo_message->dataLen = can_dlc_payload;
  519. /* Reverse the endianness */
  520. for (j = 0U; j < idhit_offset; j++)
  521. {
  522. FLEXCAN_IP_SWAP_BYTES_IN_WORD(msgData_32[j], msgData_32[j]);
  523. }
  524. fifo_message++;
  525. msgData_32 = (uint32 *)fifo_message->data;
  526. }
  527. }
  528. else
  529. {
  530. /* If Enhanced Rx FIFO has Pending Request that generated error,
  531. * the EnhancedRxFIFO need to be empty to activate DMA */
  532. FlexCAN_ClearOutputEnhanceFIFO(base);
  533. }
  534. }
  535. else
  536. {
  537. /* avoid misra */
  538. }
  539. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  540. /* Clear enhanced rx fifo message*/
  541. state->enhancedFifoOutput.pMBmessage = NULL_PTR;
  542. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  543. if (state->enhancedFifoOutput.state != FLEXCAN_MB_DMA_ERROR)
  544. {
  545. state->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  546. if ((state->callback != NULL_PTR) && (FLEXCAN_RXFIFO_USING_DMA == state->transferType))
  547. {
  548. state->callback(instance, FLEXCAN_EVENT_DMA_COMPLETE, FLEXCAN_IP_MB_ENHANCED_RXFIFO, state);
  549. }
  550. }
  551. else
  552. {
  553. if (state->callback != NULL_PTR)
  554. {
  555. state->callback((uint8)instance, FLEXCAN_EVENT_DMA_ERROR, FLEXCAN_IP_MB_ENHANCED_RXFIFO, state);
  556. }
  557. }
  558. #else /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  559. state->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  560. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  561. }
  562. /*FUNCTION**********************************************************************
  563. *
  564. * Function Name : FLEXCAN_StartRxMessageEnhancedFifoData
  565. * Description : Initiate (start) a receive by beginning the process of
  566. * receiving data and enabling the interrupt.
  567. * This is not a public API as it is called from other driver functions.
  568. *
  569. *END**************************************************************************/
  570. static Flexcan_Ip_StatusType FlexCAN_StartRxMessageEnhancedFifoData(uint8 instance, Flexcan_Ip_MsgBuffType * data)
  571. {
  572. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  573. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  574. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  575. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  576. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  577. #endif /* (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON) */
  578. /* Start receiving fifo */
  579. if (FLEXCAN_MB_RX_BUSY == state->enhancedFifoOutput.state)
  580. {
  581. eResult = FLEXCAN_STATUS_BUSY;
  582. }
  583. else
  584. {
  585. state->enhancedFifoOutput.state = FLEXCAN_MB_RX_BUSY;
  586. /* This will get filled by the interrupt handler */
  587. state->enhancedFifoOutput.pMBmessage = data;
  588. if (FLEXCAN_RXFIFO_USING_INTERRUPTS == state->transferType)
  589. {
  590. state->enhancedFifoOutput.isPolling = FALSE;
  591. if (TRUE == state->isIntActive)
  592. {
  593. /* Enable All Enhanced RX FIFO interrupts*/
  594. FlexCAN_SetEnhancedRxFifoIntAll(base, TRUE);
  595. }
  596. }
  597. if (FLEXCAN_RXFIFO_USING_POLLING == state->transferType)
  598. {
  599. state->enhancedFifoOutput.isPolling = TRUE;
  600. }
  601. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  602. if (FLEXCAN_RXFIFO_USING_DMA == state->transferType)
  603. {
  604. /* reset to default to avoid enabling interrupt */
  605. state->enhancedFifoOutput.isPolling = TRUE;
  606. Dma_Ip_ReturnType edmaStatus;
  607. const Dma_Ip_LogicChannelTransferListType trasfer[FLEXCAN_IP_ENHANCE_TRASNFER_DIMENSION_LIST] =
  608. {
  609. {
  610. .Param = DMA_IP_CH_SET_SOURCE_ADDRESS,
  611. .Value = ((uint32)base + (uint32)FLEXCAN_IP_FEATURE_ENHANCED_FIFO_RAM_OFFSET),
  612. },
  613. {
  614. .Param = DMA_IP_CH_SET_SOURCE_SIGNED_OFFSET ,
  615. .Value = 4,
  616. },
  617. {
  618. .Param = DMA_IP_CH_SET_SOURCE_TRANSFER_SIZE,
  619. .Value = DMA_IP_TRANSFER_SIZE_4_BYTE,
  620. },
  621. {
  622. .Param = DMA_IP_CH_SET_DESTINATION_ADDRESS,
  623. .Value = (uint32)(state->enhancedFifoOutput.pMBmessage),
  624. },
  625. {
  626. .Param = DMA_IP_CH_SET_DESTINATION_SIGNED_OFFSET,
  627. .Value = 4,
  628. },
  629. {
  630. .Param = DMA_IP_CH_SET_DESTINATION_TRANSFER_SIZE,
  631. .Value = DMA_IP_TRANSFER_SIZE_4_BYTE,
  632. },
  633. {
  634. .Param = DMA_IP_CH_SET_MINORLOOP_SIZE,
  635. .Value = 80,
  636. },
  637. {
  638. .Param = DMA_IP_CH_SET_MAJORLOOP_COUNT,
  639. .Value = state->u32NumOfMbTransferByDMA,
  640. },
  641. {
  642. .Param = DMA_IP_CH_SET_CONTROL_EN_MAJOR_INTERRUPT,
  643. .Value = 1,
  644. },
  645. {
  646. .Param = DMA_IP_CH_SET_CONTROL_DIS_AUTO_REQUEST,
  647. .Value = 1,
  648. },
  649. {
  650. .Param = DMA_IP_CH_SET_MINORLOOP_EN_SRC_OFFSET,
  651. .Value = 1, /* enable for src address: after each minor loop, jump back to output of enhance fifo */
  652. },
  653. {
  654. .Param = DMA_IP_CH_SET_MINORLOOP_EN_DST_OFFSET,
  655. .Value = 0, /* disable for dst address: after each minor loop: standing on next element of pMBmessage array */
  656. },
  657. {
  658. .Param = DMA_IP_CH_SET_MINORLOOP_SIGNED_OFFSET,
  659. .Value = (uint32)((sint32)(-80)), /* enable for src address: after each minor loop, jump back to output of enhance fifo */
  660. }
  661. };
  662. edmaStatus = Dma_Ip_SetLogicChannelTransferList(state->rxFifoDMAChannel, &trasfer[0], FLEXCAN_IP_ENHANCE_TRASNFER_DIMENSION_LIST);
  663. if (edmaStatus != DMA_IP_STATUS_SUCCESS)
  664. {
  665. state->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  666. eResult = FLEXCAN_STATUS_ERROR;
  667. }
  668. else
  669. {
  670. edmaStatus = Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  671. if (edmaStatus != DMA_IP_STATUS_SUCCESS)
  672. {
  673. state->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  674. eResult = FLEXCAN_STATUS_ERROR;
  675. }
  676. }
  677. }
  678. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  679. }
  680. return eResult;
  681. }
  682. /*FUNCTION**********************************************************************
  683. *
  684. * Function Name : FlexCAN_ProccessEnhancedRxFifo
  685. * Description : This function will process the enhanced RxFIFO in blocking mode.
  686. * This is not a public API as it is called from other driver functions.
  687. *
  688. *END**************************************************************************/
  689. static Flexcan_Ip_StatusType FlexCAN_ProccessEnhancedRxFifo(uint8 u8Instance, uint32 u32TimeoutMs)
  690. {
  691. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  692. Flexcan_Ip_StateType * pState = Flexcan_Ip_apxState[u8Instance];
  693. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  694. uint32 timeStart = 0U;
  695. uint32 timeElapsed = 0U;
  696. uint32 mS2Ticks = OsIf_MicrosToTicks((u32TimeoutMs * 1000U), FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  697. uint32 u32intType = 0U;
  698. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  699. while (FLEXCAN_MB_RX_BUSY == pState->enhancedFifoOutput.state)
  700. {
  701. if (FLEXCAN_RXFIFO_USING_POLLING == pState->transferType)
  702. {
  703. for (u32intType = FLEXCAN_IP_ENHANCED_RXFIFO_UNDERFLOW; \
  704. u32intType >= FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE; \
  705. u32intType--)
  706. {
  707. if (FlexCAN_GetEnhancedRxFIFOStatusFlag(pBase, u32intType) != 0U)
  708. {
  709. FlexCAN_IRQHandlerEnhancedRxFIFO(u8Instance, u32intType);
  710. }
  711. }
  712. }
  713. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  714. if (timeElapsed >= mS2Ticks)
  715. {
  716. eResult = FLEXCAN_STATUS_TIMEOUT;
  717. break;
  718. }
  719. }
  720. if ((FLEXCAN_STATUS_TIMEOUT == eResult) && (FLEXCAN_RXFIFO_USING_POLLING != pState->transferType))
  721. {
  722. /* Disable Enhanced RX FIFO interrupts*/
  723. FlexCAN_SetEnhancedRxFifoIntAll(pBase, FALSE);
  724. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  725. /* Check if transfer is done over DMA and stop transfer */
  726. if ((FLEXCAN_MB_RX_BUSY == pState->enhancedFifoOutput.state) && (FLEXCAN_RXFIFO_USING_DMA == pState->transferType))
  727. {
  728. (void)Dma_Ip_SetLogicChannelCommand(pState->rxFifoDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  729. /* Reset Entire Enhance FIFO if timeout occurred */
  730. if (FLEXCAN_MB_RX_BUSY == pState->enhancedFifoOutput.state)
  731. {
  732. #if ((STD_ON == FLEXCAN_IP_ENABLE_USER_MODE_SUPPORT) && (STD_OFF == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE))
  733. OsIf_Trusted_Call1param(FlexCAN_ClearOutputEnhanceFIFO, pBase);
  734. #else
  735. FlexCAN_ClearOutputEnhanceFIFO(pBase);
  736. #endif
  737. }
  738. }
  739. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  740. }
  741. switch (pState->enhancedFifoOutput.state)
  742. {
  743. case FLEXCAN_MB_RX_BUSY:
  744. pState->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  745. break;
  746. case FLEXCAN_MB_IDLE:
  747. eResult = FLEXCAN_STATUS_SUCCESS;
  748. break;
  749. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  750. case FLEXCAN_MB_DMA_ERROR:
  751. eResult = FLEXCAN_STATUS_ERROR;
  752. break;
  753. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  754. default:
  755. eResult = FLEXCAN_STATUS_ERROR;
  756. break;
  757. }
  758. return eResult;
  759. }
  760. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  761. /*FUNCTION**********************************************************************
  762. *
  763. * Function Name : FlexCAN_ProccessLegacyRxFIFO
  764. * Description : This function will process the enhanced RxFIFO in blocking mode.
  765. * This is not a public API as it is called from other driver functions.
  766. *
  767. *END**************************************************************************/
  768. static Flexcan_Ip_StatusType FlexCAN_ProccessLegacyRxFIFO(uint8 u8Instance, uint32 u32TimeoutMs)
  769. {
  770. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  771. Flexcan_Ip_StateType * pState = Flexcan_Ip_apxState[u8Instance];
  772. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  773. uint32 timeStart = 0U;
  774. uint32 timeElapsed = 0U;
  775. uint32 mS2Ticks = OsIf_MicrosToTicks((u32TimeoutMs * 1000U), FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  776. uint32 u32intType = 0U;
  777. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  778. while (FLEXCAN_MB_RX_BUSY == pState->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state)
  779. {
  780. if (FLEXCAN_RXFIFO_USING_POLLING == pState->transferType)
  781. {
  782. for (u32intType = FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW; \
  783. u32intType >= FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE; \
  784. u32intType--)
  785. {
  786. if (FlexCAN_GetBuffStatusFlag(pBase, u32intType) != 0U)
  787. {
  788. FlexCAN_IRQHandlerRxFIFO(u8Instance, u32intType);
  789. }
  790. }
  791. }
  792. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  793. if (timeElapsed >= mS2Ticks)
  794. {
  795. eResult = FLEXCAN_STATUS_TIMEOUT;
  796. break;
  797. }
  798. }
  799. if ((FLEXCAN_STATUS_TIMEOUT == eResult) && (FLEXCAN_RXFIFO_USING_POLLING != pState->transferType))
  800. {
  801. /* Disable RX FIFO interrupts*/
  802. (void)FlexCAN_SetMsgBuffIntCmd(pBase, u8Instance, FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE, FALSE, pState->isIntActive);
  803. (void)FlexCAN_SetMsgBuffIntCmd(pBase, u8Instance, FLEXCAN_IP_LEGACY_RXFIFO_WARNING, FALSE, pState->isIntActive);
  804. (void)FlexCAN_SetMsgBuffIntCmd(pBase, u8Instance, FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW, FALSE, pState->isIntActive);
  805. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  806. /* Check if transfer is done over DMA and stop transfer */
  807. if ((FLEXCAN_MB_RX_BUSY == pState->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state) && (FLEXCAN_RXFIFO_USING_DMA == pState->transferType))
  808. {
  809. /* This function always return status success */
  810. (void)Dma_Ip_SetLogicChannelCommand(pState->rxFifoDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  811. }
  812. #endif
  813. }
  814. switch (pState->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state)
  815. {
  816. case FLEXCAN_MB_RX_BUSY:
  817. pState->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  818. break;
  819. case FLEXCAN_MB_IDLE:
  820. eResult = FLEXCAN_STATUS_SUCCESS;
  821. break;
  822. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  823. case FLEXCAN_MB_DMA_ERROR:
  824. eResult = FLEXCAN_STATUS_ERROR;
  825. break;
  826. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  827. default:
  828. eResult = FLEXCAN_STATUS_ERROR;
  829. break;
  830. }
  831. return eResult;
  832. }
  833. /*FUNCTION**********************************************************************
  834. *
  835. * Function Name : FLEXCAN_DRV_StartRxMessageBufferData
  836. * Description : Initiate (start) a receive by beginning the process of
  837. * receiving data and enabling the interrupt.
  838. * This is not a public API as it is called from other driver functions.
  839. *
  840. *END**************************************************************************/
  841. static Flexcan_Ip_StatusType FlexCAN_StartRxMessageBufferData(uint8 instance,
  842. uint8 mb_idx,
  843. Flexcan_Ip_MsgBuffType * data,
  844. boolean isPolling
  845. )
  846. {
  847. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  848. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  849. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  850. #endif
  851. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  852. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  853. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  854. if (TRUE == FlexCAN_IsMbOutOfRange(base, mb_idx, state->bIsLegacyFifoEn, state->u32MaxMbNum))
  855. {
  856. result = FLEXCAN_STATUS_BUFF_OUT_OF_RANGE;
  857. }
  858. else
  859. {
  860. #endif
  861. /* Start receiving mailbox */
  862. if (state->mbs[mb_idx].state != FLEXCAN_MB_IDLE)
  863. {
  864. result = FLEXCAN_STATUS_BUSY;
  865. }
  866. else
  867. {
  868. state->mbs[mb_idx].state = FLEXCAN_MB_RX_BUSY;
  869. state->mbs[mb_idx].pMBmessage = data;
  870. state->mbs[mb_idx].isPolling = isPolling;
  871. }
  872. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  873. }
  874. #endif
  875. return result;
  876. }
  877. /*FUNCTION**********************************************************************
  878. *
  879. * Function Name : FLEXCAN_DRV_StartSendData
  880. * Description : Initiate (start) a transmit by beginning the process of
  881. * sending data.
  882. * This is not a public API as it is called from other driver functions.
  883. *
  884. *END**************************************************************************/
  885. static Flexcan_Ip_StatusType FlexCAN_StartSendData(uint8 Flexcan_Ip_u8Instance,
  886. uint8 mb_idx,
  887. const Flexcan_Ip_DataInfoType * tx_info,
  888. uint32 msg_id,
  889. const uint8 * mb_data
  890. )
  891. {
  892. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  893. Flexcan_Ip_MsbuffCodeStatusType cs;
  894. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[Flexcan_Ip_u8Instance];
  895. FLEXCAN_Type * base = Flexcan_Ip_apxBase[Flexcan_Ip_u8Instance];
  896. volatile uint32 * pMbAddr = NULL_PTR;
  897. /* aici e ilogic ca nu are rost sa faci checkul dupa ce ai alocat aiurea */
  898. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  899. DevAssert(Flexcan_Ip_u8Instance < FLEXCAN_INSTANCE_COUNT);
  900. DevAssert(tx_info != NULL_PTR);
  901. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  902. /* Check if the Payload Size is smaller than the payload configured */
  903. DevAssert(((uint8)tx_info->data_length) <= FlexCAN_GetMbPayloadSize(base, mb_idx));
  904. #else
  905. DevAssert(((uint8)tx_info->data_length) <= 8U);
  906. #endif
  907. if (TRUE == FlexCAN_IsMbOutOfRange(base, mb_idx, state->bIsLegacyFifoEn, state->u32MaxMbNum))
  908. {
  909. eResult = FLEXCAN_STATUS_BUFF_OUT_OF_RANGE;
  910. }
  911. else
  912. {
  913. #endif
  914. if (state->mbs[mb_idx].state != FLEXCAN_MB_IDLE)
  915. {
  916. eResult = FLEXCAN_STATUS_BUSY;
  917. }
  918. else
  919. {
  920. /* Clear message buffer flag */
  921. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  922. state->mbs[mb_idx].state = FLEXCAN_MB_TX_BUSY;
  923. state->mbs[mb_idx].time_stamp = 0U;
  924. state->mbs[mb_idx].isPolling = tx_info->is_polling;
  925. state->mbs[mb_idx].isRemote = tx_info->is_remote;
  926. cs.dataLen = tx_info->data_length;
  927. cs.msgIdType = tx_info->msg_id_type;
  928. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  929. cs.fd_enable = tx_info->fd_enable;
  930. cs.fd_padding = tx_info->fd_padding;
  931. cs.enable_brs = tx_info->enable_brs;
  932. #endif
  933. if (tx_info->is_remote)
  934. {
  935. cs.code = (uint32)FLEXCAN_TX_REMOTE;
  936. }
  937. else
  938. {
  939. cs.code = (uint32)FLEXCAN_TX_DATA;
  940. }
  941. pMbAddr = FlexCAN_GetMsgBuffRegion(base, mb_idx);
  942. FlexCAN_SetTxMsgBuff(pMbAddr, &cs, msg_id, mb_data, FALSE);
  943. }
  944. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  945. }
  946. #endif
  947. return eResult;
  948. }
  949. /*FUNCTION**********************************************************************
  950. *
  951. * Function Name : FLEXCAN_StartRxMessageFifoData
  952. * Description : Initiate (start) a receive by beginning the process of
  953. * receiving data and enabling the interrupt.
  954. * This is not a public API as it is called from other driver functions.
  955. *
  956. *END**************************************************************************/
  957. static Flexcan_Ip_StatusType FlexCAN_StartRxMessageFifoData(uint8 instance, Flexcan_Ip_MsgBuffType * data)
  958. {
  959. FLEXCAN_Type * base = NULL_PTR;
  960. Flexcan_Ip_StateType * state = NULL_PTR;
  961. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  962. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  963. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  964. #endif
  965. base = Flexcan_Ip_apxBase[instance];
  966. state = Flexcan_Ip_apxState[instance];
  967. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  968. /* Check if RxFIFO feature is enabled */
  969. if (FALSE == state->bIsLegacyFifoEn)
  970. {
  971. eResult = FLEXCAN_STATUS_ERROR;
  972. }
  973. else
  974. {
  975. #endif
  976. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  977. if (FLEXCAN_RXFIFO_USING_DMA == state->transferType)
  978. {
  979. if (FLEXCAN_MB_DMA_ERROR == state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state)
  980. {
  981. /* Check if FIFO has Pending Request that generated error,
  982. * the RxFIFO need to be empty to activate DMA */
  983. #if ((STD_ON == FLEXCAN_IP_ENABLE_USER_MODE_SUPPORT) && (STD_OFF == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE))
  984. OsIf_Trusted_Call1param(FlexCAN_ClearOutputLegacyFIFO, base);
  985. #else
  986. FlexCAN_ClearOutputLegacyFIFO(base);
  987. #endif
  988. /* Change status of MB to be reconfigured with DMA transfer */
  989. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  990. }
  991. }
  992. #endif /* FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  993. /* Start receiving fifo */
  994. if (state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state != FLEXCAN_MB_IDLE)
  995. {
  996. eResult = FLEXCAN_STATUS_BUSY;
  997. }
  998. else
  999. {
  1000. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_RX_BUSY;
  1001. if (FLEXCAN_RXFIFO_USING_POLLING == state->transferType)
  1002. {
  1003. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].isPolling = TRUE;
  1004. }
  1005. /* This will get filled by the interrupt handler */
  1006. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage = data;
  1007. if (FLEXCAN_RXFIFO_USING_INTERRUPTS == state->transferType)
  1008. {
  1009. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].isPolling = FALSE;
  1010. /* Enable RX FIFO interrupts*/
  1011. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, FLEXCAN_IP_LEGACY_RXFIFO_WARNING, TRUE, state->isIntActive);
  1012. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW, TRUE, state->isIntActive);
  1013. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE, TRUE, state->isIntActive);
  1014. }
  1015. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  1016. if (FLEXCAN_RXFIFO_USING_DMA == state->transferType)
  1017. {
  1018. Dma_Ip_ReturnType edmaStatus;
  1019. const Dma_Ip_LogicChannelTransferListType trasfer[FLEXCAN_IP_TRASNFER_DIMENSION_LIST] =
  1020. {
  1021. {
  1022. .Param = DMA_IP_CH_SET_SOURCE_ADDRESS,
  1023. .Value = ((uint32)base + (uint32)FLEXCAN_IP_FEATURE_RAM_OFFSET)
  1024. },
  1025. {
  1026. .Param = DMA_IP_CH_SET_SOURCE_SIGNED_OFFSET ,
  1027. .Value = 4,
  1028. },
  1029. {
  1030. .Param = DMA_IP_CH_SET_SOURCE_TRANSFER_SIZE,
  1031. .Value = DMA_IP_TRANSFER_SIZE_4_BYTE,
  1032. },
  1033. {
  1034. .Param = DMA_IP_CH_SET_DESTINATION_ADDRESS,
  1035. .Value = (uint32)(state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage)
  1036. },
  1037. {
  1038. .Param = DMA_IP_CH_SET_DESTINATION_SIGNED_OFFSET,
  1039. .Value = 4,
  1040. },
  1041. {
  1042. .Param = DMA_IP_CH_SET_DESTINATION_TRANSFER_SIZE,
  1043. .Value = DMA_IP_TRANSFER_SIZE_4_BYTE,
  1044. },
  1045. {
  1046. .Param = DMA_IP_CH_SET_MINORLOOP_SIZE,
  1047. .Value = 16,
  1048. },
  1049. {
  1050. .Param = DMA_IP_CH_SET_MAJORLOOP_COUNT,
  1051. .Value = 1,
  1052. },
  1053. {
  1054. .Param = DMA_IP_CH_SET_CONTROL_EN_MAJOR_INTERRUPT,
  1055. .Value = 1,
  1056. },
  1057. {
  1058. .Param = DMA_IP_CH_SET_CONTROL_DIS_AUTO_REQUEST,
  1059. .Value = 1,
  1060. },
  1061. };
  1062. edmaStatus = Dma_Ip_SetLogicChannelTransferList(state->rxFifoDMAChannel, &trasfer[0], FLEXCAN_IP_TRASNFER_DIMENSION_LIST);
  1063. if (edmaStatus != DMA_IP_STATUS_SUCCESS)
  1064. {
  1065. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  1066. eResult = FLEXCAN_STATUS_ERROR;
  1067. }
  1068. else
  1069. {
  1070. edmaStatus = Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_SET_HARDWARE_REQUEST);
  1071. if (edmaStatus != DMA_IP_STATUS_SUCCESS)
  1072. {
  1073. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  1074. eResult = FLEXCAN_STATUS_ERROR;
  1075. }
  1076. }
  1077. }
  1078. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  1079. }
  1080. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1081. }
  1082. #endif
  1083. return eResult;
  1084. }
  1085. /*FUNCTION**********************************************************************
  1086. *
  1087. * Function Name : FlexCAN_IRQHandlerRxMB
  1088. * Description : Process IRQHandler in case of Rx MessageBuffer selection
  1089. * for CAN interface.
  1090. *
  1091. * This is not a public API as it is called whenever an interrupt and receive
  1092. * individual MB occurs
  1093. *END**************************************************************************/
  1094. static void FlexCAN_IRQHandlerRxMB(uint8 instance, uint32 mb_idx)
  1095. {
  1096. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1097. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1098. Flexcan_Ip_MsgBuffType data;
  1099. boolean bCurrentIntStat = FALSE;
  1100. /* If use pass NULL_PTR, they can get data in callback function by getting state->mbs[mb_idx].pMBmessage */
  1101. if (NULL_PTR == state->mbs[mb_idx].pMBmessage)
  1102. {
  1103. state->mbs[mb_idx].pMBmessage = &data;
  1104. }
  1105. #if (defined (ERR_IPV_FLEXCAN_E050246) || defined (ERR_IPV_FLEXCAN_E050630))
  1106. boolean bIsCriticalSectionNeeded = FALSE;
  1107. /* Expectation: the sequence will not be interrupted when it already in interupt context */
  1108. if (TRUE == state->mbs[mb_idx].isPolling)
  1109. {
  1110. #if (defined (ERR_IPV_FLEXCAN_E050246) && defined (ERR_IPV_FLEXCAN_E050630))
  1111. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1112. if ((state->bIsLegacyFifoEn) || (state->bIsEnhancedFifoEn && (0U != (base->CTRL2 & FLEXCAN_CTRL2_TSTAMPCAP_MASK))))
  1113. #else
  1114. if (state->bIsLegacyFifoEn)
  1115. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  1116. #elif (defined (ERR_IPV_FLEXCAN_E050630))
  1117. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1118. if ((state->bIsLegacyFifoEn || state->bIsEnhancedFifoEn) && (0U != (base->CTRL2 & FLEXCAN_CTRL2_TSTAMPCAP_MASK)))
  1119. #else
  1120. if ((state->bIsLegacyFifoEn) && (0U != (base->CTRL2 & FLEXCAN_CTRL2_TSTAMPCAP_MASK)))
  1121. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  1122. #elif defined (ERR_IPV_FLEXCAN_E050246)
  1123. if (state->bIsLegacyFifoEn)
  1124. #endif
  1125. {
  1126. bIsCriticalSectionNeeded = TRUE;
  1127. /* Disable all IRQs */
  1128. OsIf_SuspendAllInterrupts();
  1129. }
  1130. }
  1131. #endif /* (defined(ERR_IPV_FLEXCAN_E050246) || defined(ERR_IPV_FLEXCAN_E050630)) */
  1132. /* Lock RX message buffer and RX FIFO*/
  1133. FlexCAN_LockRxMsgBuff(base, mb_idx);
  1134. /* Get RX MB field values*/
  1135. FlexCAN_GetMsgBuff(base, mb_idx, state->mbs[mb_idx].pMBmessage);
  1136. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  1137. #if defined (ERR_IPV_FLEXCAN_E050246)
  1138. /* the CODE field is updated with an incorrect value when MBx is locked by software for more than 20 CAN bit times and FIFO enable */
  1139. if ((state->bIsLegacyFifoEn) && ((uint32)FLEXCAN_RX_INACTIVE == ((state->mbs[mb_idx].pMBmessage->cs & FLEXCAN_IP_CS_CODE_MASK) >> FLEXCAN_IP_CS_CODE_SHIFT)))
  1140. {
  1141. /* Update the cs code for next sequence move in MB.
  1142. A CPU write into the C/S word also unlocks the MB */
  1143. volatile uint32 *flexcan_mb = FlexCAN_GetMsgBuffRegion(base, mb_idx);
  1144. *flexcan_mb &= ~FLEXCAN_IP_CS_CODE_MASK;
  1145. *flexcan_mb |= (((uint32)FLEXCAN_RX_EMPTY) << FLEXCAN_IP_CS_CODE_SHIFT) & FLEXCAN_IP_CS_CODE_MASK;
  1146. }
  1147. else
  1148. #endif
  1149. {
  1150. /* Unlock RX message buffer and RX FIFO*/
  1151. FlexCAN_UnlockRxMsgBuff(base);
  1152. }
  1153. #if (defined (ERR_IPV_FLEXCAN_E050246) || defined (ERR_IPV_FLEXCAN_E050630))
  1154. /* To ensure that interrupts are resumed when they are suspended */
  1155. if (TRUE == bIsCriticalSectionNeeded)
  1156. {
  1157. /* Enable all IRQs */
  1158. OsIf_ResumeAllInterrupts();
  1159. }
  1160. #endif /* (defined(ERR_IPV_FLEXCAN_E050246) || defined(ERR_IPV_FLEXCAN_E050630)) */
  1161. state->mbs[mb_idx].state = FLEXCAN_MB_IDLE;
  1162. bCurrentIntStat = state->mbs[mb_idx].isPolling;
  1163. /* Invoke callback */
  1164. if (state->callback != NULL_PTR)
  1165. {
  1166. state->callback(instance, FLEXCAN_EVENT_RX_COMPLETE, mb_idx, state);
  1167. }
  1168. if ((FLEXCAN_MB_IDLE == state->mbs[mb_idx].state) && (FALSE == state->mbs[mb_idx].isPolling))
  1169. {
  1170. /* callback is not called, need to reset to default value */
  1171. state->mbs[mb_idx].isPolling = TRUE;
  1172. /* Disable the transmitter data register empty interrupt for case: mb is interrupt (it was not use in above callback with the same index) */
  1173. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, mb_idx, FALSE, state->isIntActive);
  1174. }
  1175. else if ((FALSE == bCurrentIntStat) && (TRUE == state->mbs[mb_idx].isPolling))
  1176. {
  1177. /* Disable the transmitter data register empty interrupt for case: switch from interrupt to polling for the same MB (called in above callback with same mb index) */
  1178. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, mb_idx, FALSE, state->isIntActive);
  1179. }
  1180. else
  1181. {
  1182. /* Prevent misra */
  1183. /* When processing type change from POLL->POLL or INTERRUPT -> INTERRUPT(this Mb is used continously in callback), no need to disable interrupt in the ISR */
  1184. }
  1185. }
  1186. /*FUNCTION**********************************************************************
  1187. *
  1188. * Function Name : FlexCAN_IRQHandlerTxMB
  1189. * Description : Process IRQHandler in case of Tx MessageBuffer selection
  1190. * for CAN interface.
  1191. * note: just using in interrupt mode
  1192. * This is not a public API as it is called whenever an interrupt and receive
  1193. * individual MB occurs
  1194. *END**************************************************************************/
  1195. static void FlexCAN_IRQHandlerTxMB(uint8 u8Instance, uint32 u32MbIdx)
  1196. {
  1197. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  1198. Flexcan_Ip_StateType * pState = Flexcan_Ip_apxState[u8Instance];
  1199. Flexcan_Ip_MsgBuffType mb;
  1200. boolean bCurrentIntStat = FALSE;
  1201. if (pState->mbs[u32MbIdx].isRemote)
  1202. {
  1203. FlexCAN_LockRxMsgBuff(pBase, u32MbIdx);
  1204. FlexCAN_GetMsgBuff(pBase, u32MbIdx, &mb);
  1205. FlexCAN_UnlockRxMsgBuff(pBase);
  1206. pState->mbs[u32MbIdx].time_stamp = mb.time_stamp;
  1207. /* If the frame was a remote frame, clear the flag only if the response was
  1208. * not received yet. If the response was received, leave the flag set in order
  1209. * to be handled when the user calls FLEXCAN_DRV_RxMessageBuffer. */
  1210. if ((uint32)FLEXCAN_RX_EMPTY == ((mb.cs & FLEXCAN_IP_CS_CODE_MASK) >> FLEXCAN_IP_CS_CODE_SHIFT))
  1211. {
  1212. FlexCAN_ClearMsgBuffIntStatusFlag(pBase, u32MbIdx);
  1213. }
  1214. }
  1215. else
  1216. {
  1217. pState->mbs[u32MbIdx].time_stamp = FlexCAN_GetMsgBuffTimestamp(pBase, u32MbIdx);
  1218. FlexCAN_UnlockRxMsgBuff(pBase);
  1219. FlexCAN_ClearMsgBuffIntStatusFlag(pBase, u32MbIdx);
  1220. }
  1221. pState->mbs[u32MbIdx].state = FLEXCAN_MB_IDLE;
  1222. bCurrentIntStat = pState->mbs[u32MbIdx].isPolling;
  1223. /* Invoke callback */
  1224. if (pState->callback != NULL_PTR)
  1225. {
  1226. pState->callback(u8Instance, FLEXCAN_EVENT_TX_COMPLETE, u32MbIdx, pState);
  1227. }
  1228. if ((FLEXCAN_MB_IDLE == pState->mbs[u32MbIdx].state) && (FALSE == pState->mbs[u32MbIdx].isPolling))
  1229. {
  1230. /* callback is not called, need to reset to default value */
  1231. pState->mbs[u32MbIdx].isPolling = TRUE;
  1232. /* Disable the transmitter data register empty interrupt for case: mb is interrupt (it was not use in above callback with the same index) */
  1233. (void)FlexCAN_SetMsgBuffIntCmd(pBase, u8Instance, u32MbIdx, FALSE, pState->isIntActive);
  1234. }
  1235. else if ((FALSE == bCurrentIntStat) && (TRUE == pState->mbs[u32MbIdx].isPolling))
  1236. {
  1237. /* Disable the transmitter data register empty interrupt for case: switch from interrupt to polling for the same MB (called in above callback with same mb index) */
  1238. (void)FlexCAN_SetMsgBuffIntCmd(pBase, u8Instance, u32MbIdx, FALSE, pState->isIntActive);
  1239. }
  1240. else
  1241. {
  1242. /* Prevent misra */
  1243. /* When processing type change from POLL->POLL or INTERRUPT -> INTERRUPT(this Mb is used continously in callback), no need to disable interrupt in the ISR */
  1244. }
  1245. }
  1246. /*FUNCTION**********************************************************************
  1247. *
  1248. * Function Name : FlexCAN_IRQHandlerRxFIFO
  1249. * Description : Process IRQHandler in case of RxFIFO mode selection for CAN interface.
  1250. *
  1251. *END**************************************************************************/
  1252. static inline void FlexCAN_IRQHandlerRxFIFO(uint8 instance, uint32 mb_idx)
  1253. {
  1254. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1255. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1256. Flexcan_Ip_MsgBuffType data;
  1257. /* If use pass NULL_PTR, they can get data in callback function by getting state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage */
  1258. if (NULL_PTR == state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage)
  1259. {
  1260. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage = &data;
  1261. }
  1262. switch (mb_idx)
  1263. {
  1264. case FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE:
  1265. if (FLEXCAN_MB_RX_BUSY == state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state)
  1266. {
  1267. /* Get RX FIFO field values */
  1268. FlexCAN_ReadRxFifo(base, state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage);
  1269. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  1270. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  1271. /* Invoke callback */
  1272. if (state->callback != NULL_PTR)
  1273. {
  1274. state->callback(instance, FLEXCAN_EVENT_RXFIFO_COMPLETE, FLEXCAN_IP_MB_HANDLE_RXFIFO, state);
  1275. }
  1276. if (FLEXCAN_MB_IDLE == state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state)
  1277. {
  1278. /* reset to default value */
  1279. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].isPolling = TRUE;
  1280. /* Complete receive data */
  1281. FlexCAN_CompleteRxMessageFifoData(instance);
  1282. }
  1283. }
  1284. break;
  1285. case FLEXCAN_IP_LEGACY_RXFIFO_WARNING:
  1286. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  1287. /* Invoke callback */
  1288. if (state->callback != NULL_PTR)
  1289. {
  1290. state->callback(instance, FLEXCAN_EVENT_RXFIFO_WARNING, FLEXCAN_IP_MB_HANDLE_RXFIFO, state);
  1291. }
  1292. break;
  1293. case FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW:
  1294. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  1295. /* Invoke callback */
  1296. if (state->callback != NULL_PTR)
  1297. {
  1298. state->callback(instance, FLEXCAN_EVENT_RXFIFO_OVERFLOW, FLEXCAN_IP_MB_HANDLE_RXFIFO, state);
  1299. }
  1300. break;
  1301. default:
  1302. /* Do Nothing */
  1303. break;
  1304. }
  1305. }
  1306. /*FUNCTION**********************************************************************
  1307. *
  1308. * Function Name : FlexCAN_ProcessSpuriousInterruptMB
  1309. * Description : clear Iflag when spurious interrupt occurred.
  1310. * note: just use in FlexCAN_IRQHandler
  1311. *END**************************************************************************/
  1312. static inline void FlexCAN_ProcessSpuriousInterruptMB(uint8 instance, uint32 startMbIdx, uint32 endMbIdx)
  1313. {
  1314. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1315. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1316. uint32 mb_idx = 0U;
  1317. uint32 u32MbHandle = 0U;
  1318. /* Process spurious interrupt */
  1319. for (mb_idx = startMbIdx; mb_idx <= endMbIdx; mb_idx++)
  1320. {
  1321. if (((uint8)0U != FlexCAN_GetBuffStatusFlag(base, mb_idx)) && ((uint8)0U == FlexCAN_GetBuffStatusImask(base, mb_idx)))
  1322. {
  1323. u32MbHandle = mb_idx;
  1324. if ((TRUE == state->bIsLegacyFifoEn) && (mb_idx <= FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW))
  1325. {
  1326. u32MbHandle = (uint32)FLEXCAN_IP_MB_HANDLE_RXFIFO;
  1327. }
  1328. if ((FALSE == state->mbs[u32MbHandle].isPolling))
  1329. {
  1330. /* clear the MB flag */
  1331. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  1332. if (FLEXCAN_MB_TX_BUSY == state->mbs[u32MbHandle].state)
  1333. {
  1334. /* reset to default state */
  1335. state->mbs[u32MbHandle].state = FLEXCAN_MB_IDLE;
  1336. state->mbs[u32MbHandle].isPolling = TRUE;
  1337. }
  1338. }
  1339. }
  1340. }
  1341. }
  1342. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1343. void FlexCAN_EnhancedRxFIFODataIRQHandler(uint8 u8Instance)
  1344. {
  1345. FLEXCAN_Type * base = Flexcan_Ip_apxBase[u8Instance];
  1346. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  1347. uint32 u32intType = 0U;
  1348. boolean bIsSpuriousInt = TRUE;
  1349. /* Get the interrupts that are enabled and ready */
  1350. for (u32intType = FLEXCAN_IP_ENHANCED_RXFIFO_WATERMARK; u32intType >= FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE; u32intType--)
  1351. {
  1352. if ((uint8)0U != FlexCAN_GetEnhancedRxFIFOStatusFlag(base, u32intType))
  1353. {
  1354. if ((uint8)0U != FlexCAN_GetEnhancedRxFIFOIntStatusFlag(base, u32intType))
  1355. {
  1356. FlexCAN_IRQHandlerEnhancedRxFIFO(u8Instance, u32intType);
  1357. bIsSpuriousInt = FALSE;
  1358. }
  1359. /* just process spurious interrupt when no real interrupt at all */
  1360. else if ((TRUE == bIsSpuriousInt) && (FALSE == state->enhancedFifoOutput.isPolling))
  1361. {
  1362. /* Process spurious interrupt */
  1363. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, u32intType);
  1364. }
  1365. else
  1366. {
  1367. /* prevent misra */
  1368. }
  1369. }
  1370. }
  1371. }
  1372. /*FUNCTION**********************************************************************
  1373. *
  1374. * Function Name : FlexCAN_ProcessIRQHandlerEnhancedRxFIFO
  1375. * Description : Process IRQHandler in case of Enhanced RxFIFO mode selection for CAN interface.
  1376. * note: just use in FlexCAN_IRQHandler
  1377. *END**************************************************************************/
  1378. static inline boolean FlexCAN_ProcessIRQHandlerEnhancedRxFIFO(uint8 u8Instance, boolean bIsSpuriousIntPrevious)
  1379. {
  1380. FLEXCAN_Type * base = Flexcan_Ip_apxBase[u8Instance];
  1381. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  1382. uint32 u32intType = 0U;
  1383. boolean bIsSpuriousInt = bIsSpuriousIntPrevious;
  1384. /* Get the interrupts that are enabled and ready */
  1385. for (u32intType = FLEXCAN_IP_ENHANCED_RXFIFO_UNDERFLOW; u32intType >= FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE; u32intType--)
  1386. {
  1387. if ((uint8)0U != FlexCAN_GetEnhancedRxFIFOStatusFlag(base, u32intType))
  1388. {
  1389. if ((uint8)0U != FlexCAN_GetEnhancedRxFIFOIntStatusFlag(base, u32intType))
  1390. {
  1391. FlexCAN_IRQHandlerEnhancedRxFIFO(u8Instance, u32intType);
  1392. bIsSpuriousInt = FALSE;
  1393. }
  1394. /* just process spurious interrupt when no real interrupt at all */
  1395. else if ((TRUE == bIsSpuriousInt) && (FALSE == state->enhancedFifoOutput.isPolling))
  1396. {
  1397. /* Process spurious interrupt */
  1398. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, u32intType);
  1399. }
  1400. else
  1401. {
  1402. /* prevent misra */
  1403. }
  1404. }
  1405. }
  1406. return bIsSpuriousInt;
  1407. }
  1408. /*FUNCTION**********************************************************************
  1409. *
  1410. * Function Name : FLEXCAN_IRQHandlerEnhancedRxFIFO
  1411. * Description : Process IRQHandler in case of Enhanced RxFIFO mode selection for CAN interface.
  1412. *
  1413. * Implements : FLEXCAN_IRQHandlerEnyhancedRxFIFO_Activity
  1414. *END**************************************************************************/
  1415. static void FlexCAN_IRQHandlerEnhancedRxFIFO(uint8 instance, uint32 intType)
  1416. {
  1417. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1418. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1419. Flexcan_Ip_MsgBuffType data;
  1420. switch (intType)
  1421. {
  1422. case FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE:
  1423. if (FLEXCAN_MB_RX_BUSY == state->enhancedFifoOutput.state)
  1424. {
  1425. /* If use pass NULL_PTR, they can get data in callback function by getting state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage */
  1426. if (NULL_PTR == state->enhancedFifoOutput.pMBmessage)
  1427. {
  1428. state->enhancedFifoOutput.pMBmessage = &data;
  1429. }
  1430. /* Get Enhanced RX FIFO field values */
  1431. FlexCAN_ReadEnhancedRxFifo(base, state->enhancedFifoOutput.pMBmessage);
  1432. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, intType);
  1433. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_WATERMARK);
  1434. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_OVERFLOW);
  1435. state->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  1436. /* Invoke callback */
  1437. if (state->callback != NULL_PTR)
  1438. {
  1439. state->callback(instance, FLEXCAN_EVENT_ENHANCED_RXFIFO_COMPLETE, FLEXCAN_IP_MB_ENHANCED_RXFIFO, state);
  1440. }
  1441. if (FLEXCAN_MB_IDLE == state->enhancedFifoOutput.state)
  1442. {
  1443. /* Complete receive data */
  1444. FlexCAN_CompleteRxMessageEnhancedFifoData(instance);
  1445. }
  1446. }
  1447. break;
  1448. case FLEXCAN_IP_ENHANCED_RXFIFO_WATERMARK:
  1449. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, intType);
  1450. /* Invoke callback */
  1451. if (state->callback != NULL_PTR)
  1452. {
  1453. state->callback(instance, FLEXCAN_EVENT_ENHANCED_RXFIFO_WATERMARK, FLEXCAN_IP_MB_ENHANCED_RXFIFO, state);
  1454. }
  1455. break;
  1456. case FLEXCAN_IP_ENHANCED_RXFIFO_OVERFLOW:
  1457. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, intType);
  1458. /* Invoke callback */
  1459. if (state->callback != NULL_PTR)
  1460. {
  1461. state->callback(instance, FLEXCAN_EVENT_ENHANCED_RXFIFO_OVERFLOW, FLEXCAN_IP_MB_ENHANCED_RXFIFO, state);
  1462. }
  1463. break;
  1464. case FLEXCAN_IP_ENHANCED_RXFIFO_UNDERFLOW:
  1465. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, intType);
  1466. /* Invoke callback */
  1467. if (state->callback != NULL_PTR)
  1468. {
  1469. state->callback(instance, FLEXCAN_EVENT_ENHANCED_RXFIFO_UNDERFLOW, FLEXCAN_IP_MB_ENHANCED_RXFIFO, state);
  1470. }
  1471. break;
  1472. default:
  1473. /* Do Nothing */
  1474. break;
  1475. }
  1476. }
  1477. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  1478. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  1479. static void DMA_Can_Callback(uint8 instance)
  1480. {
  1481. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1482. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1483. if (TRUE == FlexCAN_IsEnhancedRxFifoAvailable(base))
  1484. {
  1485. if (TRUE == FlexCAN_IsEnhancedRxFifoEnabled(base))
  1486. {
  1487. /* Need to add handler for Enhaced RxFifoDataDMA */
  1488. FlexCAN_CompleteRxMessageEnhancedFifoData(instance);
  1489. }
  1490. else
  1491. {
  1492. FlexCAN_CompleteRxMessageFifoData(instance);
  1493. }
  1494. }
  1495. else
  1496. #endif
  1497. {
  1498. FlexCAN_CompleteRxMessageFifoData(instance);
  1499. }
  1500. }
  1501. #endif
  1502. #if (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE)
  1503. /*FUNCTION**********************************************************************
  1504. *
  1505. * Function Name : FlexCAN_SetUserAccessAllowed
  1506. * Description : Sets the UAA bit in REG_PROT to make the instance accessible
  1507. * in user mode.
  1508. *
  1509. * This is not a public API as it is called from other driver functions.
  1510. *END**************************************************************************/
  1511. static void FlexCAN_SetUserAccessAllowed(const FLEXCAN_Type * pBase)
  1512. {
  1513. SET_USER_ACCESS_ALLOWED((uint32)pBase, FLEXCAN_PROT_MEM_U32);
  1514. }
  1515. /*FUNCTION**********************************************************************
  1516. *
  1517. * Function Name : FlexCAN_ClrUserAccessAllowed
  1518. * Description : Clears the UAA bit in REG_PROT to make the instance accessible
  1519. * in user mode.
  1520. *
  1521. * This is not a public API as it is called from other driver functions.
  1522. *END**************************************************************************/
  1523. static void FlexCAN_ClrUserAccessAllowed(const FLEXCAN_Type * pBase)
  1524. {
  1525. CLR_USER_ACCESS_ALLOWED((uint32)pBase, FLEXCAN_PROT_MEM_U32);
  1526. }
  1527. #endif /* (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE) */
  1528. /*FUNCTION**********************************************************************
  1529. *
  1530. * Function Name : FlexCAN_AbortTxTransfer
  1531. * Description : Abort transfer for Tx buffer.
  1532. *
  1533. * This is not a public API as it is called from other driver functions.
  1534. *END**************************************************************************/
  1535. static Flexcan_Ip_StatusType FlexCAN_AbortTxTransfer(uint8 u8Instance, uint8 mb_idx)
  1536. {
  1537. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  1538. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  1539. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  1540. uint32 timeStart = 0U;
  1541. uint32 timeElapsed = 0U;
  1542. uint32 flexcan_mb_config = 0;
  1543. uint32 uS2Ticks = 0U;
  1544. volatile uint32 * flexcan_mb = NULL_PTR;
  1545. flexcan_mb = FlexCAN_GetMsgBuffRegion(pBase, mb_idx);
  1546. flexcan_mb_config = * flexcan_mb;
  1547. /* Reset the code */
  1548. flexcan_mb_config &= (~FLEXCAN_IP_CS_CODE_MASK);
  1549. flexcan_mb_config |= (uint32)(((uint32)FLEXCAN_TX_ABORT & (uint32)0x1F) << (uint8)FLEXCAN_IP_CS_CODE_SHIFT) & (uint32)FLEXCAN_IP_CS_CODE_MASK;
  1550. *flexcan_mb = flexcan_mb_config;
  1551. /* Wait to finish abort operation */
  1552. uS2Ticks = OsIf_MicrosToTicks(FLEXCAN_IP_TIMEOUT_DURATION, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  1553. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  1554. while (0U == FlexCAN_GetBuffStatusFlag(pBase, mb_idx))
  1555. {
  1556. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  1557. if (timeElapsed >= uS2Ticks)
  1558. {
  1559. result = FLEXCAN_STATUS_TIMEOUT;
  1560. break;
  1561. }
  1562. }
  1563. if (result != FLEXCAN_STATUS_TIMEOUT)
  1564. {
  1565. flexcan_mb_config = *flexcan_mb;
  1566. /* Check if the MBs have been safely Inactivated */
  1567. if ((uint32)FLEXCAN_TX_INACTIVE == ((flexcan_mb_config & FLEXCAN_IP_CS_CODE_MASK) >> FLEXCAN_IP_CS_CODE_SHIFT))
  1568. {
  1569. /* Transmission have occurred */
  1570. result = FLEXCAN_STATUS_NO_TRANSFER_IN_PROGRESS;
  1571. }
  1572. if ((uint32)FLEXCAN_TX_ABORT == ((flexcan_mb_config & FLEXCAN_IP_CS_CODE_MASK) >> FLEXCAN_IP_CS_CODE_SHIFT))
  1573. {
  1574. /* Transmission have been aborted */
  1575. result = FLEXCAN_STATUS_SUCCESS;
  1576. }
  1577. }
  1578. /* Clear message buffer flag */
  1579. FlexCAN_ClearMsgBuffIntStatusFlag(pBase, mb_idx);
  1580. state->mbs[mb_idx].state = FLEXCAN_MB_IDLE;
  1581. return result;
  1582. }
  1583. /*FUNCTION**********************************************************************
  1584. *
  1585. * Function Name : FlexCAN_AbortRxTransfer
  1586. * Description : Abort transfer for Rx normal or legacy fifo if enabled.
  1587. *
  1588. * This is not a public API as it is called from other driver functions.
  1589. *END**************************************************************************/
  1590. static void FlexCAN_AbortRxTransfer(uint8 u8Instance, uint8 mb_idx)
  1591. {
  1592. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  1593. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  1594. uint32 val1 = 0U;
  1595. uint32 val2 = 0U;
  1596. uint32 flexcan_mb_config = 0;
  1597. volatile uint32 * flexcan_mb = NULL_PTR;
  1598. state->mbs[mb_idx].state = FLEXCAN_MB_IDLE;
  1599. /* Check if fifo enabled */
  1600. if (TRUE == state->bIsLegacyFifoEn)
  1601. {
  1602. /* Get the number of RX FIFO Filters*/
  1603. val1 = (((pBase->CTRL2) & FLEXCAN_CTRL2_RFFN_MASK) >> FLEXCAN_CTRL2_RFFN_SHIFT);
  1604. /* Get the number if MBs occupied by RX FIFO and ID filter table*/
  1605. /* the Rx FIFO occupies the memory space originally reserved for MB0-5*/
  1606. /* Every number of RFFN means 8 number of RX FIFO filters*/
  1607. /* and every 4 number of RX FIFO filters occupied one MB*/
  1608. val2 = RxFifoOcuppiedLastMsgBuff(val1);
  1609. if (mb_idx > val2)
  1610. {
  1611. /* This operation is not allowed for MB that are part of RxFIFO */
  1612. flexcan_mb = FlexCAN_GetMsgBuffRegion(pBase, mb_idx);
  1613. flexcan_mb_config = * flexcan_mb;
  1614. /* Reset the code and unlock the MB */
  1615. flexcan_mb_config &= (uint32)(~FLEXCAN_IP_CS_CODE_MASK);
  1616. flexcan_mb_config |= (uint32)(((uint32)FLEXCAN_RX_INACTIVE & (uint32)0x1F) << (uint8)FLEXCAN_IP_CS_CODE_SHIFT) & (uint32)FLEXCAN_IP_CS_CODE_MASK;
  1617. *flexcan_mb = flexcan_mb_config;
  1618. /* Reconfigure The MB as left by RxMBconfig */
  1619. flexcan_mb_config &= (~FLEXCAN_IP_CS_CODE_MASK);
  1620. flexcan_mb_config |= (uint32)(((uint32)FLEXCAN_RX_EMPTY & (uint32)0x1F) << (uint8)FLEXCAN_IP_CS_CODE_SHIFT) & (uint32)FLEXCAN_IP_CS_CODE_MASK;
  1621. *flexcan_mb = flexcan_mb_config;
  1622. }
  1623. if (FLEXCAN_IP_MB_HANDLE_RXFIFO == mb_idx)
  1624. {
  1625. FLEXCAN_ClearMsgBuffIntCmd(pBase, u8Instance, FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE, state->isIntActive);
  1626. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  1627. if (FLEXCAN_RXFIFO_USING_DMA == state->transferType)
  1628. {
  1629. (void)Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  1630. }
  1631. #endif
  1632. }
  1633. }
  1634. else
  1635. {
  1636. /* This operation is not allowed for MB that are part of RxFIFO */
  1637. flexcan_mb = FlexCAN_GetMsgBuffRegion(pBase, mb_idx);
  1638. flexcan_mb_config = * flexcan_mb;
  1639. /* Reset the code and unlock the MB */
  1640. flexcan_mb_config &= (~FLEXCAN_IP_CS_CODE_MASK);
  1641. flexcan_mb_config |= (uint32)(((uint32)FLEXCAN_RX_INACTIVE & (uint32)0x1F) << (uint8)FLEXCAN_IP_CS_CODE_SHIFT) & (uint32)FLEXCAN_IP_CS_CODE_MASK;
  1642. *flexcan_mb = flexcan_mb_config;
  1643. /* Reconfigure The MB as left by RxMBconfig */
  1644. flexcan_mb_config &= (~FLEXCAN_IP_CS_CODE_MASK);
  1645. flexcan_mb_config |= (uint32)(((uint32)FLEXCAN_RX_EMPTY & (uint32)0x1F) << (uint8)FLEXCAN_IP_CS_CODE_SHIFT) & (uint32)FLEXCAN_IP_CS_CODE_MASK;
  1646. *flexcan_mb = flexcan_mb_config;
  1647. }
  1648. /* Clear message buffer flag */
  1649. FlexCAN_ClearMsgBuffIntStatusFlag(pBase, mb_idx);
  1650. }
  1651. /*==================================================================================================
  1652. * GLOBAL FUNCTIONS
  1653. ==================================================================================================*/
  1654. /* implements FlexCAN_Ip_Init_Activity */
  1655. Flexcan_Ip_StatusType FlexCAN_Ip_Init_Privileged(uint8 Flexcan_Ip_u8Instance,
  1656. Flexcan_Ip_StateType * Flexcan_Ip_pState,
  1657. const Flexcan_Ip_ConfigType * Flexcan_Ip_pData
  1658. )
  1659. {
  1660. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  1661. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[Flexcan_Ip_u8Instance];
  1662. uint32 i;
  1663. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1664. DevAssert(Flexcan_Ip_u8Instance < FLEXCAN_INSTANCE_COUNT);
  1665. DevAssert(Flexcan_Ip_pData != NULL_PTR);
  1666. #endif
  1667. #if defined(CAN_FEATURE_S32K1XX)
  1668. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1669. /* Check if the instance support FD capability */
  1670. if (TRUE == Flexcan_Ip_pData->fd_enable)
  1671. {
  1672. DevAssert(FlexCAN_IsFDAvailable(pBase) == Flexcan_Ip_pData->fd_enable);
  1673. }
  1674. #endif
  1675. #endif
  1676. #if (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE)
  1677. /* Set the UAA bit in REG_PROT to make the instance accessible in user mode */
  1678. if (Flexcan_Ip_u8Instance < FLEXCAN_IP_CTRL_REG_PROT_SUPPORT_U8)
  1679. {
  1680. OsIf_Trusted_Call1param(FlexCAN_SetUserAccessAllowed, pBase);
  1681. }
  1682. #endif
  1683. eResult = FlexCAN_InitController(pBase, Flexcan_Ip_pData);
  1684. if (FLEXCAN_STATUS_SUCCESS == eResult)
  1685. {
  1686. /* Enable the use of extended bit time definitions */
  1687. FlexCAN_EnableExtCbt(pBase, Flexcan_Ip_pData->fd_enable);
  1688. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  1689. if (Flexcan_Ip_pData->enhCbtEnable)
  1690. {
  1691. /* Enable Enhanced CBT time segments */
  1692. pBase->CTRL2 |= FLEXCAN_CTRL2_BTE_MASK;
  1693. FlexCAN_SetEnhancedNominalTimeSegments(pBase, &Flexcan_Ip_pData->bitrate);
  1694. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  1695. if (Flexcan_Ip_pData->fd_enable)
  1696. {
  1697. FlexCAN_SetEnhancedDataTimeSegments(pBase, &Flexcan_Ip_pData->bitrate_cbt);
  1698. }
  1699. #endif
  1700. }
  1701. else
  1702. #endif /* End of (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON) */
  1703. {
  1704. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  1705. /* Disable Enhanced CBT time segments */
  1706. pBase->CTRL2 &= ~FLEXCAN_CTRL2_BTE_MASK;
  1707. #endif
  1708. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  1709. /* Set bit rate. */
  1710. if (Flexcan_Ip_pData->fd_enable)
  1711. {
  1712. /* Write Normal bit time configuration to CBT register */
  1713. FlexCAN_SetExtendedTimeSegments(pBase, &Flexcan_Ip_pData->bitrate);
  1714. /* Write Data bit time configuration to FDCBT register */
  1715. FlexCAN_SetFDTimeSegments(pBase, &Flexcan_Ip_pData->bitrate_cbt);
  1716. }
  1717. else
  1718. {
  1719. /* Write Normal bit time configuration to CTRL1 register */
  1720. FlexCAN_SetTimeSegments(pBase, &Flexcan_Ip_pData->bitrate);
  1721. }
  1722. #endif
  1723. }
  1724. /* Select mode */
  1725. FlexCAN_SetOperationMode(pBase, Flexcan_Ip_pData->flexcanMode);
  1726. #if (STD_ON == FLEXCAN_IP_ENABLE_USER_MODE_SUPPORT)
  1727. #if (STD_ON == FLEXCAN_IP_FEATURE_HAS_SUPV)
  1728. pBase->MCR = (pBase->MCR & ~FLEXCAN_MCR_SUPV_MASK) | FLEXCAN_MCR_SUPV(0U);
  1729. #endif
  1730. #endif
  1731. #if (FLEXCAN_IP_FEATURE_HAS_TS_ENABLE == STD_ON)
  1732. FlexCAN_ConfigTimestamp(pBase, (const Flexcan_Ip_TimeStampConfigType *)(&Flexcan_Ip_pData->time_stamp));
  1733. #endif /* (FLEXCAN_IP_FEATURE_HAS_TS_ENABLE == STD_ON) */
  1734. for (i = 0; i < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM; i++)
  1735. {
  1736. /* Check if blocking need to be any more present in sync\async discussions */
  1737. /* Sync up isPolling status with hw (Imask), at the begining all Imask = 0 => isPolling = TRUE */
  1738. Flexcan_Ip_pState->mbs[i].isPolling = TRUE;
  1739. Flexcan_Ip_pState->mbs[i].pMBmessage = NULL_PTR;
  1740. Flexcan_Ip_pState->mbs[i].state = FLEXCAN_MB_IDLE;
  1741. Flexcan_Ip_pState->mbs[i].time_stamp = 0U;
  1742. }
  1743. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1744. /* Sync up isPolling status with hw (Imask), at the begining all Imask = 0 => isPolling = TRUE */
  1745. Flexcan_Ip_pState->enhancedFifoOutput.isPolling = TRUE;
  1746. Flexcan_Ip_pState->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  1747. #endif
  1748. Flexcan_Ip_pState->transferType = Flexcan_Ip_pData->transfer_type;
  1749. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  1750. Flexcan_Ip_pState->rxFifoDMAChannel = Flexcan_Ip_pData->rxFifoDMAChannel;
  1751. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1752. Flexcan_Ip_pState->u32NumOfMbTransferByDMA = Flexcan_Ip_pData->num_enhanced_watermark;
  1753. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  1754. #endif /* FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  1755. /* Clear Callbacks in case of autovariables garbage */
  1756. Flexcan_Ip_pState->callback = Flexcan_Ip_pData->Callback;
  1757. Flexcan_Ip_pState->callbackParam = NULL_PTR;
  1758. Flexcan_Ip_pState->error_callback = Flexcan_Ip_pData->ErrorCallback;
  1759. Flexcan_Ip_pState->errorCallbackParam = NULL_PTR;
  1760. Flexcan_Ip_pState->bIsLegacyFifoEn = Flexcan_Ip_pData->is_rx_fifo_needed;
  1761. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  1762. Flexcan_Ip_pState->bIsEnhancedFifoEn = Flexcan_Ip_pData->is_enhanced_rx_fifo_needed;
  1763. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  1764. Flexcan_Ip_pState->u32MaxMbNum = Flexcan_Ip_pData->max_num_mb;
  1765. Flexcan_Ip_pState->isIntActive = TRUE;
  1766. /* Save runtime structure pointers so irq handler can point to the correct state structure */
  1767. Flexcan_Ip_apxState[Flexcan_Ip_u8Instance] = Flexcan_Ip_pState;
  1768. }
  1769. return eResult;
  1770. }
  1771. /*FUNCTION**********************************************************************
  1772. *
  1773. * Function Name : FlexCAN_DRV_Send
  1774. * Description : This function sends a CAN frame using a configured message
  1775. * buffer. The function returns immediately. If a callback is installed, it will
  1776. * be invoked after the frame was sent.
  1777. *
  1778. *END**************************************************************************/
  1779. /* implements FlexCAN_Ip_Send_Activity */
  1780. Flexcan_Ip_StatusType FlexCAN_Ip_Send(uint8 instance,
  1781. uint8 mb_idx,
  1782. const Flexcan_Ip_DataInfoType * tx_info,
  1783. uint32 msg_id,
  1784. const uint8 * mb_data
  1785. )
  1786. {
  1787. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_ERROR;
  1788. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1789. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1790. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1791. #else
  1792. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1793. #endif
  1794. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1795. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  1796. DevAssert(tx_info != NULL_PTR);
  1797. #endif
  1798. if (!FlexCAN_IsListenOnlyModeEnabled(base))
  1799. {
  1800. result = FlexCAN_StartSendData(instance, mb_idx, tx_info, msg_id, mb_data);
  1801. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1802. if ((FLEXCAN_STATUS_SUCCESS == result) && (FALSE == tx_info->is_polling))
  1803. {
  1804. /* Enable message buffer interrupt*/
  1805. result = FlexCAN_SetMsgBuffIntCmd(base, instance, mb_idx, TRUE, state->isIntActive);
  1806. }
  1807. #endif
  1808. }
  1809. return result;
  1810. }
  1811. /*FUNCTION**********************************************************************
  1812. *
  1813. * Function Name : FlexCAN_Ip_ConfigMb
  1814. * Description : Configure a Rx message buffer.
  1815. * This function will first check if RX FIFO is enabled. If RX FIFO is enabled,
  1816. * the function will make sure if the MB requested is not occupied by RX FIFO
  1817. * and ID filter table. Then this function will set up the message buffer fields,
  1818. * configure the message buffer code for Rx message buffer as NOT_USED, enable
  1819. * the Message Buffer interrupt, configure the message buffer code for Rx
  1820. * message buffer as INACTIVE, copy user's buffer into the message buffer data
  1821. * area, and configure the message buffer code for Rx message buffer as EMPTY.
  1822. *
  1823. *END**************************************************************************/
  1824. /* implements FlexCAN_Ip_ConfigRxMb_Activity */
  1825. Flexcan_Ip_StatusType FlexCAN_Ip_ConfigRxMb(uint8 instance,
  1826. uint8 mb_idx,
  1827. const Flexcan_Ip_DataInfoType * rx_info,
  1828. uint32 msg_id
  1829. )
  1830. {
  1831. Flexcan_Ip_StatusType eResult = FLEXCAN_STATUS_SUCCESS;
  1832. Flexcan_Ip_MsbuffCodeStatusType cs;
  1833. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1834. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1835. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1836. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  1837. DevAssert(rx_info != NULL_PTR);
  1838. if (TRUE == FlexCAN_IsMbOutOfRange(base, mb_idx, state->bIsLegacyFifoEn, state->u32MaxMbNum))
  1839. {
  1840. eResult = FLEXCAN_STATUS_BUFF_OUT_OF_RANGE;
  1841. }
  1842. else
  1843. {
  1844. #endif
  1845. /* Clear the message buffer flag if previous remained triggered */
  1846. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  1847. cs.dataLen = rx_info->data_length;
  1848. cs.msgIdType = rx_info->msg_id_type;
  1849. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  1850. cs.fd_enable = rx_info->fd_enable;
  1851. #endif
  1852. /* Initialize rx mb*/
  1853. cs.code = (uint32)FLEXCAN_RX_NOT_USED;
  1854. FlexCAN_SetRxMsgBuff(base, mb_idx, &cs, msg_id);
  1855. /* Initialize receive MB*/
  1856. cs.code = (uint32)FLEXCAN_RX_INACTIVE;
  1857. FlexCAN_SetRxMsgBuff(base, mb_idx, &cs, msg_id);
  1858. /* Set up FlexCAN message buffer fields for receiving data*/
  1859. cs.code = (uint32)FLEXCAN_RX_EMPTY;
  1860. FlexCAN_SetRxMsgBuff(base, mb_idx, &cs, msg_id);
  1861. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1862. }
  1863. #endif
  1864. return eResult;
  1865. }
  1866. /*FUNCTION**********************************************************************
  1867. *
  1868. * Function Name : FlexCAN_Ip_Receive
  1869. * Description : This function receives a CAN frame into a configured message
  1870. * buffer. The function returns immediately. If a callback is installed, it will
  1871. * be invoked after the frame was received and read into the specified buffer.
  1872. *
  1873. *END**************************************************************************/
  1874. /* implements FlexCAN_Ip_Receive_Activity */
  1875. Flexcan_Ip_StatusType FlexCAN_Ip_Receive(uint8 instance,
  1876. uint8 mb_idx,
  1877. Flexcan_Ip_MsgBuffType * data,
  1878. boolean isPolling
  1879. )
  1880. {
  1881. Flexcan_Ip_StatusType result;
  1882. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1883. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1884. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1885. #endif
  1886. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1887. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  1888. #endif
  1889. result = FlexCAN_StartRxMessageBufferData(instance, mb_idx, data, isPolling);
  1890. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1891. if ((FLEXCAN_STATUS_SUCCESS == result) && (FALSE == isPolling))
  1892. {
  1893. /* Enable MB interrupt*/
  1894. result = FlexCAN_SetMsgBuffIntCmd(base, instance, mb_idx, TRUE, state->isIntActive);
  1895. }
  1896. #endif
  1897. return result;
  1898. }
  1899. /*FUNCTION**********************************************************************
  1900. *
  1901. * Function Name : FlexCAN_Ip_ReceiveBlocking
  1902. * Description : This function receives a CAN frame into a configured message
  1903. * buffer. The function blocks until either a frame was received, or the
  1904. * specified timeout expired.
  1905. *
  1906. *END**************************************************************************/
  1907. /* implements FlexCAN_Ip_ReceiveBlocking_Activity */
  1908. Flexcan_Ip_StatusType FlexCAN_Ip_ReceiveBlocking(uint8 instance,
  1909. uint8 mb_idx,
  1910. Flexcan_Ip_MsgBuffType * data,
  1911. boolean isPolling,
  1912. uint32 u32TimeoutMs
  1913. )
  1914. {
  1915. Flexcan_Ip_StatusType result;
  1916. uint32 timeStart = 0U;
  1917. uint32 timeElapsed = 0U;
  1918. uint32 mS2Ticks = OsIf_MicrosToTicks((u32TimeoutMs * 1000U), FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  1919. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  1920. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  1921. #endif
  1922. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  1923. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1924. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1925. #else
  1926. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  1927. #endif
  1928. result = FlexCAN_StartRxMessageBufferData(instance, mb_idx, data, isPolling);
  1929. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1930. if ((FLEXCAN_STATUS_SUCCESS == result) && (FALSE == isPolling))
  1931. {
  1932. /* Enable MB interrupt*/
  1933. result = FlexCAN_SetMsgBuffIntCmd(base, instance, mb_idx, TRUE, state->isIntActive);
  1934. }
  1935. #endif
  1936. if (FLEXCAN_STATUS_SUCCESS == result)
  1937. {
  1938. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  1939. while (FLEXCAN_MB_RX_BUSY == state->mbs[mb_idx].state)
  1940. {
  1941. if (TRUE == isPolling)
  1942. {
  1943. if (FlexCAN_GetBuffStatusFlag(base, mb_idx) != 0U)
  1944. {
  1945. FlexCAN_IRQHandlerRxMB(instance, mb_idx);
  1946. }
  1947. }
  1948. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  1949. if (timeElapsed >= mS2Ticks)
  1950. {
  1951. result = FLEXCAN_STATUS_TIMEOUT;
  1952. break;
  1953. }
  1954. }
  1955. }
  1956. if ((FLEXCAN_STATUS_TIMEOUT == result) && (FALSE == isPolling))
  1957. {
  1958. #if (FLEXCAN_IP_MB_INTERRUPT_SUPPORT == STD_ON)
  1959. /* Disable Mb interrupt*/
  1960. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, mb_idx, FALSE, state->isIntActive);
  1961. #endif
  1962. }
  1963. if ((FLEXCAN_STATUS_BUFF_OUT_OF_RANGE != result) && (FLEXCAN_STATUS_BUSY != result))
  1964. {
  1965. if ((FLEXCAN_MB_IDLE == state->mbs[mb_idx].state))
  1966. {
  1967. result = FLEXCAN_STATUS_SUCCESS;
  1968. }
  1969. else
  1970. {
  1971. state->mbs[mb_idx].state = FLEXCAN_MB_IDLE;
  1972. result = FLEXCAN_STATUS_TIMEOUT;
  1973. }
  1974. }
  1975. return result;
  1976. }
  1977. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  1978. void DMA_Can_Callback0(void)
  1979. { DMA_Can_Callback(0U); }
  1980. #if FLEXCAN_INSTANCE_COUNT > 1U
  1981. void DMA_Can_Callback1(void)
  1982. { DMA_Can_Callback(1U); }
  1983. #endif
  1984. #if FLEXCAN_INSTANCE_COUNT > 2U
  1985. void DMA_Can_Callback2(void)
  1986. { DMA_Can_Callback(2U); }
  1987. #endif
  1988. #if FLEXCAN_INSTANCE_COUNT > 3U
  1989. void DMA_Can_Callback3(void)
  1990. { DMA_Can_Callback(3U); }
  1991. #endif
  1992. #if FLEXCAN_INSTANCE_COUNT > 4U
  1993. void DMA_Can_Callback4(void)
  1994. { DMA_Can_Callback(4U); }
  1995. #endif
  1996. #if FLEXCAN_INSTANCE_COUNT > 5U
  1997. void DMA_Can_Callback5(void)
  1998. { DMA_Can_Callback(5U); }
  1999. #endif
  2000. #if FLEXCAN_INSTANCE_COUNT > 6U
  2001. void DMA_Can_Callback6(void)
  2002. { DMA_Can_Callback(6U); }
  2003. #endif
  2004. #if FLEXCAN_INSTANCE_COUNT > 7U
  2005. void DMA_Can_Callback7(void)
  2006. { DMA_Can_Callback(7U); }
  2007. #endif
  2008. #endif /* FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  2009. /*FUNCTION**********************************************************************
  2010. *
  2011. * Function Name : FlexCAN_Ip_RxFifo
  2012. * Description : This function receives a CAN frame using the Rx FIFO or
  2013. * Enhanced Rx FIFO (if available and enabled). If use Enhanced Rx FIFO, the size of
  2014. * the data array will be considered the same as the configured FIFO watermark.
  2015. * The function returns immediately. If a callback is installed, it will be invoked
  2016. * after the frame was received and read into the specified buffer.
  2017. *
  2018. *END**************************************************************************/
  2019. /* implements FlexCAN_Ip_RxFifo_Activity */
  2020. Flexcan_Ip_StatusType FlexCAN_Ip_RxFifo(uint8 instance, Flexcan_Ip_MsgBuffType * data)
  2021. {
  2022. #ifdef MCAL_ENABLE_FAULT_INJECTION
  2023. /* Fault injection point to test dma error event */
  2024. MCAL_FAULT_INJECTION_POINT(CAN_FIP_0_DMA_ERROR_EVENT);
  2025. #endif
  2026. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2027. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2028. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2029. #endif
  2030. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2031. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2032. if (TRUE == FlexCAN_IsEnhancedRxFifoAvailable(base))
  2033. {
  2034. if (TRUE == FlexCAN_IsEnhancedRxFifoEnabled(base))
  2035. {
  2036. result = FlexCAN_StartRxMessageEnhancedFifoData(instance, data);
  2037. }
  2038. else
  2039. {
  2040. result = FlexCAN_StartRxMessageFifoData(instance, data);
  2041. }
  2042. }
  2043. else
  2044. #endif /*FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO*/
  2045. {
  2046. result = FlexCAN_StartRxMessageFifoData(instance, data);
  2047. }
  2048. return result;
  2049. }
  2050. /*FUNCTION**********************************************************************
  2051. *
  2052. * Function Name : FlexCAN_Ip_RxFifoBlocking
  2053. * Description : This function receives a CAN frame using the Rx FIFO or
  2054. * Enhanced Rx FIFO (if available and enabled). If use Enhanced Rx FIFO, the size of
  2055. * the data array will be considered the same as the configured FIFO watermark.
  2056. * The function blocks until either a frame was received, or the specified timeout expired.
  2057. *
  2058. *END**************************************************************************/
  2059. /* implements FlexCAN_Ip_RxFifoBlocking_Activity */
  2060. Flexcan_Ip_StatusType FlexCAN_Ip_RxFifoBlocking(uint8 instance, Flexcan_Ip_MsgBuffType *data, uint32 timeout)
  2061. {
  2062. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2063. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2064. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2065. #endif
  2066. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2067. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2068. #endif
  2069. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2070. if (TRUE == FlexCAN_IsEnhancedRxFifoAvailable(base))
  2071. {
  2072. if (TRUE == FlexCAN_IsEnhancedRxFifoEnabled(base))
  2073. {
  2074. result = FlexCAN_StartRxMessageEnhancedFifoData(instance, data);
  2075. if (FLEXCAN_STATUS_SUCCESS == result)
  2076. {
  2077. result = FlexCAN_ProccessEnhancedRxFifo(instance, timeout);
  2078. }
  2079. }
  2080. else
  2081. {
  2082. result = FlexCAN_StartRxMessageFifoData(instance, data);
  2083. if (FLEXCAN_STATUS_SUCCESS == result)
  2084. {
  2085. result = FlexCAN_ProccessLegacyRxFIFO(instance, timeout);
  2086. }
  2087. }
  2088. }
  2089. else
  2090. #endif /*FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO*/
  2091. {
  2092. result = FlexCAN_StartRxMessageFifoData(instance, data);
  2093. if (FLEXCAN_STATUS_SUCCESS == result)
  2094. {
  2095. result = FlexCAN_ProccessLegacyRxFIFO(instance, timeout);
  2096. }
  2097. }
  2098. return result;
  2099. }
  2100. /*FUNCTION**********************************************************************
  2101. *
  2102. * Function Name : FlexCAN_Ip_ConfigRxFifo
  2103. * Description : Confgure RX FIFO ID filter table elements.
  2104. * This function will confgure RX FIFO ID filter table elements, and enable RX
  2105. * FIFO interrupts.
  2106. *
  2107. *END**************************************************************************/
  2108. /* implements FlexCAN_Ip_ConfigRxFifo_Activity */
  2109. Flexcan_Ip_StatusType FlexCAN_Ip_ConfigRxFifo_Privileged(uint8 instance,
  2110. Flexcan_Ip_RxFifoIdElementFormatType id_format,
  2111. const Flexcan_Ip_IdTableType * id_filter_table
  2112. )
  2113. {
  2114. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2115. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2116. #endif
  2117. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2118. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  2119. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2120. boolean disabled = !FlexCAN_IsEnabled(pBase);
  2121. if (TRUE == disabled)
  2122. {
  2123. result = FlexCAN_Enable(pBase);
  2124. }
  2125. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2126. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  2127. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  2128. {
  2129. result = FLEXCAN_STATUS_ERROR;
  2130. }
  2131. #endif
  2132. if (FLEXCAN_STATUS_SUCCESS == result)
  2133. {
  2134. /* Initialize rx fifo*/
  2135. FlexCAN_SetRxFifoFilter(pBase, id_format, id_filter_table);
  2136. }
  2137. if (TRUE == disabled)
  2138. {
  2139. status = FlexCAN_Disable(pBase);
  2140. if (FLEXCAN_STATUS_SUCCESS != status)
  2141. {
  2142. result = status;
  2143. }
  2144. }
  2145. return result;
  2146. }
  2147. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2148. /*FUNCTION**********************************************************************
  2149. *
  2150. * Function Name : FlexCAN_Ip_ConfigEnhancedRxFifo
  2151. * Description : Confgure Enhanced RX FIFO ID filter table elements.
  2152. * This function will confgure Enhanced RX FIFO ID filter table elements, and enable Enhanced RX
  2153. * FIFO interrupts.
  2154. *END**************************************************************************/
  2155. /* implements FlexCAN_Ip_ConfigEnhancedRxFifo_Activity */
  2156. Flexcan_Ip_StatusType FlexCAN_Ip_ConfigEnhancedRxFifo_Privileged(uint8 instance, const Flexcan_Ip_EnhancedIdTableType * id_filter_table)
  2157. {
  2158. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2159. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  2160. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2161. boolean disabled = !FlexCAN_IsEnabled(pBase);
  2162. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2163. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2164. DevAssert(FlexCAN_IsEnhancedRxFifoAvailable(pBase));
  2165. DevAssert(id_filter_table != NULL_PTR);
  2166. #endif
  2167. if (TRUE == disabled)
  2168. {
  2169. result = FlexCAN_Enable(pBase);
  2170. }
  2171. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2172. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  2173. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  2174. {
  2175. result = FLEXCAN_STATUS_ERROR;
  2176. }
  2177. #endif
  2178. if (FLEXCAN_STATUS_SUCCESS == result)
  2179. {
  2180. /* Initialize rx fifo*/
  2181. FlexCAN_SetEnhancedRxFifoFilter(pBase, id_filter_table);
  2182. }
  2183. if (TRUE == disabled)
  2184. {
  2185. status = FlexCAN_Disable(pBase);
  2186. if (FLEXCAN_STATUS_SUCCESS != status)
  2187. {
  2188. result = status;
  2189. }
  2190. }
  2191. return result;
  2192. }
  2193. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  2194. /*FUNCTION**********************************************************************
  2195. *
  2196. * Function Name : FlexCAN_Ip_ConfigRemoteResponseMb
  2197. * Description : Configures a transmit message buffer for remote frame
  2198. * response. This function will first check if RX FIFO is enabled. If RX FIFO is
  2199. * enabled, the function will make sure if the MB requested is not occupied by
  2200. * the RX FIFO and ID filter table. Then this function will set up the message
  2201. * buffer fields, configure the message buffer code for Tx buffer as RX_RANSWER,
  2202. * and enable the Message Buffer interrupt if required by user.
  2203. *
  2204. *END**************************************************************************/
  2205. /* implements FlexCAN_Ip_ConfigRemoteResponseMb_Activity */
  2206. Flexcan_Ip_StatusType FlexCAN_Ip_ConfigRemoteResponseMb(uint8 instance,
  2207. uint8 mb_idx,
  2208. const Flexcan_Ip_DataInfoType *tx_info,
  2209. uint32 msg_id,
  2210. const uint8 *mb_data
  2211. )
  2212. {
  2213. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2214. Flexcan_Ip_MsbuffCodeStatusType cs;
  2215. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2216. const Flexcan_Ip_StateType * const state = Flexcan_Ip_apxState[instance];
  2217. volatile uint32 * pMbAddr = NULL_PTR;
  2218. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2219. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2220. DevAssert(tx_info != NULL_PTR);
  2221. /* Remote Request Store can't operate same time with automatic remote response */
  2222. DevAssert(0U == (pBase->CTRL2 & FLEXCAN_CTRL2_RRS_MASK));
  2223. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  2224. /* Check if the Payload Size is smaller than the payload configured */
  2225. DevAssert(((uint8)tx_info->data_length) <= FlexCAN_GetMbPayloadSize(pBase, mb_idx));
  2226. #else
  2227. DevAssert(((uint8)tx_info->data_length) <= 8U);
  2228. #endif
  2229. if (TRUE == FlexCAN_IsMbOutOfRange(pBase, mb_idx, state->bIsLegacyFifoEn, state->u32MaxMbNum))
  2230. {
  2231. result = FLEXCAN_STATUS_BUFF_OUT_OF_RANGE;
  2232. }
  2233. #endif
  2234. if (FLEXCAN_STATUS_SUCCESS == result)
  2235. {
  2236. /* Initialize transmit mb*/
  2237. cs.dataLen = tx_info->data_length;
  2238. cs.msgIdType = tx_info->msg_id_type;
  2239. cs.code = (uint32)FLEXCAN_RX_RANSWER;
  2240. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  2241. cs.fd_enable = FALSE;
  2242. cs.enable_brs = FALSE;
  2243. cs.fd_padding = 0x00U;
  2244. #endif
  2245. FlexCAN_ClearMsgBuffIntStatusFlag(pBase, mb_idx);
  2246. pMbAddr = FlexCAN_GetMsgBuffRegion(pBase, mb_idx);
  2247. FlexCAN_SetTxMsgBuff(pMbAddr, &cs, msg_id, mb_data, tx_info->is_remote);
  2248. if (FALSE == tx_info->is_polling)
  2249. {
  2250. /* Enable MB interrupt*/
  2251. result = FlexCAN_SetMsgBuffIntCmd(pBase, instance, mb_idx, TRUE, state->isIntActive);
  2252. }
  2253. }
  2254. return result;
  2255. }
  2256. /*FUNCTION**********************************************************************
  2257. *
  2258. * Function Name : FlexCAN_Ip_GetTransferStatus
  2259. * Description : This function returns whether the previous FLEXCAN receive is
  2260. * completed.
  2261. * When performing a non-blocking receive, the user can call this function to
  2262. * ascertain the state of the current receive progress: in progress (or busy)
  2263. * or complete (success). In case Enhanced Rx Fifo, mb_idx will be 255.
  2264. *
  2265. *END**************************************************************************/
  2266. /* implements FlexCAN_Ip_GetTransferStatus_Activity */
  2267. Flexcan_Ip_StatusType FlexCAN_Ip_GetTransferStatus(uint8 instance, uint8 mb_idx)
  2268. {
  2269. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2270. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_ERROR;
  2271. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2272. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2273. DevAssert((mb_idx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM) || (255u == mb_idx));
  2274. #endif
  2275. if (mb_idx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM)
  2276. {
  2277. if (FLEXCAN_MB_IDLE == state->mbs[mb_idx].state)
  2278. {
  2279. status = FLEXCAN_STATUS_SUCCESS;
  2280. }
  2281. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  2282. else if (FLEXCAN_MB_DMA_ERROR == state->mbs[mb_idx].state)
  2283. {
  2284. status = FLEXCAN_STATUS_ERROR;
  2285. }
  2286. #endif
  2287. else
  2288. {
  2289. status = FLEXCAN_STATUS_BUSY;
  2290. }
  2291. }
  2292. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2293. else
  2294. {
  2295. if (FLEXCAN_MB_IDLE == state->enhancedFifoOutput.state)
  2296. {
  2297. status = FLEXCAN_STATUS_SUCCESS;
  2298. }
  2299. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  2300. else if (FLEXCAN_MB_DMA_ERROR == state->enhancedFifoOutput.state)
  2301. {
  2302. status = FLEXCAN_STATUS_ERROR;
  2303. }
  2304. #endif
  2305. else
  2306. {
  2307. status = FLEXCAN_STATUS_BUSY;
  2308. }
  2309. }
  2310. #endif /* FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  2311. return status;
  2312. }
  2313. /*FUNCTION**********************************************************************
  2314. *
  2315. * Function Name : FlexCAN_CompleteRxMessageFifoData
  2316. * Description : Finish up a receive by completing the process of receiving
  2317. * data and disabling the interrupt.
  2318. * This is not a public API as it is called from other driver functions.
  2319. *
  2320. *END**************************************************************************/
  2321. static void FlexCAN_CompleteRxMessageFifoData(uint8 instance)
  2322. {
  2323. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2324. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2325. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2326. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2327. #endif
  2328. if (FLEXCAN_RXFIFO_USING_INTERRUPTS == state->transferType)
  2329. {
  2330. /* Disable RX FIFO interrupts*/
  2331. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE, FALSE, state->isIntActive);
  2332. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, FLEXCAN_IP_LEGACY_RXFIFO_WARNING, FALSE, state->isIntActive);
  2333. (void)FlexCAN_SetMsgBuffIntCmd(base, instance, FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW, FALSE, state->isIntActive);
  2334. }
  2335. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  2336. else if (FLEXCAN_RXFIFO_USING_DMA == state->transferType)
  2337. {
  2338. Dma_Ip_LogicChannelStatusType dmaStatus;
  2339. (void)Dma_Ip_GetLogicChannelStatus(state->rxFifoDMAChannel, &dmaStatus);
  2340. if (DMA_IP_CH_ERROR_STATE == dmaStatus.ChStateValue)
  2341. {
  2342. (void)Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_CLEAR_ERROR);
  2343. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_DMA_ERROR;
  2344. }
  2345. (void)Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  2346. if (state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state != FLEXCAN_MB_DMA_ERROR)
  2347. {
  2348. Flexcan_Ip_MsgBuffType * fifo_message = state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage;
  2349. uint32 * msgData_32 = (uint32 *)fifo_message->data;
  2350. /* Adjust the ID if it is not extended */
  2351. if (0U == ((fifo_message->cs) & FLEXCAN_IP_CS_IDE_MASK))
  2352. {
  2353. fifo_message->msgId = fifo_message->msgId >> FLEXCAN_IP_ID_STD_SHIFT;
  2354. }
  2355. /* Extract the data length */
  2356. fifo_message->dataLen = (uint8)((fifo_message->cs & FLEXCAN_IP_CS_DLC_MASK) >> FLEXCAN_IP_CS_DLC_SHIFT);
  2357. /* Extract the IDHIT */
  2358. fifo_message->id_hit = (uint8)((fifo_message->cs & FLEXCAN_IP_CS_IDHIT_MASK) >> FLEXCAN_IP_CS_IDHIT_SHIFT);
  2359. /* Extract the Time Stamp */
  2360. fifo_message->time_stamp = (uint32)((fifo_message->cs & FLEXCAN_IP_CS_TIME_STAMP_MASK) >> FLEXCAN_IP_CS_TIME_STAMP_SHIFT);
  2361. /* Reverse the endianness */
  2362. FLEXCAN_IP_SWAP_BYTES_IN_WORD(msgData_32[0], msgData_32[0]);
  2363. FLEXCAN_IP_SWAP_BYTES_IN_WORD(msgData_32[1], msgData_32[1]);
  2364. }
  2365. }
  2366. else
  2367. {
  2368. /* do nothing when transferType is POLLING */
  2369. }
  2370. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  2371. /* Clear fifo message*/
  2372. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].pMBmessage = NULL_PTR;
  2373. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  2374. if (state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state != FLEXCAN_MB_DMA_ERROR)
  2375. {
  2376. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  2377. if ((state->callback != NULL_PTR) && (FLEXCAN_RXFIFO_USING_DMA == state->transferType))
  2378. {
  2379. state->callback(instance, FLEXCAN_EVENT_DMA_COMPLETE, FLEXCAN_IP_MB_HANDLE_RXFIFO, state);
  2380. }
  2381. }
  2382. else
  2383. {
  2384. if (state->callback != NULL_PTR)
  2385. {
  2386. state->callback(instance, FLEXCAN_EVENT_DMA_ERROR, FLEXCAN_IP_MB_HANDLE_RXFIFO, state);
  2387. }
  2388. }
  2389. #else
  2390. state->mbs[FLEXCAN_IP_MB_HANDLE_RXFIFO].state = FLEXCAN_MB_IDLE;
  2391. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  2392. }
  2393. /*FUNCTION**********************************************************************
  2394. *
  2395. * Function Name : FlexCAN_IRQHandler
  2396. * Description : Interrupt handler for FLEXCAN.
  2397. * This handler read data from MB or FIFO, and then clear the interrupt flags.
  2398. * This is not a public API as it is called whenever an interrupt occurs.
  2399. * Spurious interrupt implementation:
  2400. * This handler will process only one Normal Buffers (tx or rx or legacy fifo)
  2401. * and Enhance FIFO(if supported). The processing of spurious interrupt will be
  2402. * done when there is no real interrupt(both IF and IE are set) at all.
  2403. * If a spurious interrupt found:
  2404. * - for Rx (Rx normal, Legacy FIFO, Enhanced FIFO): Just clear IF and exit,
  2405. * the Buffer is still ready to receive a new Frame.
  2406. * - for Tx: Clear IF and reset the Buffer to default state to ready to use in next time.
  2407. *END**************************************************************************/
  2408. /* implements CAN_X_MB_Y_ISR_Activity */
  2409. void FlexCAN_IRQHandler
  2410. (
  2411. uint8 instance,
  2412. uint32 startMbIdx,
  2413. uint32 endMbIdx
  2414. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2415. ,boolean bEnhancedFifoExisted
  2416. #endif
  2417. )
  2418. {
  2419. uint32 u32MbHandle = 0U;
  2420. uint32 flag_reg = 0;
  2421. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2422. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2423. boolean bIsSpuriousInt = TRUE;
  2424. /* Get the interrupts that are enabled and ready */
  2425. uint32 mb_idx = endMbIdx;
  2426. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2427. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2428. DevAssert(endMbIdx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM);
  2429. #endif
  2430. /* Check if instance initialized */
  2431. if (NULL_PTR != state)
  2432. {
  2433. flag_reg = FlexCAN_GetMsgBuffIntStatusFlag(base, mb_idx);
  2434. while ((0U == flag_reg) && (mb_idx > startMbIdx))
  2435. {
  2436. mb_idx--;
  2437. flag_reg = FlexCAN_GetMsgBuffIntStatusFlag(base, mb_idx);
  2438. }
  2439. /* Check Tx/Rx interrupt flag and clear the interrupt */
  2440. if (flag_reg != 0U)
  2441. {
  2442. /* At least one real interrupt -> no process spurious interurpt */
  2443. bIsSpuriousInt = FALSE;
  2444. /* For legacy fifo, mb handler is FLEXCAN_IP_MB_HANDLE_RXFIFO(0) */
  2445. u32MbHandle = mb_idx;
  2446. if ((TRUE == state->bIsLegacyFifoEn) && (mb_idx <= FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW))
  2447. {
  2448. FlexCAN_IRQHandlerRxFIFO(instance, mb_idx);
  2449. u32MbHandle = (uint32)FLEXCAN_IP_MB_HANDLE_RXFIFO;
  2450. }
  2451. else
  2452. {
  2453. /* Check mailbox completed reception */
  2454. if (FLEXCAN_MB_RX_BUSY == state->mbs[u32MbHandle].state)
  2455. {
  2456. FlexCAN_IRQHandlerRxMB(instance, mb_idx);
  2457. }
  2458. }
  2459. /* Check mailbox completed transmission */
  2460. if (FLEXCAN_MB_TX_BUSY == state->mbs[u32MbHandle].state)
  2461. {
  2462. FlexCAN_IRQHandlerTxMB(instance, mb_idx);
  2463. }
  2464. /* Check for spurious interrupt */
  2465. if (FlexCAN_GetMsgBuffIntStatusFlag(base, mb_idx) != 0U)
  2466. {
  2467. if (state->mbs[u32MbHandle].state == FLEXCAN_MB_IDLE)
  2468. {
  2469. /* In case of desynchronized status of the MB to avoid trapping in ISR
  2470. * clear the MB flag */
  2471. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  2472. }
  2473. }
  2474. }
  2475. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2476. if (TRUE == bEnhancedFifoExisted)
  2477. {
  2478. if ((TRUE == FlexCAN_IsEnhancedRxFifoEnabled(base)) && (FLEXCAN_RXFIFO_USING_INTERRUPTS == state->transferType))
  2479. {
  2480. bIsSpuriousInt = FlexCAN_ProcessIRQHandlerEnhancedRxFIFO(instance, bIsSpuriousInt);
  2481. }
  2482. }
  2483. #endif /* End of (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  2484. /* continue processing spurious interrupt for normal Mb only if enhance fifo interrupt is not existed on ISR or no enhance fifo interrupts occurred */
  2485. /* Expectation is that ISR just process one Object (Tx, Rx normal, Rx legacy fifo, Rx enhance fifo) when ISR triggered */
  2486. if (TRUE == bIsSpuriousInt)
  2487. {
  2488. FlexCAN_ProcessSpuriousInterruptMB(instance, startMbIdx, endMbIdx);
  2489. }
  2490. }
  2491. else
  2492. {
  2493. /* Clear all interrupt flags when driver is not initialized */
  2494. /* Process spurious interrupt */
  2495. for (mb_idx = startMbIdx; mb_idx <= endMbIdx; mb_idx++)
  2496. {
  2497. /* clear the MB flag */
  2498. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  2499. }
  2500. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  2501. /* Check if Enhance fifo interrupt existed on the ISR! */
  2502. if (TRUE == bEnhancedFifoExisted)
  2503. {
  2504. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE);
  2505. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_WATERMARK);
  2506. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_OVERFLOW);
  2507. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_UNDERFLOW);
  2508. }
  2509. #endif /* End of (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  2510. }
  2511. }
  2512. /*FUNCTION**********************************************************************
  2513. *
  2514. * Function Name : FlexCAN_Ip_ClearErrorStatus
  2515. * Description : Clears various error conditions detected in the reception and
  2516. * transmission of a CAN frame.
  2517. *
  2518. *END**************************************************************************/
  2519. /* implements FlexCAN_Ip_ClearErrorStatus_Activity */
  2520. void FlexCAN_Ip_ClearErrorStatus(uint8 instance, uint32 error)
  2521. {
  2522. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2523. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2524. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2525. #endif
  2526. base->ESR1 = error;
  2527. }
  2528. /*FUNCTION**********************************************************************
  2529. *
  2530. * Function Name : FlexCAN_Ip_GetErrorStatus
  2531. * Description : Reports various error conditions detected in the reception and
  2532. * transmission of a CAN frame and some general status of the device.
  2533. *
  2534. *END**************************************************************************/
  2535. /* implements FlexCAN_Ip_GetErrorStatus_Activity */
  2536. uint32 FlexCAN_Ip_GetErrorStatus(uint8 instance)
  2537. {
  2538. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2539. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2540. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2541. #endif
  2542. return (uint32)(base->ESR1);
  2543. }
  2544. /*FUNCTION**********************************************************************
  2545. *
  2546. * Function Name : FlexCAN_Ip_GetControllerTxErrorCounter
  2547. * Description : Reports Transmit error counter for all errors detected in
  2548. * transmitted messages.
  2549. *
  2550. *END**************************************************************************/
  2551. /* implements FlexCAN_Ip_GetControllerTxErrorCounter_Activity */
  2552. uint8 FlexCAN_Ip_GetControllerTxErrorCounter(uint8 instance)
  2553. {
  2554. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2555. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2556. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2557. #endif
  2558. return (uint8)((base->ECR & FLEXCAN_ECR_TXERRCNT_MASK) >> FLEXCAN_ECR_TXERRCNT_SHIFT);
  2559. }
  2560. /*FUNCTION**********************************************************************
  2561. *
  2562. * Function Name : FlexCAN_Ip_GetControllerRxErrorCounter
  2563. * Description : Reports Receive error counter for all errors detected in
  2564. * received messages.
  2565. *
  2566. *END**************************************************************************/
  2567. /* implements FlexCAN_Ip_GetControllerRxErrorCounter_Activity */
  2568. uint8 FlexCAN_Ip_GetControllerRxErrorCounter(uint8 instance)
  2569. {
  2570. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2571. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2572. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2573. #endif
  2574. return (uint8)((base->ECR & FLEXCAN_ECR_RXERRCNT_MASK) >> FLEXCAN_ECR_RXERRCNT_SHIFT);
  2575. }
  2576. #if (FLEXCAN_IP_FEATURE_BUSOFF_ERROR_INTERRUPT_UNIFIED == STD_ON)
  2577. /*FUNCTION**********************************************************************
  2578. *
  2579. * Function Name : FlexCAN_Busoff_Error_IRQHandler
  2580. * Description : BusOff and Error interrupt handler for FLEXCAN.
  2581. * This is not a public API as it is called whenever an interrupt occurs.
  2582. *
  2583. *END**************************************************************************/
  2584. /* implements CAN_X_BUSOFF_ERROR_ISR_Activity */
  2585. void FlexCAN_Busoff_Error_IRQHandler(uint8 instance)
  2586. {
  2587. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2588. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2589. uint32 u32ErrStatus = 0U;
  2590. boolean isSpuriousInt = TRUE;
  2591. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2592. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2593. #endif
  2594. /* Check if the instance initialized */
  2595. if (NULL_PTR != state)
  2596. {
  2597. /* Get error status to get value updated */
  2598. u32ErrStatus = pBase->ESR1;
  2599. /* Check spurious interrupt */
  2600. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_ERRINT_MASK))) && ((uint32)0U != (pBase->CTRL1 & ((uint32)FLEXCAN_CTRL1_ERRMSK_MASK))))
  2601. {
  2602. pBase->ESR1 = FLEXCAN_ESR1_ERRINT_MASK;
  2603. /* Invoke callback */
  2604. if (state->error_callback != NULL_PTR)
  2605. {
  2606. state->error_callback(instance, FLEXCAN_EVENT_ERROR, u32ErrStatus, state);
  2607. /* Get error status to get value updated due to user may handle ESR1 register */
  2608. u32ErrStatus = pBase->ESR1;
  2609. }
  2610. isSpuriousInt = FALSE;
  2611. }
  2612. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  2613. /* Check if this is spurious interrupt */
  2614. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_ERRINT_FAST_MASK))) && ((uint32)0U != (pBase->CTRL2 & ((uint32)FLEXCAN_CTRL2_ERRMSK_FAST_MASK))))
  2615. {
  2616. pBase->ESR1 = FLEXCAN_ESR1_ERRINT_FAST_MASK;
  2617. /* Invoke callback */
  2618. if (state->error_callback != NULL_PTR)
  2619. {
  2620. state->error_callback(instance, FLEXCAN_EVENT_ERROR_FAST, u32ErrStatus, state);
  2621. /* Get error status to get value updated due to user may handle ESR1 register */
  2622. u32ErrStatus = pBase->ESR1;
  2623. }
  2624. isSpuriousInt = FALSE;
  2625. }
  2626. #endif /* FLEXCAN_IP_FEATURE_HAS_FD */
  2627. /* Check spurious interrupt */
  2628. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_TWRNINT_MASK))) && ((uint32)0U != (pBase->CTRL1 & ((uint32)FLEXCAN_CTRL1_TWRNMSK_MASK))))
  2629. {
  2630. pBase->ESR1 = FLEXCAN_ESR1_TWRNINT_MASK;
  2631. /* Invoke callback */
  2632. if (state->error_callback != NULL_PTR)
  2633. {
  2634. state->error_callback(instance, FLEXCAN_EVENT_TX_WARNING, u32ErrStatus, state);
  2635. /* Get error status to get value updated due to user may handle ESR1 register */
  2636. u32ErrStatus = pBase->ESR1;
  2637. }
  2638. isSpuriousInt = FALSE;
  2639. }
  2640. /* Check spurious interrupt */
  2641. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_RWRNINT_MASK))) && ((uint32)0U != (pBase->CTRL1 & ((uint32)FLEXCAN_CTRL1_RWRNMSK_MASK))))
  2642. {
  2643. pBase->ESR1 = FLEXCAN_ESR1_RWRNINT_MASK;
  2644. /* Invoke callback */
  2645. if (state->error_callback != NULL_PTR)
  2646. {
  2647. state->error_callback(instance, FLEXCAN_EVENT_RX_WARNING, u32ErrStatus, state);
  2648. /* Get error status to get value updated due to user may handle ESR1 register */
  2649. u32ErrStatus = pBase->ESR1;
  2650. }
  2651. isSpuriousInt = FALSE;
  2652. }
  2653. /* Check spurious interrupt */
  2654. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_BOFFINT_MASK))) && ((uint32)0U != (pBase->CTRL1 & ((uint32)FLEXCAN_CTRL1_BOFFMSK_MASK))))
  2655. {
  2656. pBase->ESR1 = FLEXCAN_ESR1_BOFFINT_MASK;
  2657. /* Invoke callback */
  2658. if (state->error_callback != NULL_PTR)
  2659. {
  2660. state->error_callback(instance, FLEXCAN_EVENT_BUSOFF, u32ErrStatus, state);
  2661. }
  2662. isSpuriousInt = FALSE;
  2663. }
  2664. #if (defined(S32K116) || defined (S32K118))
  2665. #if (FLEXCAN_IP_FEATURE_HAS_PRETENDED_NETWORKING == STD_ON)
  2666. if (FLEXCAN_MCR_PNET_EN_MASK == (pBase->MCR & FLEXCAN_MCR_PNET_EN_MASK))
  2667. {
  2668. FlexCAN_WakeUp_IRQHandler(instance);
  2669. }
  2670. #endif /* FLEXCAN_IP_FEATURE_HAS_PRETENDED_NETWORKING */
  2671. #endif /* (defined(S32K116) || defined(S32K118)) */
  2672. }
  2673. if (TRUE == isSpuriousInt)
  2674. {
  2675. pBase->ESR1 = FLEXCAN_IP_ALL_INT;
  2676. #if (defined(S32K116) || defined (S32K118))
  2677. #if (FLEXCAN_IP_FEATURE_HAS_PRETENDED_NETWORKING == STD_ON)
  2678. /* Clear spurious Interrupt */
  2679. FlexCAN_ClearWTOF(pBase);
  2680. FlexCAN_ClearWUMF(pBase);
  2681. #endif /* FLEXCAN_IP_FEATURE_HAS_PRETENDED_NETWORKING */
  2682. #endif /* (defined(S32K116) || defined(S32K118)) */
  2683. }
  2684. }
  2685. #else /* (FLEXCAN_IP_FEATURE_BUSOFF_ERROR_INTERRUPT_UNIFIED == STD_OFF) */
  2686. /*FUNCTION**********************************************************************
  2687. *
  2688. * Function Name : FLEXCAN_Error_IRQHandler
  2689. * Description : Error interrupt handler for FLEXCAN.
  2690. * This is not a public API as it is called whenever an interrupt occurs.
  2691. *
  2692. *END**************************************************************************/
  2693. /* implements CAN_X_ERROR_ISR_Activity */
  2694. void FlexCAN_Error_IRQHandler(uint8 instance)
  2695. {
  2696. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2697. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2698. uint32 u32ErrStatus = 0U;
  2699. boolean isSpuriousInt = TRUE;
  2700. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2701. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2702. #endif
  2703. /* Check if the instance initialized */
  2704. if (NULL_PTR != state)
  2705. {
  2706. /* Get error status to get value updated */
  2707. u32ErrStatus = pBase->ESR1;
  2708. /* Check spurious interrupt */
  2709. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_ERRINT_MASK))) && ((uint32)0U != (pBase->CTRL1 & ((uint32)FLEXCAN_CTRL1_ERRMSK_MASK))))
  2710. {
  2711. pBase->ESR1 = FLEXCAN_ESR1_ERRINT_MASK;
  2712. /* Invoke callback */
  2713. if (state->error_callback != NULL_PTR)
  2714. {
  2715. state->error_callback(instance, FLEXCAN_EVENT_ERROR, u32ErrStatus, state);
  2716. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  2717. /* Get error status to get value updated due to user may handle ESR1 register */
  2718. u32ErrStatus = pBase->ESR1;
  2719. #endif /* FLEXCAN_IP_FEATURE_HAS_FD */
  2720. }
  2721. isSpuriousInt = FALSE;
  2722. }
  2723. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  2724. /* Check if this is spurious interrupt */
  2725. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_ERRINT_FAST_MASK))) && ((uint32)0U != (pBase->CTRL2 & ((uint32)FLEXCAN_CTRL2_ERRMSK_FAST_MASK))))
  2726. {
  2727. pBase->ESR1 = FLEXCAN_ESR1_ERRINT_FAST_MASK;
  2728. /* Invoke callback */
  2729. if (state->error_callback != NULL_PTR)
  2730. {
  2731. state->error_callback(instance, FLEXCAN_EVENT_ERROR_FAST, u32ErrStatus, state);
  2732. }
  2733. isSpuriousInt = FALSE;
  2734. }
  2735. #endif /* FLEXCAN_IP_FEATURE_HAS_FD */
  2736. }
  2737. if (TRUE == isSpuriousInt)
  2738. {
  2739. (pBase->ESR1) = FLEXCAN_IP_ERROR_INT;
  2740. }
  2741. }
  2742. /*FUNCTION**********************************************************************
  2743. *
  2744. * Function Name : FlexCAN_BusOff_IRQHandler
  2745. * Description : BusOff and Tx/Rx Warning interrupt handler for FLEXCAN.
  2746. * This handler only provides a error status report and invokes the user callback,
  2747. * and then clears the interrupt flags.
  2748. * This is not a public API as it is called whenever an interrupt occurs.
  2749. *
  2750. *END**************************************************************************/
  2751. /* implements CAN_X_BUSOFF_ISR_Activity */
  2752. void FlexCAN_BusOff_IRQHandler(uint8 instance)
  2753. {
  2754. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2755. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2756. uint32 u32ErrStatus = 0U;
  2757. boolean isSpuriousInt = TRUE;
  2758. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2759. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2760. #endif
  2761. /* Check If Driver initialized */
  2762. if (NULL_PTR != state)
  2763. {
  2764. /* Get error status to get value updated */
  2765. u32ErrStatus = base->ESR1;
  2766. /* Check spurious interrupt */
  2767. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_TWRNINT_MASK))) && (0U != (base->CTRL1 & ((uint32)FLEXCAN_CTRL1_TWRNMSK_MASK))))
  2768. {
  2769. base->ESR1 = FLEXCAN_ESR1_TWRNINT_MASK;
  2770. /* Invoke callback */
  2771. if (state->error_callback != NULL_PTR)
  2772. {
  2773. state->error_callback(instance, FLEXCAN_EVENT_TX_WARNING, u32ErrStatus, state);
  2774. /* Get error status to get value updated due to user may handle ESR1 register */
  2775. u32ErrStatus = base->ESR1;
  2776. }
  2777. isSpuriousInt = FALSE;
  2778. }
  2779. /* Check spurious interrupt */
  2780. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_RWRNINT_MASK))) && (0U != (base->CTRL1 & ((uint32)FLEXCAN_CTRL1_RWRNMSK_MASK))))
  2781. {
  2782. base->ESR1 = FLEXCAN_ESR1_RWRNINT_MASK;
  2783. /* Invoke callback */
  2784. if (state->error_callback != NULL_PTR)
  2785. {
  2786. state->error_callback(instance, FLEXCAN_EVENT_RX_WARNING, u32ErrStatus, state);
  2787. /* Get error status to get value updated due to user may handle ESR1 register */
  2788. u32ErrStatus = base->ESR1;
  2789. }
  2790. isSpuriousInt = FALSE;
  2791. }
  2792. /* Check spurious interrupt */
  2793. if (((uint32)0U != (u32ErrStatus & ((uint32)FLEXCAN_ESR1_BOFFINT_MASK))) && ((uint32)0U != (base->CTRL1 & ((uint32)FLEXCAN_CTRL1_BOFFMSK_MASK))))
  2794. {
  2795. base->ESR1 = FLEXCAN_ESR1_BOFFINT_MASK;
  2796. /* Invoke callback */
  2797. if (state->error_callback != NULL_PTR)
  2798. {
  2799. state->error_callback(instance, FLEXCAN_EVENT_BUSOFF, u32ErrStatus, state);
  2800. }
  2801. isSpuriousInt = FALSE;
  2802. }
  2803. }
  2804. if (TRUE == isSpuriousInt)
  2805. {
  2806. base->ESR1 = FLEXCAN_IP_BUS_OFF_INT;
  2807. }
  2808. }
  2809. #endif /* (FLEXCAN_IP_FEATURE_BUSOFF_ERROR_INTERRUPT_UNIFIED) */
  2810. /*FUNCTION**********************************************************************
  2811. *
  2812. * Function Name : FlexCAN_Ip_SendBlocking
  2813. * Description : This function sends a CAN frame using a configured message
  2814. * buffer. The function blocks until either the frame was sent, or the specified
  2815. * timeout expired.
  2816. *
  2817. *END**************************************************************************/
  2818. /* implements FlexCAN_Ip_SendBlocking_Activity */
  2819. Flexcan_Ip_StatusType FlexCAN_Ip_SendBlocking(uint8 instance,
  2820. uint8 mb_idx,
  2821. const Flexcan_Ip_DataInfoType * tx_info,
  2822. uint32 msg_id,
  2823. const uint8 * mb_data,
  2824. uint32 timeout_ms
  2825. )
  2826. {
  2827. Flexcan_Ip_StatusType result;
  2828. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2829. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  2830. uint32 timeStart = 0U;
  2831. uint32 timeElapsed = 0U;
  2832. uint32 mS2Ticks = OsIf_MicrosToTicks((timeout_ms * 1000U), FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  2833. uint32 uS2Ticks = 0U;
  2834. uint32 flexcan_mb_config = 0;
  2835. volatile uint32 * flexcan_mb = NULL_PTR;
  2836. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2837. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2838. DevAssert(tx_info != NULL_PTR);
  2839. #endif
  2840. result = FlexCAN_StartSendData(instance, mb_idx, tx_info, msg_id, mb_data);
  2841. if (FLEXCAN_STATUS_SUCCESS == result)
  2842. {
  2843. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  2844. while (FlexCAN_GetBuffStatusFlag(base, mb_idx) != 1U)
  2845. {
  2846. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  2847. if (timeElapsed >= mS2Ticks)
  2848. {
  2849. result = FLEXCAN_STATUS_TIMEOUT;
  2850. break;
  2851. }
  2852. }
  2853. if ((FLEXCAN_STATUS_TIMEOUT == result) && (state->mbs[mb_idx].state != FLEXCAN_MB_IDLE))
  2854. {
  2855. /* Clear message buffer flag */
  2856. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  2857. flexcan_mb = FlexCAN_GetMsgBuffRegion(base, mb_idx);
  2858. flexcan_mb_config = * flexcan_mb;
  2859. /* Reset the code */
  2860. flexcan_mb_config &= (uint32)(~FLEXCAN_IP_CS_CODE_MASK);
  2861. flexcan_mb_config |= ((uint32)(((uint32)FLEXCAN_TX_ABORT & (uint32)0x1F) << (uint8)FLEXCAN_IP_CS_CODE_SHIFT) & (uint32)FLEXCAN_IP_CS_CODE_MASK);
  2862. *flexcan_mb = flexcan_mb_config;
  2863. /* Wait to finish abort operation */
  2864. uS2Ticks = OsIf_MicrosToTicks(FLEXCAN_IP_TIMEOUT_DURATION, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  2865. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  2866. timeElapsed = 0U;
  2867. while (0U == FlexCAN_GetBuffStatusFlag(base, mb_idx))
  2868. {
  2869. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  2870. if (timeElapsed >= uS2Ticks)
  2871. {
  2872. result = FLEXCAN_STATUS_TIMEOUT;
  2873. break;
  2874. }
  2875. }
  2876. flexcan_mb_config = *flexcan_mb;
  2877. /* Check if the MBs have been safely Inactivated */
  2878. if ((uint32)FLEXCAN_TX_INACTIVE == ((flexcan_mb_config & FLEXCAN_IP_CS_CODE_MASK) >> FLEXCAN_IP_CS_CODE_SHIFT))
  2879. {
  2880. /* Transmission have occurred */
  2881. result = FLEXCAN_STATUS_SUCCESS;
  2882. }
  2883. if ((uint32)FLEXCAN_TX_ABORT == ((flexcan_mb_config & FLEXCAN_IP_CS_CODE_MASK) >> FLEXCAN_IP_CS_CODE_SHIFT))
  2884. {
  2885. /* Transmission have been aborted */
  2886. result = FLEXCAN_STATUS_TIMEOUT;
  2887. }
  2888. }
  2889. /* Clear message buffer flag */
  2890. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  2891. state->mbs[mb_idx].state = FLEXCAN_MB_IDLE;
  2892. }
  2893. return result;
  2894. }
  2895. /*FUNCTION**********************************************************************
  2896. *
  2897. * Function Name : FlexCAN_Ip_SetRxMbGlobalMask
  2898. * Description : Set Rx Message Buffer global mask as the mask value provided.
  2899. *
  2900. *END**************************************************************************/
  2901. /* implements FlexCAN_Ip_SetRxMbGlobalMask_Activity */
  2902. Flexcan_Ip_StatusType FlexCAN_Ip_SetRxMbGlobalMask_Privileged(uint8 instance, uint32 mask)
  2903. {
  2904. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2905. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  2906. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2907. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2908. boolean freeze = FALSE;
  2909. #endif
  2910. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2911. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2912. #endif
  2913. boolean disabled = !FlexCAN_IsEnabled(pBase);
  2914. if (TRUE == disabled)
  2915. {
  2916. result = FlexCAN_Enable(pBase);
  2917. }
  2918. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2919. freeze = FlexCAN_IsFreezeMode(pBase);
  2920. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  2921. {
  2922. result = FLEXCAN_STATUS_ERROR;
  2923. }
  2924. #endif
  2925. if (FLEXCAN_STATUS_SUCCESS == result)
  2926. {
  2927. FlexCAN_SetRxMsgBuffGlobalMask(pBase, mask);
  2928. }
  2929. if (TRUE == disabled)
  2930. {
  2931. status = FlexCAN_Disable(pBase);
  2932. if (FLEXCAN_STATUS_SUCCESS != status)
  2933. {
  2934. result = status;
  2935. }
  2936. }
  2937. return result;
  2938. }
  2939. /*FUNCTION**********************************************************************
  2940. *
  2941. * Function Name : FlexCAN_Ip_EnterFreezeMode
  2942. * Description : Enter Driver In freeze Mode.
  2943. *
  2944. *END**************************************************************************/
  2945. /* implements FlexCAN_Ip_EnterFreezeMode_Activity */
  2946. Flexcan_Ip_StatusType FlexCAN_Ip_EnterFreezeMode_Privileged(uint8 instance)
  2947. {
  2948. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2949. return FlexCAN_EnterFreezeMode(base);
  2950. }
  2951. /*FUNCTION**********************************************************************
  2952. *
  2953. * Function Name : FlexCAN_Ip_ExitFreezeMode
  2954. * Description : Exit Driver from freeze Mode.
  2955. *
  2956. *END**************************************************************************/
  2957. /* implements FlexCAN_Ip_ExitFreezeMode_Activity */
  2958. Flexcan_Ip_StatusType FlexCAN_Ip_ExitFreezeMode_Privileged(uint8 instance)
  2959. {
  2960. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  2961. return FlexCAN_ExitFreezeMode(base);
  2962. }
  2963. /*FUNCTION**********************************************************************
  2964. *
  2965. * Function Name : FlexCAN_Ip_SetRxIndividualMask
  2966. * Description : Set Rx individual mask as absolute value provided by mask parameter
  2967. *
  2968. *END**************************************************************************/
  2969. /* implements FlexCAN_Ip_SetRxIndividualMask_Activity */
  2970. Flexcan_Ip_StatusType FlexCAN_Ip_SetRxIndividualMask_Privileged(uint8 instance, uint8 mb_idx, uint32 mask)
  2971. {
  2972. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  2973. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  2974. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  2975. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2976. boolean freeze = FALSE;
  2977. #endif
  2978. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2979. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  2980. if ((mb_idx > (((pBase->MCR) & FLEXCAN_MCR_MAXMB_MASK) >> FLEXCAN_MCR_MAXMB_SHIFT)) || (mb_idx >= FLEXCAN_RXIMR_COUNT))
  2981. {
  2982. result = FLEXCAN_STATUS_BUFF_OUT_OF_RANGE;
  2983. }
  2984. else
  2985. {
  2986. #endif
  2987. boolean disabled = !FlexCAN_IsEnabled(pBase);
  2988. if (TRUE == disabled)
  2989. {
  2990. result = FlexCAN_Enable(pBase);
  2991. }
  2992. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  2993. freeze = FlexCAN_IsFreezeMode(pBase);
  2994. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  2995. {
  2996. result = FLEXCAN_STATUS_ERROR;
  2997. }
  2998. #endif
  2999. if (FLEXCAN_STATUS_SUCCESS == result)
  3000. {
  3001. FlexCAN_SetRxIndividualMask(pBase, mb_idx, mask);
  3002. }
  3003. if (TRUE == disabled)
  3004. {
  3005. status = FlexCAN_Disable(pBase);
  3006. if (FLEXCAN_STATUS_SUCCESS != status)
  3007. {
  3008. result = status;
  3009. }
  3010. }
  3011. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3012. }
  3013. #endif
  3014. return result;
  3015. }
  3016. /*FUNCTION**********************************************************************
  3017. *
  3018. * Function Name : FlexCAN_Ip_SetRxFifoGlobalMask
  3019. * Description : Set RxFifo Global Mask.
  3020. *
  3021. *END**************************************************************************/
  3022. /* implements FlexCAN_Ip_SetRxFifoGlobalMask_Activity */
  3023. Flexcan_Ip_StatusType FlexCAN_Ip_SetRxFifoGlobalMask_Privileged(uint8 instance, uint32 mask)
  3024. {
  3025. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3026. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3027. #endif
  3028. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3029. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3030. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3031. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3032. if (TRUE == disabled)
  3033. {
  3034. result = FlexCAN_Enable(pBase);
  3035. }
  3036. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3037. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  3038. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  3039. {
  3040. result = FLEXCAN_STATUS_ERROR;
  3041. }
  3042. #endif
  3043. if (FLEXCAN_STATUS_SUCCESS == result)
  3044. {
  3045. FlexCAN_SetRxFifoGlobalMask(pBase, mask);
  3046. }
  3047. if (TRUE == disabled)
  3048. {
  3049. status = FlexCAN_Disable(pBase);
  3050. if (FLEXCAN_STATUS_SUCCESS != status)
  3051. {
  3052. result = status;
  3053. }
  3054. }
  3055. return result;
  3056. }
  3057. /*FUNCTION**********************************************************************
  3058. *
  3059. * Function Name : FlexCAN_Ip_Deinit
  3060. * Description : Shutdown a FlexCAN module.
  3061. * This function will disable all FlexCAN interrupts, and disable the FlexCAN.
  3062. *
  3063. *END**************************************************************************/
  3064. /* implements FlexCAN_Ip_Deinit_Activity */
  3065. Flexcan_Ip_StatusType FlexCAN_Ip_Deinit_Privileged(uint8 instance)
  3066. {
  3067. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  3068. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_ERROR;
  3069. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3070. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3071. #endif
  3072. /* Enter Freeze Mode Required before to enter Disabled Mode */
  3073. result = FlexCAN_EnterFreezeMode(base);
  3074. if (FLEXCAN_STATUS_SUCCESS == result)
  3075. {
  3076. /* Reset registers */
  3077. FlexCAN_SetRegDefaultVal(base);
  3078. /* wait for disable */
  3079. (void)FlexCAN_Disable(base);
  3080. /* Clear state pointer that is checked by FLEXCAN_DRV_Init */
  3081. Flexcan_Ip_apxState[instance] = NULL_PTR;
  3082. #if (STD_ON == FLEXCAN_IP_SET_USER_ACCESS_ALLOWED_AVAILABLE)
  3083. /* Ignore if the instance doesn't support REG_PROT */
  3084. if (instance < FLEXCAN_IP_CTRL_REG_PROT_SUPPORT_U8)
  3085. {
  3086. /* Clear the UAA bit in REG_PROT to reset the elevation requirement */
  3087. OsIf_Trusted_Call1param(FlexCAN_ClrUserAccessAllowed, base);
  3088. }
  3089. #endif
  3090. }
  3091. return result;
  3092. }
  3093. /*FUNCTION**********************************************************************
  3094. *
  3095. * Function Name : FlexCAN_Ip_MainFunctionRead
  3096. * Description : Process received Rx MessageBuffer or RxFifo.
  3097. * This function read the messages received as pulling or if the Interrupts are disabled.
  3098. *
  3099. *END**************************************************************************/
  3100. /* implements FlexCAN_Ip_MainFunctionRead_Activity */
  3101. void FlexCAN_Ip_MainFunctionRead(uint8 instance, uint8 mb_idx)
  3102. {
  3103. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  3104. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  3105. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3106. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3107. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3108. DevAssert((mb_idx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM) || (FLEXCAN_IP_MB_ENHANCED_RXFIFO == mb_idx));
  3109. #else
  3110. DevAssert(mb_idx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM);
  3111. #endif
  3112. #endif
  3113. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3114. if ((uint8)FLEXCAN_IP_MB_ENHANCED_RXFIFO == mb_idx)
  3115. {
  3116. if (TRUE == FlexCAN_IsEnhancedRxFifoAvailable(base))
  3117. {
  3118. if (TRUE == FlexCAN_IsEnhancedRxFifoEnabled(base))
  3119. {
  3120. if (FlexCAN_GetEnhancedRxFIFOStatusFlag(base, FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE) != 0U)
  3121. {
  3122. FlexCAN_IRQHandlerEnhancedRxFIFO(instance, FLEXCAN_IP_ENHANCED_RXFIFO_FRAME_AVAILABLE);
  3123. }
  3124. }
  3125. }
  3126. }
  3127. else
  3128. {
  3129. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  3130. if ((TRUE == state->bIsLegacyFifoEn) && (mb_idx <= FLEXCAN_IP_LEGACY_RXFIFO_OVERFLOW))
  3131. {
  3132. /* just process available legacy fifo event only */
  3133. if ((uint8)FLEXCAN_IP_MB_HANDLE_RXFIFO == mb_idx)
  3134. {
  3135. if (FlexCAN_GetBuffStatusFlag(base, FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE) != 0U)
  3136. {
  3137. FlexCAN_IRQHandlerRxFIFO(instance, FLEXCAN_IP_LEGACY_RXFIFO_FRAME_AVAILABLE);
  3138. }
  3139. }
  3140. }
  3141. else
  3142. {
  3143. if (FlexCAN_GetBuffStatusFlag(base, mb_idx) != 0U)
  3144. {
  3145. /* Check mailbox completed reception */
  3146. if (FLEXCAN_MB_RX_BUSY == state->mbs[mb_idx].state)
  3147. {
  3148. FlexCAN_IRQHandlerRxMB(instance, mb_idx);
  3149. }
  3150. }
  3151. }
  3152. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3153. }
  3154. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) */
  3155. }
  3156. /*FUNCTION**********************************************************************
  3157. *
  3158. * Function Name : FlexCAN_Ip_MainFunctionBusOff
  3159. * Description : Check if BusOff occurred
  3160. * This function check the bus off event.
  3161. *
  3162. *END**************************************************************************/
  3163. /* implements FlexCAN_Ip_MainFunctionBusOff_Activity */
  3164. Flexcan_Ip_StatusType FlexCAN_Ip_MainFunctionBusOff_Privileged(uint8 instance)
  3165. {
  3166. Flexcan_Ip_StatusType eRetVal = FLEXCAN_STATUS_ERROR;
  3167. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  3168. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  3169. uint32 u32ErrStatus = 0U;
  3170. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3171. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3172. #endif
  3173. /* Get error status */
  3174. u32ErrStatus = base->ESR1;
  3175. if (0U != (u32ErrStatus & FLEXCAN_ESR1_BOFFINT_MASK))
  3176. {
  3177. /* Invoke callback */
  3178. if (state->error_callback != NULL_PTR)
  3179. {
  3180. state->error_callback(instance, FLEXCAN_EVENT_BUSOFF, u32ErrStatus, state);
  3181. }
  3182. /* Clear BusOff Status Flag */
  3183. base->ESR1 = FLEXCAN_ESR1_BOFFINT_MASK;
  3184. eRetVal = FLEXCAN_STATUS_SUCCESS;
  3185. }
  3186. return eRetVal;
  3187. }
  3188. /*FUNCTION**********************************************************************
  3189. *
  3190. * Function Name : FlexCAN_Ip_MainFunctionWrite
  3191. * Description : Process transmitted Tx MessageBuffer
  3192. * This function check the message if have been sent.
  3193. *
  3194. *END**************************************************************************/
  3195. /* implements FlexCAN_Ip_MainFunctionWrite_Activity */
  3196. void FlexCAN_Ip_MainFunctionWrite(uint8 instance, uint8 mb_idx)
  3197. {
  3198. FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  3199. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[instance];
  3200. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3201. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3202. #endif
  3203. if (FlexCAN_GetBuffStatusFlag(base, mb_idx) != 0U)
  3204. {
  3205. state->mbs[mb_idx].time_stamp = FlexCAN_GetMsgBuffTimestamp(base, mb_idx);
  3206. FlexCAN_UnlockRxMsgBuff(base);
  3207. /* Clear message buffer flag */
  3208. FlexCAN_ClearMsgBuffIntStatusFlag(base, mb_idx);
  3209. state->mbs[mb_idx].state = FLEXCAN_MB_IDLE;
  3210. }
  3211. }
  3212. /*FUNCTION**********************************************************************
  3213. *
  3214. * Function Name : FlexCAN_Ip_GetStopMode
  3215. * Description : Check if the FlexCAN instance is STOPPED.
  3216. *
  3217. *END**************************************************************************/
  3218. /* implements FlexCAN_Ip_GetStopMode_Activity */
  3219. boolean FlexCAN_Ip_GetStopMode_Privileged(uint8 instance)
  3220. {
  3221. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  3222. return ((FLEXCAN_MCR_LPMACK_MASK == (base->MCR & FLEXCAN_MCR_LPMACK_MASK)) ? TRUE : FALSE);
  3223. }
  3224. /*FUNCTION**********************************************************************
  3225. *
  3226. * Function Name : FlexCAN_Ip_GetStartMode
  3227. * Description : Check if the FlexCAN instance is STARTED.
  3228. *
  3229. *END**************************************************************************/
  3230. /* implements FlexCAN_Ip_GetStartMode_Activity */
  3231. boolean FlexCAN_Ip_GetStartMode_Privileged(uint8 instance)
  3232. {
  3233. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  3234. return ((0U == (base->MCR & (FLEXCAN_MCR_LPMACK_MASK | FLEXCAN_MCR_FRZACK_MASK))) ? TRUE : FALSE);
  3235. }
  3236. /*FUNCTION**********************************************************************
  3237. *
  3238. * Function Name : FlexCAN_Ip_SetStartMode
  3239. * Description : Set the FlexCAN instance in START mode.
  3240. *
  3241. *END**************************************************************************/
  3242. /* implements FlexCAN_Ip_SetStartMode_Activity */
  3243. Flexcan_Ip_StatusType FlexCAN_Ip_SetStartMode_Privileged(uint8 instance)
  3244. {
  3245. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3246. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3247. #endif
  3248. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3249. /* Start critical section: implementation depends on integrator */
  3250. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_07();
  3251. /* Enable Flexcan Module */
  3252. pBase->MCR &= ~FLEXCAN_MCR_MDIS_MASK;
  3253. /* End critical section: implementation depends on integrator */
  3254. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_07();
  3255. return (FlexCAN_ExitFreezeMode(pBase));
  3256. }
  3257. /*FUNCTION**********************************************************************
  3258. *
  3259. * Function Name : FlexCAN_Ip_SetStopMode
  3260. * Description : Set the FlexCAN instance in STOP mode.
  3261. *
  3262. *END**************************************************************************/
  3263. /* implements FlexCAN_Ip_SetStopMode_Activity */
  3264. Flexcan_Ip_StatusType FlexCAN_Ip_SetStopMode_Privileged(uint8 instance)
  3265. {
  3266. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3267. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3268. #endif
  3269. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3270. Flexcan_Ip_StatusType status;
  3271. status = FlexCAN_EnterFreezeMode(pBase);
  3272. if (FLEXCAN_STATUS_SUCCESS == status)
  3273. {
  3274. /* TODO: deactivate all pending tx, this should be move to IPW */
  3275. /* TODO:Clear all flag */
  3276. /* TODO: reset MB status */
  3277. /* TODO: disable all interrupt */
  3278. status = FlexCAN_Disable(pBase);
  3279. }
  3280. return status;
  3281. }
  3282. /*FUNCTION**********************************************************************
  3283. *
  3284. * Function Name : FlexCAN_Ip_SetRxMaskType
  3285. * Description : Set RX masking type.
  3286. * This function will set RX masking type as RX global mask or RX individual
  3287. * mask.
  3288. *
  3289. *END**************************************************************************/
  3290. /* implements FlexCAN_Ip_SetRxMaskType_Activity */
  3291. Flexcan_Ip_StatusType FlexCAN_Ip_SetRxMaskType_Privileged(uint8 instance, Flexcan_Ip_RxMaskType type)
  3292. {
  3293. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3294. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3295. #endif
  3296. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3297. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3298. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3299. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3300. if (TRUE == disabled)
  3301. {
  3302. result = FlexCAN_Enable(pBase);
  3303. }
  3304. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3305. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  3306. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  3307. {
  3308. result = FLEXCAN_STATUS_ERROR;
  3309. }
  3310. #endif
  3311. if (FLEXCAN_STATUS_SUCCESS == result)
  3312. {
  3313. /* Start critical section: implementation depends on integrator */
  3314. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_08();
  3315. FlexCAN_SetRxMaskType(pBase, type);
  3316. /* End critical section: implementation depends on integrator */
  3317. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_08();
  3318. }
  3319. if (TRUE == disabled)
  3320. {
  3321. status = FlexCAN_Disable(pBase);
  3322. if (FLEXCAN_STATUS_SUCCESS != status)
  3323. {
  3324. result = status;
  3325. }
  3326. }
  3327. return result;
  3328. }
  3329. /*FUNCTION**********************************************************************
  3330. *
  3331. * Function Name : FlexCAN_Ip_SetBitrate
  3332. * Description : Set FlexCAN baudrate.
  3333. * This function will set up all the time segment values for classical frames or the
  3334. * extended time segments. Those time segment values are passed in by the user and
  3335. * are based on the required baudrate.
  3336. *
  3337. *END**************************************************************************/
  3338. /* implements FlexCAN_Ip_SetBitrate_Activity */
  3339. Flexcan_Ip_StatusType FlexCAN_Ip_SetBitrate_Privileged(uint8 instance, const Flexcan_Ip_TimeSegmentType * bitrate, boolean enhExt)
  3340. {
  3341. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3342. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3343. DevAssert(bitrate != NULL_PTR);
  3344. #endif
  3345. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3346. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3347. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3348. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3349. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_OFF)
  3350. (void)enhExt;
  3351. #endif
  3352. if (TRUE == disabled)
  3353. {
  3354. result = FlexCAN_Enable(pBase);
  3355. }
  3356. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3357. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  3358. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  3359. {
  3360. result = FLEXCAN_STATUS_ERROR;
  3361. }
  3362. #endif
  3363. if (FLEXCAN_STATUS_SUCCESS == result)
  3364. {
  3365. /* Start critical section: implementation depends on integrator */
  3366. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_14();
  3367. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3368. /* Enable the use of extended bit time definitions */
  3369. FlexCAN_EnhCbtEnable(pBase, enhExt);
  3370. if (TRUE == enhExt)
  3371. {
  3372. /* Set extended time segments*/
  3373. FlexCAN_SetEnhancedNominalTimeSegments(pBase, bitrate);
  3374. }
  3375. else
  3376. #endif
  3377. {
  3378. if (TRUE == FlexCAN_IsExCbtEnabled(pBase))
  3379. {
  3380. FlexCAN_SetExtendedTimeSegments(pBase, bitrate);
  3381. }
  3382. else
  3383. {
  3384. FlexCAN_SetTimeSegments(pBase, bitrate);
  3385. }
  3386. }
  3387. /* End critical section: implementation depends on integrator */
  3388. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_14();
  3389. }
  3390. if (TRUE == disabled)
  3391. {
  3392. status = FlexCAN_Disable(pBase);
  3393. if (FLEXCAN_STATUS_SUCCESS != status)
  3394. {
  3395. result = status;
  3396. }
  3397. }
  3398. return result;
  3399. }
  3400. /*FUNCTION**********************************************************************
  3401. *
  3402. * Function Name : FlexCAN_Ip_GetBitrate
  3403. * Description : Get FlexCAN Baudrate.
  3404. * This function will be return the current bit rate settings for classical frames
  3405. * or the arbitration phase of FD frames.
  3406. *
  3407. *END**************************************************************************/
  3408. /* implements FlexCAN_Ip_GetBitrate_Activity */
  3409. boolean FlexCAN_Ip_GetBitrate(uint8 instance, Flexcan_Ip_TimeSegmentType * bitrate)
  3410. {
  3411. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3412. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3413. DevAssert(bitrate != NULL_PTR);
  3414. #endif
  3415. const FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3416. boolean enhCbt = FALSE;
  3417. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3418. enhCbt = FlexCAN_IsEnhCbtEnabled(pBase);
  3419. if (TRUE == enhCbt)
  3420. {
  3421. FlexCAN_GetEnhancedNominalTimeSegments(pBase, bitrate);
  3422. }
  3423. else
  3424. #endif
  3425. {
  3426. if (TRUE == FlexCAN_IsExCbtEnabled(pBase))
  3427. {
  3428. /* Get the Extended time segments*/
  3429. FlexCAN_GetExtendedTimeSegments(pBase, bitrate);
  3430. }
  3431. else
  3432. {
  3433. /* Get the time segments*/
  3434. FlexCAN_GetTimeSegments(pBase, bitrate);
  3435. }
  3436. }
  3437. return enhCbt;
  3438. }
  3439. #if (FLEXCAN_IP_FEATURE_HAS_FD == STD_ON)
  3440. /*FUNCTION**********************************************************************
  3441. *
  3442. * Function Name : FlexCAN_Ip_ClearTDCFail
  3443. * Description : This function clear the TDC Fail flag.
  3444. *
  3445. *END**************************************************************************/
  3446. /* implements FlexCAN_Ip_ClearTDCFail_Activity */
  3447. void FlexCAN_Ip_ClearTDCFail(uint8 u8Instance)
  3448. {
  3449. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3450. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3451. #endif
  3452. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3453. /* Start critical section: implementation depends on integrator */
  3454. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_09();
  3455. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3456. /* Check if enhaced CBT is Enabled */
  3457. if (TRUE == FlexCAN_IsEnhCbtEnabled(pBase))
  3458. {
  3459. pBase->ETDC |= FLEXCAN_ETDC_ETDCFAIL_MASK;
  3460. }
  3461. else
  3462. #endif
  3463. {
  3464. pBase->FDCTRL |= FLEXCAN_FDCTRL_TDCFAIL_MASK;
  3465. }
  3466. /* End critical section: implementation depends on integrator */
  3467. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_09();
  3468. }
  3469. /*FUNCTION**********************************************************************
  3470. *
  3471. * Function Name : FlexCAN_Ip_GetTDCFail
  3472. * Description : This function get the value of the TDC Fail flag.
  3473. *
  3474. *END**************************************************************************/
  3475. /* implements FlexCAN_Ip_GetTDCFail_Activity */
  3476. boolean FlexCAN_Ip_GetTDCFail(uint8 u8Instance)
  3477. {
  3478. boolean value=FALSE;
  3479. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3480. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3481. #endif
  3482. const FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3483. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3484. /* Check if enhaced CBT is Enabled */
  3485. if (TRUE == FlexCAN_IsEnhCbtEnabled(pBase))
  3486. {
  3487. value = ((pBase->ETDC & FLEXCAN_ETDC_ETDCFAIL_MASK) == FLEXCAN_ETDC_ETDCFAIL_MASK) ? TRUE : FALSE;
  3488. }
  3489. else
  3490. #endif
  3491. {
  3492. value = ((pBase->FDCTRL & FLEXCAN_FDCTRL_TDCFAIL_MASK) == FLEXCAN_FDCTRL_TDCFAIL_MASK) ? TRUE : FALSE;
  3493. }
  3494. return value;
  3495. }
  3496. /*FUNCTION**********************************************************************
  3497. *
  3498. * Function Name : FlexCAN_Ip_GetTDCValue
  3499. * Description : This function get the value of the Transceiver Delay Compensation.
  3500. *
  3501. *END**************************************************************************/
  3502. /* implements FlexCAN_Ip_GetTDCValue_Activity */
  3503. uint8 FlexCAN_Ip_GetTDCValue(uint8 u8Instance)
  3504. {
  3505. uint8 value = 0;
  3506. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3507. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3508. #endif
  3509. const FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3510. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3511. /* Check if enhaced CBT is Enabled */
  3512. if (TRUE == FlexCAN_IsEnhCbtEnabled(pBase))
  3513. {
  3514. value = (uint8)((pBase->ETDC& FLEXCAN_ETDC_ETDCVAL_MASK) >> FLEXCAN_ETDC_ETDCVAL_SHIFT);
  3515. }
  3516. else
  3517. #endif
  3518. {
  3519. value = (uint8)((pBase->FDCTRL & FLEXCAN_FDCTRL_TDCVAL_MASK) >> FLEXCAN_FDCTRL_TDCVAL_SHIFT);
  3520. }
  3521. return value;
  3522. }
  3523. /*FUNCTION**********************************************************************
  3524. *
  3525. * Function Name : FlexCAN_Ip_SetBitrateCbt
  3526. * Description : Set FlexCAN bitrate.
  3527. * This function will set up all the time segment values for the data phase of
  3528. * FD frames. Those time segment values are passed in by the user and are based
  3529. * on the required baudrate.
  3530. *
  3531. *END**************************************************************************/
  3532. /* implements FlexCAN_Ip_SetBitrateCbt_Activity */
  3533. Flexcan_Ip_StatusType FlexCAN_Ip_SetBitrateCbt_Privileged(uint8 instance, const Flexcan_Ip_TimeSegmentType * bitrate, boolean bitRateSwitch)
  3534. {
  3535. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3536. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3537. DevAssert(bitrate != NULL_PTR);
  3538. #endif
  3539. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3540. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3541. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3542. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3543. boolean fd_enable = FlexCAN_IsFDEnabled(pBase);
  3544. if (TRUE == disabled)
  3545. {
  3546. result = FlexCAN_Enable(pBase);
  3547. }
  3548. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3549. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  3550. if ((FALSE == fd_enable) || ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result)))
  3551. {
  3552. result = FLEXCAN_STATUS_ERROR;
  3553. }
  3554. #endif
  3555. if (FLEXCAN_STATUS_SUCCESS == result)
  3556. {
  3557. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3558. boolean enhCbt = FlexCAN_IsEnhCbtEnabled(pBase);
  3559. #endif
  3560. /* Start critical section: implementation depends on integrator */
  3561. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_15();
  3562. FlexCAN_SetFDEnabled(pBase, fd_enable, bitRateSwitch);
  3563. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3564. if (TRUE == enhCbt)
  3565. {
  3566. FlexCAN_SetEnhancedDataTimeSegments(pBase, bitrate);
  3567. }
  3568. else
  3569. #endif
  3570. {
  3571. /* Set time segments*/
  3572. FlexCAN_SetFDTimeSegments(pBase, bitrate);
  3573. }
  3574. /* End critical section: implementation depends on integrator */
  3575. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_15();
  3576. }
  3577. if (TRUE == disabled)
  3578. {
  3579. status = FlexCAN_Disable(pBase);
  3580. if (FLEXCAN_STATUS_SUCCESS != status)
  3581. {
  3582. result = status;
  3583. }
  3584. }
  3585. return result;
  3586. }
  3587. /*FUNCTION**********************************************************************
  3588. *
  3589. * Function Name : FlexCAN_Ip_GetBitrateFD
  3590. * Description : Get FlexCAN baudrate.
  3591. * This function will be return the current bit rate settings for the data phase
  3592. * of FD frames.
  3593. *
  3594. *END**************************************************************************/
  3595. /* implements FlexCAN_Ip_GetBitrateFD_Activity */
  3596. boolean FlexCAN_Ip_GetBitrateFD(uint8 instance, Flexcan_Ip_TimeSegmentType * bitrate)
  3597. {
  3598. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3599. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3600. DevAssert(bitrate != NULL_PTR);
  3601. #endif
  3602. const FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3603. boolean enhCbt = FALSE;
  3604. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3605. enhCbt = FlexCAN_IsEnhCbtEnabled(pBase);
  3606. if (TRUE == enhCbt)
  3607. {
  3608. FlexCAN_GetEnhancedDataTimeSegments(pBase, bitrate);
  3609. }
  3610. else
  3611. #endif
  3612. {
  3613. /* Get the time segments*/
  3614. FlexCAN_GetFDTimeSegments(pBase, bitrate);
  3615. }
  3616. return enhCbt;
  3617. }
  3618. /*FUNCTION**********************************************************************
  3619. *
  3620. * Function Name : FlexCAN_Ip_SetTDCOffset
  3621. * Description : Enables/Disables the Transceiver Delay Compensation feature and sets
  3622. * the Transceiver Delay Compensation Offset.
  3623. *
  3624. *END**************************************************************************/
  3625. /* implements FlexCAN_Ip_SetTDCOffset_Activity */
  3626. Flexcan_Ip_StatusType FlexCAN_Ip_SetTDCOffset_Privileged(uint8 instance, boolean enable, uint8 offset)
  3627. {
  3628. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3629. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3630. #endif
  3631. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3632. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3633. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3634. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3635. if (TRUE == disabled)
  3636. {
  3637. result = FlexCAN_Enable(pBase);
  3638. }
  3639. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3640. #if defined(CAN_FEATURE_S32K1XX)
  3641. /* Check if the instance support FD capability */
  3642. DevAssert(TRUE == FlexCAN_IsFDAvailable(pBase));
  3643. #endif
  3644. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  3645. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  3646. {
  3647. result = FLEXCAN_STATUS_ERROR;
  3648. }
  3649. #endif
  3650. if (FLEXCAN_STATUS_SUCCESS == result)
  3651. {
  3652. /* Check if enhaced CBT is Enabled */
  3653. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_16();
  3654. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCE_CBT == STD_ON)
  3655. /* End critical section: implementation depends on integrator */
  3656. if (FLEXCAN_CTRL2_BTE_MASK == (pBase->CTRL2 & FLEXCAN_CTRL2_BTE_MASK))
  3657. {
  3658. FlexCAN_SetEnhancedTDCOffset(pBase, enable, offset);
  3659. }
  3660. else
  3661. #endif
  3662. {
  3663. /* Enable/Disable TDC and set the TDC Offset */
  3664. FlexCAN_SetTDCOffset(pBase, enable, offset);
  3665. }
  3666. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_16();
  3667. /* Check if enhaced CBT is Enabled */
  3668. }
  3669. if (TRUE == disabled)
  3670. {
  3671. status = FlexCAN_Disable(pBase);
  3672. if (FLEXCAN_STATUS_SUCCESS != status)
  3673. {
  3674. result = status;
  3675. }
  3676. }
  3677. return result;
  3678. }
  3679. /*FUNCTION**********************************************************************
  3680. *
  3681. * Function Name : FlexCAN_Ip_SetTxArbitrationStartDelay
  3682. * Description : Set FlexCAN Tx Arbitration Start Delay.
  3683. * This function will set how many CAN bits the Tx arbitration process start point can
  3684. * be delayed from the first bit of CRC field on CAN bus.
  3685. *
  3686. *END**************************************************************************/
  3687. /* implements FlexCAN_Ip_SetTxArbitrationStartDelay_Activity */
  3688. Flexcan_Ip_StatusType FlexCAN_Ip_SetTxArbitrationStartDelay_Privileged(uint8 instance, uint8 value)
  3689. {
  3690. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3691. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3692. #endif
  3693. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3694. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3695. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3696. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3697. if (TRUE == disabled)
  3698. {
  3699. result = FlexCAN_Enable(pBase);
  3700. }
  3701. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3702. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  3703. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  3704. {
  3705. result = FLEXCAN_STATUS_ERROR;
  3706. }
  3707. #endif
  3708. if (FLEXCAN_STATUS_SUCCESS == result)
  3709. {
  3710. /* Start critical section: implementation depends on integrator */
  3711. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_17();
  3712. FlexCAN_SetTxArbitrationStartDelay(pBase, value);
  3713. /* End critical section: implementation depends on integrator */
  3714. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_17();
  3715. }
  3716. if (TRUE == disabled)
  3717. {
  3718. status = FlexCAN_Disable(pBase);
  3719. if (FLEXCAN_STATUS_SUCCESS != status)
  3720. {
  3721. result = status;
  3722. }
  3723. }
  3724. return result;
  3725. }
  3726. /*FUNCTION**********************************************************************
  3727. *
  3728. * Function Name : FlexCAN_Ip_GetBuffStatusFlag
  3729. * Description : Get FlexCAN Message Buffer Status Flag for an operation.
  3730. * In case of a complete operation this flag is set.
  3731. * In case msgBuff is 255 will return Enhanced Overflow Status Flag.
  3732. *END**************************************************************************/
  3733. /* implements FlexCAN_Ip_GetBuffStatusFlag_Activity */
  3734. boolean FlexCAN_Ip_GetBuffStatusFlag(uint8 instance, uint8 msgBuffIdx)
  3735. {
  3736. boolean returnResult = TRUE;
  3737. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3738. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3739. #endif
  3740. const FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3741. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3742. if (FLEXCAN_IP_MB_ENHANCED_RXFIFO == msgBuffIdx)
  3743. {
  3744. returnResult = ((1U == FlexCAN_GetEnhancedRxFIFOStatusFlag(pBase, FLEXCAN_IP_ENHANCED_RXFIFO_OVERFLOW)) ? TRUE : FALSE);
  3745. }
  3746. else
  3747. #endif
  3748. {
  3749. returnResult = ((1U == FlexCAN_GetBuffStatusFlag(pBase, msgBuffIdx)) ? TRUE : FALSE);
  3750. }
  3751. return returnResult;
  3752. }
  3753. /*FUNCTION**********************************************************************
  3754. *
  3755. * Function Name : FlexCAN_Ip_ClearBuffStatusFlag
  3756. * Description : Clear FlexCAN Message Buffer Status Flag.
  3757. * In case msgBuff is 255 will clear Enhanced Overflow Status Flag.
  3758. *END**************************************************************************/
  3759. /* implements FlexCAN_Ip_ClearBuffStatusFlag_Activity */
  3760. void FlexCAN_Ip_ClearBuffStatusFlag(uint8 instance, uint8 msgBuffIdx)
  3761. {
  3762. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3763. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  3764. #endif
  3765. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  3766. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3767. if (FLEXCAN_IP_MB_ENHANCED_RXFIFO == msgBuffIdx)
  3768. {
  3769. FlexCAN_ClearEnhancedRxFifoIntStatusFlag(pBase, FLEXCAN_IP_ENHANCED_RXFIFO_OVERFLOW);
  3770. }
  3771. else
  3772. #endif
  3773. {
  3774. FlexCAN_ClearMsgBuffIntStatusFlag(pBase, msgBuffIdx);
  3775. }
  3776. }
  3777. #endif
  3778. /*FUNCTION**********************************************************************
  3779. *
  3780. * Function Name : FlexCAN_Ip_EnableInterrupts
  3781. * Description : Enable all mb interrupts configured.
  3782. *
  3783. *END**************************************************************************/
  3784. /* implements FlexCAN_Ip_EnableInterrupts_Activity */
  3785. Flexcan_Ip_StatusType FlexCAN_Ip_EnableInterrupts_Privileged(uint8 u8Instance)
  3786. {
  3787. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3788. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_ERROR;
  3789. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  3790. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3791. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3792. #endif
  3793. if (TRUE == FlexCAN_IsEnabled(pBase))
  3794. {
  3795. FlexCAN_EnableInterrupts(pBase, u8Instance);
  3796. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3797. if ((FALSE == state->enhancedFifoOutput.isPolling) && (TRUE == FlexCAN_IsEnhancedRxFifoAvailable(pBase)))
  3798. { /* Check if Enabled Enhanced Fifo */
  3799. if (TRUE == FlexCAN_IsEnhancedRxFifoEnabled(pBase))
  3800. {
  3801. FlexCAN_SetEnhancedRxFifoIntAll(pBase, TRUE);
  3802. }
  3803. }
  3804. #endif
  3805. state->isIntActive = TRUE;
  3806. result = FLEXCAN_STATUS_SUCCESS;
  3807. }
  3808. return result;
  3809. }
  3810. /*FUNCTION**********************************************************************
  3811. *
  3812. * Function Name : FlexCAN_Ip_DisableInterrupts
  3813. * Description : Enable all interrupts configured.
  3814. *
  3815. *END**************************************************************************/
  3816. /* implements FlexCAN_Ip_DisableInterrupts_Activity */
  3817. Flexcan_Ip_StatusType FlexCAN_Ip_DisableInterrupts_Privileged(uint8 u8Instance)
  3818. {
  3819. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3820. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_ERROR;
  3821. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  3822. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3823. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3824. #endif
  3825. if (TRUE == FlexCAN_IsEnabled(pBase))
  3826. {
  3827. FlexCAN_DisableInterrupts(pBase);
  3828. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3829. if (TRUE == FlexCAN_IsEnhancedRxFifoAvailable(pBase))
  3830. { /* Check if Enabled Enhanced Fifo */
  3831. if (TRUE == FlexCAN_IsEnhancedRxFifoEnabled(pBase))
  3832. {
  3833. FlexCAN_SetEnhancedRxFifoIntAll(pBase, FALSE);
  3834. }
  3835. }
  3836. #endif
  3837. state->isIntActive = FALSE;
  3838. result = FLEXCAN_STATUS_SUCCESS;
  3839. }
  3840. return result;
  3841. }
  3842. /*FUNCTION**********************************************************************
  3843. *
  3844. * Function Name : FlexCAN_Ip_SetErrorInt
  3845. * Description : Enable\Disable Error or BusOff Interrupt
  3846. *
  3847. *END**************************************************************************/
  3848. /* implements FlexCAN_Ip_SetErrorInt_Activity */
  3849. Flexcan_Ip_StatusType FlexCAN_Ip_SetErrorInt_Privileged(uint8 u8Instance, Flexcan_Ip_ErrorIntType type, boolean enable)
  3850. {
  3851. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3852. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3853. #endif
  3854. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3855. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  3856. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3857. boolean disabled = !FlexCAN_IsEnabled(pBase);
  3858. if (TRUE == disabled)
  3859. {
  3860. result = FlexCAN_Enable(pBase);
  3861. }
  3862. if (FLEXCAN_STATUS_SUCCESS == result)
  3863. {
  3864. switch (type)
  3865. {
  3866. case FLEXCAN_IP_INT_BUSOFF:
  3867. {
  3868. FlexCAN_SetErrIntCmd(pBase, FLEXCAN_INT_BUSOFF, enable);
  3869. break;
  3870. }
  3871. case FLEXCAN_IP_INT_ERR:
  3872. {
  3873. FlexCAN_SetErrIntCmd(pBase, FLEXCAN_INT_ERR, enable);
  3874. break;
  3875. }
  3876. case FLEXCAN_IP_INT_ERR_FAST :
  3877. {
  3878. FlexCAN_SetErrIntCmd(pBase, FLEXCAN_INT_ERR_FAST, enable);
  3879. break;
  3880. }
  3881. case FLEXCAN_IP_INT_RX_WARNING :
  3882. {
  3883. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3884. if (TRUE != FlexCAN_IsFreezeMode(pBase))
  3885. {
  3886. result = FLEXCAN_STATUS_ERROR;
  3887. }else
  3888. #endif
  3889. {
  3890. FlexCAN_SetErrIntCmd(pBase, FLEXCAN_INT_RX_WARNING, enable);
  3891. }
  3892. break;
  3893. }
  3894. case FLEXCAN_IP_INT_TX_WARNING :
  3895. {
  3896. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3897. if (TRUE != FlexCAN_IsFreezeMode(pBase))
  3898. {
  3899. result = FLEXCAN_STATUS_ERROR;
  3900. }else
  3901. #endif
  3902. {
  3903. FlexCAN_SetErrIntCmd(pBase, FLEXCAN_INT_TX_WARNING, enable);
  3904. }
  3905. break;
  3906. }
  3907. default:
  3908. {
  3909. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3910. DevAssert(FALSE);
  3911. /* Should not get here */
  3912. #endif
  3913. break;
  3914. }
  3915. }
  3916. }
  3917. if (TRUE == disabled)
  3918. {
  3919. status = FlexCAN_Disable(pBase);
  3920. if (FLEXCAN_STATUS_SUCCESS != status)
  3921. {
  3922. result = status;
  3923. }
  3924. }
  3925. return result;
  3926. }
  3927. /*FUNCTION**********************************************************************
  3928. *
  3929. * Function Name : FlexCAN_Ip_AbortTransfer
  3930. * Description : This function shuts down the FLEXCAN by disabling interrupts and
  3931. * the transmitter/receiver.
  3932. * This function disables the FLEXCAN interrupts, disables the transmitter and
  3933. * receiver.
  3934. *
  3935. *END**************************************************************************/
  3936. /* implements FlexCAN_Ip_AbortTransfer_Activity */
  3937. Flexcan_Ip_StatusType FlexCAN_Ip_AbortTransfer(uint8 u8Instance, uint8 mb_idx)
  3938. {
  3939. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3940. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  3941. DevAssert((mb_idx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM) || (255u == mb_idx));
  3942. #endif
  3943. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  3944. #if ((FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) && (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON))
  3945. Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  3946. #else
  3947. const Flexcan_Ip_StateType * state = Flexcan_Ip_apxState[u8Instance];
  3948. #endif /* (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON) && (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON) */
  3949. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  3950. if (mb_idx < (uint8)FLEXCAN_IP_FEATURE_MAX_MB_NUM)
  3951. {
  3952. if (FLEXCAN_MB_IDLE == state->mbs[mb_idx].state)
  3953. {
  3954. result = FLEXCAN_STATUS_NO_TRANSFER_IN_PROGRESS;
  3955. }
  3956. else
  3957. {
  3958. FLEXCAN_ClearMsgBuffIntCmd(pBase, u8Instance, mb_idx, state->isIntActive);
  3959. if (FLEXCAN_MB_TX_BUSY == state->mbs[mb_idx].state)
  3960. {
  3961. result = FlexCAN_AbortTxTransfer(u8Instance, mb_idx);
  3962. }
  3963. if (FLEXCAN_MB_RX_BUSY == state->mbs[mb_idx].state)
  3964. {
  3965. FlexCAN_AbortRxTransfer(u8Instance, mb_idx);
  3966. }
  3967. }
  3968. }
  3969. #if (FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO == STD_ON)
  3970. #if (FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE == STD_ON)
  3971. else
  3972. {
  3973. /* Check if transfer is done over DMA and stop transfer */
  3974. if ((FLEXCAN_MB_RX_BUSY == state->enhancedFifoOutput.state) && (FLEXCAN_RXFIFO_USING_DMA == state->transferType))
  3975. {
  3976. (void)Dma_Ip_SetLogicChannelCommand(state->rxFifoDMAChannel, DMA_IP_CH_CLEAR_HARDWARE_REQUEST);
  3977. if (FLEXCAN_MB_RX_BUSY == state->enhancedFifoOutput.state)
  3978. {
  3979. state->enhancedFifoOutput.state = FLEXCAN_MB_IDLE;
  3980. }
  3981. else
  3982. {
  3983. result = FLEXCAN_STATUS_NO_TRANSFER_IN_PROGRESS;
  3984. }
  3985. }
  3986. else
  3987. {
  3988. result = FLEXCAN_STATUS_NO_TRANSFER_IN_PROGRESS;
  3989. }
  3990. }
  3991. #endif /* if FLEXCAN_IP_FEATURE_HAS_DMA_ENABLE */
  3992. #endif /* if FLEXCAN_IP_FEATURE_HAS_ENHANCED_RX_FIFO */
  3993. return result;
  3994. }
  3995. /* implements FlexCAN_Ip_SetRxMb14Mask_Activity */
  3996. Flexcan_Ip_StatusType FlexCAN_Ip_SetRxMb14Mask_Privileged(uint8 instance, uint32 mask)
  3997. {
  3998. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  3999. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  4000. #endif
  4001. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  4002. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  4003. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  4004. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4005. boolean freeze = FALSE;
  4006. #endif
  4007. boolean disabled = !FlexCAN_IsEnabled(pBase);
  4008. if (TRUE == disabled)
  4009. {
  4010. result = FlexCAN_Enable(pBase);
  4011. }
  4012. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4013. freeze = FlexCAN_IsFreezeMode(pBase);
  4014. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  4015. {
  4016. result = FLEXCAN_STATUS_ERROR;
  4017. }
  4018. #endif
  4019. if (FLEXCAN_STATUS_SUCCESS == result)
  4020. {
  4021. pBase->RX14MASK = mask;
  4022. }
  4023. if (TRUE == disabled)
  4024. {
  4025. status = FlexCAN_Disable(pBase);
  4026. if (FLEXCAN_STATUS_SUCCESS != status)
  4027. {
  4028. result = status;
  4029. }
  4030. }
  4031. return result;
  4032. }
  4033. /* implements FlexCAN_Ip_SetRxMb15Mask_Activity */
  4034. Flexcan_Ip_StatusType FlexCAN_Ip_SetRxMb15Mask_Privileged(uint8 instance, uint32 mask)
  4035. {
  4036. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4037. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  4038. #endif
  4039. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  4040. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  4041. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  4042. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4043. boolean freeze = FALSE;
  4044. #endif
  4045. boolean disabled = !FlexCAN_IsEnabled(pBase);
  4046. if (TRUE == disabled)
  4047. {
  4048. result = FlexCAN_Enable(pBase);
  4049. }
  4050. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4051. freeze = FlexCAN_IsFreezeMode(pBase);
  4052. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  4053. {
  4054. result = FLEXCAN_STATUS_ERROR;
  4055. }
  4056. #endif
  4057. if (FLEXCAN_STATUS_SUCCESS == result)
  4058. {
  4059. pBase->RX15MASK = mask;
  4060. }
  4061. if (TRUE == disabled)
  4062. {
  4063. status = FlexCAN_Disable(pBase);
  4064. if (FLEXCAN_STATUS_SUCCESS != status)
  4065. {
  4066. result = status;
  4067. }
  4068. }
  4069. return result;
  4070. }
  4071. /*FUNCTION**********************************************************************
  4072. *
  4073. * Function Name : FlexCAN_Ip_SetListenOnlyMode
  4074. * Description : Set FlexCAN Listen Only.
  4075. * This function will enable or disable Listen Only mode.
  4076. *
  4077. *END**************************************************************************/
  4078. /* implements FlexCAN_Ip_SetListenOnlyMode_Activity */
  4079. Flexcan_Ip_StatusType FlexCAN_Ip_SetListenOnlyMode_Privileged(uint8 instance, const boolean enable)
  4080. {
  4081. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4082. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  4083. #endif
  4084. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  4085. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  4086. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  4087. boolean disabled = !FlexCAN_IsEnabled(pBase);
  4088. if (TRUE == disabled)
  4089. {
  4090. result = FlexCAN_Enable(pBase);
  4091. }
  4092. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4093. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  4094. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  4095. {
  4096. result = FLEXCAN_STATUS_ERROR;
  4097. }
  4098. #endif
  4099. if (FLEXCAN_STATUS_SUCCESS == result)
  4100. {
  4101. /* Start critical section: implementation depends on integrator */
  4102. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_10();
  4103. FlexCAN_SetListenOnlyMode(pBase, enable);
  4104. /* End critical section: implementation depends on integrator */
  4105. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_10();
  4106. }
  4107. if (TRUE == disabled)
  4108. {
  4109. status = FlexCAN_Disable(pBase);
  4110. if (FLEXCAN_STATUS_SUCCESS != status)
  4111. {
  4112. result = status;
  4113. }
  4114. }
  4115. return result;
  4116. }
  4117. /*FUNCTION**********************************************************************
  4118. *
  4119. * Function Name : FlexCAN_Ip_GetListenOnlyMode
  4120. * Description : Check if Listen Only mode is ENABLE.
  4121. *
  4122. *END**************************************************************************/
  4123. /* implements FlexCAN_Ip_GetListenOnlyMode_Activity */
  4124. boolean FlexCAN_Ip_GetListenOnlyMode(uint8 instance)
  4125. {
  4126. const FLEXCAN_Type * base = Flexcan_Ip_apxBase[instance];
  4127. return FlexCAN_IsListenOnlyModeEnabled(base);
  4128. }
  4129. #if (FLEXCAN_IP_FEATURE_HAS_TS_ENABLE == STD_ON)
  4130. /*FUNCTION**********************************************************************
  4131. *
  4132. * Function Name : FlexCAN_Ip_ConfigTimeStamp_Privileged
  4133. * Description : Timestamp configuration
  4134. *
  4135. *END**************************************************************************/
  4136. /* implements FlexCAN_Ip_ConfigTimeStamp_Activity */
  4137. Flexcan_Ip_StatusType FlexCAN_Ip_ConfigTimeStamp_Privileged(uint8 instance, const Flexcan_Ip_TimeStampConfigType * time_stamp)
  4138. {
  4139. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4140. DevAssert(instance < FLEXCAN_INSTANCE_COUNT);
  4141. #endif
  4142. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  4143. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  4144. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[instance];
  4145. boolean disabled = !FlexCAN_IsEnabled(pBase);
  4146. if (TRUE == disabled)
  4147. {
  4148. result = FlexCAN_Enable(pBase);
  4149. }
  4150. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4151. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  4152. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  4153. {
  4154. result = FLEXCAN_STATUS_ERROR;
  4155. }
  4156. #endif
  4157. if (FLEXCAN_STATUS_SUCCESS == result)
  4158. {
  4159. FlexCAN_ConfigTimestamp(pBase, time_stamp);
  4160. }
  4161. if (TRUE == disabled)
  4162. {
  4163. status = FlexCAN_Disable(pBase);
  4164. if (FLEXCAN_STATUS_SUCCESS != status)
  4165. {
  4166. result = status;
  4167. }
  4168. }
  4169. return result;
  4170. }
  4171. #endif /* FLEXCAN_IP_FEATURE_HAS_TS_ENABLE */
  4172. #if (FLEXCAN_IP_FEATURE_HAS_PRETENDED_NETWORKING == STD_ON)
  4173. /*FUNCTION**********************************************************************
  4174. *
  4175. * Function Name : FlexCAN_Ip_ConfigPN
  4176. * Description : Configures Pretended Networking settings.
  4177. *
  4178. *END**************************************************************************/
  4179. /* implements FlexCAN_Ip_ConfigPN_Activity */
  4180. Flexcan_Ip_StatusType FlexCAN_Ip_ConfigPN_Privileged(uint8 u8Instance,
  4181. boolean bEnable,
  4182. const Flexcan_Ip_PnConfigType * pPnConfig)
  4183. {
  4184. Flexcan_Ip_StatusType result = FLEXCAN_STATUS_SUCCESS;
  4185. Flexcan_Ip_StatusType status = FLEXCAN_STATUS_SUCCESS;
  4186. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  4187. boolean disabled = !FlexCAN_IsEnabled(pBase);
  4188. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4189. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  4190. DevAssert((FALSE == bEnable) || (pPnConfig != NULL_PTR));
  4191. #endif
  4192. if (0U == u8Instance)
  4193. {
  4194. if (TRUE == disabled)
  4195. {
  4196. result = FlexCAN_Enable(pBase);
  4197. }
  4198. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4199. boolean freeze = FlexCAN_IsFreezeMode(pBase);
  4200. if ((FALSE == freeze) && (FLEXCAN_STATUS_SUCCESS == result))
  4201. {
  4202. result = FLEXCAN_STATUS_ERROR;
  4203. }
  4204. #endif
  4205. if (FLEXCAN_STATUS_SUCCESS == result)
  4206. {
  4207. if (bEnable)
  4208. {
  4209. FlexCAN_ConfigPN(pBase, pPnConfig);
  4210. }
  4211. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_12();
  4212. FlexCAN_SetPN(pBase, bEnable);
  4213. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_12();
  4214. }
  4215. if (TRUE == disabled)
  4216. {
  4217. status = FlexCAN_Disable(pBase);
  4218. if (FLEXCAN_STATUS_SUCCESS != status)
  4219. {
  4220. result = status;
  4221. }
  4222. }
  4223. }
  4224. else
  4225. {
  4226. result = FLEXCAN_STATUS_ERROR;
  4227. }
  4228. return result;
  4229. }
  4230. /*FUNCTION**********************************************************************
  4231. *
  4232. * Function Name : FlexCAN_Ip_GetWMB
  4233. * Description : Extracts one of the frames which triggered the wake up event.
  4234. *
  4235. *END**************************************************************************/
  4236. /* implements FlexCAN_Ip_GetWMB_Activity */
  4237. void FlexCAN_Ip_GetWMB(uint8 u8Instance,
  4238. uint8 u8WmbIndex,
  4239. Flexcan_Ip_MsgBuffType * pWmb)
  4240. {
  4241. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4242. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  4243. DevAssert(pWmb != NULL_PTR);
  4244. DevAssert(0U == u8Instance);
  4245. #endif
  4246. const FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  4247. uint32 * pTmp, pWmbData;
  4248. pTmp = (uint32 *)&pWmb->data[0];
  4249. pWmbData = pBase->WMB[u8WmbIndex].WMBn_D03;
  4250. FLEXCAN_IP_SWAP_BYTES_IN_WORD(pWmbData, *pTmp);
  4251. pTmp = (uint32 *)&pWmb->data[4];
  4252. pWmbData = pBase->WMB[u8WmbIndex].WMBn_D47;
  4253. FLEXCAN_IP_SWAP_BYTES_IN_WORD(pWmbData, *pTmp);
  4254. pWmb->cs = pBase->WMB[u8WmbIndex].WMBn_CS;
  4255. if ((pWmb->cs & FLEXCAN_IP_CS_IDE_MASK) != 0U)
  4256. {
  4257. pWmb->msgId = pBase->WMB[u8WmbIndex].WMBn_ID;
  4258. }
  4259. else
  4260. {
  4261. pWmb->msgId = pBase->WMB[u8WmbIndex].WMBn_ID >> FLEXCAN_IP_ID_STD_SHIFT;
  4262. }
  4263. pWmb->dataLen = (uint8)((pWmb->cs & FLEXCAN_IP_CS_DLC_MASK) >> 16U);
  4264. }
  4265. /*FUNCTION**********************************************************************
  4266. *
  4267. * Function Name : FlexCAN_WakeUp_IRQHandler
  4268. * Description : Wake up handler for FLEXCAN.
  4269. * This handler verifies the event which caused the wake up and invokes the
  4270. * user callback, if configured.
  4271. * This is not a public API as it is called whenever an wake up event occurs.
  4272. *
  4273. *END**************************************************************************/
  4274. void FlexCAN_WakeUp_IRQHandler(uint8 u8Instance)
  4275. {
  4276. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4277. DevAssert(u8Instance < FLEXCAN_INSTANCE_COUNT);
  4278. #endif
  4279. FLEXCAN_Type * pBase = Flexcan_Ip_apxBase[u8Instance];
  4280. const Flexcan_Ip_StateType * pState = Flexcan_Ip_apxState[u8Instance];
  4281. if (NULL_PTR != pState)
  4282. {
  4283. if (FlexCAN_GetWTOF(pBase) != 0U)
  4284. {
  4285. FlexCAN_ClearWTOF(pBase);
  4286. /* Invoke callback */
  4287. if ((0U != FlexCAN_GetWTOIE(pBase)) && (pState->callback != NULL_PTR))
  4288. {
  4289. pState->callback(u8Instance, FLEXCAN_EVENT_WAKEUP_TIMEOUT, 0U, pState);
  4290. }
  4291. }
  4292. if (FlexCAN_GetWUMF(pBase) != 0U)
  4293. {
  4294. FlexCAN_ClearWUMF(pBase);
  4295. /* Invoke callback */
  4296. if ((0U != FlexCAN_GetWUMIE(pBase)) && (pState->callback != NULL_PTR))
  4297. {
  4298. pState->callback(u8Instance, FLEXCAN_EVENT_WAKEUP_MATCH, 0U, pState);
  4299. }
  4300. }
  4301. }
  4302. else
  4303. {
  4304. FlexCAN_ClearWTOF(pBase);
  4305. FlexCAN_ClearWUMF(pBase);
  4306. }
  4307. }
  4308. #endif /* FLEXCAN_IP_FEATURE_HAS_PRETENDED_NETWORKING */
  4309. /*FUNCTION**********************************************************************
  4310. *
  4311. * Function Name : FlexCAN_Ip_ManualBusOffRecovery
  4312. * Description : Recover manually from bus-off if possible.
  4313. *
  4314. *END**************************************************************************/
  4315. /* implements FlexCAN_Ip_ManualBusOffRecovery_Activity */
  4316. Flexcan_Ip_StatusType FlexCAN_Ip_ManualBusOffRecovery(uint8 Instance)
  4317. {
  4318. FLEXCAN_Type * Base = Flexcan_Ip_apxBase[Instance];
  4319. uint32 timeStart = 0U;
  4320. uint32 timeElapsed = 0U;
  4321. uint32 uS2Ticks = OsIf_MicrosToTicks(FLEXCAN_IP_TIMEOUT_DURATION, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  4322. Flexcan_Ip_StatusType RetVal = FLEXCAN_STATUS_ERROR;
  4323. #if (FLEXCAN_IP_DEV_ERROR_DETECT == STD_ON)
  4324. DevAssert(Instance < FLEXCAN_INSTANCE_COUNT);
  4325. #endif
  4326. /* Recover from bus-off when Automatic recovering from Bus Off state disabled. */
  4327. if ((Base->CTRL1 & FLEXCAN_CTRL1_BOFFREC_MASK) != 0U)
  4328. {
  4329. RetVal = FLEXCAN_STATUS_SUCCESS;
  4330. /* return success if the controller is not in bus-off */
  4331. if ((Base->ESR1 & FLEXCAN_IP_ESR1_FLTCONF_BUS_OFF) != 0U)
  4332. {
  4333. SchM_Enter_Can_CAN_EXCLUSIVE_AREA_20();
  4334. /* negate to recover from bus-off */
  4335. Base->CTRL1 &= ~FLEXCAN_CTRL1_BOFFREC_MASK;
  4336. /* re-assert to disable bus-off auto reocvery */
  4337. Base->CTRL1 |= FLEXCAN_CTRL1_BOFFREC_MASK;
  4338. SchM_Exit_Can_CAN_EXCLUSIVE_AREA_20();
  4339. /* Wait till exit bus-off */
  4340. timeStart = OsIf_GetCounter(FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  4341. while ((Base->ESR1 & FLEXCAN_IP_ESR1_FLTCONF_BUS_OFF) != 0U)
  4342. {
  4343. timeElapsed += OsIf_GetElapsed(&timeStart, FLEXCAN_IP_SERVICE_TIMEOUT_TYPE);
  4344. if (timeElapsed >= uS2Ticks)
  4345. {
  4346. RetVal = FLEXCAN_STATUS_TIMEOUT;
  4347. break;
  4348. }
  4349. }
  4350. }
  4351. }
  4352. return RetVal;
  4353. }
  4354. #define CAN_STOP_SEC_CODE
  4355. #include "Can_MemMap.h"
  4356. #ifdef __cplusplus
  4357. }
  4358. #endif
  4359. /** @} */