py32f030_hal 0.1.0

Peripheral Hal Crate for Puya's PY32F030 microcontroller
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
447
448
449
//! ADC

#[cfg(feature = "embassy")]
mod future;
mod hal;
mod pins;
mod types;

#[cfg(not(feature = "embassy"))]
mod interrupt;

#[cfg(not(feature = "embassy"))]
pub use interrupt::*;

#[cfg(feature = "embassy")]
use core::{future::Future, task::Poll};

use core::marker::PhantomData;

#[cfg(feature = "embassy")]
use crate::mcu::peripherals::ADC;
#[cfg(feature = "embassy")]
use crate::mode::Async;
use enumset::EnumSet;
#[cfg(feature = "embassy")]
use future::ChannelInputFuture;

pub use types::*;

use crate::{
    clock::peripheral::PeripheralInterrupt, macro_def::impl_sealed_peripheral_id, mode::Blocking,
};

use embassy_hal_internal::Peripheral;
pub use pins::{TemperatureChannel, VRrefChannel};

use crate::{
    clock::peripheral::{PeripheralClockIndex, PeripheralIdToClockIndex},
    delay::wait_for_true_timeout_block,
    mode::Mode,
};

#[cfg(feature = "embassy")]
use embassy_sync::waitqueue::AtomicWaker;

#[cfg(feature = "embassy")]
static ADC_INT_WAKER: [AtomicWaker; 1] = [AtomicWaker::new()];

#[allow(private_bounds)]
pub trait Instance: Peripheral<P = Self> + hal::sealed::Instance + 'static + Send {}

#[derive(PartialEq)]
pub enum Id {
    ADC1 = 0,
}

impl_sealed_peripheral_id!(ADC, ADC1);

impl PeripheralIdToClockIndex for Id {
    fn clock(&self) -> PeripheralClockIndex {
        match *self {
            Self::ADC1 => PeripheralClockIndex::ADC,
        }
    }
}

impl PeripheralInterrupt for Id {
    fn interrupt(&self) -> crate::pac::interrupt {
        match *self {
            Self::ADC1 => crate::pac::interrupt::ADC_COMP,
        }
    }
}

pub struct AnyAdc<'d, T: Instance, M: Mode> {
    t: PhantomData<&'d T>,
    _m: PhantomData<M>,
}

impl<'d, T: Instance, M: Mode> AnyAdc<'d, T, M> {
    pub fn new(
        _adc: impl Peripheral<P = T>,
        config: Config,
        channel_config: ChannelConfig,
        channels: &[AdcChannel],
    ) -> Result<Self, Error> {
        T::id().clock().reset();
        T::id().clock().open();

        T::stop();

        Self::new_inner(config, channel_config, channels)?;

        T::enable();

        // 异步方式需要打开外设中断
        if M::is_async() {
            T::id().enable_interrupt();
        }

        Ok(Self {
            t: PhantomData,
            _m: PhantomData,
        })
    }

    /// 校准 adc
    pub fn calibration(config: CalibrationConfig, timeout: usize) -> Result<(), Error> {
        T::set_calibration_content(config.content);
        T::set_calibration_sample_time(config.sample_time);
        T::calibration_start();

        let block = T::block();
        wait_for_true_timeout_block(timeout, || {
            block.ccsr.read().calon().bit_is_clear() && block.ccsr.read().calfail().bit_is_clear()
        })
        .map_err(|_| Error::Calibrate)?;

        Ok(())
    }

    #[inline]
    pub fn id(&self) -> Id {
        T::id()
    }

    #[inline]
    pub fn start(&self) {
        for e in EnumSet::all() {
            T::event_clear(e);
        }

        T::start();
    }

    #[inline]
    pub fn stop(&self) {
        T::stop()
    }

