Você está na página 1de 3

FINAL REPORT PSHEV.

EECPS/STEPS MASTER, NOVEMBER 2015

CAN Practical Implementation


Sridhar Chellappa, Attendant, STEPS Master
Gabriel Borge, Attendant, EECPS Master

AbstractThe objetive of this worktask is to develop an


application based on a TMS320F28335 microcontroller. This
application must allow to sniff in the CAN bus of a vehicle and
extract some interesting data of the vehicle.

In the task, all the data except one are to be sent every 10ms.
The one is to be sent for every 20ms. In order to achieve this,
we declare a count variable that ensures that that this is done.
/ / C o u n t e r f o r t i m e r 20ms
int counter timer = 0;

I. E NUNCIATION
This task aims to develop a C program that transmits and
receives data in the CAN module of a Texas TMS320F28335
microcontroller. A list is provided with data from a SEAT
car that needs to be transmitted (Fig. 1). The list defines the
identifier of the data, data length,units and cycle time. The list
also defines the structure of the CAN data frame. The speed of
data transmission is defined to be 500kbps. Additionally, the
received data must be sent via the serial port and the result
displayed in a terminal in the PC.

B. Initialization
In this part, all the registers needed for the functioning of
the CAN module - clock,input/output pins, interrupts and the
ADC - are initialised.
InitSysCtrl () ;
Gpio select () ;
InitECan ( ) ;
InitPieCtrl () ;
InitPieVectTable () ;
InitAdc () ;

Now the different interrupts (serial, CAN and Timer) are


assigned to the interrupt table of the DSP.

Fig. 1.

Table with car data.

For the first task, the CAN module is set for Self-test mode
(STM). This means that the data is sent from one mailbox to
the other and the acknowledge signal to the first mailbox is
generated internally. In the STM, mailboxes are programmed
with the respective identifiers of each data that needs to be
transmitted. The data to be transmitted is taken from the ADC
of the peripheral explorer kit.
For the second task, the data is received from the CANoe
module directly to the mailbox and it is sent to the PC via
serial port.
II. TASK 1 - S ELF T EST M ODE
A. Variable declaration
To demonstrate the task, we need to send and receive several
data. They are stored in the mailboxes. One of them is the
Wheel based vehicle speed. To receive this data, an integer
variable is being used. After transmission, some pre-defined
operation is performed on this data, and the results are stored
in a floating variable, to be transmitted serially. An example
for the Mailbox 1 is:
/ / MAILBOX 1 :
i n t wbv speed ;
f l o a t wbv speed RS232 = 0 ;

Similar process is defined for each of the other data in the


list.

/ / Interrupt Vectors
P i e V e c t T a b l e . SCIRXINTA = &SCIA RX isr ;
P i e V e c t T a b l e . TINT0 = &c p u t i m e r 0 i s r ;
P i e V e c t T a b l e . ECAN0INTA = &e c a n 0 i n t A i s r ;

Here, the mailboxes are enabled by accessing the CANMC


register. Also, the Self-test mode is set, by setting the STM
bit of this register to 1. Then, we also show how the interrupts
are enabled for one set of mailboxes ( mailbox 1 and 5),
by accessing the CANMIM,CANMIL registers. This interrupt
enabling is repeated for the other mailboxes in the same
manner.
ECanaShadow .CANMC. b i t . SCB= 1 ;
/ / S e l f t e s t mode :
ECanaShadow .CANMC. b i t . STM = 1 ;
ECanaRegs .CANMC. a l l = ECanaShadow .CANMC. a l l ;
ECanaShadow .CANMIM. a l l = 0 ;
/ / Enable mailbox i n t e r r u p t s :
ECanaShadow .CANMIM. b i t . MIM1 = 1 ;
ECanaShadow .CANMIM. b i t . MIM5 = 1 ;
// Interrupt levels :
ECanaShadow . CANMIL . b i t . MIL1 = 0 ;
ECanaShadow . CANMIL . b i t . MIL5 = 0 ;
/ / R e c e i v e r M a i l b o x #MBX1
/ * W r i t e t o M a i l b o x 1 m e s s a g e ID f i e l d * /
/ / Standard i d e n t i f i e r :
ECanaMboxes .MBOX1. MSGID . b i t . STDMSGID = 0x1A0 ;
ECanaMboxes .MBOX1. MSGID . b i t . IDE = 0 ;
ECanaMboxes .MBOX1. MSGID . b i t .AME = 0 ;
/ * Configure Mailbox 1 as R e c e i v e r mailbox * /
ECanaShadow .CANMD. a l l = ECanaRegs .CANMD. a l l ;
ECanaShadow .CANMD. b i t .MD1 = 1 ;
ECanaRegs .CANMD. a l l = ECanaShadow .CANMD. a l l ;
/ * Enable Mailbox 1 * /
ECanaShadow .CANME. a l l = ECanaRegs .CANME. a l l ;
ECanaShadow .CANME. b i t . ME1 = 1 ;
ECanaRegs .CANME. a l l = ECanaShadow .CANME. a l l ;

