Sunday, July 31, 2016

Nusbio SPI

Overview


The Serial Peripheral Interface (SPI) bus is a communication protocol primarily in embedded systems.
A lot devices like an EEPROM, SD card reader, graphic screen (AKA OLED) and more support the SPI protocol.
It is generally not available for Windows easily, though it is available on the Raspberry PI via Python and C.
That is why I created Nusbio.net that offers in a simple way to talk SPI from Windows and .NET.



For more information see the

C# Class

This post explains the SPIEngine .NET class for the Nusbio device.


Wiring

Since Nusbio has 8 gpio pins, you can setup an SPI bus and control up to 5 SPI devices. I generally use the following:
  • Gpio0 - CLOCK
  • Gpio1 - MOSI
  • Gpio2 - MISO
  • Gpio3 - CS 1
  • Gpio4 - CS 2
  • Gpio5 - CS 3
  • Gpio6 - CS 4
  • Gpio7 - CS 5

Controlling more than 5 SPI devices is for now not possible due to a software limitation in the class SPIEngine, which will be fixed by the end of 2017.

Note that you could use Gpio6 and Gpio7 to setup an I2C bus and therefore control for example
  • 3 SPI devices
  • 127 I2C devices
  • Using the SPI bus you could add an analog to digital converter (ADC) like the MCP3008 and therefore also add 8 ADCs (ADC TutorialSensors Extension).




Software

For each SPI device on the bus you must create an object of the class SPIEngine. After that you call the
the method Transfer() to send or receive data. You do not need to call the method Select() and Unselect() when calling Transfer().

public SPIEngine(Nusbio nusbio, 
                 NusbioGpio selectGpio, 
                 NusbioGpio mosiGpio, 
                 NusbioGpio misoGpio, 
                 NusbioGpio clockGpio, 
                 NusbioGpio resetGpio = NusbioGpio.None);

public SPIResult Transfer(byte b);

public SPIResult Transfer(List<byte> bytes, 
                          bool select = true, 
                          bool optimizeDataLine = false);


public SPIMode Mode;

public enum SPIMode
{
    MODE_CPOL0_CPHA0 = 0,
    MODE_CPOL1_CPHA0 = 1,
    MODE_CPOL0_CPHA1 = 2,
    MODE_CPOL1_CPHA1 = 3
}

public void Select()
public void Unselect()



Samples


I2C EEPROM


Here is a basic sample reading the first 4 pages of the SPI EEPROM 25AA1024 which is a 128 k bytes EEPROM with a page size of 256 bytes (20 mHz). We are therefore transferring 1 k bytes of data from the EEPROM to the PC.



var _eeprom = new EEPROM_25AA1024(
    nusbio   : nusbio,
    clockPin : NusbioGpio.Gpio0,
    mosiPin  : NusbioGpio.Gpio1, 
    misoPin  : NusbioGpio.Gpio2,
    selectPin: NusbioGpio.Gpio3
    );

var r = _eeprom.ReadPage(0, _eeprom.PAGE_SIZE * 4);
if(r.Succeeded)
{
    var data = r.Buffer;
}

// ...
// Class 25AA1024
// ...
public override EEPROM_BUFFER ReadPage(int addr, int len = -1)
{
    if (len == -1)
        len = PAGE_SIZE;

    var eb = new EEPROM_BUFFER();
    int byteSent = 0;
    var spiBufferWrite = GetEepromApiReadBuffer(addr);
    var spiBufferRead = GetEepromApiDataBuffer(len);
    var buffer = new List<byte>();
    buffer.AddRange(spiBufferWrite);
    buffer.AddRange(spiBufferRead);

    var r = this._spi.Transfer(buffer);
    if (r.Succeeded)
    {
        eb.Succeeded = true;
        eb.Buffer = r.ReadBuffer.GetRange(spiBufferWrite.Length, r.ReadBuffer.Count - spiBufferWrite.Length).ToArray();
    }
    return eb;
}



