-
Using the examples in the documentation, I am trying to instantiate the ADC, so I can use it to read sensor values in my attiny85 application: #![no_std]
#![no_main]
use attiny_hal;
use panic_halt as _;
#[no_mangle]
pub extern "C" fn main() {
let dp = attiny_hal::Peripherals::take().unwrap();
let pins = attiny_hal::pins!(dp);
let mut led = pins.pb3.into_output();
let mut adc= attiny_hal::Adc::new(dp.ADC, Default::default());
// initialise more things
loop {
// do things.
}
} But when building this code, the compiler then complains because it wants me to be explicit about the types I am using. I don't really understand why this is necessary (as it was not necessary in the examples in the docs):
I have tried a number of things (none of which worked) but I felt I was not trying anything particularly informed (as I am new to embedded programming), so I was hoping someone more knowledgeable than myself could help me solve this problem. I feel like I am not doing anything differently than as is instructed in the documentation, so I am wondering if there's something I am missing. Thanks in advance! |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
First of all, this doesn't look right. You should be using the #[attiny_hal::entry]
fn main() -> ! {
...
} That out of the way, the problem is that the ADC needs to know about your clock-rate. As the rate is not fixed, you have to make it explicit in your code. The best way to do this is to define the clock-rate globally and then reference that type everywhere else. Then you can easily change the clock-rate in the future. It works like this: pub type CoreClock = attiny_hal::clock::MHz1;
#[attiny_hal::entry]
fn main() -> ! {
let mut adc = attiny_hal::Adc<CoreClock>::new(...);
} |
Beta Was this translation helpful? Give feedback.
-
On that note, we should really add some examples of using the bare HALs like |
Beta Was this translation helpful? Give feedback.
-
Thanks a lot for your quick, and helpful response! I ended up with: pub type CoreClock = attiny_hal::clock::MHz1;
let mut adc: attiny_hal::Adc<CoreClock> = attiny_hal::Adc::new(dp.ADC, Default::default()); That compiles now! 😄 Concerning your When I use the standard rust embedded programming approach (which is what is in my example above) it works perfectly fine; it builds, flashes and runs live on my attiny85 chip without issues. Does this macro do anything specific for AVR that makes it necessary? About the examples: I would be willing to contribute examples for attiny-hal as my project progresses. I will also be doing similar things with an atmega chip once I get my hands on a few. Perhaps we can discuss a set of examples I could work on for the repo? |
Beta Was this translation helpful? Give feedback.
-
For reference, I edited my initial code snippet to include |
Beta Was this translation helpful? Give feedback.
-
Do you have the You are right that you don't technically need it right now but that only works because we rely on the avr-libc runtime for the time being. As soon as that changes, the macro will be required. So to make sure your code continues to work, it should use the |
Beta Was this translation helpful? Give feedback.
-
Thanks! I didn't know about the |
Beta Was this translation helpful? Give feedback.
-
About
That's great to hear! My suggestion would be to create a |
Beta Was this translation helpful? Give feedback.
First of all, this doesn't look right. You should be using the
attiny_hal::entry
macro like this:That out of the way, the problem is that the ADC needs to know about your clock-rate. As the rate is not fixed, you have to make it explicit in your code. The best way to do this is to define the clock-rate globally and then reference that type everywhere else. Then you can easily change the clock-rate in the future. It works like this: