Changes between Initial Version and Version 1 of TBR/Review/RsgTrialsAndTribulations


Ignore:
Timestamp:
05/20/08 22:59:31 (16 years ago)
Author:
Rsg
Comment:

New page: == SPI Driver Development == === Major/Minor? Device Driver Numbers === As is commonly done in various OS environments, device resources are assigned major and minor "device numbers" by R...

Legend:

Unmodified
Added
Removed
Modified
  • TBR/Review/RsgTrialsAndTribulations

    v1 v1  
     1= RsgTrialsAndTribulations =
     2
     3=  SPI Driver Development  =
     4
     5=  Major/Minor Device Driver Numbers  =
     6
     7
     8As is commonly done in various OS environments, device resources are assigned major and minor "device numbers" by RTEMS. While this concept is quite simple on the face of it, with libi2c things are a bit more complicated.  An explanation of how these numbers are assigned was provided to me by Till Straumann:
     9
     10<blockquote>
     11Traditionally, the major number is associated with a particular driver (registered with the OS) the minor number with a device instance; ultimately the semantics of the minor number are defined by the driver, the major number by the OS.
     12
     13libi2c registers itself with RTEMS under one major number and it uses specific semantics for the minor number.
     14</blockquote>
     15
     16One thing to realize here is that the major number here applies to the libi2c driver, which includes both I2C '''and''' SPI interfaces.  So a hypothetical system could have two I2C and three SPI controllers, each with a number of devices attached. RTEMS would assign the same major number to all controllers and devices; only the minor number for each device would differ.
     17=  Device Minor Numbers  =
     18
     19
     20So, device minor numbers hide quite a bit of complexity behind a simple integer!  Again from Till:
     21
     22<blockquote>
     23The 'minor' number actually encodes three things:
     24
     251)  'device' driver (e.g., temp. sensor) as registered with
     26     rtems_libi2c_register_drv(). The device driver
     27     knows what to write to/read from the device
     28     to achieve certain things. However, it doesn't know
     29     how to interact with the particular i2c/spi controller
     30     chip in order to write to/read from the device.
     31
     322) 'bus' driver (for a particular i2c/spi controller) as registered
     33    with rtems_libi2c_register_bus(). The bus driver knows
     34    *how* to communicate with the attached devices but
     35    it doesn't know about the semantics.
     36
     373) device address on the bus (multiple devices using the
     38    same 'device' and 'bus' drivers may be present.
     39</blockquote>
     40
     41
     42a) you register a bus-driver for your SPI chip with libi2c and get
     43    a bus number returned.
     44=  Subsection  =
     45
     46
     47b) you register a device-driver for your EEPROM with libi2c on
     48    a bus (use number from step a) at a device address on that bus.
     49    You obtain a minor number back.
     50    Optionally, step b) also creates a file system entry (mknod)
     51    with your name of choice, libi2c's major number and the minor
     52    number from step b)
     53
     54    Step b) can be repeated for multiple instances of identical
     55    devices (same dev-driver but different dev-address => different
     56    minor number).
     57=  Section  =
     58
     59
     60Here's what happens when you e.g., read() from a libi2c device:
     61
     62   1) OS knows (by major number) that it has to call libi2c
     63   2) OS dispatches rtems_i2c_read()
     64   3) libi2c figures out what bus and device drivers (as registered in a+b)
     65       to use. It sends a addressing sequence using the bus driver and
     66       the device address (encoded in minor number)
     67   4) libi2c calls the 'device' driver's 'read' function
     68   5) the 'read' function does it's job (e.g., if you read from a
     69thermometer
     70       the driver has to i) trigger a measurement, ii) read back from the
     71       device, interpret the results and hand them to the user.
     72   6) the device driver accomplishes i), ii) using libi2c's low-level
     73routines
     74        rtems_libi2c_start_write_bytes & friends to interact with the device
     75   7) low-level routines use the bus driver (as encoded in 'minor') to
     76        actually perform the communication
     77
     78A bit complicated but it strictly separates drivers for the
     79bus controllers ('how' to read/write) from the drivers for
     80the attached devices ('what' to read/write).
     81
     82Look in libchip/i2c for examples of libi2c 'device' drivers.