/// <summary> /// This function returns a handle for the SPI device on the channel. /// Data will be transferred at baud bits per second. The flags may /// be used to modify the default behaviour of 4-wire operation, mode 0, /// active low chip select. /// /// An auxiliary SPI device is available on all models but the /// A and B and may be selected by setting the A bit in the flags. /// The auxiliary device has 3 chip selects and a selectable word /// size in bits. /// /// spiFlags consists of the least significant 22 bits. /// /// mm defines the SPI mode. /// /// Warning: modes 1 and 3 do not appear to work on the auxiliary device. /// /// px is 0 if CEx is active low (default) and 1 for active high. /// /// ux is 0 if the CEx GPIO is reserved for SPI (default) and 1 otherwise. /// /// A is 0 for the standard SPI device, 1 for the auxiliary SPI. /// /// W is 0 if the device is not 3-wire, 1 if the device is 3-wire. Standard /// SPI device only. /// /// nnnn defines the number of bytes (0-15) to write before switching /// the MOSI line to MISO to read data. This field is ignored /// if W is not set. Standard SPI device only. /// /// T is 1 if the least significant bit is transmitted on MOSI first, the /// default (0) shifts the most significant bit out first. Auxiliary SPI /// device only. /// /// R is 1 if the least significant bit is received on MISO first, the /// default (0) receives the most significant bit first. Auxiliary SPI /// device only. /// /// bbbbbb defines the word size in bits (0-32). The default (0) /// sets 8 bits per word. Auxiliary SPI device only. /// /// The <see cref="SpiRead"/>, <see cref="SpiWrite"/>, and <see cref="SpiXfer"/> functions /// transfer data packed into 1, 2, or 4 bytes according to /// the word size in bits. /// /// For bits 1-8 there will be one byte per word. /// For bits 9-16 there will be two bytes per word. /// For bits 17-32 there will be four bytes per word. /// /// Multi-byte transfers are made in least significant byte first order. /// /// E.g. to transfer 32 11-bit words buf should contain 64 bytes /// and count should be 64. /// /// E.g. to transfer the 14 bit value 0x1ABC send the bytes 0xBC followed /// by 0x1A. /// /// The other bits in flags should be set to zero. /// </summary> /// <remarks> /// 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 /// b b b b b b R T n n n n W A u2 u1 u0 p2 p1 p0 m m /// Mode POL PHA /// 0 0 0 /// 1 0 1 /// 2 1 0 /// 3 1 1 /// </remarks> /// <param name="spiChannel">0-1 (0-2 for the auxiliary SPI device)</param> /// <param name="baudRate">32K-125M (values above 30M are unlikely to work)</param> /// <param name="spiFlags">see below</param> /// <returns>Returns a handle (>=0) if OK, otherwise PI_BAD_SPI_CHANNEL, PI_BAD_SPI_SPEED, PI_BAD_FLAGS, PI_NO_AUX_SPI, or PI_SPI_OPEN_FAILED.</returns> public static UIntPtr SpiOpen(SpiChannelId spiChannel, int baudRate, SpiFlags spiFlags) { var result = BoardException.ValidateResult(SpiOpenUnmanaged(spiChannel, Convert.ToUInt32(baudRate), spiFlags)); return(new UIntPtr(Convert.ToUInt32(result))); }
/// <summary> /// Opens the given SPI channel. /// </summary> /// <param name="channel">The channel.</param> /// <param name="baudRate">The baud rate.</param> /// <param name="flags">The flags.</param> /// <returns>The peripheral service</returns> public SpiChannel OpenSpiChannel(SpiChannelId channel, int baudRate, SpiFlags flags) => new SpiChannel(channel, baudRate, flags);
private static extern int SpiOpenUnmanaged(SpiChannelId spiChannel, uint baudRate, SpiFlags spiFlags);
/// <summary> /// Initializes a new instance of the <see cref="SpiChannel"/> class. /// </summary> /// <param name="channel">The channel.</param> /// <param name="baudRate">The baud rate.</param> /// <param name="flags">The flags.</param> internal SpiChannel(SpiChannelId channel, int baudRate, SpiFlags flags) { BaudRate = baudRate; Channel = channel; Handle = Spi.SpiOpen(channel, baudRate, flags); }