首页 > 其他分享 >esp32 spi

esp32 spi

时间:2022-09-05 18:34:54浏览次数:66  
标签:TARGET esp esp32 spi IDF GPIO include define

/* SPI Slave example, sender (uses SPI master driver)

   This example code is in the Public Domain (or CC0 licensed, at your option.)

   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include <stdio.h>
#include <stdint.h>
#include <stddef.h>
#include <string.h>

#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/semphr.h"
#include "freertos/queue.h"

#include "lwip/sockets.h"
#include "lwip/dns.h"
#include "lwip/netdb.h"
#include "lwip/igmp.h"

#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "soc/rtc_periph.h"
#include "driver/spi_master.h"
#include "esp_log.h"
#include "esp_spi_flash.h"

#include "driver/gpio.h"
#include "esp_intr_alloc.h"

/*
SPI sender (master) example.

This example is supposed to work together with the SPI receiver. It uses the standard SPI pins (MISO, MOSI, SCLK, CS) to
transmit data over in a full-duplex fashion, that is, while the master puts data on the MOSI pin, the slave puts its own
data on the MISO pin.

This example uses one extra pin: GPIO_HANDSHAKE is used as a handshake pin. The slave makes this pin high as soon as it is
ready to receive/send data. This code connects this line to a GPIO interrupt which gives the rdySem semaphore. The main
task waits for this semaphore to be given before queueing a transmission.
*/

/*
Pins in use. The SPI Master can use the GPIO mux, so feel free to change these if needed.
*/
#if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
#define GPIO_HANDSHAKE 2
#define GPIO_MOSI 35
#define GPIO_MISO 37
#define GPIO_SCLK 36
#define GPIO_CS 34

#elif CONFIG_IDF_TARGET_ESP32C3
#define GPIO_HANDSHAKE 3
#define GPIO_MOSI 7
#define GPIO_MISO 2
#define GPIO_SCLK 6
#define GPIO_CS 10

#elif CONFIG_IDF_TARGET_ESP32S3
#define GPIO_HANDSHAKE 2
#define GPIO_MOSI 11
#define GPIO_MISO 13
#define GPIO_SCLK 12
#define GPIO_CS 10

#endif // CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2

#ifdef CONFIG_IDF_TARGET_ESP32
#define SENDER_HOST HSPI_HOST

#else
#define SENDER_HOST SPI2_HOST

#endif

// The semaphore indicating the slave is ready to receive stuff.
static xQueueHandle rdySem;

uint8_t getLRCx(uint8_t *pData, uint16_t length)
{
    uint8_t LCR1 = pData[0];

    for (uint16_t i = 1; i < length; i++)
    {

        LCR1 ^= pData[i];
    }

    LCR1 = ~LCR1;

    return LCR1;
}
/*
This ISR is called when the handshake line goes high.
*/
static void IRAM_ATTR gpio_handshake_isr_handler(void *arg)
{
    // Sometimes due to interference or ringing or something, we get two irqs after eachother. This is solved by
    // looking at the time between interrupts and refusing any interrupt too close to another one.
    static uint32_t lasthandshaketime;
    uint32_t currtime = esp_cpu_get_ccount();
    uint32_t diff = currtime - lasthandshaketime;
    if (diff < 240000)
        return; // ignore everything <1ms after an earlier irq
    lasthandshaketime = currtime;

    // Give the semaphore.
    BaseType_t mustYield = false;
    xSemaphoreGiveFromISR(rdySem, &mustYield);
    if (mustYield)
        portYIELD_FROM_ISR();
}

