0% found this document useful (0 votes)
147 views

MSP430 Teaching Materials: Communications

Uploaded by

Ivan Petrov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

MSP430 Teaching Materials: Communications

Uploaded by

Ivan Petrov
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPS, PDF, TXT or read online on Scribd
You are on page 1/ 73

MSP430 Teaching Materials

UBI

Chapter 14
Communications
USI Module

Texas Instruments Incorporated


University of Beira Interior (PT)

Pedro Dinis Gaspar, António Espírito Santo, Bruno Ribeiro, Humberto Santos
University of Beira Interior, Electromechanical Engineering Department
www.msp430.ubi.pt

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved

www.msp430.ubi.pt
Contents
UBI

 MSP430 communications interfaces

 USI module introduction

 USI operation: SPI mode

 USI operation: I2C mode

 USI registers (SPI and I2C modes)

 Lab10b: Echo test using SPI

 Lab10c: Echo test using I2C

 Quiz

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
2
www.msp430.ubi.pt
MSP430 communications interfaces (1/2)
UBI

 Equipped with three serial communication interfaces:


 USART (Universal Synchronous/Asynchronous
Receiver/Transmitter):
• UART mode;
• SPI mode;
• I2C (on ‘F15x/’F16x only).

 USCI (Universal Serial Communication Interface):


• UART with Lin/IrDA support;
• SPI (Master/Slave, 3 and 4 wire modes);
• I2C (Master/Slave, up to 400 kHz).

 USI (Universal Serial Interface):


• SPI (Master/Slave, 3 & 4 wire mode);
• I2C (Master/Slave, up to 400 kHz).
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
3
www.msp430.ubi.pt
MSP430 communications interfaces (2/2)
UBI

 Comparison between the communication modules:


USART USCI USI
UART:
UART: - Two modulators support
- Only one modulator n/16 timings
- n/a - Auto baud rate detection
- n/a - IrDA encoder & decoder
- n/a - Simultaneous USCI_A and
USCI_B (2 channels)
SPI:
SPI:
- Two SPI (one on each SPI:
- Only one SPI available
USCI_A and USCI_B) - Only one SPI available
- Master and Slave Modes
- Master and Slave Modes - Master and Slave Modes
- 3 and 4 Wire Modes
- 3 and 4 Wire Modes

I2C:
I (on ‘15x/’16x only)
2C:
I2C:
- Simplified interrupt usage
- Master and Slave Modes - SW state machine needed
- Master and Slave Modes
- up to 400kbps - Master and Slave Modes
- up to 400kbps
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
4
www.msp430.ubi.pt
USI module introduction (1/2)
UBI

 The USI (Universal Serial Interface) module supports


basic SPI and I2C synchronous serial communications;

 It is available in the MSP430x20xx family of devices;

 The USI module supports:


 SPI or I2C modes;
 Interrupt driven;
 Reduces CPU load;
 Flexible clock source selection.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
5
www.msp430.ubi.pt
USI module introduction (2/2)
UBI

 USI block diagram:


 SPI mode:
• Programmable data length (8/16-bit shift register);
• MSB/LSB first.

 I2C mode:
• START/STOP detection;
• Arbitration lost detection.

 Interrupt driven;

 Reduces CPU load;

 Flexible clock source.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
6
www.msp430.ubi.pt
USI operation: SPI and I2C modes (1/5)
UBI

 Shift register and bit counter that include logic to


support SPI and I2C communication;
 USISR shift register (up to 16 bits supported):
 Directly accessible by software;
 Contains the data to be transmitted/received
(simultaneously);
 MSB or LSB first.
 Bit counter:
 Controls the number of bits transmitted/received;
 Counts the number of sampled bits;
 Sets USIIFG when the USICNTx = 0 (decrementing or
writing zero to USICNTx bits);
 Writing USICNTx > 0 automatically clears USIIFG when
USIIFGCC = 0 (automatically stops clocking after last bit).

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
7
www.msp430.ubi.pt
USI operation: SPI and I2C modes (2/5)
UBI

 USI initialization:
 Reset USISWRST;

 Set USIPEx bits (USI function for the pin and maintains the
PxIN and PxIFG functions for the pin):
• Port input levels can be read via the PxIN register by
software;

• Incoming data stream can generate port interrupts on


data transitions.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
8
www.msp430.ubi.pt
USI operation: SPI and I2C modes (3/5)
UBI

 Recommended USI initialization process:


 Set the USIPEx bits in the USI control register (USI function
for the pin and set up the PxIN and PxIFG functions for the
pin as well);
 Set the direction of the RX and TX shift register (MSB or LSB
first) by USILSB bit;
 Select the mode (master or slave) by USIMTS bit;
 Enable or disable output data by USIOE bit;
 Enable USI interrupts by setting USIIE bit;
 Set up USI clock by configuring the USICKCTL control