FINAL REPORT PSHEV. EECPS/STEPS MASTER, NOVEMBER 2015

In the above code, the mailbox 1 is configured for reception.


It is assigned an identifier. The different registers ( MSGID,
CANMD, CANME) are modified accordingly to enable this.
The same process is performed for the other receive mailbox
(mailbox 2, 3, 4) and assigned the correct identifiers from the
Fig. 1.
The following code shows the configuring of Mailbox 5
for transmission. The identifier is set as the same as for its
receive partner mailbox (mailbox 1). The different registers
(MSGID, CANMD, CANME ) are modified accordingly. The
Data length code is set for 8 bytes, by accessing the DLC byte
in the MSGCTRL register.
/ * Configure Mailbox 5 as t r a n s m i t mailbox * /
/ * W r i t e t o M a i l b o x 5 m e s s a g e ID f i e l d
*/
/ / Message i d e n t i f i e r :
ECanaMboxes .MBOX5. MSGID . b i t . STDMSGID = 0x1A0 ;
/ / Standard I d e n t i f i e r :
ECanaMboxes .MBOX5. MSGID . b i t . IDE = 0 ;
ECanaShadow .CANMD. a l l = ECanaRegs .CANMD. a l l ;
ECanaShadow .CANMD. b i t .MD5 = 0 ;
ECanaRegs .CANMD. a l l = ECanaShadow .CANMD. a l l ;
/ * Enable Mailbox 5 * /
ECanaShadow .CANME. a l l = ECanaRegs .CANME. a l l ;
ECanaShadow .CANME. b i t . ME5 = 1 ;
ECanaRegs .CANME. a l l = ECanaShadow .CANME. a l l ;
/ * W r i t e t o DLC f i e l d i n M a s t e r C o n t r o l r e g * /
ECanaMboxes .MBOX5. MSGCTRL . a l l = 0 ;
ECanaMboxes .MBOX5. MSGCTRL . b i t . DLC = 8 ;

Next, the serial port is initialised and the CPU timer is


enabled and the interrupt time set for every 10ms.
SCIA init ( ) ;
InitCpuTimers ( ) ;
Co n f ig C p uT i me r (& CpuTimer0 , 1 5 0 , 1 0 0 0 0 ) ;

The CPU timer and CAN interrupts are enabled and the
global interrupt is also assigned.
/ / E n a b l e CPUT i m e r 0 ISR :
P i e C t r l R e g s . PIEIER1 . b i t . INTx7 = 1 ;
/ / E n a b l e ECAN0INTA ISR :
P i e C t r l R e g s . PIEIER9 . b i t . INTx5 = 1 ;
IER | = 0 x0101 ;
/ / e n a b l e INT1 & INT9 i n IER

The output is formatted to be sent serially and displayed in


the PC.
/ / Send t h e h e a d e r m e s s a g e f o r RS232 :
s p r i n t f ( m e s s a g e t x s c i , \n\ r Speed Angle S p e e d r l w
S p e e d f l w S p e e d f r w S p e e d r r w App\n ) ;
do
{
/ / Send s i n g l e c h a r a c t e r :
S c i a R e g s . SCITXBUF= m e s s a g e t x s c i [ i n d e x + + ] ;
w h i l e ( S c i a R e g s . SCICTL2 . b i t . TXEMPTY == 0 ) ; / / w a i t
f o r TX e m p t y
}
/ / W a i t f o r TX e m p t y :
w h i l e ( m e s s a g e t x s c i [ i n d e x ] ! = \0 ) ;
index =0;

C. Infinite loop
Here, the DSP is waiting in a sleep mode. When a CAN
interrupt is produced then the program enters the internal loop
and sends the data serially.

while ( 1 )
{
/ / I f a new CAN Frame was r e c e i v e d :
i f ( CANreceived ==TRUE)
{
/ / The new v a l u e s a r e SCI t r a n s m i t t e d :
CANreceived =FALSE ;
s p r i n t f ( m e s s a g e t x s c i , \ r %2.1 fkm / h %c %4.1 f d e g %2.1
fkm / h %2.1 fkm / h %2.1 fkm / h %2.1 fkm / h %3.1 f%c ,
wbv speed RS232 , s t w a n g d i r , stw ang RS232 ,
rlw speed RS232 , flw speed RS232 ,
frw speed RS232 , rrw speed RS232 , app RS232 , %
);
do
{
/ / Send s i n g l e c h a r a c t e r :
S c i a R e g s . SCITXBUF= m e s s a g e t x s c i [ i n d e x + + ] ;
/ / W a i t f o r TX e m p t y :
w h i l e ( S c i a R e g s . SCICTL2 . b i t . TXEMPTY == 0 ) ;
}
/ / W a i t f o r TX e m p t y :
w h i l e ( m e s s a g e t x s c i [ i n d e x ] ! = \0 ) ;
index =0;
} / / i f ( C A N r e c e i v e d==TRUE )
i f ( TXSCI pending ==TRUE)
{
do
{
/ / Send s i n g l e c h a r a c t e r :
S c i a R e g s . SCITXBUF= m e s s a g e t x s c i [ i n d e x + + ] ;
/ / W a i t f o r TX e m p t y :
w h i l e ( S c i a R e g s . SCICTL2 . b i t . TXEMPTY == 0 ) ;
}
/ / W a i t f o r TX e m p t y :
w h i l e ( m e s s a g e t x s c i [ i n d e x ] ! = \0 ) ;
index =0;
TXSCI pending =FALSE ;
} / / i f ( T XS C I pe n d i ng==TRUE )
} / / while (1)

D. Fuction expansions
The functions expanded below are all declared in the initial
part of the program.
First, the serial interrupt function is defined. The bit baud
rates are defined as required (115200 bps).
void SCIA init ( )
{
/ / 1 s t o p b i t , No l o o p b a c k No p a r i t y , 8 c h a r b i t s ,
/ / a s y n c mode , i d l e l i n e p r o t o c o l :
S c i a R e g s . SCICCR . a l l =0 x0007 ;
/ / E n a b l e TX , RX , i n t e r n a l SCICLK ,
/ / D i s a b l e RX ERR , SLEEP , TXWAKE:
S c i a R e g s . SCICTL1 . a l l =0 x0003 ;
S c i a R e g s . SCIHBAUD
= 40 >> 8 ;
/ / Highbyte
S c i a R e g s . SCILBAUD
= 40 & 0 x00FF ;
/ / Lowbyte
/ / E n a b l e SCI A RxISR :
S c i a R e g s . SCICTL2 . b i t . RXBKINTENA = 1 ;
/ / R e l i n q u i s h SCI f r o m R e s e t :
S c i a R e g s . SCICTL1 . a l l = 0 x0023 ;
}

The time interrupt is produced every 10ms. During this time,


the data data is loaded from the ADC to the mailboxes prepared for transmission. It checks if the previous transmission
has finished, and if so, it proceeds to the new transmission. In
this case, we demonstrate the case for 20ms interrupt where the
counter is used. The other cases are the same, but without the
use of counter. The bits are selected according to the numbe
of bits assigned for each data in the Fig. 1.

FINAL REPORT PSHEV. EECPS/STEPS MASTER, NOVEMBER 2015

/ * Timer0 I n t e r r u p t F u n c t i o n * /
i n t e r r u p t void c p u t i m e r 0 i s r ( void )
{
/ / Receiving the required data i n t o the v a r i a b l e
declared :
app = A d c M i r r o r . ADCRESULT0 ;
ECanaMboxes .MBOX8.MDH. b y t e . BYTE5 = ( app & 0 x00FF ) ;
/ / I f prev . transmission completed :
i f ( CanMbox8Quit == TRUE && c o u n t e r t i m e r == 1 )
{
/ / t r a n s m i t a new one
CanMbox8Quit=FALSE ;
ECanaShadow . CANTRS . a l l = 0 ;
/ / S e t TRS f o r m a i l b o x u n d e r t e s t :
ECanaShadow . CANTRS . b i t . TRS8 = 1 ;
ECanaRegs . CANTRS . a l l = ECanaShadow . CANTRS . a l l ;
/ / R e s e t c o u n t e r o f 20 ms :
counter timer = 0;
}
/ / A c k n o w l e d g e t h e PIE g r o u p 1 :
P i e C t r l R e g s . PIEACK . b i t . ACK1 = 1 ;
/ / Increment of counter :
c o u n t e r t i m e r ++;
}

