greaseweazle 0.2.0

Support library to control a Greaseweazle from the host.
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
use byteorder::{LE, ReadBytesExt};
use num_enum::{FromPrimitive, IntoPrimitive};
use std::{
    fmt,
    io::{self, Read},
};

/// Information about the hardware and firmware of the Greaseweazle device.
///
/// Returned by the [`get_firmware_info`](crate::Greaseweazle::get_firmware_info) command.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct FirmwareInfo {
    /// The major version of the firmware.
    pub major: u8,

    /// The minor version of the firmware.
    pub minor: u8,

    /// Whether the device is currently running the main firmware (`true`), or in firmware update
    /// mode (`false`).
    pub is_main_firmware: bool,

    /// The highest command number supported by the device.
    pub max_cmd: u8,

    /// The sample frequency in Hz used when sampling floppy flux signals.
    ///
    /// [`Ticks`](crate::Ticks) holds a duration in Greaseweazle "sample ticks", a time unit equal
    /// to the period of a sample. There are `sample_freq` sample ticks in a second, and
    /// one sample tick is `1.0 / sample_freq` seconds.
    pub sample_freq: u32,

    /// The hardware model of the Greaseweazle.
    pub hw_model: HwModel,

    /// The connected USB speed of the device.
    pub usb_speed: UsbSpeed,

    /// The specific type of microcontroller, if applicable.
    pub mcu_id: Option<McuId>,

    /// The clock speed in MHz of the microcontroller.
    pub mcu_mhz: u16,

    /// The amount of SRAM in kB that the microcontroller is equipped with.
    pub mcu_sram_kb: u16,

    /// The size in kB of the buffer used to read USB commands.
    pub usb_buf_kb: u16,
}

impl FirmwareInfo {
    pub(crate) fn read_from(mut read: impl Read) -> Result<Self, io::Error> {
        Ok(Self {
            major: read.read_u8()?,
            minor: read.read_u8()?,
            is_main_firmware: read.read_u8()? != 0,
            max_cmd: read.read_u8()?,
            sample_freq: read.read_u32::<LE>()?,
            hw_model: {
                let hw_model = read.read_u8()?;
                let hw_submodel = read.read_u8()?;

                match hw_model {
                    1 => HwModel::F1(hw_submodel.into()),
                    4 => HwModel::V4(hw_submodel.into()),
                    7 => HwModel::F7(hw_submodel.into()),
                    8 => HwModel::AdafruitFloppy(hw_submodel.into()),
                    _ => HwModel::Unknown(hw_model, hw_submodel),
                }
            },
            usb_speed: read.read_u8()?.into(),
            mcu_id: Some(read.read_u8()?).filter(|&id| id != 0).map(Into::into),
            mcu_mhz: read.read_u16::<LE>()?,
            mcu_sram_kb: read.read_u16::<LE>()?,
            usb_buf_kb: read.read_u16::<LE>()?,
        })
    }
}

/// Greaseweazle hardware model.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
#[repr(u8)]
pub enum HwModel {
    /// STM32F103 microcontroller.
    F1(HwSubmodelF1) = 1,

    /// AT32F4xx series microcontroller.
    V4(HwSubmodelV4) = 4,

    /// STM32F730 microcontroller.
    F7(HwSubmodelF7) = 7,

    /// Adafruit Floppy.
    AdafruitFloppy(HwSubmodelAdafruitFloppy) = 8,

    /// Unknown model.
    Unknown(u8, u8) = 0xFF,
}

impl fmt::Display for HwModel {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::F1(submodel) => submodel.fmt(f),
            Self::V4(submodel) => submodel.fmt(f),
            Self::F7(submodel) => submodel.fmt(f),
            Self::AdafruitFloppy(submodel) => submodel.fmt(f),
            Self::Unknown(m, s) => write!(f, "unknown model {m}, submodel {s}"),
        }
    }
}

