Writing Managed Drivers

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

Create managed drivers that enable the .NET Micro Framework application to communicate with various types of hardware devices.

Managed drivers are classes that communicate with specific hardware through industry-standard connections. For example, you can write managed drivers for devices that connect to your hardware platform through General Purpose Input/Output (GPIO) pins, the serial peripheral interface (SPI), I2C, and serial ports.

Example

The purpose of a managed driver is to interpret information passing across the connection. For instance, a managed driver for a hardware button that is connected to GPIO pins might interpret pin states as button states. It would then pass the button states to your .NET Micro Framework application to notify it of events. In this way, your application can receive button-pressed notifications. This is demonstrated in the following example.

public class Button : IDisposable
{
    protected InterruptPort m_interruptPort;

    //--//

    public Button( Cpu.Pin pin, NativeEventHandler callback )        
    {
        m_interruptPort = 
            new InterruptPort( pin, 
                               true, 
                               Port.ResistorMode.PullUp, 
                               Port.InterruptMode.InterruptEdgeLow );

        if(callback != null)
        {
            m_interruptPort.OnInterrupt += callback;
        }
    }

    public Cpu.Pin Id
    {
        get
        {
            return m_interruptPort.Id;
        }
    }

    public void Dispose()
    {
        m_interruptPort.Dispose();
    }
}

The class in this example provides applications with a driver for buttons connected to GPIO pins. When the application calls the Button constructor, it must specify in the pin parameter the pin to which the button is connected. The application must also supply the callback method in the callback parameter. Any time the button is pressed, the framework automatically invokes the callback method. The application uses the callback method to react to the button pressed event.

Like the example above, drivers for other devices or devices using other types of connections must manage input or output information in ways that are appropriate for the devices and connections. For instance, if your hardware contains a device on an I2C connection, your driver would receive commands or requests for data from your application. The driver would then translate the commands or data requests into valid I2C input or output transactions and execute them. If the transaction involved a request for data, the driver would need to communicate the data back to the application.