hpm_isp 0.5.0

ISP tool for HPMicro MCUs.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
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
261
262
263
264
265
266
267
268
269
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#![allow(unused)]

use std::fmt::Debug;
use std::fs::File;
use std::io::{Read, Write};
use std::path::Path;
use std::{cmp, error, fmt, io, mem};

use zerocopy::{AsBytes, FromBytes, FromZeroes};

#[derive(AsBytes, FromZeroes, FromBytes)]
#[repr(C, packed)]
pub struct Packet {
    cmd: u8,
    arg_num: u8,
    cmd_type: u8,
    reserved: u8,
    payload: [u8; 508],
}

#[repr(u8)]
enum Commands {
    /// Query runtime environment
    QueryRuntimeEnv = 0x01,
    /// Configure runtime environment
    ConfigureRuntimeEnv = 0x02,
    /// Configure memory
    ConfigureMemory = 0x03,
    /// Write memory
    WriteMemory = 0x04,
    /// Read memory
    ReadMemory = 0x05,
}

#[repr(u8)]
enum CommandType {
    CommandData = 0x00,
    DataOnly = 0x01,
    ResponseOnly = 0x02,
}

#[derive(AsBytes)]
#[repr(C, packed)]
struct QueryRuntimeEnvironment {
    id: RuntimeEnvironment,
}

impl QueryRuntimeEnvironment {
    fn new(id: RuntimeEnvironment) -> Self {
        Self { id }
    }
}

impl From<QueryRuntimeEnvironment> for Packet {
    fn from(query_rte: QueryRuntimeEnvironment) -> Self {
        let mut payload: [u8; 508] = [0; 508];
        payload[..mem::size_of::<QueryRuntimeEnvironment>()].copy_from_slice(query_rte.as_bytes());
        Packet {
            cmd: Commands::QueryRuntimeEnv as u8,
            arg_num: 1,
            cmd_type: CommandType::CommandData as u8,
            reserved: 0,
            payload,
        }
    }
}

#[derive(AsBytes)]
#[repr(u32)]
pub enum RuntimeEnvironment {
    RomParameter = 0x00,
    ActivePeripheralInfo = 0x01,
    LastBootStatus = 0x03,
    MemoryAttribute = 0x04,
}

struct RomParameter {
    /// TODO: 完善具体字段
    none: u8,
}

#[derive(AsBytes)]
#[repr(C, packed)]
struct ConfigureMemory {
    memory_id: u32,
    cfg_addr: u32,
}

impl ConfigureMemory {
    fn new(cfg_addr: u32, memory_id: MemoryId) -> Self {
        ConfigureMemory {
            memory_id: memory_id as u32,
            cfg_addr,
        }
    }
}

impl From<ConfigureMemory> for Packet {
    fn from(configure_memory: ConfigureMemory) -> Self {
        let mut payload: [u8; 508] = [0; 508];
        payload[..mem::size_of::<ConfigureMemory>()].copy_from_slice(configure_memory.as_bytes());
        Packet {
            cmd: Commands::ConfigureMemory as u8,
            arg_num: 2,
            cmd_type: CommandType::ResponseOnly as u8,
            reserved: 0,
            payload,
        }
    }
}

#[derive(AsBytes)]
#[repr(C, packed)]
struct WriteMemory {
    start: u32,
    length: u32,
    memory_id: u32,
}

impl WriteMemory {
    fn new(start: u32, length: u32, memory_id: MemoryId) -> Self {
        WriteMemory {
            start,
            length,
            memory_id: memory_id as u32,
        }
    }
}

impl From<WriteMemory> for Packet {
    fn from(write_memory: WriteMemory) -> Self {
        let mut payload: [u8; 508] = [0; 508];
        payload[..mem::size_of::<WriteMemory>()].copy_from_slice(write_memory.as_bytes());
        Packet {
            cmd: Commands::WriteMemory as u8,
            arg_num: 3,
            cmd_type: CommandType::CommandData as u8,
            reserved: 0,
            payload,
        }
    }
}

#[derive(AsBytes)]
#[repr(C, packed)]
struct ReadMemory {
    start: u32,
    length: u32,
    memory_id: u32,
}

