esp32 spi cpu demo

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
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);
}
 
/**/
static esp_err_t spi_write_read(spi_device_handle_t spi, uint8_t *w_data, uint8_t *r_data, int len){
    esp_err_t err;
    err = spi_write(spi, w_data, len);
    if (err!= ESP_OK) return err;
 
    err = spi_read(spi, r_data, len);
 
    return err;
}
  
#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[10] = {0x55, 0x00, 0xB0, 0x99, 0x00, 0x00, 0x02, 0x00, 0x08, 0x00};
    cmd[9] = getLRCx(&cmd[1], 8);
    uint8_t tx_str[10];
  
  
    memcpy(tx_str, cmd, 10);
 
    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, 10);
   
    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, 10);
   ESP_LOGI(TAG, "W25Q64_readStatusReg2=%x", rx_buffer[0]);
    // Never reached.
    err = spi_bus_remove_device(spi_hdl);
 
}

  

posted on   lydstory  阅读(27)  评论(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

统计

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