CONFIG TWIXSLAVE

From MCS Wiki AVR
Revision as of 17:03, 4 February 2014 by Admin (Talk | contribs)
Jump to: navigation, search

Contents

Action

Configure the Xmega TWIC,TWID,TWIE or TWIF hardware to be used a a slave.

 

 

Syntax

CONFIG TWICSLAVE = address , BTR = value ,GENCALL=value

CONFIG TWIDSLAVE = address , BTR = value ,GENCALL=value

CONFIG TWIESLAVE = address , BTR = value ,GENCALL=value

CONFIG TWIFSLAVE = address , BTR = value ,GENCALL=value

 

(I2C TWI Slave is part of the I2C-Slave library. This is an add-on library which is not included with Bascom-AVR by default. It is a commercial add on library. It is available from MCS Electronics )

 

See also: I2C TWI SlaveUSING I2C ProtocolUsing USICONFIG I2CSLAVE , CONFIG USI

 

Remarks

Address

The slave address which is assigned to the slave chip. This must be an Even number. Bit 0 of the address is used to activate the general call address.

The GENCAL option will set this bit automatic.

 

I2C uses a 7 bit address from bit 1 to bit 7. Bit 0 is used to specify a read/write operation. In BASCOM the byte transmission address is used for I2C.

This means that an I2C 7-bit address of 1 becomes &B10 = 2. And we say the address is 2. This is done so you can copy the address from the data sheets which are in the same format in most cases.

So if you work with 7 bit address, you need to multiply the address by 2.

BTR

Bytes to receive. With this constant you specify how many bytes will be expected when the master reads data from the slave. And thus how many bytes will be sent to the master. This value can be changed dynamically.

GENCALL

General call address activated or not. When you specify 1, the General call address will be activated which mean that the slave will respond not only to it's own address, but also to the general call address 0.

When you omit the option or specify 0, the general call address will not be honored.

 

The variables  TwiX , TwiX_btr, TwiX_CBTR and TwiX_btw are created by the compiler. These are all byte variables.

The X represents the TWI interface letter which can be C, D, E or F.

 

The TWIx interrupt is enabled as well but you need to enabled the global interrupt

 

The TWI Slave code is running as an interrupt process. Each time there is a TWI interrupt some slave code is executed. Your BASIC code is called from the low level slave code by a number of events. You must include all these labels in your Slave application. You do not need to write code in all these sub routines.

 

Label

Event

Twi_stop_rstart_received

TwiD_stop_rstart_received

TwiE_stop_rstart_received

TwiF_stop_rstart_received

The Master sent a stop(i2CSTOP) or repeated start.

Typical you do not need to do anything here.

Twi_addressed_goread

TwiD_addressed_goread

TwiE_addressed_goread

TwiF_addressed_goread

The master has addressed the slave and will now continue to send data to the slave. You do not need to take action here.

Twi_addressed_gowrite

TwiD_addressed_gowrite

TwiE_addressed_gowrite

TwiF_addressed_gowrite

The master has addressed the slave and will now continue to receive data from the slave. You do not need to take action here.

Twi_gotdata

TwiD_gotdata

TwiE_gotdata

TwiF_gotdata

The master has sent data. The variable TWIx holds the received value. The byte TWIx_BTW is an index that holds the value of the number of received bytes. The first received byte will have an index value of 1.

Twi_master_needs_byte

TwiD_master_needs_byte

TwiE_master_needs_byte

TwiF_master_needs_byte

The master reads from the slave and needs a value. The variable TWIx_BTR can be inspected to see which index byte was requested. With the CONFIG parameter BTR, you specify how many bytes the master will read. This value is stored in the variable TWIx_CBTR. You can alter this value but you should not do that in the middle of a transaction.

 

The name of the label called depends on the used TWI interface. TWIC is the default TWI interface. All I2C commands work with TWIC by default.

In order to make the normal slave code compatible with the Xmega, the TWIC interface uses the same label names as used for normal AVR TWI interface.

This means that your BASCOM slave code for the M32 should work for the TWIC interface without much changes.

 

Notice.jpgIt is important that you do not use the MASTER TWI routines when using the TWI as a slave. Just supply or read data at the provided routines.

 

In most cases your main application is just an empty DO LOOP. But when you write a slave that performs other tasks on the background these other tasks are interrupted by the TWI traffic.

Do NOT write blocking code inside an interrupt. While servicing another interrupt, the TWI interrupt can not be serviced.

Also, do not block execution by putting delays in the called routines such as TWI_GOTDATA. All these labels are called from the TWIX SLAVE library which is an interrupt routine that will halt the main application and other interrupts.

 

The TWI Slave code will save all used registers.

 

In order to get a working slave it is important that the slave matches the protocol used by the master. Thus if the slave reads data from the master and only expects 2 bytes, the master should not send less or more. We advise to make a simple slave first like a PCF8574 clone.

 

 

See also

CONFIG TWI

Example

The following example uses two TWI interfaces. TWID is used in master mode while TWIC is used as the slave.

Personal tools
Namespaces
Variants
Actions
Navigation
Language