ESP32的VSPI和HSPI

说明

SPI共有4根线,MOSI、MISO、CS、CLK,在ESP32中对应规则如下表:
在这里插入图片描述
ESP32共有4个SPI,但是用户能够使用的只有2个SPI,分为VSPI和HSPI。
在这里插入图片描述

引脚接口

在ESP32的数据手册中,说明了VSPI和HSPI对应的引脚:

  • VSPI:
    在这里插入图片描述
  • HSPI:
    在这里插入图片描述
    但是比较麻烦的是,ESP官方不知道犯了什么SB毛病,非弄出来个没有丝毫用途的Strapping 管脚,稍不注意,上电瞬间Strapping管脚电平不对,就没法正常启动,导致但凡是有Strapping管脚功能的引脚,大家都不敢使用
    在SPI中也是这样,VSPI和HSPI默认的引脚中,都有作为Strapping管脚的引脚。我们要格外格外的小心
    在这里插入图片描述
    在这里插入图片描述

结合Strapping管脚,将引脚对应整理如下表

  • VSPI
引脚功能备注
IO23MOSI
IO19MISO
IO18CLK
IO5CSStrapping管脚,上电瞬间必须保证上拉
  • HSPI
引脚功能备注
IO13MOSI
IO12MISOStrapping管脚,上电瞬间必须保证下拉
IO14CLK
IO15CSStrapping管脚,上电瞬间必须保证上拉

更改Auduino框架中SPI默认的引脚

在用arduino框架时,其默认的SPI引脚如下:
在这里插入图片描述

除了使用默认的引脚之外,还可以使用其他方式更改默认引脚。官方给出的示例如下:

/* The ESP32 has four SPi buses, however as of right now only two of
 * them are available to use, HSPI and VSPI. Simply using the SPI API 
 * as illustrated in Arduino examples will use VSPI, leaving HSPI unused.
 * 
 * However if we simply intialise two instance of the SPI class for both
 * of these buses both can be used. However when just using these the Arduino
 * way only will actually be outputting at a time.
 * 
 * Logic analyser capture is in the same folder as this example as
 * "multiple_bus_output.png"
 * 
 * created 30/04/2018 by Alistair Symonds
 */
#include <SPI.h>

// Define ALTERNATE_PINS to use non-standard GPIO pins for SPI bus

#ifdef ALTERNATE_PINS
  #define VSPI_MISO   2
  #define VSPI_MOSI   4
  #define VSPI_SCLK   0
  #define VSPI_SS     33

  #define HSPI_MISO   26
  #define HSPI_MOSI   27
  #define HSPI_SCLK   25
  #define HSPI_SS     32
#else
  #define VSPI_MISO   MISO
  #define VSPI_MOSI   MOSI
  #define VSPI_SCLK   SCK
  #define VSPI_SS     SS

  #define HSPI_MISO   12
  #define HSPI_MOSI   13
  #define HSPI_SCLK   14
  #define HSPI_SS     15
#endif

#if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
#define VSPI FSPI
#endif

static const int spiClk = 1000000; // 1 MHz

//uninitalised pointers to SPI objects
SPIClass * vspi = NULL;
SPIClass * hspi = NULL;

void setup() {
  //initialise two instances of the SPIClass attached to VSPI and HSPI respectively
  vspi = new SPIClass(VSPI);
  hspi = new SPIClass(HSPI);
  
  //clock miso mosi ss

#ifndef ALTERNATE_PINS
  //initialise vspi with default pins
  //SCLK = 18, MISO = 19, MOSI = 23, SS = 5
  vspi->begin();
#else
  //alternatively route through GPIO pins of your choice
  vspi->begin(VSPI_SCLK, VSPI_MISO, VSPI_MOSI, VSPI_SS); //SCLK, MISO, MOSI, SS
#endif

#ifndef ALTERNATE_PINS
  //initialise hspi with default pins
  //SCLK = 14, MISO = 12, MOSI = 13, SS = 15
  hspi->begin();
#else
  //alternatively route through GPIO pins
  hspi->begin(HSPI_SCLK, HSPI_MISO, HSPI_MOSI, HSPI_SS); //SCLK, MISO, MOSI, SS
#endif

  //set up slave select pins as outputs as the Arduino API
  //doesn't handle automatically pulling SS low
  pinMode(vspi->pinSS(), OUTPUT); //VSPI SS
  pinMode(hspi->pinSS(), OUTPUT); //HSPI SS

}

// the loop function runs over and over again until power down or reset
void loop() {
  //use the SPI buses
  spiCommand(vspi, 0b01010101); // junk data to illustrate usage
  spiCommand(hspi, 0b11001100);
  delay(100);
}

void spiCommand(SPIClass *spi, byte data) {
  //use it as you would the regular arduino SPI API
  spi->beginTransaction(SPISettings(spiClk, MSBFIRST, SPI_MODE0));
  digitalWrite(spi->pinSS(), LOW); //pull SS slow to prep other end for transfer
  spi->transfer(data);
  digitalWrite(spi->pinSS(), HIGH); //pull ss high to signify end of data transfer
  spi->endTransaction();
}

实际的两块arduino设备进行spi通信代码如下:
arduino进行spi通信

参考:
ESP32官网spi代码
Arduino spi帮助

  • 6
    点赞
  • 32
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
ESP32SPI口可通过编程来定义和配置。以下是一种常见的定义和配置SPI口的方法: 首先,你需要包含ESP32SPI库头文件,例如: #include "driver/spi_master.h" 然后,定义SPI总线配置结构体,例如: spi_bus_config_t bus_config = { .mosi_io_num = GPIO_NUM_23, // 设置MOSI引脚 .miso_io_num = GPIO_NUM_19, // 设置MISO引脚 .sclk_io_num = GPIO_NUM_18, // 设置时钟引脚 .quadwp_io_num = -1, // 不使用四线串行接口 .quadhd_io_num = -1, // 不使用四线串行接口 .max_transfer_sz = 0, // 传输的最大字节数,0表示无限制 }; 接下来,初始化SPI总线,例如: esp_err_t ret = spi_bus_initialize(VSPI_HOST, &bus_config, 1); // 初始化SPI总线 assert(ret == ESP_OK); // 确认初始化成功 接着,定义SPI设备配置结构体,例如: spi_device_interface_config_t dev_config = { .mode = 0, // SPI模式,0表示模式0(CPOL=0,CPHA=0) .clock_speed_hz = 1000000, // SPI时钟频率1MHz .spics_io_num = GPIO_NUM_5, // 设置片选引脚 .queue_size = 7, // SPI传输队列的长度 }; 然后,添加SPI设备,例如: spi_device_handle_t spi_handle; // 定义SPI设备句柄 ret = spi_bus_add_device(VSPI_HOST, &dev_config, &spi_handle); // 添加SPI设备 assert(ret == ESP_OK); // 确认添加成功 最后,你可以使用spi_device_transmit()等函数来进行SPI数据的传输和通信。 以上就是一个简单的定义和配置ESP32SPI口的过程。当然,根据具体应用的需求,你可能还需要根据需要进行其他的SPI参数配置。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天城寺电子

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值