See full source code of class EEPROM_25AAXXX_BASE.cs.


Performance Improvement

By default the SPI transfer rate using Nusbio and a Windows machine is limited around 15 Kb/s.
There are 3 ways improve performance transfer giving from 20 Kb/s to 28 Kb/s depending of what type of SPI device your are controlling

  1. If there is no MISO (Master In Slave Out) needed or in other way if we only send data from the PC to the SPI device. In the constructor pass the miso parameter as None, and when calling the method Transfer() set the paramter optimizeDataLine to true. This will increase the output speed by about 33%.
  2. To force an SPI EEPROM to send the next 64 bytes of data from an EEPROM to the PC,  EEPROMs generally require that the master send 64 0 as byte. 64 0 is equal to 64 x 8 (512) bit set to 0. There is no need to set the data line to 0, 512 times, once is enough. Based on this concept the class EEPROM_25AAXXX_BASE.cs contains the method ReadPageOptimized(), which implement the optimization. It is open source and complicated. But this increase the performance from 15 k byte/s to 28 k byte/s.
  3. This third way only works for control APA 102 RGB LED and is kind a pushy. It consist to use the same cycle to set the data and to set the clock data line high. It gives a transfer rate up to 20 Kb/S.
    See the our Github C# project on Github Extension.APA102_2_StripAdapter.

 

OLED, Data Command Pin and SPI

Some SPI OLED  driver may requires an extra pin called DataOrCommand (D/C). This is not par per say part of the SPI protocol, but it is needed, to achieve better performance the SPIEngine class offers the following methods. The methods allow to package buffers of type Data and buffers of type Command and then send the buffers as SPI buffers with limited USB operations.

public void TransferNoMiso(bool reverse, List buffer);
public void TransferNoMiso(byte dataOrCommandBit, bool reverse, List PackagedBuffers);
public void TransferNoMiso(int dataOrCommand, byte dataOrCommandBit, bool reverse, List buffer);

See our folder Oled on Github for more information. We do support the SH1106 and SSD1306 OLED driver.




APA 102 RGB LED and SPI

The RGB LED (multi color LED) of type APA 102 use the SPI protocol and are supported by Nusbio.
See our Tutorial page.
See the our Github C# project on Github Extension.APA102_2_StripAdapter.



SPI Modes


The 4 spi modes have been implemented, but only the first mode has been tested.

  • MODE_CPOL0_CPHA0
  • MODE_CPOL1_CPHA0
  • MODE_CPOL0_CPHA1
  • MODE_CPOL1_CPHA1

Saturday, July 30, 2016

Nusbio I2C

Overview

The Inter-Integrated Circuit (I²C, i2c) is a multi-master, multi-slave serial communication protocol invented by Philips Semiconductor (now NXP Semiconductors), primarily used in embedded systems.
A lot devices like an EEPROM, LCD, GPIO expander, LED drivers and more support the I2c protocol.

It is generally not available for Windows easily, though it is available on the Raspberry PI via Python and C.

That is why I created Nusbio.net that offers in a simple way to talk I2C from Windows and .NET

A lot of devices designed by Adafruit use the I2C protocol and are compatibles with Nusbio and any .NET languages.

C# Class


This post explains the I2CEngine .NET class for the Nusbio device.

Wiring

Since Nusbio has 8 gpio pins, you can technically setup 4 independent I2C buses. But generally one is enough.
I use Gpio0 for SCL and Gpio1 for SDA as convention. Obviously you can connect multiple I2C devices to the bus.
Nusbio does not come with any pull up resistors so it is up you to take care of it.
Generally if you use an I2C breakout it is part of the breakout.

Software

For each I2C device on the bus you must create an object of the class I2CEngine. After that you call the WriteBuffer() method to initiate an I2C write operation and the method ReadBuffer for a I2C read operation.
The methods takes care of the I2C Control byte. You do not have to pass it as part of the buffer.
The methods return true if the operation succeeded. For the Readxxxx methods, if the operation succeeded you can then read the buffer which will be updated with the data.


