Rev. 4229B–CAN–01/03
1
CAN Bus Drivers for Atmel C51 Products
This application note describes the CAN Driver functions Version 1.0.0 for the CAN
microcontroller T89C51CC01.
The purpose of these functions is to help customer start a CAN application by provide
him some basic functions.
The package contain following files:
•
can_lib.c
•
can_lib.h
•
compiler.h
The KEIL compiler was used for compilation.
CAN
Microcontrollers
Application Note
2
CAN Application Note
4229B–CAN–01/03
Interface Description
Types definition
Typedef declared in compiler.h file.
•
Uchar: unsigned char
•
Uint16: unsigned short
•
Uint32: unsigned long
•
Int16: signed short
•
Int32: signed long
Typedef declared in can_lib.h file.
•
can_id_t: union
–
Uint32 ext:
"ext" field allows access to 29-bit identifier (extended mode).
–
Uint16 std:
"std" field allows access to 11-bit identifier (standard mode).
–
Uchar tab[4]:
"tab" allow access by byte to identifier.
•
st_can_msg_t: struct
–
can_id_t id:
identifier of the CAN message, 11 bits in standard mode and 29 bits in
extended mode.
–
Uchar ctrl:
–
b7: RTR -> Data Frame = 0 or Remote Frame = 1 (access by mask
MSK_CTRL_RTR)
–
b4: IDE -> Standard = 0 or Extended = 1(access by mask MSK_CTRL_IDE)
–
b3:0 DLC -> number of data used dlc_t (access by mask MSK_CTRL_DLC)
–
Uchar * pt_data:
pointer on Uchar table of data (size max = 8).
•
channel_t: enum
CHANNEL_0 .. CHANNEL_14, NO_CHANNEL
•
dlc_t: enum
CONF_DLC_0 .. CONF_DLC_8
3
CAN Application Note
4229B–CAN–01/03
Functional Description
CanSetBRP
This function is used to initialize the prescaler of CAN controller with parameter
"prescaler".
1.
Prototype:
–
void CanSetBRP (Uchar prescaler)
2.
Input:
–
prescaler: Value of field BRP (Baud Rate Prescaler) in CANBT1 register
3.
Output:
–
void
CanSetSJW
This function is used to initialize the re_Synchronization Jump Width of CAN controller
with parameter "sjw".
1.
Prototype:
–
void CanSetSJW (Uchar sjw)
2.
Input:
–
sjw: Value of field SJW (re-Synchronization Jump Width) in CANBT2 register
3.
Output:
–
void
CanSetPRS
This function is used to initialize the programming time segment of CAN controller with
parameter "prs"
1.
Prototype:
–
void CanSetPRS (Uchar prs)
2.
Input:
–
prs: Value of field PRS (Programming time Segment) in CANBT2 register
3.
Output:
–
void
CanSetPHS2
–
This function is used to initialize the phase segment 2 of CAN controller with
parameter "phs2".
1.
Prototype:
–
void CanSetPHS2 (Uchar phs2);
2.
Input:
–
phs2: Value of field PHS2 (Phase Segment 2) in CANBT3 register.
3.
Output:
–
void
CanSetPHS1
This function is used to initialize the phase segment 1 of CAN controller with parameter
"phs1".
1.
Prototype:
–
void CanSetPHS1 (Uchar phs1)
2.
Input:
–
phs1: Value of field PHS1 (Phase Segment 1) in CANBT3 register.
3.
Output:
–
void
4
CAN Application Note
4229B–CAN–01/03
ConfChannel_Rx
This function is used to configure a channel in reception mode.
Before calling this function the corresponding channel must be selected.
Make sure the channel is free because no verification is performed by the function.
To increase that the performance global variables are used.
1.
Prototype:
–
void ConfChannel_Rx (void)
2.
Input:
–
void
3.
Output:
–
void
4.
Global Variables:
–
can_id_t can_rx_filt: Value of the filter for identifier
–
can_it_t can_rx_msk: Value of the mask for identifier
–
Uchar conf_rx:
–
b7: mask ide field (CONF_MSK_IDE) or not (CONF_NOMSK_IDE)
–
b5: if mask on rtr field accept only RTR frame (CONF_RTR) or data frame
(CONF_NORTR)
–
b4: if mask on ide field accept only standard identifier (CONF_NOIDE) or
extended (CONF_IDE)
–
b2: mask rtr field (CONF_MSK_RTR) or not (CONF_NOMSK_RTR)
–
b0: configure channel in reception (CONF_NOBUFFER) or in buffer
reception (CONF_BUFFER)
5.
C Example:
–
To configure channel 3 in reception only for standard frame data accepting
only identifier start by 0x35X.
–
can_rx_filt.std = 0x35F;
–
can_rx_msk.std = 0x350;
–
conf_rx =
CONF_NOIDE|CONF_MSK_IDE|CONF_NOBUFFER|CONF_NORTR;
–
CAN_SET_CHANNEL(CHANNEL_3);
–
ConfChannel_Rx();
SendCanMsg
This function is used to send a message on the CAN bus.
Before calling this function the corresponding channel must be selected.
Make sure that the channel is free because no verification is performed by the function.
To increase the performance global variables are used.
1.
Prototype:
–
void SendCanMsg (void)
2.
Input:
–
void
3.
Output:
–
void
4.
Global Variables:
–
can_id_t can_tx_id: Value of the filter for identifier
–
Uchar * pt_candata_tx: Table with data to transmit
5
CAN Application Note
4229B–CAN–01/03
–
Uchar conf_tx:
–
b5: remote frame (CONF_RTR) or data frame (CONF_NORTR)
–
b4: standard identifier (CONF_NOIDE) or extended identifier (CONF_IDE)
–
b3-0: number of data bytes to transmit (used dlc_t, CONF_DLC_0...
CONF_DLC_8)
5.
C Example:
–
To send standard frame with id = 0x318, and 8 data bytes.
–
Uchar data[8] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77};
–
st_can_msg_t can_msg_tx = {0x318, CONF_NOIDE | CONF_NORTR |
CONF_DLC_8, data[]};
–
–
conf_tx = can_msg_tx.ctrl;
–
CAN_SET_CHANNEL(CHANNEL_3);
–
SendCanMsg ();
ReadCanMsg
This function is used to read a message received on "num_channel" and store it in the
structure pt_st_can_msg.
Before calling this function the corresponding channel must be selected.
To increase the performance global variables are used.
1.
Prototype:
–
void ReadCanMsg (Uchar next_conf)
2.
Input:
–
Uchar next_conf: This field select the configuration of the corresponding
channel after reading message.
–
channel must be disable = CHANNEL_DISABLE
–
channel enable in reception with same configuration =
CHANNEL_RX_ENABLE
–
channel must be enable with same configuration but in buffer mode =
CHANNEL_RXB_ENABLE.
3.
Output:
–
void
4.
Global Variables:
–
can_msg_t * pt_st_can_rx: application structure for received message.
5.
C Example:
–
For read message received on channel 5, and reconfigure the channel in
reception.
–
Uchar data[8];
–
st_can_msg_t can_msg_rx = {0x00, 0x00, data[0]};
–
–
pt_st_can_rx = &can_msg_rx;
–
CAN_SET_CHANNEL(CHANNEL_5);
–
ReadCanMsg (CHANNEL_RX_ENABLE);
Fct_can_it
This function is used to manage interrupt.
This function must be called in application interrupt routine.
6
CAN Application Note
4229B–CAN–01/03
Four functions can be called if they are declared by user:
•
void can_fct_it_rxok (void):
–
When a message is received this function can be called.
–
To use this function:
#define USER_CAN_FCT_IT_RXOK in a config.h file.
–
Create this function in user application.
•
void can_fct_it_txok (void):
–
When a message is sent this function can be called.
–
To use this function:
#define USER_CAN_FCT_IT_TXOK in a config.h file.
–
Create this function in user application.
•
void can_fct_it_error (void):
–
When a error occurs on a specific channel this function can be called.
–
To use this function:
#define USER_CAN_FCT_IT_ERROR in config.h file
–
Create this function in user application.
•
void can_fct_it_gen (void):
–
When a error occurs which match with any channel this function can be
called.
–
To use this function:
#define USER_CAN_FCT_IT_GEN
–
Create this function in user application.
1.
Example of config.h file
–
#define USER_CAN_FCT_IT_RXOK
–
extern void can_fct_it_rxok (void);
–
#define USER_CAN_FCT_IT_TXOK
–
extern void can_fct_it_txok (void);
–
#undef USER_CAN_FC_IT_ERROR
–
#undef USER_CAN_FCT_IT_GEN
Fct_tim_ovf_it
This function is used to manage CAN timer overflow interrupt.
It must be called in application interrupt routine.
Fct_tim_ovf_it can called can_fct_it_timovf only if this function is declared by user
•
void can_fct_it_timovf (void):
–
When the CAN timer overflows from 0xFFFF to 0x0000 this function can be
called.
–
To use this function:
#define USER_CAN_FCT_IT_TIMOVF in a config.h file.
–
Create this function in user application.
Macros Description
•
CAN_CONTROLLER_ENABLE/DISABLE
–
These macros enable or disable the CAN macro.
–
Must be called before any action on the CAN bus is initiated.
•
CAN_CONTROLLER_RESET
–
This macro reset the CAN macro.
7
CAN Application Note
4229B–CAN–01/03
–
Must be called once.
•
CAN_IT_ENABLE/DISABLE
–
These macros enable or disable general CAN interrupt.
–
The macro CAN_IT_ENABLE must be called for used all other interrupts.
•
CAN_RX_IT_ENABLE/DISABLE
–
These macros enable or disable interrupt on flag RXOK in CANSTCH
register.
•
CAN_TX_IT_ENABLE/DISABLE
–
These macros enable or disable interrupt on flag TXOK in CANSTCH
register.
•
CAN_ERCH_IT_ENABLE/DISABLE
–
These macros enable or disable interrupt on flags BERR, SERR, CERR,
FERR and AERR in CANSTCH register.
•
CAN_ERG_IT_ENABLE/DISABLE
–
These macros enable or disable interrupt on flags SERG, CERG, FERG and
AERG in CANGIT register.
•
CAN_BUF_IT_ENABLE/DISABLE
–
These macros enable or disable interrupt on flag OVRBUF in CANGIT
register.
•
CANTIM_IT_ENABLE/DISABLE
–
These macros enable or disable interrupt on flag OVRTIM in CANGIT
register.
•
CAN_CHANNEL_IT_ENABLE/DISABLE(channel_t)
–
These macros enable or disable interrupt on selected channel.
•
CAN_SET_CHANNEL(channel_t)
–
This macro change channel with the channel pasted in parameter.
–
All registers depending of the channel are updated.
•
CAN_GET_CHANNEL
–
return the channel selected.
Typical use
#define BRP_500k 0x00
#define SJW_500k 0x00
#define PRS_500k 0x02
#define PHS1_500k 0x03
#define PHS2_500k 0x03
void can_task_init(void)
{
–
CAN_CONTROLLER_RESET;
CAN_IT_DISABLE;
CANTIM_IT_DISABLE;
RazAllMailbox();
CanSetBRP(BRP_500k);
CanSetSJW(SJW_500k);
CanSetPRS(PRS_500k);
CanSetPHS1(PHS1_500k);
8
CAN Application Note
4229B–CAN–01/03
CanSetPHS2(PHS2_500k);
CAN_CONTROLLER_ENABLE;
can_rx_filt.ext = 0x00;
can_rx_msk.ext = 0x00;
conf_rx = CONF_NOIDE|CONF_NOMSK_IDE|CONF_NOBUFFER;
CAN_SET_CHANNEL(CHANNEL_0);
ConfChannel_Rx();
CAN_IT_ENABLE;
CAN_TX_IT_ENABLE;
CAN_RX_IT_ENABLE
}
void can_task(void)
{
–
CAN_SET_CHANNEL(CHANNEL_1);
can_tx_id = can_200.id;
conf_tx = can_200.ctrl;
pt_candata_tx = can_200.pt_donne;
SendCanMsg();
CAN_SET_CHANNEL(CHANNEL_2);
can_tx_id = can_208.id;
conf_tx = can_208.ctrl;
pt_candata_tx = can_208.pt_donne;
CAN_CHANNEL_IT_ENABLE(CHANNEL_2);
SendCanMsg();
}
Acronyms
CAN: Controller Area Network (BOSCH Standard)
References
•
Datasheet T89C51CC01
Printed on recycled paper.
ATMEL
®
is a registered trademark of Atmel. .
Other terms and product names may be the trademarks of others.
© Atmel Corporation 2002.
Atmel Corporation makes no warranty for the use of its products, other than those expressly contained in the Company’s standard warranty
which is detailed in Atmel’s Terms and Conditions located on the Company’s web site. The Company assumes no responsibility for any errors
which may appear in this document, reserves the right to change devices or specifications detailed herein at any time without notice, and does
not make any commitment to update the information contained herein. No licenses to patents or other intellectual property of Atmel are granted
by the Company in connection with the sale of Atmel products, expressly or by implication. Atmel’s products are not authorized for use as critical
components in life support devices or systems.
Atmel Headquarters
Atmel Operations
Corporate Headquarters
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 487-2600
Europe
Atmel Sarl
Route des Arsenaux 41
Case Postale 80
CH-1705 Fribourg
Switzerland
TEL (41) 26-426-5555
FAX (41) 26-426-5500
Asia
Room 1219
Chinachem Golden Plaza
77 Mody Road Tsimhatsui
East Kowloon
Hong Kong
TEL (852) 2721-9778
FAX (852) 2722-1369
Japan
9F, Tonetsu Shinkawa Bldg.
1-24-8 Shinkawa
Chuo-ku, Tokyo 104-0033
Japan
TEL (81) 3-3523-3551
FAX (81) 3-3523-7581
Memory
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 436-4314
Microcontrollers
2325 Orchard Parkway
San Jose, CA 95131
TEL 1(408) 441-0311
FAX 1(408) 436-4314
La Chantrerie
BP 70602
44306 Nantes Cedex 3, France
TEL (33) 2-40-18-18-18
FAX (33) 2-40-18-19-60
ASIC/ASSP/Smart Cards
Zone Industrielle
13106 Rousset Cedex, France
TEL (33) 4-42-53-60-00
FAX (33) 4-42-53-60-01
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
TEL 1(719) 576-3300
FAX 1(719) 540-1759
Scottish Enterprise Technology Park
Maxwell Building
East Kilbride G75 0QR, Scotland
TEL (44) 1355-803-000
FAX (44) 1355-242-743
RF/Automotive
Theresienstrasse 2
Postfach 3535
74025 Heilbronn, Germany
TEL (49) 71-31-67-0
FAX (49) 71-31-67-2340
1150 East Cheyenne Mtn. Blvd.
Colorado Springs, CO 80906
TEL 1(719) 576-3300
FAX 1(719) 540-1759
Biometrics/Imaging/Hi-Rel MPU/
High Speed Converters/RF Data-
com
Avenue de Rochepleine
BP 123
38521 Saint-Egreve Cedex, France
TEL (33) 4-76-58-30-00
FAX (33) 4-76-58-34-80
literature@atmel.com
Web Site
http://www.atmel.com
4229B–CAN–01/03
/xM