在3.0的Micro Framework中,咱可以使用SPI(Serial Peripheral Interface)来和外围设备进行通信了。SPI是一种串口总线系统,和I2C,CAN,USB一样。
使用SPI来进行串口通信中,从设备的通讯模式如下:
两个数据口,一个进一个出,另外两个控制口。在标准SPI通信模式中,设备分为master设备和slave设备。Master设备决定clock信号和线路状态。多个slave设备之间可以串联:
上图里面,这些串联的设备看起来象是一个设备,也可以这样连接:
SPI Master的CPOL 和 CPHA分别改变0和1的状态,就可以对应到SPI Device的四个状态:
SPI-Mode |
CPOL |
CPHA |
0 |
0 |
0 |
1 |
0 |
1 |
2 |
1 |
0 |
3 |
1 |
1 |
所以在Microsoft.SPOT.Hardware.SPI的实现中,就包含了以上四种状态。
另外,外围设备有很多中,主要可以分为下面五种类型:
1. Converters (ADC and DAC)
2. Memories (EEPROM and FLASH)
3. Real Time Clocks (RTC)
4. Sensors (temperature, pressure)
5. Others (signalmixer, potentiometer, LCD controller, UART, CAN controller, USB controller, amplifier)
前三种外围设备是最多的,后面两种就少多了。那什么时候适用SPI来进行通信呢?
存储设备,主要是EEPROM的变种,也有一些少量的SPI存储设备。容量从几个bits到64Kbit不等。时钟频率可以到3MHZ。串行的EEPROMS SPI可以提供2.7V到5V的电压,同时,保存的数据可以从10到100年。每个组件,可以读写1000000次以上。
RTCs是比较适合用SPI来进行通信的。因为只有少量的数据需要传输。
OK,最后看看Microsoft的MF种实现的对SPI的支持为咱提供了什么功能:
Microsoft.SPOT.Hardware.SPI
public sealed class SPI : IDisposable
{
// Fields
private Configuration m_config;
private OutputPort m_cs;
private bool m_disposed;
// Methods
public SPI(Configuration config);
public void Dispose();
[MethodImpl(MethodImplOptions.Synchronized)]
private void Dispose(bool fDisposing);
protected override void Finalize();
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void InternalWriteRead(byte[] writeBuffer, byte[] readBuffer, int readOffset);
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void InternalWriteRead(ushort[] writeBuffer, ushort[] readBuffer, int readOffset);
public void Write(byte[] writeBuffer);
public void Write(ushort[] writeBuffer);
public void WriteRead(byte[] writeBuffer, byte[] readBuffer);
public void WriteRead(ushort[] writeBuffer, ushort[] readBuffer);
public void WriteRead(byte[] writeBuffer, byte[] readBuffer, int readOffset);
public void WriteRead(ushort[] writeBuffer, ushort[] readBuffer, int readOffset);
// Properties
public Configuration Config { get; set; }
// Nested Types
public class Configuration
{
// Fields
public readonly bool ChipSelect_ActiveState;
public readonly uint ChipSelect_HoldTime;
public readonly Cpu.Pin ChipSelect_Port;
public readonly uint ChipSelect_SetupTime;
public readonly bool Clock_Edge;
public readonly bool Clock_IdleState;
public readonly uint Clock_RateKHz;
public readonly SPI.SPI_module SPI_mod;
// Methods
public Configuration(Cpu.Pin ChipSelect_Port, bool ChipSelect_ActiveState, uint ChipSelect_SetupTime, uint ChipSelect_HoldTime, bool Clock_IdleState, bool Clock_Edge, uint Clock_RateKHz, SPI.SPI_module SPI_mod);
}
public enum SPI_module
{
SPI1,
SPI2,
SPI3,
SPI4
}
}
[email protected] Friday, December 05, 2008