    fn new_inner(
        config: Config,
        channel_config: ChannelConfig,
        channels: &[AdcChannel],
    ) -> Result<(), Error> {
        const CALIBRATE_TIMEOUT: usize = 1000000;
        T::disable();
        // 设置时钟
        T::set_clock_mode(config.clock);
        T::set_resolution(config.resolution);
        T::set_sample_cycle(config.sample_cycle);
        // 上电后硬件会自动校准一次
        if config.calibration {
            // 必须先校准再开启时钟
            Self::calibration(Default::default(), CALIBRATE_TIMEOUT)?
        }
        T::align(config.align);

        Self::channel_config(channel_config);

        // 使能通道
        for channel in channels {
            T::channel_enable(*channel, true)
        }

        Ok(())
    }

    pub fn channel_enable(&self, channels: &[impl AnalogPin<T>]) {
        for channel in channels {
            channel.as_anlog();
            T::channel_enable(channel.channel(), true);
        }
    }

    pub fn event_config(&mut self, event: Event, en: bool) {
        T::event_config(event, en);
    }

    pub fn event_clear(&mut self, event: Event) {
        T::event_clear(event);
    }

    pub fn event_flag(&self, event: Event) -> bool {
        T::event_flag(event)
    }

    pub fn set_watchdog(config: Option<WatchDogConfig>) {
        if let Some(config) = config {
            T::set_watch_dog_threshold(config.high, config.low)
        }
    }

    fn channel_config(config: ChannelConfig) {
        T::conversion_mode(config.mode);
        T::set_scan_dir(config.scan_dir);
        T::set_overwrite(config.over_write);
        T::trigle_signal(config.signal);
        T::set_wait(config.wait);
    }

    #[cfg(not(feature = "embassy"))]
    pub fn on_interrupt(
        &mut self,
        events: EnumSet<Event>,
        callback: alloc::boxed::Box<dyn Fn(u16)>,
    ) {
        crate::interrupt::register(
            #[allow(static_mut_refs)]
            unsafe {
                &mut CLOSURE
            },
            alloc::boxed::Box::new(move || {
                callback(T::data_read());
                for e in events {
                    T::event_flag(e);
                }
            }),
        );
        for e in events {
            self.event_config(e, true);
        }
    }
}

impl<'d, T: Instance, M: Mode> Drop for AnyAdc<'d, T, M> {
    fn drop(&mut self) {
        if M::is_async() {
            T::id().disable_interrupt();
        }
    }
}

impl<'d, T: Instance> AnyAdc<'d, T, Blocking> {
    pub fn read_block(&self, timeout: usize) -> Result<u16, Error> {
        wait_for_true_timeout_block(timeout, || T::event_flag(Event::EOC))
            .map_err(|_| Error::Timeout)?;
        Ok(T::data_read())
    }
}

#[cfg(feature = "embassy")]
impl<'d, T: Instance> AnyAdc<'d, T, Async> {
    pub async fn read(&self, channel: impl AnalogPin<T>) -> u16 {
        ChannelInputFuture::<T>::new_with_channel(channel.channel()).await
    }
}

#[derive(Clone, Copy)]
pub struct CalibrationConfig {
    content: CalibrationSelect,
    sample_time: CalibrationSampleTime,
}

impl CalibrationConfig {
    pub fn new(content: CalibrationSelect, sample_time: CalibrationSampleTime) -> Self {
        Self {
            content,
            sample_time,
        }
    }
}

impl Default for CalibrationConfig {
    fn default() -> Self {
        Self {
            content: CalibrationSelect::OffsetLinearity,
            sample_time: CalibrationSampleTime::Cycle_8,
        }
    }
}

pub struct WatchDogConfig {
    high: u16,
    low: u16,
    // interrupt: bool,
}

pub struct Config {
    /// 是否初始化前是否开始校验
    calibration: bool,
    /// 采样周期
    sample_cycle: SampleCycles,
    /// adc 精度
    resolution: Resolution,
    /// 数据对齐
    align: Align,
    ///  adc 时钟源
    clock: ClockMode,
}

impl Default for Config {
    fn default() -> Self {
        Self {
            calibration: true,
            sample_cycle: SampleCycles::Cycle_3_5,
            resolution: Resolution::Bit12,
            align: Align::Right,
            clock: ClockMode::PCLK,
        }
    }
}

