Showing posts with label Device Drivers. Show all posts
Showing posts with label Device Drivers. Show all posts

Tuesday, November 2, 2010

Automaticaly Add files to an SDK - refreshed

When building an SDK using Platform Builder 6.0 (and previous), you may need to include your own files. Those files are the list of IOcontrols of drivers you implemented, or header files required to link to a new library that you want to provide in your SDK. In that case you have to include those files to your SDKs.
The first one is a manual inclusion of those files using the configuration wizard of the SDK. That’s mean that every time you generates the SDK you have to take care of those files.
The second solution is to automated this mechanism by copying those files to a specific folder that will be used automatically by the wizard during the generation of the SDK.


Identification of the sdk folder
The SDK tool uses the content of the cesysgen folder (located in your OSDesign folder), and the sdk folder located in the same folder as the cesysgen and OAK folder.$(_PROJECTROOT)\sdk or $(_PROJECTSDKROOT)
The SDK tool adds the content of the sdk\Inc and sdk\Lib folder to the SDK msi file. So you must store the files you need to provide with the SDK in those sub folders. Doing it manually is never the solution, so by asking the Windows embedded CE build environment to do it, it is more reliable. This could be done during the build of those libraries or drivers.


Add an automated task
Using the sources files you can add dependencies and enable automated task for the build engine. You could also use the postlink macro, but this is not the purpose of this article.
In the sources file add the following lines :


#------------------------------------------------------------------------------
# SDK files
#------------------------------------------------------------------------------
# Enable copy to the SDK directory
WINCETARGETFILES=$(WINCETARGETFILES) CopyFilesToSDK

# Hearder files
SDK_H_FILES=gpio_iocontrol.h driverheadertoshare.h

#Lib files
SDK_LIB_FILES=


Any target files that you specify using WINCETARGETFILES are added to the list of dependencies that must be satisfied to generate a successful build. Also by listing the files you need for your SDK in the SDK_ FILES variable, you will generate a list actions that will be solved later in the makefile.inc.


Add an automated copy instructions
In association to the SOURCES file you must provide a makefile.inc (located in the same folder), this file is proceeded after your sources files during the build. In this file you must satisfy the dependency list, so it must contain information about the CopyFilesToSDK.
Makefile.inc content will be the following :

#
# DO NOT EDIT THIS FILE!!! Edit .\sources. if you want to include new files
# in the SDK.
#

CopyFilesToSDK:
if not EXIST $(_PROJECTSDKROOT)\Inc mkdir $(_PROJECTSDKROOT)\Inc
FOR %%f IN ($(SDK_H_FILES)) DO xcopy /I /D /Q %%f $(_PROJECTSDKROOT)\Inc > nul

if not EXIST $(_PROJECTSDKROOT)\Lib mkdir $(_PROJECTSDKROOT)\Lib\$(_CPUINDPATH)
FOR %%f IN ($(SDK_LIB_FILES)) DO xcopy /I /D /Q $(_RELEASELIBDIR)\%%f $(_PROJECTSDKROOT)\Lib\$(_CPUINDPATH)\ > nul

The copy will be done in batch script language and executed by the build engine.
After the build of the driver or the library, you may find all your required files in the $(_PROJECTSDKROOT) folder.
In this article I described only one list of files to be proceeded, but it make sense to have two lists, one for the header files and the sdk\Inc folder and another one for the sdk\Lib folder for the libraries.

- Nicolas

Monday, October 11, 2010

Develop a driver easily...

CEDriverWiz is a Windows Embedded CE wizard that works within the Visual Studio IDE, helps simplify the tasks needed to setup the initial subproject workspace to develop stream interface device driver for Windows CE 6.0.


Check out all the details about this nice project on CodePlex.

- Nicolas

Tuesday, April 6, 2010

Enable RamDisk, and Mass Storage under Windows CE [Updates]

A Ram Disk is a storage disk located in RAM of your device, meaning its content isn't persistent between system boot. The RamDisk usually used for storing temporary content like software updates source binaries.

Reserve Memory space on target :

The Ram disk will be located at a customizable memory address and size on the device, and will be mounted on your root file system as any other file system. In order to reserve the physical memory area, you have to modify the config.bib file of your BSP and add a reserved section for RAM Disk as shown as below. It make sense to map this area on a physical portion of the RAM.

MEMORY


; Name Start Size Type
; ------- -------- -------- ----
BLDR 80000000 00058000 RESERVED
DRVGLOB 80058000 00001000 RESERVED
EMACBUF 80059000 0000E000 RESERVED
NK 80067000 01000000 RAMIMAGE
RAM 81067000 02BA7000 RAM ; 43.65 MB
RAMDISK 83C0E000 00300000 RESERVED ; 3.00 MB


In the example above, I reduced the size of RAM to have enough room for the RAMDISK section of 0x300000 (3MBytes).

Include RamDisk into OSDesign
Add the RamDisk support to your runtime image, by enabling the SYSGEN_RAMDISK variable into the OSDesign property window.

Configure RamDisk
The RamDisk driver is by default mounted by the storage manager and no configuration of the storage driver name allowed. So I prefer to make few registry modifications in my platform.reg to be able to fix the driver DSK name and index.

; HIVE BOOT SECTION

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\RamDisk]
"Dll"="ramdisk.dll"
"Prefix"="DSK"
"FriendlyName"="RAM Disk Driver"
"Order"=dword:0
"Ioctl"=dword:4
"IClass"=multi_sz:"{A4E7EDDA-E575-4252-9D6B-4195D48BB865}"
"Profile"="RAMDisk"
"Size"=dword:300000
"Address"=dword:83C0E000
"SectorSize"=dword:200
"index"=dword:9

[HKEY_LOCAL_MACHINE\System\StorageManager\AutoLoad\RAMDisk]
"DriverPath"="Drivers\\BuiltIn\\RamDisk"
"LoadFlags"=dword:1
"BootPhase"=dword:0

; END HIVE BOOT SECTION

Note that the Address and Size of the RamDisk driver definition match the values defined in the config.bib. The storage driver will then be accessible through DSK9:, and you can then easily configure a USB Function mass storage profile to point on this storage.

Configure USB Function Mass Storage
The USB Function driver loads profiles when device plugged to a host, the profile to be loaded by default is set through the registry.

[HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers]

"DefaultClientDriver"=- ; erase previous default
[HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers]
"DefaultClientDriver"="Mass_Storage_Class"

[HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\Mass_Storage_Class]
"DeviceName"=- ; erase previous default
[HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers\Mass_Storage_Class]
"DeviceName"="DSK9:"


Note as the "DeviceName" is set to DSK9: matching the driver index described before for the RamDisk.
Note don't forget to add the USB Function driver of the BSP and set the following variables : SYSGEN_USBFN, SYSGEN_USBFN_STORAGE

To go further :
- RamDisk can be used in association to the ROM Only mechanism to reduce write access to the physical storage.
- RamDisk and USB Function, this to provide a way to drop files on your target using USB mass storage profile.

Special thanks to Juanito for the hints

[Updated on 08-22-2011] : fixed a platform configuration file name.

- Nicolas

Monday, April 28, 2008

Accessing native API from C#

The .Net Compact Framework, in his CLR (Common Language Runtime), contains a fixed set of API, and when working on an embedded device running Windows Embedded CE, most of the time, the managed code developers require access to some native APIs like custom DLL provided by a third party company. In this case the required APIs cannot be directly accessed in C# by the application and developer have to use the P/Invoke mechanism for the marshalling of the data from the managed to the native environment.

[DllImport ("coredll.dll")]
private static extern int CreateFile(
string lpFileName, int dwDesiredAccess,
int dwShareMode, int lpSecurityAttributes,
int dwCreationDisposition, int dwFlagsAndAttributes,
int hTemplateFile);


