1 / 29

Course Introduction

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.

daire
Télécharger la présentation

Course Introduction

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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. M16C/R8C MCU CAN Peripheral MCU data bus CAN global mask register CAN bus timing control register CAN control register CAN extended ID register CAN local mask register A Message control registers 0 to 15 CAN local mask register B Message boxes (Slots 0 to 15) CTx Protocol controller To/From External CAN Transceiver Acceptance filter slots 0 to 15 • Message ID • DLC • Data field• Timestamp 16-bit timer CRx Timestamp register(not in R8C/23) Wakeuplogic Interruptcontrol logic CAN receive errorcount register CAN receive complete CAN transmit errorcount register CAN status register CAN slot status register CAN interruptcontrol register CAN transmit complete CAN error CAN wakeup MCU data bus

  3. Message Boxes 006016 006116 Message ID (Standard, Extended) 006216 006316 006416 DLC (6 Bytes) 006516 006616 006716 006816 006916 Data Field (8 Bytes) 006A16 006B16 006C16 006D16 006E16 Timestamp Value (2 Bytes) 006F16 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 Control Logic 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 An interrupt service function must be declared with ‘pragma’ somewhere in the program. Examples: #pragma INTERRUPT /B fnc_c0recic(); #pragma INTERRUPT /B fnc_c0trmic();

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

  6. Initializing CAN Peripheral CAN Configuration Procedure START Enter CAN reset/ initialization mode Exit CAN sleep mode Enable CAN interrupts NO CAN reset mode? START YES Enable CAN ports Clear interruptenable flag 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 interruptpriority level Set CAN bit timing and baud rate Set interruptenable flag Set mask register END Go to CAN operation mode NO CAN operation mode? YES END If using CAN interrupts 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

  7. Set Baud Rate and Bit Timing Setting CAN bit timing and baud rate START Clear protect register Set CAN clockdivide-by-N value Set protect register Set: • Prescaler divide-by-N value • Sampling count • PTS, PBS1, PBS2, SJW END set_bitrate_can ( ) 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

  8. Question 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

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

  10. Verify Successful Transmission 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 Keep interrupt code short to avoid blocking other interrupts. fnc_c0trmic ( ) 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.

  11. Transmit Abort Applying a time limit to message transmission Error Arbitration loss Message transmission Error frame Message retransmission Reception Receiving Transmit abort request Time limit Transmit request Transmitting the highest-priority message Arbitration loss Message (1) transmission Reception Message (2) transmission Transmit abort requestTransmission request for now highest priority message (2)

  12. Transmit Abort Abort transmissionprocedure START NO Transmitrequested? YES Abort transmission requests unnecessary Request abort transmission YES Beingtransmitted? NO Send priority message END 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 Which of the following functions is used to set up a slot to receive? Select the response that applies and click Done. • set_rec_std_dataframe_can • abort_trm_can • set_trm_std_dataframe_can • set_bitrate_can Done

  15. Mask Setting Example 1: Receiving standard data frame/ remote frame with ID 123 in CAN slot 0 SID10-6 SID5-0 Slot 0 sees on bus 0 0 1 0 0 1 0 0 0 1 1 Mask register C0GMR 1 1 1 1 1 1 1 1 1 1 1 Received message ID123 0 0 1 0 0 1 0 0 0 1 1 Example 2: Receiving standard data frames/remote frames with IDs 122 and 123 in CAN slot 0 SID10-6 SID5-0 Slot 0 sees on bus 0 0 1 0 0 1 0 0 0 1 X Mask register C0GMR 1 1 1 1 1 1 1 1 1 1 0 ID123 0 0 1 0 0 1 0 0 0 1 1 Received message ID122 0 0 1 0 0 1 0 0 0 1 0 set_mask_can ( ) 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 ‘don’t-care’ bits of ID to zero in MASK

  16. Acceptance Filter Support Unit Upper 8 bitsof received ID Acceptance filter support registers Received ID (11 bits) Converted value of lower3 bits of received ID Register configuration during read operation b15 b8 b7 b0 CSID7 CSID6 CSID5 CSID4 CSID3 CSID2 CSID1 CSID0 SID10 SID9 SID8 SID7 SID6 SID5 SID4 SID3 Converted value of lower 3 bits of received ID Upper 8 bits of received ID The ASU function: • Determines if received Standard IDs are valid • Provides faster response than software filtering (ASU registers: C0AFS, C1AFS)

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

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

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

  20. Question 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. Tells main application that message has been sent successfully D A set_trm_std_dataframe_can Determines if received Standard IDs are valid C B fnc_c0recic Executed when an interrupt verifies successful data frame reception C B ASU D A fnc_c0trmic Sets up a slot to transmit ShowSolution Reset Done

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

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

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

  24. CAN Status Register Error! Error! Error! Error Active State-Normal Error passive state flag Error bus off state flag Bus error state flagSet if any Error frame Data Frames b15 b14 b13 b12 b11 b10 b2 b1 b0 CAN Status Register ErrorPassive State- more than 128 erros BusOff State- too many errorsstop transmitting Error Passive State

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

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

  27. Listen-Only Mode No ACK or error frame transmit operations are performed Receive unit(Listen-Only mode) Receive unit(Listen-Only mode) Receive unit Transmit unit Receive unit Transmit unit Normal mode Transmitting Message Getting Responses Receive unit Receive unit • Normal receive completed: ACK output • Communication error: Error frame output For more CAN API information: • CAN Application Note — Data on API for R8C and M16C MCUs; download fromhttp://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. Course Summary • CAN peripheral in R8C and M16C microcontrollers • Initializing the CAN peripheral • CAN API functions

More Related