1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197 |
- /**********************************************************************
- * $Id$ lpc_can.c 2011-06-02
- *//**
- * @file lpc_can.c
- * @brief Contains all functions support for CAN firmware library on
- * LPC
- * @version 1.0
- * @date 02. June. 2011
- * @author NXP MCU SW Application Team
- *
- * Copyright(C) 2011, NXP Semiconductor
- * All rights reserved.
- *
- ***********************************************************************
- * Software that is described herein is for illustrative purposes only
- * which provides customers with programming information regarding the
- * products. This software is supplied "AS IS" without any warranties.
- * NXP Semiconductors assumes no responsibility or liability for the
- * use of the software, conveys no license or title under any patent,
- * copyright, or mask work right to the product. NXP Semiconductors
- * reserves the right to make changes in the software without
- * notification. NXP Semiconductors also make no representation or
- * warranty that such application will be suitable for the specified
- * use without further testing or modification.
- * Permission to use, copy, modify, and distribute this software and its
- * documentation is hereby granted, under NXP Semiconductors'
- * relevant copyright in the software, without fee, provided that it
- * is used in conjunction with NXP Semiconductors microcontrollers. This
- * copyright, permission, and disclaimer notice must appear in all copies of
- * this code.
- **********************************************************************/
- /* Peripheral group ----------------------------------------------------------- */
- /** @addtogroup CAN
- * @{
- */
- #ifdef __BUILD_WITH_EXAMPLE__
- #include "lpc_libcfg.h"
- #else
- #include "lpc_libcfg_default.h"
- #endif /* __BUILD_WITH_EXAMPLE__ */
- #ifdef _CAN
-
- /* Includes ------------------------------------------------------------------- */
- #include "lpc_can.h"
- #include "lpc_clkpwr.h"
- /* Private Variables ---------------------------------------------------------- */
- /** @defgroup CAN_Private_Variables CAN Private Variables
- * @{
- */
- FunctionalState FULLCAN_ENABLE;
- /* Counts number of filters (CAN message objects) used */
- uint16_t CANAF_FullCAN_cnt = 0;
- uint16_t CANAF_std_cnt = 0;
- uint16_t CANAF_gstd_cnt = 0;
- uint16_t CANAF_ext_cnt = 0;
- uint16_t CANAF_gext_cnt = 0;
- /* End of Private Variables ----------------------------------------------------*/
- /**
- * @}
- */
- /* Private Variables ---------------------------------------------------------- */
- static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId);
- static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate);
- /*********************************************************************//**
- * @brief Setting CAN baud rate (bps)
- * @param[in] canId point to LPC_CAN_TypeDef object, should be:
- * - LPC_CAN1: CAN1 peripheral
- * - LPC_CAN2: CAN2 peripheral
- * @return The pointer to CAN peripheral that's expected to use
- ***********************************************************************/
- static LPC_CAN_TypeDef* CAN_GetPointer (uint8_t canId)
- {
- LPC_CAN_TypeDef* pCan;
- switch (canId)
- {
- case CAN_ID_1:
- pCan = LPC_CAN1;
- break;
- case CAN_ID_2:
- pCan = LPC_CAN2;
- break;
- default:
- pCan = NULL;
- break;
- }
- return pCan;
- }
- /*********************************************************************//**
- * @brief Setting CAN baud rate (bps)
- * @param[in] CANx point to LPC_CAN_TypeDef object, should be:
- * - LPC_CAN1: CAN1 peripheral
- * - LPC_CAN2: CAN2 peripheral
- * @param[in] baudrate: is the baud rate value will be set
- * @return None
- ***********************************************************************/
- static void can_SetBaudrate (LPC_CAN_TypeDef *CANx, uint32_t baudrate)
- {
- uint32_t result = 0;
- uint8_t NT, TSEG1, TSEG2;
- uint32_t CANPclk = 0;
- uint32_t BRP;
- CANPclk = CLKPWR_GetCLK(CLKPWR_CLKTYPE_PER);
- result = CANPclk / baudrate;
- /* Calculate suitable nominal time value
- * NT (nominal time) = (TSEG1 + TSEG2 + 3)
- * NT <= 24
- * TSEG1 >= 2*TSEG2
- */
- for(NT = 24; NT > 0; NT = NT-2)
- {
- if ((result%NT) == 0)
- {
- BRP = result / NT - 1;
- NT--;
- TSEG2 = (NT/3) - 1;
- TSEG1 = NT -(NT/3) - 1;
- break;
- }
- }
- /* Enter reset mode */
- CANx->MOD = 0x01;
- /* Set bit timing
- * Default: SAM = 0x00;
- * SJW = 0x03;
- */
- CANx->BTR = (TSEG2 << 20) | (TSEG1 << 16) | (3 << 14) | BRP;
- /* Return to normal operating */
- CANx->MOD = 0;
- }
- /* End of Private Functions ----------------------------------------------------*/
- /* Public Functions ----------------------------------------------------------- */
- /** @addtogroup CAN_Public_Functions
- * @{
- */
- /********************************************************************//**
- * @brief Initialize CAN peripheral with given baudrate
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] baudrate: the value of CAN baudrate will be set (bps)
- * @return None
- *********************************************************************/
- void CAN_Init(uint8_t canId, uint32_t baudrate)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- uint16_t i;
- if(canId == CAN_ID_1)
- {
- /* Turn on power and clock for CAN1 */
- CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, ENABLE);
- }
- else if(canId == CAN_ID_2)
- {
- /* Turn on power and clock for CAN2 */
- CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, ENABLE);
- }
- else
- {
- return;
- }
- pCan->MOD = 1; // Enter Reset Mode
- pCan->IER = 0; // Disable All CAN Interrupts
- pCan->GSR = 0;
- /* Request command to release Rx, Tx buffer and clear data overrun */
- //pCan->CMR = CAN_CMR_AT | CAN_CMR_RRB | CAN_CMR_CDO;
- pCan->CMR = (1 << 1) | (1 << 2) | (1 << 3);
- /* Read to clear interrupt pending in interrupt capture register */
- i = pCan->ICR;
- pCan->MOD = 0;// Return Normal operating
- //Reset CANAF value
- LPC_CANAF->AFMR = 0x01;
- //clear ALUT RAM
- for (i = 0; i < 512; i++)
- {
- LPC_CANAF_RAM->mask[i] = 0x00;
- }
- LPC_CANAF->SFF_sa = 0x00;
- LPC_CANAF->SFF_GRP_sa = 0x00;
- LPC_CANAF->EFF_sa = 0x00;
- LPC_CANAF->EFF_GRP_sa = 0x00;
- LPC_CANAF->ENDofTable = 0x00;
- LPC_CANAF->AFMR = 0x00;
- /* Set baudrate */
- can_SetBaudrate (pCan, baudrate);
- }
- /********************************************************************//**
- * @brief CAN deInit
- * @param[in] canId The Id of the expected CAN component
- *
- * @return None
- *********************************************************************/
- void CAN_DeInit(uint8_t canId)
- {
- if(canId == CAN_ID_1)
- {
- /* Turn on power and clock for CAN1 */
- CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN1, DISABLE);
- }
- else if(canId == CAN_ID_2)
- {
- /* Turn on power and clock for CAN1 */
- CLKPWR_ConfigPPWR(CLKPWR_PCONP_PCAN2, DISABLE);
- }
- return;
- }
- /********************************************************************//**
- * @brief Setup Acceptance Filter Look-Up Table
- * @param[in] CANAFx pointer to LPC_CANAF_TypeDef
- * Should be: LPC_CANAF
- * @param[in] AFSection the pointer to AF_SectionDef structure
- * It contain information about 5 sections will be install in AFLUT
- * @return CAN Error could be:
- * - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
- * - CAN_AF_ENTRY_ERROR: table error-violation of ascending numerical order
- * - CAN_OK: ID is added into table successfully
- *********************************************************************/
- CAN_ERROR CAN_SetupAFLUT(AF_SectionDef* AFSection)
- {
- uint8_t ctrl1,ctrl2;
- uint8_t dis1, dis2;
- uint16_t SID, ID_temp,i, count = 0;
- uint32_t EID, entry, buf;
- uint16_t lowerSID, upperSID;
- uint32_t lowerEID, upperEID;
- LPC_CANAF->AFMR = 0x01;
- /***** setup FullCAN Table *****/
- if(AFSection->FullCAN_Sec == NULL)
- {
- FULLCAN_ENABLE = DISABLE;
- }
- else
- {
- FULLCAN_ENABLE = ENABLE;
- for(i = 0; i < (AFSection->FC_NumEntry); i++)
- {
- if(count + 1 > 64)
- {
- return CAN_OBJECTS_FULL_ERROR;
- }
- ctrl1 = AFSection->FullCAN_Sec->controller;
- SID = AFSection->FullCAN_Sec->id_11;
- dis1 = AFSection->FullCAN_Sec->disable;
- entry = 0x00; //reset entry value
- if((CANAF_FullCAN_cnt & 0x00000001)==0)
- {
- if(count != 0x00)
- {
- buf = LPC_CANAF_RAM->mask[count-1];
- ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
- if(ID_temp > ((ctrl1<<13)|SID))
- {
- return CAN_AF_ENTRY_ERROR;
- }
- }
- entry = (ctrl1<<29)|(dis1<<28)|(SID<<16)|(1<<27);
- LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;
- LPC_CANAF_RAM->mask[count] |= entry;
- CANAF_FullCAN_cnt++;
- if(CANAF_FullCAN_cnt == AFSection->FC_NumEntry) //this is the lastest FullCAN entry
- count++;
- }
- else
- {
- buf = LPC_CANAF_RAM->mask[count];
- ID_temp = (buf >>16) & 0xE7FF;
- if(ID_temp > ((ctrl1<<13)|SID))
- {
- return CAN_AF_ENTRY_ERROR;
- }
- entry = (ctrl1 << 13) | (dis1 << 12) | (SID << 0) | (1 << 11);
- LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;
- LPC_CANAF_RAM->mask[count]|= entry;
- count++;
- CANAF_FullCAN_cnt++;
- }
- AFSection->FullCAN_Sec = (FullCAN_Entry *)((uint32_t)(AFSection->FullCAN_Sec)+ sizeof(FullCAN_Entry));
- }
- }
- /***** Setup Explicit Standard Frame Format Section *****/
- if(AFSection->SFF_Sec != NULL)
- {
- for(i=0;i<(AFSection->SFF_NumEntry);i++)
- {
- if(count + 1 > 512)
- {
- return CAN_OBJECTS_FULL_ERROR;
- }
- ctrl1 = AFSection->SFF_Sec->controller;
- SID = AFSection->SFF_Sec->id_11;
- dis1 = AFSection->SFF_Sec->disable;
- entry = 0x00; //reset entry value
- if((CANAF_std_cnt & 0x00000001)==0)
- {
- if(CANAF_std_cnt !=0 )
- {
- buf = LPC_CANAF_RAM->mask[count-1];
- ID_temp = (buf & 0xE7FF); //mask controller & identifier bits
- if(ID_temp > ((ctrl1<<13)|SID))
- {
- return CAN_AF_ENTRY_ERROR;
- }
- }
- entry = (ctrl1<<29)|(dis1<<28)|(SID<<16);
- LPC_CANAF_RAM->mask[count] &= 0x0000FFFF;
- LPC_CANAF_RAM->mask[count] |= entry;
- CANAF_std_cnt++;
- if(CANAF_std_cnt == AFSection->SFF_NumEntry)//if this is the last SFF entry
- count++;
- }
- else
- {
- buf = LPC_CANAF_RAM->mask[count];
- ID_temp = (buf >>16) & 0xE7FF;
- if(ID_temp > ((ctrl1<<13)|SID))
- {
- return CAN_AF_ENTRY_ERROR;
- }
- entry = (ctrl1 << 13) | (dis1 << 12) | (SID << 0);
- LPC_CANAF_RAM->mask[count] &= 0xFFFF0000;
- LPC_CANAF_RAM->mask[count] |= entry;
- count++;
- CANAF_std_cnt++;
- }
- AFSection->SFF_Sec = (SFF_Entry *)((uint32_t)(AFSection->SFF_Sec)+ sizeof(SFF_Entry));
- }
- }
- /***** Setup Group of Standard Frame Format Identifier Section *****/
- if(AFSection->SFF_GPR_Sec != NULL)
- {
- for(i=0;i<(AFSection->SFF_GPR_NumEntry);i++)
- {
- if(count + 1 > 512)
- {
- return CAN_OBJECTS_FULL_ERROR;
- }
- ctrl1 = AFSection->SFF_GPR_Sec->controller1;
- ctrl2 = AFSection->SFF_GPR_Sec->controller2;
- dis1 = AFSection->SFF_GPR_Sec->disable1;
- dis2 = AFSection->SFF_GPR_Sec->disable2;
- lowerSID = AFSection->SFF_GPR_Sec->lowerID;
- upperSID = AFSection->SFF_GPR_Sec->upperID;
- entry = 0x00;
- if(CANAF_gstd_cnt!=0)
- {
- buf = LPC_CANAF_RAM->mask[count-1];
- ID_temp = buf & 0xE7FF;
- if((ctrl1 != ctrl2)||(lowerSID > upperSID)||(ID_temp > ((ctrl1<<13)|lowerSID)))
- {
- return CAN_AF_ENTRY_ERROR;
- }
- }
- entry = (ctrl1 << 29)|(dis1 << 28)|(lowerSID << 16)| \
- (ctrl2 << 13)|(dis2 << 12)|(upperSID << 0);
- LPC_CANAF_RAM->mask[count] = entry;
- CANAF_gstd_cnt++;
- count++;
- AFSection->SFF_GPR_Sec = (SFF_GPR_Entry *)((uint32_t)(AFSection->SFF_GPR_Sec)+ sizeof(SFF_GPR_Entry));
- }
- }
- /***** Setup Explicit Extend Frame Format Identifier Section *****/
- if(AFSection->EFF_Sec != NULL)
- {
- for(i=0;i<(AFSection->EFF_NumEntry);i++)
- {
- if(count + 1 > 512)
- {
- return CAN_OBJECTS_FULL_ERROR;
- }
- EID = AFSection->EFF_Sec->ID_29;
- ctrl1 = AFSection->EFF_Sec->controller;
- entry = 0x00; //reset entry value
- entry = (ctrl1 << 29)|(EID << 0);
- if(CANAF_ext_cnt != 0)
- {
- buf = LPC_CANAF_RAM->mask[count-1];
- // EID_temp = buf & 0x0FFFFFFF;
- if(buf > entry)
- {
- return CAN_AF_ENTRY_ERROR;
- }
- }
- LPC_CANAF_RAM->mask[count] = entry;
- CANAF_ext_cnt ++;
- count++;
- AFSection->EFF_Sec = (EFF_Entry *)((uint32_t)(AFSection->EFF_Sec)+ sizeof(EFF_Entry));
- }
- }
- /***** Setup Group of Extended Frame Format Identifier Section *****/
- if(AFSection->EFF_GPR_Sec != NULL)
- {
- for(i=0;i<(AFSection->EFF_GPR_NumEntry);i++)
- {
- if(count + 2 > 512)
- {
- return CAN_OBJECTS_FULL_ERROR;
- }
- ctrl1 = AFSection->EFF_GPR_Sec->controller1;
- ctrl2 = AFSection->EFF_GPR_Sec->controller2;
- lowerEID = AFSection->EFF_GPR_Sec->lowerEID;
- upperEID = AFSection->EFF_GPR_Sec->upperEID;
- entry = 0x00;
- if(CANAF_gext_cnt != 0)
- {
- buf = LPC_CANAF_RAM->mask[count-1];
- // EID_temp = buf & 0x0FFFFFFF;
- if((ctrl1 != ctrl2) || (lowerEID > upperEID) || (buf > ((ctrl1 << 29)|(lowerEID << 0))))
- {
- return CAN_AF_ENTRY_ERROR;
- }
- }
- entry = (ctrl1 << 29)|(lowerEID << 0);
- LPC_CANAF_RAM->mask[count++] = entry;
- entry = (ctrl2 << 29)|(upperEID << 0);
- LPC_CANAF_RAM->mask[count++] = entry;
- CANAF_gext_cnt++;
- AFSection->EFF_GPR_Sec = (EFF_GPR_Entry *)((uint32_t)(AFSection->EFF_GPR_Sec)+ sizeof(EFF_GPR_Entry));
- }
- }
- //update address values
- LPC_CANAF->SFF_sa = ((CANAF_FullCAN_cnt + 1)>>1)<<2;
- LPC_CANAF->SFF_GRP_sa = LPC_CANAF->SFF_sa + (((CANAF_std_cnt+1)>>1)<< 2);
- LPC_CANAF->EFF_sa = LPC_CANAF->SFF_GRP_sa + (CANAF_gstd_cnt << 2);
- LPC_CANAF->EFF_GRP_sa = LPC_CANAF->EFF_sa + (CANAF_ext_cnt << 2);
- LPC_CANAF->ENDofTable = LPC_CANAF->EFF_GRP_sa + (CANAF_gext_cnt << 3);
- if(FULLCAN_ENABLE == DISABLE)
- {
- LPC_CANAF->AFMR = 0x00; // Normal mode
- }
- else
- {
- LPC_CANAF->AFMR = 0x04;
- }
- return CAN_OK;
- }
- /********************************************************************//**
- * @brief Add Explicit ID into AF Look-Up Table dynamically.
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] id: The ID of entry will be added
- * @param[in] format: is the type of ID Frame Format, should be:
- * - STD_ID_FORMAT: 11-bit ID value
- * - EXT_ID_FORMAT: 29-bit ID value
- * @return CAN Error, could be:
- * - CAN_OBJECTS_FULL_ERROR: No more rx or tx objects available
- * - CAN_ID_EXIT_ERROR: ID exited in table
- * - CAN_OK: ID is added into table successfully
- *********************************************************************/
- CAN_ERROR CAN_LoadExplicitEntry(uint8_t canId, uint32_t id, CAN_ID_FORMAT_Type format)
- {
- uint32_t buf0 = 0, buf1 = 0;
- int16_t cnt1 = 0, cnt2 = 0, bound1 = 0, total = 0;
- /* Acceptance Filter Memory full - return */
- total =((CANAF_FullCAN_cnt + 1) >> 1) + CANAF_FullCAN_cnt * 3 + ((CANAF_std_cnt + 1) >> 1) \
- + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);
- if (total >= 512)
- {
- //don't have enough space
- return CAN_OBJECTS_FULL_ERROR;
- }
- /* Setup Acceptance Filter Configuration
- Acceptance Filter Mode Register = Off */
- LPC_CANAF->AFMR = 0x00000001;
- /*********** Add Explicit Standard Identifier Frame Format entry *********/
- if(format == STD_ID_FORMAT)
- {
- id &= 0x07FF;
- id |= canId << 13;/* Add controller number */
- /* Move all remaining sections one place up
- if new entry will increase FullCAN list */
- if ((CANAF_std_cnt & 0x0001) == 0)
- {
- cnt1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
- bound1 = total - cnt1;
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- while(bound1--)
- {
- cnt1++;
- buf1 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = buf0;
- buf0 = buf1;
- }
- }
- if (CANAF_std_cnt == 0)
- {
- cnt2 = (CANAF_FullCAN_cnt + 1)>>1;
- /* For entering first ID */
- LPC_CANAF_RAM->mask[cnt2] = 0x0000FFFF | (id << 16);
- }
- else if (CANAF_std_cnt == 1)
- {
- cnt2 = (CANAF_FullCAN_cnt + 1) >> 1;
- /* For entering second ID */
- if (((LPC_CANAF_RAM->mask[cnt2] >> 16)& 0xE7FF) > id)
- {
- LPC_CANAF_RAM->mask[cnt2] = (LPC_CANAF_RAM->mask[cnt2] >> 16) | (id << 16);
- }
- else
- {
- LPC_CANAF_RAM->mask[cnt2] = (LPC_CANAF_RAM->mask[cnt2] & 0xFFFF0000) | id;
- }
- }
- else
- {
- /* Find where to insert new ID */
- cnt1 = (CANAF_FullCAN_cnt+1)>>1;
- cnt2 = CANAF_std_cnt;
- bound1 = ((CANAF_FullCAN_cnt+1)>>1)+((CANAF_std_cnt+1)>>1);
- while (cnt1 < bound1)
- {
- /* Loop through standard existing IDs */
- if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > id)
- {
- cnt2 = cnt1 * 2;
- break;
- }
- if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > id)
- {
- cnt2 = cnt1 * 2 + 1;
- break;
- }
- cnt1++;
- }
- /* cnt1 = U32 where to insert new ID */
- /* cnt2 = U16 where to insert new ID */
- if (cnt1 == bound1)
- {
- /* Adding ID as last entry */
- /* Even number of IDs exists */
- if ((CANAF_std_cnt & 0x0001) == 0)
- {
- LPC_CANAF_RAM->mask[cnt1] = 0x0000FFFF | (id << 16);
- }
- /* Odd number of IDs exists */
- else
- {
- LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id;
- }
- }
- else
- {
- buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */
- if ((cnt2 & 0x0001) == 0)
- {
- /* Insert new mask to even address*/
- buf1 = (id << 16) | (buf0 >> 16);
- }
- else
- {
- /* Insert new mask to odd address */
- buf1 = (buf0 & 0xFFFF0000) | id;
- }
- LPC_CANAF_RAM->mask[cnt1] = buf1;/* Insert mask */
- bound1 = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt+1) >> 1) - 1;
- /* Move all remaining standard mask entries one place up */
- while (cnt1 < bound1)
- {
- cnt1++;
- buf1 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16);
- buf0 = buf1;
- }
- if ((CANAF_std_cnt & 0x0001) == 0)
- {
- /* Even number of IDs exists */
- LPC_CANAF_RAM->mask[cnt1+1] = (buf0 <<16) |(0x0000FFFF);
- }
- }
- }
- CANAF_std_cnt++;
- //update address values
- LPC_CANAF->SFF_GRP_sa += 0x04 ;
- LPC_CANAF->EFF_sa += 0x04 ;
- LPC_CANAF->EFF_GRP_sa += 0x04;
- LPC_CANAF->ENDofTable += 0x04;
- }
- /*********** Add Explicit Extended Identifier Frame Format entry *********/
- else
- {
- /* Add controller number */
- id |= canId << 29;
- cnt1 = ((CANAF_FullCAN_cnt+1) >> 1) + (((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt);
- cnt2 = 0;
- while (cnt2 < CANAF_ext_cnt)
- {
- /* Loop through extended existing masks*/
- if (LPC_CANAF_RAM->mask[cnt1] > id)
- {
- break;
- }
- cnt1++;/* cnt1 = U32 where to insert new mask */
- cnt2++;
- }
- buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */
- LPC_CANAF_RAM->mask[cnt1] = id; /* Insert mask */
- CANAF_ext_cnt++;
- bound1 = total;
- /* Move all remaining extended mask entries one place up*/
- while (cnt2 < bound1)
- {
- cnt1++;
- cnt2++;
- buf1 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = buf0;
- buf0 = buf1;
- }
- /* update address values */
- LPC_CANAF->EFF_GRP_sa += 4;
- LPC_CANAF->ENDofTable += 4;
- }
- if(CANAF_FullCAN_cnt == 0) //not use FullCAN mode
- {
- LPC_CANAF->AFMR = 0x00;//not use FullCAN mode
- }
- else
- {
- LPC_CANAF->AFMR = 0x04;
- }
- return CAN_OK;
- }
- /********************************************************************//**
- * @brief Load FullCAN entry into AFLUT
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] id: identifier of entry that will be added
- * @return CAN_ERROR, could be:
- * - CAN_OK: loading is successful
- * - CAN_ID_EXIT_ERROR: ID exited in FullCAN Section
- * - CAN_OBJECTS_FULL_ERROR: no more space available
- *********************************************************************/
- CAN_ERROR CAN_LoadFullCANEntry (uint8_t canId, uint16_t id)
- {
- uint32_t buf0 = 0, buf1 = 0, buf2 = 0;
- uint32_t tmp0 = 0, tmp1 = 0, tmp2 = 0;
- int16_t cnt1 = 0, cnt2 = 0, bound1 = 0, total = 0;
- /* Acceptance Filter Memory full - return */
- total =((CANAF_FullCAN_cnt + 1) >> 1) + CANAF_FullCAN_cnt*3 + ((CANAF_std_cnt + 1) >> 1) \
- + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);
- //don't have enough space for this fullCAN Entry and its Object(3*32 bytes)
- if ((total >= 508) || (CANAF_FullCAN_cnt >= 64))
- {
- return CAN_OBJECTS_FULL_ERROR;
- }
- /* Setup Acceptance Filter Configuration
- Acceptance Filter Mode Register = Off */
- LPC_CANAF->AFMR = 0x00000001;
- /* Add mask for standard identifiers */
- id &= 0x07FF;
- id |= (canId << 13) | (1 << 11);
- /* Move all remaining sections one place up
- if new entry will increase FullCAN list */
- if (((CANAF_FullCAN_cnt & 0x0001) == 0)&&(total!=0))
- {
- //then remove remaining section
- cnt1 = (CANAF_FullCAN_cnt >> 1);
- bound1 = total;
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- while (bound1--)
- {
- cnt1++;
- buf1 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = buf0;
- buf0 = buf1;
- }
- }
- if (CANAF_FullCAN_cnt == 0)
- {
- /* For entering first ID */
- LPC_CANAF_RAM->mask[0] = 0x0000FFFF | (id << 16);
- }
- else if (CANAF_FullCAN_cnt == 1)
- {
- /* For entering second ID */
- if (((LPC_CANAF_RAM->mask[0] >> 16)& 0xE7FF) > id)
- {
- LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] >> 16) | (id << 16);
- }
- else
- {
- LPC_CANAF_RAM->mask[0] = (LPC_CANAF_RAM->mask[0] & 0xFFFF0000) | id;
- }
- }
- else
- {
- /* Find where to insert new ID */
- cnt1 = 0;
- cnt2 = CANAF_FullCAN_cnt;
- bound1 = (CANAF_FullCAN_cnt - 1) >> 1;
- while (cnt1 <= bound1)
- {
- /* Loop through standard existing IDs */
- if (((LPC_CANAF_RAM->mask[cnt1] >> 16) & 0xE7FF) > (id & 0xE7FF))
- {
- cnt2 = cnt1 * 2;
- break;
- }
- if ((LPC_CANAF_RAM->mask[cnt1] & 0x0000E7FF) > (id & 0xE7FF))
- {
- cnt2 = cnt1 * 2 + 1;
- break;
- }
- cnt1++;
- }
- /* cnt1 = U32 where to insert new ID */
- /* cnt2 = U16 where to insert new ID */
- if (cnt1 > bound1)
- {
- /* Adding ID as last entry */
- /* Even number of IDs exists */
- if ((CANAF_FullCAN_cnt & 0x0001) == 0)
- {
- LPC_CANAF_RAM->mask[cnt1] = 0x0000FFFF | (id << 16);
- }
- /* Odd number of IDs exists */
- else
- {
- LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000) | id;
- }
- }
- else
- {
- buf0 = LPC_CANAF_RAM->mask[cnt1]; /* Remember current entry */
- if ((cnt2 & 0x0001) == 0)
- {
- /* Insert new mask to even address*/
- buf1 = (id << 16) | (buf0 >> 16);
- }
- else
- {
- /* Insert new mask to odd address */
- buf1 = (buf0 & 0xFFFF0000) | id;
- }
- LPC_CANAF_RAM->mask[cnt1] = buf1;/* Insert mask */
- bound1 = CANAF_FullCAN_cnt >> 1;
- /* Move all remaining standard mask entries one place up */
- while (cnt1 < bound1)
- {
- cnt1++;
- buf1 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = (buf1 >> 16) | (buf0 << 16);
- buf0 = buf1;
- }
- if ((CANAF_FullCAN_cnt & 0x0001) == 0)
- {
- /* Even number of IDs exists */
- LPC_CANAF_RAM->mask[cnt1] = (LPC_CANAF_RAM->mask[cnt1] & 0xFFFF0000)
- | (0x0000FFFF);
- }
- }
- }
- //restruct FulCAN Object Section
- bound1 = CANAF_FullCAN_cnt - cnt2;
- cnt1 = total - (CANAF_FullCAN_cnt)*3 + cnt2*3 + 1;
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- buf1 = LPC_CANAF_RAM->mask[cnt1+1];
- buf2 = LPC_CANAF_RAM->mask[cnt1+2];
- LPC_CANAF_RAM->mask[cnt1]=LPC_CANAF_RAM->mask[cnt1+1]= LPC_CANAF_RAM->mask[cnt1+2]=0x00;
- cnt1+=3;
- while(bound1--)
- {
- tmp0 = LPC_CANAF_RAM->mask[cnt1];
- tmp1 = LPC_CANAF_RAM->mask[cnt1+1];
- tmp2 = LPC_CANAF_RAM->mask[cnt1+2];
- LPC_CANAF_RAM->mask[cnt1]= buf0;
- LPC_CANAF_RAM->mask[cnt1+1]= buf1;
- LPC_CANAF_RAM->mask[cnt1+2]= buf2;
- buf0 = tmp0;
- buf1 = tmp1;
- buf2 = tmp2;
- cnt1+=3;
- }
- CANAF_FullCAN_cnt++;
- //update address values
- LPC_CANAF->SFF_sa += 0x04;
- LPC_CANAF->SFF_GRP_sa += 0x04 ;
- LPC_CANAF->EFF_sa += 0x04 ;
- LPC_CANAF->EFF_GRP_sa += 0x04;
- LPC_CANAF->ENDofTable += 0x04;
- LPC_CANAF->AFMR = 0x04;
- return CAN_OK;
- }
- /********************************************************************//**
- * @brief Load Group entry into AFLUT
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] lowerID, upperID: lower and upper identifier of entry
- * @param[in] format: type of ID format, should be:
- * - STD_ID_FORMAT: Standard ID format (11-bit value)
- * - EXT_ID_FORMAT: Extended ID format (29-bit value)
- * @return CAN_ERROR, could be:
- * - CAN_OK: loading is successful
- * - CAN_CONFLICT_ID_ERROR: Conflict ID occurs
- * - CAN_OBJECTS_FULL_ERROR: no more space available
- *********************************************************************/
- CAN_ERROR CAN_LoadGroupEntry(uint8_t canId, uint32_t lowerID,
- uint32_t upperID, CAN_ID_FORMAT_Type format)
- {
- uint32_t buf0, buf1, entry1, entry2, LID,UID;
- int16_t cnt1, bound1, total;
- if(lowerID > upperID)
- return CAN_CONFLICT_ID_ERROR;
- total =((CANAF_FullCAN_cnt+1) >> 1)+ CANAF_FullCAN_cnt*3 +((CANAF_std_cnt + 1) >> 1) \
- + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
- /* Setup Acceptance Filter Configuration
- Acceptance Filter Mode Register = Off */
- LPC_CANAF->AFMR = 0x00000001;
- /*********Add Group of Standard Identifier Frame Format************/
- if(format == STD_ID_FORMAT)
- {
- if ((total >= 512))
- {
- //don't have enough space
- return CAN_OBJECTS_FULL_ERROR;
- }
- lowerID &=0x7FF; //mask ID
- upperID &=0x7FF;
- entry1 = (canId << 29) | (lowerID << 16) | (canId << 13)|(upperID << 0);
- cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1);
- //if this is the first Group standard ID entry
- if(CANAF_gstd_cnt == 0)
- {
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- }
- else
- {
- //find the position to add new Group entry
- bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt;
- while(cnt1 < bound1)
- {
- //compare controller first
- while((LPC_CANAF_RAM->mask[cnt1] >> 29)< (entry1 >> 29))//increase until meet greater or equal controller
- cnt1++;
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- if((LPC_CANAF_RAM->mask[cnt1] >> 29)> (entry1 >> 29)) //meet greater controller
- {
- //add at this position
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- break;
- }
- else //meet equal controller
- {
- LID = (buf0 >> 16)&0x7FF;
- UID = buf0 & 0x7FF;
- if (upperID <= LID)
- {
- //add new entry before this entry
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- break;
- }
- else if (lowerID >= UID)
- {
- //load next entry to compare
- cnt1 ++;
- }
- else
- return CAN_CONFLICT_ID_ERROR;
- }
- }
- if(cnt1 >= bound1)
- {
- //add new entry at the last position in this list
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- }
- //remove all remaining entry of this section one place up
- bound1 = total - cnt1;
- while(bound1--)
- {
- cnt1++;
- buf1 = LPC_CANAF_RAM->mask[cnt1];
- LPC_CANAF_RAM->mask[cnt1] = buf0;
- buf0 = buf1;
- }
- }
- CANAF_gstd_cnt++;
- //update address values
- LPC_CANAF->EFF_sa +=0x04 ;
- LPC_CANAF->EFF_GRP_sa +=0x04;
- LPC_CANAF->ENDofTable +=0x04;
- }
- /*********Add Group of Extended Identifier Frame Format************/
- else
- {
- if ((total >= 511))
- {
- //don't have enough space
- return CAN_OBJECTS_FULL_ERROR;
- }
- lowerID &= 0x1FFFFFFF; //mask ID
- upperID &= 0x1FFFFFFF;
- entry1 = (canId << 29)|(lowerID << 0);
- entry2 = (canId << 29)|(upperID << 0);
- cnt1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt + CANAF_ext_cnt;
- //if this is the first Group standard ID entry
- if(CANAF_gext_cnt == 0)
- {
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- LPC_CANAF_RAM->mask[cnt1+1] = entry2;
- }
- else
- {
- //find the position to add new Group entry
- bound1 = ((CANAF_FullCAN_cnt+1)>>1) + ((CANAF_std_cnt + 1) >> 1) + CANAF_gstd_cnt \
- + CANAF_ext_cnt + (CANAF_gext_cnt<<1);
- while(cnt1 < bound1)
- {
- while((LPC_CANAF_RAM->mask[cnt1] >>29)< canId) //increase until meet greater or equal controller
- cnt1++;
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- buf1 = LPC_CANAF_RAM->mask[cnt1+1];
- if((LPC_CANAF_RAM->mask[cnt1] >> 29)> canId) //meet greater controller
- {
- //add at this position
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- LPC_CANAF_RAM->mask[++cnt1] = entry2;
- break;
- }
- else //meet equal controller
- {
- LID = buf0 & 0x1FFFFFFF; //mask ID
- UID = buf1 & 0x1FFFFFFF;
- if (upperID <= LID)
- {
- //add new entry before this entry
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- LPC_CANAF_RAM->mask[++cnt1] = entry2;
- break;
- }
- else if (lowerID >= UID)
- {
- //load next entry to compare
- cnt1 +=2;
- }
- else
- return CAN_CONFLICT_ID_ERROR;
- }
- }
- if(cnt1 >= bound1)
- {
- //add new entry at the last position in this list
- buf0 = LPC_CANAF_RAM->mask[cnt1];
- buf1 = LPC_CANAF_RAM->mask[cnt1+1];
- LPC_CANAF_RAM->mask[cnt1] = entry1;
- LPC_CANAF_RAM->mask[++cnt1] = entry2;
- }
- //remove all remaining entry of this section two place up
- bound1 = total - cnt1 + 1;
- cnt1++;
- while(bound1>0)
- {
- entry1 = LPC_CANAF_RAM->mask[cnt1];
- entry2 = LPC_CANAF_RAM->mask[cnt1+1];
- LPC_CANAF_RAM->mask[cnt1] = buf0;
- LPC_CANAF_RAM->mask[cnt1+1] = buf1;
- buf0 = entry1;
- buf1 = entry2;
- cnt1 +=2;
- bound1 -=2;
- }
- }
- CANAF_gext_cnt++;
- //update address values
- LPC_CANAF->ENDofTable +=0x08;
- }
- LPC_CANAF->AFMR = 0x04;
- return CAN_OK;
- }
- /********************************************************************//**
- * @brief Remove AFLUT entry (FullCAN entry and Explicit Standard entry)
- * @param[in] EntryType: the type of entry that want to remove, should be:
- * - FULLCAN_ENTRY
- * - EXPLICIT_STANDARD_ENTRY
- * - GROUP_STANDARD_ENTRY
- * - EXPLICIT_EXTEND_ENTRY
- * - GROUP_EXTEND_ENTRY
- * @param[in] position: the position of this entry in its section
- * Note: the first position is 0
- * @return CAN_ERROR, could be:
- * - CAN_OK: removing is successful
- * - CAN_ENTRY_NOT_EXIT_ERROR: entry want to remove is not exit
- *********************************************************************/
- CAN_ERROR CAN_RemoveEntry(AFLUT_ENTRY_Type EntryType, uint16_t position)
- {
- uint16_t cnt, bound, total;
- uint32_t buf0, buf1;
- /* Setup Acceptance Filter Configuration
- Acceptance Filter Mode Register = Off */
- LPC_CANAF->AFMR = 0x00000001;
- total = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1) + \
- + CANAF_gstd_cnt + CANAF_ext_cnt + (CANAF_gext_cnt << 1);
- /************** Remove FullCAN Entry *************/
- if(EntryType == FULLCAN_ENTRY)
- {
- if((CANAF_FullCAN_cnt == 0)||(position >= CANAF_FullCAN_cnt))
- {
- return CAN_ENTRY_NOT_EXIT_ERROR;
- }
- else
- {
- cnt = position >> 1;
- buf0 = LPC_CANAF_RAM->mask[cnt];
- bound = (CANAF_FullCAN_cnt - position -1)>>1;
- if((position & 0x0001) == 0) //event position
- {
- while(bound--)
- {
- //remove all remaining FullCAN entry one place down
- buf1 = LPC_CANAF_RAM->mask[cnt+1];
- LPC_CANAF_RAM->mask[cnt] = (buf1 >> 16) | (buf0 << 16);
- buf0 = buf1;
- cnt++;
- }
- }
- else //odd position
- {
- while(bound--)
- {
- //remove all remaining FullCAN entry one place down
- buf1 = LPC_CANAF_RAM->mask[cnt+1];
- LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000)|(buf1 >> 16);
- LPC_CANAF_RAM->mask[cnt+1] = LPC_CANAF_RAM->mask[cnt+1] << 16;
- buf0 = buf1<<16;
- cnt++;
- }
- }
- if((CANAF_FullCAN_cnt & 0x0001) == 0)
- {
- if((position & 0x0001)==0)
- LPC_CANAF_RAM->mask[cnt] = (buf0 << 16) | (0x0000FFFF);
- else
- LPC_CANAF_RAM->mask[cnt] = buf0 | 0x0000FFFF;
- }
- else
- {
- //remove all remaining section one place down
- cnt = (CANAF_FullCAN_cnt + 1)>>1;
- bound = total + CANAF_FullCAN_cnt * 3;
- while(bound>cnt)
- {
- LPC_CANAF_RAM->mask[cnt-1] = LPC_CANAF_RAM->mask[cnt];
- cnt++;
- }
- LPC_CANAF_RAM->mask[cnt-1]=0x00;
- //update address values
- LPC_CANAF->SFF_sa -= 0x04;
- LPC_CANAF->SFF_GRP_sa -= 0x04 ;
- LPC_CANAF->EFF_sa -= 0x04 ;
- LPC_CANAF->EFF_GRP_sa -= 0x04;
- LPC_CANAF->ENDofTable -= 0x04;
- }
- CANAF_FullCAN_cnt--;
- //delete its FullCAN Object in the FullCAN Object section
- //remove all remaining FullCAN Object three place down
- cnt = total + position * 3;
- bound = (CANAF_FullCAN_cnt - position + 1) * 3;
- while(bound)
- {
- LPC_CANAF_RAM->mask[cnt]= LPC_CANAF_RAM->mask[cnt+3];;
- LPC_CANAF_RAM->mask[cnt+1]= LPC_CANAF_RAM->mask[cnt+4];
- LPC_CANAF_RAM->mask[cnt+2]= LPC_CANAF_RAM->mask[cnt+5];
- bound -= 3;
- cnt += 3;
- }
- }
- }
- /************** Remove Explicit Standard ID Entry *************/
- else if(EntryType == EXPLICIT_STANDARD_ENTRY)
- {
- if((CANAF_std_cnt == 0)||(position >= CANAF_std_cnt))
- {
- return CAN_ENTRY_NOT_EXIT_ERROR;
- }
- else
- {
- cnt = ((CANAF_FullCAN_cnt+1) >> 1) + (position >> 1);
- buf0 = LPC_CANAF_RAM->mask[cnt];
- bound = (CANAF_std_cnt - position - 1) >> 1;
- if((position & 0x0001) == 0) //event position
- {
- while(bound--)
- {
- //remove all remaining FullCAN entry one place down
- buf1 = LPC_CANAF_RAM->mask[cnt + 1];
- LPC_CANAF_RAM->mask[cnt] = (buf1 >> 16) | (buf0 << 16);
- buf0 = buf1;
- cnt++;
- }
- }
- else //odd position
- {
- while(bound--)
- {
- //remove all remaining FullCAN entry one place down
- buf1 = LPC_CANAF_RAM->mask[cnt + 1];
- LPC_CANAF_RAM->mask[cnt] = (buf0 & 0xFFFF0000) | (buf1 >> 16);
- LPC_CANAF_RAM->mask[cnt + 1] = LPC_CANAF_RAM->mask[cnt + 1] << 16;
- buf0 = buf1<<16;
- cnt++;
- }
- }
- if((CANAF_std_cnt & 0x0001) == 0)
- {
- if((position & 0x0001)==0)
- LPC_CANAF_RAM->mask[cnt] = (buf0 << 16) | (0x0000FFFF);
- else
- LPC_CANAF_RAM->mask[cnt] = buf0 | 0x0000FFFF;
- }
- else
- {
- //remove all remaining section one place down
- cnt = ((CANAF_FullCAN_cnt + 1)>>1) + ((CANAF_std_cnt + 1) >> 1);
- bound = total + CANAF_FullCAN_cnt * 3;
- while(bound>cnt)
- {
- LPC_CANAF_RAM->mask[cnt-1] = LPC_CANAF_RAM->mask[cnt];
- cnt++;
- }
- LPC_CANAF_RAM->mask[cnt-1]=0x00;
- //update address value
- LPC_CANAF->SFF_GRP_sa -= 0x04 ;
- LPC_CANAF->EFF_sa -= 0x04 ;
- LPC_CANAF->EFF_GRP_sa -= 0x04;
- LPC_CANAF->ENDofTable -= 0x04;
- }
- CANAF_std_cnt--;
- }
- }
- /************** Remove Group of Standard ID Entry *************/
- else if(EntryType == GROUP_STANDARD_ENTRY)
- {
- if((CANAF_gstd_cnt == 0)||(position >= CANAF_gstd_cnt))
- {
- return CAN_ENTRY_NOT_EXIT_ERROR;
- }
- else
- {
- cnt = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1)+ position + 1;
- bound = total + CANAF_FullCAN_cnt * 3;
- while (cnt < bound)
- {
- LPC_CANAF_RAM->mask[cnt - 1] = LPC_CANAF_RAM->mask[cnt];
- cnt++;
- }
- LPC_CANAF_RAM->mask[cnt - 1]=0x00;
- }
- CANAF_gstd_cnt--;
- //update address value
- LPC_CANAF->EFF_sa -= 0x04;
- LPC_CANAF->EFF_GRP_sa -= 0x04;
- LPC_CANAF->ENDofTable -= 0x04;
- }
- /************** Remove Explicit Extended ID Entry *************/
- else if(EntryType == EXPLICIT_EXTEND_ENTRY)
- {
- if((CANAF_ext_cnt == 0)||(position >= CANAF_ext_cnt))
- {
- return CAN_ENTRY_NOT_EXIT_ERROR;
- }
- else
- {
- cnt = ((CANAF_FullCAN_cnt + 1) >> 1) + ((CANAF_std_cnt + 1) >> 1)+ CANAF_gstd_cnt + position + 1;
- bound = total + CANAF_FullCAN_cnt * 3;
- while (cnt<bound)
- {
- LPC_CANAF_RAM->mask[cnt - 1] = LPC_CANAF_RAM->mask[cnt];
- cnt++;
- }
- LPC_CANAF_RAM->mask[cnt - 1]=0x00;
- }
- CANAF_ext_cnt--;
- LPC_CANAF->EFF_GRP_sa -= 0x04;
- LPC_CANAF->ENDofTable -= 0x04;
- }
- /************** Remove Group of Extended ID Entry *************/
- else
- {
- if((CANAF_gext_cnt == 0)||(position >= CANAF_gext_cnt))
- {
- return CAN_ENTRY_NOT_EXIT_ERROR;
- }
- else
- {
- cnt = total - (CANAF_gext_cnt << 1) + (position << 1);
- bound = total + CANAF_FullCAN_cnt * 3;
- while (cnt<bound)
- {
- //remove all remaining entry two place up
- LPC_CANAF_RAM->mask[cnt] = LPC_CANAF_RAM->mask[cnt + 2];
- LPC_CANAF_RAM->mask[cnt + 1] = LPC_CANAF_RAM->mask[cnt + 3];
- cnt += 2;
- }
- }
- CANAF_gext_cnt--;
- LPC_CANAF->ENDofTable -= 0x08;
- }
- LPC_CANAF->AFMR = 0x04;
- return CAN_OK;
- }
- /********************************************************************//**
- * @brief Send message data
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] CAN_Msg point to the CAN_MSG_Type Structure, it contains message
- * information such as: ID, DLC, RTR, ID Format
- * @return Status:
- * - SUCCESS: send message successfully
- * - ERROR: send message unsuccessfully
- *********************************************************************/
- Status CAN_SendMsg (uint8_t canId, CAN_MSG_Type *CAN_Msg)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- uint32_t data;
- //Check status of Transmit Buffer 1
- if (pCan->SR & (1 << 2))
- {
- /* Transmit Channel 1 is available */
- /* Write frame informations and frame data into its CANxTFI1,
- * CANxTID1, CANxTDA1, CANxTDB1 register */
- pCan->TFI1 &= ~ 0x000F0000;
- pCan->TFI1 |= (CAN_Msg->len) << 16;
- if(CAN_Msg->type == REMOTE_FRAME)
- {
- pCan->TFI1 |= (1 << 30); //set bit RTR
- }
- else
- {
- pCan->TFI1 &= ~(1 << 30);
- }
- if(CAN_Msg->format == EXT_ID_FORMAT)
- {
- pCan->TFI1 |= (((uint32_t)1) << 31); //set bit FF
- }
- else
- {
- pCan->TFI1 &= ~(((uint32_t)1) << 31);
- }
- /* Write CAN ID*/
- pCan->TID1 = CAN_Msg->id;
- /*Write first 4 data bytes*/
- data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1]))<< 8) | ((CAN_Msg->dataA[2]) << 16) | ((CAN_Msg->dataA[3]) << 24);
- pCan->TDA1 = data;
- /*Write second 4 data bytes*/
- data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8)|((CAN_Msg->dataB[2]) << 16)|((CAN_Msg->dataB[3]) << 24);
- pCan->TDB1 = data;
- /*Write transmission request*/
- pCan->CMR = 0x21;
- return SUCCESS;
- }
- //check status of Transmit Buffer 2
- else if((pCan->SR) & (1 << 10))
- {
- /* Transmit Channel 2 is available */
- /* Write frame informations and frame data into its CANxTFI2,
- * CANxTID2, CANxTDA2, CANxTDB2 register */
- pCan->TFI2 &= ~0x000F0000;
- pCan->TFI2 |= (CAN_Msg->len) << 16;
- if(CAN_Msg->type == REMOTE_FRAME)
- {
- pCan->TFI2 |= (1 << 30); //set bit RTR
- }
- else
- {
- pCan->TFI2 &= ~(1 << 30);
- }
- if(CAN_Msg->format == EXT_ID_FORMAT)
- {
- pCan->TFI2 |= (((uint32_t)1) << 31); //set bit FF
- }
- else
- {
- pCan->TFI2 &= ~(((uint32_t)1) << 31);
- }
- /* Write CAN ID*/
- pCan->TID2 = CAN_Msg->id;
- /*Write first 4 data bytes*/
- data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1])) << 8) | ((CAN_Msg->dataA[2]) << 16)|((CAN_Msg->dataA[3]) << 24);
- pCan->TDA2 = data;
- /*Write second 4 data bytes*/
- data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8) | ((CAN_Msg->dataB[2]) << 16) | ((CAN_Msg->dataB[3]) << 24);
- pCan->TDB2 = data;
- /*Write transmission request*/
- pCan->CMR = 0x41;
- return SUCCESS;
- }
- //check status of Transmit Buffer 3
- else if (pCan->SR & (1<<18))
- {
- /* Transmit Channel 3 is available */
- /* Write frame informations and frame data into its CANxTFI3,
- * CANxTID3, CANxTDA3, CANxTDB3 register */
- pCan->TFI3 &= ~0x000F0000;
- pCan->TFI3 |= (CAN_Msg->len) << 16;
- if(CAN_Msg->type == REMOTE_FRAME)
- {
- pCan->TFI3 |= (1 << 30); //set bit RTR
- }
- else
- {
- pCan->TFI3 &= ~(1 << 30);
- }
- if(CAN_Msg->format == EXT_ID_FORMAT)
- {
- pCan->TFI3 |= (((uint32_t)1) << 31); //set bit FF
- }
- else
- {
- pCan->TFI3 &= ~(((uint32_t)1) << 31);
- }
- /* Write CAN ID*/
- pCan->TID3 = CAN_Msg->id;
- /*Write first 4 data bytes*/
- data = (CAN_Msg->dataA[0]) | (((CAN_Msg->dataA[1])) << 8) | ((CAN_Msg->dataA[2]) << 16) | ((CAN_Msg->dataA[3]) << 24);
- pCan->TDA3 = data;
- /*Write second 4 data bytes*/
- data = (CAN_Msg->dataB[0]) | (((CAN_Msg->dataB[1])) << 8) | ((CAN_Msg->dataB[2]) << 16) | ((CAN_Msg->dataB[3]) << 24);
- pCan->TDB3 = data;
- /*Write transmission request*/
- pCan->CMR = 0x81;
- return SUCCESS;
- }
- else
- {
- return ERROR;
- }
- }
- /********************************************************************//**
- * @brief Receive message data
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] CAN_Msg point to the CAN_MSG_Type Struct, it will contain received
- * message information such as: ID, DLC, RTR, ID Format
- * @return Status:
- * - SUCCESS: receive message successfully
- * - ERROR: receive message unsuccessfully
- *********************************************************************/
- Status CAN_ReceiveMsg (uint8_t canId, CAN_MSG_Type *CAN_Msg)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- uint32_t data;
- //check status of Receive Buffer
- if((pCan->SR &0x00000001))
- {
- /* Receive message is available */
- /* Read frame informations */
- CAN_Msg->format = (uint8_t)(((pCan->RFS) & 0x80000000) >> 31);
- CAN_Msg->type = (uint8_t)(((pCan->RFS) & 0x40000000) >> 30);
- CAN_Msg->len = (uint8_t)(((pCan->RFS) & 0x000F0000) >> 16);
- /* Read CAN message identifier */
- CAN_Msg->id = pCan->RID;
- /* Read the data if received message was DATA FRAME */
- if (CAN_Msg->type == DATA_FRAME)
- {
- /* Read first 4 data bytes */
- data = pCan->RDA;
- *((uint8_t *) &CAN_Msg->dataA[0])= data & 0x000000FF;
- *((uint8_t *) &CAN_Msg->dataA[1])= (data & 0x0000FF00) >> 8;;
- *((uint8_t *) &CAN_Msg->dataA[2])= (data & 0x00FF0000) >> 16;
- *((uint8_t *) &CAN_Msg->dataA[3])= (data & 0xFF000000) >> 24;
- /* Read second 4 data bytes */
- data = pCan->RDB;
- *((uint8_t *) &CAN_Msg->dataB[0])= data & 0x000000FF;
- *((uint8_t *) &CAN_Msg->dataB[1])= (data & 0x0000FF00) >> 8;
- *((uint8_t *) &CAN_Msg->dataB[2])= (data & 0x00FF0000) >> 16;
- *((uint8_t *) &CAN_Msg->dataB[3])= (data & 0xFF000000) >> 24;
- /*release receive buffer*/
- pCan->CMR = 0x04;
- }
- else
- {
- /* Received Frame is a Remote Frame, not have data, we just receive
- * message information only */
- pCan->CMR = 0x04; /*release receive buffer*/
- return SUCCESS;
- }
- }
- else
- {
- // no receive message available
- return ERROR;
- }
- return SUCCESS;
- }
- /********************************************************************//**
- * @brief Receive FullCAN Object
- * @param[in] CANAFx: CAN Acceptance Filter register, should be: LPC_CANAF
- * @param[in] CAN_Msg point to the CAN_MSG_Type Struct, it will contain received
- * message information such as: ID, DLC, RTR, ID Format
- * @return CAN_ERROR, could be:
- * - CAN_FULL_OBJ_NOT_RCV: FullCAN Object is not be received
- * - CAN_OK: Received FullCAN Object successful
- *
- *********************************************************************/
- CAN_ERROR FCAN_ReadObj (CAN_MSG_Type *CAN_Msg)
- {
- uint32_t *pSrc, data;
- uint32_t interrut_word, msg_idx, test_bit, head_idx, tail_idx;
- interrut_word = 0;
- if (LPC_CANAF->FCANIC0 != 0)
- {
- interrut_word = LPC_CANAF->FCANIC0;
- head_idx = 0;
- tail_idx = 31;
- }
- else if (LPC_CANAF->FCANIC1 != 0)
- {
- interrut_word = LPC_CANAF->FCANIC1;
- head_idx = 32;
- tail_idx = 63;
- }
- if (interrut_word != 0)
- {
- /* Detect for interrupt pending */
- msg_idx = 0;
- for (msg_idx = head_idx; msg_idx <= tail_idx; msg_idx++)
- {
- test_bit = interrut_word & 0x1;
- interrut_word = interrut_word >> 1;
- if (test_bit)
- {
- pSrc = (uint32_t *) (LPC_CANAF->ENDofTable + LPC_CANAF_RAM_BASE + msg_idx * 12);
- /* Has been finished updating the content */
- if ((*pSrc & 0x03000000L) == 0x03000000L)
- {
- /*clear semaphore*/
- *pSrc &= 0xFCFFFFFF;
- /*Set to DatA*/
- pSrc++;
- /* Copy to dest buf */
- data = *pSrc;
- *((uint8_t *) &CAN_Msg->dataA[0])= data & 0x000000FF;
- *((uint8_t *) &CAN_Msg->dataA[1])= (data & 0x0000FF00) >> 8;
- *((uint8_t *) &CAN_Msg->dataA[2])= (data & 0x00FF0000) >> 16;
- *((uint8_t *) &CAN_Msg->dataA[3])= (data & 0xFF000000) >> 24;
- /*Set to DatB*/
- pSrc++;
- /* Copy to dest buf */
- data = *pSrc;
- *((uint8_t *) &CAN_Msg->dataB[0])= data & 0x000000FF;
- *((uint8_t *) &CAN_Msg->dataB[1])= (data & 0x0000FF00) >> 8;
- *((uint8_t *) &CAN_Msg->dataB[2])= (data & 0x00FF0000) >> 16;
- *((uint8_t *) &CAN_Msg->dataB[3])= (data & 0xFF000000) >> 24;
- /*Back to Dat1*/
- pSrc -= 2;
- CAN_Msg->id = *pSrc & 0x7FF;
- CAN_Msg->len = (uint8_t) (*pSrc >> 16) & 0x0F;
- CAN_Msg->format = 0; //FullCAN Object ID always is 11-bit value
- CAN_Msg->type = (uint8_t)(*pSrc >> 30) &0x01;
- /*Re-read semaphore*/
- if ((*pSrc & 0x03000000L) == 0)
- {
- return CAN_OK;
- }
- }
- }
- }
- }
- return CAN_FULL_OBJ_NOT_RCV;
- }
- /********************************************************************//**
- * @brief Get CAN Control Status
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] arg: type of CAN status to get from CAN status register
- * Should be:
- * - CANCTRL_GLOBAL_STS: CAN Global Status
- * - CANCTRL_INT_CAP: CAN Interrupt and Capture
- * - CANCTRL_ERR_WRN: CAN Error Warning Limit
- * - CANCTRL_STS: CAN Control Status
- * @return Current Control Status that you want to get value
- *********************************************************************/
- uint32_t CAN_GetCTRLStatus (uint8_t canId, CAN_CTRL_STS_Type arg)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- switch (arg)
- {
- case CANCTRL_GLOBAL_STS:
- return pCan->GSR;
- case CANCTRL_INT_CAP:
- return pCan->ICR;
- case CANCTRL_ERR_WRN:
- return pCan->EWL;
- default: // CANCTRL_STS
- return pCan->SR;
- }
- }
- /********************************************************************//**
- * @brief Get CAN Central Status
- * @param[in] CANCRx point to LPC_CANCR_TypeDef, should be: LPC_CANCR
- * @param[in] arg: type of CAN status to get from CAN Central status register
- * Should be:
- * - CANCR_TX_STS: Central CAN Tx Status
- * - CANCR_RX_STS: Central CAN Rx Status
- * - CANCR_MS: Central CAN Miscellaneous Status
- * @return Current Central Status that you want to get value
- *********************************************************************/
- uint32_t CAN_GetCRStatus (CAN_CR_STS_Type arg)
- {
- switch (arg)
- {
- case CANCR_TX_STS:
- return LPC_CANCR->TxSR;
- case CANCR_RX_STS:
- return LPC_CANCR->RxSR;
- default: // CANCR_MS
- return LPC_CANCR->MSR;
- }
- }
- /********************************************************************//**
- * @brief Enable/Disable CAN Interrupt
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] arg: type of CAN interrupt that you want to enable/disable
- * Should be:
- * - CANINT_RIE: CAN Receiver Interrupt Enable
- * - CANINT_TIE1: CAN Transmit Interrupt Enable
- * - CANINT_EIE: CAN Error Warning Interrupt Enable
- * - CANINT_DOIE: CAN Data Overrun Interrupt Enable
- * - CANINT_WUIE: CAN Wake-Up Interrupt Enable
- * - CANINT_EPIE: CAN Error Passive Interrupt Enable
- * - CANINT_ALIE: CAN Arbitration Lost Interrupt Enable
- * - CANINT_BEIE: CAN Bus Error Interrupt Enable
- * - CANINT_IDIE: CAN ID Ready Interrupt Enable
- * - CANINT_TIE2: CAN Transmit Interrupt Enable for Buffer2
- * - CANINT_TIE3: CAN Transmit Interrupt Enable for Buffer3
- * - CANINT_FCE: FullCAN Interrupt Enable
- * @param[in] NewState: New state of this function, should be:
- * - ENABLE
- * - DISABLE
- * @return none
- *********************************************************************/
- void CAN_IRQCmd (uint8_t canId, CAN_INT_EN_Type arg, FunctionalState NewState)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- if(NewState == ENABLE)
- {
- if(arg == CANINT_FCE)
- {
- LPC_CANAF->AFMR = 0x01;
- LPC_CANAF->FCANIE = 0x01;
- LPC_CANAF->AFMR = 0x04;
- }
- else
- pCan->IER |= (1 << arg);
- }
- else
- {
- if(arg == CANINT_FCE)
- {
- LPC_CANAF->AFMR = 0x01;
- LPC_CANAF->FCANIE = 0x01;
- LPC_CANAF->AFMR = 0x00;
- }
- else
- pCan->IER &= ~(1 << arg);
- }
- }
- /********************************************************************//**
- * @brief Setting Acceptance Filter mode
- * @param[in] CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
- * @param[in] AFMode: type of AF mode that you want to set, should be:
- * - CAN_NORMAL: Normal mode
- * - CAN_ACC_OFF: Acceptance Filter Off Mode
- * - CAN_ACC_BP: Acceptance Fileter Bypass Mode
- * - CAN_EFCAN: FullCAN Mode Enhancement
- * @return none
- *********************************************************************/
- void CAN_SetAFMode (CAN_AFMODE_Type AFMode)
- {
- switch(AFMode)
- {
- case CAN_NORMAL:
- LPC_CANAF->AFMR = 0x00;
- break;
- case CAN_ACC_OFF:
- LPC_CANAF->AFMR = 0x01;
- break;
- case CAN_ACC_BP:
- LPC_CANAF->AFMR = 0x02;
- break;
- case CAN_EFCAN:
- LPC_CANAF->AFMR = 0x04;
- break;
- }
- }
- /********************************************************************//**
- * @brief Enable/Disable CAN Mode
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] mode: type of CAN mode that you want to enable/disable, should be:
- * - CAN_OPERATING_MODE: Normal Operating Mode
- * - CAN_RESET_MODE: Reset Mode
- * - CAN_LISTENONLY_MODE: Listen Only Mode
- * - CAN_SELFTEST_MODE: Self Test Mode
- * - CAN_TXPRIORITY_MODE: Transmit Priority Mode
- * - CAN_SLEEP_MODE: Sleep Mode
- * - CAN_RXPOLARITY_MODE: Receive Polarity Mode
- * - CAN_TEST_MODE: Test Mode
- * @param[in] NewState: New State of this function, should be:
- * - ENABLE
- * - DISABLE
- * @return none
- *********************************************************************/
- void CAN_ModeConfig(uint8_t canId, CAN_MODE_Type mode, FunctionalState NewState)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- switch(mode)
- {
- case CAN_OPERATING_MODE:
- pCan->MOD = 0x00;
- break;
- case CAN_RESET_MODE:
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_RM;
- else
- pCan->MOD &= ~CAN_MOD_RM;
- break;
- case CAN_LISTENONLY_MODE:
- pCan->MOD |=CAN_MOD_RM;//Enter Reset mode
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_LOM;
- else
- pCan->MOD &= ~ CAN_MOD_LOM;
- pCan->MOD &= ~ CAN_MOD_RM;//Release Reset mode
- break;
- case CAN_SELFTEST_MODE:
- pCan->MOD |= CAN_MOD_RM;//Enter Reset mode
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_STM;
- else
- pCan->MOD &= ~ CAN_MOD_STM;
- pCan->MOD &= ~ CAN_MOD_RM;//Release Reset mode
- break;
- case CAN_TXPRIORITY_MODE:
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_TPM;
- else
- pCan->MOD &= ~ CAN_MOD_TPM;
- break;
- case CAN_SLEEP_MODE:
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_SM;
- else
- pCan->MOD &= ~ CAN_MOD_SM;
- break;
- case CAN_RXPOLARITY_MODE:
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_RPM;
- else
- pCan->MOD &= ~ CAN_MOD_RPM;
- break;
- case CAN_TEST_MODE:
- if(NewState == ENABLE)
- pCan->MOD |= CAN_MOD_TM;
- else
- pCan->MOD &= ~ CAN_MOD_TM;
- break;
- }
- }
- /*********************************************************************//**
- * @brief Set CAN command request
- * @param[in] canId The Id of the expected CAN component
- *
- * @param[in] CMRType command request type, should be:
- * - CAN_CMR_TR: Transmission request
- * - CAN_CMR_AT: Abort Transmission request
- * - CAN_CMR_RRB: Release Receive Buffer request
- * - CAN_CMR_CDO: Clear Data Overrun request
- * - CAN_CMR_SRR: Self Reception request
- * - CAN_CMR_STB1: Select Tx Buffer 1 request
- * - CAN_CMR_STB2: Select Tx Buffer 2 request
- * - CAN_CMR_STB3: Select Tx Buffer 3 request
- * @return CANICR (CAN interrupt and Capture register) value
- **********************************************************************/
- void CAN_SetCommand(uint8_t canId, uint32_t CMRType)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- pCan->CMR |= CMRType;
- }
- /*********************************************************************//**
- * @brief Get CAN interrupt status
- * @param[in] canId The Id of the expected CAN component
- *
- * @return CANICR (CAN interrupt and Capture register) value
- **********************************************************************/
- uint32_t CAN_IntGetStatus(uint8_t canId)
- {
- LPC_CAN_TypeDef* pCan = CAN_GetPointer(canId);
- return pCan->ICR;
- }
- /*********************************************************************//**
- * @brief Check if FullCAN interrupt enable or not
- * @param[in] CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
- * @return IntStatus, could be:
- * - SET: if FullCAN interrupt is enable
- * - RESET: if FullCAN interrupt is disable
- **********************************************************************/
- IntStatus CAN_FullCANIntGetStatus (void)
- {
- if (LPC_CANAF->FCANIE)
- return SET;
- return RESET;
- }
- /*********************************************************************//**
- * @brief Get value of FullCAN interrupt and capture register
- * @param[in] CANAFx point to LPC_CANAF_TypeDef object, should be: LPC_CANAF
- * @param[in] type: FullCAN IC type, should be:
- * - FULLCAN_IC0: FullCAN Interrupt Capture 0
- * - FULLCAN_IC1: FullCAN Interrupt Capture 1
- * @return FCANIC0 or FCANIC1 (FullCAN interrupt and Capture register) value
- **********************************************************************/
- uint32_t CAN_FullCANPendGetStatus(FullCAN_IC_Type type)
- {
- if (type == FULLCAN_IC0)
- return LPC_CANAF->FCANIC0;
- return LPC_CANAF->FCANIC1;
- }
- /* End of Public Variables ---------------------------------------------------------- */
- /**
- * @}
- */
- #endif /*_CAN*/
- /**
- * @}
- */
- /* --------------------------------- End Of File ------------------------------ */
|