AN1105 STMicroelectronics, AN1105 Datasheet

no-image

AN1105

Manufacturer Part Number
AN1105
Description
ST7 PCAN PERIPHERAL DRIVER
Manufacturer
STMicroelectronics
Datasheet

Available stocks

Company
Part Number
Manufacturer
Quantity
Price
Part Number:
AN1105W-12-RR
Quantity:
13 804
Part Number:
AN1105W-22-RR
Manufacturer:
SANYO
Quantity:
8 570
Part Number:
AN1105W-TR
Manufacturer:
PANASONIC
Quantity:
27 995
Part Number:
AN1105W-TR
Manufacturer:
STANLEY
Quantity:
20 000
INTRODUCTION
The Controller Area Network (CAN) norm defines a fast and robust serial bus protocol, suited
for local networking of intelligent devices such as microcontrollers, sensors and actuators. It is
now widely used, mostly in the automotive domain, but also for home automation and indus-
trial equipment control.
Several members of the ST7 MCU family have a built-in CAN peripheral named pCAN, which
allows them to be used as nodes in a CAN network. A software driver provided is by ST to help
you start designing and writing applications using the ST7 pCAN cell.
The purpose of the following application note is to explain to you how to use the driver, and
how it works. Thus, you can either build your software from the provided files, or modify them
to meet specific needs.
You will find in this application note:
AN1105/0801
– a brief description of the CAN protocol
– an overview of the pCAN peripheral
– the complete description of the user interface
– the step-by-step development of an example application using the driver features
– a technical report including a description of algorithms and internal data types
ST7 pCAN PERIPHERAL DRIVER
APPLICATION NOTE
by Central European MCU Support
AN1105
Version 1.3
1/100
1

Related parts for AN1105

AN1105 Summary of contents

Page 1

... AN1105/0801 APPLICATION NOTE ST7 pCAN PERIPHERAL DRIVER by Central European MCU Support AN1105 Version 1.3 1/100 1 ...

Page 2

INTRODUCTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Page 3

DRIVER CODE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ...

Page 4

ST7 pCAN PERIPHERAL DRIVER 1 CAN COMMUNICATION PROTOCOL In the early 80s, electronic applications appeared in the automotive world. The need gradually arose to establish real-time communications between various types of on-board equipment. In 1986, the CAN bus protocol, designed ...

Page 5

Message prioritization – The identifier of a message also indicates the priority of its carrier. During the arbitration phase, a bitwise comparison of the different identifiers is performed. The message with the higher identifier being the more urgent, it wins ...

Page 6

ST7 pCAN PERIPHERAL DRIVER manages frame shaping and acknowledgment mechanisms, bus arbitration and checks the formal correctness of the data transmitted and received. The physical layer is only partly defined, and the user is free to choose the transmission me- ...

Page 7

A RTR bit used to differentiate data and remote frames dominant in a data frame. A 6-bit control field, including: – A 4-bit data length code bits giving the size (in bytes) of the data field – ...

Page 8

ST7 pCAN PERIPHERAL DRIVER Figure 3. CAN Remote Frame 19 bits Arbitration Field R 11-bit Identifier D RTR = 1 SOF 1.2.3 CAN Error Frame The CAN Error frame is sent by a node as soon as it detects a ...

Page 9

Bit Stuffing Once built, a frame is passed to the Transfer sub-layer, where the CRC is calculated. Then, Stuffing ...

Page 10

ST7 pCAN PERIPHERAL DRIVER To deal with signal phase variations on the transmission line due mostly to desynchronization between clocks of bus participants, TSEG1 and TSEG2 are of variable length. A resynchroni- zation of the local clock occurs on every ...

Page 11

Message Acknowledgement Any cell having received a message correctly, regardless of the result of the acceptance test, must acknowledge it by sending a dominant bit during the ACK slot of the current frame, thus overwriting the recessive bit sent ...

Page 12

ST7 pCAN PERIPHERAL DRIVER Figure 8. CAN Error States 128 blocks of 11 suc- cessive recessive bits Bus Off When both counters contain values between 0 and 127, the CAN node is in Error Active state, which means that it ...

Page 13

Two 11-bit filters and two 11-bit masks are used for hardware filtering with identifier/iden- tifier-range definition. The masks define which bits of the filters are to be ignored and which are to be tested. Any message whose identifier does ...

Page 14

ST7 pCAN PERIPHERAL DRIVER Any event (successful transmission, data reception, error, state change, overrun) can gen- erate a maskable interrupt. The Interrupt Control Register (see datasheet p.112) allows you to set/reset these masks. Note that each buffer has its own ...

Page 15

CAN DRIVER To quickly start developing your own CAN applications using the ST7 pCAN peripheral, STMi- croelectronics provides you with a driver. The source code (can.h and can.c) is given as an ex- ample, and can therefore be used ...

Page 16

ST7 pCAN PERIPHERAL DRIVER functions defined in can_custom.c on certain events. The user only has to enter these func- tions according to his needs before compiling the software. 2.1.3 Principle of Use During compilation, the values of the hardware control ...

Page 17

