📄 mscandrvlite.c
字号:
/******************************************************************************
MOTOROLA INTERNAL USE ONLY
Copyright (c) Motorola 2000
File Name : $RCSfile: msCANdrvlite.c,v $
Engineer : $Author: r40356 $
Location : EKB
Date Created : 25/05/98
Current Revision : $Revision: 1.4 $
Notes : msCAN driver routines for single msCAN module.
*******************************************************************************
Motorola reserves the right to make changes without further notice to any
product herein to improve reliability, function or design. Motorola does
not assume any liability arising out ot the application or use of any
product, circuit, or software described herein; neither does it convey any
license under its patent rights nor the rights of others. Motorola products
are not designed, intended, or authorized for use as components in systems
intended for surgical implant into the body, or other applications intended
to support life, or for any other application in which the failure of the
Motorola product could create a situation where personal injury or death may
occur. Should Buyer purchase or use Motorola products for any such unintended
or unauthorized application, Buyer shall idemnify and hold Motorola and its
officers, employees, subsidiaries, affiliates, and distributors harmless
against all claims costs, damages, and expenses, and reasonable attorney fees
arising out of, directly or indirectly, any claim of personal injury or death
associated with such unintended or unathorized use, even if such claim alleges
that Motorola was negligent regarding the design or manufacture of the part.
Motorola and the Motorola logo* are registered trademarks of Motorola Ltd.
******************************************************************************/
/******************************************************************************
Include files
******************************************************************************/
#include "msCANstd.h" /* Standard defines */
#include "msCANdrv.h" /* General defines, enums, prototypes */
#include "msCANinc.h" /* Internal typedefs, prototypes */
#include "msCANreg.h" /* msCAN register defines */
#include "msCANvarlite.c" /* msCAN driver variables */
//Added by rc574c, 6.12.2005
#include "can_con.h" /* Header file for CAN connectivity */
/****************************************************************************/
//#define CAN_BaseAddress &msCAN0Start /* start address of msCAN0 module */
/******************************************************************************
VERSION
******************************************************************************/
#define VERSION MSCAN12_L_01
/******************************************************************************
BENCHMARK
*******************************************************************************
If BENCHMARK defined, the following port pins are toggled:
PORTB
Bit 0: Set at start of ReceiveISR, cleared at end.
Bit 1: Set at start of TransmitISR, cleared at end.
******************************************************************************/
#ifdef BENCHMARK
#define PORTB (*((volatile UINT8*)0x0001))
#endif
/******************************************************************************
Compiler dependancies
******************************************************************************/
#ifdef HICROSS
#define SEI asm PSHA; asm TPA; asm SEI; asm STAA CCRcopy; asm PULA
#define CLI asm CLI
#endif /* HICROSS */
#ifdef COSMIC
#define SEI _asm("PSHA \n TPA \n SEI \n STAA _CCRcopy \n PULA")
#define CLI _asm("CLI")
#endif /* COSMIC */
#pragma DATA_SEG EEPROM_CAN_CHECK
static UINT16 CAN_dataCheck; /* variable used for checking security for HACO */
#pragma DATA_SEG DEFAULT
/******************************************************************************
Function Name : CAN_L_Init
Engineer : Stuart Robb
Date : 10/8/98
Arguments : mode: FAST immediate reset
CMPTX complete current transmission
Return : ERR_OK if no error, otherwise error code
Notes : If CMPTX argument is used, the fn could take the duration of
a single message transmission to return.
******************************************************************************/
UINT8
CAN_L_Init(UINT8 rmode)
{
UINT8 errorStatus, /* error code from Reset_CAN */
i; /* loop counter */
#if defined(EEPROM_SECURITY_CHECK)
/* test for correct value in EEPROM */
if (CAN_dataCheck != EEPROM_SECURITY)
{
/* if test fails do not initialise */
return(ERR_OK);
}
#endif /* EEPROM_SECURITY_CHECK */
errorStatus = CAN_L_Reset(rmode); /* reset msCAN */
if (errorStatus != ERR_OK)
{
return (errorStatus);
}
CANCTL1 = CANCTL1_Def; /* initialise hardware registers */
CANBTR0 = CANBTR0_Def;
CANBTR1 = CANBTR1_Def;
CANIDAC = CANIDAC_Def;
for (i = 0; i < NO_OF_FILTER_REG; i++)
{
*(&CANIDAR0 + i) = CANIDFilter_Def[i]; /* initialise hardware acceptance filter */
}
CANCTL0 &= ~INITRQ; /* clear soft reset */
CANCTL0 = CANCTL0_Def; /* must write separate from clearing SFTRST */
for (i = 0 ; i < NoOfMB ; i++) /* configure all MB's to CLOSED, NODATA */
{
BufferMode_CAN0[i] = CLOSED;
BufferStatus_CAN0[i] = NODATA;
}
MB_MSCAN[0] = 0xff; /* more efficient than for loop */
MB_MSCAN[1] = 0xff;
MB_MSCAN[2] = 0xff;
MBP_MSCAN[0] = 0xff;
MBP_MSCAN[1] = 0xff;
MBP_MSCAN[2] = 0xff;
#if (NO_OF_MSCAN_TXBUFFER != 3)
#error MB_MSCAN not initialised
#endif
if (CANCTL1 & INITAK) return ERR_INIT;
CLI; /* clear interrupt mask */
CANRIER = 0x01; /* enable Receive Interrupts */
if ((CANRFLG & BUSOFF) == BUSOFF) /* Bus-off */
{
return (ERR_BOFF);
}
else
{
return (ERR_OK);
}
}
/******************************************************************************
Function Name : CAN_L_Reset
Engineer : Stuart Robb
Date : 10/8/98
Arguments : mode FAST immediate reset
CMPTX complete current transmission
Return : ERR_OK if no error, otherwise error code
Notes : If CMPTX argument is used, the fn could take the duration of
a single message transmission to return.
******************************************************************************/
UINT8
CAN_L_Reset(UINT8 rmode)
{
if (CANCTL1 & INITAK) return ERR_OK;
if (CANCTL0 & INITRQ) return ERR_INIT;
if (!((rmode == FAST) || (rmode == CMPTX)))
{
return (ERR_MODE);
}
SEI;
if (rmode == CMPTX) /* wait for current transmission to complete */
{
CANRIER = 0; /* clear all interrupt enables */
CANTARQ = (~CANTFLG); /* and abort any scheduled transmissions */
if (!(CCRcopy & IMASK))
{
CLI; /* restore interrupt mask */
}
while ((CANTFLG & 0x07) != 0x07)
{ /* wait for all msCAN tx buffers not scheduled */
} /* this could take time of one message transmission */
}
CANCTL0 = INITRQ; /* soft reset */
if (!(CCRcopy & IMASK))
{
CLI; /* restore interrupt mask */
}
DriverFlags = 0; /* clear flags */
if (CANCTL1 & INITAK) return ERR_OK;
else return ERR_INIT;
}
/******************************************************************************
Function Name : CAN_L_Sleep
Engineer : Stuart Robb
Date : 10/8/98
Arguments : mode FAST go to sleep immediately
CMPTX complete all queued transmissions
Return : ERR_OK if no error, otherwise error code
******************************************************************************/
UINT8
CAN_L_Sleep(UINT8 rmode)
{
if (!((rmode == FAST) || (rmode == CMPTX)))
{
return (ERR_MODE);
}
if (CANCTL1 & INITAK) /* in initialization mode */
{
return (ERR_INIT);
}
CANRFLG = WUPIF; /* clear wakeup interrupt flag */
if ((rmode == CMPTX) && (CANTIER & 0x07)) /* rmode = CMPTX and Tx interrupt enabled */
{
DriverFlags |= SLEEP; /* TransmitISR_CAN will set SLPRQ */
}
else /* rmode = FAST */
{
CANCTL0 |= SLPRQ; /* sleep request */
CANRIER |= WUPIE; /* enable wakeup interrupt */
}
return (ERR_OK);
}
/******************************************************************************
Function Name : CAN_L_Wakeup
Engineer : Stuart Robb
Date : 10/8/98
Arguments : none
Return : ERR_OK if no error, otherwise error code
******************************************************************************/
UINT8
CAN_L_Wakeup(void)
{
UINT8 currentCANCTL0;
currentCANCTL0 = CANCTL0;
if (CANCTL1 & INITAK) /* initialization mode */
{
return (ERR_INIT);
}
DriverFlags &= ~SLEEP; /* clear driver sleep request flag */
if ((currentCANCTL0 & SLPRQ) && !(CANCTL1 & SLPAK)) /* SLPRQ set but SLPAK clear */
{
return (ERR_NSLP); /* not yet in Sleep mode: cannot clear SLPRQ */
}
else /* in Sleep mode (or SLPRQ not set) */
{
SEI;
CANRIER &= ~WUPIE; /* clear wakeup interrupt */
if (!(CCRcopy & IMASK))
{
CLI; /* restore interrupt mask */
}
CANCTL0 &= ~SLPRQ; /* wake-up msCAN */
return (ERR_OK);
}
}
/******************************************************************************
Function Name : CAN_L_CheckStatus
Engineer : Stuart Robb
Date : 10/8/98
Arguments : address for status
Return : ERR_OK
******************************************************************************/
UINT8
CAN_L_CheckStatus(UINT16 *statusPtr)
{
*((UINT8*)(statusPtr)) = ((CANCTL0 & (SYNCH | SLPRQ | INITRQ)) | ((CANCTL1 & (SLPAK | INITAK)) << 2));
*((UINT8*)(statusPtr + 1)) = CANRFLG;
return (ERR_OK);
}
/******************************************************************************
Function Name : CAN_L_ClearStatus
Engineer : Stuart Robb
Date : 10/8/98
Arguments : none
Return : ERR_OK
******************************************************************************/
UINT8
CAN_L_ClearStatus(void)
{
CANRFLG = 0xFE; /* write 1's to clear all flags except RXF */
return (ERR_OK);
}
/******************************************************************************
Function Name : CAN_L_ConfigMB
Engineer : Stuart Robb
Date : 10/8/98
Arguments : buffer number
buffer mode
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -