aboutsummaryrefslogtreecommitdiff
path: root/src/sample/io_sample/SPI/Flash/spi_flash.c
blob: c1595963869976a391dfc0c789f2c0027af64a14 (plain)
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
/**
*********************************************************************************************************
*               Copyright(c) 2015, Realtek Semiconductor Corporation. All rights reserved.
**********************************************************************************************************
* @file     main.c
* @brief    This file provides demo code of spi write and ead data from GD25Q128E flash.
* @details
* @author   yuan
* @date     2019-07-10
* @version  v1.0
*********************************************************************************************************
*/
#include "spi_flash.h"
#include "io_spi.h"
#include "trace.h"
#include <string.h>


void spi_flash_read_id(uint8_t *pFlashId)
{
    uint8_t send_buf[4] = {SPI_FLASH_JEDEC_ID, 0, 0, 0};
    uint8_t recv_buf[10] = {0};
    uint8_t recv_len = 3;

#if (SPI_MODE_EEPROM == SPI_MODE)
    SPI_SetReadLen(FLASH_SPI, 3);
    SPI_SendBuffer(FLASH_SPI, send_buf, 1);
#elif (SPI_MODE_FULLDUPLEX == SPI_MODE)
    SPI_SendBuffer(FLASH_SPI, send_buf, 4);
    recv_len += 1;
#endif

    uint8_t idx = 0;
    while (recv_len--)
    {
        while (RESET == SPI_GetFlagState(FLASH_SPI, SPI_FLAG_RFNE));
        recv_buf[idx++] = SPI_ReceiveData(FLASH_SPI);
    }

#if (SPI_MODE_EEPROM == SPI_MODE)
    memcpy(pFlashId, recv_buf, 3);
#elif (SPI_MODE_FULLDUPLEX == SPI_MODE)
    memcpy(pFlashId, &recv_buf[1], 3);
#endif
}

void spi_flash_busy_check(void)
{
    uint8_t send_buf[2] = {SPI_FLASH_READ_STATUS_REG_1, 0};
    uint8_t recv_len = 0;
    uint8_t recv_data[2] = {0};

    while (1)
    {
        SPI_SendBuffer(FLASH_SPI, send_buf, 2);
        while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
        recv_len = SPI_GetRxFIFOLen(FLASH_SPI);
        uint8_t i = 0;
        while (recv_len--)
        {
            recv_data[i++] = SPI_ReceiveData(FLASH_SPI);
        }
        i = 0;
        if ((recv_data[1] & 0x01) == 0)
        {
            break;
        }
    }
}

void spi_flash_write_enable(void)
{
    uint8_t len = 0;

    SPI_SendData(FLASH_SPI, SPI_FLASH_WRITE_ENABLE);
    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
    len = SPI_GetRxFIFOLen(FLASH_SPI);
    while (len--)
    {
        SPI_ReceiveData(FLASH_SPI);
    }
}

void spi_flash_status_reg_write(uint8_t vStatus)
{
    uint8_t len = 0;
    uint8_t send_buf[2] = {SPI_FLASH_WRITE_STATUS_REG_1, 0};

    send_buf[1] = vStatus & 0xff;

    /* Enable write */
    spi_flash_write_enable();

    /* Write status register */
    SPI_SendBuffer(FLASH_SPI, send_buf, 2);

    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
    len = SPI_GetRxFIFOLen(FLASH_SPI);
    while (len--)
    {
        SPI_ReceiveData(FLASH_SPI);
    }
    spi_flash_busy_check();
}

void spi_flash_sector_erase(uint32_t vAddress)
{
    uint8_t send_buf[4] = {SPI_FLASH_SECTOR_ERASE, 0, 0, 0};
    uint8_t recv_len = 0;

    send_buf[1] = (vAddress >> 16) & 0xff;
    send_buf[2] = (vAddress >> 8) & 0xff;
    send_buf[3] = vAddress & 0xff;

    /* enable write */
    spi_flash_write_enable();

    /* erase data */
    SPI_SendBuffer(FLASH_SPI, send_buf, 4);

    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
    recv_len = SPI_GetRxFIFOLen(FLASH_SPI);
    while (recv_len--)
    {
        SPI_ReceiveData(FLASH_SPI);
    }
    spi_flash_busy_check();
}

void spi_flash_block_erase(uint32_t vAddress)
{
    uint8_t send_buf[4] = {SPI_FLASH_BLOCK_ERASE_32K, 0, 0, 0};
    uint8_t recv_len = 0;

    send_buf[1] = (vAddress >> 16) & 0xff;
    send_buf[2] = (vAddress >> 8) & 0xff;
    send_buf[3] = vAddress & 0xff;

    /* Enable write */
    spi_flash_write_enable();
    /* erase data */
    SPI_SendBuffer(FLASH_SPI, send_buf, 4);

    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
    recv_len = SPI_GetRxFIFOLen(FLASH_SPI);
    while (recv_len--)
    {
        SPI_ReceiveData(FLASH_SPI);
    }
    spi_flash_busy_check();
}

void spi_flash_chip_erase(void)
{
    uint8_t recv_len = 0;

    /* Enable write */
    spi_flash_write_enable();

    SPI_SendData(FLASH_SPI, SPI_FLASH_CHIP_ERASE);

    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
    recv_len = SPI_GetRxFIFOLen(FLASH_SPI);
    while (recv_len--)
    {
        SPI_ReceiveData(FLASH_SPI);
    }
    spi_flash_busy_check();
}

