• STM32移植SFUD


    简介

    项目地址:https://github.com/armink/SFUD.git

    SFUD 是一款开源的串行 SPI Flash 通用驱动库。由于现有市面的串行 Flash 种类居多,各个 Flash 的规格及命令存在差异, SFUD 就是为了解决这些 Flash 的差异现状而设计,让我们的产品能够支持不同品牌及规格的 Flash,提高了涉及到 Flash 功能的软件的可重用性及可扩展性,同时也可以规避 Flash 缺货或停产给产品所带来的风险。


    先看效果

    初始化

    sfud_init();
    sfud_flash *sfud_flash = sfud_get_device(SFUD_W25_DEVICE_INDEX);
    sfud_device_init(sfud_flash);
    
    • 1
    • 2
    • 3

    随后可调用读写接口

    sfud_err sfud_read(const sfud_flash *flash, uint32_t addr, size_t size, uint8_t *data);
    sfud_err sfud_erase(const sfud_flash *flash, uint32_t addr, size_t size);
    sfud_err sfud_write(const sfud_flash *flash, uint32_t addr, size_t size, const uint8_t *data);
    sfud_err sfud_erase_write(const sfud_flash *flash, uint32_t addr, size_t size, const uint8_t *data);
    
    • 1
    • 2
    • 3
    • 4

    驱动成功日志

    [SFUD]Start initialize Serial Flash Universal Driver(SFUD) V1.1.0.
    [SFUD]You can get the latest version on https://github.com/armink/SFUD .
    [SFUD]The flash device manufacturer ID is 0xEF, memory type ID is 0x40, capacity ID is 0x17.
    [SFUD]Check SFDP header is OK. The reversion is V1.5, NPN is 0.
    [SFUD]Check JEDEC basic flash parameter header is OK. The table id is 0, reversion is V1.5, length is 16, parameter table pointer is 0x000080.
    [SFUD]JEDEC basic flash parameter table info:
    [SFUD]MSB-LSB  3    2    1    0
    [SFUD][0001] 0xFF 0xF9 0x20 0xE5
    [SFUD][0002] 0x03 0xFF 0xFF 0xFF
    [SFUD][0003] 0x6B 0x08 0xEB 0x44
    [SFUD][0004] 0xBB 0x42 0x3B 0x08
    [SFUD][0005] 0xFF 0xFF 0xFF 0xFE
    [SFUD][0006] 0x00 0x00 0xFF 0xFF
    [SFUD][0007] 0xEB 0x40 0xFF 0xFF
    [SFUD][0008] 0x52 0x0F 0x20 0x0C
    [SFUD][0009] 0x00 0x00 0xD8 0x10
    [SFUD]4 KB Erase is supported throughout the device. Command is 0x20.
    [SFUD]Write granularity is 64 bytes or larger.
    [SFUD]Target flash status register is non-volatile.
    [SFUD]3-Byte only addressing.
    [SFUD]Capacity is 8388608 Bytes.
    [SFUD]Flash device supports 4KB block erase. Command is 0x20.
    [SFUD]Flash device supports 32KB block erase. Command is 0x52.
    [SFUD]Flash device supports 64KB block erase. Command is 0xD8.
    [SFUD]Found a Winbond flash chip. Size is 8388608 bytes.
    [SFUD]Flash device reset success.
    [SFUD]W25Q64JV flash device initialized successfully.
    [SFUD]Found a Winbond flash chip. Size is 8388608 bytes.
    [SFUD]Flash device reset success.
    [SFUD]W25Q64JV flash device initialized successfully.
    
    • 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

    移植

    移植时只需要适配SPI/QSPI接口即可,本文使用的是STM32H750+QSPI HAL库

    将sfud文件夹添加到工程中(源代码和头文件路径)

    ├─sfud
       ├─inc
       ├─port
       └─src
    
    • 1
    • 2
    • 3
    • 4

    修改sfud_cfg.h

    添加flash列表

    enum {
        SFUD_W25_DEVICE_INDEX = 0,
    };
    
    #define SFUD_FLASH_DEVICE_TABLE                                                \
    {                                                                              \
        [SFUD_W25_DEVICE_INDEX] = {.name = "W25Q64JV", .spi.name = "QSPI1"},       \
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    修改sfud_port.c

    源代码如下,其中主要适配SPI的读写

    #include "quadspi.h"
    #include 
    #include 
    
    static char log_buf[256];
    void sfud_log_debug(const char *file, const long line, const char *format, ...);
    void sfud_log_info(const char *format, ...);
    
    static void spi_lock(const sfud_spi *spi)
    {
        __disable_irq();
    }
    
    static void spi_unlock(const sfud_spi *spi)
    {
        __enable_irq();
    }
    
    /**
     * This function can send or send then receive QSPI data.
     */
    sfud_err qspi_send_then_recv(const void *send_buf, size_t send_length, void *recv_buf, size_t recv_length)
    {
        QSPI_CommandTypeDef Cmdhandler;
        unsigned char *ptr = (unsigned char *)send_buf;
        size_t count = 0;
        sfud_err result = SFUD_SUCCESS;
    
        /* get instruction */
        Cmdhandler.Instruction = ptr[0];
        Cmdhandler.InstructionMode = QSPI_INSTRUCTION_1_LINE;
        count++;
    
        /* get address */
        if (send_length > 1)
        {
            if (send_length >= 4)
            {
                /* address size is 3 Byte */
                Cmdhandler.Address = (ptr[1] << 16) | (ptr[2] << 8) | (ptr[3]);
                Cmdhandler.AddressSize = QSPI_ADDRESS_24_BITS;
                count += 3;
            }
            else
            {
                return SFUD_ERR_READ;
            }
            Cmdhandler.AddressMode = QSPI_ADDRESS_1_LINE;
        }
        else
        {
            /* no address stage */
            Cmdhandler.Address = 0;
            Cmdhandler.AddressMode = QSPI_ADDRESS_NONE;
            Cmdhandler.AddressSize = 0;
        }
    
        Cmdhandler.AlternateBytes = 0;
        Cmdhandler.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
        Cmdhandler.AlternateBytesSize = 0;
    
        Cmdhandler.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
        Cmdhandler.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
        Cmdhandler.DdrMode = QSPI_DDR_MODE_DISABLE;
        Cmdhandler.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
    
        if (send_buf && recv_buf)
        {
            /* recv data */
            /* set dummy cycles */
            if (count != send_length)
            {
                Cmdhandler.DummyCycles = (send_length - count) * 8;
            }
            else
            {
                Cmdhandler.DummyCycles = 0;
            }
    
            /* set recv size */
            Cmdhandler.DataMode = QSPI_DATA_1_LINE;
            Cmdhandler.NbData = recv_length;
            HAL_QSPI_Command(&hqspi, &Cmdhandler, 5000);
    
            if (recv_length != 0)
            {
                if (HAL_QSPI_Receive(&hqspi, recv_buf, 5000) != HAL_OK)
                {
                    sfud_log_info("qspi recv data failed(%d)!", hqspi.ErrorCode);
                    hqspi.State = HAL_QSPI_STATE_READY;
                    result = SFUD_ERR_READ;
                }
            }
    
            return result;
        }
        else
        {
            /* send data */
            /* set dummy cycles */
            Cmdhandler.DummyCycles = 0;
    
            /* determine if there is data to send */
            if (send_length - count > 0)
            {
                Cmdhandler.DataMode = QSPI_DATA_1_LINE;
            }
            else
            {
                Cmdhandler.DataMode = QSPI_DATA_NONE;
            }
    
            /* set send buf and send size */
            Cmdhandler.NbData = send_length - count;
            HAL_QSPI_Command(&hqspi, &Cmdhandler, 5000);
    
            if (send_length - count > 0)
            {
                if (HAL_QSPI_Transmit(&hqspi, (uint8_t *)(ptr + count), 5000) != HAL_OK)
                {
                    sfud_log_info("qspi send data failed(%d)!", hqspi.ErrorCode);
                    hqspi.State = HAL_QSPI_STATE_READY;
                    result = SFUD_ERR_WRITE;
                }
            }
    
            return result;
        }
    }
    
    /**
     * SPI write data then read data
     */
    static sfud_err spi_write_read(const sfud_spi *spi, const uint8_t *write_buf, size_t write_size, uint8_t *read_buf,
                                   size_t read_size)
    {
        sfud_err result = SFUD_SUCCESS;
    
        if (write_size && read_size)
        {
            /* read data */
            qspi_send_then_recv(write_buf, write_size, read_buf, read_size);
        }
        else if (write_size)
        {
            /* send data */
            qspi_send_then_recv(write_buf, write_size, NULL, NULL);
        }
    
        return result;
    }
    
    #ifdef SFUD_USING_QSPI
    /**
     * read flash data by QSPI
     */
    static sfud_err qspi_read(const struct __sfud_spi *spi, uint32_t addr, sfud_qspi_read_cmd_format *qspi_read_cmd_format,
                              uint8_t *read_buf, size_t read_size)
    {
        sfud_err result = SFUD_SUCCESS;
        QSPI_CommandTypeDef Cmdhandler;
        extern QSPI_HandleTypeDef hqspi;
    
        /* set cmd struct */
        Cmdhandler.Instruction = qspi_read_cmd_format->instruction;
        if(qspi_read_cmd_format->instruction_lines == 0)
        {
            Cmdhandler.InstructionMode = QSPI_INSTRUCTION_NONE;
        }else if(qspi_read_cmd_format->instruction_lines == 1)
        {
            Cmdhandler.InstructionMode = QSPI_INSTRUCTION_1_LINE;
        }else if(qspi_read_cmd_format->instruction_lines == 2)
        {
            Cmdhandler.InstructionMode = QSPI_INSTRUCTION_2_LINES;
        }else if(qspi_read_cmd_format->instruction_lines == 4)
        {
            Cmdhandler.InstructionMode = QSPI_INSTRUCTION_4_LINES;
        }
        
        Cmdhandler.Address = addr;
        Cmdhandler.AddressSize = QSPI_ADDRESS_24_BITS;
        if(qspi_read_cmd_format->address_lines == 0)
        {
            Cmdhandler.AddressMode = QSPI_ADDRESS_NONE;
        }else if(qspi_read_cmd_format->address_lines == 1)
        {
            Cmdhandler.AddressMode = QSPI_ADDRESS_1_LINE;
        }else if(qspi_read_cmd_format->address_lines == 2)
        {
            Cmdhandler.AddressMode = QSPI_ADDRESS_2_LINES;
        }else if(qspi_read_cmd_format->address_lines == 4)
        {
            Cmdhandler.AddressMode = QSPI_ADDRESS_4_LINES;
        }
    
        Cmdhandler.AlternateBytes = 0;
        Cmdhandler.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
        Cmdhandler.AlternateBytesSize = 0;
    
        Cmdhandler.DummyCycles = qspi_read_cmd_format->dummy_cycles;
    
        Cmdhandler.NbData = read_size;
        if(qspi_read_cmd_format->data_lines == 0)
        {
            Cmdhandler.DataMode = QSPI_DATA_NONE;
        }else if(qspi_read_cmd_format->data_lines == 1)
        {
            Cmdhandler.DataMode = QSPI_DATA_1_LINE;
        }else if(qspi_read_cmd_format->data_lines == 2)
        {
            Cmdhandler.DataMode = QSPI_DATA_2_LINES;
        }else if(qspi_read_cmd_format->data_lines == 4)
        {
            Cmdhandler.DataMode = QSPI_DATA_4_LINES;
        }
    
        Cmdhandler.SIOOMode = QSPI_SIOO_INST_EVERY_CMD;
        Cmdhandler.AlternateByteMode = QSPI_ALTERNATE_BYTES_NONE;
        Cmdhandler.DdrMode = QSPI_DDR_MODE_DISABLE;
        Cmdhandler.DdrHoldHalfCycle = QSPI_DDR_HHC_ANALOG_DELAY;
        HAL_QSPI_Command(&hqspi, &Cmdhandler, 5000);
    
        if (HAL_QSPI_Receive(&hqspi, read_buf, 5000) != HAL_OK)
        {
            sfud_log_info("qspi recv data failed(%d)!", hqspi.ErrorCode);
            hqspi.State = HAL_QSPI_STATE_READY;
            result = SFUD_ERR_READ;
        }
    
        return result;
    }
    #endif /* SFUD_USING_QSPI */
    
    /* about 100 microsecond delay */
    static void retry_delay_100us(void)
    {
        uint32_t delay = 2400;
        while (delay--)
            ;
    }
    
    sfud_err sfud_spi_port_init(sfud_flash *flash)
    {
        sfud_err result = SFUD_SUCCESS;
    
        switch (flash->index)
        {
        case SFUD_W25_DEVICE_INDEX: {
            /* set the interfaces and data */
            flash->spi.wr = spi_write_read;
            flash->spi.qspi_read = qspi_read;
            flash->spi.lock = spi_lock;
            flash->spi.unlock = spi_unlock;
            // flash->spi.user_data = &spi1;
            /* about 100 microsecond delay */
            flash->retry.delay = retry_delay_100us;
            /* adout 60 seconds timeout */
            flash->retry.times = 60 * 10000;
    
            break;
        }
        }
    
        return result;
    }
    
    #include "rtthread.h"
    
    /**
     * This function is print debug info.
     *
     * @param file the file which has call this function
     * @param line the line number which has call this function
     * @param format output format
     * @param ... args
     */
    void sfud_log_debug(const char *file, const long line, const char *format, ...)
    {
        va_list args;
    
        /* args point to the first variable parameter */
        va_start(args, format);
        //rt_kprintf("[SFUD](%s:%ld) ", file, line);
        rt_kprintf("[SFUD]");
        /* must use vprintf to print */
        vsnprintf(log_buf, sizeof(log_buf), format, args);
        rt_kprintf("%s\n", log_buf);
        va_end(args);
    }
    
    /**
     * This function is print routine info.
     *
     * @param format output format
     * @param ... args
     */
    void sfud_log_info(const char *format, ...)
    {
        va_list args;
    
        /* args point to the first variable parameter */
        va_start(args, format);
        rt_kprintf("[SFUD]");
        /* must use vprintf to print */
        vsnprintf(log_buf, sizeof(log_buf), format, args);
        rt_kprintf("%s\n", log_buf);
        va_end(args);
    }
    
    
    • 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
    • 259
    • 260
    • 261
    • 262
    • 263
    • 264
    • 265
    • 266
    • 267
    • 268
    • 269
    • 270
    • 271
    • 272
    • 273
    • 274
    • 275
    • 276
    • 277
    • 278
    • 279
    • 280
    • 281
    • 282
    • 283
    • 284
    • 285
    • 286
    • 287
    • 288
    • 289
    • 290
    • 291
    • 292
    • 293
    • 294
    • 295
    • 296
    • 297
    • 298
    • 299
    • 300
    • 301
    • 302
    • 303
    • 304
    • 305
    • 306
    • 307
    • 308
    • 309
  • 相关阅读:
    Maven依赖管理项目构建工具(保姆级教学---下篇)
    三维模型3DTile格式轻量化压缩处理工具常用几款软件介绍
    1.3操作系统运行环境-拓展知识
    初识生成对抗网络(11)——利用Pytorch搭建WGAN生成手写数字
    Android(Linux)常用的Shell指令
    基于图搜索的规划算法之A*家族(五):D* 算法
    计算机毕业设计django基于python鲜花培育专家系统
    Java 18 新特性:简单Web服务器 jwebserver
    【React】手把手学习React - 元素渲染
    铁轨(Rails, ACM/ICPC CERC 1997, UVa 514)rust解法
  • 原文地址:https://blog.csdn.net/qq_36973838/article/details/136730032