summaryrefslogtreecommitdiff
path: root/bsp/efm32/dev_sflash.c
blob: fa94152886cdaa1ce58a7a71e09750864d99177c (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date         Author      Notes
 * 2011-05-06   onelife     Initial creation by using USART module
 */

/***************************************************************************//**
 * @addtogroup efm32
 * @{
 ******************************************************************************/

/* Includes ------------------------------------------------------------------*/
#include "board.h"
#include "drv_usart.h"
#include "dev_sflash.h"

#if defined(EFM32_USING_SFLASH)
/* Private typedef -------------------------------------------------------------*/
typedef struct
{
    rt_uint8_t code;
    rt_uint32_t address:24;
    rt_uint32_t dummy:8;
} sflash_instruction;

/* Private define --------------------------------------------------------------*/
/* Private macro --------------------------------------------------------------*/
#ifdef EFM32_SFLASH_DEBUG
#define sflash_debug(format,args...)        rt_kprintf(format, ##args)
#else
#define sflash_debug(format,args...)
#endif

/* Private constants -----------------------------------------------------------*/
static rt_uint8_t sflash_inst_code_tbl[] =
{
    /* Instruction only */
    SFLASH_INST_CODE_WREN,
    SFLASH_INST_CODE_WRDI,
    SFLASH_INST_CODE_RDID_L,
    SFLASH_INST_CODE_RDID_S,
    SFLASH_INST_CODE_RDSR,
    SFLASH_INST_CODE_WRSR,
    SFLASH_INST_CODE_BE,
    SFLASH_INST_CODE_DP,
    SFLASH_INST_CODE_RDP,
    /* Instruction and address */
    SFLASH_INST_CODE_WRLR,
    SFLASH_INST_CODE_RDLR,
    SFLASH_INST_CODE_READ,
    SFLASH_INST_CODE_POTP,
    SFLASH_INST_CODE_PP,
    SFLASH_INST_CODE_DIFP,
    SFLASH_INST_CODE_SSE,
    SFLASH_INST_CODE_SE,
    /* Instruction, address and dummy read */
    SFLASH_INST_CODE_READ_F,
    SFLASH_INST_CODE_DOFR,
    SFLASH_INST_CODE_ROTP
};
static rt_uint16_t sflash_data_len_tbl[] =
{
    /* Instruction only */
    SFLASH_REPLY_LEN_WREN,
    SFLASH_REPLY_LEN_WRDI,
    SFLASH_REPLY_LEN_RDID_L,
    SFLASH_REPLY_LEN_RDID_S,
    SFLASH_REPLY_LEN_RDSR,
    SFLASH_REPLY_LEN_WRSR,
    SFLASH_REPLY_LEN_BE,
    SFLASH_REPLY_LEN_DP,
    SFLASH_REPLY_LEN_RDP,
    /* Instruction and address */
    SFLASH_REPLY_LEN_WRLR,
    SFLASH_REPLY_LEN_RDLR,
    SFLASH_REPLY_LEN_READ,
    SFLASH_REPLY_LEN_POTP,
    SFLASH_REPLY_LEN_PP,
    SFLASH_REPLY_LEN_DIFP,
    SFLASH_REPLY_LEN_SSE,
    SFLASH_REPLY_LEN_SE,
    /* Instruction, address and dummy read */
    SFLASH_REPLY_LEN_READ_F,
    SFLASH_REPLY_LEN_DOFR,
    SFLASH_REPLY_LEN_ROTP
};
static rt_bool_t sflash_read_inst_tbl[] =
{
    /* Instruction only */
    false,
    false,
    true,
    true,
    true,
    false,
    false,
    false,
    false,
    /* Instruction and address */
    false,
    true,
    true,
    false,
    false,
    false,
    false,
    false,
    /* Instruction, address and dummy read */
    true,
    true,
    true
};

/* Private variables ------------------------------------------------------------*/
static rt_device_t  sFlash                  = RT_NULL;
static rt_bool_t    sFlashAutoCs            = true;

/* Private function prototypes ---------------------------------------------------*/
/* Private functions ------------------------------------------------------------*/
/******************************************************************//**
 * @brief
 *   Initialize the SPI Flash
 *
 * @details
 *
 * @note
 *
 * @return
 *   Error code
 *********************************************************************/
rt_err_t efm_spiFlash_init(void)
{
    struct efm32_usart_device_t *usart;

    usart = (struct efm32_usart_device_t *)(sFlash->user_data);

#if defined(EFM32_GXXX_DK)
    /* Enable SPI access to Flash */
    DVK_writeRegister(BC_SPI_CFG, 0);
#endif

    do
    {
        /* Find SPI device */
        sFlash = rt_device_find(SFLASH_USING_DEVICE_NAME);
        if (sFlash == RT_NULL)
        {
            sflash_debug("SFLASH: Can't find device %s!\n",
                SFLASH_USING_DEVICE_NAME);
            break;
        }
        sflash_debug("SFLASH: Find device %s\n", SFLASH_USING_DEVICE_NAME);

        /* Config chip slect pin */
        if (!(usart->state & USART_STATE_AUTOCS))
        {
            GPIO_PinModeSet(SFLASH_CS_PORT, SFLASH_CS_PIN, gpioModePushPull, 1);
            sFlashAutoCs = false;
        }

        /* Open SPI device */
        if (sFlash->open(sFlash, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
        {
            break;
        }

        return RT_EOK;
    } while(0);

    sflash_debug("SFLASH: Init failed!\n");
    return -RT_ERROR;
}

/******************************************************************//**
 * @brief
 *   De-initialize the SPI Flash
 *
 * @details
 *
 * @note
 *
 * @return
 *   Error code
 *********************************************************************/
rt_err_t efm_spiFlash_deinit(void)
{
    do
    {
        if (sFlash == RT_NULL)
        {
            sflash_debug("SFLASH: Already deinit!\n");
            break;
        }

        /* Close SPI device */
        if (sFlash->close(sFlash) != RT_EOK)
        {
            break;
        }
        sFlash = RT_NULL;
        sflash_debug("SFLASH: Close device %s\n", SFLASH_USING_DEVICE_NAME);

        return RT_EOK;
    } while(0);

    sflash_debug("SFLASH: Deinit failed!\n");
    return -RT_ERROR;
}

/******************************************************************//**
 * @brief
 *   Set/Clear chip select
 *
 * @details
 *
 * @note
 *
 * @param[in] enable
 *  Chip select pin setting
 *********************************************************************/
static void efm_spiFlash_cs(rt_uint8_t enable)
{
    if (!sFlashAutoCs)
    {
        if (enable)
        {
            GPIO_PinOutClear(SFLASH_CS_PORT, SFLASH_CS_PIN);
        }
        else
        {
            GPIO_PinOutSet(SFLASH_CS_PORT, SFLASH_CS_PIN);
        }
    }
}

/******************************************************************//**
 * @brief
 *   Execute a command
 *
 * @details
 *
 * @note
 *
 * @param[in] command
 *  SPI Flash instruction
 *
 * @param[in] address
 *   Memory address
 *
 * @param[in] buffer
 *   Poniter to the read/write buffer
 *
 * @param[in] size
 *   Buffer size in byte
 *
 * @return
 *   Number of read/written bytes
 *********************************************************************/
rt_uint32_t efm_spiFlash_cmd(
    enum sflash_inst_type_t command,
    rt_uint32_t address,
    rt_uint8_t *buffer,
    rt_uint32_t size)
{
    RT_ASSERT(sFlash != RT_NULL);

    sflash_instruction *inst;
    rt_uint8_t *inst_buf;
    rt_uint8_t inst_len, head_len;
    rt_uint32_t data_len;

    sflash_debug("SFLASH: Inst %x\n", sflash_inst_code_tbl[command]);
    if (sflash_data_len_tbl[command] && !size)
    {
        sflash_debug("SFLASH: No data!\n");
        return 0x00;
    }

    data_len = (sflash_data_len_tbl[command] < size)? \
                sflash_data_len_tbl[command] : size;
    if (data_len && (buffer == RT_NULL))
    {
        sflash_debug("SFLASH: No buffer specified!\n");
        return 0x00;
    }

    /* Allocate memory for write buffer */
    if (sflash_read_inst_tbl[command])
    {
        inst_buf = rt_malloc(6 + 4);
        inst = (sflash_instruction *)(inst_buf + 1);
        head_len = 1;
    }
    else
    {
        inst_buf = rt_malloc(5 + data_len);
        inst = (sflash_instruction *)inst_buf;
        head_len = 0;
    }

    /* Fill in instruction */
    inst->code = sflash_inst_code_tbl[command];
    if (command >= sflash_inst_wrlr)
    {
        /* MSB first */
        inst->address = ((address & 0x000000FF) << 16) | \
                        (address & 0x0000FF00) | \
                        ((address & 0x00FF0000) >> 16);
        if (command >= sflash_inst_read_f)
        {
            inst->dummy = 0x00;
            inst_len = 5;
        }
        else
        {
            inst_len = 4;
        }
    }
    else
    {
        inst_len = 1;
    }
    head_len += inst_len;

    /* Fill in data and send the buffer */
    if (sflash_read_inst_tbl[command])
    {
        rt_off_t skip;

        inst_buf[0] = inst_len;
        *(rt_uint8_t **)(inst_buf + head_len) = buffer;
        if (command == sflash_inst_read)
        {
            skip = SFLASH_SPI_READ_SKIP;
        }
        else
        {
            skip = SFLASH_SPI_COMMAND_SKIP;
        }

        efm_spiFlash_cs(1);
        if (sFlash->read(sFlash, skip, inst_buf, \
            (data_len == size)? data_len - 1 : data_len) == 0)
        {
            sflash_debug("SFLASH: Read failed!\n");
            return 0x00;
        }
        efm_spiFlash_cs(0);
        buffer[data_len] = 0x00;
        sflash_debug("SFLASH: Read %d bytes data to 0x%x\n", data_len, buffer);
    }
    else
    {
        if (data_len)
        {
            rt_memcpy((inst_buf + head_len), buffer, data_len);
        }

        efm_spiFlash_cs(1);
        if (sFlash->write(sFlash, EFM32_NO_DATA, inst_buf, \
            head_len + data_len) == 0)
        {
            sflash_debug("SFLASH: Write failed!\n");
            return 0x00;
        }
        efm_spiFlash_cs(0);
        sflash_debug("SFLASH: Write %d/%d bytes data\n", data_len, \
            head_len + data_len);
    }

    /* Free the buffer */
    rt_free(inst_buf);
    return data_len;
}

/*********************************************************************
*   Export to FINSH
*********************************************************************/
#ifdef RT_USING_FINSH
#include <finsh.h>

void list_sflash(void)
{
    rt_uint8_t buf[4];

    efm_spiFlash_cmd(sflash_inst_rdid_s, EFM32_NO_DATA, buf, sizeof(buf));

    rt_kprintf("    spi flash on %s\n", SFLASH_USING_DEVICE_NAME);
    rt_kprintf(" ------------------------------\n");
    rt_kprintf(" Manufacturer ID:\t%x\n", buf[0]);
    rt_kprintf(" Memory type:\t\t%x\n", buf[1]);
    rt_kprintf(" Memory capacity:\t%x\n", buf[2]);
}
FINSH_FUNCTION_EXPORT(list_sflash, list the SPI Flash.)
#endif

#endif /* defined(EFM32_USING_SFLASH) */

/******************************************************************//**
 * @}
*********************************************************************/