Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
To make a driver loadable, you must add a function that will register the required driver callback routines (DriverEntry), a function that will attach the driver to a device stack (DeviceAdd), and a function that will unload the driver when it's no longer needed (DriverUnload).
The following sections will use code snippets from the sample driver that was developed for the ADXL345 accelerometer, as a test case for showing how to make your sensor driver loadable. If you haven't already done so, navigate to the ADXL345Acc sample on GitHub, so that you can follow along with the explanations.
These code snippets highlight some of the important sections of the sensor driver files. But you must review all the driver files in their entirety, to get a good understanding of how the driver works, and to be able to figure out how to customize these files for your sensor.
Register the driver - DriverEntry
Click to open the driver.cpp file, then find the code block that starts with NTSTATUS DriverEntry. The DriverEntry function registers the sensor driver by configuring and initializing the DriverObject. This function also initializes logging by using WPP tracing.
Within DriverEntry, find the following code:
WPP_INIT_TRACING(DriverObject, NULL);This code configures logging for the driver.
Find the WDF_DRIVER_CONFIG_INIT statement.
WDF_DRIVER_CONFIG_INIT(&DriverConfig, ADXL345AccDevice::OnDeviceAdd);The WDF_DRIVER_CONFIG_INIT function is called to set the DeviceAdd callback.
Find the code block that starts with NTSTATUS Status = WdfDriverCreate.
NTSTATUS Status = WdfDriverCreate(DriverObject, RegistryPath, WDF_NO_OBJECT_ATTRIBUTES, &DriverConfig, WDF_NO_HANDLE);The WdfDriverCreate function is used to initialize the driver object.
Close the driver.cpp file.
Set up the device object - DeviceAdd
Click to open the device.cpp file, then find the OnDeviceAdd function. This function attaches the driver to a device stack, and allocates a per-device context structure to the device. OnDeviceAdd is typically invoked during the second call that is made into the driver. As you develop the driver you may also include code to handle PnP, Power management, and I/O.
Find the following code:
// Create WDFOBJECT for the sensor WDF_OBJECT_ATTRIBUTES attributes; WDF_OBJECT_ATTRIBUTES_INIT_CONTEXT_TYPE(WDFDRIVER Driver, &attributes, ADXL345AccDevice);This code sets up a context structure of type ADXL345AccDevice for the device object.
Find the following code:
// Call the framework to create the device NTSTATUS Status = WdfDeviceCreate(&pAccDeviceInit, &FdoAttributes, &Device);This function is used to create a WDFDEVICE object. WDF creates the device object, and then allocates an ADXL345 accelerometer context to it.
Close the device.cpp file.
Unload the driver - DriverUnload
Click to open the driver.cpp file, and find the OnDriverUnload function. This function is used to perform cleanup after the IO manager unloads the driver from memory.
Find the following code:
WPP_CLEANUP(WdfDriverWdmGetDriverObject(Driver));This code turns off WPP diagnostic tracing.