register;
 Enable USI by setting USISWRST bit;
 Read port input levels via the PxIN register by software;
 Incoming data stream will generate port interrupts on data
transitions.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
9
www.msp430.ubi.pt
USI operation: SPI and I2C modes (4/5)
UBI

 USI clock generation:


 Clock selection multiplexer:
• Internal clocks ACLK or SMCLK;
• External clock SCLK;
• USISWCLK (software clock input bit);
• Timer_A CAP/COM outputs.
 Configurable divider;
 Auto-stop on interrupt: USIIFG;
 Selectable phase and polarity.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
10
www.msp430.ubi.pt
USI operation: SPI and I2C modes (5/5)
UBI

 USICKPL: Selects the inactive level of the SPI clock (data


latching on rising or falling edge);
 USICKPH: Selects the clock edge on which SDO is
updated and SDI is sampled (idle high or low support).
 USIIFG automatically cleared and set by USICNTx;
 Clock stop on IFG: USIIFG and USISTTIFG.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
11
www.msp430.ubi.pt
USI operation: SPI mode (1/2)
UBI

 Configure SPI mode:


 SPI master:
• USIMST = 1;
• USII2C = 0;
• Select clock source;
• SCLK -> output.
 SPI slave:
• USIMST = 0;
• USII2C = 0;
• SCLK -> input;
• Receives the clock externally from the master.
 USIPEx bits enable data and clock pins;
 Port logic functions, including interrupts as normal;
 Data output latched on shift clock.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
12
www.msp430.ubi.pt
USI operation: SPI mode (2/2)
UBI

 SPI interrupts:
 One interrupt vector associated with the USI module;
 One interrupt flag, USIIFG:
• Set when bit counter counts to zero;
• Generates an interrupt request when USIIE = 1;
• Cleared when USICNTx > 0 (USIIFGCC = 0), or directly
by software;
• Stops clock when set.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
13
www.msp430.ubi.pt
USI operation: I2C mode (1/10)
UBI

 Configure USI module in I2C mode:


 USII2C =1;
 USICKPL = 1;
 USICKPH = 0;
 I2C data compatibility:
 USILSB = 0;
 USI16B = 0;
 Enable SCL and SDA
port functions:
 Set USIPE6 and USIPE7.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
14
www.msp430.ubi.pt
USI operation: I2C mode (2/10)
UBI

 I2C master:
 USIMST = 1 and USII2C = 1;
 Select clock source (output to SCL line while USIIFG = 0).

 I2C slave:
 USIMST = 0;
 SCL is held low if USIIFG=1, USISTTIFG=1 or if
USICNTx=0.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
15
www.msp430.ubi.pt
USI operation: I2C mode (3/10)
UBI

 I2C transmitter:
 Data value is first loaded into USISRL;
 USIOE= 1: Enable output and start transmission (writes 8 into
USICNTx);
 Send Start (or repeated Start);
 Define address and set R/W;
 Slave ACK: (Data TX/RX + ACK for N bytes);
 SCL is generated in master mode or released from being held
low in slave mode;
 USIIFG is set after the transmission of all 8 bits (stops clock
signal on SCL in master mode or held low at the next low
phase in slave mode);
 Stop (or repeated Start).
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
16
www.msp430.ubi.pt
USI operation: I2C mode (4/10)
UBI

 I2C receiver:
 Clear USIOE (disable output);

 Enable reception by writing 8 into USICNTx (USIIFG = 0);

 SCL is generated in master mode or released from being


held low in slave mode;

 USIIFG is set after 8 clocks (stops the clock signal on SCL in


master mode or holds SCL low at the next low phase in
slave mode).

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
17
www.msp430.ubi.pt
USI operation: I2C mode (5/10)
UBI

 SDA configuration:
 Direction;
 Used for TX/RX, ACK/NACK handling and START/STOP
generation;
 USIGE: Output latch control;
 USIOE: Data output enable.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
18
www.msp430.ubi.pt
USI operation: I2C mode (6/10)
UBI

 START condition:
 (high-to-low transition on SDA while SCL is high);
 Clear MSB of the shift register;
 USISTTIFG set on start (Sources USI interrupt).

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
19
www.msp430.ubi.pt
USI operation: I2C mode (7/10)
UBI

 STOP condition:
 (low-to-high transition on SDA while SCL is high):
 Clear the MSB in the shift register and loads 1 into USICNTx
