|
| 1 | +//! Analog-digital conversion traits |
| 2 | +
|
| 3 | +/// Non-blocking ADC traits |
| 4 | +pub mod nb { |
| 5 | + /// A marker trait to identify MCU pins that can be used as inputs to an ADC channel. |
| 6 | + /// |
| 7 | + /// This marker trait denotes an object, i.e. a GPIO pin, that is ready for use as an input to the |
| 8 | + /// ADC. As ADCs channels can be supplied by multiple pins, this trait defines the relationship |
| 9 | + /// between the physical interface and the ADC sampling buffer. |
| 10 | + /// |
| 11 | + /// ``` |
| 12 | + /// # use core::marker::PhantomData; |
| 13 | + /// # use embedded_hal::adc::nb::Channel; |
| 14 | + /// |
| 15 | + /// struct Adc1; // Example ADC with single bank of 8 channels |
| 16 | + /// struct Gpio1Pin1<MODE>(PhantomData<MODE>); |
| 17 | + /// struct Analog(()); // marker type to denote a pin in "analog" mode |
| 18 | + /// |
| 19 | + /// // GPIO 1 pin 1 can supply an ADC channel when it is configured in Analog mode |
| 20 | + /// impl Channel<Adc1> for Gpio1Pin1<Analog> { |
| 21 | + /// type ID = u8; // ADC channels are identified numerically |
| 22 | + /// |
| 23 | + /// fn channel(&self) -> Self::ID { |
| 24 | + /// 7_u8 // GPIO pin 1 is connected to ADC channel 7 |
| 25 | + /// } |
| 26 | + /// } |
| 27 | + /// |
| 28 | + /// struct Adc2; // ADC with two banks of 16 channels |
| 29 | + /// struct Gpio2PinA<MODE>(PhantomData<MODE>); |
| 30 | + /// struct AltFun(()); // marker type to denote some alternate function mode for the pin |
| 31 | + /// |
| 32 | + /// // GPIO 2 pin A can supply an ADC channel when it's configured in some alternate function mode |
| 33 | + /// impl Channel<Adc2> for Gpio2PinA<AltFun> { |
| 34 | + /// type ID = (u8, u8); // ADC channels are identified by bank number and channel number |
| 35 | + /// |
| 36 | + /// fn channel(&self) -> Self::ID { |
| 37 | + /// (0, 3) // bank 0 channel 3 |
| 38 | + /// } |
| 39 | + /// } |
| 40 | + /// ``` |
| 41 | + pub trait Channel<ADC> { |
| 42 | + /// Channel ID type |
| 43 | + /// |
| 44 | + /// A type used to identify this ADC channel. For example, if the ADC has eight channels, this |
| 45 | + /// might be a `u8`. If the ADC has multiple banks of channels, it could be a tuple, like |
| 46 | + /// `(u8: bank_id, u8: channel_id)`. |
| 47 | + type ID: Copy; |
| 48 | + |
| 49 | + /// Get the specific ID that identifies this channel, for example `0_u8` for the first ADC |
| 50 | + /// channel, if Self::ID is u8. |
| 51 | + fn channel(&self) -> Self::ID; |
| 52 | + } |
| 53 | + |
| 54 | + /// ADCs that sample on single channels per request, and do so at the time of the request. |
| 55 | + /// |
| 56 | + /// This trait is the interface to an ADC that is configured to read a specific channel at the time |
| 57 | + /// of the request (in contrast to continuous asynchronous sampling). |
| 58 | + /// |
| 59 | + /// ``` |
| 60 | + /// use embedded_hal::adc::nb::{Channel, OneShot}; |
| 61 | + /// |
| 62 | + /// struct MyAdc; // 10-bit ADC, with 5 channels |
| 63 | + /// # impl MyAdc { |
| 64 | + /// # pub fn power_up(&mut self) {} |
| 65 | + /// # pub fn power_down(&mut self) {} |
| 66 | + /// # pub fn do_conversion(&mut self, chan: u8) -> u16 { 0xAA55_u16 } |
| 67 | + /// # } |
| 68 | + /// |
| 69 | + /// impl<WORD, PIN> OneShot<MyAdc, WORD, PIN> for MyAdc |
| 70 | + /// where |
| 71 | + /// WORD: From<u16>, |
| 72 | + /// PIN: Channel<MyAdc, ID=u8>, |
| 73 | + /// { |
| 74 | + /// type Error = (); |
| 75 | + /// |
| 76 | + /// fn read(&mut self, pin: &mut PIN) -> nb::Result<WORD, Self::Error> { |
| 77 | + /// let chan = 1 << pin.channel(); |
| 78 | + /// self.power_up(); |
| 79 | + /// let result = self.do_conversion(chan); |
| 80 | + /// self.power_down(); |
| 81 | + /// Ok(result.into()) |
| 82 | + /// } |
| 83 | + /// } |
| 84 | + /// ``` |
| 85 | + pub trait OneShot<ADC, Word, Pin: Channel<ADC>> { |
| 86 | + /// Error type returned by ADC methods |
| 87 | + type Error; |
| 88 | + |
| 89 | + /// Request that the ADC begin a conversion on the specified pin |
| 90 | + /// |
| 91 | + /// This method takes a `Pin` reference, as it is expected that the ADC will be able to sample |
| 92 | + /// whatever channel underlies the pin. |
| 93 | + fn read(&mut self, pin: &mut Pin) -> nb::Result<Word, Self::Error>; |
| 94 | + } |
| 95 | +} |
0 commit comments