public I2CEngine(Nusbio nusbio, NusbioGpio sdaOutPin, NusbioGpio sclPin, byte deviceId);
public bool ReadBuffer(int len, byte[] data);
public bool WriteBuffer(byte[] buffer);
public bool WriteBuffer(byte address8bit, byte[] buffer);


Samples


I2C EEPROM


Here is a basic sample reading the first 64 bytes of the I2C EEPROM 24LC256 which is a 32k bytes EEPROM with a page size of 64 byte.

byte EEPROM1_WR = 80; // 0xA0;
                                
var i2c = new I2CEngine(nusbio, NusbioGpio.Gpio1, NusbioGpio.Gpio0, EEPROM1_WR);
var addr = 0;
var buffer = new byte[64];
if (i2c.WriteBuffer(new byte[2] { (byte)(addr >> 8), (byte)(addr & 0xFF) }))
{
  var r = i2c.ReadBuffer(64, buffer);
}

We do offer specific classes to handle I2C and SPI EEPROM, see our folder EEPROM on github.


I2C MCP9808 Temperature Sensor

Here part of our class MCP9808 _Temperature Sensor.cs available on Github.
The method Begin will return true if the device is detected on the bus else false.

public bool Begin(byte deviceAddress = MCP9808_I2CADDR_DEFAULT)
{
    try
    {
        this._i2c.DeviceId = deviceAddress;
        if (read16(MCP9808_REG_MANUF_ID) != MCP9808_REG_MANUF_ID_ANSWER) return false;
        if (read16(MCP9808_REG_DEVICE_ID) != MCP9808_REG_DEVICE_ID_ANSWER) return false;
        return true;
    }
    catch (System.Exception ex)
    {
        System.Diagnostics.Trace.WriteLine(ex.ToString());
        return false;
    }
}

public double GetTemperature(TemperatureType type = TemperatureType.Celsius)
{
    uint16_t t = read16(MCP9808_REG_AMBIENT_TEMP);
    double temp = t & 0x0FFF;
    temp /= 16.0;
    if ((t & 0x1000) == 0x1000) temp -= 256;
    switch (type)
    {
        case TemperatureType.Celsius: return temp;
        case TemperatureType.Fahrenheit: return CelsiusToFahrenheit(temp);
        case TemperatureType.Kelvin: return temp*CELCIUS_TO_KELVIN;
        default:
            throw new ArgumentException();
    }
}

private UInt16 read16(uint8_t reg)
{
    UInt16 value = 0;

    if (this._i2c.WriteBuffer(new byte[1] { reg }))
    {
        var buffer = new byte[2];
        this._i2c.ReadBuffer(2, buffer);
        value = (System.UInt16)((buffer[0] << 8) + buffer[1]);
    }
    else throw new ArgumentException();    
}


We do offer specific classes to handle I2C and SPI EEPROM, see our folder EEPROM on github.


Advanced Methods

To improve transfer performance, the class I2CEngine expose the following methods which optimize the number of USB operations to execute the I2C operations (we combine an I2C Read + I2C Write operation in one USB transaction)

public bool Send16BitAddressAnd1Byte(int address16bit, byte b);
public bool Send16BitsAddressAndBuffer(int address16bit, int len, byte[] buffer);
public int  Send16BitsAddressAndRead1Byte(short address8bit);
public bool Send16BitsAddressAndReadBuffer(int address16bit, int len, byte[] data);

public bool Send1ByteCommand(byte command);
public bool Send2BytesCommand(byte command0, byte command1);
public bool Send3BytesCommand(byte command0, byte command1, byte command2);

public int Send1ByteRead1Byte(byte address8bits);
public _2BytesOrInt16Result Send1ByteRead2Bytes(byte cmd);        



Performance

