
RP6 ROBOT SYSTEM - 4. Programming the RP6
The arrays and the variable for general calls are called:
I2CTWI_readRegisters, I2CTWI_writeRegisters and I2CTWI_genCallCMD
The readable registers are named I2CTWI_readRegisters and the writeable registers
I2CTWI_writeRegisters. The I2CTWI_genCallCMD variable stores the most recently re-
ceived General Call command. Data exchange in slave mode works completely with
these registers.
In order to make specific data available on the bus, you have to put it in the array
I2CTWI_readRegisters. A master may now read this data by addressing the corres-
ponding array position (equivalent to a register number). For instance, if a master
needs to read sensor data from a slave, the slave first has to put the information into
a predefined location in the I2CTWI_readRegisters array. Then the master can read
the data by transferring the register number to the slave and subsequently read the
information. The register number will be automatically incremented to allow the mas-
ter to read several registers in a single run.
A similar procedure has to be followed to write data. The master initially transmits the
register number and then starts transferring data. In analogy to the reading process,
the register number will be automatically incremented to allow the master to write
several registers in a single run. The slave performs this completely in background by
using interrupts.
While writing to the slave, data structures may easily get inconsistent if the data is
used simultaneously. If you read data from one register, another one belonging to this
register may have been overwritten by the master in the meantime. A better way to
handle these transfers is an intermediate storage location. Reading data may also lead
to inconsistencies in handling related variables (e.g. low and high bytes for 16 Bit
Variables).
I2CTWI_readBusy and I2CTWI_writeBusy
The interrupt routine sets the variable I2CTWI_writeBuisy to true – and this can be
used to check for writing access to this data. If it is set to “false”, we may transfer
data from the registers into temporary variables and use these for further processing.
The example program on the following page and the “slave”-example program on the
CD demonstrate this – there is a command register, used by the master device to
transfer commands to the slave (e.g. “start driving forwards at a speed of 100”). The
main loop of the slave device is constantly evaluating register 0 as long as
I2CTWI_busy is set to false. The arrival of a master command in register 0 will be fol-
lowed by transferring data from register 0 and registers 1 up to 6 into temporary vari-
ables, which may be evaluated afterwards. Parameter usage depends on the content
of the command variable. E.g. parameter 1 may describe a speed value for a move-
ment command and parameter 2 the direction. Any other parameters would be ig-
nored with this command.
The variable I2CTWI_readBusy works similar – it is set whenever a register is being
read and allows us to check write ability for registers and to prevent inconsistencies.
Current implementations cannot guarantee consistency to 100% as this would imply
deactivation of TWI-Interrupt during write processes to the registers, which might
cause other problems...
- 105 -