2 * Copyright (c) 2019 Tilman Sauerbeck (tilman at code-monkey de)
4 * Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 * "Software"), to deal in the Software without restriction, including
7 * without limitation the rights to use, copy, modify, merge, publish,
8 * distribute, sublicense, and/or sell copies of the Software, and to
9 * permit persons to whom the Software is furnished to do so, subject to
10 * the following conditions:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 type Reg8 = register::Register<u8>;
28 const FTFA_BASE: u32 = 0x40020000;
30 const FTFA_FSTAT : u32 = FTFA_BASE + 0x00;
31 const FTFA_FCCOB3: u32 = FTFA_BASE + 0x04;
32 const FTFA_FCCOB2: u32 = FTFA_BASE + 0x05;
33 const FTFA_FCCOB1: u32 = FTFA_BASE + 0x06;
34 const FTFA_FCCOB0: u32 = FTFA_BASE + 0x07;
35 const FTFA_FCCOB7: u32 = FTFA_BASE + 0x08;
36 const FTFA_FCCOB6: u32 = FTFA_BASE + 0x09;
37 const FTFA_FCCOB5: u32 = FTFA_BASE + 0x0a;
38 const FTFA_FCCOB4: u32 = FTFA_BASE + 0x0b;
40 const FTFA_FSTAT_MGSTAT0 : u8 = 1 << 0;
41 const FTFA_FSTAT_FPVIOL : u8 = 1 << 4;
42 const FTFA_FSTAT_ACCERR : u8 = 1 << 5;
43 const FTFA_FSTAT_RDCOLERR: u8 = 1 << 6;
45 const FCMD_RD1SEC : u8 = 0x01;
46 const FCMD_PGM4 : u8 = 0x06;
47 const FCMD_ERSSCR : u8 = 0x09;
49 pub const SECTOR_SIZE : usize = 1024;
52 fn flash_run_command() -> u8;
55 // The hardware refuses to run another command (or even take the new command's
56 // parameters) until the errors reported by a previous command have been
57 // acknowledged (see KL26P121M48SF4RM.pdf p. 445).
58 fn prepare_command() {
59 let mut fstat = Reg8::new(FTFA_FSTAT);
61 fstat.write(FTFA_FSTAT_FPVIOL | FTFA_FSTAT_ACCERR | FTFA_FSTAT_RDCOLERR);
64 fn run_command() -> u8 {
65 unsafe { flash_run_command() }
68 fn erase_check(sector: u32) -> bool {
69 let address = sector as usize * SECTOR_SIZE;
70 let num_words = SECTOR_SIZE / 4;
74 Reg8::new(FTFA_FCCOB0).write(FCMD_RD1SEC);
75 Reg8::new(FTFA_FCCOB1).write((address >> 16) as u8);
76 Reg8::new(FTFA_FCCOB2).write((address >> 8) as u8);
77 Reg8::new(FTFA_FCCOB3).write((address >> 0) as u8);
78 Reg8::new(FTFA_FCCOB4).write((num_words >> 8) as u8);
79 Reg8::new(FTFA_FCCOB5).write((num_words >> 0) as u8);
80 Reg8::new(FTFA_FCCOB6).write(0);
82 (run_command() & FTFA_FSTAT_MGSTAT0) == 0
85 pub fn erase(sector: u32) {
87 if erase_check(sector) {
91 let address = sector as usize * SECTOR_SIZE;
95 Reg8::new(FTFA_FCCOB0).write(FCMD_ERSSCR);
96 Reg8::new(FTFA_FCCOB1).write((address >> 16) as u8);
97 Reg8::new(FTFA_FCCOB2).write((address >> 8) as u8);
98 Reg8::new(FTFA_FCCOB3).write((address >> 0) as u8);
103 pub fn program(sector: u32, data: &[u8]) {
106 for (i, bytes) in data.chunks(4).enumerate() {
107 if bytes[0] == 0xff &&
114 let address = (sector as usize * SECTOR_SIZE) + (i * 4);
116 Reg8::new(FTFA_FCCOB0).write(FCMD_PGM4);
117 Reg8::new(FTFA_FCCOB1).write((address >> 16) as u8);
118 Reg8::new(FTFA_FCCOB2).write((address >> 8) as u8);
119 Reg8::new(FTFA_FCCOB3).write((address >> 0) as u8);
120 Reg8::new(FTFA_FCCOB4).write(bytes[3]);
121 Reg8::new(FTFA_FCCOB5).write(bytes[2]);
122 Reg8::new(FTFA_FCCOB6).write(bytes[1]);
123 Reg8::new(FTFA_FCCOB7).write(bytes[0]);
129 pub fn verify(sector: u32, data: &[u8]) -> (u32, u32) {
130 let mut num_mismatches = 0;
131 let mut first_mismatch_offset = 0;
133 for i in (0..data.len()).rev() {
134 let address = (sector as usize * SECTOR_SIZE) + i;
135 let ptr = address as *const u8;
136 let actual_byte = unsafe { core::ptr::read(ptr) };
137 let expected_byte = data[i];
139 if actual_byte != expected_byte {
141 first_mismatch_offset = i;
145 (num_mismatches, first_mismatch_offset as u32)