Multimaster Rej06b0634 - H8sap
Multimaster Rej06b0634 - H8sap
On April 1st, 2010, NEC Electronics Corporation merged with Renesas Technology
Corporation, and Renesas Electronics Corporation took over all the business of both
companies. Therefore, although the old company name remains in this document, it is a valid
Renesas Electronics document. We appreciate your understanding.
(Note 1) “Renesas Electronics” as used in this document means Renesas Electronics Corporation and also includes its majority-
owned subsidiaries.
(Note 2) “Renesas Electronics product(s)” means any product developed or manufactured by or for Renesas Electronics.
APPLICATION NOTE
H8S Family
Multi-Master Mode Communications Using I2C Bus Interface (IIC)
Introduction
This application note describes the usage of the I2C bus interface (IIC) module in the multi-master mode.
Target Device
H8S/2638
Contents
1. Specifications .................................................................................................................................... 2
5. Description of Software................................................................................................................... 11
1. Specifications
• Figure 1 shows the connections for communications using the I2C bus interface in multi-master mode. The slave
addresses and settings for the SAR_0 registers of the individual devices are listed in table 1.
• The multi-master system in this sample task consists of two master devices and one slave device.
• The I2C bus transfer rate is 100 kbits/s (kHz).
• When communications from master 1 and master 2 are attempted simultaneously, the master that loses the
arbitration will stop processing.
The following describes the procedures for the operation of this sample task.
1. The I2C bus interface multi-master transfer starts on the input of the low trigger to the IRQ0 pin of the master side.
2. The master side transmits 128 bytes of data, which have been prepared in the on-chip ROM in advance, to the on-
chip RAM on the slave side.
3. The slave device returns the 128 bytes of data received in step 2 from its on-chip RAM to the on-chip RAM on the
master side.
4. The master side compares the received data in its on-chip RAM with the data transmitted from its on-chip ROM,
and confirms whether the two match.
5. Based on the results of this comparison and the state of arbitration lost, the master side outputs levels on the PE2 to
PE0 pins that indicate the result of operation.
6. From the value of the first byte of received data on the slave side, the slave judges whether the partner in
communications is master 1 or master 2, and outputs levels on pins P15 and P14 that indicate the state of operation.
PE0 PE0
SCL
SDA
SCL SDA
State of operation
P15
P14
Slave
2
Figure 1 Connections for I C Bus Interface Multi-Master Mode Communication
Table 1 Slave Addresses
Device Slave Address Setting Address of SAR_0
Master 1 1 H'02
Master 2 2 H'04
Slave 3 H'06
2. Applicable Conditions
3. Description of Functions
3.1 Description of I2C Bus Interface (IIC)
An I2C bus interface is used in multi-master operation to demonstrate bi-directional communications between in master
mode and slave mode.
4. Description of Operations
4.1 Timing of Operations in Master Transmit Mode
Figure 2 shows the timing of operations of the I2C bus interface in master transmit mode. Table 6 describes processing
by hardware and software at the numbered points in figure 2.
SCL
1 9 1 1 9
SDA
IRIC
Start condition
SCL
1 9 1 9
SDA
IRIC
IRTR
Stop condition
SCL
1 9 1 9
SDA
IRIC
IRTR
(5) (6)
Output position
Issue start condition of start condition
SCL0 pin
Signal output
from master 1
SDA0 pin
Start condition
SCL0 pin
Signal output
from master 2
SDA0 pin
Start condition
SCL
Bus lines
SDA
Note: * Bus line is compared with the signal output from master 1.
Since the signal output from master 1 is at the high level, master 1 loses mastership in bus arbitration.
SCL0 pin 1 2 3 4 5 6 7 8 9
Signal output
from master 1
H'03
SDA0 pin 0 0 0 0 0 0 1 1
SCL0 pin 1 2 3 4 5 6 7 8 9
Signal output
from master 2
H'05
SDA0 pin 0 0 0 0 0 1 0 1
SCL0 pin 1 2 3 4 5 6 7 8 9
Bus lines
[Legend]
A: Acknowledge. Receiver sets SDA to the low level.
Reset
Slave reception
(d) (c)
Master reception
5. Description of Software
5.1 List of Functions
Table 9 List of Functions: main.c file
Function Description
init Initialization routine
Sets the CCR and clock, releases IIC module 0 from module stop mode, and calls
function "main".
main Main routine
• Defining macro for MASTER1 and MASTER2
Selects master mode operation, judges the state of the IRQ0 pin, and handles
master transmission/reception processing.
• Defining macro for SLAVE
Selects slave mode operation and handles slave transmission/reception
processing.
wovi_int WDT interval timer interrupt
*
mrcv_start
iici0_int receive_stop_condition
[Interrupt exception processing]
master_transfer
master_receive
slave_transfer
slave_receive
wovi_int
[Interrupt exception processing]
5.4 Constants
Table 13 Constants
Type Name of Variable Setting Description Usage in Function
unsigned char MTrs_dt[128] H'81, H'01, H'02 … Data for master master_transfer
………. H'7E, H'7F transmission 1 when
defining macro of
MASTER1.
unsigned char MTrs_dt[128] H'82, H'01, H'02 … Data for master master_transfer
………. H'7E, H'7F transmission 2 when
defining macro of
MASTER2.
MSTPCRA, MSTPCRB, MSTPCRC, and MSTPCRD control the module stop mode. Setting a bit in these registers
places the corresponding module in the module stop mode. Clearing a bit takes the module out of module stop mode.
5. Flow Chart
init
tmp = MDCR
Dummy read for latching input level
of mode pin.
CCR = H'80
Initialize the CCR.
Disable interrupts.
SCKCR = H'00
LPWRCR = H'00
System clock = φ
MSTPCRA = H'FF
MSTPCRB = H'EF
MSTPCRC = H'FF
MSTPCRD = H'FF
Release IIC module 0 from
module stop mode.
main()
Execution of the main routine
5. Flow Chart
main
REDDR = H'07
REDR & = H'F8
PE2 to 0 = 0, 0, 0
i=0
Yes
i < DTNUM?
No
MRcv_dt[i] = 0
Clear the reception area.
i++
iic_init
Initialization of the I2C bus
interface
IRQ ON
alcnt = 0
Clear the arbitration lost counter.
I bit in CCR = 0
Enable interrupts.
rtn = mtrs_start()
Setting of the master transmit
mode.
No Stop WDT.
No
alcnt = 0? Yes
Judge arbitration lost.
No rtn = mrcv_start()
Setting of the master receive
mode.
No Stop WDT.
Master reception
Clear the WDT counter.
No
Stop WDT.
No
I bit in CCR = 1
Disable interrupts.
No
err = 0
i=0
Yes
i < DTNUM (128)?
Yes
No MTrs_dt[i] ≠ MRcv_dt[i]? Compare error
Compare the received data
with the original data.
err++
No
Data match Judging whether transmitted data
and received data match
i++
No Compare error
err = 0?
END
5. Flowchart
main
P1DDR = H'30
P1DR = H'CF
P15, P14 = 0, 0
i=0
Yes
i < DTNUM?
No
SRcv_dt[i] = 0
Clear the reception area.
i++
iic_init ()
Initialization of the I2C bus
interface
I bit in CCR = 0
Enable interrupts.
Yes
sr_cnt < DTNUM (128) + 1?
Judge whether or not
128-byte data are
received.
No
No
I bit in CCR = 1
Disable interrupts.
No
No
error
5. Flowchart
wovi_int
Stop WDT.
ICE in ICCR_0 = 0
Initialize the internal state of
IIC module.
ICCR_0 = H'00
Since BBSY = 0, SCP = 0,
this issues the stop condition.
Yes
Wait for two cycles of the
transfer rate clock.
No
ICCR_0 = H'00
Since BBSY = 0, SCP = 0,
this issue the stop condition.
END
5. Flow Chart
iic_init
SCRX = H'38
Enable access to the I2C bus interface data register
and control register.
ICE in ICCR_0 = 0
Enable access to SAR_0.
SAR_0 = SLAVE_ADDR
Set the slave address.
(H'02, 0th bit is 0.)
ICE in ICCR_0 = 1
Enable access to ICMR_0 and ICDR_0.
tmp = ICSR_0
AAS in ICSR_0 = 0
Clear the flag.
ACKB in ICSR_0 = 0
Set the acknowledge bit in the receive mode to 0.
ICMR_0 = H'28
- Consecutively transfer the data and
acknowledge bits.
- With the CPU clock at 20 MHz, this setting plus
the IICX0 bit in SCRX selects a transfer rate
of 100 kbps.
- Transfer counter is set to 9 bits.
ICCR_0 = H'C9
- Enable interrupts.
- Slave receive mode
- Suspend transfer when 1 (non-acknowledgement) is
received as the acknowledge data.
- Clear the bus-busy flag.
mt_cnt = 0;
mr_cnt = 0;
st_cnt = 0;
sr_cnt = 0;
Clear the data counter for master transmission/
master reception/slave transmission/
slave reception.
iic_mode = MODE_SR
Set the processing state of this sample task
as the slave receive mode.
END
3. Return value
Type Description
unsigned char 0: Arguments were normal.
1: Arguments were abnormal.
5. Flow Chart
mtrs_start
rtn = 0
Yes
BBSY in ICCR_0 = 1?
Bus busy?
No
BBSY = 0
The bus is available.
dtnum = 0? No
Number of data for transmission is 0?
mt_data = *dtadd
Set the first address of the data
for transmission.
mt_num = dtnum
Set the number of data for transmission.
mt_cnt = 0
Clear the master transmit counter.
ICCR_0 = H'F9
Master transmit mode.
Enable interrupts.
No
ICCR_0 = H'FC
Issue the start condition.
Return rtn
End
3. Return value
Type Description
unsigned char 0: Arguments were normal.
1: Arguments were abnormal.
5. Flow Chart
mrcv_start
rtn = 0
No
BBSY = 0
The bus is available.
dtnum = 0? No
Number of data for reception is 0?
mr_data = *dtadd
Set the first address of the data
for reception.
mr_num = dtnum
Set the number of data for reception.
mt_cnt = 0
Clear the master receive counter.
ICCR_0 = H'F9
Master transmit mode.
Enable interrupts.
No
ICCR_0 = H'FC
Issue the start condition.
Return rtn
End
5. Flow Chart
iici0_int
STOP in ICSR_0 = 1? No
1
Stop condition detected?
Yes
receive_stop_condition()
Processing of stop condition detection
End
TRS in ICCR_0 = 1? No
master_transfer()
Transmit mode?
Master transmission
Yes
slave_transfer()
Slave transmission
slave_transfer() slave_receive()
Slave transmission Slave reception
5. Flow Chart
receive_stop_condition
tmp = ICSR_0
TCSR_0 = H'00
Clear the status flag.
tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the interrupt flag.
iic_mode = MODE_SR
Set the processing state of this
sample task as the slave receive
mode.
End
5. Flow Chart
master_transfer
AL in ICSR_0 = 1? No
Generation of arbitration lost?
Yes
Arbitration lost in the master mode.
No
ACKB in ICSR_0 = 1? 1
IEIC in ICCR_0 = 0
Yes
Disable the transmit end interrupt.
In transmission when ACKB in ICSR_0 = 1,
no acknowledgement from receiver device.
tmp = ICSR_0
ICSR_0 = H'00 IEIC in ICCR_0 = 0
Clear the flag. Disable interrupts.
ICCR_0 = H'B0
alcnt++ Since BBSY = 0, SCP = 0,
Increment the arbitration lost counter. this issues the stop condition
mt_cnt++
mt_cnt = 0 Increment the master transmit counter.
Clear the master transmit counter.
End
Yes
mt_cnt = 0?
ICDR_0 = MT_ID
No Transmit the slave address in the master
transmit mode.
mt_cnt++
Increment the master transmit counter.
Yes
mt_cnt ≤ mt_num?
No
Transmit end processing ICDR_0 = mt_data[mt_cnt-1]
Load the data for transmission.
tmp = ICCR_0
IRIC in ICCR_0 = 0 Data transmission
Clear the interrupt flag.
mt_cnt++
Increment the master transmit counter.
IEIC in ICCR_0 = 0
Disable interrupts.
tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the interrupt flag.
ICCR_0 = H'80
Since BBSY = 0, SCP = 0,
this issues the stop condition
mt_cnt++
Increment the master transmit
counter.
5. Flow Chart
master_receive
No
mr_cnt = 1? 2
Yes
Receive the
first byte of data.
AL in ICSR_0 = 1? No
Generation of arbitration lost?
Yes
Arbitration lost in No
ACKB in IXSR_0 = 1? 1
the master mode.
Yes
In transmission when ACKB in ICSR_0 = 1,
no acknowledgement from transmitter device.
IEIC in ICCR_0 = 0
Disable I2C bus interface interrupt.
IEIC in ICCR_0 = 0
Disable I2C bus interface interrupt.
tmp = ICSR_0
AL, TDRE, TEND in ICSR_0 = 0
Clear the flag. tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the interrupt flag.
iic_mode = MODE_SR
Set the processing state of this sample task
as the slave receive mode. ICCR_0 =H'B0
Since BBSY = 0, SCP = 0,
this issues the stop condition.
alcnt++
Increment the arbitration lost.
mr_cnt++
Increment the master receive
counter.
mr_cnt = 0
Clear the master receive counter.
6
End
TRS in ICCRA_0 = 0
Set the receive mode. 2
ACKB in ICSR_0 = 0
Output 0 as the acknowledge bit
Yes
in reception. mr_cnt = 1?
tmp = ICCR_0 No
IRIC in ICCR_0 = 0
Clear the interrupt flag. ICDR_0 = MR_ID
Data transmission Transmit the slave address
of the first byte in the master receive mode.
WAIT in ICMR_0 = 1
Insert wait between data and
the acknowledge bit.
tmp = ICCR_0
Transmit the slave IRIC in ICCR_0 = 0
tmp = ICDR_0 address in the master Clear the interrupt flag.
Dummy read. receive mode.
(R/W = 1)
mr_cnt++ mr_cnt++
Increment the master receive Increment the master receive
counter. counter.
6 5
Yes
mr_cnt < mr_num?
No
Yes IRTR bit in ICSR_0 = 1?
Judge completion of
data reception.
No
mr_data[mr_cnt-2] = ICDR_0
Save the data.
3
Data reception
of the second
or later byte mr_cnt++
Increment the master receive
counter.
tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the interrupt flag.
Yes
mr_cnt = mr_num?
No
IRTR in ICSR_0 = 1? Yes
Judge completion of
data reception.
No
ACKB in ICSR_0 = 1
Acknowledge output bit
in reception = 1
No
Data reception of
the last byte
TRS in ICCR_0 = 1
Set to the transmit mode.
mr_data[mr_cnt-2] = ICDR_0
Save the data.
mr_cnt++
Increment the master receive
counter.
tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the interrupt flag.
4 5
Yes
mr_cnt = 2?
No ACKB in ICSR_0 = 1
Transmit end Acknowledge output bit
processing in reception = 1
TRS in ICCR_0 = 1
Set to the transmit mode.
Processing to transmit
one byte of data
tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the interrupt flag.
mr_cnt++
Increment the master receive
counter.
WAIT in ICMR_0 = 0
No Release the interface from
wait mode.
tmp = ICCR_0
IRIC in ICCR_0 = 0 tmp = ICCR_0
Clear the interrupt flag. IRIC in ICCR_0 = 0
Clear the interrupt flag.
mr_data[mr_cnt-2] = ICDR_0
Saving of the last byte of data
Save the data.
mr_cnt++
Increment the master receive
counter.
ICCR_0 = H'B0
Issue the stop condition.
5. Flow Chart
slave_transfer
Yes
st_cnt = 0?
No
AAS in ICSR_0 = 0? Yes
Judge slave address
is detected.
Yes
ACKB in ICSR_0 = 1?
No
No
tmp = ICCR_0 tmp = ICCR_0
IRIC in ICCR_0 = 0 IRIC in ICCR_0 = 0
Clear the flag. Clear the flag.
ACKE in ICCR_0 = 0 or 1
Initialize ACKB = 0.
TRS in ICCR_0 = 0
Receive mode
tmp = ICDR_0
Dummy read for releasing
SCL line.
st_cnt++
Increment the slave transmit
counter.
ICDR_0 = SRcv_dt[st_cnt]
ICDR_0 = SRcv_dt[st_cnt] Transmit the data.
Transmit the data.
tmp = ICCR_0
tmp = ICCR_0 IRIC in ICCR_0 = 0
IRIC in ICCR_0 = 0 Clear the flag.
Clear the flag.
st_cnt++
st_cnt++ Increment the slave transmit
Increment the slave transmit counter.
counter.
End
5. Flow Chart
slave_receive
Yes
sr_cnt = 0?
No
AAS in ICSR_0 = 0? Yes
Judge slave address
is detected.
No
tmp = ICCR_0
IRIC in ICCR_0 = 0
Clear the the flag.
ACKBT in ICIER_0 = 0
Disable interrupts.
sr_cnt++
Increment the slave receive
counter.
End
Inquiries
http://www.renesas.com/inquiry
[email protected]
Revision Record
Description
Rev. Date Page Summary
1.00 Jan.31.07 — First edition issued