加入收藏
举报
02-14 20:02
#0
文件名称:
SPI.md
所在目录:
Openharmony v1.0 / docs / api / api-SmartVision-Devices
文件大小:
30.66 KB
下载地址:
fenwii/OpenHarmony
   
免责声明:本网站仅提供指向 GitHub 上的文件的链接,所有文件的版权归原作者所有,本网站不对文件内容的合法性、准确性或安全性承担任何责任。
文本预览:
# SPI
## **Overview**
Defines standard APIs of the Serial Peripheral Interface \(SPI\) capabilities.
The SPI module abstracts the SPI capabilities of different system platforms to provide stable APIs for driver development. This module can create and destroy SPI device handles, read and write SPI data, and obtain and set configuration parameters.
**Since:**
1.0
## **Summary**
## Files









File Name


Description


spi_if.h


Defines standard SPI-specific interfaces for driver development.



## Data Structures















Data Structure Name


Description


SpiDevInfo


Defines the general SPI device descriptor, which can be used as the unique identifier of an SPI device. When operating an SPI device, you need to specify a descriptor of the SpiDevInfo type, and obtain the handle of the SPI device by calling SpiOpen.


SpiMsg


Defines the custom SPI transfer message.


SpiCfg


Defines the configuration of an SPI device.



## Macros






























Macro Name and Value


Description


SPI_CLK_PHASE   (1 << 0)


Indicates the SPI clock phase. The value 0 indicates that data will be sampled on the first clock edge, and 1 indicates that data will be sampled on the second clock edge.


SPI_CLK_POLARITY   (1 << 1)


Indicates the SPI clock polarity. The value 0 indicates a low-level clock signal in the idle state, and 1 indicates a high-level clock signal in the idle state.


SPI_MODE_3WIRE   (1 << 2)


Indicates that a single data line is used for both input and output.


SPI_MODE_LOOP   (1 << 3)


Indicates the SPI loopback mode.


SPI_MODE_LSBFE   (1 << 4)


Indicates the SPI data transfer order. The value 0 indicates that data is transferred from the most significant bit (MSB) to the least significant bit (LSB), and 1 indicates the opposite.


SPI_MODE_NOCS   (1 << 5)


Indicates that there is only one SPI device, and no chip select (CS) is required.


SPI_MODE_CS_HIGH   (1 << 6)


Indicates that the CS level is high when an SPI device is selected.


SPI_MODE_READY   (1 << 7)


Indicates that the SPI device is set to low for pausing data transfer.



## Enumerations









Enumeration Name


Description


SpiTransferMode { SPI_INTERRUPT_TRANSFER = 0, SPI_POLLING_TRANSFER, SPI_DMA_TRANSFER }


Enumerates transfer modes of SPI data.



## Functions



























Function Name


Description


SpiOpen (const struct SpiDevInfo *info)


struct DevHandle


Obtains the handle of an SPI device.


SpiClose (struct DevHandle *handle)


void 


Releases the handle of an SPI device.


SpiTransfer (struct DevHandle *handle, struct SpiMsg *msgs, uint32_t count)


int32_t 


Launches a custom transfer to an SPI device.


SpiRead (struct DevHandle *handle, uint8_t *buf, uint32_t len)


int32_t 


Reads data of a specified length from an SPI device.


SpiWrite (struct DevHandle *handle, uint8_t *buf, uint32_t len)


int32_t 


Writes data of a specified length to an SPI device.


SpiSetCfg (struct DevHandle *handle, struct SpiCfg *cfg)


int32_t 


Sets configuration parameters for an SPI device.


SpiGetCfg (struct DevHandle *handle, struct SpiCfg *cfg)


int32_t 


Obtains the configuration parameters of an SPI device.



