This page is available in: en


We are currently preparing the source code related to this article for download. It will be available soon.

SDI Device Driver Guide – Part 2

Written by: Mukul Sharma and Fauzi Abbas, Centre for High Performance Embedded Systems, NTU Singapore
Edited by: Mohit Sindhwani (TE@Onghu)

In the first part of this article, we explained the process for writing a device driver. In this concluding part, we look at the process for using the device driver – building, running and accessing the driver from an application.

The main thing to remember is that a device driver is not very different from a regular application, and many of the rules regarding applications apply also to device drivers.

Compiling the Device Driver

The Sample Device Driver can be build and compiled similarly as any other T-Kernel Application. The procedure described in the article, “Building and Running a Sample Program”, is applicable to the device driver also. It compiles like a regular application.

However, there is one big difference – the makefile.

The Makerules files for applications and device drivers are quite different and the Makefile for the driver should point to the correct Makerules file which is at /te/etc/Makerules. It is a good idea to build the driver from a project folder inside the /te/driver directory, so that the Makefile points to the correct Makerules file.

Running the Device Driver

If you follow the procedure in the above article, you can download the device driver executable to the T-Engine using the serial console. Assuming that the executable is called sample_driver run the following command to launch the driver after it has been downloaded.

lodspg     sample_driver

If you want to automatically launch the device driver when the T-Engine is started, you can follow the steps in this HOWTO article.

Sample Device Driver – Include File

As mentioned in the previous part, the device driver can define certain constants that may be needed by the application that uses the device driver. This can be done by defining the constants in a header file (e.g. sample_drvr.h) and this header file can be copied to an include folder of the T-Engine environment (somewhere under /te/include).

Sample Application using the Device Driver

The following source code shows a simple application that uses the device driver.

#include <stdio.h>
#include <tk/tkernel.h>
#include <string.h>
#include <sample_drvr.h>

void main(void)
    UB  devnm[8]; /* device name */
    ID  dds;      /* device descriptor */
    INT asiz;     /* read/write size */
    ER  ercd;     /* error code */
    INT dvc;

    strcpy( devnm, "DRVSAMP" );  /* our device name */
    /* The next line will result in our openfn function being called */
    dds = tk_opn_dev( devnm, TD_UPDATE ); /* open device in update mode */

    if (dds < E_OK)
        printf (" Failed to open device. \n");
        exit -1;

    printf(" Device Descripter - %x \n", dds);

    /* read the device specifications */
    ercd = tk_srea_dev( dds, DN_DVCSPEC, &dvc, sizeof(INT), &asiz );
    if (ercd < E_OK) goto ext;

    /* write device specification */
    ercd = tk_swri_dev( dds, DN_DVCSPEC, &dvc, sizeof(INT), &asiz );
    if (ercd < E_OK) goto ext;

    /* Use the device for other things */

    tk_cls_dev( dds, 0 );  /* Close device - will call our closefn */
    printf(" Exiting Demo ! \n");


Building and Running the Application

Build the application like a regular T-Kernel application, download it to the T-Engine and if it is called driver_demo, launch it using:

lodspg     driver_demo

You should see something like the screen below when it runs.