TWI — I2C compatible two-wire interface

The TWI master is compatible with I2C operating at 100 kHz and 400 kHz.

Figure 1. TWI master's main features
Two-wire interface (TWI) main features

Functional description

This TWI master is not compatible with CBUS. The TWI transmitter and receiver are single buffered.

See TWI master's main features.

A TWI setup with one master and three slaves is shown in the following figure. This TWI master is only able to operate as the only master on the TWI bus.

Figure 2. A typical TWI setup with one master and three slaves
A typical TWI setup with one master and three slaves

This TWI master supports clock stretching performed by the slaves. The TWI master is started by triggering the STARTTX or STARTRX tasks, and stopped by triggering the STOP task.

If a NACK is clocked in from the slave, the TWI master will generate an ERROR event.

Master mode pin configuration

The different signals SCL and SDA associated with the TWI master are mapped to physical pins according to the configuration specified in the PSEL.SCL and PSEL.SDA registers respectively.

If the CONNECT field of a PSEL.xxx register is set to Disconnected, the associated TWI signal is not connected to any physical pin. The PSEL.SCL and PSEL.SDA registers and their configurations are only used as long as the TWI master is enabled, and retained only as long as the device is in ON mode. PSEL.SCL and PSEL.SDA must only be configured when TWI is disabled.

To secure correct signal levels on the pins used by the TWI master when the system is in OFF mode, and when the TWI master is disabled, these pins must be configured in the GPIO peripheral as described in GPIO configuration.

Only one peripheral can be assigned to drive a particular GPIO pin at a time, failing to do so may result in unpredictable behavior.

Table 1. GPIO configuration
TWI master signalTWI master pinDirectionDrive strengthOutput value
SCLAs specified in PSEL.SCLInputS0D1Not applicable
SDAAs specified in PSEL.SDAInputS0D1Not applicable

Shared resources

TWI shares registers and other resources with other peripherals that have the same ID as TWI.

Therefore, you must disable all peripherals that have the same ID as TWI before TWI can be configured and used. Disabling a peripheral that has the same ID as TWI will not reset any of the registers that are shared with TWI. It is therefore important to configure all relevant TWI registers explicitly to secure that it operates correctly.

The Instantiation table in Instantiation shows which peripherals have the same ID as TWI.

Master write sequence

A TWI master write sequence is started by triggering the STARTTX task. After the STARTTX task has been triggered, the TWI master will generate a start condition on the TWI bus, followed by clocking out the address and the READ/WRITE bit set to 0 (WRITE=0, READ=1).

The address must match the address of the slave device that the master wants to write to. The READ/WRITE bit is followed by an ACK/NACK bit (ACK=0 or NACK=1) generated by the slave.

After receiving the ACK bit, the TWI master will clock out the data bytes that are written to the TXD register. Each byte clocked out from the master will be followed by an ACK/NACK bit clocked in from the slave. A TXDSENT event will be generated each time the TWI master has clocked out a TXD byte, and the associated ACK/NACK bit has been clocked in from the slave.

The TWI master transmitter is single buffered. A second byte can only be written to the TXD register after the previous byte has been clocked out and the ACK/NACK bit clocked in, that is, after the TXDSENT event has been generated.

If the CPU is prevented from writing to TXD when the TWI master is ready to clock out a byte, the TWI master will stretch the clock until the CPU has written a byte to the TXD register.

A typical TWI master write sequence is illustrated in The TWI master writing data to a slave. Occurrence 3 in the figure illustrates delayed processing of the TXDSENT event associated with TXD byte 1. In this scenario the TWI master will stretch the clock to prevent writing erroneous data to the slave.

Figure 3. The TWI master writing data to a slave
The TWI master writing data to a slave

The TWI master write sequence is stopped when the STOP task is triggered, causing the TWI master to generate a stop condition on the TWI bus.

Master read sequence

A TWI master read sequence is started by triggering the STARTRX task. After the STARTRX task has been triggered, the TWI master will generate a start condition on the TWI bus, followed by clocking out the address and the READ/WRITE bit set to 1 (WRITE = 0, READ = 1).

