UART

Overview

In the Hardware Driver Foundation (HDF), the Universal Asynchronous Receiver/Transmitter (UART) uses the independent service mode for API adaptation. In this mode, each device independently publishes a device service to handle external access requests. After receiving an access request from an API, the device manager extracts the parameters in the request to call the internal method of the target device. In the independent service mode, the service management capabilities of the HDF Device Manager can be directly used. However, you need to configure a device node for each device, which increases the memory usage.

Figure 1 Independent service mode

Available APIs

UartHostMethod

struct UartHostMethod {
  int32_t (*Init)(struct UartHost *host);
  int32_t (*Deinit)(struct UartHost *host);
  int32_t (*Read)(struct UartHost *host, uint8_t *data, uint32_t size);
  int32_t (*Write)(struct UartHost *host, uint8_t *data, uint32_t size);
  int32_t (*GetBaud)(struct UartHost *host, uint32_t *baudRate);
  int32_t (*SetBaud)(struct UartHost *host, uint32_t baudRate);
  int32_t (*GetAttribute)(struct UartHost *host, struct UartAttribute *attribute);
  int32_t (*SetAttribute)(struct UartHost *host, struct UartAttribute *attribute);
  int32_t (*SetTransMode)(struct UartHost *host, enum UartTransMode mode);
  int32_t (*pollEvent)(struct UartHost *host, void *filep, void *table);
};

Table 1 Callbacks for the members in the UartHostMethod structure

Callback

Input Parameter

Output Parameter

Return Value

Description

Init

host: structure pointer to the UART controller at the core layer.

HDF_STATUS

Initializes the UART device.

Deinit

host: structure pointer to the UART controller at the core layer.

HDF_STATUS

Deinitializes the UART device.

Read

host: structure pointer to the UART controller at the core layer.

size: data size, which is of the uint32_t type.

data: uint8_t pointer to the output data.

HDF_STATUS

Receives data.

Write

host: structure pointer to the UART controller at the core layer.

data: pointer to the input data, which is of the uint8_t type.

size: data size, which is of the uint32_t type.

HDF_STATUS

Sends data.

SetBaud

host: structure pointer to the UART controller at the core layer.

baudRate: pointer to the input baud rate, which is of the uint32_t type.

HDF_STATUS

Sets the baud rate.

GetBaud

host: structure pointer to the UART controller at the core layer.

baudRate: uint32_t pointer to the output baud rate.

HDF_STATUS

Obtains the current baud rate.

GetAttribute

host: structure pointer to the UART controller at the core layer.

attribute: structure pointer to the output attributes. For details, see UartAttribute in uart_if.h.

HDF_STATUS

Obtains UART attributes.

SetAttribute

host: structure pointer to the UART controller at the core layer.

attribute: structure pointer to the input attributes.

HDF_STATUS

Sets UART attributes.

SetTransMode

host: structure pointer to the UART controller at the core layer.

mode: transmission mode, which is an enumerated value. For details, see UartTransMode in uart_if.h).

HDF_STATUS

Sets the UART transmission mode.

PollEvent

host: structure pointer to the UART controller at the core layer.

filep: void pointer to a file.

table: void pointer to a poll_table.

HDF_STATUS

Polls for pending events.

How to Develop

The UART module adaptation involves the following steps:

  1. Instantiate the driver entry.

    • Instantiate the HdfDriverEntry structure.
    • Call HDF_INIT to register the HdfDriverEntry instance with the HDF.
  2. Configure attribute files.

    • Add the deviceNode information to the device_info.hcs file.
    • (Optional) Add the uart_config.hcs file.
  3. Instantiate the UART controller object.

    • Initialize UartHost.

    • Instantiate UartHostMethod in the UartHost object.

      NOTE

      For details, see Available APIs.

  4. Debug the driver.

    • (Optional) For new drivers, verify the basic functions, such as the UART control status and response to interrupts.

Development Example