(finishes the acknowledgment bit and pulls SDA low);
 USISTP set on stop (CPU-accessible flag).

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
20
www.msp430.ubi.pt
USI operation: I2C mode (8/10)
UBI

 Receiver ACK/NACK generation:


 After address/data reception;
 SDA = output;
 Output 1 data bit: 0 = ACK, 1 = NACK.

 Transmitter ACK/NACK Detection:


 After address/data transmission;
 SDA = input;
 Receive 1 data bit: 0 = ACK, 1 = NACK.

 Arbitration procedure (in multi-master I2C systems);

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
21
www.msp430.ubi.pt
USI operation: I2C mode (9/10)
UBI

 I2C Interrupts:
 One interrupt vector associated with the USI;

 Two interrupt flags, USIIFG and USISTTIFG;

 Each interrupt flag has its own interrupt enable bit, USIIE
and USISTTIE;

 When an interrupt is enabled and the GIE bit is set, a set


interrupt flag will generate an interrupt request;

 USIIFG is set (USICNTx = 0);

 USISTTIFG is set (START condition detection).

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
22
www.msp430.ubi.pt
USI operation: I2C mode (10/10)
UBI

 Example:
 Procedure for I2C communication between a Master TX and a
Slave RX.

Master TX Slave RX
1: Send Start, Address and R/W bit 1: Detect Start, receive address and
R/W
2: Receive (N)ACK 2: Transmit (N)ACK
3: Test (N)ACK and handle TX data 3: Data RX
4: Receive (N)ACK 4: Transmit (N)ACK
5: Test (N)ACK and prepare Stop 5: Reset for next Start
6: Send Stop

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
23
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (1/8)
UBI

 USICTL0, USI Control Register 0


7 6 5 4 3 2 1 0
USIPE7 USIPE6 USIPE5 USILSB USIMST USIGE USIOE USIWRST

Bit Description
7 USIPE7 USI SDI/SDA port enable:
 SPI mode  Input
 I2C mode  Input or open drain output
USIPE7 = 0  USI function disabled
USIPE7 = 1  USI function enabled
6 USIPE6 USI SDO/SCL port enable:
 SPI mode  Output
 I2C mode  Input or open drain output
USIPE6 = 0  USI function disabled
USIPE6 = 1  USI function enabled
5 USIPE5 USI SCLK port enable:
 SPI slave mode  Input
 SPI master mode  Output
 I2C mode  Input
USIPE5 = 0  USI function disabled
USIPE5 = 1  USI function enabled
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
24
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (2/8)
UBI

 USICTL0, USI Control Register 0 (continued)


7 6 5 4 3 2 1 0
USIPE7 USIPE6 USIPE5 USILSB USIMST USIGE USIOE USIWRST

4 USILSB LSB first select (direction of the receive and transmit shift
register):
USILSB = 0  MSB first
USILSB = 1  LSB first
3 USIMST Master select:
USIMST = 0  Slave mode
USIMST = 1  Master mode
2 USIGE Output latch control:
USIGE = 0  Output latch enable depends on shift clock
USIGE = 1  Output latch always enabled and transparent
1 USIOE Data output enable:
USIOE = 0  Output disabled
USIOE = 1  Output enabled
0 USIWRST USI software reset:
USIWRST = 0  USI released for operation
USIWRST = 1  USI logic held in reset state
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
25
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (3/8)
UBI

 USICTL1, USI Control Register 1


7 6 5 4 3 2 1 0
USICKPH USII2C USISTTIE USIIE USIAL USISTP USISTTIFG USIIFG

Bit Description
7 USICKPH Clock phase select:
USICKPH = 0  Data is changed on the first SCLK edge and
captured on the following edge
USICKPH = 1  Data is captured on the first SCLK edge and
changed on the following edge
6 USII2C I2C mode enable:
USII2C = 0  I2C mode disabled
USII2C = 1  I2C mode enabled
5 USISTTIE START condition interrupt-enable:
USISTTIE = 0  Interrupt on START condition disabled
USISTTIE = 1  Interrupt on START condition enabled
4 USIIE USI counter interrupt enable:
USIIE = 0  Interrupt disabled
USIIE = 1  Interrupt enabled
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
26
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (4/8)
UBI

 USICTL1, USI Control Register 1 (continued)


7 6 5 4 3 2 1 0
USICKPH USII2C USISTTIE USIIE USIAL USISTP USISTTIFG USIIFG

3 USIAL Arbitration lost:


USIAL = 0  No arbitration lost condition
USIAL = 1  Arbitration lost
2 USISTP STOP condition received:
USISTP = 0  No STOP condition received
USISTP = 1  STOP condition received
1 USISTTIFG START condition interrupt flag:
USISTTIFG = 0  No interrupt pending
USISTTIFG = 1  Interrupt pending
0 USIIFG USI counter interrupt flag:
USIIFG = 0  No interrupt pending
USIIFG = 1  Interrupt pending

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
27
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (5/8)
UBI

 USICKCTL, USI Clock Control Register


