Article: Writing Device Drivers
A Device Driver provides an interface for application software to communicate with peripherals of the microcontroller. This article explains the various components which should be part of most drivers.
A Device Driver should contain at least the following sections:-
1. Init Section
2. Start/Stop or Read/Write Section
3. Interrupt or Polling Function
1. Initialization:- Init is called before the device driver is required to be used.Init should perform the following opeations:-
i. Set the Operating Mode:- Most peripherals will have different modes of operation. Parameters passed to Init should setup the required mode by writing values to Control and Data registers.
ii. Setup the ports:- The peripheral may be using port pins which are typically multiplexed. Init should setup these pins correctly.
iii. Setup Interrupts or Polling:- If interrupts are being used they should be enabled. If polling is being used, required flags should be cleared
2.Start/Stop or Read/Write:- Peripheral operation like ADC requires methods to Start or Stop them. Others like EEPROM require Read or Write Operations.Following operations may be required:-
i.Application Interface:- Start/Stop and Read/Write methods should provide a way for higher level application software to control lower level drivers. These methods should provide clean entry and exit and also prevent multiple operations when some operations are already ON.
ii.Memory checks:- Read/Write methods should provide checks to make sure protected sections of memory are not accessed. Only memory within limits of operation of the peripheral should be allowed access.
3.Interrupt or Polling Function:-Device Drivers require either Interrupts or Polling Functions to check status of operations. Single or multiple Interrupts may be used for monitoring the status of a single peripheral. Status flags may be polled(checked) repeatedly to do the same. Both types of functions may have methods of passing on the status to application layer.
For developing Device Drivers Inputs may be taken from the Datasheet as well as Hardware Interface Documents.