4 * This file is part of BeRTOS.
6 * Bertos is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * As a special exception, you may use this file as part of a free software
21 * library without restriction. Specifically, if other files instantiate
22 * templates or use macros or inline functions from this file, or you compile
23 * this file and link it with other files to produce an executable, this
24 * file does not by itself cause the resulting executable to be covered by
25 * the GNU General Public License. This exception does not however
26 * invalidate any other reasons why the executable file might be covered by
27 * the GNU General Public License.
29 * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
33 * \brief STM32F103xx internal flash memory driver.
35 * \author Daniele Basile <asterix@develer.com>
38 #include "flash_stm32.h"
40 #include "cfg/cfg_emb_flash.h"
42 // Define log settings for cfg/log.h
43 #define LOG_LEVEL CONFIG_FLASH_EMB_LOG_LEVEL
44 #define LOG_FORMAT CONFIG_FLASH_EMB_LOG_FORMAT
47 #include <drv/timer.h>
48 #include <drv/flash.h>
50 #include <cpu/power.h>
51 #include <cpu/detect.h>
57 #define EMB_FLASH ((struct stm32_flash*)FLASH_R_BASE)
64 static bool flash_wait(struct KBlock *blk)
66 Flash *fls = FLASH_CAST(blk);
67 ticks_t start = timer_clock();
70 if (!(EMB_FLASH->SR & FLASH_FLAG_BSY))
73 if (EMB_FLASH->SR & FLASH_FLAG_PGERR)
75 fls->hw->status |= FLASH_NOT_ERASED;
76 LOG_ERR("flash not erased..\n");
80 if (EMB_FLASH->SR & FLASH_FLAG_WRPRTERR)
82 fls->hw->status |= FLASH_WR_PROTECT;
83 LOG_ERR("wr protect..\n");
87 if (timer_clock() - start > ms_to_ticks(CONFIG_FLASH_WR_TIMEOUT))
89 fls->hw->status |= FLASH_WR_TIMEOUT;
90 LOG_ERR("Timeout..\n");
100 static bool stm32_erasePage(struct KBlock *blk, uint32_t page_add)
103 EMB_FLASH->CR |= CR_PER_SET;
104 EMB_FLASH->AR = page_add;
105 EMB_FLASH->CR |= CR_STRT_SET;
107 if (!flash_wait(blk))
110 EMB_FLASH->CR &= CR_PER_RESET;
115 static bool stm32_eraseAll(struct KBlock *blk)
117 EMB_FLASH->CR |= CR_MER_SET;
118 EMB_FLASH->CR |= CR_STRT_SET;
120 if (!flash_wait(blk))
123 EMB_FLASH->CR &= CR_MER_RESET;
128 static int stm32_flash_error(struct KBlock *blk)
130 Flash *fls = FLASH_CAST(blk);
131 return fls->hw->status;
134 static void stm32_flash_clearerror(struct KBlock *blk)
136 Flash *fls = FLASH_CAST(blk);
140 static size_t stm32_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
143 ASSERT(size == blk->blk_size);
145 memcpy(buf, (void *)(idx * blk->blk_size), size);
150 INLINE bool stm32_writeWord(struct KBlock *blk, uint32_t addr, uint16_t data)
154 EMB_FLASH->CR |= CR_PG_SET;
156 *(reg16_t *)addr = data;
158 if (!flash_wait(blk))
161 EMB_FLASH->CR &= CR_PG_RESET;
166 static size_t stm32_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
169 ASSERT(size == blk->blk_size);
171 if (!stm32_erasePage(blk, (idx * blk->blk_size)))
174 uint32_t addr = idx * blk->blk_size;
175 const uint8_t *buf = (const uint8_t *)_buf;
177 kprintf("idx[%ld],size[%u]\n", idx, size);
180 uint16_t data = (*(buf + 1) << 8) | *buf;
181 if (!stm32_writeWord(blk, addr, data))
189 return blk->blk_size;
192 static const KBlockVTable flash_stm32_buffered_vt =
194 .readDirect = stm32_flash_readDirect,
195 .writeDirect = stm32_flash_writeDirect,
197 .readBuf = kblock_swReadBuf,
198 .writeBuf = kblock_swWriteBuf,
199 .load = kblock_swLoad,
200 .store = kblock_swStore,
202 .close = kblock_swClose,
204 .error = stm32_flash_error,
205 .clearerr = stm32_flash_clearerror,
208 static const KBlockVTable flash_stm32_unbuffered_vt =
210 .readDirect = stm32_flash_readDirect,
211 .writeDirect = stm32_flash_writeDirect,
213 .close = kblock_swClose,
215 .error = stm32_flash_error,
216 .clearerr = stm32_flash_clearerror,
219 static struct FlashHardware flash_stm32_hw;
220 static uint8_t flash_buf[FLASH_PAGE_SIZE];
222 static void common_init(Flash *fls)
224 memset(fls, 0, sizeof(*fls));
225 DB(fls->blk.priv.type = KBT_FLASH);
227 fls->hw = &flash_stm32_hw;
229 fls->blk.blk_size = FLASH_PAGE_SIZE;
230 fls->blk.blk_cnt = (F_SIZE * 1024) / FLASH_PAGE_SIZE;
232 /* Unlock flash memory for the FPEC Access */
233 EMB_FLASH->KEYR = FLASH_KEY1;
234 EMB_FLASH->KEYR = FLASH_KEY2;
238 void flash_hw_init(Flash *fls)
241 fls->blk.priv.vt = &flash_stm32_buffered_vt;
242 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
243 fls->blk.priv.buf = flash_buf;
245 /* Load the first block in the cache */
247 memcpy(fls->blk.priv.buf, a, fls->blk.blk_size);
250 void flash_hw_initUnbuffered(Flash *fls)
253 fls->blk.priv.vt = &flash_stm32_unbuffered_vt;