7 6 5 4 3 2 1 0
USIDIVx USISSELx USICKPL USISWCLK
Bit Description
7-5 USIDIVx Clock divider select:
USIDIV2 USIDIV1 USIDIV0 = 000  Divide by 1
USIDIV2 USIDIV1 USIDIV0 = 001  Divide by 2
USIDIV2 USIDIV1 USIDIV0 = 010  Divide by 4
USIDIV2 USIDIV1 USIDIV0 = 011  Divide by 8
USIDIV2 USIDIV1 USIDIV0 = 100  Divide by 16
USIDIV2 USIDIV1 USIDIV0 = 101  Divide by 32
USIDIV2 USIDIV1 USIDIV0 = 110  Divide by 64
USIDIV2 USIDIV1 USIDIV0 = 111  Divide by 128
4-2 USISSELx Clock source select. Not used in slave mode.
USISSEL2 USISSEL1 USISSEL0 = 000  SCLK (1)
USISSEL2 USISSEL1 USISSEL0 = 001  ACLK
USISSEL2 USISSEL1 USISSEL0 = 010  SMCLK
USISSEL2 USISSEL1 USISSEL0 = 011  SMCLK
USISSEL2 USISSEL1 USISSEL0 = 100  USISWCLK bit
USISSEL2 USISSEL1 USISSEL0 = 101  TACCR0
USISSEL2 USISSEL1 USISSEL0 = 110  TACCR1
USISSEL2 USISSEL1 USISSEL0 = 111  TACCR2 (2)
(1)
Not used in SPI mode
(2)
Reserved on MSP430F20xx devices
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
28
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (6/8)
UBI

 USICKCTL, USI Clock Control Register (continued)


7 6 5 4 3 2 1 0
USIDIVx USISSELx USICKPL USISWCLK

1 USICKPL Clock polarity select:


USICKPL = 0  Inactive state is low
USICKPL = 1  Inactive state is high
0 USISWCLK Software clock:
USISWCLK = 0  Input clock is low
USISWCLK = 1  Input clock is high

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
29
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (7/8)
UBI

 USICNT, USI Bit Counter Register


7 6 5 4 3 2 1 0
USISCLREL USI16B USIIFGCC USICNTx

Bit Description
7 USISCLREL SCL line release from low to idle:
USISCLREL = 0  SCL line is held low if USIIFG is set
USISCLREL = 1  SCL line is released
6 USI16B 16-bit shift register enable:
USI16B = 0  8-bit shift register mode. (Uses USISRL low byte)
USI16B = 1  16-bit shift register mode (Uses both USISRx bytes)
5 USIIFGCC USI interrupt flag clear control:
USIIFGCC = 0  USIIFG automatically cleared on USICNTx update
USIIFGCC = 1  USIIFG is not cleared automatically
4-0 USICNTx USI bit count (Number of bits to be received or transmitted)

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
30
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (8/8)
UBI

 USISRL, USI Low Byte Shift Register


7 6 5 4 3 2 1 0
USISRLx

Bit Description
7-0 USISRLx Contents of the USI low byte shift register

 USISRH, USI High Byte Shift Register


7 6 5 4 3 2 1 0
USISRHx

Bit Description
7-0 USISRHx Contents of the USI high byte shift register

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
31
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (1/17)
UBI

 Project files:
 C source files: Chapter 14 > Lab10 > Lab10b1_student.
Chapter 14 > Lab10 > Lab10b2_student.c
 Solution files: Chapter 14 > Lab10 > Lab10b1_solution.c
Chapter 14 > Lab10 > Lab10b2_solution.c

 Overview:
 This laboratory explores the USCI and USI communication
interfaces in SPI mode;

 The MSP430 devices supported by the Experimenter’s board


will exchange messages between themselves;

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
32
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (2/17)
UBI

 Overview (continued):
 MSP430FG4618: Master reads the current state of the slave,
and drives it to the new desired state;
 MSP430F2013: Slave commanded by the Master.

 A. Resources:
 USCI module: MSP430FG4618;
 USI module: MSP430F2013;
 Both units operate in SPI mode;
 Basic Timer1 of the master device is programmed to switch
the status of the slave device once every 2 seconds;
 The slave is notified of the arrival of information through the
end of counting interrupt of the USI module.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
33
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (3/17)
UBI

 A. Resources (continued):

 The resources used are:

• USCI module;

• USI module;

• Basic Timer1;

• Interrupts;

• I/O ports.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
34
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (4/17)
UBI

 B. Software application organization:

MASTER SLAVE

SOMI
USCI USI USI
SIMO
RX SPI SPI ISR
SCLK
Main TX
RX
Master Task

TX

P3.0 Slave Status P1.4


Main
Slave Task
Basic Timer Basic
2s ISR Timer LED3

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
35
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (5/17)
UBI

 B. Software application organization:

 The master unit is composed of two software modules:


• The "Main master task" module contains the operational
algorithm of the master unit;
• The "ISR Basic Timer" module wakes the "Main master
task" with a rate of once every 2 seconds.

 Similarly, the slave unit is composed of two modules:


• The "Main slave task" module contains the operational
algorithm of the slave unit;
• The "USI ISR" module reads the data received, prepares
the USI module for reception of a new command and
wakes the "Main slave task" to execute the algorithm
associated with the reception of the new command.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
36
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (6/17)
UBI

 C. Configuration:

 Configure the control registers USCI_B (master):

• The SPI connection will operate as follows:


– Clock phase -> Data bits are sent on the first UCLK
edge and captured on the following edge;
– Clock polarity -> the inactive state is low;
– MSB first;
– 8-bit data;
– Master mode;
– 3-Pin SPI;
– Source clock -> SMCLK.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
37
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (7/17)
UBI

 C. Configuration (continued):

 Configure the control registers USCI_B (master):

• Configure the following control registers based on these


characteristics:

UCB0CTL0 = _______________;

UCB0CTL1 = _______________;

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
38
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (8/17)
UBI

 C. Configuration (continued):

 Data rate USCI_B (master):


• The system clock is configured to operate with a
frequency of ~ 1048 kHz from the DCO;

• This frequency will be the working base of the USCI


module;

• The connection operates at a clock frequency of ~ 500


kHz. Configure the following registers:

UCB0BR0= _______________;

UCB0BR1= _______________;
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
39
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (9/17)
UBI

 C. Configuration (continued):

 Ports configuration USCI_B (master):

• In order to set the external interfaces of the USCI


module, it is necessary to configure the I/O ports;

• Select the USCI peripheral in SPI mode following the


connections provided at the Experimenter’s board:

P3SEL = __________________;

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
40
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (10/17)
UBI

 C. Configuration (continued):

 Configure the control registers USI (slave):

• The SPI connection will operate in the following mode:


– MSB first;
– 8-bit data;
– Slave mode;
– Clock phase -> Data bits are sent on the first SCLK
edge and captured on the following edge;
– USI counter interrupt enable.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
41
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (11/17)
UBI

 C. Configuration (continued):

 Configure the control registers USI (slave):

• Configure the following control registers based on these


characteristics:

USICTL0 = _______________;

USICTL1 = _______________;

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
42
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (12/17)
UBI

 D. Analysis of operation:

 Once the USCI module is configured in accordance with the


previous steps, to initiate the experiment, complete the files
Lab10b1_student.c (master – MSP430FG4618) and
Lab10b2_student.c (slave – MSP430F2013), compile them
and run them on the Experimenter’s board;
 The finished solution can be found in the files
Lab10b1_solution.c and Lab10b2_soluction.c.

 For this laboratory, the following jumper settings are


required:
• PWR1/2, BATT, LCL1/2, JP2;
• SPI: H1- 1&2, 3&4, 5&6, 7&8.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
43
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (13/17)
UBI

 D. Analysis of operation:

 Verification:
• Once the program code is running in the two
microcontrollers, monitor LED3 of the Experimenter’s
board. It will blink at a rate of 4 flashes per second.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
44
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (14/17)
UBI

MSP-EXP430FG4618 SOLUTION
(master)
Using USCI module in SPI mode included in the FG4618 (configured as master) of the
Experimenter’s board, establish a connection to the F2013 by its USI module in SPI
mode. The data exchanged is displayed by the LED blinking.

 Control registers USCI_B (master):


UCB0CTL0 = 0x29;
//UCB0CTL0 = UCCKPH|UCCKPL|UCMSB|UC7BIT|UCMST|UCMODEx|UCSYNC
//UCCKPH (Clock phase) = 0b -> Data is changed on the
// first UCLK edge and captured on the following edge.
//UCCKPL (Clock polarity) = 0b -> Inactive state is low
//UCMSB (MSB first select) = 1b -> MSB first
//UC7BIT (Character length) = 0b -> 8-bit data
//UCMST (Master mode) = 1b -> Master mode
//UCMODEx (USCI mode) = 00b -> 3-Pin SPI
//UCSYNC (Synch. mode enable) = 1b -> Synchronous mode

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
45
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (15/17)
UBI

 Control registers USCI_B (master):


UCB0CTL1 = 0x81;
//UCB0CTL1 = UCSSELx | Unused |UCSWRST|
//UCSSELx (USCI clock source select)= 10b -> SMCLK
//UCSWRST (Software reset) = 1b -> normally set by a PUC

 Data rate USCI_B (master):


UCB0BR0 = 0x02;
UCB0BR1 = 0x00;
// Data rate = SMCLK/2 ~= 500kHz
// UCB0BR1 = 0x00 & UCB0BR0 = 0x02

 Configure I/O ports:


P3SEL |= 0x0E;
// P3.1,P3.2,P3.3 option select

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
46
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (16/17)
UBI

MSP-EXP430F2013 SOLUTION
(slave)
Using the USCI module in SPI mode included in the FG4618 (configured as
master) of the Experimenter’s board, establish a connection to the F2013 by
its USI module in SPI mode. The data exchanged is displayed by the LED
blinking.

 USI (slave) control registers:


USICTL0 = 0xE3;
//USICTL0 = USIPE7|USIPE6|USIPE5|USILSB|USIMST|USIGE|USIOE|USISWRST
//USIPE7 (USI SDI/SDA port enable) = 1b -> USI enabled
//USIPE6 (USI SDO/SCL port enable) = 1b -> USI enabled
//USIPE5 (USI SCLK port enable) = 1b -> USI enabled
//USILSB (LSB first) = 0b -> MSB first
//USIMST (Master) = 0b -> Slave mode
//USIGE (Output latch control) = 0b -> Output latch enable
//USIOE (Serial data output enable) = 1b -> Output enabled
//USISWRST (USI software reset) = 1b -> Software reset

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
47
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (17/17)
UBI

 USI (slave) control registers:

USICTL1 = 0x10;

//USICTL1= USICKPH|USII2C|USISTTIE|USIIE|USIAL|USISTP|USISTTIFG|USIIFG
//USICKPH (Clock phase select) = 0b -> Data is changed on the first
// SCLK edge and captured on the following edge
//USII2C (I2C mode enable) = 0b -> I2C mode disabled
//USISTTIE (START condition interrupt) = 0b -> Not used
//USIIE (USI counter) = 1b -> Interrupt enabled
//USIAL (Arbitration lost) = 0b -> Not used
//USISTP (STOP condition received) = 0b -> Not used
//USISTTIFG (START condition int. flag) = 0b -> Not used
//USIIFG (USI counter int. flag) = 0b -> No int. pending

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
48
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (1/21)
UBI

 Project files:
 C source files: Chapter 14 > Lab10 > Lab10c1_student.c
Chapter 14 > Lab10 > Lab10c2_student.c
 Solution files: Chapter 14 > Lab10 > Lab10c1_solution.c
Chapter 14 > Lab10 > Lab10c2_solution.c

 Overview:
 This laboratory explores the USCI and USI communication
interfaces in I2C mode;
 It uses the two MSP430 devices included on the
Experimenter’s board: MSP430FG4618 as the master and
the MSP430F2013 as slave;
 The master receives a single byte from the slave as soon as
a button connected to P1.0 is pressed.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
49
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (2/21)
UBI

 A. Resources:
 This laboratory uses the USCI module of the MSP430FG4618
device and the USI module included in the MSP430F2013.
Both units operate in I2C mode;

 The interrupts on the slave unit are generated exclusively by


the USI module. They are:
• START condition on the I2C bus;
• Data reception and transmission.

 The interrupts in the master unit are provided by the USCI


module. They are:
• Data reception;
• Logic level change on Port1.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
50
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (3/21)
UBI

 A. Resources:

 The resources used are:

• USCI module;

• USI module;

• Interrupts;

• I/O ports.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
51
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (4/21)
UBI

 B. Software application organization:

 Software architecture:
MASTER SLAVE
RX Buffer Slave Data

SDA
USCI USCI USI USI
RX SCL
ISR I2C I2C ISR

RX

P1 TX TX
ISR

P1.0 S1 LED3
Status

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
52
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (5/21)
UBI

 B. Software application organization:

 The master task is composed of two interrupt service


routines:
• The S1 switch service routine is used to control the way
the master receives a new data frame from the slave;
• The USCI module interrupt service routine ensures that the
data sent by the slave is read by the master .

 A state machine has been implemented for the USI module of


the slave unit;

 It is important to note that the states “RX Address” and “RX


(N)ACK" are transient states that ensure the USI module is
ready for the next activity.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
53
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (6/21)
UBI
I2C USI SLAVE STATE MACHINE
 B. Software application organization:
IDLE

 Slave state machine: 1

RX
4 Adress

7
Process
Adress 3

1- START condition TX

2- Adress received
5
3- Read command
4 - Adress not match RX
(N)ACK
5 – Data sent

6 – (N)ACK reveived 6

7 – END
Process
ACK

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
54
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (7/21)
UBI

 C. Configuration:

 Configure the control registers USCI_B (master):

• The connection via I2C bus is to operate as follows:


– Address slave with 7-bit address;
– Master mode;
– Single master;
– USCI clock source is SMCLK.

• Configure the following control registers based on these


characteristics:
UCB0CTL0 = _______________;
UCB0CTL1 = _______________;
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
55
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (8/21)
UBI

 C. Configuration (continued):

 Data rate USCI_B (master):


• The system clock is configured to operate with a
frequency of ~ 1048 kHz from the DCO;

• This frequency will be the working base for the USCI


module;

• The connection operates at a clock frequency of


~ 95.3kHz. Configure the following registers:

UCB0BR0= _______________;
UCB0BR1= _______________;
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
56
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (9/21)
UBI

 C. Configuration (continued):

 Ports configuration USCI_B (master):

• In order to set the external interfaces for the USCI


module, it is necessary to configure the I/O ports;

• Select the USCI peripheral in I2C mode to be compatible


with the connections on the Experimenter’s board:

P3SEL = __________________;

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
57
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (10/21)
UBI

 C. Configuration (continued):

 Configure the control registers USI (slave):


• The connection via I2C bus is to operate as follows:
– Slave mode;
– USI counter interrupt enable (RX and TX);
– START condition interrupt-enable;
– USIIFG is not cleared automatically.

• Configure the following control registers:


USICTL0 = _______________;
USICTL1 = _______________;
USICNT = ________________;
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
58
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (11/21)
UBI

 C. Configuration (continued):

 Configure the control registers USI (slave):


• The slave unit interrupt service routine is not yet
complete. The portion related to the “I2C_TX” state
needs to be completed:
– Configure the USI module as an output;
– Insert the value to transmit in the transmit register;
– Configure the bit counter.

USICTL0 |=________________;
USISRL =_________________;
USICNT |=________________;

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
59
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (12/21)
UBI

 D. Analysis of operation:
 Once the USCI module is configured in accordance with the
previous steps, to initiate the experiment, complete the files:
• Lab10c1_student.c (master – MSP430FG4618)
• Lab10c2_student.c (slave – MSP430F2013)
Compile them and run them on the Experimenter’s board;

 The completed solution can be found in the files


Lab10c1_solution.c and Lab10c2_soluction.c.

 For this laboratory it is necessary to set up the following


jumper settings:
• PWR1/2, BATT, LCL1/2, JP2;
• SPI: H1- 1&2, 3&4.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
60
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (13/21)
UBI

 D. Analysis of operation:

 Verification:
• The slave data values are sent and incremented from
0x00 with each transmitted byte, and are verified by the
Master;

• The LED is off for address/data Acknowledge and the LED


turns on for address/data Not Acknowledge;

• The LED3 blinks at each data request:


– It is turned on by a START condition;
– It is turned off by the data transmit acknowledge by
the slave;
(Note: the I2C bus is not released by the master
because the successive START conditions are
interpreted as “repeated START”).
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
61
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (14/21)
UBI

 D. Analysis of operation:

 Verification:

• Verify the value received by setting a breakpoint in the


line of code “RxBuffer = UCB0RXBUF;” of the USCI
interrupt.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
62
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (15/21)
UBI

MSP-EXP430FG4618 SOLUTION
(master)
Using USCI module in I2C mode included in the FG4618 (configured as
master) of the Experimenter’s board, establish a connection to the F2013 by
its USI module in I2C mode. The master receives a single byte from the slave
as soon as a button connected on P1.0 is pressed.

 USCI (master) control registers:


UCB0CTL0 = 0x0F;
//UCB0CTL0 =
//UCA10|UCSLA10|UCMM|Unused|UCMST|UCMODEx|UCSYNC|
//UCA10 (Own address) = 0b -> Own address (7-bit)
//UCSLA10 (Slave address) = 0b -> 7-bit slave address
//UCMM (Multi-master) = 0b -> Single master
//Unused
//UCMST (Master mode) = 1b -> Master mode
//UCMODEx (USCI mode) = 11b -> I2C Mode
//UCSYNC (Synchronous mode enable) = 1b -> Synchronous

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
63
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (16/21)
UBI

 USCI (master) control registers:

UCB0CTL1 = 0x81;

//UCB0CTL1 =
//UCSSELx|Unused|UCTR|UCTXNACK|UCTXSTP|UCTXSTT|UCSWRST|
//UCSSELx (USCI clock source select) = 10b -> SMCLK
//Unused
//UCTR (Transmitter/Receiver) = 0b -> Receiver
//UCTXNACK (Transmit a NACK) = 0b -> Ack normally
//UCTXSTP (Transmit STOP condition) = 0b -> No STOP
//UCTXSTT (Transmit START condition) = 0b -> No START
//UCSWRST (Software reset) = 1b -> Enabled

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
64
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (17/21)
UBI

 Data rate:
// DATA RATE
// data rate -> fSCL = SMCLK/11 = 95.3 kHz
UCB0BR0 = 0x0B; // fSCL = SMCLK/11 = 95.3 kHz
UCB0BR1 = 0x00;

 Configure ports:
P3SEL |=0x06; // Assign I2C pins to USCI_B0

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
65
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (18/21)
UBI

MSP-EXP430F2013 SOLUTION
(slave)
Using USCI module in I2C mode included in the FG4618 (configured as
master) of the Experimenter’s board, establish a connection to the F2013 by
its USI module in I2C mode. The master receives a single byte from the slave
as soon as a button connected on P1.0 is pressed.

 USI (slave) control registers:


USICTL0 = 0XC1;
//USICTL0 =|USIPE7|USIPE6|USIPE5|USILSB|USIMST|USIGE|USIOE|USISWRST|
//USIPE7 (USI SDI/SDA port enable) = 1b -> USI function enabled
//USIPE6 (USI SDO/SCL port enable) = 1b -> USI function enabled
//USIPE5 (USI SCLK port enable) = 0b -> USI function disable
//USILSB (LSB first) = 0b -> MSB first
//USIMST (Master) = 0b -> Slave mode
//USIGE (Output latch control) = 0b -> Depends on shift clock
//USIOE (Serial data output enable) = 0b -> Output
enabled
//USISWRST (USI software reset) = 1b -> Software reset
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
66
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (19/21)
UBI

 USI (slave) control registers:


USICTL1 = 0x70;

//USICTL1 =
//|USICKPH|USII2C|USISTTIE|USIIE|USIAL|USISTP|USISTTIFG|USIIFG|
//USICKPH (Clock phase select) = 0b -> Data is changed
// on the first SCLK edge and captured on the following edge.
//USII2C (I2C mode enable) = 1b -> I2C mode enabled
//USISTTIE = 1b -> Interrupt on START condition enabled
//USIIE = 1b -> USI counter interrupt enable
//USIAL (Arbitration lost) = 0b -> Not used
//USISTP (STOP condition received) = 0b -> Not used
//USISTTIFG (START condition int. flag) = 0b -> Not used
//USIIFG (USI counter int. flag) = 0b -> No int. pending

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
67
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (20/21)
UBI

 USI Bit Counter Register:

USICNT |= 0x20;

//USICNT =
//USISCLREL| USI16B |USIIFGCC |USICNTx|

//USISCLREL (SCL release) = 0b -> SCL line is held low


// if USIIFG is set
//USI16B (16-bit shift register enable) = 0b -> 8-bit
// shift register mode
//USIIFGCC (USI int. flag clear control) = 1b -> USIIFG
// is not cleared automatically
//USICNTx (USI bit count) = 00000b (not relevant)

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
68
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (21/21)
UBI

 I2C state machine:

USICTL0 |= USIOE; // SDA = output


USISRL = SlaveData; // Send data byte
USICNT |= 0x08; // Bit counter = 8, TX data

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
69
www.msp430.ubi.pt
Quiz (1/4)
UBI

 1. The USI module has:


(a) A SPI interface;
(b) An I2C interface;
(c) All of above;
(d) None of above.

 2. The internal USI clock generation can use:


(a) ACLK and SMCLK;
(b) ACLK and MCLK;
(c) SMCLK and MCLK;
(d) None of above.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
70
www.msp430.ubi.pt
Quiz (2/4)
UBI

 3. The USISR shift register supports:


(a) 8 bits;
(b) 16 bits;
(c) All of above;
(d) None of above.

 4. The USIIFG is set when:


(a) Bit counter counts to 0xFF;
(b) Bit counter counts to 0x00;
(c) Bit counter counts to 0x80;
(d) Bit counter counts to 0x08.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
71
www.msp430.ubi.pt
Quiz (3/4)
UBI

 5. After address/data reception, the receiver ACK/NACK


is:
(a) SDA = input: 0 = ACK, 1 = NACK;
(b) SDA = output: 0 = ACK, 1 = NACK;
(c) SDA = input: 1 = ACK, 0 = NACK;
(d) SDA = output: 1 = ACK, 0 = NACK.

 6. After address/data transmission the transmitter


ACK/NACK is:
(a) SDA = input: 0 = ACK, 1 = NACK;
(b) SDA = output: 0 = ACK, 1 = NACK;
(c) SDA = input: 1 = ACK, 0 = NACK;
(d) SDA = output: 1 = ACK, 0 = NACK.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
72
www.msp430.ubi.pt
Quiz (4/4)
UBI

 Answers:

1. (c) All of above.

2. (a) ACLK and SMCLK.

3. (c) All of above.

4. (b) Bit counter counts to 0x00.

5. (b) SDA = output: 0 = ACK, 1 = NACK.

6. (a) SDA = input: 0 = ACK, 1 = NACK.

Copyright 2009 Texas Instruments


>> Contents All Rights Reserved
73
www.msp430.ubi.pt

You might also like