drv_flash_f7.c 11.9 KB
Newer Older
1 2 3 4 5 6 7 8
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-12-5      SummerGift   first version
9
 * 2019-3-2       jinsheng     add Macro judgment
10
 * 2020-1-6       duminmin     support single bank mode
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 */

#include "board.h"

#ifdef BSP_USING_ON_CHIP_FLASH
#include "drv_config.h"
#include "drv_flash.h"

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

//#define DRV_DEBUG
#define LOG_TAG                "drv.flash"
#include <drv_log.h>
26 27 28 29 30 31 32 33 34 35 36 37
#define ADDR_FLASH_SECTOR_0     ((rt_uint32_t)0x08000000) /* Base address of Sector 0, 32 Kbytes */
#define ADDR_FLASH_SECTOR_1     ((rt_uint32_t)0x08008000) /* Base address of Sector 1, 32 Kbytes */
#define ADDR_FLASH_SECTOR_2     ((rt_uint32_t)0x08010000) /* Base address of Sector 2, 32 Kbytes */
#define ADDR_FLASH_SECTOR_3     ((rt_uint32_t)0x08018000) /* Base address of Sector 3, 32 Kbytes */
#define ADDR_FLASH_SECTOR_4     ((rt_uint32_t)0x08020000) /* Base address of Sector 4, 128 Kbytes */
#define ADDR_FLASH_SECTOR_5     ((rt_uint32_t)0x08040000) /* Base address of Sector 5, 256 Kbytes */
#define ADDR_FLASH_SECTOR_6     ((rt_uint32_t)0x08080000) /* Base address of Sector 6, 256 Kbytes */
#define ADDR_FLASH_SECTOR_7     ((rt_uint32_t)0x080C0000) /* Base address of Sector 7, 256 Kbytes */
#define ADDR_FLASH_SECTOR_8     ((rt_uint32_t)0x08100000) /* Base address of Sector 8, 256 Kbytes */
#define ADDR_FLASH_SECTOR_9     ((rt_uint32_t)0x08140000) /* Base address of Sector 9, 256 Kbytes */
#define ADDR_FLASH_SECTOR_10    ((rt_uint32_t)0x08180000) /* Base address of Sector 10, 256 Kbytes */
#define ADDR_FLASH_SECTOR_11    ((rt_uint32_t)0x081C0000) /* Base address of Sector 11, 256 Kbytes */
38 39 40 41 42 43 44
/**
  * @brief  Gets the sector of a given address
  * @param  None
  * @retval The sector of a given address
  */
static rt_uint32_t GetSector(rt_uint32_t Address)
{
E
ethan.du 已提交
45
    uint32_t sector = 0;
46
  
47
#if defined (FLASH_OPTCR_nDBANK)
E
ethan.du 已提交
48 49
    FLASH_OBProgramInitTypeDef OBInit;
    uint32_t nbank = 0;
50

E
ethan.du 已提交
51 52 53 54
    //get duel bank ability:nDBANK(Bit29)
    HAL_FLASHEx_OBGetConfig(&OBInit);
    nbank = ((OBInit.USERConfig & 0x20000000U) >> 29);
    //1:single bank mode
E
ethan.du 已提交
55
    if (1 == nbank)
E
ethan.du 已提交
56
    {  
E
ethan.du 已提交
57
        if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
E
ethan.du 已提交
58 59 60
        {
            sector = FLASH_SECTOR_0;
        }
E
ethan.du 已提交
61
        else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
E
ethan.du 已提交
62 63 64
        {
            sector = FLASH_SECTOR_1;
        }
E
ethan.du 已提交
65
        else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
E
ethan.du 已提交
66 67 68
        {
            sector = FLASH_SECTOR_2;
        }
E
ethan.du 已提交
69
        else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
E
ethan.du 已提交
70 71 72
        {
            sector = FLASH_SECTOR_3;
        }
E
ethan.du 已提交
73
        else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
E
ethan.du 已提交
74 75 76
        {
            sector = FLASH_SECTOR_4;
        }
E
ethan.du 已提交
77
        else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
E
ethan.du 已提交
78 79 80
        {
            sector = FLASH_SECTOR_5;
        }
E
ethan.du 已提交
81
        else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
E
ethan.du 已提交
82 83 84
        {
            sector = FLASH_SECTOR_6;
        }
E
ethan.du 已提交
85
        else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
E
ethan.du 已提交
86 87 88
        {
            sector = FLASH_SECTOR_7;
        }
E
ethan.du 已提交
89
        else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
E
ethan.du 已提交
90 91 92
        {
            sector = FLASH_SECTOR_8;
        }
E
ethan.du 已提交
93
        else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
E
ethan.du 已提交
94 95 96
        {
            sector = FLASH_SECTOR_9;
        }
E
ethan.du 已提交
97
        else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
E
ethan.du 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110
        {
            sector = FLASH_SECTOR_10;
        }
        else 
        {
            sector = FLASH_SECTOR_11;
        }
    }
    else  //0:dual bank mode
    {
        RT_ASSERT("rtthread doesn't support duel bank mode yet!");
    }
#else //no dual bank ability
E
ethan.du 已提交
111
    if ((Address < ADDR_FLASH_SECTOR_1) && (Address >= ADDR_FLASH_SECTOR_0))
112
    {
E
ethan.du 已提交
113
        sector = FLASH_SECTOR_0;
114
    }
E
ethan.du 已提交
115
    else if ((Address < ADDR_FLASH_SECTOR_2) && (Address >= ADDR_FLASH_SECTOR_1))
116
    {
E
ethan.du 已提交
117
        sector = FLASH_SECTOR_1;
118
    }
E
ethan.du 已提交
119
    else if ((Address < ADDR_FLASH_SECTOR_3) && (Address >= ADDR_FLASH_SECTOR_2))
120
    {
E
ethan.du 已提交
121
        sector = FLASH_SECTOR_2;
122
    }
E
ethan.du 已提交
123
    else if ((Address < ADDR_FLASH_SECTOR_4) && (Address >= ADDR_FLASH_SECTOR_3))
124
    {
E
ethan.du 已提交
125
        sector = FLASH_SECTOR_3;
126
    }
E
ethan.du 已提交
127
    else if ((Address < ADDR_FLASH_SECTOR_5) && (Address >= ADDR_FLASH_SECTOR_4))
128
    {
E
ethan.du 已提交
129
        sector = FLASH_SECTOR_4;
130
    }
E
ethan.du 已提交
131
    else if ((Address < ADDR_FLASH_SECTOR_6) && (Address >= ADDR_FLASH_SECTOR_5))
132
    {
E
ethan.du 已提交
133
        sector = FLASH_SECTOR_5;
134
    }
E
ethan.du 已提交
135
    else if ((Address < ADDR_FLASH_SECTOR_7) && (Address >= ADDR_FLASH_SECTOR_6))
136
    {
E
ethan.du 已提交
137
        sector = FLASH_SECTOR_6;
138
    }
E
ethan.du 已提交
139
    else if ((Address < ADDR_FLASH_SECTOR_8) && (Address >= ADDR_FLASH_SECTOR_7))
140
    {
E
ethan.du 已提交
141
        sector = FLASH_SECTOR_7;
142
    }
E
ethan.du 已提交
143
    else if ((Address < ADDR_FLASH_SECTOR_9) && (Address >= ADDR_FLASH_SECTOR_8))
144
    {
E
ethan.du 已提交
145
        sector = FLASH_SECTOR_8;
146
    }
E
ethan.du 已提交
147
    else if ((Address < ADDR_FLASH_SECTOR_10) && (Address >= ADDR_FLASH_SECTOR_9))
148
    {
E
ethan.du 已提交
149
        sector = FLASH_SECTOR_9;
150
    }
E
ethan.du 已提交
151
    else if ((Address < ADDR_FLASH_SECTOR_11) && (Address >= ADDR_FLASH_SECTOR_10))
152
    {
E
ethan.du 已提交
153
        sector = FLASH_SECTOR_10;
154 155 156
    }
    else 
    {
E
ethan.du 已提交
157
        sector = FLASH_SECTOR_11;
158
    }
J
jinsheng 已提交
159
#endif
E
ethan.du 已提交
160
    return sector;
161 162
}

