High speed, low drag with the SPI (Serial Peripheral Interface) and .Net Micro Framework
Today's Hardware Friday post is from Friend of the Blog, Paolo Patierno. he shows off the the Serial Peripheral Interface and how we can use it in a .Net Micro Framework development...
Some of the past projects of his we've highlighted:
- Get your .Net Micro Framework devices up with the μPLibrary!
- Machine 2 Machine with a MQTT .Net Library
- More Machine 2 Machine with M2Mqtt. Now with SSL/TLS and async events
- Modern UI Weather Station, .Net Micro Framework powered
The SPI (Serial Peripheral Interface) bus is a synchronous communication bus typically used to transfer data between a microcontroller and an external device (eg sensor, actuator, memory, SD card, ...). Being synchronous, unlike the typical asynchronous serial communication (UART), it uses a clock signal to ensure the perfect synchronism in the transmission and reception between the two counterparts known as master and slave.
Overall, the SPI bus is characterized by the following signals :
SCLK (Serial CLocK) : clock for synchronization in the data exchange;
SS (Slave Select) : signal for enabling the slave (receiver);
MOSI (MasterOut / SlaveIn) : data line used for the transmission from master to slave;
MISO (MasterIn / SlaveOut) : data line used for the transmission from slave to master;
Excluding the SS signal, which can be handled separately, the bus should be considered a 3-wires bus.
.Net Micro Framework : the supported classes for the bus
The .Net Micro Framework, according to the logic of abstraction that characterizes it, provides the SPI class (Microsoft.SPOT.Hardware namespace, assembly Microsoft.SPOT.Hardware.dll) in order to use this type of bus with any device that supports it. To start using this feature, you must configure the SPI port to be used by the nested class SPI.Configuration, this configuration allows you to set:
ChipSelect_Port : the pin (Cpu.Pin enum) which will be used as SS (Slave Select). It is possible to set the value GPIO_NONE if you prefer to drive this pin directly without leaving the burden to the SPI class;
ChipSelect_ActiveState : the active state of the chip select. Typically, the devices have an SPI chip select "active low", it is necessary to set the logic level 0 (false) to turn on and communicate with the device;
ChipSelect_SetupTime : it is the time that must elapse from the time when the chip select is activated and the clock signal is transmitted on its line. This parameter is closely related to the device with which you are communicating (see datasheet), because it is the time it takes for the device to "see" that has been activated and that the master wants to talk to him;
ChipSelect_HoldTime : it is the time that must elapse between the end of the read/write transaction and the instant when the chip select is inactive. In practice, it serves to complete the operation for the slave then be switched off (in this case also depends on the device and is to be found in the datasheet);
Clock_IdleState : it indicates the clock idle condition on the line when the slave has not been activated; typically it is known as the the clock polarity;
Clock_Edge : it indicates the rising edge or falling edge at which the data on the communication line (MOSI or MISO) is sampled, it is typically known as the clock phase;
Clock_Rate : it is the clock frequency;
SPI_mod : it represents the SPI.SPI_module enum that indicates the physical SPI port of the CPU to use;
An important aspect to note is that the SPI Netduino shift and transmits the output data in MSB first mode, which starts the transmission from the most significant bit (Most Significant Bit). In the case that the device expects to receive the bits in the reverse order (LSB Least Significant Bit), you must reverse the order of the bits before starting transmission.
The SPI bus has the advantage of being full duplex but especially to work at very high speeds. In addition, the hardware implementation of a device that supports it is relatively simple. The main disadvantages are the need for more pins (for SS), and the absence of a hardware flow control than that of a acknoweledge from the slave (which should be implemented at the software level and higher protocol).
The .NET Micro Framework enables you to use this bus very easily with a single class and two main methods. In this way, it is possible to develop a managed driver to be able to communicate with any SPI device.