RASP-FT232H-MPSSE-Usage-SPI

De MCHobby - Wiki
Sauter à la navigation Sauter à la recherche


MCHobby investit du temps et de l'argent dans la réalisation de traduction et/ou documentation. C'est un travail long et fastidieux réalisé dans l'esprit Open-Source... donc gratuit et librement accessible.
SI vous aimez nos traductions et documentations ALORS aidez nous à en produire plus en achetant vos produits chez MCHobby.

FT232 en mode SPI

The FT232H's MPSSE is great for generating signals to communicate using the SPI protocol. The MPSSE can take care of generating a clock signal from about 450hz to 30Mhz, and read & write bytes of data at that frequency. The Python GPIO library that was installed includes a small wrapper around MPSSE functions to simplify the use of reading and writing SPI data.

When using SPI with the FT232H the following pins will have a special meaning:

  • D0 - SCK / Clock signal : This will be the clock that tells devices when to sample and write data.
  • D1 - MOSI / Data Out : This will output data from the FT232H to the connected device.
  • D2 - MISO / Data In : This will read data from the connected device to the FT232H.

One thing to note is that there isn't an explicit chip select / enable pin. You should use any of the free GPIO pins as a dedicated chip select pin and specify that pin when creating the SPI object.

To use SPI with the Python library you need to create an instance of the Adafruit_GPIO.FT232H.SPI class. For example see the following code:

import Adafruit_GPIO.FT232H as FT232H

# Temporarily disable FTDI serial drivers.
FT232H.use_FT232H()

# Find the first FT232H device.
ft232h = FT232H.FT232H()

# Create a SPI interface from the FT232H using pin 8 (C0) as chip select.
# Use a clock speed of 3mhz, SPI mode 0, and most significant bit first.
spi = FT232H.SPI(ft232h, cs=8, max_speed_hz=3000000, mode=0, bitorder=FT232H.MSBFIRST)

# Write three bytes (0x01, 0x02, 0x03) out using the SPI protocol.
spi.write([0x01, 0x02, 0x03])

Notice that the code starts by importing the FT232H part of the GPIO library and disabling the FTDI serial drivers as your saw in the GPIO example.

Next the code creates a FT232H object also like was done in the GPIO example.

The next line of code creates a FT232H.SPI object using the FT232H device that was just created. An optional chip select/slave select line is specified using GPIO 8 / pin C0 with the cs parameter value.

Notice too the speed, mode, and bit order of the SPI protocol are specified as parameters of the initializer. Mode 0 and bit order of MSBFIRST are actually the default values and do not necessarily need to be specified here, but it's helpful to show them for clarity.

Possible mode values are 0 through 3 and they correspond to SPI mode values for AVR processors (wikipedia, anglais).

Bitorder can be either MSBFIRST for most significant bits to be clocked out first, or LSBFIRST for the least significant bits to be clocked out first.

Finally the last line shows how to send 3 bytes of data out the D1 (MOSI) line using the write() function. The D0 (SCK) line will generate a clock signal, and the D1 (MOSI) line will clock out bits of data with every clock pulse.

There are also SPI functions you can use to read and transfer (read and write at the same time) data:

# Read 3 bytes of data using the SPI protocol.
response = spi.read(3)
print 'Received {0}'.format(response)

# Write 3 bytes and simultaneously read 3 bytes using the SPI protocl.
response = spi.transfer([0x01, 0x02, 0x03])
print 'Received {0}'.format(response)

The read() function will read the specified number of bytes on the D2 (MISO) line (sending clock pulses out D0 (SCK) as necessary).

The transfer() function is like calling write() and read() at the same time. The specified array of bytes will be sent out D1 (MOSI) while at the same time data will be read from D2 (MISO).

That's all there is to using the SPI protocol with the FT232H and the Adafruit Python GPIO library!

You might also be interested in this tutorial which shows how to use the FT232H breakout with some Adafruit SPI devices that have been ported to use Adafruit's Python GPIO library.

Contrôler des NeoPixels avec SPI

One interesting use of the SPI protocol is driving the colors of WS2811/WS2812 NeoPixel addressable RGB LEDs. These LEDs don't actually use SPI to communicate, instead they have a very specific self-clocked signal for sending pixel color bits. However by using a high speed 6Mhz SPI signal we can 'oversample' the NeoPixel control signal and generate a close approximation of it from the D1 (MOSI) line of the FT232H.

Note that this method of driving NeoPixels is limited to lighting about 340 pixels at a time. This limitation comes from the maximum amount of data that can be sent to the FT232H at one time over the USB bus, about 64 kilobytes of data. Because we're oversampling the NeoPixel control signal each pixel actually takes 24*8 bytes of SPI data (or one byte of SPI data for every bit of pixel data).

To demonstrate lighting NeoPixels with the FT232H breakout you'll need the following parts:

  • Assembled FT232H breakout board.
  • NeoPixel strip, strand, matrix, etc.
    • Remember at most you can only light about 340 pixels.
  • Strong 5 volt power supply to drive the NeoPixels.
    • Each pixel can take up to 60mA, so driving more than a handful of pixels can quickly add up to a few amps or more of current. Do not try to power more than a couple NeoPixels over the FT232H 5V line!
  • Level converter chip to convert 3.3 to 5 volts OR a power diode that can handle the full power of all the NeoPixels.
    • The NeoPixel control signal needs to be at least 0.7*Vcc (power supply voltage) which is just a little too high for the 3.3 volt output of the FT232H breakout. Just like lighting NeoPixels with the Raspberry Pi (Adafruit, Anglais) you need to either convert the control signal to 5 volts using a chip like the 74AHCT125, or drop the NeoPixel power supply down slightly below 5 volts using a power diode.
  • Jumper wires and breadboard.

In this example I'm lighting a 16 pixel ring so I'll use a power diode that can handle 1 amp of current. If you're using more than 16 NeoPixels you'll want a larger power diode, or a level converter chip.

Connect the hardware as follows:

  • FT232H GND to power supply ground.
  • FT232H D1 (MOSI) to NeoPixel signal input.
  • Power supply positive voltage to diode anode (side without the stripe).
  • NeoPixel positive voltage to diode cathode (side with the stripe).
  • NeoPixel ground to power supply ground.

A picture of the hardware setup is below (note I've added a large capacitor to the power supply as recommended in the NeoPixel Uberguide):

{{{2}}}
Crédit: AdaFruit Industries www.adafruit.com


Source: Adafruit FT232H Breakout Add a serial protocol 'swiss army knife' to your computer and talk directly to devices with SPI, I2C, serial UART, GPIO's, and more!
Créé par Toni DiCola pour AdaFruit Industries.

Traduction réalisée par Meurisse D pour MCHobby.be.

Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : «  Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.

L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.

Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com