esp32 spi 标准sender

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
/* 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
 
 #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);
}
 
void vSetMISO(int value)
{
    gpio_set_direction(GPIO_MISO, GPIO_MODE_OUTPUT); //写这个或下一个
                                                  // 1为高电平,0为低电平
    gpio_set_level(GPIO_MISO, 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 = 500000,
        .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];
 
    memcpy(sendbuf, cmd, 10);
  
    // 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);
   
 
    // vSetSSN(0);
    //   vSetMOSI(1);
    //    vSetMISO(1);
    // vSetSCK(1);
 
    vSetSSN(0);
    vTaskDelay(pdMS_TO_TICKS(100));
  
    spi_transaction_t SPITransaction;
    memset(&SPITransaction, 0, sizeof(spi_transaction_t));
    SPITransaction.length = 10 * 8;
    SPITransaction.tx_buffer = sendbuf;
    SPITransaction.rx_buffer = recvbuf;
    ret = spi_device_transmit(handle, &SPITransaction);
    assert(ret == ESP_OK);
  
        ESP_LOGI(TAG, "W25Q64_readStatusReg2=%x %x %x", recvbuf[0], recvbuf[1], recvbuf[2]);
    vTaskDelay(pdMS_TO_TICKS(5));
    //  vTaskDelay(100);
    vSetSSN(1);
    vTaskDelay(pdMS_TO_TICKS(10));
    vSetSSN(0);
    vTaskDelay(pdMS_TO_TICKS(100));
 
    uint8_t cmd2[1] = {0x00};
    char sendbuf2[1];
    char recvbuf2[1];
    memcpy(sendbuf2, cmd2, 1);
    /*
   spi_transaction_t SPITransaction2;
   memset(&SPITransaction2, 0, sizeof(spi_transaction_t));
   SPITransaction2.length = 1 * 8;
   SPITransaction2.tx_buffer = sendbuf2;
   SPITransaction2.rx_buffer = recvbuf2;
   ret = spi_device_transmit(dev._SPIHandle, &SPITransaction2);
   */
 
    spi_transaction_t recv2;
    memset(&recv2, 0, sizeof(recv2));
    recv2.length = 1 * 8;
    recv2.rxlength = 1 * 8;
    recv2.rx_buffer = recvbuf2;
    ret = spi_device_polling_transmit(handle, &recv2);
   
    assert(ret == ESP_OK);
  
        ESP_LOGI(TAG, "W25Q64_readStatusReg2=%x", recvbuf2[0]);
    // Never reached.
    ret = spi_bus_remove_device(handle);
    assert(ret == ESP_OK);
}

  

posted on   lydstory  阅读(51)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用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

导航

< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示