The address must match the address of the slave device that the master wants to read from. The READ/WRITE bit is followed by an ACK/NACK bit (ACK=0 or NACK = 1) generated by the slave.

After having sent the ACK bit, the TWI slave will send data to the master using the clock generated by the master.

The TWI master will generate a RXDRDY event every time a new byte is received in the RXD register.

After receiving a byte, the TWI master will delay sending the ACK/NACK bit by stretching the clock until the CPU has extracted the received byte, by reading the RXD register.

The TWI master read sequence is stopped by triggering the STOP task. This task must be triggered before the last byte is extracted from RXD to ensure that the TWI master sends a NACK back to the slave before generating the stop condition.

A typical TWI master read sequence is illustrated in The TWI master reading data from a slave. Occurrence 3 in this figure illustrates delayed processing of the RXDRDY event associated with RXD byte B. In this scenario the TWI master will stretch the clock to prevent the slave from overwriting the contents of the RXD register.

Figure 4. The TWI master reading data from a slave
TWI master reading data from a slave

Master repeated start sequence

A typical repeated start sequence is one in which the TWI master writes one byte to the slave followed by reading M bytes from the slave. Any combination and number of transmit and receive sequences can be combined in this fashion. Only one shortcut to STOP can be enabled at any given time.

The following figure shows a repeated start sequence where the TWI master writes one byte, followed by reading M bytes from the slave without performing a stop in-between.

Figure 5. Repeated start sequence illustration
Repeated start sequence illustration

To generate a repeated start after a read sequence, a second start task, STARTRX or STARTTX, must be triggered instead of the STOP task. This start task must be triggered before the last byte is extracted from RXD to ensure that the TWI master sends a NACK back to the slave before generating the repeated start condition.

Low power

When putting the system in low power and the peripheral is not needed, lowest possible power consumption is achieved by stopping, and then disabling the peripheral.

The STOP task is not always needed, like when the peripheral is already stopped. If the STOP task is sent, the software shall wait until the STOPPED event was received as a response before disabling the peripheral through the ENABLE register.

Registers

Table 2. Instances
Base addressPeripheralInstanceDescriptionConfiguration
0x40003000TWITWI0Two-wire interface master Deprecated
Table 3. Register overview
RegisterOffsetDescription
TASKS_STARTRX0x000Start TWI receive sequence 
TASKS_STARTTX0x008Start TWI transmit sequence 
TASKS_STOP0x014Stop TWI transaction 
TASKS_SUSPEND0x01CSuspend TWI transaction 
TASKS_RESUME0x020Resume TWI transaction 
EVENTS_STOPPED0x104TWI stopped 
EVENTS_RXDREADY0x108TWI RXD byte received 
EVENTS_TXDSENT0x11CTWI TXD byte sent 
EVENTS_ERROR0x124TWI error 
EVENTS_BB0x138TWI byte boundary, generated before each byte that is sent or received 
EVENTS_SUSPENDED0x148TWI entered the suspended state 
SHORTS0x200Shortcuts between local events and tasks 
INTENSET0x304Enable interrupt 
INTENCLR0x308Disable interrupt 
ERRORSRC0x4C4Error source 
ENABLE0x500Enable TWI 
PSEL.SCL0x508Pin select for SCL 
PSEL.SDA0x50CPin select for SDA 
RXD0x518RXD register 
TXD0x51CTXD register 
FREQUENCY0x524TWI frequency. Accuracy depends on the HFCLK source selected. 
ADDRESS0x588Address used in the TWI transfer 

TASKS_STARTRX

Address offset: 0x000

Start TWI receive sequence

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
AWTASKS_STARTRX  Start TWI receive sequence
   Trigger1Trigger task

TASKS_STARTTX

Address offset: 0x008

Start TWI transmit sequence

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
AWTASKS_STARTTX  Start TWI transmit sequence
   Trigger1Trigger task

TASKS_STOP

Address offset: 0x014

Stop TWI transaction

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
AWTASKS_STOP  Stop TWI transaction
   Trigger1Trigger task

TASKS_SUSPEND

Address offset: 0x01C