Using the EEPROM 24LC256 which is an I2C 32k bytes with a max clock of 400 kHz, we can transfer the 32 k byte of data from the EEPROM to the PC at the rate of 15 k bytes per second in batch mode of 64 pages at a time.

Transferring one page a the time give a transfer rate of 8 k bytes per second.

Output or input batch mode is necessary to increase performance for all I2C devices.

Transfer speed may vary also depending on the speed of the computer.

 

Saturday, July 16, 2016

Faster serial port communication from .NET to Arduino

Overview

I was for a long time thinking that serial port communication on Windows and/or .NET was limited to 115 200 bauds, which translate to 11Kb/s at the most ( 115200 / 10 (8 bit-data + 2 (1-start-bit 1-stop-bit )) ).

In reality it is possible to use faster transfer rate, but there are a few things to know and it can be a little bit convoluted.

I am currently working on my "device" NusbioMCU, which use an Arduino Nano compatible (ATmega328),
so I will focus in this post on communication from Windows+.NET to different Arduino and compatibles.
But this can be applied to any serial communication from Windows and .NET.

This post should be taken with some grains of salt, because I am not an expert. I am just a guy that would like to do faster serial communication from Windows and .NET. This post is the fruit of my research and experimentation.


What do you need to know?

  • Hardware: You must know what UART chip is used in your MCU board. Different chip will support different baud rates, and the values may not be as expected.

    Popular UART chip:

    The Windows Device Manager program can help you figure this out. Below is the list of baud rate supported by the FT231X UART.

As shown in the image above, the FT231X UART which comes with the Adafruit Metro (a compatible Arduino UNO) support the 460 800 and  921 600 baud rates (Same for the FT232RL).


The Atmega16U2 UART which comes with a real Arduino UNO R3, will only display up to 128 000,
but multiplying this value by 2, 4 or 8 works.


Regarding the CH340, Chinese Arduino UNO or Nano compatibles comes with a CH340 UART which is limited to 115 200 bauds. That said based on information from RyanteckLTD and their device the RTK.Gpio, it could be possible that if the CH340 has its own 12 Mhz clock, it then can support faster transfer rates.

  • USB 2.0 Full Speed. The FTDI FT232RL or FT231X UART chips implement serial communication over USB 2.0 Full Speed. In USB 2.0 Full Speed there is a 1 ms latency to access the USB and transfer data. So if we transfer data in a 64 byte or less buffer, we cannot expect a transfer rate greater then 64 x 1000 == 53K b/s. 
    There are 2 transfer phases:
    1. From the PC to the UART at USB 2.0 Full Speed, dependent on the data buffer size
    2. From the UART to the MCU based on the baud rate. UART generally have an output buffer that allow to receive byte faster that they can sent according to the baud rate, but they are limted to 128 or 512 bytes
For example if we transfer data in buffer of 256 bytes at the USB level we can reach 212 Kb/s (256 * 1000 / 1024), that said since the max baud rate is 921 600 or 90 Kb/s I think the FTDI driver will stop the communication until the UART buffer is empty.
So for now I am only focusing on reaching 64 Kb/s.

  • Sofware:
    • The SerialPort class available in .NET does not support transfer speed greater than 115 200 bauds. The Windows low level API does support it, but the .NET implementation does not.
      See blog post If you *must* use .NET System.IO.Ports.SerialPort.
      Though is his blog post Ben Voigt gave some code small snippet that could work.
    • I did not find another open source and/or free library on the internet
    • With the Windows Serial Communication Component Library from Marshallsoft, I was able to achieve greater transfer speed.

 

Transfer Test

To test the transfer speed I am sending from the PC to the MCU 640 times, 4 buffers containing 33 bytes of data (82.5 Kb). Each buffer is processed by the MCU and the data is sent to a 32x8 LED matrix driven by 4 MAX7219 chips chained using the SPI protocol at 10Mhz (about 1 Mb/s).