The DllImport macro is used to specify to the CLR that the next function declared is located inside a native dll, coredll.dll for instance. The tricky operation when defining Marshalling operation is the way to define the native types into managed types. Because if the wrong type definition is used, the CLR is going to Marshall the data in a way that is not expected by the native functions, and can be the reason of a crash or memory leaks on the system (null pointer, bad type conversion, …).

Accessing Device Drivers from C#
You have the same issue when trying to access device drivers from a C# application as the .Net Compact Framework do not contain any classes that allow developer to load a open driver and access it. Under Windows Embedded CE, device drivers are managed by the device manager, and in any case you do not access a driver directly through its Dll. You always have to use file system API to do so. For this reason, the CreateFile, CloseHandle, WriteFile, ReadFile, SetFilePojnter and DeviceIoControl functions should be redefined in C# to specify the Marshalling of the various functions’ parameters.

[DllImport ("coredll.dll")]
private static extern int CreateFile(
string lpFileName,
int dwDesiredAccess,
int dwShareMode,
int lpSecurityAttributes,
int dwCreationDisposition,
int dwFlagsAndAttributes,
int hTemplateFile);

[DllImport ("coredll.dll")]
private static extern int CloseHandle(int hObject);

[DllImport ("coredll.dll")]
private static extern int ReadFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToRead,
ref int lpNumberOfBytesRead,
ref OVERLAPPED lpOverlapped);

[DllImport ("coredll.dll")]
private static extern int WriteFile(
int hFile,
byte[] lpBuffer,
int nNumberOfBytesToWrite,
ref int lpNumberOfBytesWritten,
ref OVERLAPPED lpOverlapped);

[DllImport ("coredll.dll")]
private static extern int SetFilePointer(
int hFile,
int nDistanceToMove,
ref int lpDistanceToMoveHigh,
uint nMoveMethod);


[DllImport ("coredll.dll")]
private static extern int DeviceIoControl(
int hFile,
uint dwIoControlCode,
byte[] lpInBuffer,
uint nInBufferSize,
byte[] lpOutBuffer,
uint nOutBufferSize,
ref uint lpBytesReturned,
ref OVERLAPPED lpOverlapped);


The OVERLAPPED parameter, used by ReadFile, WriteFile and DeviceIoControl, should also be re-defined as this type is unknown in C#. In native code this object is a structure of basic types and will be defined like this in C#:
private class OVERLAPPED
{
public int Internal; // Reserved for operating system use.
public int InternalHigh; // Reserved for operating system use.
public int Offset; // Specifies a file position at which to start the transfer.
public int OffsetHigh; // Specifies the high word of the byte offset at which to start the transfer.
public int hEvent; // Handle to an event set to the signaled state when the operation has been completed.
}


To have a nice way to use those different native functions, the DllImport definition should be wrapped inside a standard C# class and public function with managed types parameters can be provided to the developer.
For example the Open function published by a device driver wrapper class could be write like this :

int m_intHandle = INVALID_HANDLE_VALUE;

public bool Open(String strDriverName)
{
m_intHandle = CreateFile(strDriverName, GENERIC_READ | GENERIC_WRITE, 0, 0, OPEN_EXISTING, 0, 0);
return m_intHandle != INVALID_HANDLE_VALUE;
}

Avoid Memory Leaks
Unlike managed code when allocating memory in native, do not forget to explicitly destroy those objects to avoid memory leaks. Even if you are accessing the native code from a managed application, the Garbage Collector is not able to detect those memory objects and freed them for you.


- Nicolas

Friday, April 4, 2008

Windows Embedded CE Webcast

Few weeks ago I did a live web cast session for the device drivers development under Windows Embedded CE 6.0 R2. All the webcast sessions recorded at this time are available now from the Microsoft Embedded Seminars website. For the Device Driver development session use this link.

Enjoy the nice French accent of your host.

- Nicolas