-
Notifications
You must be signed in to change notification settings - Fork 226
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add atmega-hal examples #524
Changes from 8 commits
3eb4d29
807f2bb
f033602
8fcab70
b31b3d3
6fa3aee
988ec2b
6a34fe0
7432b3e
2a53fd1
18b58b2
47ab236
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[build] | ||
target = "../../avr-specs/avr-atmega2560.json" | ||
|
||
[target.'cfg(target_arch = "avr")'] | ||
runner = "ravedude -cb 57600 mega2560" | ||
|
||
[unstable] | ||
build-std = ["core"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
[package] | ||
name = "atmega2560-examples" | ||
version = "0.0.0" | ||
authors = ["Rahix <[email protected]>", "Armandas Jarušauskas <[email protected]>"] | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
panic-halt = "0.2.0" | ||
ufmt = "0.2.0" | ||
nb = "1.1.0" | ||
embedded-hal = "1.0" | ||
avr-device = { version = "0.5.4", features = ["rt"] } | ||
|
||
[dependencies.embedded-hal-v0] | ||
version = "0.2.3" | ||
package = "embedded-hal" | ||
|
||
[dependencies.atmega-hal] | ||
path = "../../mcu/atmega-hal/" | ||
features = ["atmega2560"] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atmega_hal::adc::channel; | ||
use atmega_hal::clock; | ||
use atmega_hal::delay::Delay; | ||
use atmega_hal::usart::{Baudrate, Usart}; | ||
use embedded_hal::delay::DelayNs; | ||
use panic_halt as _; | ||
|
||
type Adc = atmega_hal::adc::Adc<clock::MHz16>; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
|
||
let mut delay = Delay::<clock::MHz16>::new(); | ||
|
||
// set up serial interface for text output | ||
let mut serial = Usart::new( | ||
dp.USART0, | ||
pins.pe0, | ||
pins.pe1.into_output(), | ||
Baudrate::<clock::MHz16>::new(57600), | ||
); | ||
|
||
let mut adc = Adc::new(dp.ADC, Default::default()); | ||
|
||
let (vbg, gnd) = ( | ||
adc.read_blocking(&channel::Vbg), | ||
adc.read_blocking(&channel::Gnd), | ||
); | ||
ufmt::uwriteln!(&mut serial, "Vbandgap: {}", vbg).unwrap(); | ||
ufmt::uwriteln!(&mut serial, "Ground: {}", gnd).unwrap(); | ||
|
||
// To store multiple channels in an array, we use the `into_channel()` method. | ||
let channels: [atmega_hal::adc::Channel; 16] = [ | ||
pins.pf0.into_analog_input(&mut adc).into_channel(), | ||
pins.pf1.into_analog_input(&mut adc).into_channel(), | ||
pins.pf2.into_analog_input(&mut adc).into_channel(), | ||
pins.pf3.into_analog_input(&mut adc).into_channel(), | ||
pins.pf4.into_analog_input(&mut adc).into_channel(), | ||
pins.pf5.into_analog_input(&mut adc).into_channel(), | ||
pins.pf6.into_analog_input(&mut adc).into_channel(), | ||
pins.pf7.into_analog_input(&mut adc).into_channel(), | ||
pins.pk0.into_analog_input(&mut adc).into_channel(), | ||
pins.pk1.into_analog_input(&mut adc).into_channel(), | ||
pins.pk2.into_analog_input(&mut adc).into_channel(), | ||
pins.pk3.into_analog_input(&mut adc).into_channel(), | ||
pins.pk4.into_analog_input(&mut adc).into_channel(), | ||
pins.pk5.into_analog_input(&mut adc).into_channel(), | ||
pins.pk6.into_analog_input(&mut adc).into_channel(), | ||
pins.pk7.into_analog_input(&mut adc).into_channel(), | ||
]; | ||
|
||
loop { | ||
for (i, ch) in channels.iter().enumerate() { | ||
let v = adc.read_blocking(ch); | ||
ufmt::uwrite!(&mut serial, "A{}: {} ", i, v).unwrap(); | ||
} | ||
|
||
ufmt::uwriteln!(&mut serial, "").unwrap(); | ||
delay.delay_ms(1000); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use panic_halt as _; | ||
use atmega_hal::clock; | ||
use atmega_hal::delay::Delay; | ||
use embedded_hal::delay::DelayNs; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
let mut delay = Delay::<clock::MHz16>::new(); | ||
|
||
let mut led = pins.pb7.into_output(); | ||
|
||
loop { | ||
led.toggle(); | ||
delay.delay_ms(1000); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atmega_hal::clock; | ||
use atmega_hal::usart::{Baudrate, Usart}; | ||
use panic_halt as _; | ||
|
||
type I2c = atmega_hal::i2c::I2c<clock::MHz16>; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
|
||
// set up serial interface for text output | ||
let mut serial = Usart::new( | ||
dp.USART0, | ||
pins.pe0, | ||
pins.pe1.into_output(), | ||
Baudrate::<clock::MHz16>::new(57600), | ||
); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, not entirely correct, I do have a comment ;) You're hard coding the clock-rate in different parts of the code here. This works, but it will make changes very tedious later on. Instead, I would recommend to define a project-wide clock type alias which is then referenced everywhere else. Maybe you can do this in the examples to demonstrate what people should be doing? In code, it would look like this: /// The project-global CPU clock speed. This type alias should be defined once
/// in a project and then referenced whereever types need the clock speed.
type CoreClock = atmega_hal::clocl::MHz16;
type I2c = atmega_hal::i2c::I2c<crate::CoreClock>;
// ...
let mut serial = Usart::new(
dp.USART0,
pins.pe0,
pins.pe1.into_output(),
Baudrate::<crate::CoreClock>::new(57600),
); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a good point, thanks! |
||
|
||
let mut i2c = I2c::new( | ||
dp.TWI, | ||
pins.pd1.into_pull_up_input(), | ||
pins.pd0.into_pull_up_input(), | ||
50_000, | ||
); | ||
|
||
ufmt::uwriteln!(&mut serial, "Write direction test:\r").unwrap(); | ||
i2c.i2cdetect(&mut serial, atmega_hal::i2c::Direction::Write) | ||
.unwrap(); | ||
ufmt::uwriteln!(&mut serial, "\r\nRead direction test:\r").unwrap(); | ||
i2c.i2cdetect(&mut serial, atmega_hal::i2c::Direction::Read) | ||
.unwrap(); | ||
|
||
loop {} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
//! This example demonstrates how to set up a SPI interface and communicate | ||
//! over it. The physical hardware configuration consists of connecting a | ||
//! jumper directly from pin `PB2` to pin `PB3`. | ||
//! | ||
//! Run the program using `cargo run`. | ||
//! You should see it output the line `data: 42` repeatedly. | ||
//! If the output you see is `data: 255`, you may need to check your jumper. | ||
|
||
#![no_std] | ||
#![no_main] | ||
|
||
use atmega_hal::clock; | ||
use atmega_hal::spi; | ||
use atmega_hal::usart::{Baudrate, Usart}; | ||
use atmega_hal::delay::Delay; | ||
use embedded_hal::delay::DelayNs; | ||
use embedded_hal::spi::SpiBus; | ||
use panic_halt as _; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
|
||
let mut delay = Delay::<clock::MHz16>::new(); | ||
|
||
// set up serial interface for text output | ||
let mut serial = Usart::new( | ||
dp.USART0, | ||
pins.pe0, | ||
pins.pe1.into_output(), | ||
Baudrate::<clock::MHz16>::new(57600), | ||
); | ||
|
||
// Create SPI interface. | ||
let (mut spi, _) = spi::Spi::new( | ||
dp.SPI, | ||
pins.pb1.into_output(), | ||
pins.pb2.into_output(), | ||
pins.pb3.into_pull_up_input(), | ||
pins.pb0.into_output(), | ||
spi::Settings::default(), | ||
); | ||
|
||
loop { | ||
// Send a byte | ||
let data_out: [u8; 1] = [42]; | ||
let mut data_in: [u8; 1] = [0]; | ||
// Send a byte | ||
// Because MISO is connected to MOSI, the read data should be the same | ||
spi.transfer(&mut data_in, &data_out).unwrap(); | ||
|
||
ufmt::uwriteln!(&mut serial, "data: {}\r", data_in[0]).unwrap(); | ||
delay.delay_ms(1000); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atmega_hal::prelude::*; | ||
use atmega_hal::clock; | ||
use atmega_hal::usart::{Baudrate, Usart}; | ||
use panic_halt as _; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
let mut serial = Usart::new( | ||
dp.USART0, | ||
pins.pe0, | ||
pins.pe1.into_output(), | ||
Baudrate::<clock::MHz16>::new(57600), | ||
); | ||
|
||
ufmt::uwriteln!(&mut serial, "Hello from ATmega!\r").unwrap(); | ||
|
||
loop { | ||
// Read a byte from the serial connection | ||
let b = nb::block!(serial.read()).unwrap(); | ||
|
||
// Answer | ||
ufmt::uwriteln!(&mut serial, "Got {}!\r", b).unwrap(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a though: We could use one of the examples to show off a nicer way for dealing with delays: Using the previously mentioned
CoreClock
, you can define more things:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I saw this one in the Arduino HAL implementation. Will update blink example.