## **Details**
## **Macro Definition Documentation**
## SPI\_CLK\_PHASE
```
#define SPI_CLK_PHASE   (1 << 0)
```
**Description:**
Indicates the SPI clock phase. The value **0** indicates that data will be sampled on the first clock edge, and **1** indicates that data will be sampled on the second clock edge.
## SPI\_CLK\_POLARITY
```
#define SPI_CLK_POLARITY   (1 << 1)
```
**Description:**
Indicates the SPI clock polarity. The value **0** indicates a low-level clock signal in the idle state, and **1** indicates a high-level clock signal in the idle state.
## SPI\_MODE\_3WIRE
```
#define SPI_MODE_3WIRE   (1 << 2)
```
**Description:**
Indicates that a single data line is used for both input and output.
## SPI\_MODE\_CS\_HIGH
```
#define SPI_MODE_CS_HIGH   (1 << 6)
```
**Description:**
Indicates that the CS level is high when an SPI device is selected.
## SPI\_MODE\_LOOP
```
#define SPI_MODE_LOOP   (1 << 3)
```
**Description:**
Indicates the SPI loopback mode.
## SPI\_MODE\_LSBFE
```
#define SPI_MODE_LSBFE   (1 << 4)
```
**Description:**
Indicates the SPI data transfer order. The value **0** indicates that data is transferred from the most significant bit \(MSB\) to the least significant bit \(LSB\), and **1** indicates the opposite.
## SPI\_MODE\_NOCS
```
#define SPI_MODE_NOCS   (1 << 5)
```
**Description:**
Indicates that there is only one SPI device, and no chip select \(CS\) is required.
## SPI\_MODE\_READY
```
#define SPI_MODE_READY   (1 << 7)
```
**Description:**
Indicates that the SPI device is set to low for pausing data transfer.
## **Enumeration Type Documentation**
## SpiTransferMode
```
enum [SpiTransferMode](SPI.md#ga55946d1d895fc2b7b33007019de1668f)
```
**Description:**
Enumerates transfer modes of SPI data.
**Attention:**
The specific SPI controller determines which variables in this structure are supported.















Enumerator


Description


SPI_INTERRUPT_TRANSFER 

Interrupt transfer mode


SPI_POLLING_TRANSFER 

Polling transfer mode


SPI_DMA_TRANSFER 

Direct Memory Access (DMA) transfer mode



## **Function Documentation**
## SpiClose\(\)
```
void SpiClose (struct [DevHandle](DevHandle.md) * handle)
```
**Description:**
Releases the handle of an SPI device.
**Parameters:**









Name


Description


handle Indicates the pointer to the SPI device handle obtained via SpiOpen.

## SpiGetCfg\(\)
```
int32_t SpiGetCfg (struct [DevHandle](DevHandle.md) * handle, struct [SpiCfg](SpiCfg.md) * cfg )
```
**Description:**
Obtains the configuration parameters of an SPI device.
**Parameters:**












Name


Description


handle Indicates the pointer to the SPI device handle obtained via SpiOpen.
cfg Indicates the pointer to the configuration parameters.

**Returns:**
Returns **0** if the operation is successful; returns a negative value otherwise.
## SpiOpen\(\)
```
struct [DevHandle](DevHandle.md)* SpiOpen (const struct [SpiDevInfo](SpiDevInfo.md) * info)
```
**Description:**
Obtains the handle of an SPI device.
**Parameters:**









Name


Description


info Indicates the pointer to the SPI device information.

**Returns:**
Returns the pointer to the handle of the SPI device if the operation is successful; returns **NULL** otherwise.
## SpiRead\(\)
```
int32_t SpiRead (struct [DevHandle](DevHandle.md) * handle, uint8_t * buf, uint32_t len )
```
**Description:**
Reads data of a specified length from an SPI device.
**Parameters:**















Name


Description


handle Indicates the pointer to the SPI device handle obtained via SpiOpen.
buf Indicates the pointer to the buffer for receiving the data.
len Indicates the length of the data to read.

**Returns:**
Returns **0** if the operation is successful; returns a negative value otherwise.
## SpiSetCfg\(\)
```
int32_t SpiSetCfg (struct [DevHandle](DevHandle.md) * handle, struct [SpiCfg](SpiCfg.md) * cfg )
```
**Description:**
Sets configuration parameters for an SPI device.
**Parameters:**












Name


Description


handle Indicates the pointer to the SPI device handle obtained via SpiOpen.
cfg Indicates the pointer to the configuration parameters.

**Returns:**
Returns **0** if the operation is successful; returns a negative value otherwise.
## SpiTransfer\(\)
```
int32_t SpiTransfer (struct [DevHandle](DevHandle.md) * handle, struct [SpiMsg](SpiMsg.md) * msgs, uint32_t count )
```
**Description:**
Launches a custom transfer to an SPI device.
**Parameters:**















Name


Description


handle Indicates the pointer to the SPI device handle obtained via SpiOpen.
msgs Indicates the pointer to the data to transfer.
count Indicates the length of the message structure array.

**Returns:**
Returns **0** if the operation is successful; returns a negative value otherwise.
**See also:**
[SpiMsg](SpiMsg.md)
## SpiWrite\(\)
```
int32_t SpiWrite (struct [DevHandle](DevHandle.md) * handle, uint8_t * buf, uint32_t len )
```
**Description:**
Writes data of a specified length to an SPI device.
**Parameters:**















Name


Description


handle Indicates the pointer to the SPI device handle obtained via SpiOpen.
buf Indicates the pointer to the data to write.
len Indicates the length of the data to write.

**Returns:**
Returns **0** if the operation is successful; returns a negative value otherwise.
点赞 回复
回帖
支持markdown部分语法 ?