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 2007 Develer S.r.l. (http://www.develer.com/)
33 * \brief Self programming routines.
35 * \author Francesco Sacchi <batt@develer.com>
36 * \author Daniele Basile <asterix@develer.com>
38 * This module implements a kfile-like access for Atmel avr internal flash.
39 * Internal flash writing access is controlled by BOOTSZ fuses, check
40 * datasheet for details.
43 #include "flash_avr.h"
45 #include "cfg/cfg_emb_flash.h"
47 #include <cfg/macros.h> // MIN()
48 #include <cfg/compiler.h>
49 #include <cfg/debug.h>
52 // Define log settings for cfg/log.h
53 #define LOG_LEVEL CONFIG_FLASH_EMB_LOG_LEVEL
54 #define LOG_FORMAT CONFIG_FLASH_EMB_LOG_FORMAT
58 #include <drv/flash.h>
61 #include <io/kfile_block.h>
62 #include <io/kblock.h>
66 #include <avr/pgmspace.h>
72 static size_t avr_flash_readDirect(struct KBlock *blk, block_idx_t idx, void *buf, size_t offset, size_t size)
75 ASSERT(size == blk->blk_size);
77 memcpy_P(buf, (const void *)(uint16_t)(idx * blk->blk_size), size);
81 static size_t avr_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
84 ASSERT(size == blk->blk_size);
86 uint32_t page_addr = idx * blk->blk_size;
87 uint32_t addr = idx * blk->blk_size;
88 const uint8_t *buf = (const uint8_t *)_buf;
90 /* Wait while the SPM instruction is busy. */
93 /* Fill the temporary buffer of the AVR */
96 uint16_t data = ((*buf + 1) << 8) | *buf;
97 ATOMIC(boot_page_fill(addr, data));
107 ATOMIC(boot_page_erase(page_addr));
109 /* Wait until the memory is erased. */
110 boot_spm_busy_wait();
112 /* Store buffer in flash page. */
113 ATOMIC(boot_page_write(page_addr));
115 /* Wait while the SPM instruction is busy. */
116 boot_spm_busy_wait();
119 * Reenable RWW-section again. We need this if we want to jump back
120 * to the application after bootloading.
122 ATOMIC(boot_rww_enable());
124 return blk->blk_size;
127 static int avr_flash_dummy(UNUSED_ARG(struct KBlock, *blk))
132 static const KBlockVTable flash_avr_buffered_vt =
134 .readDirect = avr_flash_readDirect,
135 .writeDirect = avr_flash_writeDirect,
137 .readBuf = kblock_swReadBuf,
138 .writeBuf = kblock_swWriteBuf,
139 .load = kblock_swLoad,
140 .store = kblock_swStore,
142 .error = avr_flash_dummy,
143 .clearerr = (kblock_clearerr_t)avr_flash_dummy,
146 static const KBlockVTable flash_avr_unbuffered_vt =
148 .readDirect = avr_flash_readDirect,
149 .writeDirect = avr_flash_writeDirect,
151 .error = avr_flash_dummy,
152 .clearerr = (kblock_clearerr_t)avr_flash_dummy,
155 static uint8_t flash_buf[SPM_PAGESIZE];
157 static void common_init(Flash *fls)
159 memset(fls, 0, sizeof(*fls));
160 DB(fls->blk.priv.type = KBT_FLASH);
162 fls->blk.blk_size = SPM_PAGESIZE;
163 fls->blk.blk_cnt = (FLASHEND + 1) / SPM_PAGESIZE;
167 void flash_hw_init(Flash *fls)
170 fls->blk.priv.vt = &flash_avr_buffered_vt;
171 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
172 fls->blk.priv.buf = flash_buf;
175 void flash_hw_initUnbuffered(Flash *fls)
178 fls->blk.priv.vt = &flash_avr_unbuffered_vt;