The CAN_Switch_Off and CAN_Sleep functions are used to switch the cell to Standby mode. The first function kills any pending jobs and erases all data in the reception buffers, even if the data is unread. The second function is ...

Page 18

ST7 pCAN PERIPHERAL DRIVER Figure 11. Transmission IT Procedure What you see 1 IT Driver Processing Successful Transmission – In the event of a reception IT, after having found which buffer is involved, the driver performs an optional software filtering, ...

Page 19

Error ITs are only processed if the DEBUG option was chosen in can_custom.h. Then, the application is notified of any other events through function calls. (Functions entered in can_custom.c). 2.1.5 User Interface Description This section describes the user interface ...

Page 20

ST7 pCAN PERIPHERAL DRIVER If CTRUE, the data can be modified. Take care to check and/or initialize it before passing a buffer to the driver. Init_Data -type variables typedef struct { u8 brpr_init; u8 btr_init; u8 fhr1_init; u8 flr1_init; u8 ...

Page 21

Describes the different states of the cell. typedef enum {CFALSE=0,CTRUE=1}CAN_Bool; Custom Boolean type variable. typedef enum {BUS_WAKEUP,SOFT_WAKEUP}WakeUp_Cause; Possible parameters of the CAN_Sleep and CAN_Switch_Off functions. (See "Functions".) typedef enum{CAN_SLEEP_ERROR,CAN_SLEEP_SUCCESS,CAN_SWITCH_ON_SUCCES,CAN_SWITCH_ON_FA ILURE}CAN_Switch_Error; Possible values returned by CAN_Sleep, CAN_Switch_Off and CAN_Switch_On functions ...

Page 22

ST7 pCAN PERIPHERAL DRIVER CAN Cell Power-on initialization routine. During its execution, all registers are initialized with values entered in can_custom.h or calculated from the compilation options chosen Description It initializes the transmission queue. This function must be called in ...

Page 23

Puts the CAN node into Standby state. This function aborts any pending transmissions and does not wait for the reception buffers to be read. Then, it resets the driver in the same state as after power-on ini- tialization. If BUS_WAKEUP ...

Page 24

ST7 pCAN PERIPHERAL DRIVER Puts a buffer into the transmission queue, and may request an immediate transmis- sion if the queue is empty. The buffer_rw field has to be CFALSE before passing it to the function immedi- ately ...

Page 25

INIT_FLR0 0x00 #define INIT_MHR0 0x00 #define INIT_MLR0 0x00 #define INIT_FHR1 0x00 #define INIT_FLR1 0x00 #define INIT_MHR1 0x00 #define INIT_MLR1 0x00 These values are used to initialize hardware filters and masks. (See Compilation Options //Start options //#define WAKE_UP_PULSE #define RUN_ON_START_UP ...

Page 26

ST7 pCAN PERIPHERAL DRIVER If DEBUG is defined, you can then chose to add the following options: //#define GENERAL_RECEPTION_ERROR All reception errors trigger an interrupt. You CANNOT use this option and the Status Change Notification function together because both use ...

Page 27

CAN_Bus_Off_Notification(void) These functions are called after a status change. Option: #define STATUS_CHANGE_NOTIFICATION void CAN_General_Reception_Error_Notification(void) This function notifies the application of any hardware reception errors. Option: #define DEBUG and #define GENERAL_RECEPTION_ERROR void CAN_Reception_Status(CAN_Receive_Error status) This function is called after a ...

Page 28

ST7 pCAN PERIPHERAL DRIVER – Identifiers may exist on the bus that do not concern our cell. 2.2.2 Cell Configuration Open then the can_custom.h file. You can see the two first lines of code: //#define WAKE_UP_PULSE //#define RUN_ON_START_UP According to ...

Page 29

BTR Let’s choose, for example, BTR = 3, BS1 = 3, BS2 = 2 (second row). We have to complete the code: #define INIT_BRPR 0x03 #define INIT_BTR 0x23 2.2.2.2 Filtering The driver is able to filter out all ...

Page 30

ST7 pCAN PERIPHERAL DRIVER Now look at the can_custom.h file: There are several #define statements, including: #define INIT_FHRi 0x00 #define INIT_FLRi 0x00 where i is either These are the two registers containing hardware filters. In the FHRi ...

Page 31

We have then to write our code. Look at the last lines of can_custom.h. There is a set of #de- fine options want to be notified neither of status changes, nor of errors, we will leave them as ...

Page 32

ST7 pCAN PERIPHERAL DRIVER example! You can implement a buffer for each message type, a FIFO of standardized buffers etc. Note 2: For safe programming, you should systematically verify whether the buffer is not currently being read/written, and whether the ...

Page 33

The structure is initialized here with 0x500 ID, which has a length of five and five data items. Note real network, the message would not be statically initialized, but would be filled with the content of one or ...

Page 34

ST7 pCAN PERIPHERAL DRIVER CAN_Buffer periodic_transmission; In the TIMA_Init() function, make sure the following lines are included: TACR1=0x40 TACR2=0x08 TAOC1HR=0x3A TAOC1LR=0x98 and finally, #asm LD _TAOC2HR,A; /* Write the Output Compare 2 high register to disable the OC2 */ LD ...

Page 35

CAN_Transmit_Request(&periodic_transmission); } //We have given a transmission order here, that will be called on every //timer IT if the message is not already in the transmission queue. //The data is filled each time with the value of the A/D converter ...

