course introduction purpose this training course provides an overview of the can peripheral in...
TRANSCRIPT
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
MCU data bus
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 slotstatus register
CAN interruptcontrol register
CAN receive errorcount register
CAN transmit errorcount register
CTx
CRx
CAN receive complete
CAN transmit complete
CAN error
CAN wakeup
CAN extended ID register
Interruptcontrol logic
To/From External CAN Transceiver
Protocol controller
Wakeuplogic
M16C/R8C MCU CAN Peripheral
Message Boxes
006016
006116
006216
006316
006416
006516
006616
006716
006816
006916
006A16
006B16
006C16
006D16
006E16
006F16
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
.
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:
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
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 PeripheralCAN 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 interruptpriority level
Clear interruptenable flag
Set interruptenable flag
Enable CAN interrupts
If using CAN interrupts
Set Baud Rate and Bit Timing
END
Set CAN clockdivide-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 ( )
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
Sets up a slot to transmit:1. Check if transmit possible [ “TrmActive” = 0?]2. Clear message control register3. Prohibit interrupts for slot (if using TX interrupts)4. Select standard or extended ID5. Set TX-ID6. 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
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
Transmitting the highest-priority message
Message transmission Error frame Message retransmission ReceptionReceiving
Error Arbitration 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
Transmit Abort
Abort transmissionprocedure
START
Transmitrequested?
Request abort transmission
END
NO
YES
Beingtransmitted?
NO
YES
Send priority message
Ab
ort
tra
nsm
issi
on
re
qu
est
s u
nn
ece
ssa
ry
abort_trm_can ( in_slot )
• Transmit incomplete? “SentData” = 0?
• Transmit abort request “TrmReq” 0
• When transmit abort complete, transmit highest-priority message
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)
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
Example 2: Receiving standard data frames/remote frames with IDs 122 and 123 in CAN slot 0
Slot 0 sees on busMask register
Received message
C0GMRID123
1 1 1 1 1 0 0 1 0 0
0 0 1 0 0 1 1 1 1 1 01 0 0 0 1 X
1 0 0 0 1 1
SID10-6 SID5-0
ID122 0 0 1 0 0 1 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 ‘don’t-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 busMask registerReceived message
C0GMRID123
1 1 1 1 1 0 0 1 0 0
0 0 1 0 0 1 1 1 1 1 11 0 0 0 1 1
1 0 0 0 1 1
SID10-6 SID5-0
set_mask_can ( )
Acceptance Filter Support Unit
The ASU function:• Determines if received Standard IDs are valid• Provides faster response than software filtering
Acceptance filtersupport registers Received ID (11 bits)
Upper 8 bits of received ID
Converted value of lower3 bits of received ID
Register configuration during read operation
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
b0b15 b8 b7
(ASU registers: C0AFS, C1AFS)
Receive Complete using Polling
Confirming by Polling
START
Finished receivingsuccessfully?
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
• 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 ( )
• 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 )
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_c0recicB C
Done ResetShow
Solution
ATells main application that message has been sent successfullyD
C ASU
Determines if received Standard IDs are valid
BExecuted when an interrupt verifies successful data frame reception
D fnc_c0trmic A Sets up a slot to transmit
Question
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 initializationare programmable
Timestamp Function
This function:• Automatically writes
timestamp during normal receive operation
• Useful for determining sequence in which a group of messages was received
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 level2. 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 interruptcontrol register (c0recic)
“NewData” in CAN message control register
CAN status registerReceived message processing
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
CAN Status
Register
b15 b14 b13 b12 b11 b10 b2 b1 b0
CAN Status Register
Error bus off state flag
Error passive state flag
ErrorActiveState-Normal
Bus error state flagSet if any
Error frame
ErrorPassive
State
BusOff
State- too many errorsstop transmitting
Error!
Error!
Error!
Data Frames
ErrorPassive
State- more than 128 erros
Return From Bus Off State
BusOff
State
ErrorActiveState
Slot’s 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 doesn’t keep coming back.
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 transmitand receive slot C
AN
BusOL
O
PB
AC
K
Listen-Only Mode
• Normal receive completed: ACK output• Communication error: Error frame output
No ACK or error frame transmit operations are performed
Transmit unit
GettingResponses
Receive unit(Listen-Only
mode)
Receive unit
Receive unit
Receive unitNormal mode
TransmittingMessage
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 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
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
• CAN peripheral in R8C and M16C microcontrollers
• Initializing the CAN peripheral
• CAN API functions
Course Summary