163

164 165 166 167 168 169 170 171 172 173
/**
 * Read data from flash.
 * @note This operation's units is word.
 *
 * @param addr flash address
 * @param buf buffer to store read data
 * @param size read bytes size
 *
 * @return result
 */
174
int stm32_flash_read(rt_uint32_t addr, rt_uint8_t *buf, size_t size)
175 176 177 178 179
{
    size_t i;

    if ((addr + size) > STM32_FLASH_END_ADDRESS)
    {
180
        LOG_E("read outrange flash size! addr is (0x%p)", (void *)(addr + size));
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
        return -1;
    }

    for (i = 0; i < size; i++, buf++, addr++)
    {
        *buf = *(rt_uint8_t *) addr;
    }

    return size;
}

/**
 * Write data to flash.
 * @note This operation's units is word.
 * @note This operation must after erase. @see flash_erase.
 *
 * @param addr flash address
 * @param buf the write data buffer
 * @param size write bytes size
 *
 * @return result
 */
203
int stm32_flash_write(rt_uint32_t addr, const rt_uint8_t *buf, size_t size)
204 205 206 207 208 209
{
    rt_err_t result      = RT_EOK;
    rt_uint32_t end_addr = addr + size;

    if ((end_addr) > STM32_FLASH_END_ADDRESS)
    {
210
        LOG_E("write outrange flash size! addr is (0x%p)", (void *)(addr + size));
211 212 213 214 215 216 217 218
        return -RT_EINVAL;
    }

    if (size < 1)
    {
        return -RT_EINVAL;
    }

219
    /* Unlock the Flash to enable the flash control register access */
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
    HAL_FLASH_Unlock();
    __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR);

    for (size_t i = 0; i < size; i++, addr++, buf++)
    {
        /* write data to flash */
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, addr, (rt_uint64_t)(*buf)) == HAL_OK)
        {
            if (*(rt_uint8_t *)addr != *buf)
            {
                result = -RT_ERROR;
                break;
            }
        }
        else
        {
            result = -RT_ERROR;
            break;
        }
    }

    HAL_FLASH_Lock();

    if (result != RT_EOK)
    {
        return result;
    }

    return size;
}

/**
 * Erase data on flash.
 * @note This operation is irreversible.
 * @note This operation's units is different which on many chips.
 *
 * @param addr flash address
 * @param size erase bytes size
 *
 * @return result
 */
261
int stm32_flash_erase(rt_uint32_t addr, size_t size)
262 263 264 265 266 267 268
{
    rt_err_t result = RT_EOK;
    rt_uint32_t FirstSector = 0, NbOfSectors = 0;
    rt_uint32_t SECTORError = 0;

    if ((addr + size) > STM32_FLASH_END_ADDRESS)
    {
269
        LOG_E("ERROR: erase outrange flash size! addr is (0x%p)\n", (void *)(addr + size));
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
        return -RT_EINVAL;
    }

    /*Variable used for Erase procedure*/
    FLASH_EraseInitTypeDef EraseInitStruct;

    /* Unlock the Flash to enable the flash control register access */
    HAL_FLASH_Unlock();

    /* Get the 1st sector to erase */
    FirstSector = GetSector(addr);
    /* Get the number of sector to erase from 1st sector*/
    NbOfSectors = GetSector(addr + size) - FirstSector + 1;
    /* Fill EraseInit structure*/
    EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.VoltageRange  = FLASH_VOLTAGE_RANGE_3;
    EraseInitStruct.Sector        = FirstSector;
    EraseInitStruct.NbSectors     = NbOfSectors;

    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError) != HAL_OK)
    {
        result = -RT_ERROR;
        goto __exit;
    }

__exit:
296

297 298 299 300 301 302 303
    HAL_FLASH_Lock();

    if (result != RT_EOK)
    {
        return result;
    }