Page 36

ST7 pCAN PERIPHERAL DRIVER ADC_Init(); //Initializes the ADC if ((CAN_First_Init())==CAN_INIT_FAILURE) //Initializes the CAN peripheral { return; } TIMA_Init();//Initializes the timer _asm(“RIM”);// EnableInterrupts; /* /*---------------------------- while ( Finished! Now compile the entire code and run the software. ...

Page 37

For example, rename function to function_init_call. – In the link file (extension .lkf), define an alias the following way: +def _function=_function_init_call //!! Note the underscore as first character. – If the function takes parameters, add also: +def _function$L=_function_init_call$L//!! This ...

Page 38

ST7 pCAN PERIPHERAL DRIVER 3 DETAILED DESCRIPTION In the following section we will give a detailed description of function algorithms. We will also look at the internal aspects of the driver. 3.1 USER INTERFACE FUNCTIONS As described above in the ...

Page 39

CAN_Status CAN_Get_Status (void) Figure 14. CAN_Get_Status Flowchart Begin N RUN=0 Y Returns CAN_BUS_PASSIVE CAN_STANDBY CAN_Switch_Error CAN_Switch_Off(WakeUp_Cause) Figure 15. CAN_Switch_Off Flowchart Begin Sets RUN = 0 Sets Counter = 0xFFF Counter = 0 Returns CAN_SLEEP_FATAL CAN_Switch_Error CAN_Sleep (WakeUp_Cause) N EPSV=1 BOFF=1 ...

Page 40

ST7 pCAN PERIPHERAL DRIVER Figure 16. CAN_Sleep Flowchart Begin Y status_counter=0 N Returns CAN_SLEEP_ERROR Returns CAN_SLEEP_FATAL Returns CAN_SLEEP_SUCCESS Modifies CANICR register following the parameter (enables or disables IT) CAN_Switch_Error CAN_Switch_On(Init_Data_Ptr,CAN_Bool) Figure 17. CAN_Switch_On Flowchart Begin Y RUN = 0 N ...

Page 41

Figure 18. CAN_Transmit_Request Flowchart Begin message_to_send.buffer_rw=CTRUE Returns CAN_TRANSMIT_FAILURE Returns CAN_FIFO_FULL message_to_send.buffer_rw=CFALSE Returns CAN_TRANSMIT_FATAL 3.2 INTERNAL FUNCTIONS AND DATA TYPES 3.2.1 Internal Data Types and Variables 3.2.1.1 Transmission FIFO To avoid losing data, messages to be sent are queued in a ...

Page 42

ST7 pCAN PERIPHERAL DRIVER FIFO Management Structure In the main structure, we find: typedef struct { u8 fifo_size; FIFO_Object* first_object; FIFO_Object* last_object; CAN_Bool isinuse; } FIFO; The structures shown here look more like parts of a chained-list. Indeed, the data-type ...

Page 43

Figure 19. FIFO Transmission System Next data to send Next Object 1 & message Preceding For more information, refer to CAN_Out_Queue). 3.2.1.2 Software Filters Once the SOFTWARE_FILTERS option is defined in can_custom.h and the list of filters is completed, the ...

Page 44

ST7 pCAN PERIPHERAL DRIVER typedef enum {CAN_FILTER_MATCH,CAN_FILTER_NO_MATCH}CAN_Filter_Status; Return values of the software filtering routine. typedef enum {ITERR,ITRECEPT,ITTRANSMIT,ITSCIF}IT_Type; Return values of the routine that determines which IT has to be processed first. typedef enum {CAN_CLEAN_FATAL,CAN_CLEAN_SUCCESS,CAN_CLEAN_FAILURE}CAN_Clean_Error; Return values of the Cleaning function. ...

Page 45

IT_Type CAN_Get_IT_Type(void) INPUT -- OUTPUT Generic type of the event that caused the IT Tests matching of IDs of received messages and software filters. If the DEBUG option is defined, checks the IT Error flags first. Otherwise checks ...

Page 46

ST7 pCAN PERIPHERAL DRIVER 3.2.2.3 static CAN_Init_Error CAN_Init(Init_Data_Ptr data_ptr, CAN_Bool run_set, CAN_Bool wkup_set) INPUT Pointer to an Init_Data structure, Boolean, Boolean OUTPUT Error status CAN cell internal registers initialization. Not executed if RUN bit set. DESCRIPTION The first Boolean determines ...

Page 47

CAN_Transmit_Error CAN_In_Queue (CAN_Message* msg_to_queue) INPUT Pointer on a CAN message OUTPUT Error code Tests matching of IDs of received messages and software filters. DESCRIPTION Possible return values: CAN_FIFO_FULL, CAN_TRANSMIT_SUCCESS and CAN_TRANSMIT_FATAL. Must not be interrupted COMMENTS Called by ...

Page 48

ST7 pCAN PERIPHERAL DRIVER 3.2.2.5 static CAN_Message* CAN_Out_Queue (void) INPUT -- OUTPUT Pointer to a CAN message Returns a pointer to the first message in the transmission queue and updates the DESCRIPTION queue parameters Must not be interrupted COMMENTS Called ...

Page 49

CAN_Transmit_Error CAN_Fill_Transmission_Buffer() INPUT -- OUTPUT Error status Fills the hardware buffer for transmission. DESCRIPTION Possible return values: CAN_TRANSMIT_BUFFER_FULL, CAN_TRANSMIT_NO_MSG and CAN_TRANSMIT_SUCCESS ITs have to be disabled COMMENTS Called by CAN_Transmit_Request Calls CAN_Out_Queue Figure 24. CAN_Fill_Transmission_Buffer Flowchart Begin Selects ...

Page 50

ST7 pCAN PERIPHERAL DRIVER 3.2.2.8 static CAN_Receive_Error CAN_Recept_Buffer* dest_ptr) Number of the hardware buffer to release, pointer to the buffer where the data must INPUT be saved OUTPUT Error status Fetches data received in a hardware buffer. DESCRIPTION Possible return ...

Page 51

CAN_Receive_Error CAN_Receive(u8 num_buff_hard) INPUT ID of the buffer to void, pointer on the software buffer to fill. OUTPUT Error status Fetches data received in a hardware buffer and saves them in a software buffer sup- DESCRIPTION plied by ...

Page 52

