DIO protocol

From BitWizard Wiki
Jump to navigation Jump to search

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 .. 0x26 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. (Read: Using the digital ports)
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 .. 0x56 v1.1 and up X Set PWM value. 0x50: output 0, 0x51 output 1 etc. (Read: Using the digital ports)
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 .. 0x76 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. It is 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 .. 0x26 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 read PWM on output 0
0x60.. 0x66 v1.2 and up Return analog value (2 bytes)
0x68 .. 0x6f v1.2 and up Return added and bitshifted analog value (2 bytes)
0x70 .. 0x76 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. So value 0x01 defines only "output 0" as an output, the other as inputs. The value 0xff will make all pins outputs. Register 0x5F (PWM mask) is also a bitmask and works in the same way. For "dio" the pins will all default to inputs, to prevent bus contention if another source is driving the pin.

For boards that only have outputs, like 3fets, 7fets, relay and bigrelay, the firmware will now default all the pins to outputs. However some early versions didn't have that yet. So we recommend that you still explicitly make the signals into outputs in your code. So we recommend that you send a 0xff to register 0x30 in the initialization sequence.

BitMask value for every pin

Pin Function Value
3 IO0 01
4 IO1 02
5 IO2 04
6 IO3 08
8 IO4 10
9 IO5 20
10 IO6 40

inputs and pullups

When a bit is defined as "input", you can use the data register (0x10) to configure the pullup on the pin. So if you write a 0 to a bit there, the corresponding pin will float, and can very easily be driven high or low. (max load by the chip: 1 micro amp). If you write a 1 there, the chip will drive a weak pullup of about 50k. This is useful to connect a switch for example. The switch goes from the pin to ground, and you activate the pullup to define the pin as "high" when the switch is not activated.

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 (as my DIO is currently mounted vertically.... Most people might lay the board down on their desk, so that would become left-and-right.. :-) ).

#!/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

Arduino example sketch: I2C

Again, a single "on" state moves back and forth across the 7 outputs. I have a DIO_LEDS connected and see the led move back and forth.

This sketch uses "Wire" or I2C. See below for an SPI example for arduino.

#include <Wire.h>

#define ADDR (0x84/2)

void bw_write_reg (unsigned char addr, unsigned char  reg, unsigned char val)
{
  Wire.beginTransmission(addr); // transmit to device #4
  Wire.write(reg);              // sends five bytes
  Wire.write(val);              // sends one byte  
  Wire.endTransmission();       // stop transmitting
} 


void setup()
{
  Wire.begin(); // join i2c bus (address optional for master)
  bw_write_reg (ADDR, 0x30, 0xff); // set all pins as outputs.   
  //bw_write_reg (ADDR, 0x30, 0x00); // Just use the pullups: My leds are too bright.  
}


#define LED 13

void loop()
{
  static int t;
  
  if (t < 7) bw_write_reg (ADDR, 0x10, 1 << t);
  else       bw_write_reg (ADDR, 0x10, 0x40 >> (t-7));  

  if (t++ == 12) t = 0;
 
  digitalWrite (LED, t&1);

  delay(100);
}

Arduino example sketch: SPI

Again, a single "on" state moves back and forth across the 7 outputs. I have a DIO_LEDS connected and see the led move back and forth.

This sketch uses SPI. This sketch runs like this on an STM32F103 board. Thus, on a normal AVR based ARDUINO you'll have to set "SLAVESELECT" to "13" (IIRC). As the LED is also on digital pin 13, the flashing led on the board is not possible on a standard arduino. Remove the two lines in the "loop" function if the led clashes with the slave select.


#include "SPI.h"

#define DELAY 10
#define ADDR (0x84)
#define SLAVESELECT 18 

void bw_write_reg (unsigned char addr, unsigned char  reg, unsigned char val)
{
  digitalWrite(SLAVESELECT, LOW);

  delayMicroseconds (DELAY);
  SPI.transfer(addr); //Send register location
  delayMicroseconds (DELAY);
  SPI.transfer(reg);  //Send value to record into register
  delayMicroseconds (DELAY);
  SPI.transfer(val);  //Send value to record into register
  delayMicroseconds (DELAY);

  digitalWrite(SLAVESELECT, HIGH);
}

void setup() {
  // put your setup code here, to run once:
  SPI.begin ();
  SPI.setClockDivider(SPI_CLOCK_DIV64);
  pinMode (SLAVESELECT, OUTPUT);
   
  bw_write_reg (ADDR, 0x30, 0xff); // set all pins as outputs.   
  bw_write_reg (ADDR, 0x30, 0x00); // Just use the pullups: My leds are too bright.  
  pinMode (LED, OUTPUT);
}

#define LED 25

void loop() {
  // put your main code here, to run repeatedly:
  static int t; 
  
  if (t < 7) bw_write_reg (ADDR, 0x10, 1 << t);
  else       bw_write_reg (ADDR, 0x10, 0x40 >> (t-7));  

  if (t++ == 12) t = 0;

  digitalWrite (LED, t&1);
  
  delay(100);
}