Access hardware in. Net micro framework-Part2

Source: Internet
Author: User

Related Articles

Access hardware in. Net micro framework-Part1

 

Access hardware in. Net micro framework-Part2

 

Abstract: This article introduces a simple and unique access method for hardware in. Net micro framework. It involves I2C and SPI. A concise routine illustrates how to create and access I2C and SPI devices in. Net MF
Keywords:
. Net micro framework, I2C, SPI, embedded, C #

 

I² C bus

Although the MCU uses the i² C (Inter-Integrated Circuit) bus to communicate with peripherals more slowly than many other bus systems (up to 100 Kbps), due to its low cost, it provides better support for pluggable devices. In many cases, it is still supported as a standard feature.

In. Net micro framework, how does one access devices connected in I2C mode?

. Net MF provides a class of Microsoft. Spot. Hardware. i2cdevice to access IIC devices. First, you need to create an i2cdevice instance for each IIC device. Similar to the SerialPort mentioned earlier, you need to input a configuration object of i2cdevice. configuration for its constructor. In this configuration object, you need to specify the address and communication speed (kHz) of the device ). Next, you need to create a set of transaction objects (createreadtransaction and createwritetransaction) for read and write operations. The specific transaction type naturally depends on the role of your specific component. When creating a transaction, You need to input a buffer to be read (written.

Then, you need to pass this transaction group to the execute method as an array for execution and start communication. The execute method will return when all transactions are completed or the method times out.

The following code demonstrates how to communicate with an I2C device whose IP address is 58.

 

Using system;

Using Microsoft. spot;

Using Microsoft. Spot. hardware;

Namespace i2csample

{

Public class Program

{

Public static void main ()

{

I2cdevice. Configuration Config =

New i2cdevice. configuration (

58, // address NO.

100 // kHz clock frequency

);

I2cdevice device = new i2cdevice (config );

Byte [] outbuffer = new byte [] {0xaa}; // buffer to be written

Byte [] inbuffer = new byte [4]; // buffer to be read

I2cdevice. i2cwritetransaction writetransaction =

Device. createwritetransaction (outbuffer );

I2cdevice. i2creadtransaction readtransaction =

Device. createreadtransaction (inbuffer );

// Create a transaction Array

I2cdevice. i2ctransaction [] transactions =

New i2cdevice. i2ctransaction [] {

Writetransaction,

Readtransaction

};

Int transferred = device. Execute (

Transactions,

100 // The timeout value is set to 100 ms.

);

// Transferred bytes shocould be 1 + 4 = 5

}

}

}

 

SPI bus

SPI (serial peripheral interface) connection requires three lines: Clock (sclk), input (Miso) and output (MoSi), supporting full duplex communication. For SPI devices, there is no address concept. They are connected to MCU through a separate pin. The signal on this line is called the chip selection signal (SS, CS or STE ). MCU provides clock signals for Spi devices. One (1 bit) data is sent between MCU and SPI in each clock cycle (bus clock) (after the chip selection signal arrives ).

The Microsoft. Spot. Hardware. SPI class is provided in. Net microframework to access the SPI device. For each SPI device, you first need to create an SPI instance for it. Similar to other connected devices, you also need to pass an SPI to the constructor. confuguration instance to configure it. Of course, before completing this configuration, You need to first refer to the data sheet of your SPI component.

The following code demonstrates how to use SPI in. Net MF:

 

SPI. Configuration Config = new SPI. configuration (
Chipselectpin, // PIN number for chip selection
False, // access the SPI device when the chip selection signal is low
1, // chip signal setup time (MS) before Data Transmission)
1, // Chip retention time (MS)

True, // when the device is not selected, sckl is set to a high level

False, // data is collected along the descent

15000, // The clock frequency is 15000 kHz (15 MHz)
SPI. spi_module1 // The group of SPI of the MCU used

);
SPI = new SPI (config); // use the preceding configuration to construct an SPI instance

 

After the SPI instance is created, data transmission can be implemented through its write or writeread method. the operations of slice selection are as follows. net MF maintenance, you do not need to manually read and write from gpio according to a certain amount of time. The following code snippet demonstrates how to send a 16-bit command from the MCU and read data (feedback ):

 

Ushort writebuffer = new ushort [1] = 0 xaaaa;
Ushort readbuffer = new ushort [1];
SPI. writeread (writebuffer, readbuffer );

 

SPI hardwareprovider

We can also register hardwareprovider for Spi to keep the corresponding three pins. You need to reload the getspipins virtual method of hardverprovider and pass the three CPU pins required by the corresponding SPI bus:

 

Public override void getspipins (SPI. spi_module spi_mod,
Out CPU. Pin MSK,
Out CPU. Pin miso,
Out CPU. Pin MoSi)
{
MSK = CPU. Pin. gpio_pin1; // serial clock line (sckl)

Miso = CPU. Pin. gpio_pin2; // master in/slave out line (Miso)
Mosi = CPU. Pin. gpio_pin3; // master out/slave in line (MoSi)
}

 

Reference resources:

 

  • MS. Net micro Framework Team
  • Windows Embedded blog from China
  • MVP Liu Hongfeng's blog
  • . Net MF China's resource site winbile. Net (being revised)
  • [Books] embedded programming with the. NET micro framework
  • [Books] expert. Net micro framework

     

    Enjoy it!

     

    Yellow winter

     

     

     

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.