stw ang dir = + ;


}
CANreceived = TRUE ;
} / / i f ( m a i l l b o x n r ==2)
/ / Transmission
i f ( m a i l b o x n r == 6 )
{
ECanaShadow . CANTA. a l l = 0 ;
ECanaShadow . CANTA. b i t . TA6 = 1 ;
//
clear transmission
ECanaRegs . CANTA. a l l = ECanaShadow . CANTA. a l l ;
CanMbox6Quit = TRUE ;
/ / the flag of
CAN f r a m e t r a n s m i t t e d i s a c t i v a t e d
}
P i e C t r l R e g s . PIEACK . b i t . ACK9 = 1 ;
//
a c k n o w l e d g e t h e PIE g r o u p 1
}

III. TASK 2 - R ECEPTION


For the reception part, the Self-test mode is commented out.
By default the value of the STM bit is zero, so the CAN now
is either transmitting or receiving.

Here, the program checks for the number of the mailbox that
is raising the CAN interrupt and makes a transmission/reception based on its number. In this example we use the mailbox
2 (for reception) and mailbox 6 (for transmission). In mailbox
6, transmission is done by charging the values of the ADC to
the ECAN mailbox register, making sure that the number of
bits are the same as that in the given data. This message is
placed in the CAN bus and is read by mailbox 2. Once the
message is read by the intended mailbox, it saves the value
to an array. We then perform an equation to this array with
the correct bits and save the result in a new variable which is
used for serial transmission.
A similar structure is used for transmitting and receiving
data of the other mailboxes.

/ / ECanaShadow . CANMC. b i t . STM = 1 ;


f o r s e l f t e s t mode

Additionally, the mailbox intended for transmission are


commented out. Also, the code for the CPU timer interrupt is
removed. Now , the program only has the receive mailboxes
enabled and is ready to process the data from the CANoe
module and display it serially.
IV. C ONCLUSIONS
The CAN transmission and reception has been tested
through this program. The results of the same are shown in
Fig. 2. In this part the self test mode and standalone reception
of the CAN module are demonstrated.

i n t e r r u p t void e c a n 0 i n t A i s r ( void )
{
unsigned i n t mailbox nr ;
/ / M a i l b o x t h a t s e t GMIF0I0EN :
m a i l b o x n r = ECanaRegs . CANGIF0 . b i t . MIV0 ;
/ / Reception
i f ( m a i l b o x n r == 2 )
{
m e s s a g e [ 0 ] = ECanaMboxes .MBOX2.MDL.
read message
m e s s a g e [ 1 ] = ECanaMboxes .MBOX2.MDL.
m e s s a g e [ 2 ] = ECanaMboxes .MBOX2.MDL.
m e s s a g e [ 3 ] = ECanaMboxes .MBOX2.MDL.
m e s s a g e [ 4 ] = ECanaMboxes .MBOX2.MDH.
m e s s a g e [ 5 ] = ECanaMboxes .MBOX2.MDH.
m e s s a g e [ 6 ] = ECanaMboxes .MBOX2.MDH.
m e s s a g e [ 7 ] = ECanaMboxes .MBOX2.MDH.

Fig. 2.

b y t e . BYTE0 ;
byte
byte
byte
byte
byte
byte
byte

//

. BYTE1 ;
. BYTE2 ;
. BYTE3 ;
. BYTE4 ;
. BYTE5 ;
. BYTE6 ;
. BYTE7 ;

/ / C l e a r t h e s t a t u s f l a g RMP1
/ / and p r e p a r e MBX1 f o r n e x t r e c e i v e
ECanaRegs .CANRMP. b i t . RMP2 = 1 ;
stw ang RS232 = ( ( ( u n s i g n e d i n t ) m e s s a g e [ 1 ] & 0 x7F )
* 256 + ( u n s i g n e d i n t ) m e s s a g e [ 0 ] ) * 0 . 0 4 3 7 5 ;
i f ( ( m e s s a g e [ 1 ] & 0 x80 ) == 1 2 8 )
{
s t w a n g d i r = ;
}
i f ( ( m e s s a g e [ 1 ] & 0 x80 ) == 0 )
{

/ / C o n f i g u r e CAN

Visualisation of the simulation.

Você também pode gostar