-
Notifications
You must be signed in to change notification settings - Fork 227
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 examples to atmega-hal documentation #591
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e94f5af
ADC example.
armandas 5ef8a84
EEPROM example.
armandas 10d02a2
I2C example.
armandas f07f7eb
Port example.
armandas d9aa6fb
SPI example.
armandas 5f3eb81
USART example.
armandas 7d961b8
Apply suggestions from code review
armandas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#![no_std] | ||
#![no_main] | ||
|
||
use atmega_hal::delay::Delay; | ||
use atmega_hal::usart::{Baudrate, Usart}; | ||
use atmega_hal::Eeprom; | ||
use embedded_hal::delay::DelayNs; | ||
use panic_halt as _; | ||
|
||
// Define core clock in the root crate | ||
type CoreClock = atmega_hal::clock::MHz16; | ||
|
||
const BOOT_COUNT_OFFSET: u16 = 0; | ||
|
||
#[avr_device::entry] | ||
fn main() -> ! { | ||
let dp = atmega_hal::Peripherals::take().unwrap(); | ||
let pins = atmega_hal::pins!(dp); | ||
|
||
let mut delay = Delay::<crate::CoreClock>::new(); | ||
|
||
// set up serial interface for text output | ||
let mut serial = Usart::new( | ||
dp.USART0, | ||
pins.pe0, | ||
pins.pe1.into_output(), | ||
Baudrate::<crate::CoreClock>::new(57600), | ||
); | ||
|
||
let mut eeprom = Eeprom::new(dp.EEPROM); | ||
|
||
let mut boot_count = eeprom.read_byte(BOOT_COUNT_OFFSET); | ||
boot_count = boot_count.wrapping_add(1); | ||
eeprom.write_byte(BOOT_COUNT_OFFSET, boot_count); | ||
|
||
ufmt::uwriteln!(&mut serial, "Boot count: {}", boot_count).unwrap(); | ||
|
||
loop {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
A minor thing, but because people like copying example code for production use, let's talk about it:
I think this should be using a
saturating_add()
instead ofwrapping_add()
so the value doesn't cycle back to zero after 256 boots. The print statement should then display a different message on overflow. E.g.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.
I agree, examples are how people learn, so we should try to show the best code.
The issue with EEPROM is that the data is
0xFF
in the default/erased state, so we need to "initialize" it on first boot. A couple of possible solutions would be:u8::MAX - 1
u32
should be enough for everyone™)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.
Hmm, I see it is getting even more difficult... Tell you what, let's merge it as is and then maybe think about improvements in the future. I smell the solution being a custom "driver" to manage a bootcount, to contain all this complexity...