ST7 pCAN PERIPHERAL DRIVER 3.2.2.10 Interrupt Routine INPUT -- OUTPUT -- DESCRIPTION Interrupt processing function. Calls CAN_Get_IT_Type CAN_Reception_Notification CAN_Transmission_Notification CAN_Fill_Transmission_Buffer CAN_Dominant_Bit_Reception_Notification May call (optional, see COMMENTS CAN_Transmission_Error_Notification CAN_Bus_Passive_Notification CAN_Bus_Active_Notification CAN_Bus_Off_Notification CAN_Overrun_Notification CAN_General_Reception_Error_Notification Figure 27. Interrupt Routine Flowchart Begin Case: ITRECEPT ...

Page 53

Case: ITTRANSMIT Case: ITSCIF if STATUS_CHANGE_NOTIFICATION defined or RUN_ON_STARTUP not defined status = STANDBY N if GENERAL_RECEPTION_ERROR defined CAN_General_Reception_Error _Notification() ST7 pCAN PERIPHERAL DRIVER Clears interrupt flag CAN_Transmission_Notification() CAN_Fill_Transmission_Buffer() CAN_Transmission_Error_Notification() BREAK Clears interrupt flag CAN_Get_Status() Y CAN_Dominant_Bit_Reception_Notification () if ...

Page 54

ST7 pCAN PERIPHERAL DRIVER continued if DEBUG defined Case: ITERR ORIF IT N TEIF IT N 54/100 Y CAN_Overrun_Notification() Clears interrupt flag Y CAN_Transmission_Error_Notification() Clears interrupt flag BREAK ...

Page 55

A FEW WORDS ABOUT DRIVER PERFORMANCE 3.3.1 CPU Load Let’s imagine the following situation: – Bus rate: 100 kilobauds – Bus load: 80% – MHz CPU – 14 identifiers (typical value) have to be received. Let’s ...

Page 56

ST7 pCAN PERIPHERAL DRIVER 3.3.2 Code Size The numbers given here correspond to the size of the driver code WITHOUT any addition of application specific code. 3.3.2.1 Can.o Module Table 2. Can.o Code Size Functions 1326 bytes Variables & Constants ...

Page 57

... DRIVER CODE 4.1 CAN.C /*********************** (c) 2000 STMicroelectronics ************************* PROJECT : COMPILER : ST7 COSMIC C v4.2e MODULE : can.c VERSION : V 1.1.6 build 80 CREATION DATE : 04/00 AUTHOR : Central Europe 8bit Micro Application Group -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- DESCRIPTION : CAN routines -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- MODIFICATIONS : RIM statement removed from interrupt routine (1.0.0) Transmission and reception structures merged into one "buffer type"(1.1.1) buffer_free)slightly modified(1 ...

Page 58

ST7 pCAN PERIPHERAL DRIVER //Compilation options coherence check #ifndef DEBUG #ifdef GENERAL_RECEPTION_ERROR #error "Debug not defined in CAN_custom.h" #endif #ifdef SIMULTANEOUS_EMISSION_RECEPTION #error "Debug not defined in CAN_custom.h" #endif #endif #ifdef GENERAL_RECEPTION_ERROR #ifdef STATUS_CHANGE_NOTIFICATION #error "GENERAL_RECEPTION_ERROR and STATUS_CHANGE_NOTIFICATION incompatibles in CAN_custom.h" ...

Page 59

REMOTE_BIT 5 #define REGISTER_SIZE 8 / *#########################################################################*/ /* STRUCTURES TYPEDEF / *#########################################################################*/ //Transmission queue element typedef struct FIFO_Object{ CAN_Buffer* message; // Pointer to a CAN message structure struct FIFO_Object* next_object; ...

Page 60

ST7 pCAN PERIPHERAL DRIVER / *#########################################################################*/ //Static variables //Transmission queue static FIFO CAN_transmit_queue; static FIFO_Object CAN_queue_object_1; static FIFO_Object CAN_queue_object_2; static FIFO_Object CAN_queue_object_3; volatile static u8 status_counter=0; tion to determine the state isters //It is used the following way : //+1 ...

Page 61

FUNCTIONS / *#########################################################################*/ //////////////////////////////////////////////////////////////////////////// //Notification functions///////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////////////// CAN_Buffer* CAN_Request_Buffer(u16); void CAN_Data_Reception_Notification(u16); void CAN_Remote_Reception_Notification(u16); void CAN_Transmission_Notification(void); void CAN_Dominant_Bit_Reception_Notification(void); #ifdef DEBUG void CAN_Overrun_Notification(void); void CAN_Transmission_Error_Notification(CAN_Transmit_Error); void CAN_Reception_Status(CAN_Receive_Error); #endif #ifdef GENERAL_RECEPTION_ERROR void CAN_General_Reception_Error_Notification(void); #endif #ifdef ...

Page 62

ST7 pCAN PERIPHERAL DRIVER void CAN_Bus_Off_Notification(void); #endif ////////////////////////// //can.c static functions// ////////////////////////// #ifdef FILTERS_ENABLED /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Filter INPUT/OUTPUT : identifier code/error status DESCRIPTION : Tests matching of IDs of received messages and software filters. COMMENTS : Called by ...

Page 63

FILTERS_ENABLED*/ /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Get_IT_Type INPUT/OUTPUT : -- / generic type of the ...

Page 64

ST7 pCAN PERIPHERAL DRIVER if ((SCIF_MASK & CANISR)!=0) { return ITSCIF; } #endif #endif if ((RECEPT_MASK & CANISR)!=0) then emission, then Status-Change/Wake-up condition// { return ITRECEPT; } else if ((TRANSMIT_MASK & CANISR)!=0) //If not, tests a transmit return ...

Page 65

CANBRPR=data_ptr->brpr_init;//Init of timing variables CANBTR=data_ptr->btr_init; //Reset CANISR register CANISR = 0x00; //Calculation of the CANICR value CANICR=0x30; #ifdef STATUS_CHANGE_NOTIFICATION CANICR=CANICR|0x08; //A bus wake up may not be wished, but a status change monitoring is wanted #endif #ifdef ...

Page 66

ST7 pCAN PERIPHERAL DRIVER SetBit(CANCSR,SRTE); #endif /*SIMULTANEOUS_EMISSION_RECEPTION*/ if (run_set) { CAN_RUN_Cell(); } return CAN_INIT_SUCCESS; } else { return CAN_INIT_FAILURE /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_In_Queue INPUT/OUTPUT : pointer on a CAN buffer output error code DESCRIPTION : Put a ...

Page 67

CAN_transmit_queue.last_object=(CAN_transmit_queue.last_object- >next_object); CAN_transmit_queue.last_object->message=msg_to_queue; CAN_transmit_queue.fifo_size=(CAN_transmit_queue.fifo_size)+1; CAN_transmit_queue.isinuse=CFALSE; return CAN_TRANSMIT_SUCCESS /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Out_Queue INPUT/OUTPUT : -- /pointer on a CAN buffer DESCRIPTION : Returns a pointer on the first message in ...

Page 68

ST7 pCAN PERIPHERAL DRIVER result=CAN_transmit_queue.first_object->message; CAN_transmit_queue.first_object->message=NULL; parameters CAN_transmit_queue.first_object=CAN_transmit_queue.first_object- >next_object; CAN_transmit_queue.fifo_size=(CAN_transmit_queue.fifo_size)-1; CAN_transmit_queue.isinuse=CFALSE; return result /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Clean INPUT/OUTPUT : -- /error status DESCRIPTION : Cleans the recept. and emission buffers, and resets the transmis- sion queue. COMMENTS ...

Page 69

CAN_transmit_queue.fifo_size=0; CAN_queue_object_1.message=NULL; CAN_queue_object_2.message=NULL; CAN_queue_object_3.message=NULL; CAN_transmit_queue.isinuse=CFALSE;//Releases the queue //Hardware cleaning (releases the hardware buffers) CANPSR=PAGE_3; while (CANPSR>0) { Clr_RDY_Bit(); CANPSR--; } //IT cleaning : clears any pending IT flag CANISR=CANISR&0; return CAN_CLEAN_SUCCESS; } else { return ...

Page 70

ST7 pCAN PERIPHERAL DRIVER if (ValBit(CANBCSR,RDY)) { return CAN_TRANSMIT_BUFFER_FULL; } else { CAN_Buffer* msg_to_send_ptr; u8* data_ptr; CAN_Data_Size data_length; msg_to_send_ptr=CAN_Out_Queue(); in the queue if (msg_to_send_ptr==NULL) { return CAN_TRANSMIT_NO_MSG; empty } CANIDLR=0x00; CANIDHR=0x00; data_length=msg_to_send_ptr->data_size; data_ptr=msg_to_send_ptr->CAN_msg_data; //!!!Code is here dependant of the order ...

Page 71

CANDR[counter]=data_ptr[counter counter=7; CANDR[7]=data_ptr[counter]; } msg_to_send_ptr->buffer_free=CTRUE; msg_to_send_ptr->buffer_rw=CFALSE; } return CAN_TRANSMIT_SUCCESS; } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Store_Rcvd_Msg INPUT/OUTPUT : ID of the buffer to void/Status message DESCRIPTION : Fetches the datas received in a hardware buffer. COMMENTS : ITs have ...