impl ReadMemory {
    fn new(start: u32, length: u32, memory_id: MemoryId) -> Self {
        ReadMemory {
            start,
            length,
            memory_id: memory_id as u32,
        }
    }
}

impl From<ReadMemory> for Packet {
    fn from(write_memory: ReadMemory) -> Self {
        let mut payload: [u8; 508] = [0; 508];
        payload[..mem::size_of::<ReadMemory>()].copy_from_slice(write_memory.as_bytes());
        Packet {
            cmd: Commands::ReadMemory as u8,
            arg_num: 3,
            cmd_type: CommandType::CommandData as u8,
            reserved: 0,
            payload,
        }
    }
}

#[derive(Copy, Clone)]
#[repr(u32)]
pub enum MemoryId {
    ILM = 0x00,
    DLM = 0x01,
    XRAM = 0x02,
    XPI0 = 0x10000,
    XPI1 = 0x10001,
}

impl MemoryId {
    pub fn base_address(&self) -> u32 {
        match self {
            MemoryId::ILM => 0x0000_0000,
            MemoryId::DLM => 0x0008_0000,
            MemoryId::XRAM => 0x0108_0000,
            MemoryId::XPI0 => 0x8000_0000,
            MemoryId::XPI1 => 0x9000_0000,
        }
    }
}

#[derive(FromZeroes, FromBytes)]
#[repr(C, packed)]
struct GenericCommandResponse {
    status: u32,
}

impl From<GenericCommandResponse> for Result<(), Error> {
    fn from(resp: GenericCommandResponse) -> Self {
        if resp.status == 0 {
            Ok(())
        } else {
            Err(Error::Other(resp.status))
        }
    }
}

pub trait Interface {
    fn write(&self, packet: &Packet, length: u16) -> Result<(), Error>;
    fn read(&self, packet: &mut Packet) -> Result<u16, Error>;
}

fn write_from_reader<D, R, F>(
    device: &D,
    memory_id: MemoryId,
    offset: u32,
    total_length: usize,
    mut reader: R,
    update_progress: F,
) -> Result<(), Error>
where
    D: Interface + ?Sized,
    R: Read,
    F: Fn(usize, usize),
{
    let command_length = mem::size_of::<WriteMemory>();
    let mut packet: Packet = WriteMemory::new(
        offset + memory_id.base_address(),
        total_length as u32,
        memory_id,
    )
    .into();

    if total_length == 0 {
        device.write(&packet, command_length as u16)?;
        device.read(&mut packet)?;
        let resp = GenericCommandResponse::read_from_prefix(&packet.payload[..]).unwrap();
        return resp.into();
    }

    let mut bytes_written = 0;
    let mut max_length = packet.payload.len() - command_length;
    let mut payload_offset = command_length;

    while bytes_written < total_length {
        let write_length = cmp::min(max_length, total_length - bytes_written);
        reader.read_exact(&mut packet.payload[payload_offset..payload_offset + write_length])?;
        device.write(&packet, (payload_offset + write_length) as u16)?;
        bytes_written += write_length;
        update_progress(bytes_written, total_length);

        packet.arg_num = 0;
        packet.cmd_type = CommandType::DataOnly as u8;
        payload_offset = 0;
        max_length = packet.payload.len();
    }

    device.read(&mut packet)?;
    let resp = GenericCommandResponse::read_from_prefix(&packet.payload[..]).unwrap();
    resp.into()
}

fn read_to_writer<D, W, F>(
    device: &D,
    memory_id: MemoryId,
    offset: u32,
    total_length: usize,
    mut writer: W,
    update_progress: F,
) -> Result<(), Error>
where
    D: Interface + ?Sized,
    W: Write,
    F: Fn(usize, usize),
{
    let mut packet: Packet = ReadMemory::new(
        offset + memory_id.base_address(),
        total_length as u32,
        memory_id,
    )
    .into();
    let mut bytes_read = 0;

    device.write(&packet, mem::size_of::<ReadMemory>() as u16)?;
    device.read(&mut packet)?;
    let resp = GenericCommandResponse::read_from_prefix(&packet.payload[..]).unwrap();
    if resp.status != 0 {
        return resp.into();
    }

    while bytes_read < total_length {
        let length = device.read(&mut packet)? as usize;
        if length == 0 || length > packet.payload.len() || length > total_length - bytes_read {
            return Err(Error::TransferError);
        }

        writer.write_all(&packet.payload[..length])?;
        bytes_read += length;
        update_progress(bytes_read, total_length);
    }

    Ok(())
}

