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)
74 memcpy_P(buf, (const void *)(uint16_t)(idx * blk->blk_size + offset), size);
78 static size_t avr_flash_writeDirect(struct KBlock *blk, block_idx_t idx, const void *_buf, size_t offset, size_t size)
81 ASSERT(size == blk->blk_size);
83 uint32_t page_addr = idx * blk->blk_size;
84 uint32_t addr = idx * blk->blk_size;
85 const uint8_t *buf = (const uint8_t *)_buf;
87 /* Wait while the SPM instruction is busy. */
90 /* Fill the temporary buffer of the AVR */
93 uint16_t data = ((*buf + 1) << 8) | *buf;
94 ATOMIC(boot_page_fill(addr, data));
104 ATOMIC(boot_page_erase(page_addr));
106 /* Wait until the memory is erased. */
107 boot_spm_busy_wait();
109 /* Store buffer in flash page. */
110 ATOMIC(boot_page_write(page_addr));
112 /* Wait while the SPM instruction is busy. */
113 boot_spm_busy_wait();
116 * Reenable RWW-section again. We need this if we want to jump back
117 * to the application after bootloading.
119 ATOMIC(boot_rww_enable());
121 return blk->blk_size;
124 static int avr_flash_dummy(UNUSED_ARG(struct KBlock, *blk))
129 static const KBlockVTable flash_avr_buffered_vt =
131 .readDirect = avr_flash_readDirect,
132 .writeDirect = avr_flash_writeDirect,
134 .readBuf = kblock_swReadBuf,
135 .writeBuf = kblock_swWriteBuf,
136 .load = kblock_swLoad,
137 .store = kblock_swStore,
139 .error = avr_flash_dummy,
140 .clearerr = (kblock_clearerr_t)avr_flash_dummy,
143 static const KBlockVTable flash_avr_unbuffered_vt =
145 .readDirect = avr_flash_readDirect,
146 .writeDirect = avr_flash_writeDirect,
148 .error = avr_flash_dummy,
149 .clearerr = (kblock_clearerr_t)avr_flash_dummy,
152 static uint8_t flash_buf[SPM_PAGESIZE];
154 static void common_init(Flash *fls)
156 memset(fls, 0, sizeof(*fls));
157 DB(fls->blk.priv.type = KBT_FLASH);
159 fls->blk.blk_size = SPM_PAGESIZE;
160 fls->blk.blk_cnt = (FLASHEND + 1) / SPM_PAGESIZE;
164 void flash_hw_init(Flash *fls, UNUSED_ARG(int, flags))
167 fls->blk.priv.vt = &flash_avr_buffered_vt;
168 fls->blk.priv.flags |= KB_BUFFERED | KB_PARTIAL_WRITE;
169 fls->blk.priv.buf = flash_buf;
172 void flash_hw_initUnbuffered(Flash *fls, UNUSED_ARG(int, flags))
175 fls->blk.priv.vt = &flash_avr_unbuffered_vt;