esp32 spi 队列问题

/* 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 35
#define GPIO_MISO 37
#define GPIO_SCLK 36
#define GPIO_CS 34

#elif CONFIG_IDF_TARGET_ESP32S3
#define GPIO_HANDSHAKE 2
#define GPIO_MOSI 35
#define GPIO_MISO 37
#define GPIO_SCLK 36
#define GPIO_CS 34

#endif // CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2

#ifdef CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
#define SENDER_HOST SPI3_HOST

#else
#define SENDER_HOST SPI2_HOST

#endif

#define TAG "W25Q64"
// 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);
    gpio_reset_pin(GPIO_CS);
    gpio_set_direction(GPIO_CS, GPIO_MODE_INPUT);
    gpio_set_level(GPIO_CS, value);
}

void vSetMISO(int value)
{
    //  gpio_set_direction(GPIO_MISO, GPIO_MODE_OUTPUT); //写这个或下一个
    // 1为高电平,0为低电平
    //  gpio_set_level(GPIO_MISO, value);
    gpio_reset_pin(GPIO_MISO);
    gpio_set_direction(GPIO_MISO, GPIO_MODE_OUTPUT);
    gpio_set_level(GPIO_MISO, value);
}

void vSetMOSI(int value)
{
    // gpio_set_direction(GPIO_MOSI, GPIO_MODE_INPUT); //写这个或下一个
    //  1为高电平,0为低电平
    // gpio_set_level(GPIO_MOSI, value);
    gpio_reset_pin(GPIO_MOSI);
    gpio_set_direction(GPIO_MOSI, GPIO_MODE_INPUT);
    gpio_set_level(GPIO_MOSI, value);
}

void vSetSCK(int value)
{
    // gpio_set_direction(GPIO_SCLK, GPIO_MODE_INPUT); //写这个或下一个
    //  1为高电平,0为低电平
    // gpio_set_level(GPIO_SCLK, value);
    gpio_reset_pin(GPIO_SCLK);
    gpio_set_direction(GPIO_SCLK, GPIO_MODE_INPUT);
    gpio_set_level(GPIO_SCLK, value);
}

static spi_device_handle_t spi_init(spi_host_device_t host_id, gpio_num_t miso_io_num, gpio_num_t mosi_io_num, gpio_num_t sclk_io_num, gpio_num_t cs_io_num, int clock_speed_mhz)
{
    spi_bus_config_t buscfg = {
        .miso_io_num = miso_io_num,
        .mosi_io_num = mosi_io_num,
        .sclk_io_num = sclk_io_num,
        .quadwp_io_num = GPIO_NUM_NC,
        .quadhd_io_num = GPIO_NUM_NC,
    };
    ESP_ERROR_CHECK(spi_bus_initialize(host_id, &buscfg, SPI_DMA_DISABLED));

    spi_device_interface_config_t devcfg = {
        .mode = 3,
        .clock_speed_hz = clock_speed_mhz * 1000 * 1000,
        .spics_io_num = cs_io_num,
        .cs_ena_posttrans = 0,
    };
    spi_device_handle_t spi_handle = NULL;
    ESP_ERROR_CHECK(spi_bus_add_device(host_id, &devcfg, &spi_handle));
    return spi_handle;
}

/**/
static esp_err_t spi_write(spi_device_handle_t spi, uint8_t *data, int len)
{
    esp_err_t err;
    err = spi_device_acquire_bus(spi, portMAX_DELAY);
    if (err != ESP_OK)
        return err;

    spi_transaction_t t = {
        .length = len * 8,
        .flags = SPI_TRANS_USE_TXDATA,
        .tx_buffer = data,
    };
    err = spi_device_polling_transmit(spi, &t);

    spi_device_release_bus(spi);
    return err;
}

/**/
static esp_err_t spi_read(spi_device_handle_t spi, uint8_t *data, int len)
{
    spi_transaction_t t = {
        .length = len * 8,
        .flags = SPI_TRANS_USE_RXDATA,
        .rx_buffer = data,
    };

    return spi_device_polling_transmit(spi, &t);
}

 

#define ESP_SPI_FREQ 1
#define ESP_SPI_HOST SPI3_HOST
#define PIN_NUM_MISO 37
#define PIN_NUM_MOSI 35
#define PIN_NUM_CLK 36
#define PIN_NUM_CS 34
// Main application
void app_main(void)
{

    uint8_t cmd[8] = {0x55, 00, 0x5B, 0x00, 0x40, 0x00, 0x00, 0x00};
    uint16_t status, length = 0;
    uint8_t buf[128];

    cmd[7] = getLRCx(&cmd[1], 6);

    uint8_t tx_str[8];

    memcpy(tx_str, cmd, 8);

    spi_device_handle_t spi_hdl;
    spi_hdl = spi_init(ESP_SPI_HOST, PIN_NUM_MISO, PIN_NUM_MOSI, PIN_NUM_CLK, PIN_NUM_CS, ESP_SPI_FREQ);

    vSetSSN(0);
    vTaskDelay(pdMS_TO_TICKS(100));

    esp_err_t err;
    err = spi_write(spi_hdl, tx_str, 8);

    vTaskDelay(pdMS_TO_TICKS(5));
    //  vTaskDelay(100);
    vSetSSN(1);
    vTaskDelay(pdMS_TO_TICKS(10));
    vSetSSN(0);
    vTaskDelay(pdMS_TO_TICKS(100));
    uint8_t rx_buffer[10];

    err = spi_read(spi_hdl, rx_buffer, 8);
    ESP_LOGI(TAG, "W25Q64_readStatusReg2=%x", rx_buffer[0]);
    // Never reached.
    err = spi_bus_remove_device(spi_hdl);
}

  

posted on 2022-09-06 15:05  lydstory  阅读(77)  评论(0)    收藏  举报

导航