28
DEVELOPING A WINDOWS UNIFORM SERIAL BUS DRIVER FOR THE PIC MICROCONTROLLER JOHN H. DUNBAR LIB-495 LIBERAL ARTS CAPSTONE SECTION NUMBER OL011

PICDriver

Embed Size (px)

Citation preview

Developing a Windows Uniform Serial Bus Driver for the PIC Microcontroller

Developing a Windows Uniform Serial Bus Driver for the PIC Microcontroller

John H. DunbarLIB-495 Liberal Arts CapstoneSection Number OL011

What is a driver?In the most fundamental sense, a driver is a software component that lets the operating system and a device communicate with each other. (What is a Driver?, 2016, para. 1)

Development EnvironmentSoftware Requirements:Visual Studio 2015 IDEMPLABX IDEVMWare Windows 10 Virtual MachinePickKit2 SoftwareHardware:Computer with Windows 10 OSBreadboard with LED, Wires, Male end of USB cableMale/Female USB CablePIC18F45K50 MicrocontrollerCanaKit UK1300 PIC programmer

Loading firmwareThe firmware is code which holds the details of what a device is and how it interacts with the host. The firmware for Microchip Technology chips is written using a program called MPLABX. For this project, sample firmware provided by Microchip Technology can be used as the firmware for the device. Download MPLABX Libraries for applications (mla) here. Open an MPLABX project from C:\microchip\mla\v2015_08_10\apps\usb\device\vendor_basic\firmwareTake note of the Vendor ID and Product ID from the usb_descriptors.c file. These values will be used for creating the driver. Change them if you are actually release to the public and you have your own Vendor ID and Product ID.Click the build Icon and make note of the output hex file location.

Setting up the device

The firmware is written to the device using the CanaKit device programmer. (I recommend using the PIC Kit 3 because I had to tweak the software to make CanaKit compatible with the PIC18F45K50)

Insert a PIC18F45K50 into the ZIF socket and secure it.

Use the Erase button to prepare the PIC18F45K50 just in case there happens to be anything on it.

Open the PicKit2 software and import the previously compiled hex code (file-> import hex)

Then write the hex to the device using the Write button.

Electronic PrototypingUsually electronics are assembled using a circuit board. Designing and ordering a custom printed circuit board would be time consuming and costly. A simple way to test device functionality without ordering a custom board is to use a breadboard. A breadboard is an electronic prototyping device which allows electronic component leads and connecting wires to be inserted into a plastic board with rows of conductive material running underneath. This structure allows electronic prototypes to be tested before finalizing a design. The next slide shows how the PIC18F45K50 is set up on a breadboard with an LED and USB connections.

Setting things up

USB CableA breadboard does not include connections for USB devices. In this case, a breadboard friendly USB connector was made.

First, the male end was cut off an old cell phone USB cable.

Copper leads were soldered in place where the wires were connected

Custom end is paired with male to female usb cable

Result when inserted into breadboard:

Result when inserted into host:

Write the driverIn Visual Studio 2015, open a new Kernel Mode Driver, USB (KMDF) project.

Write the driverThe driver must have a method of distinguishing its device from other devices on the host. This is done using the INF file. The inf file has a device description section where the device can be defined based on its unique combination of vendor ID and product ID.

Write the driver Cont.Remove the red underlined trace code from all files. It is for debugging and it doesnt work right.Dont do anything else to Driver.cIn Queue.c, remove the following functions and their definitions/implementation :yourDriverIODeviceControl() yourDriverIOStop() These functions are empty and just causing confusion.

Write the Driver cont.In the Device.c file, an Interface with the USB device must be created in the myDriverEvtDevicePrepareHardware() function. This is done with the following lines of code:WDFUSBINTERFACE UsbInterface;

Write the Driver cont.

UsbInterface = WdfUsbTargetDeviceGetInterface(pDeviceContext->UsbDevice,0);pipe = WdfUsbInterfaceGetConfiguredPipe(UsbInterface,0,NULL);In the Device.h header, make sure to define the pipe so that it can be used by other parts of the driver for performing I/O.WDFUSBPIPE pipe;

Write the driver cont.The final step is to add some code:

VMWARETo test the driver, it is important that a safe environment is created to avoid causing unnecessary damage to your computer from any errors that may exist in the driver. Damage can occur because the driver runs in the kernel where many other vital system tasks run.VMWARE is a program which creates a simulation of an operating system. It can run within the Windows operating system which makes it perfect for testing drivers.

VMWARE driver installVMWARE has the functionality to recognize connected USB devices. Even though it is a simulated operating system running inside of another, it borrows resources and devices from the main operating system.

Upon device plugin, the VMWARE operating system recognizes a new device connected but cannot match it to a known driver.

VMWARE driver install cont.Because this driver is just for development, it is not digitally signed.To allow installation of unsigned drivers in Windows 10, press the Windows key then select restart while holding shift.Go to troubleshooting, startup settings, then press 7 to restart and allow unsigned drivers.

VMWARE driver install cont.Move the folder containing the driver to the VMWARE host. In deice manager, double click the device to open device properties. Select the driver tab and click Update driver. Navigate to the driver folder and click next. The driver will begin installing and a Warning will appear informing the user that an unsigned driver is about to be installed. Proceed with the install.

VMWARE driver install cont.Once the install is complete, device properties, show the driver provider and the date installed. When Driver Details is selected, the name of the .sys driver created from the Visual Studio project can be seen. The device now has a driver. Next, an application must be written to determine if the driver allows communication with the device.

Write the applicationAt the beginning of the application, it is necessary to define a device to connect with.This can be accomplished using the device GUID defined in the driver (line 25).

Write the applicationA connection with the device is established using the GUID and the CreateFile function listed in line 101.

An input buffer is created to store device input (line 120). The buffer is then filled with the command to toggle an LED (line 127). The rest of the input buffer is not important but should contain some value to avoid any NULL pointer errors. In this case, the rest of the buffer is filled with zeros (lines 130-132).

Once the buffer is full, it is written to the device (line 137).

Final demo

Running the application in VMWARE with the device connected gives the following results (hover over image for play controls):

implicationsThis project shows the steps to set up a device to be written to by a host. It would also be possible to add more LEDs or other outputs. Applications could be programmed to connect to the device over the internet and control outputs over long distance. It is also possible to read data from a device. This would allow for a device with buttons and knobs that could be used for multiple purposes such a mouse, keyboard, or game controller.