/// Greaseweazle F1 submodel.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum HwSubmodelF1 {
    /// F1.
    F1 = 0,

    /// F1 Plus
    F1Plus = 1,

    /// F1 Plus (unbuffered)
    F1PlusUnbuffered = 2,

    /// F1, unknown submodel.
    #[num_enum(catch_all)]
    Unknown(u8) = 0xFF,
}

impl fmt::Display for HwSubmodelF1 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::F1 => write!(f, "F1"),
            Self::F1Plus => write!(f, "F1 Plus"),
            Self::F1PlusUnbuffered => write!(f, "F1 Plus (unbuffered)"),
            Self::Unknown(id) => write!(f, "F1, unknown submodel {id}"),
        }
    }
}

/// Greaseweazle V4 submodel.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum HwSubmodelV4 {
    /// V4.
    V4 = 0,

    /// V4 Slim.
    V4Slim = 1,

    /// V4.1.
    V4_1 = 2,

    /// V4, unknown submodel.
    #[num_enum(catch_all)]
    Unknown(u8) = 0xFF,
}

impl fmt::Display for HwSubmodelV4 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::V4 => write!(f, "V4"),
            Self::V4Slim => write!(f, "V4 Slim"),
            Self::V4_1 => write!(f, "V4.1"),
            Self::Unknown(id) => write!(f, "V4, unknown submodel {id}"),
        }
    }
}

/// Greaseweazle F7 submodel.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum HwSubmodelF7 {
    /// F7 v1.
    F7V1 = 0,

    /// F7 Plus (Ant Goffart, v1).
    F7PlusV1 = 1,

    /// F7 Lightning.
    F7Lightning = 2,

    /// F7 v2.
    F7V2 = 3,

    /// F7 Plus (Ant Goffart, v2).
    F7PlusV2 = 4,

    /// F7 Lightning Plus.
    F7LightningPlus = 5,

    /// F7 Slim.
    F7Slim = 6,

    /// F7 v3 Thunderbolt.
    F7V3Thunderbolt = 7,

    /// F7, unknown submodel.
    #[num_enum(catch_all)]
    Unknown(u8) = 0xFF,
}

impl fmt::Display for HwSubmodelF7 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::F7V1 => write!(f, "F7 v1"),
            Self::F7PlusV1 => write!(f, "F7 Plus (Ant Goffart, v1)"),
            Self::F7Lightning => write!(f, "F7 Lightning"),
            Self::F7V2 => write!(f, "F7 v2"),
            Self::F7PlusV2 => write!(f, "F7 Plus (Ant Goffart, v2)"),
            Self::F7LightningPlus => write!(f, "F7 Lightning Plus"),
            Self::F7Slim => write!(f, "F7 Slim"),
            Self::F7V3Thunderbolt => write!(f, "F7 v3 Thunderbolt"),
            Self::Unknown(id) => write!(f, "F7, unknown submodel {id}"),
        }
    }
}

/// Greaseweazle Adafruit Floppy submodel.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum HwSubmodelAdafruitFloppy {
    /// Adafruit Floppy Generic.
    Generic = 0,

    /// Adafruit Floppy, unknown submodel.
    #[num_enum(catch_all)]
    Unknown(u8) = 0xFF,
}

impl fmt::Display for HwSubmodelAdafruitFloppy {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::Generic => write!(f, "Adafruit Floppy Generic"),
            Self::Unknown(id) => write!(f, "Adafruit Floppy, unknown submodel {id}"),
        }
    }
}

/// USB speed.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum UsbSpeed {
    /// Full speed (12 Mbit/s).
    FullSpeed = 0,

    /// High speed (480 Mbit/s).
    HighSpeed = 1,

    /// Unknown speed.
    #[num_enum(catch_all)]
    Unknown(u8) = 0xFF,
}

impl fmt::Display for UsbSpeed {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::FullSpeed => write!(f, "full speed (12 Mbit/s)"),
            Self::HighSpeed => write!(f, "high speed (480 Mbit/s)"),
            Self::Unknown(id) => write!(f, "unknown speed {id}"),
        }
    }
}