impl Config {
    pub fn new(
        calibration: bool,
        sample_cycle: SampleCycles,
        resolution: Resolution,
        align: Align,
        clock: ClockMode,
    ) -> Self {
        Self {
            calibration,
            sample_cycle,
            resolution,
            align,
            clock,
        }
    }

    pub fn sample(self, sample: SampleCycles) -> Self {
        Self {
            sample_cycle: sample,
            ..self
        }
    }

    pub fn calibration(self, calibration: bool) -> Self {
        Self {
            calibration,
            ..self
        }
    }

    pub fn resolution(self, resolution: Resolution) -> Self {
        Self { resolution, ..self }
    }

    pub fn align(self, align: Align) -> Self {
        Self { align, ..self }
    }

    pub fn clock(self, clock: ClockMode) -> Self {
        Self { clock, ..self }
    }
}

pub struct ChannelConfig {
    /// 转换模式
    mode: ConversionMode,
    /// 扫描方向
    scan_dir: ScanDir,
    /// 过写使能
    over_write: bool,
    /// 等待读取再开始下一个通道转换
    wait: bool,
    /// 触发信号类型
    signal: TrigleSignal,
}

impl ChannelConfig {
    pub fn mode(self, mode: ConversionMode) -> Self {
        Self { mode, ..self }
    }

    pub fn singal(self, signal: TrigleSignal) -> Self {
        Self { signal, ..self }
    }

    pub fn scan_dir(self, scan_dir: ScanDir) -> Self {
        Self { scan_dir, ..self }
    }

    pub fn wait(self, wait: bool) -> Self {
        Self { wait, ..self }
    }

    pub fn over_write(self, over_write: bool) -> Self {
        Self { over_write, ..self }
    }
    /// 多通道配置读取推荐配置
    /// 连续转换/向上扫描/不过写/软件触发
    pub fn new_multiple_channel_perferred() -> Self {
        Self {
            mode: ConversionMode::Continuous,
            scan_dir: ScanDir::Up,
            over_write: false,
            wait: false,
            signal: TrigleSignal::Soft,
        }
    }

    /// 单通道读取推荐配置
    /// 连续转换/向上扫描/过写/软件触发
    pub fn new_exclusive_perferred() -> Self {
        Self {
            mode: ConversionMode::Continuous,
            scan_dir: ScanDir::Up,
            over_write: true,
            wait: true,
            signal: TrigleSignal::Soft,
        }
    }

    /// 单次扫描模式
    pub fn new_exclusive_single() -> Self {
        Self {
            mode: ConversionMode::Single,
            scan_dir: ScanDir::Up,
            over_write: false,
            wait: true,
            signal: TrigleSignal::Soft,
        }
    }
}

impl Default for ChannelConfig {
    fn default() -> Self {
        Self {
            mode: ConversionMode::Continuous,
            scan_dir: ScanDir::Up,
            over_write: true,
            wait: true,
            signal: TrigleSignal::Soft,
        }
    }
}

pub trait AnalogPin<T: Instance> {
    fn channel(&self) -> AdcChannel;
    fn as_anlog(&self);
}

pub fn temperature(dr: u16) -> f32 {
    const TS_CAL1_ADDR: u32 = 0x1fff_0f14;
    const TS_CAL2_ADDR: u32 = 0x1fff_0f18;

    let ts_cal2 = unsafe { core::ptr::read(TS_CAL2_ADDR as *const u32) } as f32;
    let ts_cal1 = unsafe { core::ptr::read(TS_CAL1_ADDR as *const u32) } as f32;
    // let temp_k = (85.0 - 30.0) / (ts_cal2 - ts_cal1);
    // temp_k * (dr as f32 - ts_cal1) + ts_cal1
    // dr as f32 / 4095.0 * 3.3
    ((85.0 - 30.0) * (dr as f32 - ts_cal1) / (ts_cal2 - ts_cal1)) + 30.0
}

pub fn vrefence_internal(dr: u16) -> f32 {
    // dr as f32 / 4095.0 * 3.3
    4095.0 * 1.2 / dr as f32
}