There is no acknowledgment from MCU to the PC. I am visually testing the result for now, when there are issues the expected images is not display correctly on the matrix. When there is no issue I see a slight flickering of the expected images (see picture below).

The processing time by the MCU of one buffer takes less than 1 ms, and since there is a know 1 ms overhead when using USB 2.0 full speed, the PC is always working on sending the next buffer while the MCU is processing the current one. That said this will be an issue when the MCU take more than 1 ms to process the data. I did verified that the processing by the MCU does not affect the transfer in my case.

With that configuration I reached 33 Kb/s at 500 000 bauds. At  1 000 000 bauds no improvement.

Realizing that to get more transfer speed I needed to send more data I changed buffer size to 41 bytes of data (for total of 102.5 Kb) and reached 41Kb/s.

RTS/CTS
According to the schematic of the Arduino Uno R2 or Adafruit Arduino Metro, it looks like the RST/CTS pin are not wired to the MCU. So I guess there is no hardware control flow.

Summary

Here are my results using the Marshallsoft WSC library called from a C# program.

MCU UART Max Baud Rate Expected Kb/S Measured Kb/S
Arduino UNO R3 Atmega16U2 512 000 50 Kb/s 41 Kb/s
Adafruit Arduino Metro FT231X 500 000 48 Kb/s 41 Kb/s
Adafruit Trinket Pro + FTDI Cable FT232RL 500 000 48 Kb/s 41 Kb/s
Arduino Nano Compatible Chinese CH340 115 200 11 Kb/s 11 Kb/s

Note that the baud rates for the Atmega16U2 and FT231X are different, this is not a typo.

I do see a transfer speed increase from 115200 to 230400 and from 230400  to 460800.
But at 460800 or 500000 I do measure a transfer rate lower than expected.
And at 1 024 000 baud there is no improvement so far, with the FT231X, FT232RL and the Atmega16U2.

I think the issue here is that I need be able to send bigger buffer, but for that the Arduino need to be able to handle it, up to 41 Kb/s it is ok, after that it does not work (yet). The Arduino serial library only use a 64 bytes buffer and I think this is my problem. I would need to increase this buffer to 128 bytes and and send 64 bytes buffer to achieve 64 Kb/s. Well at least that is my expectation.


To-Do

  • Optimize processing and serial communication on the Arduino side to support buffer of 64 bytes
  • Test CH340 with its own 12 Mhz clock 
  • Test Cypress CP2130
To be continued.

Contact



To contact me see my web site MadeInTheUSB.net.










Friday, July 1, 2016

Controlling one 110 volts appliance safely with .NET (C#, VB.NET, F# or PowerShell)


Overview

Turning on and off an 110 volts appliance from a computer like a Raspberry PI/Python or a PC with an our device Nusbio.net using any .NET language, is not per say difficult.

You need one GPIO (General Purpose Input/Output) connected to what is called a Relay. A Relay looks like that.
The issue is that you must make the wiring yourself and this can be slightly dangerous. 

Solution

The company Digital-loggers introduced recently the IOTRelay. The IOTRealy takes care of every thing, it is safe, solid, easy to connect to and affordable $20.
  • On the side connect the cable to one of your outlet.
  • On the top you have 4 outlets
    • 2 are labelled normally ON, this is the one you will use to plug into your lamp,
      fan or toaster. Though we have 2 outlets, they are not independent.
    • 2 are labelled normally OFF, and turn the appliances when the IOTRelay is programmed to be off. I am not sure that this will be very useful.

 

Video

 

The wiring is simple:
  • Connect any of the 8 Nusbio's GPIO to the positive of the IOT Relay. 
  • Connect the negative of the IOT Relay to Nusbio's Ground.


Nusbio board + IOTRelay $36


Programming


From a software point of view all you have to do is turn the GPIO on or off to power your appliances.
Nusbio is compatible with C#, VB.NET, F# and PowerShell.

  nusbio[0].High(); // Turn appliance on
  nusbio[0].Low();  // Turn appliance off