diff options
| author | Jacob Rosenthal <[email protected]> | 2021-11-20 18:02:59 -0700 |
|---|---|---|
| committer | Jacob Rosenthal <[email protected]> | 2021-11-20 18:07:20 -0700 |
| commit | 18d94a7d01975084457a99d6568d11ebc868a6ef (patch) | |
| tree | bdb81a2f322cb1da5a9fb59e3a969ecac3ba56b3 | |
| parent | 8d108d875394a08d6cb42d06a9c55a4c8e0833ac (diff) | |
nrf: saadc do not reexport pac enums
Closes #415
| -rw-r--r-- | embassy-nrf/src/saadc.rs | 198 |
1 files changed, 188 insertions, 10 deletions
diff --git a/embassy-nrf/src/saadc.rs b/embassy-nrf/src/saadc.rs index 7fffea1b6..bc83b5b61 100644 --- a/embassy-nrf/src/saadc.rs +++ b/embassy-nrf/src/saadc.rs | |||
| @@ -18,10 +18,10 @@ use pac::{saadc, SAADC}; | |||
| 18 | // We treat the positive and negative channels with the same enum values to keep our type tidy and given they are the same | 18 | // We treat the positive and negative channels with the same enum values to keep our type tidy and given they are the same |
| 19 | pub(crate) use saadc::ch::pselp::PSELP_A as InputChannel; | 19 | pub(crate) use saadc::ch::pselp::PSELP_A as InputChannel; |
| 20 | 20 | ||
| 21 | pub use saadc::{ | 21 | use saadc::{ |
| 22 | ch::config::{GAIN_A as Gain, REFSEL_A as Reference, RESP_A as Resistor, TACQ_A as Time}, | 22 | ch::config::{GAIN_A, REFSEL_A, RESP_A, TACQ_A}, |
| 23 | oversample::OVERSAMPLE_A as Oversample, | 23 | oversample::OVERSAMPLE_A, |
| 24 | resolution::VAL_A as Resolution, | 24 | resolution::VAL_A, |
| 25 | }; | 25 | }; |
| 26 | 26 | ||
| 27 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] | 27 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| @@ -161,8 +161,9 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 161 | 161 | ||
| 162 | // Configure channels | 162 | // Configure channels |
| 163 | r.enable.write(|w| w.enable().enabled()); | 163 | r.enable.write(|w| w.enable().enabled()); |
| 164 | r.resolution.write(|w| w.val().variant(resolution)); | 164 | r.resolution.write(|w| w.val().variant(resolution.into())); |
| 165 | r.oversample.write(|w| w.oversample().variant(oversample)); | 165 | r.oversample |
| 166 | .write(|w| w.oversample().variant(oversample.into())); | ||
| 166 | 167 | ||
| 167 | for (i, cc) in channel_configs.iter().enumerate() { | 168 | for (i, cc) in channel_configs.iter().enumerate() { |
| 168 | r.ch[i].pselp.write(|w| w.pselp().variant(cc.p_channel)); | 169 | r.ch[i].pselp.write(|w| w.pselp().variant(cc.p_channel)); |
| @@ -172,15 +173,15 @@ impl<'d, const N: usize> Saadc<'d, N> { | |||
| 172 | .write(|w| unsafe { w.pseln().bits(n_channel as u8) }); | 173 | .write(|w| unsafe { w.pseln().bits(n_channel as u8) }); |
| 173 | } | 174 | } |
| 174 | r.ch[i].config.write(|w| { | 175 | r.ch[i].config.write(|w| { |
| 175 | w.refsel().variant(cc.reference); | 176 | w.refsel().variant(cc.reference.into()); |
| 176 | w.gain().variant(cc.gain); | 177 | w.gain().variant(cc.gain.into()); |
| 177 | w.tacq().variant(cc.time); | 178 | w.tacq().variant(cc.time.into()); |
| 178 | if cc.n_channel.is_none() { | 179 | if cc.n_channel.is_none() { |
| 179 | w.mode().se(); | 180 | w.mode().se(); |
| 180 | } else { | 181 | } else { |
| 181 | w.mode().diff(); | 182 | w.mode().diff(); |
| 182 | } | 183 | } |
| 183 | w.resp().variant(cc.resistor); | 184 | w.resp().variant(cc.resistor.into()); |
| 184 | w.resn().bypass(); | 185 | w.resn().bypass(); |
| 185 | if !matches!(oversample, Oversample::BYPASS) { | 186 | if !matches!(oversample, Oversample::BYPASS) { |
| 186 | w.burst().enabled(); | 187 | w.burst().enabled(); |
| @@ -405,6 +406,183 @@ impl<'d, const N: usize> Drop for Saadc<'d, N> { | |||
| 405 | } | 406 | } |
| 406 | } | 407 | } |
| 407 | 408 | ||
| 409 | impl From<Gain> for GAIN_A { | ||
| 410 | fn from(gain: Gain) -> Self { | ||
| 411 | match gain { | ||
| 412 | Gain::GAIN1_6 => GAIN_A::GAIN1_6, | ||
| 413 | Gain::GAIN1_5 => GAIN_A::GAIN1_5, | ||
| 414 | Gain::GAIN1_4 => GAIN_A::GAIN1_4, | ||
| 415 | Gain::GAIN1_3 => GAIN_A::GAIN1_3, | ||
| 416 | Gain::GAIN1_2 => GAIN_A::GAIN1_2, | ||
| 417 | Gain::GAIN1 => GAIN_A::GAIN1, | ||
| 418 | Gain::GAIN2 => GAIN_A::GAIN2, | ||
| 419 | Gain::GAIN4 => GAIN_A::GAIN4, | ||
| 420 | } | ||
| 421 | } | ||
| 422 | } | ||
| 423 | |||
| 424 | /// Gain control | ||
| 425 | #[non_exhaustive] | ||
| 426 | #[derive(Clone, Copy)] | ||
| 427 | pub enum Gain { | ||
| 428 | /// 1/6 | ||
| 429 | GAIN1_6 = 0, | ||
| 430 | /// 1/5 | ||
| 431 | GAIN1_5 = 1, | ||
| 432 | /// 1/4 | ||
| 433 | GAIN1_4 = 2, | ||
| 434 | /// 1/3 | ||
| 435 | GAIN1_3 = 3, | ||
| 436 | /// 1/2 | ||
| 437 | GAIN1_2 = 4, | ||
| 438 | /// 1 | ||
| 439 | GAIN1 = 5, | ||
| 440 | /// 2 | ||
| 441 | GAIN2 = 6, | ||
| 442 | /// 4 | ||
| 443 | GAIN4 = 7, | ||
| 444 | } | ||
| 445 | |||
| 446 | impl From<Reference> for REFSEL_A { | ||
| 447 | fn from(reference: Reference) -> Self { | ||
| 448 | match reference { | ||
| 449 | Reference::INTERNAL => REFSEL_A::INTERNAL, | ||
| 450 | Reference::VDD1_4 => REFSEL_A::VDD1_4, | ||
| 451 | } | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | /// Reference control | ||
| 456 | #[non_exhaustive] | ||
| 457 | #[derive(Clone, Copy)] | ||
| 458 | pub enum Reference { | ||
| 459 | /// Internal reference (0.6 V) | ||
| 460 | INTERNAL = 0, | ||
| 461 | /// VDD/4 as reference | ||
| 462 | VDD1_4 = 1, | ||
| 463 | } | ||
| 464 | |||
| 465 | impl From<Resistor> for RESP_A { | ||
| 466 | fn from(resistor: Resistor) -> Self { | ||
| 467 | match resistor { | ||
| 468 | Resistor::BYPASS => RESP_A::BYPASS, | ||
| 469 | Resistor::PULLDOWN => RESP_A::PULLDOWN, | ||
| 470 | Resistor::PULLUP => RESP_A::PULLUP, | ||
| 471 | Resistor::VDD1_2 => RESP_A::VDD1_2, | ||
| 472 | } | ||
| 473 | } | ||
| 474 | } | ||
| 475 | |||
| 476 | /// Positive channel resistor control | ||
| 477 | #[non_exhaustive] | ||
| 478 | #[derive(Clone, Copy)] | ||
| 479 | pub enum Resistor { | ||
| 480 | /// Bypass resistor ladder | ||
| 481 | BYPASS = 0, | ||
| 482 | /// Pull-down to GND | ||
| 483 | PULLDOWN = 1, | ||
| 484 | /// Pull-up to VDD | ||
| 485 | PULLUP = 2, | ||
| 486 | /// Set input at VDD/2 | ||
| 487 | VDD1_2 = 3, | ||
| 488 | } | ||
| 489 | |||
| 490 | impl From<Time> for TACQ_A { | ||
| 491 | fn from(time: Time) -> Self { | ||
| 492 | match time { | ||
| 493 | Time::_3US => TACQ_A::_3US, | ||
| 494 | Time::_5US => TACQ_A::_5US, | ||
| 495 | Time::_10US => TACQ_A::_10US, | ||
| 496 | Time::_15US => TACQ_A::_15US, | ||
| 497 | Time::_20US => TACQ_A::_20US, | ||
| 498 | Time::_40US => TACQ_A::_40US, | ||
| 499 | } | ||
| 500 | } | ||
| 501 | } | ||
| 502 | |||
| 503 | /// Acquisition time, the time the SAADC uses to sample the input voltage | ||
| 504 | #[non_exhaustive] | ||
| 505 | #[derive(Clone, Copy)] | ||
| 506 | pub enum Time { | ||
| 507 | /// 3 us | ||
| 508 | _3US = 0, | ||
| 509 | /// 5 us | ||
| 510 | _5US = 1, | ||
| 511 | /// 10 us | ||
| 512 | _10US = 2, | ||
| 513 | /// 15 us | ||
| 514 | _15US = 3, | ||
| 515 | /// 20 us | ||
| 516 | _20US = 4, | ||
| 517 | /// 40 us | ||
| 518 | _40US = 5, | ||
| 519 | } | ||
| 520 | |||
| 521 | impl From<Oversample> for OVERSAMPLE_A { | ||
| 522 | fn from(oversample: Oversample) -> Self { | ||
| 523 | match oversample { | ||
| 524 | Oversample::BYPASS => OVERSAMPLE_A::BYPASS, | ||
| 525 | Oversample::OVER2X => OVERSAMPLE_A::OVER2X, | ||
| 526 | Oversample::OVER4X => OVERSAMPLE_A::OVER4X, | ||
| 527 | Oversample::OVER8X => OVERSAMPLE_A::OVER8X, | ||
| 528 | Oversample::OVER16X => OVERSAMPLE_A::OVER16X, | ||
| 529 | Oversample::OVER32X => OVERSAMPLE_A::OVER32X, | ||
| 530 | Oversample::OVER64X => OVERSAMPLE_A::OVER64X, | ||
| 531 | Oversample::OVER128X => OVERSAMPLE_A::OVER128X, | ||
| 532 | Oversample::OVER256X => OVERSAMPLE_A::OVER256X, | ||
| 533 | } | ||
| 534 | } | ||
| 535 | } | ||
| 536 | |||
| 537 | /// Oversample control | ||
| 538 | #[non_exhaustive] | ||
| 539 | #[derive(Clone, Copy)] | ||
| 540 | pub enum Oversample { | ||
| 541 | /// Bypass oversampling | ||
| 542 | BYPASS = 0, | ||
| 543 | /// Oversample 2x | ||
| 544 | OVER2X = 1, | ||
| 545 | /// Oversample 4x | ||
| 546 | OVER4X = 2, | ||
| 547 | /// Oversample 8x | ||
| 548 | OVER8X = 3, | ||
| 549 | /// Oversample 16x | ||
| 550 | OVER16X = 4, | ||
| 551 | /// Oversample 32x | ||
| 552 | OVER32X = 5, | ||
| 553 | /// Oversample 64x | ||
| 554 | OVER64X = 6, | ||
| 555 | /// Oversample 128x | ||
| 556 | OVER128X = 7, | ||
| 557 | /// Oversample 256x | ||
| 558 | OVER256X = 8, | ||
| 559 | } | ||
| 560 | |||
| 561 | impl From<Resolution> for VAL_A { | ||
| 562 | fn from(resolution: Resolution) -> Self { | ||
| 563 | match resolution { | ||
| 564 | Resolution::_8BIT => VAL_A::_8BIT, | ||
| 565 | Resolution::_10BIT => VAL_A::_10BIT, | ||
| 566 | Resolution::_12BIT => VAL_A::_12BIT, | ||
| 567 | Resolution::_14BIT => VAL_A::_14BIT, | ||
| 568 | } | ||
| 569 | } | ||
| 570 | } | ||
| 571 | |||
| 572 | /// Set the resolution | ||
| 573 | #[non_exhaustive] | ||
| 574 | #[derive(Clone, Copy)] | ||
| 575 | pub enum Resolution { | ||
| 576 | /// 8 bits | ||
| 577 | _8BIT = 0, | ||
| 578 | /// 10 bits | ||
| 579 | _10BIT = 1, | ||
| 580 | /// 12 bits | ||
| 581 | _12BIT = 2, | ||
| 582 | /// 14 bits | ||
| 583 | _14BIT = 3, | ||
| 584 | } | ||
| 585 | |||
| 408 | pub(crate) mod sealed { | 586 | pub(crate) mod sealed { |
| 409 | use super::*; | 587 | use super::*; |
| 410 | 588 | ||
