Presentation is loading. Please wait.

Presentation is loading. Please wait.

Course Introduction Purpose This training course provides an overview of the CAN peripheral in Renesas M16C and R8C microcontrollers that is used to build.

Similar presentations


Presentation on theme: "Course Introduction Purpose This training course provides an overview of the CAN peripheral in Renesas M16C and R8C microcontrollers that is used to build."— Presentation transcript:

1 Course Introduction Purpose This training course provides an overview of the CAN peripheral in Renesas M16C and R8C microcontrollers that is used to build networked, multiprocessor embedded systems, and the Application Programming Interface (API) used to program that on-chip CAN peripheral. Objectives Get an introduction to the Renesas M16C and R8C CAN peripheral hardware. Understand how to program the CAN peripheral using the Renesas API function calls. Content 29 pages 4 questions Learning Time 45 minutes

2 MCU data bus CAN bus timing control register CAN control register Message control registers 0 to 15 CAN global mask register CAN local mask register A CAN local mask register B Acceptance filter slots 0 to 15 16-bit timer Timestamp register (not in R8C/23) Message boxes (Slots 0 to 15) Message ID DLC Data field Timestamp CAN status register CAN slot status register CAN interrupt control register CAN receive error count register CAN transmit error count register CTx CRx CAN receive complete CAN transmit complete CAN error CAN wakeup CAN extended ID register Interrupt control logic To/From External CAN Transceiver Protocol controller Wakeup logic M16C/R8C MCU CAN Peripheral

3 Message Boxes 0060 16 0061 16 0062 16 0063 16 0064 16 0065 16 0066 16 0067 16 0068 16 0069 16 006A 16 006B 16 006C 16 006D 16 006E 16 006F 16 Message ID (Standard, Extended) DLC (6 Bytes) Data Field (8 Bytes) Timestamp Value (2 Bytes) Communication performed via CAN message boxes: Message boxes –Transmit and receive buffers for CAN messages –Comprise 16 slots of 16 bytes (or 8 words) each –Slots 14 and 15 can be used for BasicCAN slots Individual slots can be set as transmit or receive slots by a program During both transmit and receive operation, slots with the lowest numbers have highest priority; i.e., priority increases as slot number decreases Each slot retains its previous contents until a new message is sent or received.

4 Interrupt sources: CAN receive complete CAN transmit complete CAN error (error passive state, bus off state, bus error) CAN wakeup Related registers: Interrupt control registers (C0RECIC, C0TRMIC, C01ERRIC, [C01WKIC]) –Set priority levels for interrupt sources listed above CAN interrupt control register (C0IRC) –Enables and disables interrupts for individual message box slots Interrupt Control Logic #pragma INTERRUPT /B fnc_c0recic(); #pragma INTERRUPT /B fnc_c0trmic(); An interrupt service function must be declared with pragma somewhere in the program. Examples:

5 Renesas M16C CAN peripheral API set_mask_can ( ) config_can ( ) set_rec_std_dataframe_can ( slot_nr, id ) check_rec_success_can ( slot_nr, dataframe* ) – if polling get_message_can ( slot_nr, dataframe* ) set_bitrate_can ( ) abort_trm_can ( slot_nr ) check_err_can ( ) fnc_c0recic ( ) – if using interrupts can_initial ( ) fnc_c0trmic ( ) – if using interrupts set_trm_std_dataframe_can ( slot_nr, dataframe* ) check_trm_std_dataframe_can ( slot_nr) – if polling check_hw_tmr_can ( canblock1_tmo_diag* ) Error functions Initialization functions Transmit functions Receive functions

6 config_can ( ) Transition to CAN reset/ initialization mode Enable CAN ports Set operating mode Set bit timing and baud rate Set mask registers Transition to CAN operating mode Initializing CAN Peripheral CAN Configuration Procedure START Enter CAN reset/ initialization mode Exit CAN sleep mode CAN reset mode? NO YES Enable CAN ports Set CAN control register - Loopback mode select bit - Message order select bit - Basic CAN mode select bit - Bus error interrupt enable bit - Timestamp prescaler Set CAN bit timing and baud rate Set mask register Go to CAN operation mode CAN operation mode? NO END YES START END Set interrupt priority level Clear interrupt enable flag Set interrupt enable flag Enable CAN interrupts If using CAN interrupts

