DIO protocol

From BitWizard Wiki
Jump to navigation Jump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Introduction

The protocol for the DIO, 3FETs, 7FETs, RELAY, BIGRELAY and Pushbutton will be explained on this page. Most functions apply to all boards, but some don't.

This page describes both the SPI and the I2C version. See SPI versus I2C protocols for the explanation about how the protocols work in general.

Please see this page for the default addresses.

Write Ports

On the DIO and related boards all ports just set a single value. So writing more than one byte to such a port is redundant. The last value is the one used. The DIO boards don't have any ports that are logically a stream of bytes. So writing more than one or two bytes is not encouraged.

The DIO, 3FETS and 7FETS boards define several ports:

port available on function
DIO 3/7FETs RELAY and BIGRELAY pushbutton
0x10 X X X set all outputs (bit 0 is output 0, etc).
0x20 .. 0x27 X X X set one output (0x20 for output 0, 0x21 for output 1 etc)
0x30 X X X define pins as inputs or outputs. 0 means input, 1 means output.
0x40 X X set current position. (32 bits) The current position will be "renamed" to the given value. No motor movement will occur.
0x41 X X set target position. (32 bits) The motor will start stepping (hopefully in the rigth direction) to go to this position. You can read the current position to see if it has arrived yet.
0x42 X X set relative position. (32 bits) The target position is adjusted with this number.
0x43 X X set stepdelay. (in tenths of a microsecond, default 200: 20ms between steps). (8bits)
0x50 .. 0x57 v1.1 and up X Set PWM value. 0x50: output 0, 0x51 output 1 etc.
0x5f v1.1 and up X Set PWM mask. PWM is enabled on the outputs, who's bit is high. send 0x01 as data, to enable PWM on output 0
0x70 .. 0x77 v1.2 and up Select which i/o is coupled to which ADC channel. See analog inputs
0x80 v1.2 and up Set number of ADC channels to read
0x81 v1.2 and up Set number of samples to add (we suggest using a power of 2) (two bytes)
0x82 v1.2 and up Set number of bits to shift accumulated sample value
0xf0 X X X X change address. Requires a write to 0xf1 and 0xf2 first.
0xf1 X X X X write 0x55 here to start unlocking the change address register.
0xf2 X X X X write 0xaa here to unlock the change address register.

All the above ports are read/write. I.e. if you read from that port, you will get the current value.

Read Ports

The DIO, 3FETS, and 7FETS boards support the following read ports:

port available on function
DIO 3/7FETs RELAY pushbutton
0x01 X X X X identification string. (terminated with 0).
0x02 X X X X read eeprom (serial number).
0x10 X X X X read all inputs
registers 0x14 - 0x17: Implemented in DIO 1.5 and up.
0x14 X "hasbeenlow". The signal has been seen "low" since you last read this register.
0x15 X "hasbeenhigh". The signal has been seen "high" since you last read this register.
0x16 X "hasgoneup" the signal has been seen to transition "UP" since you last read this register.
0x17 X "hasgonedown" the signal has been seen to transition "DOWN" since you last read this register.
0x20 .. 0x27 X X read one input (0x20 for input 0, 0x21 for input 1 etc)
0x40 X X read current position.
0x41 X X read target position.
0x43 X X read stepdelay. (in tenths of a microsecond, default 200: 20ms between steps).
0x50 v1.1 and up X Return PWM value for output 0
0x51 v1.1 and up X Return PWM value for output 1
0x52 v1.1 and up X Return PWM value for output 2
0x53 v1.1 and up X Return PWM value for output 3
0x54 v1.1 and up X Return PWM value for output 4
0x55 v1.1 and up X Return PWM value for output 5
0x56 v1.1 and up X Return PWM value for output 6
0x5f v1.1 and up X Return PWM mask. PWM is enabled on the outputs, who's bit is high. send 0x01 as data, to enable PWM on output 0
0x60.. 0x67 v1.2 and up Return analog value (2 bytes)
0x68 .. 0x6f v1.2 and up Return added and bitshifted analog value (2 bytes)
0x70 .. 0x77 v1.2 and up Return which i/o is coupled to which ADC channel
0x80 v1.2 and up Return number of ADC channels to read
0x81 v1.2 and up Return number of samples to add (two bytes)
0x82 v1.2 and up Return number of bits to shift accumulated sample value

Using the digital ports

Before using a port as an output, you have to set the "data direction register". This is done by writing to port 0x30. For the first output, the lowest bit has to be "1". For the second output the next bit and so on.

For boards that only have outputs, like 3fets, 7fets, relay and bigrelay, you still have to make the signals into outputs. So we recommend that you send a 0xff to register 0x30 in the initialization sequence.

Using the analog inputs

Taking measurements

The built-in ADC has 10 bits of resolution, and can be read in two different ways:

  • Just read the latest sample
  • Add x samples, and optionally bitshift the result by n bits.

The first option is easy, but prone to some noise.

The second option gives you the ability to reduce the noise and/or obtain higher resolution.

To take the average of a number of samples, set the "nsamp" (0x81) register to 2^n, and set the shift (0x82) register to n. This tells the controller to sum 2^n samples, and then divide by 2^n, resulting in the average value, which can be read from registers 0x68 .. 0x6f, depending on the channel.