pub trait IspCommand: Interface {
    fn query_runtime_environment(&self, id: RuntimeEnvironment) -> Result<(), Error> {
        let mut packet: Packet = QueryRuntimeEnvironment::new(id).into();
        self.write(&packet, mem::size_of::<QueryRuntimeEnvironment>() as u16)?;
        self.read(&mut packet)?;
        todo!("data matching and error handling");
        Ok(())
    }
    /// Configure memory, using configuration block in RAM
    ///
    /// # Arguments
    ///
    /// * `memory_id`: Memory ID to be configure
    /// * `cfg_addr`: Configuration block address in RAM
    ///
    /// # Example
    ///
    /// ```ignore
    /// device.configure_memory(MemoryId::XPI0, 0x200);
    /// ```
    fn configure_memory(&self, memory_id: MemoryId, cfg_addr: u32) -> Result<(), Error> {
        let mut packet: Packet = ConfigureMemory::new(cfg_addr, memory_id).into();
        self.write(&packet, mem::size_of::<ConfigureMemory>() as u16)?;
        self.read(&mut packet)?;
        let resp = GenericCommandResponse::read_from_prefix(&packet.payload[..]).unwrap();
        resp.into()
    }

    fn write_memory<F>(
        &self,
        memory_id: MemoryId,
        offset: u32,
        data: &[u8],
        update_progress: F,
    ) -> Result<(), Error>
    where
        F: Fn(usize, usize),
    {
        write_from_reader(self, memory_id, offset, data.len(), data, update_progress)
    }

    fn read_memory<F>(
        &self,
        memory_id: MemoryId,
        offset: u32,
        data: &mut [u8],
        update_progress: F,
    ) -> Result<(), Error>
    where
        F: Fn(usize, usize),
    {
        read_to_writer(self, memory_id, offset, data.len(), data, update_progress)
    }

    fn write_file<P, F>(
        &self,
        path: P,
        memory_id: MemoryId,
        offset: u32,
        update_progress: F,
    ) -> Result<(), Error>
    where
        P: AsRef<Path>,
        F: Fn(usize, usize),
    {
        let file = File::open(path)?;
        let file_info = file.metadata()?;
        write_from_reader(
            self,
            memory_id,
            offset,
            file_info.len() as usize,
            file,
            update_progress,
        )
    }

    fn read_file<P, F>(
        &self,
        path: P,
        memory_id: MemoryId,
        offset: u32,
        total_length: usize,
        update_progress: F,
    ) -> Result<(), Error>
    where
        P: AsRef<Path>,
        F: Fn(usize, usize),
    {
        let file = File::create(path)?;
        read_to_writer(self, memory_id, offset, total_length, file, update_progress)
    }
}

#[derive(Debug)]
pub enum Error {
    Nak,
    TransferError,
    Timeout,
    IoError(io::Error),
    Other(u32),
}

impl Error {
    fn as_str(&self) -> &'static str {
        match self {
            Error::Nak => "negative acknowledge",
            Error::TransferError => "transfer error",
            Error::Timeout => "timeout",
            Error::IoError(_) => "io error",
            Error::Other(_) => "other error",
        }
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match &self {
            Error::IoError(e) => write!(f, "{}: {}", self.as_str(), e),
            Error::Other(code) => write!(f, "{}: {}", self.as_str(), code),
            _ => write!(f, "{}", self.as_str()),
        }
    }
}

impl error::Error for Error {
    fn description(&self) -> &str {
        self.as_str()
    }
}

impl From<io::Error> for Error {
    fn from(e: io::Error) -> Self {
        Error::IoError(e)
    }
}