void vSetSSN(int value)
{
    // GPIO_CS
    gpio_set_direction(GPIO_CS, GPIO_MODE_INPUT); //写这个或下一个
                                                  // 1为高电平,0为低电平
    gpio_set_level(GPIO_CS, value);
}
// Main application
void app_main(void)
{
    esp_err_t ret;
    spi_device_handle_t handle;

    // Configuration for the SPI bus
    spi_bus_config_t buscfg = {
        .mosi_io_num = GPIO_MOSI,
        .miso_io_num = GPIO_MISO,
        .sclk_io_num = GPIO_SCLK,
        .quadwp_io_num = -1,
        .quadhd_io_num = -1};

    // Configuration for the SPI device on the other side of the bus
    spi_device_interface_config_t devcfg = {
        .command_bits = 0,
        .address_bits = 0,
        .dummy_bits = 0,
        .clock_speed_hz = 20000,
        .duty_cycle_pos = 128, // 50% duty cycle
        .mode = 3,
        .spics_io_num = GPIO_CS,
        .cs_ena_posttrans = 3, // Keep the CS low 3 cycles after transaction, to stop slave from missing the last bit when CS has less propagation delay than CLK
        .queue_size = 3};

    // GPIO config for the handshake line.
    gpio_config_t io_conf = {
        .intr_type = GPIO_INTR_POSEDGE,
        .mode = GPIO_MODE_INPUT,
        .pull_up_en = 1,
        .pin_bit_mask = (1 << GPIO_HANDSHAKE)};

    uint8_t cmd[10] = {0x55, 0x00, 0xB0, 0x99, 0x00, 0x00, 0x02, 0x00, 0x08, 0x00};
    cmd[9] = getLRCx(&cmd[1], 8);

    char sendbuf[10];
    char recvbuf[10];
    spi_transaction_t t;
    memset(&t, 0, sizeof(t));
    memcpy(sendbuf, cmd, 10);
    // Create the semaphore.
    rdySem = xSemaphoreCreateBinary();

    // Set up handshake line interrupt.
    gpio_config(&io_conf);
    gpio_install_isr_service(0);
    gpio_set_intr_type(GPIO_HANDSHAKE, GPIO_INTR_POSEDGE);
    gpio_isr_handler_add(GPIO_HANDSHAKE, gpio_handshake_isr_handler, NULL);

    // Initialize the SPI bus and add the device we want to send stuff to.
    ret = spi_bus_initialize(SENDER_HOST, &buscfg, SPI_DMA_CH_AUTO);
    assert(ret == ESP_OK);
    ret = spi_bus_add_device(SENDER_HOST, &devcfg, &handle);
    assert(ret == ESP_OK);

    // Assume the slave is ready for the first transmission: if the slave started up before us, we will not detect
    // positive edge on the handshake line.

    vSetSSN(0);
    /*
    t.length = sizeof(sendbuf) * 8;
    t.tx_buffer = sendbuf;
    t.rx_buffer = recvbuf;
    // Wait for slave to be ready for next byte before sending
    //       xSemaphoreTake(rdySem, portMAX_DELAY); //Wait until slave is ready
    ret = spi_device_transmit(handle, &t);
     */
    memset(&t, 0, sizeof(t));
    t.length = 10 * 8;
    t.rxlength = 10 * 8;
    t.rx_buffer = recvbuf;
    ret = spi_device_polling_transmit(handle, &t);

    printf("Received: %s\n", recvbuf);
    // vSetSSN(1);

    /*

    char sendbuf2[1];
    char recvbuf2[100];
    memset(recvbuf2, 0, sizeof(recvbuf2));
    memset(&t, 0, sizeof(t));
    vTaskDelay(100);
    vSetSSN(0);
    t.length = 8;
    t.tx_buffer = sendbuf2;
    memset(sendbuf2, 0, sizeof(sendbuf2));
    t.rx_buffer = recvbuf2;
    // Wait for slave to be ready for next byte before sending
    //       xSemaphoreTake(rdySem, portMAX_DELAY); //Wait until slave is ready
    ret = spi_device_transmit(handle, &t);

    printf("Received: %s\n", recvbuf2);
    vSetSSN(1);

    */
    // Never reached.
    ret = spi_bus_remove_device(handle);
    assert(ret == ESP_OK);
}

  

标签:TARGET,esp,esp32,spi,IDF,GPIO,include,define
From: https://www.cnblogs.com/hshy/p/16659155.html

相关文章

  • esp32 jia
      /*Loadservercertificate*/  externconstunsignedcharservercert_start[]asm("_binary_servercert_pem_start");  externconstunsignedchar......
  • 接口协议(2) - SPI
    SPI(SerialPeripheralInterface)是一种可以全双工/半双工/单工通信的接口协议,由2(单工)/3(双工)条信号线和1+条(每个从设备1条)片选信号线组成。支持MSB/LSB传输模式,支持......
  • 自旋锁(spin)与互斥量(mutex)
    自旋锁程序在多处理器上运行会因为,多个线程同时进行,而导致丧失语句的原子性。例如读和写的操作是分开的,不能保证同时完成。所以软件不够用硬件来凑,通过硬件实现一条指......
  • delphi 【数字微调编辑框组件TcxSpinEdit】
      此控件仅支持数字数.默认情况下不支持小数点,但支持负数输入.1.设置控件支持小数点输入.properties---valueType:=vtFloat2.隐藏边框右边的微调按钮.pr......
  • esp32 gpio
    mode用于设置gpio的模式GPIO_MODE_INPUT输入GPIO_MODE_OUTPUT输出GPIO_MODE_OUTPUT_OD开漏输出(如果外部或者内部不上拉电阻则无法输出高电平)GPIO_MODE_INPUT_OUTPUT......
  • 2022牛客暑假多校01B[Spirit Circle Observation]
    2022牛客暑假多校01B[SpiritCircleObservation]大致题意给出一个长度为\(n\)的字符串\(s\),求有多少个子串对\((A,B)\),满足\(1.|A|=|B|\)\(2.\overline{A}+1=......
  • Spire.Cloud 私有化部署教程(三) - Windows 系统
    本教程主要介绍如何在Windows系统上实现Spire.Cloud私有化部署。详细步骤如下:一、安装依赖我们的私有部署的依赖有Nodejs、MySQL、Redis和RabbitMQ。请确认服务......
  • SPI协议的数据读写实现(spi_slave)
    SPI协议的数据读写实现(spi_slave)  ......
  • spi的工作方式
    DMA MODE3 SPI四种模式区别-百度文库(baidu.com) ......
  • esp32 自己写模块程序
      esp-idf-w25q64/w25q64.catmaster·nopnop2002/esp-idf-w25q64(github.com)......