304
    LOG_D("erase done: addr (0x%p), size %d", (void *)addr, size);
305
    return size;
306 307 308
}

#if defined(PKG_USING_FAL)
309 310 311 312 313 314 315
#define FLASH_SIZE_GRANULARITY_32K      4 * 32 * 1024
#define FLASH_SIZE_GRANULARITY_128K     128 * 1024
#define FLASH_SIZE_GRANULARITY_256K     7 * 256 *1024

#define STM32_FLASH_START_ADRESS_32K    STM32_FLASH_START_ADRESS
#define STM32_FLASH_START_ADRESS_128K   STM32_FLASH_START_ADRESS_32K + FLASH_SIZE_GRANULARITY_32K
#define STM32_FLASH_START_ADRESS_256K   STM32_FLASH_START_ADRESS_128K + FLASH_SIZE_GRANULARITY_128K
316 317 318 319 320 321 322 323 324 325 326 327 328

static int fal_flash_read_32k(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size);
static int fal_flash_read_256k(long offset, rt_uint8_t *buf, size_t size);

static int fal_flash_write_32k(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size);
static int fal_flash_write_256k(long offset, const rt_uint8_t *buf, size_t size);

static int fal_flash_erase_32k(long offset, size_t size);
static int fal_flash_erase_128k(long offset, size_t size);
static int fal_flash_erase_256k(long offset, size_t size);

329 330 331
const struct fal_flash_dev stm32_onchip_flash_32k = { "onchip_flash_32k", STM32_FLASH_START_ADRESS_32K, FLASH_SIZE_GRANULARITY_32K, (32 * 1024), {NULL, fal_flash_read_32k, fal_flash_write_32k, fal_flash_erase_32k} };
const struct fal_flash_dev stm32_onchip_flash_128k = { "onchip_flash_128k", STM32_FLASH_START_ADRESS_128K, FLASH_SIZE_GRANULARITY_128K, (128 * 1024), {NULL, fal_flash_read_128k, fal_flash_write_128k, fal_flash_erase_128k} };
const struct fal_flash_dev stm32_onchip_flash_256k = { "onchip_flash_256k", STM32_FLASH_START_ADRESS_256K, FLASH_SIZE_GRANULARITY_256K, (256 * 1024), {NULL, fal_flash_read_256k, fal_flash_write_256k, fal_flash_erase_256k} };
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

static int fal_flash_read_32k(long offset, rt_uint8_t *buf, size_t size)
{
    return stm32_flash_read(stm32_onchip_flash_32k.addr + offset, buf, size);
}
static int fal_flash_read_128k(long offset, rt_uint8_t *buf, size_t size)
{
    return stm32_flash_read(stm32_onchip_flash_128k.addr + offset, buf, size);
}
static int fal_flash_read_256k(long offset, rt_uint8_t *buf, size_t size)
{
    return stm32_flash_read(stm32_onchip_flash_256k.addr + offset, buf, size);
}

static int fal_flash_write_32k(long offset, const rt_uint8_t *buf, size_t size)
{
    return stm32_flash_write(stm32_onchip_flash_32k.addr + offset, buf, size);
}
static int fal_flash_write_128k(long offset, const rt_uint8_t *buf, size_t size)
{
    return stm32_flash_write(stm32_onchip_flash_128k.addr + offset, buf, size);
}
static int fal_flash_write_256k(long offset, const rt_uint8_t *buf, size_t size)
{
    return stm32_flash_write(stm32_onchip_flash_256k.addr + offset, buf, size);
}

static int fal_flash_erase_32k(long offset, size_t size)
{
    return stm32_flash_erase(stm32_onchip_flash_32k.addr + offset, size);
}
static int fal_flash_erase_128k(long offset, size_t size)
{
    return stm32_flash_erase(stm32_onchip_flash_128k.addr + offset, size);
}
static int fal_flash_erase_256k(long offset, size_t size)
{
    return stm32_flash_erase(stm32_onchip_flash_256k.addr + offset, size);
}

372 373
#endif
#endif /* BSP_USING_ON_CHIP_FLASH */