110
ATtiny25/45/85 [DATASHEET]
2586Q–AVR–08/2013
Figure 15-3. Three-Wire Mode, Timing Diagram
The three-wire mode timing is shown in
Figure 15-3 At the top of the figure is a USCK cycle reference. One bit is
shifted into the USI Data Register (USIDR) for each of these cycles. The USCK timing is shown for both external
clock modes. In external clock mode 0 (USICS0 = 0), DI is sampled at positive edges, and DO is changed (USI
Data Register is shifted by one) at negative edges. In external clock mode 1 (USICS0 = 1) the opposite edges with
respect to mode 0 are used. In other words, data is sampled at negative and changes the output at positive edges.
The USI clock modes corresponds to the SPI data mode 0 and 1.
Referring to the timing diagram (
Figure 15-3), a bus transfer involves the following steps:
1.
The slave and master devices set up their data outputs and, depending on the protocol used, enable their
output drivers (mark A and B). The output is set up by writing the data to be transmitted to the USI Data
Register. The output is enabled by setting the corresponding bit in the Data Direction Register of Port B.
Note that there is not a preferred order of points A and B in the figure, but both must be at least one half
USCK cycle before point C, where the data is sampled. This is in order to ensure that the data setup
requirement is satisfied. The 4-bit counter is reset to zero.
2.
The master software generates a clock pulse by toggling the USCK line twice (C and D). The bit values on
the data input (DI) pins are sampled by the USI on the first edge (C), and the data output is changed on
the opposite edge (D). The 4-bit counter will count both edges.
3.
Step
2. is repeated eight times for a complete register (byte) transfer.
4.
After eight clock pulses (i.e., 16 clock edges) the counter will overflow and indicate that the transfer has
been completed. If USI Buffer Registers are not used the data bytes that have been transferred must now
be processed before a new transfer can be initiated. The overflow interrupt will wake up the processor if it
is set to Idle mode. Depending of the protocol used the slave device can now set its output to high
impedance.
15.3.2
SPI Master Operation Example
The following code demonstrates how to use the USI as an SPI Master:
SPITransfer:
out
USIDR,r16
ldi
r16,(1<<USIOIF)
out
USISR,r16
ldi
r16,(1<<USIWM0)|(1<<USICS1)|(1<<USICLK)|(1<<USITC)
SPITransfer_loop:
out
USICR,r16
in
r16, USISR
sbrs
r16, USIOIF
rjmp
SPITransfer_loop
in
r16,USIDR
ret
MSB
654321
LSB
1
2
3
4
5
6
7
8
654321
LSB
USCK
DO
DI
D
C
B
A
E
CYCLE ( Reference )