Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

A sample device driver API structure is shown below.

Anchor
Listing - Device Driver Interface API
Listing - Device Driver Interface API

Code Block
languagecpp
linenumberstrue
const  USBD_DRV_API  USBD_DrvAPI_<controller> = { USBD_DrvInit,               (1)
                                                  USBD_DrvStart,              (2)
                                                  USBD_DrvStop,               (3)
                                                  USBD_DrvAddrSet,            (4)
                                                  USBD_DrvAddrEn,             (5)
                                                  USBD_DrvCfgSet,             (6)
                                                  USBD_DrvCfgClr,             (7)
                                                  USBD_DrvGetFrameNbr,        (8)
                                                  USBD_DrvEP_Open,            (9)
                                                  USBD_DrvEP_Close,          (10)
                                                  USBD_DrvEP_RxStart,        (11)
                                                  USBD_DrvEP_Rx,             (12)
                                                  USBD_DrvEP_RxZLP,          (13)
                                                  USBD_DrvEP_Tx,             (14)
                                                  USBD_DrvEP_TxStart,        (15)
                                                  USBD_DrvEP_TxZLP,          (16)
                                                  USBD_DrvEP_Abort,          (17)
                                                  USBD_DrvEP_Stall,          (18)
                                                  USBD_DrvISR_Handler        (19)
};


Panel
bgColor#f0f0f0

(1) Device initialization/add

(2) Device start

(3) Device stop

(4) Assign device address

(5) Enable device address

(6) Set device configuration

(7) Clear device configuration

(8) Retrieve frame number

(9) Open device endpoint

(10) Close device endpoint

(11) Configure device endpoint to receive data

(12) Receive from device endpoint

(13) Receive zero-length packet from device endpoint

(14) Configure device endpoint to transmit data

(15) Transmit to device endpoint

(16) Transmit zero-length packet to device endpoint

(17) Abort device endpoint transfer

(18) Stall device endpoint

(19) Device interrupt service routine (ISR) handler


Some non-essential functions can also be declared as null pointers. The functions that can be declared as null pointers are: AddrSet(), AddrEn(), CfgSet(), CfgClr() and FrameNbrGet(). Please note that while these functions are not essential for the core to work properly, the USB device driver used may require some or all of them to work correctly.

The shows Listing - Device Driver Interface API with Null Pointers shows a sample API structure with only the mandatory functions declared.

Anchor
Listing - Device Driver Interface API with Null Pointers
Listing - Device Driver Interface API with Null Pointers

Code Block
languagecpp
linenumberstrue
const  USBD_DRV_API  USBD_DrvAPI_<controller> = { USBD_DrvInit,
                                                  USBD_DrvStart,
                                                  USBD_DrvStop,
                                                  DEF_NULL,
                                                  DEF_NULL,
                                                  DEF_NULL,
                                                  DEF_NULL,
                                                  DEF_NULL,
                                                  USBD_DrvEP_Open,
                                                  USBD_DrvEP_Close,
                                                  USBD_DrvEP_RxStart,
                                                  USBD_DrvEP_Rx,
                                                  USBD_DrvEP_RxZLP,
                                                  USBD_DrvEP_Tx,
                                                  USBD_DrvEP_TxStart,
                                                  USBD_DrvEP_TxZLP,
                                                  USBD_DrvEP_Abort,
                                                  USBD_DrvEP_Stall,
                                                  USBD_DrvISR_Handler
};


Info

The details of each device driver API function are described in the Device Controller Driver API Reference.

...

When writing your own device driver, you can assume that each driver API function accepts a pointer to a structure of the type USBD_DRV as one of its parameters. Through this structure, you will be able to access the following fields:

Anchor
Listing - USB Device Driver Data Type
Listing - USB Device Driver Data Type

Code Block
languagecpp
linenumberstrue
typedef  struct  usbd_drv  USBD_DRV;

typedef  usb_drv {
    CPU_INT08U         DevNbr;                                                (1)
    USBD_DRV_API      *API_Ptr;                                               (2)
    USBD_DRV_CFG      *CfgPtr;                                                (3)
    void              *DataPtr;                                               (4)
    USBD_DRV_BSP_API  *BSP_API_Ptr;                                           (5)
};


Panel
bgColor#f0f0f0

(1) Unique index to identify device.

(2) Pointer to USB device controller driver API.

(3) Pointer to USB device controller driver configuration.

(4) Pointer to USB device controller driver specific data.

(5) Pointer to USB device controller BSP.


Memory Allocation

Memory allocation in the driver can be simplified by the use of memory allocation functions available from Micrium’s µC/LIB module. µC/LIB’s memory allocation functions provide allocation of memory from dedicated memory space (e.g., USB RAM) or general purpose heap. The driver may use the pool functionality offered by µC/LIB. Memory pools use fixed-sized blocks that can be dynamically allocated and freed during application execution. Memory pools may be convenient to manage objects needed by the driver. The objects could be for instance data structures mandatory for DMA operations. For more information on using µC/LIB memory allocation functions, consult the µC/LIB documentation.

...

A sample device BSP interface API structure is shown below.

Anchor
Listing - Device BSP Interface API
Listing - Device BSP Interface API

Code Block
languagecpp
linenumberstrue
const  USBD_DRV_BSP_API  USBD_DrvBSP_<controller> = { USBD_BSP_Init,          (1)
                                                      USBD_BSP_Conn,          (2)
                                                      USBD_BSP_Disconn        (3)
};


Panel
bgColor#f0f0f0

(1) Device BSP initialization function pointer

(2) Device BSP connect function pointer

(3) Device BSP disconnect function pointer


Info

The details of each device BSP API function are described in the Device Driver BSP Functions Reference.

...