receive - Renesas

advertisement

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

M16C/R8C MCU CAN Peripheral

CAN bus timing control register

MCU data bus

CAN control register

Message control registers 0 to 15

CAN global mask register

CAN local mask register A

CAN local mask register B

CTx

To/From

External CAN

Transceiver

Protocol controller

CRx

Wakeup logic

Acceptance filter slots 0 to 15

16-bit timer

Timestamp register

(not in R8C/23)

CAN receive error count register

CAN transmit error count register

CAN status register

CAN slot status register

MCU data bus

CAN interrupt control register

CAN extended

ID register

Message boxes

(Slots 0 to 15)

• Message ID

• DLC

• Data field

• Timestamp

Interrupt control logic

CAN receive complete

CAN transmit complete

CAN error

CAN wakeup

Message Boxes

.

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

0068

16

0069

16

006A

16

006B

16

006C

16

006D

16

006E

16

006F

16

0060

16

0061

16

0062

16

0063

16

0064

16

0065

16

0066

16

0067

16

Message ID

(Standard, Extended)

DLC

(6 Bytes)

Data Field

(8 Bytes)

Timestamp Value

(2 Bytes)

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();

Renesas M16C CAN peripheral API

can_initial ( ) config_can ( ) set_bitrate_can ( ) set_mask_can ( )

Initialization functions set_trm_std_dataframe_can ( slot_nr, dataframe* ) check_trm_std_dataframe_can ( slot_nr)

– if polling fnc_c0trmic ( )

– if using interrupts

Transmit functions set_rec_std_dataframe_can ( slot_nr, id ) check_rec_success_can ( slot_nr, dataframe* )

– if polling fnc_c0recic ( )

– if using interrupts get_message_can ( slot_nr, dataframe* ) check_err_can ( ) abort_trm_can ( slot_nr ) check_hw_tmr_can ( canblock1_tmo_diag* )

Receive functions

Error functions

config_can ( )

Initializing CAN Peripheral

CAN Configuration Procedure

START

Enter CAN reset/ initialization mode

Exit CAN sleep mode

• 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

CAN reset mode?

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

NO

Set CAN bit timing and baud rate

Set mask register

Go to CAN operation mode

If using CAN interrupts

Enable CAN interrupts

START

Clear interrupt enable flag

Set interrupt priority level

Set interrupt enable flag

END

NO

CAN operation mode?

YES

END

Set Baud Rate and Bit Timing

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

Setting CAN bit timing and baud rate

START

Clear protect register

Set CAN clock divide-by-N value

Set protect register

Set:

• Prescaler divide-by-N value

• Sampling count

• PTS, PBS1, PBS2, SJW

END

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

Data Frame Transmit Mode set_trm_std_dataframe_can ( slot_nr, dataframe* )

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

If multiple slots are set to data frame transmit mode simultaneously, they transmit in order, starting with slot with lowest number

Verify Successful Transmission 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.

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.

Transmit Abort

Applying a time limit to message transmission

Receiving Message transmission

Error

Error frame

Arbitration loss

Message retransmission Reception

Transmit abort request

Time limit

Transmit request

Transmitting the highest-priority message

Arbitration loss

Message (1) transmission Reception Message (2) transmission

Transmit abort request Transmission request for now highest priority message (2)

Transmit Abort abort_trm_can ( in_slot )

• Transmit incomplete? “SentData” = 0?

• Transmit abort request “TrmReq” 0

• When transmit abort complete, transmit highest-priority message

Abort transmission procedure

START

Transmit requested?

YES

Request abort transmission

NO

YES

Being transmitted?

NO

Send priority message

END

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

Which of the following functions is used to set up a slot to receive?

Select the response that applies and click Done.

a. set_rec_std_dataframe_can b. abort_trm_can c. set_trm_std_dataframe_can d. set_bitrate_can

Done

Mask Setting 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

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

SID

10-6

0 0 1 0 0

1 1 1 1 1

0 0 1 0 0

SID

5-0

1 0 0 0 1 1

1 1 1 1 1 1

1 0 0 0 1 1

Example 2: Receiving standard data frames/remote frames with IDs 122 and 123 in CAN slot 0

SID

10-6

Slot 0 sees on bus

Mask register C0GMR

0 0 1 0 0

1 1 1 1 1

Received message

ID123

ID122

0 0 1 0 0

SID

5-0

1 0 0 0 1 X

1 1 1 1 1 0

1 0 0 0 1 1

0 0 1 0 0 1 0 0 0 1 0

Acceptance Filter Support Unit

The ASU function:

• Determines if received Standard IDs are valid

• Provides faster response than software filtering

Upper 8 bits of received ID

Received ID (11 bits)

Acceptance filter support registers

(ASU registers:

C0AFS, C1AFS)

Converted value of lower

3 bits of received ID

Register configuration during read operation b15 b8 b7 b0

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

Receive Complete using Polling

Confirming by Polling

START

Finished receiving successfully?

YES

Process received message

NO

END

CAN slot status register or “NewData” in CAN message control register

Data received?

If yes: process received message

Note: Check for overruns!

Receive Complete using Interrupt fnc_c0recic ( )

• Copy data to buffer

• Notify main program that data was received

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

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

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.

A set_trm_std_dataframe_can

B fnc_c0recic

C ASU

D fnc_c0trmic

D

Tells main application that message has been sent successfully

C

Determines if received Standard IDs are valid

B

Executed when an interrupt verifies successful data frame reception

A Sets up a slot to transmit

Done Reset

Show

Solution

Timestamp Function

This function:

• Automatically writes timestamp during normal receive operation

• Useful for determining sequence in which a group of messages was received

Standard cycle:

1-bit-time cycle

Prescaler

(Divide by 1, 2, 4, or 8)

Receive slot

Received ID

Received data

Timestamp

16-bit free-running counter

Cycle and initialization are programmable

Reset by software

What Slots Have New Data?

At system setup time:

(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)

Running:

Polled message boxes - check which slots have new data

Receive interrupts - check Active Slot

Bits for which slot has received last

Received message processing

CAN receive interrupt control register (c0recic)

Flag registers

“NewData” in CAN message control register

CAN status register

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

Error Confirmation (Interrupt Processing)

CAN bus off state?

• CAN return from bus off

CAN status register

CAN Status Register

Error

Active

State

-Normal

Error passive state flag

Error bus off state flag Bus error state flag

Set if any

Error frame

CAN

Status

Register b15 b14 b13 b12 b11 b10 b2 b1 b0

Error!

Error!

Error!

Bus

Off

State

- too many errors stop transmitting

Error

Error

Passive

State

Data

Frames

Return From Bus Off State

Slot’s state changes when

- Slot receives 11 consecutive recessive bits 128 times

‘Return from bus off’ function is executed

Error

Active

State

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.

Bus

Off

State

‘Return from bus off’ function b11

CAN control register

1

Sets return from bus off bit to 1

Loopback Function

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

Same ID set for transmit and receive slot

Listen-Only Mode

Receive unit

(Listen-Only mode)

Normal mode

Receive unit

Receive unit

Transmit unit

Transmitting

Message

Receive unit

(Listen-Only mode)

Receive unit

Receive unit

No ACK or error frame transmit operations are performed

Transmit unit

Getting

Responses

• 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 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

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

Course Summary

• CAN peripheral in R8C and M16C microcontrollers

• Initializing the CAN peripheral

• CAN API functions

Download