Suspend TWI transaction

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
AWTASKS_SUSPEND  Suspend TWI transaction
   Trigger1Trigger task

TASKS_RESUME

Address offset: 0x020

Resume TWI transaction

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
AWTASKS_RESUME  Resume TWI transaction
   Trigger1Trigger task

EVENTS_STOPPED

Address offset: 0x104

TWI stopped

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWEVENTS_STOPPED  TWI stopped
   NotGenerated0Event not generated
   Generated1Event generated

EVENTS_RXDREADY

Address offset: 0x108

TWI RXD byte received

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWEVENTS_RXDREADY  TWI RXD byte received
   NotGenerated0Event not generated
   Generated1Event generated

EVENTS_TXDSENT

Address offset: 0x11C

TWI TXD byte sent

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWEVENTS_TXDSENT  TWI TXD byte sent
   NotGenerated0Event not generated
   Generated1Event generated

EVENTS_ERROR

Address offset: 0x124

TWI error

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWEVENTS_ERROR  TWI error
   NotGenerated0Event not generated
   Generated1Event generated

EVENTS_BB

Address offset: 0x138

TWI byte boundary, generated before each byte that is sent or received

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWEVENTS_BB  TWI byte boundary, generated before each byte that is sent or received
   NotGenerated0Event not generated
   Generated1Event generated

EVENTS_SUSPENDED

Address offset: 0x148

TWI entered the suspended state

Generated just after ACK bit has been transferred in a read transaction, and only if SUSPEND has been requested earlier.

Bit number313029282726252423222120191817161514131211109876543210
ID                               A
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWEVENTS_SUSPENDED  

TWI entered the suspended state

Generated just after ACK bit has been transferred in a read transaction, and only if SUSPEND has been requested earlier.

   NotGenerated0Event not generated
   Generated1Event generated

SHORTS

Address offset: 0x200

Shortcuts between local events and tasks

Bit number313029282726252423222120191817161514131211109876543210
ID                              BA
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWBB_SUSPEND  Shortcut between event BB and task SUSPEND
   Disabled0Disable shortcut
   Enabled1Enable shortcut
BRWBB_STOP  Shortcut between event BB and task STOP
   Disabled0Disable shortcut
   Enabled1Enable shortcut

INTENSET

Address offset: 0x304

Enable interrupt

Bit number313029282726252423222120191817161514131211109876543210
ID             F   E    D C    BA 
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWSTOPPED  Write '1' to enable interrupt for event STOPPED
   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
BRWRXDREADY  Write '1' to enable interrupt for event RXDREADY
   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
CRWTXDSENT  Write '1' to enable interrupt for event TXDSENT
   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
DRWERROR  Write '1' to enable interrupt for event ERROR
   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
ERWBB  Write '1' to enable interrupt for event BB
   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
FRWSUSPENDED  

Write '1' to enable interrupt for event SUSPENDED

Generated just after ACK bit has been transferred in a read transaction, and only if SUSPEND has been requested earlier.

   Set1Enable
   Disabled0Read: Disabled
   Enabled1Read: Enabled

INTENCLR

Address offset: 0x308

Disable interrupt

Bit number313029282726252423222120191817161514131211109876543210
ID             F   E    D C    BA 
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWSTOPPED  Write '1' to disable interrupt for event STOPPED
   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
BRWRXDREADY  Write '1' to disable interrupt for event RXDREADY
   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
CRWTXDSENT  Write '1' to disable interrupt for event TXDSENT
   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
DRWERROR  Write '1' to disable interrupt for event ERROR
   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
ERWBB  Write '1' to disable interrupt for event BB
   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled
FRWSUSPENDED  

Write '1' to disable interrupt for event SUSPENDED

Generated just after ACK bit has been transferred in a read transaction, and only if SUSPEND has been requested earlier.

   Clear1Disable
   Disabled0Read: Disabled
   Enabled1Read: Enabled

ERRORSRC

Address offset: 0x4C4

Error source

Bit number313029282726252423222120191817161514131211109876543210
ID                             CBA
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWOVERRUN  

Overrun error

A new byte was received before previous byte got read by software from the RXD register. (Previous data is lost)

   NotPresent0Read: no overrun occured
   Present1Read: overrun occured