The following uses uart_hi35xx.c as an example to present the contents that need to be provided by the vendor to implement device functions.

  1. Instantiate the driver entry. The driver entry must be a global variable of the HdfDriverEntry type (defined in hdf_device_desc.h), and the value of moduleName must be the same as that in device_info.hcs. In the HDF, the start address of each HdfDriverEntry object of all loaded drivers is collected to form a segment address space similar to an array for the upper layer to invoke.

    Generally, HDF calls the Bind function and then the Init function to load a driver. If Init fails to be called, HDF calls Release to release driver resources and exit.

    • UART driver entry reference

      struct HdfDriverEntry g_hdfUartDevice = {
          .moduleVersion = 1,
        .moduleName = "HDF_PLATFORM_UART", // (Mandatory) This parameter must be the same as that in the .hcs file.
            .Bind = HdfUartDeviceBind, // See the Bind function.
          .Init = HdfUartDeviceInit,  // See the Init function.
         .Release = HdfUartDeviceRelease, //See the Release function.
      };
      // Call HDF_INIT to register the driver entry with the HDF.
      HDF_INIT(g_hdfUartDevice);
      
  2. Add the deviceNode information to the device_info.hcs file and configure the device attributes in the uart_config.hcs file. The deviceNode information is related to registration of the driver entry. The device attribute values are closely related to the default values or value ranges of the UartHost members at the core layer.

    In this example, there is only one UART controller. If there are multiple UART controllers, you need to add the deviceNode information to the device_info file and add the corresponding device attributes to the uart_config file.

    • device_info.hcs configuration reference

      root {
        device_info {
          match_attr = "hdf_manager";
          platform :: host {
            hostName = "platform_host";
            priority = 50;
            device_uart :: device {
              device0 :: deviceNode {
                policy = 1; // The value 1 indicates that the driver publishes kernel-mode services. The value 2 indicates that the driver publishes user-mode services.
                          priority = 40;                        // Driver startup priority
              permission = 0644;                  // Permission for the driver to create a device node
                         moduleName = "HDF_PLATFORM_UART";  // Driver name, which must be the same as that of moduleName in the driver entry structure.
                 serviceName = "HDF_PLATFORM_UART_0";// Unique name of the service published by the driver. The name is in the HDF_PLATFORM_UART_X format. X indicates the UART controller number.
              deviceMatchAttr = "hisilicon_hi35xx_uart_0"; // Keyword matching the private data of the driver. The value must be the same as that of match_attr in the private data configuration table of the driver.
              }
              device1 :: deviceNode {
                policy = 2;
                permission = 0644;
                priority = 40;
                moduleName = "HDF_PLATFORM_UART"; 
                serviceName = "HDF_PLATFORM_UART_1";
                deviceMatchAttr = "hisilicon_hi35xx_uart_1";
              }
              ...
            }
          }
        }
      }
      
    • uart_config.hcs configuration reference

      root {
        platform {
            template uart_controller {// Template configuration. In the template, you can configure the common parameters shared by service nodes.
            match_attr = "";
              num = 0; // (Mandatory) Device number
            baudrate = 115200; // (Mandatory) Baud rate. Set the value based on service requirements.
            fifoRxEn = 1; // (Mandatory) Enable the receive of FIFOs.
            fifoTxEn = 1; // (Mandatory) Enable the transmit of FIFOs.
            flags = 4; // (Mandatory) Flag signal
            regPbase = 0x120a0000;  // (Mandatory) Used for address mapping.
            interrupt = 38; // (Mandatory) Interrupt number
            iomemCount = 0x48;  // (Mandatory) Used for address mapping.
          }
          controller_0x120a0000 :: uart_controller {
            match_attr = "hisilicon_hi35xx_uart_0";// (Mandatory) The value must be the same as that of deviceMatchAttr of the corresponding device in device_info.hcs.
          }
          controller_0x120a1000 :: uart_controller {
            num = 1;
            baudrate = 9600;
            regPbase = 0x120a1000;
            interrupt = 39;
            match_attr = "hisilicon_hi35xx_uart_1";
          }
          ...
          //(Optional) Add nodes to the device_info.hcs file as required.
        }
      }
      
  3. Initialize the UartHost object at the core layer, including initializing the vendor custom structure (transferring parameters and data), instantiating UartHostMethod (used to call underlying functions of the driver) in UartHost, and implementing the HdfDriverEntry member functions (Bind, Init, and Release).

    • Custom structure reference

      To the driver, the custom structure carries parameters and data. The values in the uart_config.hcs file are read by HDF, and the structure members are initialized through DeviceResourceIface. Some important values, such as the device number, are also passed to the objects at the core layer.

      struct UartPl011Port {// Structure related to the API
          int32_t             enable;
           unsigned long       physBase; // Physical address
              uint32_t            irqNum;          // Interrupt number
              uint32_t            defaultBaudrate;// Default baud rate
              uint32_t            flags;         // Flag signal, which is related to the following three macros.
      #define PL011_FLG_IRQ_REQUESTED    (1 << 0)
      #define PL011_FLG_DMA_RX_REQUESTED (1 << 1)
      #define PL011_FLG_DMA_TX_REQUESTED (1 << 2)
          struct UartDmaTransfer *rxUdt; // DMA transmission
          struct UartDriverData *udd; // See the following description.
      };
      struct UartDriverData {// Structure related to data transmission
          uint32_t num;
          uint32_t baudrate; // Baud rate (configurable)
          struct UartAttribute attr; // Transmission attributes, such as the data bit and stop bit
          struct UartTransfer *rxTransfer; // FIFO structure related to the buffer
          wait_queue_head_t wait; // Queuing signal related to conditional variables
          int32_t count; // Data count
          int32_t state; // UART controller status
      #define UART_STATE_NOT_OPENED 0
      #define UART_STATE_OPENING    1
      #define UART_STATE_USEABLE    2
      #define UART_STATE_SUSPENED   3
          uint32_t flags; // Status flag
      #define UART_FLG_DMA_RX       (1 << 0)
      #define UART_FLG_DMA_TX       (1 << 1)
      #define UART_FLG_RD_BLOCK     (1 << 2)
          RecvNotify recv; // Pointer to the function that receives serial port data
          struct UartOps *ops; // Custom function pointer structure. For details, see device/hisilicon/drivers/uart/uart_pl011.c.
          void *private; // It stores the pointer to the start address of UartPl011Port for easy invocation.
      };
      
      // UartHost is the controller structure at the core layer. Its members are assigned with values by using the Init function.
      struct UartHost {
          struct IDeviceIoService service;
          struct HdfDeviceObject *device;
          uint32_t num;
          OsalAtomic atom;
          void *priv; // It stores the pointer to the start address of the vendor's custom structure for invoking the structure.
          struct UartHostMethod *method; // Hook at the core layer. The vendor needs to implement and instantiate its member functions.
      };
      
    • Instantiate the callback function structure UartHostMethod in UartHost. Other members are initialized by using the Bind function.

      // Example in pwm_hi35xx.c: instantiate the hook.
      struct UartHostMethod g_uartHostMethod = {
        .Init = Hi35xxInit,
        .Deinit = Hi35xxDeinit,
        .Read = Hi35xxRead,
        .Write = Hi35xxWrite,
        .SetBaud = Hi35xxSetBaud,
        .GetBaud = Hi35xxGetBaud,
        .SetAttribute = Hi35xxSetAttribute,
        .GetAttribute = Hi35xxGetAttribute,
        .SetTransMode = Hi35xxSetTransMode,
        .pollEvent = Hi35xxPollEvent,
      };
      
    • Bind function

      Input parameters:

      HdfDeviceObject, an interface parameter exposed by the driver, contains the .hcs configuration file information.

      Return values:

      HDF_STATUS (The following table lists some status. For details about other status, see HDF_STATUS in the //drivers/framework/include/utils/hdf_base.h file.)

      Table 2 Input parameters and return values of the Bind function

      Status (Value)

      Description

      HDF_ERR_INVALID_OBJECT

      Invalid controller object

      HDF_ERR_MALLOC_FAIL

      Failed to allocate memory

      HDF_ERR_INVALID_PARAM

      Invalid parameter

      HDF_ERR_IO

      I/O error

      HDF_SUCCESS

      Initialization successful

      HDF_FAILURE

      Initialization failed

      Function description:

      Initializes the custom structure object and UartHost.

      //uart_hi35xx.c
      static int32_t HdfUartDeviceBind(struct HdfDeviceObject *device)
      {
          ...
          return (UartHostCreate(device) == NULL)? HDF_FAILURE: HDF_SUCCESS;// (Mandatory) Call UartHostCreate.
      }
      // Description of the UartHostCreate function at the core layer in uart_core.c
      struct UartHost *UartHostCreate(struct HdfDeviceObject *device)
      {
          struct UartHost *host = NULL; // Create a UartHost.
          ...
          host = (struct UartHost *)OsalMemCalloc(sizeof(*host));// Apply for memory.
          ...
           host->device = device;               // (Mandatory) Enable conversion between HdfDeviceObject and UartHost.
          device->service = &(host->service); // (Mandatory) Enable conversion between HdfDeviceObject and UartHost.
          host->device->service->Dispatch = UartIoDispatch;// Assign a value to the Dispatch method of service.
          OsalAtomicSet(&host->atom, 0); // Initialize or set atomic data.
          host->priv = NULL;
          host->method = NULL;
          return host;
      }
      
    • Init function

      Input parameters:

      HdfDeviceObject, an interface parameter exposed by the driver, contains the .hcs configuration file information.

      Return values:

      HDF_STATUS

      Function description:

      Initializes the custom structure object and UartHost, calls the artAddDev function at the core layer, and connects to VFS.

      int32_t HdfUartDeviceInit(struct HdfDeviceObject *device)
      {
          int32_t ret;
          struct UartHost *host = NULL;
          HDF_LOGI("%s: entry", __func__);
          ...
          host = UartHostFromDevice(device);// Forcibly convert to UartHost by using service. The value is assigned in the Bind function.
          ...
          ret = Hi35xxAttach(host, device); // Initialize the UartHost object.
          ...
          host->method = &g_uartHostMethod; // Connect to the UARTHostMethod instance.
          return ret;
      }
      // Complete initialization of the UartHost object.
      static int32_t Hi35xxAttach(struct UartHost *host, struct HdfDeviceObject *device)
      {
          int32_t ret;
          // udd and port are structure objects customized by the vendor. The vendor needs to implement related functions as required.
          struct UartDriverData *udd = NULL;
          struct UartPl011Port *port = NULL;
          ...
          // Steps [1] to [7] assign values to udd and then to the UartHost object at the core layer.
          udd = (struct UartDriverData *)OsalMemCalloc(sizeof(*udd));// [1] 
          ...
          port = (struct UartPl011Port *)OsalMemCalloc(sizeof(struct UartPl011Port));// [2] 
          ...
          udd->ops = Pl011GetOps();// [3] Perform operations, such as starting or stopping a device, setting attributes, and sending data.
          udd->recv = PL011UartRecvNotify;// [4] Connect to the data receiving notification function (conditional lock mechanism).
          udd->count = 0;          // [5] 
         port->udd = udd;          // [6]  Enable conversion between UartPl011Port and UartDriverData.
           ret = UartGetConfigFromHcs(port, device->property);// (Mandatory) Transfer the attributes of HdfDeviceObject to the vendor custom structure.
                                                             // The sample code is as follows:
          ...
          udd->private = port;     // [7] 
          
          host->priv = udd; // (Mandatory) Enable conversion between UartHost and UartDriverData.
          host->num = udd->num;// (Mandatory) UART device number
          UartAddDev(host); // (Mandatory) Function in uart_dev.c at the core layer used to register a character device node with the VFS so that the UART can be accessed in user mode through this virtual file node.
          return HDF_SUCCESS;
      }
      
      static int32_t UartGetConfigFromHcs(struct UartPl011Port *port, const struct DeviceResourceNode *node)
      {
          uint32_t tmp, regPbase, iomemCount;
          struct UartDriverData *udd = port->udd;
          struct DeviceResourceIface *iface = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); 
          ...
          // Extract the value based on the request parameter and assign the value to the vendor custom structure.
          if (iface->GetUint32(node, "num", &udd->num, 0) != HDF_SUCCESS) {
              HDF_LOGE("%s: read busNum fail", __func__);
              return HDF_FAILURE;
          }
          ...
          return 0;
          }
      
    • Release function

      Input parameters:

      HdfDeviceObject, an interface parameter exposed by the driver, contains the .hcs configuration file information.

      Return values:

      Function description:

      Releases the memory and deletes the controller. This function assigns a value to the Release API in the driver entry structure. When the HDF fails to call the Init function to initialize the driver, the Release function can be called to release driver resources. All forced conversion operations for obtaining the corresponding object can be successful only when the Init function has the corresponding value assignment operations.

      void HdfUartDeviceRelease(struct HdfDeviceObject *device)
      {
          struct UartHost *host = NULL;
          ...
          host = UartHostFromDevice(device);// Forcibly convert HdfDeviceObject to UartHost by using service. For details about the value assignment, see the Bind function.
          ...
          if (host->priv != NULL) {
              Hi35xxDetach(host); // Memory release function customized by the vendor. For details, see the following description.
          }
          UartHostDestroy(host); // Call the function of the core layer to release the host.
      }
      
      static void Hi35xxDetach(struct UartHost *host)
      {
          struct UartDriverData *udd = NULL;
          struct UartPl011Port *port = NULL;
          ...
          udd = host->priv; // Convert UartHost to UartDriverData.
          ...
          UartRemoveDev(host);// Remove the VFS.
          port = udd->private;// Convert UartDriverData to UartPl011Port.
          if (port != NULL) {
              if (port->physBase != 0) {
                  OsalIoUnmap((void *)port->physBase);// Remove address mapping.
              }
              (void)OsalMemFree(port);
              udd->private = NULL;
          }
          (void)OsalMemFree(udd);// Release UartDriverData.
          host->priv = NULL;
      }