For more than 10 bits of precision, it's possible to skip the bitshifting. In theory, this gives you a higher accuracy than the ADC's 10 bits. To do this, set register 0x81 to 2^n, and set register 0x82 to 0. The result can then be read from registers 0x68 .. 0x6f, depending on the channel. Do take note that statisticians have proven that you gain only n/2 of significant bits this way.

You can set nsamp (register 0x81) to 4096 (0x1000 = 2^12), and then the shift register (0x82) to 6. This will give you a 16bit results with about 16 bits of significance. The performance is such that you can get an update about every second in this mode. This is ideal for things like temperature readings.

Setting up the ADC

First decide which i/o pins you want to use as analog inputs, and which i/o pin is analog 0, 1, etc. These settings need to be written to registers 0x70 .. 0x76, 0x70 being analog0, and 0x76 being analog6. The following values are valid:

IO pin value
0 0x07
1 0x03
2 analog not available
3 0x02
4 0x01
5 analog not available
6 0x00

The measurements are done with the 5V power supply rail as the "full scale" reference voltage. You can use an internal 1.1V reference voltage by adding 0x80 to the values in the table above.

Now decide how you want to sample the analog values, and set those registers.

Finally, you need to write the amount of analog channels you want to use to register 0x80. The controller wil now start sampling those channels.

ADC Example

We want to use IO 4 and IO 0 for reading analog values, and want an average value over 16 samples. To do this, we need to send the following commands:

command explanation
0x84 0x70 0x01 Couple ADC channel 0 to IO4
0x84 0x71 0x07 Couple ADC channel 1 to IO0
0x84 0x81 0x00 0x01 Add 256 (2^8) samples
0x84 0x82 0x04 Bitshift result by 4 bits
0x84 0x80 0x02 Set number of channels to sample to 2

Now you can read from ADC channel 0 and 1:

command explanation
0x85 0x68 0xXX 0xXX read channel 0, for SPI send the bytes XX, get the data on the MISO line. For I2C, just read two bytes after sending the other bytes.
0x85 0x69 0xXX 0xXX read channel 1.

PWM Example

We want to use IO 4 and IO 0 for PWM, and then set IO 0 to 20% and IO4 to 50%:

command explanation
0x84 0x5f 0x11 bitmask of the IOs that use PWM: Add 0x01 for IO0 and 0x10 for IO4.
0x84 0x50 0x33 20% of 255 is 51 = 0x33.
0x84 0x54 0x80 50% of 255 is 128 = 0x80.

examples

For SPI in the examples below, "data sent" means the data on the MOSI line, while "data received" means the data on the MISO line. when MISO reads "xx" you should ignore the data. When MOSI reads "xx" it doesn't matter what you send.

For I2C in the examples below, you should first initiate a "write" transaction with the data in the "data sent column". Don't send the "xx" bytes. Then you initiate a "read" transaction, and you will get the data in the "data received" column (and again not the "xx" bytes).


read identification

read the identification string of the board. (SPI_DIO)

data sent data received explanation
0x85 xx select destination with address 0x84 for READ.
0x01 xx identify
xx 0x73 's'
xx 0x70 'p'
xx 0x69 'i'
xx ... etc.

read the identification string of the board. (I2C_DIO)

I2C master I2C slave (i2c_dio) explanation
START -- start I2C transaction
0x84 -- select destination with address 0x84 for write (set port).
0x01 -- identify
STOP -- terminate I2C transaction.
START -- start I2C transaction
0x85 -- select destination with address 0x84 for READ.
-- 0x69 'i'
-- 0x32 '2'
-- 0x63 'c'
-- ... etc.

Note that in the SPI example, there is bidirectional datatransfer on every cycle, but the data is "don't care" or "must ignore" (indicated by xx), while in the I2C case, the other side cannot send as there is only one data-transfer direction (indicated by "--").

turn on all outputs

data sent data recieved explanation
0x88 xx select destination with address 0x88 for WRITE
0x10 xx set outputs as in bitpattern (next byte)
0xff xx All outputs active.

turn on output 4

data sent data recieved explanation
0x88 xx select destination with address 0x88 for WRITE
0x24 xx port 0x24: output 4...
0xff xx ... active.

move stepper to step 0x1234

data sent data recieved explanation
0x88 xx select destination with address 0x88 for WRITE
0x41 xx port 0x41: set target position
0x34 xx low byte
0x12 xx high byte.


example script for rpi

This script assumes that you have "bw_tool" installed on your rpi. Actually, it will work on any Linux machine that has I2C support.

This script moves a single "on" state back and forth along the outputs. If you connect a "DIO_LEDS", you'll see the led move up and down.

#!/bin/sh
t=0.1
addr=84
dio="bw_tool -I -D /dev/i2c-1 -a $addr "
#
# Set all pins as outputs. 
$dio -W 30:ff:b
#
while true ; do
  # I'm lazy. didn't write a proper loop. Sorry. 
  $dio -W 10:01:b
  sleep $t
  $dio -W 10:02:b
  sleep $t
  $dio -W 10:04:b
  sleep $t
  $dio -W 10:08:b
  sleep $t
  $dio -W 10:10:b
  sleep $t
  $dio -W 10:20:b
  sleep $t
  $dio -W 10:40:b
  sleep $t

  $dio -W 10:20:b
  sleep $t
  $dio -W 10:10:b
  sleep $t
  $dio -W 10:08:b
  sleep $t
  $dio -W 10:04:b
  sleep $t
  $dio -W 10:02:b
  sleep $t
done