summaryrefslogtreecommitdiff
path: root/bsp/bluetrum/libraries/hal_drivers/drv_flash.c
blob: 37d150a90f61ff689009ecab5a7a5d60f2cc8ccf (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
/*
 * Copyright (c) 2006-2021, Bluetrum Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-16     greedyhao    first version
 */

#include "board.h"
#include "drv_flash.h"

#ifdef BSP_USING_ON_CHIP_FLASH

#if defined(RT_USING_FAL)
#include "fal.h"
#endif

//#define DRV_DEBUG
#define LOG_TAG                "drv.flash"
#include <drv_log.h>

#if defined(RT_USING_FAL)

#define AB32_FLASH_START_ADDRESS    0x00000000
#define AB32_FLASH_SIZE             (1024 * 1024)
#define AB32_FLASH_PAGE_SIZE        (0x1000)

static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_erase(long offset, size_t size);

const struct fal_flash_dev ab32_onchip_flash =
{
    "onchip_flash",
    AB32_FLASH_START_ADDRESS,
    AB32_FLASH_SIZE,
    AB32_FLASH_PAGE_SIZE,
    {NULL, fal_flash_read, fal_flash_write, fal_flash_erase},
    256 * 8
};

static int fal_flash_read(long offset, rt_uint8_t *buf, size_t size)
{
    return os_spiflash_read(buf, offset, size);
}

static int fal_flash_write(long offset, const rt_uint8_t *buf, size_t size)
{
    if (size % 256)
    {
        rt_kprintf("Flash write requires 256 byte alignment\n");
        return -1;
    }
    os_spiflash_program(buf, offset, size);
    return 0;
}

static int fal_flash_erase(long offset, size_t size)
{
    if (size % 4096)
    {
        rt_kprintf("Flash erase requires 4096 byte alignment\n");
        return -1;
    }
    while (size > 0)
    {
        os_spiflash_erase(offset);
        offset += 4096;
        size -= 4096;
    }
    return 0;
}

int fal_ops_test(void)
{
    int result;
    const struct fal_partition *part_dev = fal_partition_find("param");
    uint8_t *data = rt_malloc(256);
    int i;
    int size = 256;
    int addr = 0;

    for (int i = 0; i < 256; i++)
    {
        data[i] = i;
    }

    result = fal_partition_write(part_dev, 0, data, 256);
    if (result >= 0)
    {
        rt_kprintf("Write data success. Start from 0x%08X, size is %ld.\n", addr, size);
        rt_kprintf("Write data: ");
        for (i = 0; i < size; i++)
        {
            rt_kprintf("%d ", data[i]);
        }
        rt_kprintf(".\n");
    }

    rt_memset(data, 0, 256);
    result = fal_partition_read(part_dev, 0, data, 256);
    if (result >= 0)
    {
        rt_kprintf("Read data success. Start from 0x%08X, size is %ld.\n", addr, size);
        rt_kprintf("Read data: ");
        for (i = 0; i < size; i++)
        {
            rt_kprintf("%d ", data[i]);
        }
        rt_kprintf(".\n");
    }

    result = fal_partition_erase(part_dev, 0, 4096);
    if (result >= 0)
    {
        rt_kprintf("Erase data success.\n");
    }

    rt_memset(data, 0, 256);
    result = fal_partition_read(part_dev, 0, data, 256);
    if (result >= 0)
    {
        rt_kprintf("Read data success. Start from 0x%08X, size is %ld.\n", addr, size);
        rt_kprintf("Read data: ");
        for (i = 0; i < size; i++)
        {
            rt_kprintf("%d ", data[i]);
        }
        rt_kprintf(".\n");
    }
    rt_free(data);

    return 0;
}
MSH_CMD_EXPORT(fal_ops_test, "fal_ops_test");

#endif
#endif/* BSP_USING_ON_CHIP_FLASH */