SPI 模块 API 手册#

概述#

K230 内部集成了三个 SPI 硬件模块,支持片选极性配置和可调时钟速率。通道输出的 I/O 配置可参考 IOMUX 模块。

API 介绍#

SPI 类位于 machine 模块中。

示例代码#

from machine import SPI

# 初始化 SPI,时钟速率 5 MHz,极性 0,相位 0,数据位宽 8 位
spi = SPI(id, baudrate=5000000, polarity=0, phase=0, bits=8)

# 向从设备发送数据
spi.write(buf)

# 发送数据的同时将接收到的数据读入变量
spi.write_readinto(write_buf, read_buf)

# 关闭 SPI
spi.deinit()

构造函数#

spi = machine.SPI(id, baudrate=20, polarity=0, phase=0, bits=8)

参数

  • id: SPI 模块 ID,取值范围为 [0~2](对应 spi.SPI0spi.SPI2)。

  • baudrate: SPI 时钟速率,计算公式为 ( F_{sclk_out} = \frac{F_{ssi_clk}}{BAUDR} )。

  • polarity: 时钟极性。

  • phase: 时钟相位。

  • bits: 数据位宽。

read 方法#

spi.read(nbytes)

读取指定字节数的数据。

参数

  • nbytes: 要读取的字节数。

返回值

返回一个 bytes 对象。

readinto 方法#

spi.readinto(buf)

将数据读取到指定的缓冲区中。

参数

  • buf: 类型为 bytearray 的缓冲区。

返回值

write 方法#

spi.write(buf)

发送数据。

参数

  • buf: 类型为 bytearray 的数据,定义了要发送的数据及其长度。

返回值

write_readinto 方法#

spi.write_readinto(write_buf, read_buf)

发送数据的同时,将接收到的数据读取到指定的变量中。

参数

  • write_buf: 类型为 bytearray 的数据,定义了要发送的数据及其长度。

  • read_buf: 类型为 bytearray 的缓冲区,用于存放接收到的数据。

返回值

deinit 方法#

spi.deinit()

注销 SPI 模块。

参数

返回值

评论列表
条评论
登录