BRWANACK  NACK received after sending the address (write '1' to clear)
   NotPresent0Read: error not present
   Present1Read: error present
CRWDNACK  NACK received after sending a data byte (write '1' to clear)
   NotPresent0Read: error not present
   Present1Read: error present

ENABLE

Address offset: 0x500

Enable TWI

Bit number313029282726252423222120191817161514131211109876543210
ID                            AAAA
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWENABLE  Enable or disable TWI
   Disabled0Disable TWI
   Enabled5Enable TWI

PSEL.SCL

Address offset: 0x508

Pin select for SCL

Bit number313029282726252423222120191817161514131211109876543210
IDC                         AAAAA
Reset 0xFFFFFFFF11111111111111111111111111111111
IDAccessFieldValue IDValueDescription
ARWPIN [0..31]Pin number
CRWCONNECT  Connection
   Disconnected1Disconnect
   Connected0Connect

PSEL.SDA

Address offset: 0x50C

Pin select for SDA

Bit number313029282726252423222120191817161514131211109876543210
IDC                         AAAAA
Reset 0xFFFFFFFF11111111111111111111111111111111
IDAccessFieldValue IDValueDescription
ARWPIN [0..31]Pin number
CRWCONNECT  Connection
   Disconnected1Disconnect
   Connected0Connect

RXD

Address offset: 0x518

RXD register

Bit number313029282726252423222120191817161514131211109876543210
ID                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARRXD  RXD register

TXD

Address offset: 0x51C

TXD register

Bit number313029282726252423222120191817161514131211109876543210
ID                        AAAAAAAA
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWTXD  TXD register

FREQUENCY

Address offset: 0x524

TWI frequency. Accuracy depends on the HFCLK source selected.

Bit number313029282726252423222120191817161514131211109876543210
IDAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
Reset 0x0400000000000100000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWFREQUENCY  TWI master clock frequency
   K1000x01980000100 kbps
   K2500x04000000250 kbps
   K4000x06680000400 kbps (actual rate 410.256 kbps)

ADDRESS

Address offset: 0x588

Address used in the TWI transfer

Bit number313029282726252423222120191817161514131211109876543210
ID                         AAAAAAA
Reset 0x0000000000000000000000000000000000000000
IDAccessFieldValue IDValueDescription
ARWADDRESS  Address used in the TWI transfer

Electrical specification

TWI interface electrical specifications

SymbolDescriptionMin.Typ.Max.Units
fTWI,SCLBit rates for TWI1100400kbps
tTWI,STARTTime from STARTRX/STARTTX task to transmission started1.5µs

Two Wire Interface (TWI) timing specifications

SymbolDescriptionMin.Typ.Max.Units
tTWI,SU_DATData setup time before positive edge on SCL – all modes300ns
tTWI,HD_DATData hold time after negative edge on SCL – all modes500ns
tTWI,HD_STA,100kbpsTWI master hold time for START and repeated START condition, 100 kbps10000ns
tTWI,HD_STA,250kbpsTWI master hold time for START and repeated START condition, 250kbps4000ns
tTWI,HD_STA,400kbpsTWI master hold time for START and repeated START condition, 400 kbps2500ns
tTWI,SU_STO,100kbpsTWI master setup time from SCL high to STOP condition, 100 kbps5000ns
tTWI,SU_STO,250kbpsTWI master setup time from SCL high to STOP condition, 250 kbps2000ns
tTWI,SU_STO,400kbpsTWI master setup time from SCL high to STOP condition, 400 kbps1250ns
tTWI,BUF,100kbpsTWI master bus free time between STOP and START conditions, 100 kbps5800ns
tTWI,BUF,250kbpsTWI master bus free time between STOP and START conditions, 250 kbps2700ns
tTWI,BUF,400kbpsTWI master bus free time between STOP and START conditions, 400 kbps2100ns
Figure 6. TWI timing diagram, 1 byte transaction
TWI timing diagram, 1 byte transaction

1 High bit rates or stronger pull-ups may require GPIOs to be set as High Drive, see GPIO chapter for more details.