未验证 提交 b1eb439a 编写于 作者: O openharmony_ci 提交者: Gitee

!7656 [翻译完成】#I5FUBC

Merge pull request !7656 from Annie_wang/PR6394
# SPI<a name="EN-US_TOPIC_0000001199690327"></a> # SPI
## Overview<a name="section84922229152909"></a> ## Overview
In the Hardware Driver Foundation \(HDF\), the Serial Peripheral Interface \(SPI\) 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 HDFDeviceManager can be directly used. However, you need to configure a device node for each device, which increases the memory usage. Serial Peripheral Interface (SPI) is a serial bus specification used for high-speed, full-duplex, and synchronous communication. In the Hardware Driver Foundation (HDF), the SPI module uses the independent service mode for API adaptation. In this mode, each device independently publishes a service to process external access requests. When receiving an access request, the HDF DeviceManager extracts parameters from the request to call the internal APIs of the target device. In the independent service mode, the HDF DeviceManager provides service management capabilities. However, you need to configure a node for each device, which increases memory usage.
**Figure 1** Independent service mode<a name="fig666465313303"></a> **Figure 1** Independent service mode
![](figures/independent-service-mode.png "SPI-independent-service-mode")
## Available APIs<a name="section752964871810"></a> ![image](figures/independent-service-mode.png)
## **Available APIs**
**SpiCntlrMethod**:
SpiCntlrMethod:
``` ```
struct SpiCntlrMethod { struct SpiCntlrMethod {
...@@ -21,422 +23,348 @@ struct SpiCntlrMethod { ...@@ -21,422 +23,348 @@ struct SpiCntlrMethod {
int32_t (*Close)(struct SpiCntlr *cntlr); int32_t (*Close)(struct SpiCntlr *cntlr);
}; };
``` ```
**Table 1** Callbacks for the members in the SpiCntlrMethod structure
**Table 1** Description of the callback functions in SpiCntlrMethod
<a name="table7167123615321"></a>
<table><thead align="left"><tr id="row816783615326"><th class="cellrowborder" valign="top" width="25%" id="mcps1.2.5.1.1"><p id="p8167193643218"><a name="p8167193643218"></a><a name="p8167193643218"></a>Callback</p> | Function| Input Parameter| Return Value| Description|
</th> | -------- | -------- | -------- | -------- |
<th class="cellrowborder" valign="top" width="25%" id="mcps1.2.5.1.2"><p id="p31672362325"><a name="p31672362325"></a><a name="p31672362325"></a>Input Parameter</p> | Transfer | **cntlr**: structure pointer to the SPI controller at the core layer.<br>**msg**: structure pointer to the SPI message.<br>**count**: number of messages. The value is of the uint32_t type.| HDF_STATUS| Transfers messages.|
</th> | SetCfg | **cntlr**: structure pointer to the SPI controller at the core layer.<br>**cfg**: structure pointer to the SPI attributes.| HDF_STATUS| Sets SPI controller attributes.|
<th class="cellrowborder" valign="top" width="24.98%" id="mcps1.2.5.1.3"><p id="p51673367328"><a name="p51673367328"></a><a name="p51673367328"></a>Return Value</p> | GetCfg | **cntlr**: structure pointer to the SPI controller at the core layer.<br>**cfg**: structure pointer to the SPI attributes.| HDF_STATUS| Obtains SPI controller attributes.|
</th> | Open | **cntlr**: structure pointer to the SPI controller at the core layer.| HDF_STATUS| Opens an SPI device.|
<th class="cellrowborder" valign="top" width="25.019999999999996%" id="mcps1.2.5.1.4"><p id="p1116883619322"><a name="p1116883619322"></a><a name="p1116883619322"></a>Description</p> | Close | **cntlr**: structure pointer to the SPI controller at the core layer.| HDF_STATUS| Closes an SPI device.|
</th>
</tr>
</thead> ## How to Develop
<tbody><tr id="row816883693214"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p11683369329"><a name="p11683369329"></a><a name="p11683369329"></a>Transfer</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p0671913132514"><a name="p0671913132514"></a><a name="p0671913132514"></a><strong id="b19675134258"><a name="b19675134258"></a><a name="b19675134258"></a>cntlr</strong>: structure pointer to the SPI controller at the core layer.</p>
<p id="p192109186258"><a name="p192109186258"></a><a name="p192109186258"></a><strong id="b1921051810258"><a name="b1921051810258"></a><a name="b1921051810258"></a>msg</strong>: structure pointer to the SPI message.</p>
<p id="p6168736173213"><a name="p6168736173213"></a><a name="p6168736173213"></a><strong id="b92627310191"><a name="b92627310191"></a><a name="b92627310191"></a>count</strong>: number of messages. The value is of the uint32_t type.</p>
</td>
<td class="cellrowborder" valign="top" width="24.98%" headers="mcps1.2.5.1.3 "><p id="p19168736113216"><a name="p19168736113216"></a><a name="p19168736113216"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="25.019999999999996%" headers="mcps1.2.5.1.4 "><p id="p18168236143218"><a name="p18168236143218"></a><a name="p18168236143218"></a>Transfers messages.</p>
</td>
</tr>
<tr id="row3168113633211"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p4168636193212"><a name="p4168636193212"></a><a name="p4168636193212"></a>SetCfg</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p242632402513"><a name="p242632402513"></a><a name="p242632402513"></a><strong id="b742642452513"><a name="b742642452513"></a><a name="b742642452513"></a>cntlr</strong>: structure pointer to the SPI controller at the core layer.</p>
<p id="p11168163643217"><a name="p11168163643217"></a><a name="p11168163643217"></a><strong id="b1283910558207"><a name="b1283910558207"></a><a name="b1283910558207"></a>cfg</strong>: structure pointer to the SPI attributes.</p>
</td>
<td class="cellrowborder" valign="top" width="24.98%" headers="mcps1.2.5.1.3 "><p id="p101683367325"><a name="p101683367325"></a><a name="p101683367325"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="25.019999999999996%" headers="mcps1.2.5.1.4 "><p id="p1216815368329"><a name="p1216815368329"></a><a name="p1216815368329"></a>Sets SPI controller attributes.</p>
</td>
</tr>
<tr id="row13168183683215"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p11168436123220"><a name="p11168436123220"></a><a name="p11168436123220"></a>GetCfg</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p9698153182520"><a name="p9698153182520"></a><a name="p9698153182520"></a><strong id="b19698131162510"><a name="b19698131162510"></a><a name="b19698131162510"></a>cntlr</strong>: structure pointer to the SPI controller at the core layer.</p>
<p id="p3168936133218"><a name="p3168936133218"></a><a name="p3168936133218"></a><strong id="b11555148182113"><a name="b11555148182113"></a><a name="b11555148182113"></a>cfg</strong>: structure pointer to the SPI attributes.</p>
</td>
<td class="cellrowborder" valign="top" width="24.98%" headers="mcps1.2.5.1.3 "><p id="p18169036193219"><a name="p18169036193219"></a><a name="p18169036193219"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="25.019999999999996%" headers="mcps1.2.5.1.4 "><p id="p3169136173219"><a name="p3169136173219"></a><a name="p3169136173219"></a>Obtains SPI controller attributes.</p>
</td>
</tr>
<tr id="row9169133643218"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p13169163615320"><a name="p13169163615320"></a><a name="p13169163615320"></a>Open</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p181697365327"><a name="p181697365327"></a><a name="p181697365327"></a><strong id="b1876822162218"><a name="b1876822162218"></a><a name="b1876822162218"></a>cntlr</strong>: structure pointer to the SPI controller at the core layer. </p>
</td>
<td class="cellrowborder" valign="top" width="24.98%" headers="mcps1.2.5.1.3 "><p id="p1169163693216"><a name="p1169163693216"></a><a name="p1169163693216"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="25.019999999999996%" headers="mcps1.2.5.1.4 "><p id="p1116963693218"><a name="p1116963693218"></a><a name="p1116963693218"></a>Enables the SPI.</p>
</td>
</tr>
<tr id="row11169436153214"><td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.1 "><p id="p0169736153219"><a name="p0169736153219"></a><a name="p0169736153219"></a>Close</p>
</td>
<td class="cellrowborder" valign="top" width="25%" headers="mcps1.2.5.1.2 "><p id="p31691936193210"><a name="p31691936193210"></a><a name="p31691936193210"></a><strong id="b16798152112239"><a name="b16798152112239"></a><a name="b16798152112239"></a>cntlr</strong>: structure pointer to the SPI controller at the core layer. </p>
</td>
<td class="cellrowborder" valign="top" width="24.98%" headers="mcps1.2.5.1.3 "><p id="p2169123617322"><a name="p2169123617322"></a><a name="p2169123617322"></a>HDF_STATUS</p>
</td>
<td class="cellrowborder" valign="top" width="25.019999999999996%" headers="mcps1.2.5.1.4 "><p id="p1169636103220"><a name="p1169636103220"></a><a name="p1169636103220"></a>Disables the SPI.</p>
</td>
</tr>
</tbody>
</table>
## How to Develop<a name="section799667984152909"></a>
The SPI module adaptation involves the following steps: The SPI module adaptation involves the following steps:
1. Instantiate the driver entry. 1. Instantiate the driver entry.
- Instantiate the **HdfDriverEntry** structure. - Instantiate the **HdfDriverEntry** structure.
- Call **HDF\_INIT** to register the **HdfDriverEntry** instance with the HDF. - Call **HDF_INIT** to register the **HdfDriverEntry** instance with the HDF.
2. Configure attribute files. 2. Configure attribute files.
- Add the **deviceNode** information to the **device\_info.hcs** file. - Add the **deviceNode** information to the **device_info.hcs** file.
- \(Optional\) Add the **spi\_config.hcs** file. - (Optional) Add the **spi_config.hcs** file.
3. Instantiate the SPI controller object. 3. Instantiate the SPI controller object.
- Initialize **SpiCntlr**. - Initialize **SpiCntlr**.
- Instantiate **SpiCntlrMethod** in the **SpiCntlr** object. - Instantiate **SpiCntlrMethod** in the **SpiCntlr** object.
> ![icon-note.gif](public_sys-resources/icon-note.gif) **NOTE**<br>
For details, see [Available APIs](#section752964871810). > For details about the functions in **SpiCntlrMethod**, see [Available APIs](#available-apis).
4. \(Optional\) Debug the driver. 4. Debug the driver.<br>
For new drivers, verify the basic functions, such as the SPI control status and response to interrupts. (Optional) For new drivers, verify the basic functions, such as the SPI status control and response to interrupts.
## Development Example<a name="section956157227152909"></a> ## Development Example
The following uses **spi\_hi35xx.c** as an example to present the contents that need to be provided by the vendor to implement device functions. The following uses **spi_hi35xx.c** as an example to present the information required for implementing 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. 1. Instantiate the driver entry.<br/>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 framework, 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, the HDF calls the **Bind** function and then the **Init** function to load a driver. If **Init** fails to be called, the HDF calls **Release** to release driver resources and exit.
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.
SPI driver entry example:
- SPI driver entry reference
```
``` struct HdfDriverEntry g_hdfSpiDevice = {
struct HdfDriverEntry g_hdfSpiDevice = { .moduleVersion = 1,
.moduleVersion = 1, .moduleName = "HDF_PLATFORM_SPI",// (Mandatory) The value must be the same as that of moduleName in the .hcs file.
.moduleName = "HDF_PLATFORM_SPI",// (Mandatory) The value must be the same as that of moduleName in the .hcs file. .Bind = HdfSpiDeviceBind, // See the Bind function.
.Bind = HdfSpiDeviceBind, // See the Bind function. .Init = HdfSpiDeviceInit, // See the Init function.
.Init = HdfSpiDeviceInit, // See the Init function. .Release = HdfSpiDeviceRelease, //See the Release function.
.Release = HdfSpiDeviceRelease, //See the Release function. };
}; // Call HDF_INIT to register the driver entry with the HDF.
// Call HDF_INIT to register the driver entry with the HDF. HDF_INIT(g_hdfSpiDevice);
HDF_INIT(g_hdfSpiDevice); ```
```
2. Add the **deviceNode** information to the **device_info.hcs** file and configure the device attributes in the **spi_config.hcs** file.
2. Add the **deviceNode** information to the **device\_info.hcs** file and configure the device attributes in the **spi\_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 **SpiCntlr** members at the core layer.
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 **SpiCntlr** members at the core layer.
In this example, there is only one SPI controller. If there are multiple SPI controllers, you need to add the **deviceNode** information to the **device\_info** file and add the corresponding device attributes to the **spi\_config** file.
In this example, there is only one SPI controller. If there are multiple SPI controllers, you need to add the **deviceNode** information to the **device_info** file and add the corresponding device attributes to the **spi_config** file for each controller.
- **device\_info.hcs** configuration reference
- **device_info.hcs** configuration example
```
root {
device_info { ```
match_attr = "hdf_manager"; root {
platform :: host { device_info {
hostName = "platform_host"; match_attr = "hdf_manager";
priority = 50; platform :: host {
device_spi :: device {// Configure an HDF device node for each SPI controller. hostName = "platform_host";
device0 :: deviceNode { priority = 50;
policy = 1; device_spi :: device { // Configure an HDF device node for each SPI controller.
priority = 60; device0 :: deviceNode {
permission = 0644; policy = 1;
moduleName = "HDF_PLATFORM_SPI"; priority = 60;
serviceName = "HDF_PLATFORM_SPI_0"; permission = 0644;
deviceMatchAttr = "hisilicon_hi35xx_spi_0"; moduleName = "HDF_PLATFORM_SPI";
} serviceName = "HDF_PLATFORM_SPI_0";
device1 :: deviceNode { deviceMatchAttr = "hisilicon_hi35xx_spi_0";
policy = 1; }
priority = 60; device1 :: deviceNode {
permission = 0644; policy = 1;
moduleName = "HDF_PLATFORM_SPI"; // (Mandatory) Driver name, which must be the same as that of moduleName in the driver entry structure. priority = 60;
serviceName = "HDF_PLATFORM_SPI_1"; // (Mandatory) Unique name of the service published by the driver permission = 0644;
deviceMatchAttr = "hisilicon_hi35xx_spi_1";// The value must be the same as that of match_attr in the .hcs file. moduleName = "HDF_PLATFORM_SPI"; // (Mandatory) Driver name, which must be the same as that of moduleName in the driver entry structure.
} serviceName = "HDF_PLATFORM_SPI_1"; // (Mandatory) Unique name of the service published by the driver.
... deviceMatchAttr = "hisilicon_hi35xx_spi_1";// The value must be the same as that of match_attr in the .hcs file.
} }
} ...
} }
} }
``` }
}
- **spi\_config.hcs** configuration reference ```
``` - **spi_config.hcs** configuration example
root {
platform {
spi_config {// Configure private data for each SPI controller. ```
template spi_controller {// Template configuration. In the template, you can configure the common parameters shared by service nodes. root {
serviceName = ""; platform {
match_attr = ""; spi_config {// Configure private data for each SPI controller.
transferMode = 0; // Data transfer mode, which can be interrupt transfer (0), flow control transfer (1), or DMA transfer (2). template spi_controller { // Template configuration. In the template, you can configure the common parameters shared by device nodes.
busNum = 0; // Bus number serviceName = "";
clkRate = 100000000; match_attr = "";
bitsPerWord = 8; // Bit width of the data transferred transferMode = 0; // Data transfer mode. The value **0** indicates interrupt transfer, **1** indicates flow control transfer, and **2** indicates DMA transfer.
mode = 19; // SPI data input/output mode busNum = 0; // Bus number.
maxSpeedHz = 0; // Maximum clock frequency clkRate = 100000000;
minSpeedHz = 0; // Minimum clock frequency bitsPerWord = 8 // Number of bits per word.
speed = 2000000; // Current message transfer speed mode = 19; // SPI data input/output mode.
fifoSize = 256; // FIFO size maxSpeedHz = 0; // Maximum clock frequency.
numCs = 1; // Chip select (CS) number minSpeedHz = 0; // Minimum clock frequency.
regBase = 0x120c0000; // Used for address mapping. speed = 2000000; // Current message transfer speed.
irqNum = 100; // Interruption number fifoSize = 256; // FIFO size.
REG_CRG_SPI = 0x120100e4; // CRG_REG_BASE(0x12010000) + 0x0e4 numCs = 1; // Chip select (CS) number.
CRG_SPI_CKEN = 0; regBase = 0x120c0000; // Used for address mapping.
CRG_SPI_RST = 0; irqNum = 100; // Interrupt request (IRQ) number.
REG_MISC_CTRL_SPI = 0x12030024; // MISC_REG_BASE(0x12030000) + 0x24 REG_CRG_SPI = 0x120100e4; // CRG_REG_BASE(0x12010000) + 0x0e4
MISC_CTRL_SPI_CS = 0; CRG_SPI_CKEN = 0;
MISC_CTRL_SPI_CS_SHIFT = 0; CRG_SPI_RST = 0;
} REG_MISC_CTRL_SPI = 0x12030024; // MISC_REG_BASE(0x12030000) + 0x24
controller_0x120c0000 :: spi_controller { MISC_CTRL_SPI_CS = 0;
busNum = 0; // (Mandatory) Bus number MISC_CTRL_SPI_CS_SHIFT = 0;
CRG_SPI_CKEN = 0x10000; // (0x1 << 16) 0:close clk, 1:open clk }
CRG_SPI_RST = 0x1; // (0x1 << 0) 0:cancel reset, 1:reset controller_0x120c0000 :: spi_controller {
match_attr = "hisilicon_hi35xx_spi_0";// (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs. busNum = 0; // (Mandatory) Bus number.
} CRG_SPI_CKEN = 0x10000; // (0x1 << 16) 0:close clk, 1:open clk
controller_0x120c1000 :: spi_controller { CRG_SPI_RST = 0x1; // (0x1 << 0) 0:cancel reset, 1:reset
busNum = 1; match_attr = "hisilicon_hi35xx_spi_0";// (Mandatory) The value must be the same as that of deviceMatchAttr in device_info.hcs.
CRG_SPI_CKEN = 0x20000; // (0x1 << 17) 0:close clk, 1:open clk }
CRG_SPI_RST = 0x2; // (0x1 << 1) 0:cancel reset, 1:reset controller_0x120c1000 :: spi_controller {
match_attr = "hisilicon_hi35xx_spi_1"; busNum = 1;
regBase = 0x120c1000; // (Mandatory) Used for address mapping. CRG_SPI_CKEN = 0x20000; // (0x1 << 17) 0:close clk, 1:open clk
irqNum = 101; // (Mandatory) Interrupt number CRG_SPI_RST = 0x2; // (0x1 << 1) 0:cancel reset, 1:reset
} match_attr = "hisilicon_hi35xx_spi_1";
... regBase = 0x120c1000; // (Mandatory) Used for address mapping.
//(Optional) Add nodes to the device_info.hcs file as required. irqNum = 101; // (Mandatory) IRQ number.
} }
} ...
} //(Optional) Add nodes to the device_info.hcs file as required.
``` }
}
3. Initialize the **SpiCntlr** object at the core layer, including initializing the vendor custom structure \(transferring parameters and data\), instantiating **SpiCntlrMethod** \(used to call underlying functions of the driver\) in **SpiCntlr**, 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 **spi\_config.hcs** file are read by HDF, and the structure members are initialized through **DeviceResourceIface**. Some important values, such as the device number and bus number, are also passed to the object at the core layer. 3. Initialize the **SpiCntlr** object at the core layer, including defining a custom structure (to pass parameters and data) and implementing the **HdfDriverEntry** member functions (**Bind**, **Init**, and **Release**) to instantiate **SpiCntlrMethod** in **SpiCntlr** (so that the underlying driver functions can be called).
- Defining a custom structure
```
struct Pl022 {// Corresponds to parameters in .hcs. To the driver, the custom structure holds parameters and data. The **DeviceResourceIface** method provided by the HDF reads the values in the **spi_config.hcs** file to initialize the members in the custom structure and passes important parameters, such as the device number and bus number, to the **SpiCntlr** object at the core layer.
struct SpiCntlr *cntlr;
struct DListHead deviceList;
struct OsalSem sem; ```
volatile unsigned char *phyBase; struct Pl022 {// Corresponds to parameters in .hcs.
volatile unsigned char *regBase; struct SpiCntlr *cntlr;
uint32_t irqNum; struct DListHead deviceList;
uint32_t busNum; struct OsalSem sem;
uint32_t numCs; volatile unsigned char *phyBase;
uint32_t curCs; volatile unsigned char *regBase;
uint32_t speed; uint32_t irqNum;
uint32_t fifoSize; uint32_t busNum;
uint32_t clkRate; uint32_t numCs;
uint32_t maxSpeedHz; uint32_t curCs;
uint32_t minSpeedHz; uint32_t speed;
uint32_t regCrg; uint32_t fifoSize;
uint32_t clkEnBit; uint32_t clkRate;
uint32_t clkRstBit; uint32_t maxSpeedHz;
uint32_t regMiscCtrl; uint32_t minSpeedHz;
uint32_t miscCtrlCsShift; uint32_t regCrg;
uint32_t miscCtrlCs; uint32_t clkEnBit;
uint16_t mode; uint32_t clkRstBit;
uint8_t bitsPerWord; uint32_t regMiscCtrl;
uint8_t transferMode; uint32_t miscCtrlCsShift;
}; uint32_t miscCtrlCs;
uint16_t mode;
// SpiCntlr is the core layer controller structure. Its members are assigned with values by using the Init function. uint8_t bitsPerWord;
struct SpiCntlr { uint8_t transferMode;
struct IDeviceIoService service; };
struct HdfDeviceObject *device;
uint32_t busNum; // SpiCntlr is the core layer controller structure. The Init function assigns values to the members of SpiCntlr.
uint32_t numCs; struct SpiCntlr {
uint32_t curCs; struct IDeviceIoService service;
struct OsalMutex lock; struct HdfDeviceObject *device;
struct SpiCntlrMethod *method; uint32_t busNum;
struct DListHead list; uint32_t numCs;
void *priv; uint32_t curCs;
}; struct OsalMutex lock;
``` struct SpiCntlrMethod *method;
struct DListHead list;
- Instantiate the callback function structure **SpiCntlrMethod** in **SpiCntlr**. Other members are initialized by using the **Init** function. void *priv;
};
``` ```
// Example in spi_hi35xx.c: instantiate the hook.
struct SpiCntlrMethod g_method = { - Instantiating **SpiCntlrMethod** in **SpiCntlr** (other members are initialized by **Init**)
.Transfer = Pl022Transfer,
.SetCfg = Pl022SetCfg,
.GetCfg = Pl022GetCfg, ```
.Open = Pl022Open, // Example in spi_hi35xx.c: instantiate the hooks.
.Close = Pl022Close, struct SpiCntlrMethod g_method = {
}; .Transfer = Pl022Transfer,
``` .SetCfg = Pl022SetCfg,
.GetCfg = Pl022GetCfg,
- Bind function .Open = Pl022Open,
.Close = Pl022Close,
Input parameters: };
```
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information.
- **Bind** function
Return values:
Input parameter:
HDF\_STATUS
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
Function description:
Return value:
Associates the **SpiCntlr** object with **HdfDeviceObject**.
HDF_STATUS
```
static int32_t HdfSpiDeviceBind(struct HdfDeviceObject *device) Function description:
{
... Associates the **SpiCntlr** object with **HdfDeviceObject**.
return (SpiCntlrCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS;
}
```
struct SpiCntlr *SpiCntlrCreate(struct HdfDeviceObject *device) static int32_t HdfSpiDeviceBind(struct HdfDeviceObject *device)
{ {
struct SpiCntlr *cntlr = NULL; // Create the SpiCntlr object of the core layer. ...
... return (SpiCntlrCreate(device) == NULL) ? HDF_FAILURE : HDF_SUCCESS;
cntlr = (struct SpiCntlr *)OsalMemCalloc(sizeof(*cntlr));// Allocate memory. }
...
cntlr->device = device; // Enable conversion between HdfDeviceObject and SpiCntlr. struct SpiCntlr *SpiCntlrCreate(struct HdfDeviceObject *device)
device->service = &(cntlr->service); // Enable conversion between HdfDeviceObject and SpiCntlr. {
(void)OsalMutexInit(&cntlr->lock); // Initialize the lock. struct SpiCntlr *cntlr = NULL; // Create the SpiCntlr object at the core layer.
DListHeadInit(&cntlr->list); // Add the corresponding node. ...
cntlr->priv = NULL; cntlr = (struct SpiCntlr *)OsalMemCalloc(sizeof(*cntlr));// Allocate memory.
return cntlr; ...
} cntlr->device = device; // Prerequisites for conversion between HdfDeviceObject and SpiCntlr.
``` device->service = &(cntlr->service); // Prerequisites for conversion between HdfDeviceObject and SpiCntlr.
(void)OsalMutexInit(&cntlr->lock); // Initialize the lock.
- Init function DListHeadInit(&cntlr->list); // Add the corresponding nodes.
cntlr->priv = NULL;
Input parameters: return cntlr;
}
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. ```
Return values: - **Init** function
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.\) Input parameter:
**Table 2** Input parameters and return values of the init function **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
<a name="table4311552173914"></a> Return value:
<table><thead align="left"><tr id="row8314152103916"><th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.1"><p id="p331413523392"><a name="p331413523392"></a><a name="p331413523392"></a>Status (Value)</p>
</th> HDF_STATUS
<th class="cellrowborder" valign="top" width="50%" id="mcps1.2.3.1.2"><p id="p7314145220390"><a name="p7314145220390"></a><a name="p7314145220390"></a>Description</p>
</th> The table below lists some status. For more information, see **HDF_STATUS** in the /drivers/framework/include/utils/hdf_base.h file.
</tr>
</thead> **Table 2** HDF_STATUS
<tbody><tr id="row93142052183918"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p93141252123914"><a name="p93141252123914"></a><a name="p93141252123914"></a>HDF_ERR_INVALID_OBJECT</p>
</td> | Status| Description|
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p331485215390"><a name="p331485215390"></a><a name="p331485215390"></a>Invalid controller object</p> | -------- | -------- |
</td> | HDF_ERR_INVALID_OBJECT | Invalid controller object.|
</tr> | HDF_ERR_MALLOC_FAIL | Failed to allocate memory.|
<tr id="row331445211399"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p83141352183917"><a name="p83141352183917"></a><a name="p83141352183917"></a>HDF_ERR_MALLOC_FAIL</p> | HDF_ERR_INVALID_PARAM | Invalid parameter.|
</td> | HDF_ERR_IO | I/O error.|
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p15314175220397"><a name="p15314175220397"></a><a name="p15314175220397"></a>Failed to allocate memory</p> | HDF_SUCCESS | Initialization successful.|
</td> | HDF_FAILURE | Initialization failed.|
</tr>
<tr id="row20314175213918"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p9314352113917"><a name="p9314352113917"></a><a name="p9314352113917"></a>HDF_ERR_INVALID_PARAM</p> Function description:
</td>
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p131535273910"><a name="p131535273910"></a><a name="p131535273910"></a>Invalid parameter</p> Initializes the custom structure object and **SpiCntlr**.
</td>
</tr>
<tr id="row1431565233911"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p0315052103915"><a name="p0315052103915"></a><a name="p0315052103915"></a>HDF_ERR_IO</p> ```
</td> static int32_t HdfSpiDeviceInit(struct HdfDeviceObject *device)
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1831585215398"><a name="p1831585215398"></a><a name="p1831585215398"></a>I/O error</p> {
</td> int32_t ret;
</tr> struct SpiCntlr *cntlr = NULL;
<tr id="row2315152193914"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p7315185263919"><a name="p7315185263919"></a><a name="p7315185263919"></a>HDF_SUCCESS</p> ...
</td> cntlr = SpiCntlrFromDevice(device); // Use service to forcibly convert HdfDeviceObject to SpiCntlr. For details about the value assignment, see the Bind function.
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p1731585213910"><a name="p1731585213910"></a><a name="p1731585213910"></a>Initialization successful</p> // return (device == NULL) ? NULL : (struct SpiCntlr *)device->service;
</td> ...
</tr> ret = Pl022Init(cntlr, device); // (Mandatory) Instantiate the operation object customized by the vendor. The following is an example:
<tr id="row5315752163916"><td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.1 "><p id="p6315105211392"><a name="p6315105211392"></a><a name="p6315105211392"></a>HDF_FAILURE</p> ...
</td> ret = Pl022Probe(cntlr->priv);
<td class="cellrowborder" valign="top" width="50%" headers="mcps1.2.3.1.2 "><p id="p83157528399"><a name="p83157528399"></a><a name="p83157528399"></a>Initialization failed</p> ...
</td> return ret;
</tr> }
</tbody>
</table> static int32_t Pl022Init(struct SpiCntlr *cntlr, const struct HdfDeviceObject *device)
{
Function description: int32_t ret;
struct Pl022 *pl022 = NULL;
Initializes the custom structure object and **SpiCntlr**. ...
pl022 = (struct Pl022 *)OsalMemCalloc(sizeof(*pl022));// Request memory.
``` ...
static int32_t HdfSpiDeviceInit(struct HdfDeviceObject *device) ret = SpiGetBaseCfgFromHcs(pl022, device->property); // Initialize busNum, numCs, speed, fifoSize, clkRate, mode, bitsPerWord, and transferMode.
{ ...
int32_t ret; ret = SpiGetRegCfgFromHcs(pl022, device->property); // Initialize regBase, phyBase, irqNum, regCrg, clkEnBit, clkRstBit, regMiscCtrl, miscCtrlCs, and miscCtrlCsShift.
struct SpiCntlr *cntlr = NULL; ...
... // Calculate the frequencies corresponding to the maximum and minimum speeds.
cntlr = SpiCntlrFromDevice(device);// Forcibly convert HdfDeviceObject to SpiCntlr by using service. For details about the value assignment, see the Bind function. pl022->maxSpeedHz = (pl022->clkRate) / ((SCR_MIN + 1) * CPSDVSR_MIN);
//return (device == NULL) ? NULL : (struct SpiCntlr *)device->service; pl022->minSpeedHz = (pl022->clkRate) / ((SCR_MAX + 1) * CPSDVSR_MAX);
... DListHeadInit(&pl022->deviceList);// Initialize the DList linked list.
ret = Pl022Init(cntlr, device);// (Mandatory) Instantiate the operation object customized by the vendor. The following is an example: pl022->cntlr = cntlr; // Prerequisite for conversion between Pl022 and SpiCntlr.
... cntlr->priv = pl022; // Prerequisite for conversion between Pl022 and SpiCntlr.
ret = Pl022Probe(cntlr->priv); cntlr->busNum = pl022->busNum; // Assign a value to busNum in SpiCntlr.
... cntlr->method = &g_method; // Attach the SpiCntlrMethod instance.
return ret; ...
} ret = Pl022CreatAndInitDevice(pl022);
if (ret != 0) {
static int32_t Pl022Init(struct SpiCntlr *cntlr, const struct HdfDeviceObject *device) Pl022Release(pl022); // Release the Pl022 object if the initialization fails.
{ return ret;
int32_t ret; }
struct Pl022 *pl022 = NULL; return 0;
... }
pl022 = (struct Pl022 *)OsalMemCalloc(sizeof(*pl022));// Apply for memory. ```
...
ret = SpiGetBaseCfgFromHcs(pl022, device->property); // Initialize busNum, numCs, speed, fifoSize, clkRate, mode, bitsPerWord, and transferMode. - **Release** function
...
ret = SpiGetRegCfgFromHcs(pl022, device->property); // Initialize regBase, phyBase, irqNum, regCrg, clkEnBit ,clkRstBit, regMiscCtrl, miscCtrlCs, and miscCtrlCsShift. Input parameter:
...
// Calculate the frequencies corresponding to the maximum and minimum speeds. **HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs information.
pl022->maxSpeedHz = (pl022->clkRate) / ((SCR_MIN + 1) * CPSDVSR_MIN);
pl022->minSpeedHz = (pl022->clkRate) / ((SCR_MAX + 1) * CPSDVSR_MAX); Return value:
DListHeadInit(&pl022->deviceList);// Initialize the DList linked list.
pl022->cntlr = cntlr; // Enable conversion between Pl022 and SpiCntlr. No value is returned.
cntlr->priv = pl022; // Enable conversion between Pl022 and SpiCntlr.
cntlr->busNum = pl022->busNum; // Assign a value to busNum in SpiCntlr. Function description:
cntlr->method = &g_method; // Connect to the SpiCntlrMethod instance.
... Releases the memory and deletes the controller. This function assigns values to the **Release** function in the driver entry structure. If 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 value assignment operations.
ret = Pl022CreatAndInitDevice(pl022);
if (ret != 0) {
Pl022Release(pl022); // Release the Pl022 object if the initialization fails. ```
return ret; static void HdfSpiDeviceRelease(struct HdfDeviceObject *device)
} {
return 0; struct SpiCntlr *cntlr = NULL;
} ...
``` cntlr = SpiCntlrFromDevice(device); // Use service to forcibly convert HdfDeviceObject to SpiCntlr. For details about the value assignment, see the Bind function.
// return (device==NULL) ?NULL:(struct SpiCntlr *)device->service;
- Release function ...
if (cntlr->priv != NULL) {
Input parameters: Pl022Remove((struct Pl022 *)cntlr->priv);// A forced conversion from SpiCntlr to Pl022 is involved.
}
**HdfDeviceObject**, an interface parameter exposed by the driver, contains the .hcs configuration file information. SpiCntlrDestroy(cntlr); // Release the Pl022 object.
}
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.
```
static void HdfSpiDeviceRelease(struct HdfDeviceObject *device)
{
struct SpiCntlr *cntlr = NULL;
...
cntlr = SpiCntlrFromDevice(device);// Forcibly convert HdfDeviceObject to SpiCntlr by using service. For details about the value assignment, see the Bind function.
// return (device==NULL) ?NULL:(struct SpiCntlr *)device->service;
...
if (cntlr->priv != NULL) {
Pl022Remove((struct Pl022 *)cntlr->priv);// A forced conversion from SpiCntlr to Pl022 is involved.
}
SpiCntlrDestroy(cntlr); // Release the Pl022 object.
}
```
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册