SpiDevice 类

定义

表示通过 SPI 总线连接的设备。

public ref class SpiDevice sealed : IClosable
/// [Windows.Foundation.Metadata.ContractVersion(Windows.Devices.DevicesLowLevelContract, 65536)]
/// [Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
class SpiDevice final : IClosable
[Windows.Foundation.Metadata.ContractVersion(typeof(Windows.Devices.DevicesLowLevelContract), 65536)]
[Windows.Foundation.Metadata.MarshalingBehavior(Windows.Foundation.Metadata.MarshalingType.Agile)]
public sealed class SpiDevice : System.IDisposable
Public NotInheritable Class SpiDevice
Implements IDisposable
继承
Object Platform::Object IInspectable SpiDevice
属性
实现

Windows 要求

要求 说明
设备系列
Windows IoT Extension SDK (在 10.0.10240.0 中引入)
API contract
Windows.Devices.DevicesLowLevelContract (在 v1.0 中引入)

示例

以下示例通过演示如何使用默认连接设置按友好名称打开 SPI 总线来演示此 API 和其他 SPI API 的基本功能。

// Arduino SPIDigialPot example: http://arduino.cc/en/Tutorial/SPIDigitalPot 
// using digital potentiometer AD5206 the 50kohm variance 
async void Digipot_AD5206() 
{ 
    // Get a device selector query that will select buses with SP10 
    // property set on them (we expect only 1 SP10 bus at the end) 
    var spi0Aqs = SpiDevice.GetDeviceSelector("SPI0"); 
    // Find all buses using the AQS query formed above 
    var devicesInfo = await DeviceInformation.FindAllAsync(spi0Aqs); 

    // Construct time settings beforehand which can't be changed 
    // once a SPI device is created 
    const Int32 DigipotChipSelectLine = 0; 
    var settings = new SpiConnectionSettings(DigipotChipSelectLine); 

    // Ask the SPI bus to open a device with the connection settings 
    // provided. Once we go out of scope, the device will be released 
    using (var spiDev = await SpiDevice.FromIdAsync(devicesInfo[0].Id, settings)) 
    { 

        // data[0] is the channel address 
        // data[1] is the resistance step (0 - 255) 
        // 0 is max digipot resistance, and 255 is no resistance 
        byte[] data = { 0x0, 0x0 }; 

        // Go over the 6 channels of the digipot 
        for (byte channel = 0; channel < 6; ++channel) 
        { 
            data[0] = channel; 

            // Step the resistance on this channel from max to min 
            for (byte r = 0; r <= 255; ++r) 
            { 
                data[1] = r; 
                spiDev.Write(data); 
                await Task.Delay(100); 
            } 

            // Step the resistance on this channel from min to max 
            for (byte r = 255; r >= 0; --r) 
            { 
                data[1] = r; 
                spiDev.Write(data); 
                await Task.Delay(100); 
            } 
        } 
    } 
} 

以下示例演示如何使用此 API 和其他 SPI API 通过非默认连接设置初始化 SPI 设备。 它通过读取连接到 8 通道 10 位 ADC108S102 A/D 转换器的模拟 Photocell 的光强度来实现。

// Knowing that an SPI bus with 'spiBusId' exist, and has the ADC connected 
// on 'chipSelectLine', read a digital sample from some sensor wired to 'channel' 
async Task<int> AnalogRead_ADC108S102(string spiBusId, Int32 chipSelectLine, byte channel) 
{ 
    var settings = new SpiConnectionSettings(chipSelectLine); 

    // The defaults (4MHz, 8-bit, Mode0) will not work here according 
    // to the datasheet. 
    // e.g The datasheet specifies a clock freq range (8MHz - 16MHz) 
    settings.ClockFrequency = 8000000; 
    // CPOL=1, CPHA=1 
    settings.Mode = SpiMode.Mode3; 
    // Conversion happens on a 16-bit frame 
    settings.DataBitLength = 16; 
    // The ADC108S102 has 8 input analog channels, where each can be 
    // connected to a specific analog sensor and each sensor is 
    // used by a different application independently 
    // The IO requests to the SPI bus are implicitly synchronized 
    // by the driver model, plus that the ADC configuration is per 
    // 1 sampling read 
    settings.SharingMode = SpiSharingMode.Shared; 

    // Ask the SPI bus to open a shared device with the connection settings 
    // provided. 
    using (var spiDev = await SpiDevice.FromIdAsync(spiBusId, settings)) 
    { 
        if (spiDev == null) 
            return -1; 

        // Set up control register to get a conversion on a specific 
        // channel address 
        byte[] write16bitData = { (byte)(channel << 3), 0 }; 
        byte[] read16bitData = new byte[2]; 
        // The transfer is guaranteed to be atomic according to SpbCx model 
        spiDev.TransferFullDuplex(write16bitData, read16bitData); 

        ushort result = BitConverter.ToUInt16(read16bitData, 0); 
        // Get rid of the 2 LSB zeros and mask the 10-bit sampled value 
        return (int)((result >> 2) & 0x3ff); 
    } 
} 

属性

名称 说明
ConnectionSettings

获取设备的连接设置。

DeviceId

获取与设备关联的唯一 ID。

方法

名称 说明
Close()

关闭与设备的连接。

Dispose()

执行与释放或重置非托管资源关联的应用程序定义的任务。

FromIdAsync(String, SpiConnectionSettings)

使用所提供的连接设置打开设备。

GetBusInfo(String)

检索有关特定总线的信息。

GetDeviceSelector()

获取在系统上找到的所有 SPI 总线。

GetDeviceSelector(String)

获取系统上找到与输入参数匹配的所有 SPI 总线。

Read(Byte[])

从连接的设备读取数据。

TransferFullDuplex(Byte[], Byte[])

使用全双工通信系统传输数据。 全双工允许主机和外围设备同时通信。

有关 主机外围设备的定义,请参阅 Windows.Devices.Spi 命名空间

TransferSequential(Byte[], Byte[])

按顺序将数据传输到设备。

Write(Byte[])

写入连接的设备。

适用于

另请参阅