Driver: SPI Master¶
1 功能概述¶
该sample演示了SPI作为Master对开发板上的Flash进行读写操作。
2 环境要求¶
PAN1080 EVB 一块
USB-TypeC 线一条(用于供电和查看串口打印 Log)
硬件接线:
使用 USB 线,将 PC USB 与 EVB USB-TypeC(USB->UART)相连
根据 EVB 核心板芯片的封装不同,使用杜邦线将 EVB 底板上的:
TX0 与 P00 相连, RX0 与 P01 相连(若 EVB 板芯片为 QFN32 或 LQFP64 封装)
TX0 与 P30 相连, RX0 与 P31 相连(若 EVB 板芯片为 QFN48 封装)
PC 软件: 串口调试助手(UartAssist)或终端工具(SecureCRT),波特率921600
3 编译和烧录¶
例程位置:zephyr\samples_panchip\drivers\spi_master
使用 ZAL 工具可以对其进行编译、烧录、打开 VS Code 调试等操作。关于 ZAL 工具的详细介绍请参考:Zephyr APP Launcher 工具介绍。
4 演示说明¶
连接SPI引脚到Flash模块(开发板上进行跳线):
==注:请注意开发板上引脚共用问题,不要将 SPI 使用的四个引脚同时接至其他模块==
对于 pan108xxa1_evb / pan108xxb1_evb / pan108xxb5_evb 等 EVB 板:
引脚
功能
P02
CS
P03
SCK
P30
MOSI
P31
MISO
对于 pan108xxa3_evb 板:
引脚
功能
P02
CS
P03
SCK
P43
MOSI
P42
MISO
此例程支持DMA,选择
prj_dma.conf
编译即使用DMA的方式进行SPI通信。Sample执行流程:
1.擦除0地址开始的一个page 2.读取一个page数据,检查是否擦除成功 3.从0地址开始写入256byte的随机数据 4.从0地址开始读取256byte数据并且和之前写入的数据做比较,检查是否写入成功
程序正确执行时,日志界面如下:
5 开发说明¶
5.1 启用SPI模块¶
在prj.conf文件中添加“CONFIG_SPI=y”启用SPI模块。如需使用DMA则需要添加“CONFIG_SPI_PANCHIP_DMA=y“。
CONFIG_SPI=y
CONFIG_SPI_PANCHIP_DMA=y
5.2 初始化SPI¶
const struct device *spi = DEVICE_DT_GET(DT_NODELABEL(spi0));
if (!device_is_ready(spi)) {
printk("SPI device %s is not ready\n", spi->name);
return;
}
5.3 SPI配置¶
可根据实际情况配置即可,需要注意的是参数frequency指的是时钟的分频系数,并非实际的时钟数。例如当前系统外设时钟为16MHz,frequency配置为4,则SPI的时钟速率为16/4=4MHz。
5.4 写操作¶
实现向slave写数据
struct spi_buf bufs[] = {
{
.buf = data,
.len = len
},
};
struct spi_buf_set tx = {
.buffers = bufs,
.count = 1
};
/**
* @brief Write the specified amount of data from the SPI driver.
*
* @note This function is synchronous.
*
* @note This function is an helper function calling spi_transceive.
*
* @param dev Pointer to the device structure for the driver instance
* @param config Pointer to a valid spi_config structure instance.
* Pointer-comparison may be used to detect changes from
* previous operations.
* @param tx_bufs Buffer array where data to be sent originates from.
*
* @retval 0 If successful.
* @retval -errno Negative errno code on failure.
*/
spi_write(spi, spi_cfg, &tx);
5.5 读操作¶
实现从slave读取数据
struct spi_buf bufs[] = {
{
.buf = data,
.len = len
},
};
struct spi_buf_set rx = {
.buffers = bufs,
.count = 1
};
/**
* @brief Read the specified amount of data from the SPI driver.
*
* @note This function is synchronous.
*
* @note This function is an helper function calling spi_transceive.
*
* @param dev Pointer to the device structure for the driver instance
* @param config Pointer to a valid spi_config structure instance.
* Pointer-comparison may be used to detect changes from
* previous operations.
* @param rx_bufs Buffer array where data to be read will be written to.
*
* @retval 0 If successful.
* @retval -errno Negative errno code on failure.
*/
spi_read(spi, spi_cfg, &rx);
5.6 读写操作¶
实现向slave发送数据的同时接收来自slave的数据
struct spi_buf bufs_tx[] = {
{
.buf = data,
.len = len
},
};
struct spi_buf bufs_rx[] = {
{
.buf = data,
.len = len
},
};
struct spi_buf_set tx = {
.buffers = bufs_tx,
.count = 1
};
struct spi_buf_set rx = {
.buffers = bufs_rx,
.count = 1
};
/**
* @brief Read/write the specified amount of data from the SPI driver.
*
* @note This function is synchronous.
*
* @param dev Pointer to the device structure for the driver instance
* @param config Pointer to a valid spi_config structure instance.
* Pointer-comparison may be used to detect changes from
* previous operations.
* @param tx_bufs Buffer array where data to be sent originates from,
* or NULL if none.
* @param rx_bufs Buffer array where data to be read will be written to,
* or NULL if none.
*
* @retval frames Positive number of frames received in slave mode.
* @retval 0 If successful in master mode.
* @retval -errno Negative errno code on failure.
*/
spi_transceive(spi, spi_cfg, &tx, &rx);
6 RAM/Flash资源使用情况¶
Memory region Used Size Region Size %age Used
FLASH: 23628 B 256 KB 9.01%
SRAM: 4808 B 64 KB 7.34%