Page 72

ST7 pCAN PERIPHERAL DRIVER for (counter=0;counter<data_size;counter++) { data_ptr[counter]=CANDR[counter Clr_RDY_Bit(); //Releases the buffer and clears the IT flag return CAN_RECEIVE_SUCCESS; } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Receive INPUT/OUTPUT : ID of the buffer to void/Status message DESCRIPTION : Fetches the ...

Page 73

FILTER_SHIFT; message_ident &= 0xFFF0; //Filtering function #ifdef FILTERS_ENABLED filtering_status=CAN_Filter(message_ident); //Filtering fonction call if (filtering_status==CAN_FILTER_NO_MATCH) { Clr_RDY_Bit(); new reception return CAN_FILTERING_FAILURE; } #endif //Tests if remote frame if(ValBit(CANIDLR,4)) { Clr_RDY_Bit(); CAN_Remote_Reception_Notification(message_ident); return CAN_RECEIVE_REMOTE; } //Else, ...

Page 74

ST7 pCAN PERIPHERAL DRIVER Clr_RDY_Bit(); soft_buffer_ptr->buffer_rw=CFALSE;//If the buffer is full, releases it return CAN_RECEIVE_FATAL; } receive_status=CAN_Store_Rcvd_Msg(soft_buffer_ptr); //Saves message and re- leases the hardware buffer //Writing identifier : *(int*)soft_buffer_ptr = message_ident; //End of routine : if (receive_status!=CAN_RECEIVE_SUCCESS) { Clr_RDY_Bit(); soft_buffer_ptr->buffer_rw=CFALSE;//In case ...

Page 75

CAN_Interrupt(void) { IT_Type it_type; it_type=CAN_Get_IT_Type(); switch(it_type) { case ITRECEPT : { CAN_Receive_Error error_status; if (ValBit(CANISR,RXIF3)) //Check the lower priority buffer first status_counter=status_counter+1; CAN_Reception_Status(error_status); #endif status_counter=status_counter-1; if(ValBit(CANISR,RXIF2)) //Then checks the other status_counter=status_counter+1; #ifdef DEBUG CAN_Reception_Status(error_status); #endif status_counter=status_counter-1; ...

Page 76

ST7 pCAN PERIPHERAL DRIVER CAN_Transmission_Notification(); status=CAN_IT_Fill_Transmission_Buffer(); #ifdef DEBUG if (status!=CAN_TRANSMIT_SUCCESS) When an unsuccesfull transmission occured (argument : error status?) #endif break; } case ITSCIF : { CAN_Status status; Clr_SCIF_Bit(); status=CAN_Get_Status(); if (status==CAN_STANDBY) ception of a dominant bit while in stanby ...

Page 77

GENERAL_RECEPTION_ERROR { CAN_General_Reception_Error_Notification(); Clr_SCIF_Bit(); } #endif break; } #ifdef DEBUG case ITERR : { if (ValBit(CANISR,ORIF)) { CAN_Overrun_Notification(); Clr_ORIF_Bit(); } else if (ValBit(CANISR,TEIF)) { CAN_Transmission_Error_Notification(CAN_TRANSMISSION_ERROR_IT); Clr_TEIF_Bit(); } break; } #endif default : ////////////////////////////////////////// //Default case (should never ...

Page 78

ST7 pCAN PERIPHERAL DRIVER **********************************************************/ /* List of all functions defined in this module and used in other modules /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Get_TEC() INPUT/OUTPUT : -/Value of the TECR register (transmission error counter) DESCRIPTION : Returns the value of ...

Page 79

CAN_STANDBY; } else { if (ValBit(CANCSR,EPSV)) { return CAN_BUS_PASSIVE; } else { } } } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_First_Init INPUT/OUTPUT : --/ error status DESCRIPTION : Can Cell power on initialisation Routine. COMMENTS : ...

Page 80

ST7 pCAN PERIPHERAL DRIVER CAN_queue_object_1.next_object=&CAN_queue_object_2; transmission objects are being linked CAN_queue_object_1.preceding_object=&CAN_queue_object_3; CAN_queue_object_1.message=NULL; CAN_queue_object_2.next_object=&CAN_queue_object_3; CAN_queue_object_2.preceding_object=&CAN_queue_object_1; CAN_queue_object_2.message=NULL; CAN_queue_object_3.next_object=&CAN_queue_object_1; CAN_queue_object_3.preceding_object=&CAN_queue_object_2; CAN_queue_object_3.message=NULL; CAN_transmit_queue.fifo_size=0; CAN_transmit_queue.first_object=&CAN_queue_object_1; CAN_transmit_queue.last_object=&CAN_queue_object_3; ized that way, or the first input will fail CAN_transmit_queue.isinuse=CFALSE; CANPSR=PAGE_1; Clr_LOCK_Bit(); SetBit(CANBCSR,LOCK); reception //Init of the hardware buffers ...

Page 81

CAN_INIT_FAILURE; } return CAN_INIT_SUCCESS; } else return CAN_INIT_FAILURE; } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_It_Dis INPUT/OUTPUT : --/-- DESCRIPTION : Reset of the CANICR register. COMMENTS : --------------------------------------------------------------------------*/ void CAN_It_Dis (void) { CANICR=0x00; } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Switch_Off ...

Page 82

ST7 pCAN PERIPHERAL DRIVER { CAN_Clean_Error clean_status; CAN_It_Dis(); ClrBit(CANCSR,RUN); while (ValBit(CANCSR,RUN)) { static u16 counter=0xFFF; ~21,4ms with 8MHz fcpu) counter=counter-1; if (counter==0) { return CAN_SLEEP_FATAL clean_status=CAN_Clean(); if (clean_status==CAN_CLEAN_FAILURE) { return CAN_SLEEP_FATAL (wucause==BUS_WAKEUP) waken up by the ...

Page 83

DESCRIPTION : Puts the CAN node into passive state. Returns an error if any transmission request is pendingor if a hardware reception buffer is still unsaved. Dedicated to bus wake-up (for power save). COMMENTS : Doesn’t call CAN_Clean. Possible return ...

Page 84

ST7 pCAN PERIPHERAL DRIVER { Clr_SCIF_Bit(); reenabling ITs SetBit(CANICR,SCIE); reenabling ITs*/ } else if(wucause==SOFT_WAKEUP) { Clr_SCIF_Bit(); reenabling ITs } return CAN_SLEEP_SUCCESS; } return CAN_SLEEP_ERROR; } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Switch_on INPUT/OUTPUT : pointer on an init data, order : emission ...

Page 85

CAN_SWITCH_ON_FAILURE return CAN_SWITCH_ON_SUCCES; } else return CAN_SWITCH_ON_FAILURE; } /*-------------------------------------------------------------------------- ROUTINE NAME : CAN_Transmit_request INPUT/OUTPUT : pointer on the CAN buffer to send/error status DESCRIPTION : Puts ...

Page 86

... CAN_TRANSMIT_FATAL; } else { return status; is already pending) } executed by CAN_Store_Received_Message } /****** (c) 2000 STMicroelectronics ********************* END OF FILE _******/ 4.2 CAN.H /*********************** (c) 2000 STMicroelectronics ************************* PROJECT : COMPILER : ST7 COSMIC C v4.2e MODULE : can.h VERSION : V 1.1.6 build 80 CREATION DATE : 04/00 86/100 //Puts the message in the queue //Tries to fill the buffer 2 //This should never happen //Can be success or buffer in use (ie ...

Page 87

AUTHOR : Central Europe 8bit Micro Application Group -*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- DESCRIPTION : CAN routines -*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- MODIFICATIONS : none *************************************************************************** THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR ...

Page 88

ST7 pCAN PERIPHERAL DRIVER //Error status messages //Initialisation typedef enum {CAN_INIT_SUCCESS,CAN_INIT_FAILURE}CAN_Init_Error; //Sleep typedef enum {CAN_SLEEP_FATAL,CAN_SLEEP_ERROR,CAN_SLEEP_SUCCESS,CAN_SWITCH_ON_SUCCES,CAN_ SWITCH_ON_FAILURE}CAN_Switch_Error; //Status typedef enum {CAN_RUN,CAN_STANDBY,CAN_BUS_ACTIVE,CAN_BUS_PASSIVE,CAN_BUS_OFF}CAN_Status; //Misc. typedef enum {CFALSE=0,CTRUE=1}CAN_Bool; //Transmission-Reception typedef enum {CAN_TRANSMIT_FATAL,CAN_TRANSMIT_SUCCESS,CAN_TRANSMIT_FAILURE,CAN_TRANSMIT_N O_MSG,CAN_FIFO_FULL,CAN_TRANSMIT_BUFFER_FULL,CAN_TRANSMISSION_ERROR_IT}CAN_T ransmit_Error; typedef enum {CAN_RECEIVE_FATAL,CAN_RECEIVE_SUCCESS,CAN_RECEIVE_REMOTE,CAN_ILLEGAL_IDENTI FIER,CAN_FILTERING_FAILURE,CAN_BUFFER_IN_USE,CAN_NO_BUFFER,CAN_RCV_BUFFER_NO T_READY}CAN_Receive_Error; ////////////////////////////////////// //Transmission/Reception structures // ////////////////////////////////////// ...

Page 89

Init_Data* Init_Data_Ptr; // Pointer on an Init_Data structure / *#########################################################################*/ /* VARIABLES / *#########################################################################*/ //Initialization of the CAN-cell //Variables declared in can.c extern const ...

Page 90

... Clr_SCIF_Bit() (CANISR=0xF7) #define Clr_ORIF_Bit() (CANISR=0xFB) #define Clr_TEIF_Bit() (CANISR=0xFD) #define Clr_EPND_Bit() (CANISR=0xFE) //To clear #define Clr_LOCK_Bit() (CANBCSR=0xFE) #define Clr_RDY_Bit() (CANBCSR=0xFA) #define Size_Of_Words_Array(array) sizeof(array)/sizeof(unsigned int) /*----------------------CAN SETTINGS----------------------*/ #define CAN_RUN_Cell() (CANCSR |= 0x05) #endif /****** (c) 2000 STMicroelectronics ********************* END OF FILE _******/ 4.3 CAN_CUSTOM.C /********************* (c) 1999 STMicroelectronics**************************** /********************* (c) 1999 STMicroelectronics *************************** 90/100 */ ...

Page 91

PROJECT : COMPILER : ST7 COSMIC C v4.2e MODULE : can_custom.c VERSION : 1.1.6 build 44 CREATION DATE : 04/99 AUTHOR : Central Europe 8bit Micro Application Group -*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- DESCRIPTION : Customisation functions for CAN driver -*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- MODIFICATIONS : **************************************************************************** ...

Page 92

ST7 pCAN PERIPHERAL DRIVER ////////////////////////////////////////////////////////// //Optional functions//////////////////////////////////// ////////////////////////////////////////////////////////// //All those functions are meant to be called by the interrupt routine //so don’t auhorize ITs in this file, otherwise it could lead to undefined behaviour //of the application CAN_Buffer* CAN_Request_Buffer(u16 ident_of_message) ...

Page 93

... CAN_Overrun_Notification(void) { //To implement if defined } void CAN_Transmission_Error_Notification(CAN_Transmit_Error status) { //To implement if defined } #endif /******** (c) 1999 STMicroelectronics ****************** END OF FILE ********/ 4.4 CAN_CUSTOM.H /*********************** (c) 1999 STMicroelectronics ************************* PROJECT: COMPILER: ST7 COSMIC C v.4.2e MODULE: can_custom.h VERSION: V 1.1.6 build 44 CREATION DATE: 04/00 AUTHOR: Central Europe 8bit Micro Application Group ST7 pCAN PERIPHERAL DRIVER 93/100 ...

Page 94

ST7 pCAN PERIPHERAL DRIVER -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*- DESCRIPTION: CAN customization options -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*- MODIFICATIONS: none **************************************************************************** THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ...

Page 95

... SIMULTANEOUS_EMISSION_RECEPTION /*Only if debug also defined*/ //Allows simultaneous reception and reception of a message //to check the integrity of the message path / *#########################################################################*/ /* VARIABLES / *#########################################################################*/ /*----------------------CAN SETTINGS----------------------*/ #endif /****** (c) 1999 STMicroelectronics ********************* END OF FILE _******/ ST7 pCAN PERIPHERAL DRIVER /*max identifier : 2048, that’s to say 0x0800, max */ 95/100 ...

Page 96

... ST7 pCAN PERIPHERAL DRIVER 4.5 CAN_HR.H /*********************** (c) 2000 STMicroelectronics ************************* PROJECT : COMPILER : ST7 COSMIC C v.4.2e MODULE : can.c VERSION : V 1.1.6 build 44 CREATION DATE : 04/00 AUTHOR : Central Europe 8bit Micro Application Group -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- DESCRIPTION : CAN hardware registers -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*- MODIFICATIONS : **************************************************************************** THE SOFTWARE INCLUDED IN THIS FILE IS FOR GUIDANCE ONLY. ST MICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM USE OF THIS SOFTWARE ...

Page 97

CANRes00 @0x62; /* Reserved @tiny volatile unsigned char CANRes01 @0x63; /* Reserved @tiny volatile unsigned char CANRes02 @0x64; /* Reserved @tiny volatile unsigned char CANRes03 @0x65; /* Reserved @tiny volatile unsigned char CANRes04 @0x66; /* Reserved ...

Page 98

ST7 pCAN PERIPHERAL DRIVER #define TXIF 4 #define SCIF 3 #define ORIF 2 #define TEIF 1 #define EPND 0 #define ESCI 6 #define RXIE 5 #define TXIE 4 #define SCIE 3 #define ORIE 2 #define TEIE 1 #define ETX 0 ...

Page 99

... RTR 4 #define MSK_DLC 0x0F #define ACC 3 #define RDY 2 #define BUSY 1 #define LOCK 0 /*------------------------------------------------------------------------*/ #endif /****** (c) 2000 STMicroelectronics ********************* END OF FILE _******/ ST7 pCAN PERIPHERAL DRIVER /* Identifier Low Register /* Buffer Control Status Register */ /* Buffer Control Status Register */ */ 99/100 ...

Page 100

... No license is granted by implication or otherwise under any patent or patent rights of STMicroelectronics. Specifications mentioned in this publication are subject to change without notice. This publication supersedes and replaces all information previously supplied. STMicroelectronics products are not authorized for use as critical components in life support devices or systems without the express written approval of STMicroelectronics ...

Related keywords