esp32 spi 队列问题
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | /* 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); } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
2020-09-06 mylocale
2020-09-06 物证比对
2019-09-06 判断Xen虚拟机随想
2019-09-06 安全工具箱
2019-09-06 安全sysmon