7 Set Baud Rate and Bit Timing END Set CAN clock divide-by-N value Clear protect register Set: Prescaler divide-by-N value Sampling count PTS, PBS1, PBS2, SJW Setting CAN bit timing and baud rate START Set protect register Cancel register protect Set divide ratio for CAN clock Set register protect Set dividing ratio for prescaler Set sampling count Set sampling points PTS, PBS1, PBS2, and SJW set_bitrate_can ( )

8 Is the following statement true or false? Click Done when you are finished. The R8C/M16C CAN peripheral provides 16 message boxes, each of which has transmit and receive buffers. The message buffers comprise 16 slots of 16 bytes (or 8 words). True False Done Question

9 Sets up a slot to transmit: 1.Check if transmit possible [ TrmActive = 0?] 2.Clear message control register 3.Prohibit interrupts for slot (if using TX interrupts) 4.Select standard or extended ID 5.Set TX-ID 6.Byte count of transmit data (set DLC) 7.Set transmit data 8.Interrupt enable the slot (if using TX interrupts) 9.Transmit data frame Data Frame Transmit Mode set_trm_std_dataframe_can ( slot_nr, dataframe* ) If multiple slots are set to data frame transmit mode simultaneously, they transmit in order, starting with slot with lowest number

10 Keep interrupt code short to avoid blocking other interrupts. Confirming Successful Transmission using an Interrupt CAN successful transmission interrupt routine START Check that slot finished sending Notify application etc. CAN successful transmission interrupt routine END Performs successful data frame transmit interrupt routine: Sets a flag so main program knows what data was successfully sent out Notify application Flash an LED, etc. fnc_c0trmic ( ) Verify Successful Transmission

11 Transmitting the highest-priority message Message transmissionError frameMessage retransmissionReception Receiving ErrorArbitration loss Transmit abort request Transmit request Time limit Applying a time limit to message transmission Message (1) transmission Reception Message (2) transmission Arbitration loss Transmit abort request Transmission request for now highest priority message (2) Transmit Abort

12 Abort transmission procedure START Transmit requested? Request abort transmission END NO YES Being transmitted? NO YES Send priority message Abort transmission requests unnecessary abort_trm_can ( in_slot ) Transmit incomplete? SentData = 0? Transmit abort request TrmReq 0 When transmit abort complete, transmit highest-priority message

13 Data Frame Receive set_rec_std_dataframe_can ( slot_nr, id ) Functions performed: Set up slot for receive Prohibit interrupts for slot (if using RX interrupts) Set Standard or Extended ID Set RX-ID Clear message control register Set data frame receive mode Set RecReq to 1 to start receiving Enable interrupts for the receive slot (if using RX interrupts)

14 Question a.set_rec_std_dataframe_can b.abort_trm_can c.set_trm_std_dataframe_can d.set_bitrate_can Which of the following functions is used to set up a slot to receive? Select the response that applies and click Done. Done

15 Example 2: Receiving standard data frames/remote frames with IDs 122 and 123 in CAN slot 0 Slot 0 sees on bus Mask register Received message C0GMR ID123 1 1 1 1 1 0 0 1 0 0 1 1 1 1 1 0 1 0 0 0 1 X 1 0 0 0 1 1 SID 10-6 SID 5-0 ID122 0 0 1 0 01 0 0 0 1 0 This function does the following: Accept one ID for message box => sets MASK to all ones Accept all messages => sets MASK to all zeros Accept range of messages => sets the dont-care bits of ID to zero in MASK Mask Setting Example 1: Receiving standard data frame/ remote frame with ID 123 in CAN slot 0 Slot 0 sees on bus Mask register Received message C0GMR ID123 1 1 1 1 1 0 0 1 0 0 1 1 1 1 0 0 0 1 1 SID 10-6 SID 5-0 set_mask_can ( )

16 Acceptance Filter Support Unit The ASU function: Determines if received Standard IDs are valid Provides faster response than software filtering Acceptance filter support registers Received ID (11 bits) Upper 8 bits of received ID Converted value of lower 3 bits of received ID Register configuration during read operation CSID 7 CSID 6 CSID 5 CSID 4 CSID 3 CSID 2 CSID 1 CSID 0 SID 10 SID 9 SID 8 SID 7 SID 6 SID 5 SID 4 SID 3 Converted value of lower 3 bits of received ID Upper 8 bits of received ID b0b15b8b7 (ASU registers: C0AFS, C1AFS)

17 Receive Complete using Polling Confirming by Polling START Finished receiving successfully? Process received message END NO YES Data received? If yes: process received message Note: Check for overruns! CAN slot status register or NewData in CAN message control register