void spi_flash_page_write(uint32_t vWriteAddr, uint8_t *pBuffer, uint16_t vLength)
{
    uint16_t send_data_len = vLength;
    uint8_t recv_data_len = 0;
    uint8_t send_buf[4] = {SPI_FLASH_PAGE_PROGRAM, 0, 0, 0};
    send_buf[1] = (vWriteAddr >> 16) & 0xff;
    send_buf[2] = (vWriteAddr >> 8) & 0xff;
    send_buf[3] = vWriteAddr & 0xff;

    if (vLength > SPI_FLASH_PAGE_SIZE)
    {
        send_data_len = SPI_FLASH_PAGE_SIZE;
    }

    /* Enable write */
    spi_flash_write_enable();
    SPI_SendBuffer(FLASH_SPI, send_buf, 4);
    SPI_SendBuffer(FLASH_SPI, pBuffer, send_data_len);

    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_BUSY) == SET);
    recv_data_len = SPI_GetRxFIFOLen(FLASH_SPI);
    while (recv_data_len--)
    {
        SPI_ReceiveData(FLASH_SPI);
    }
    spi_flash_busy_check();
}

void spi_flash_buffer_write(uint32_t vWriteAddr, uint8_t *pBuffer, uint16_t vLength)
{
    uint32_t write_addr = vWriteAddr;
    uint8_t *p_write_data = pBuffer;
    uint16_t write_data_len = vLength;

    uint8_t offset = vWriteAddr % SPI_FLASH_PAGE_SIZE;
    uint8_t free_len = SPI_FLASH_PAGE_SIZE - offset;
    uint8_t page_num = vLength / SPI_FLASH_PAGE_SIZE;
    uint8_t remain_len = vLength % SPI_FLASH_PAGE_SIZE;

    if (offset == 0)
    {
        if (page_num == 0) // write_data_len < SPI_FLASH_PAGE_SIZE
        {
            spi_flash_page_write(write_addr, p_write_data, remain_len);
        }
        else // write_data_len > SPI_FLASH_PAGE_SIZE
        {
            while (page_num--)
            {
                spi_flash_page_write(write_addr, p_write_data, SPI_FLASH_PAGE_SIZE);
                write_addr +=  SPI_FLASH_PAGE_SIZE;
                p_write_data += SPI_FLASH_PAGE_SIZE;
            }
            spi_flash_page_write(write_addr, p_write_data, remain_len);
        }
    }
    else // write_addr is not SPI_FLASH_PAGE_SIZE aligned
    {
        if (page_num == 0)
        {
            if (remain_len > free_len) /* (write_data_len + write_addr) > SPI_FLASH_PAGE_SIZE */
            {
                uint8_t temp = remain_len - free_len;
                spi_flash_page_write(write_addr, p_write_data, free_len);
                write_addr +=  free_len;
                p_write_data += free_len;
                spi_flash_page_write(vWriteAddr, p_write_data, temp);
            }
            else
            {
                spi_flash_page_write(write_addr, p_write_data, write_data_len);
            }

        }
        else /* NumByteToWrite > SPI_FLASH_PAGE_SIZE */
        {
            write_data_len -= free_len;
            page_num =  write_data_len / SPI_FLASH_PAGE_SIZE;
            remain_len = write_data_len % SPI_FLASH_PAGE_SIZE;

            spi_flash_page_write(write_addr, p_write_data, free_len);
            write_addr +=  free_len;
            p_write_data += free_len;

            while (page_num--)
            {
                spi_flash_page_write(write_addr, p_write_data, SPI_FLASH_PAGE_SIZE);
                write_addr +=  SPI_FLASH_PAGE_SIZE;
                p_write_data += SPI_FLASH_PAGE_SIZE;
            }

            if (remain_len != 0)
            {
                spi_flash_page_write(write_addr, p_write_data, remain_len);
            }
        }
    }
}

void spi_flash_read(uint8_t vReadCmd, uint32_t vReadAddr, uint8_t *pBuffer, uint16_t vLength)
{
    uint8_t send_buf[10] = {0};
    uint8_t send_len = 0;
    uint16_t recv_len = 0;

    if (SPI_FLASH_READ_DATA == vReadCmd)
    {
        send_len = 4;
    }
    else if (SPI_FLASH_FAST_READ == vReadCmd)
    {
        send_len = 5;
    }
    send_buf[0] = vReadCmd;
    send_buf[1] = (vReadAddr >> 16) & 0xFF;
    send_buf[2] = (vReadAddr >> 8) & 0xFF;
    send_buf[3] = (vReadAddr) & 0xFF;

    SPI_SendBuffer(FLASH_SPI, send_buf, send_len);
    while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_RFNE) == RESET);
    for (uint8_t i = 0; i < send_len; i++)
    {
        SPI_ReceiveData(FLASH_SPI);//dummy read data
    }
    recv_len = vLength;
    while (recv_len--)
    {
        SPI_SendBuffer(FLASH_SPI, &send_buf[9], 1);
        while (SPI_GetFlagState(FLASH_SPI, SPI_FLAG_RFNE) == RESET);
        *pBuffer++ = SPI_ReceiveData(FLASH_SPI);
    }

}