/// Microcontroller type.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum McuId {
    /// AT32F403.
    AT32F403 = 2,

    /// AT32F415.
    AT32F415 = 5,

    /// AT32F403A.
    AT32F403A = 7,

    /// Unknown MCU.
    #[num_enum(catch_all)]
    Unknown(u8) = 0xFF,
}

impl fmt::Display for McuId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::AT32F403 => write!(f, "AT32F403"),
            Self::AT32F415 => write!(f, "AT32F415"),
            Self::AT32F403A => write!(f, "AT32F403A"),
            Self::Unknown(id) => write!(f, "unknown MCU {id}"),
        }
    }
}

/// Information about the current state of a drive.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct DriveInfo {
    /// Whether the motor is currently on.
    pub is_motor_on: bool,

    /// Whether the drive is a "flippy" drive.
    pub is_flippy: bool,

    /// The cylinder that the head of the drive is currently positioned on, if known.
    ///
    /// Note: this is based on where the Greaseweazle thinks the head is, and may not match where
    /// the head is physically in reality.
    pub cylinder: Option<i32>,
}

impl DriveInfo {
    pub(crate) fn read_from(mut read: impl Read) -> Result<Self, io::Error> {
        const FLAG_CYL_VALID: u32 = 0x1;
        const FLAG_MOTOR_ON: u32 = 0x2;
        const FLAG_FLIPPY: u32 = 0x4;

        let flags = read.read_u32::<LE>()?;

        Ok(Self {
            is_motor_on: flags & FLAG_MOTOR_ON != 0,
            is_flippy: flags & FLAG_FLIPPY != 0,
            cylinder: Some(read.read_i32::<LE>()?).filter(|_| flags & FLAG_CYL_VALID != 0),
        })
    }
}

/// The floppy bus type to be used.
#[derive(Debug, Clone, Copy, FromPrimitive, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub enum BusType {
    /// No bus type selected. This is the default state after powering on the Greaseweazle.
    None = 0,

    /// The IBM PC floppy interface. Valid drive numbers are 0 and 1, for drives A and B
    /// respectively.
    ///
    /// - Pin 10 = Motor enable A
    /// - Pin 12 = Drive select B
    /// - Pin 14 = Drive select A
    /// - Pin 16 = Motor enable B
    IbmPc = 1,

    /// The original Shugart floppy interface. Valid drive numbers are 0, 1 and 2.
    ///
    /// - Pin 10 = Drive select 1
    /// - Pin 12 = Drive select 2
    /// - Pin 14 = Drive select 3
    /// - Pin 16 = Motor on
    Shugart = 2,

    /// Other interface type.
    ///
    /// This is provided for future extensions of the Greaseweazle.
    #[num_enum(catch_all)]
    Other(u8),
}

/// Delay/timing settings.
///
/// Used by the [`get_delays`](crate::Greaseweazle::get_delays) and
/// [`set_delays`](crate::Greaseweazle::set_delays) commands.
#[derive(Debug, Clone, Copy)]
#[non_exhaustive]
pub struct Delays {
    /// Delay in microseconds after asserting drive select.
    pub select_us: u16,

    /// Delay in microseconds after issuing a head-step command.
    pub step_us: u16,

    /// Delay in milliseconds after completing a head-seek operation.
    pub seek_settle_ms: u16,

    /// Delay in milliseconds after turning on drive spindle motor.
    pub motor_ms: u16,

    /// Timeout in milliseconds since last command, upon which all drives are deselected and
    /// spindle motors turned off.
    pub watchdog_ms: u16,

    /// Minimum time in microseconds from track change to write start.
    pub pre_write_us: Option<u16>,

    /// Mininum time in microseconds from write end to track change.
    pub post_write_us: Option<u16>,

    /// Index post-trigger mask time in microseconds.
    pub index_mask_us: Option<u16>,
}

#[derive(Debug, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub(crate) enum GetInfo {
    Firmware = 0,
    BandwidthStats = 1,
    CurrentDrive = 7,
    Drive = 8,
}

#[derive(Debug, IntoPrimitive)]
#[non_exhaustive]
#[repr(u8)]
pub(crate) enum Param {
    Delays = 0,
}