18 Copy data to buffer Notify main program that data was received Receive Complete using Interrupt CAN successful reception interrupt routine START END Processing for received message Check that the slot finished receiving successfully Confirming successful reception with a CAN successful reception interrupt fnc_c0recic ( )

19 Checks whether or not received data to slot is complete Looks at receive complete flag (NewData) Reads received message Checks for more received data before leaving (to reduce nr of interrupts) Dataframe = pointer to a dataframe structure Received Message Processing check_rec_success_can ( slot_nr, dataframe ) get_message_can ( slot_nr, dataframe )

20 Match each CAN API function to the most appropriate explanation by dragging the letters on the left to the correct locations on the right. Click Done when you are finished. set_trm_std_dataframe_can fnc_c0recic B C Done Reset Show Solution A Tells main application that message has been sent successfully D C ASU Determines if received Standard IDs are valid B Executed when an interrupt verifies successful data frame reception D fnc_c0trmic A Sets up a slot to transmit Question

21 Prescaler (Divide by 1, 2, 4, or 8) 16-bit free-running counter Received ID Received data Timestamp Receive slot Standard cycle: 1-bit-time cycle Reset by software Cycle and initialization are programmable Timestamp Function This function: Automatically writes timestamp during normal receive operation Useful for determining sequence in which a group of messages was received

22 Polled message boxes - check which slots have new data Receive interrupts - check Active Slot Bits for which slot has received last (If the CAN receive interrupts are used) 1.Set interrupt priority level 2.Clear interrupt request bit 3.Set slot ICR enable flag 4.Set receive enable bit (I flag) Flag registers At system setup time: Running: What Slots Have New Data? CAN receive interrupt control register (c0recic) NewData in CAN message control register CAN status register Received message processing

23 CAN error generated? CAN bus error handling: CAN error passive state? –Error passive handling CAN bus off state? –Bus off handling Error Confirmation (Interrupt Processing) CAN bus off state? CAN return from bus off Error Confirmation (Polling Processing) Error Handling check_err_can ( ) CAN status register

24 CAN Status Register b15b14b13b12b11b10b2b1b0 CAN Status Register Error bus off state flag Error passive state flag Error Active State -Normal Bus error state flag Set if any Error frame Error Passive State Bus Off State - too many errors stop transmitting Error! Data Frames Error Passive State - more than 128 erros

25 Return From Bus Off State Bus Off State Error Active State Slots state changes when - Slot receives 11 consecutive recessive bits 128 times - Return from bus off function is executed CAN control register Sets return from bus off bit to 1 1 b11 Return from bus off function CAUTION - A node is supposed to go bus off if it is faulty. Use this function with care so a faulty node doesnt keep coming back.

26 Loopback Function This function does the following: Allows confirmation of transmitted content –Useful as self-diagnostic during debugging, etc. Transmit slot Receive slot Message transmission Transmit Unit Same ID set for transmit and receive slot CAN Bus O L O P B A C K

27 Listen-Only Mode Normal receive completed: ACK output Communication error: Error frame output No ACK or error frame transmit operations are performed Transmit unit Getting Responses Receive unit (Listen-Only mode) Receive unit Normal mode Transmitting Message Transmit unit Receive unit Receive unit (Listen-Only mode) For more CAN API information: CAN Application Note Data on API for R8C and M16C MCUs; download from http://documentation.renesas.com/eng/products/mpumcu/apn/rej05b0276_16ccanap.pdf CAN Specification Version 2.0, 1991, Robert Bosch GmbH M16C/6NK, M16C/1N, M16C/29, R8C/23, etc. hardware manuals

28 Question Which of these statements correctly describe error handling, slot status and other issues? Select all that apply and then click Done. The polling approach to error confirmation uses the bus error state flag, error active status flag, and bus off status flag in the CAN status register. A slot returns from the bus off state when it receives 11 consecutive recessive bits 128 times or when a Return from bus off function executes. The loopback function is useful as self-diagnostic during debugging because it can confirm transmitted content. A receive unit in listen-only mode doesn't transmit ACKs or error frames. Done

29 CAN peripheral in R8C and M16C microcontrollers Initializing the CAN peripheral CAN API functions Course Summary


Download ppt "Course Introduction Purpose This training course provides an overview of the CAN peripheral in Renesas M16C and R8C microcontrollers that is used to build."

Similar presentations


Ads by Google