Skip to content
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 Arduino Uno Example #20

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Any platform that implements the [embedded-hal](https://github.com/rust-embedded

### Getting Started

This library aims to keep it simple in that to get started all you will have to do is supply the `HD44780::new` function a bunch of pins from your platform that implement the `OutputPin` trait for [embedded-hal](https://github.com/rust-embedded/embedded-hal) as well as a struct that implements the delay traits `DelayUs<u16>` and `DelayMs<u8>`.
This library aims to keep it simple in that to get started all you will have to do is supply the `HD44780::new` function a bunch of pins from your platform that implement the `OutputPin` trait for [embedded-hal](https://github.com/rust-embedded/embedded-hal) as well as a struct that implements the delay traits `DelayUs<u16>` and `DelayMs<u16>`.

```rust
// Code grabbed from the metro_m0 example
Expand Down
33 changes: 33 additions & 0 deletions examples/arduino-uno/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "arduino-uno"
version = "0.1.0"
authors = ["John Doneth <[email protected]>"]
edition = "2018"

[dependencies]
avr-hal-generic = { rev = "bfc5dfe", git = "https://github.com/Rahix/avr-hal.git" }
arduino-uno = { rev = "bfc5dfe", git = "https://github.com/Rahix/avr-hal.git" }
panic-halt = "0.2.0"
hd44780-driver = { path = "../.." }

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]

[dev-dependencies]
nb = "0.1.2"
ufmt = "0.1.0"

[profile.dev]
panic = "abort"
codegen-units = 1
incremental = false
lto = true
opt-level = "s"

[profile.release]
panic = "abort"
codegen-units = 1
debug = false
lto = true
opt-level = "s"
10 changes: 10 additions & 0 deletions examples/arduino-uno/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
```
hd44780 Example for the Arduino Uno
```

Building
```
rustup override set nightly-2020-07-24-x86_64-unknown-linux-gnu

cargo build -Z build-std=core --target avr-atmega328p.json --release
```
32 changes: 32 additions & 0 deletions examples/arduino-uno/avr-atmega328p.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
{
"arch": "avr",
"cpu": "atmega328p",
"data-layout": "e-P1-p:16:8-i8:8-i16:8-i32:8-i64:8-f32:8-f64:8-n8-a:8",
"env": "",
"executables": true,
"linker": "avr-gcc",
"linker-flavor": "gcc",
"linker-is-gnu": true,
"llvm-target": "avr-unknown-unknown",
"no-compiler-rt": true,
"os": "unknown",
"position-independent-executables": false,
"exe-suffix": ".elf",
"eh-frame-header": false,
"pre-link-args": {
"gcc": [
"-Os",
"-mmcu=atmega328p"
]
},
"late-link-args": {
"gcc": [
"-lc",
"-lgcc"
]
},
"target-c-int-width": "16",
"target-endian": "little",
"target-pointer-width": "16",
"vendor": "unknown"
}
68 changes: 68 additions & 0 deletions examples/arduino-uno/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![no_std]
#![no_main]

extern crate panic_halt;
use arduino_uno::prelude::*;
use hd44780_driver::HD44780;

use embedded_hal::blocking::delay;

use arduino_uno::Delay;


#[no_mangle]
pub extern "C" fn main() -> ! {
let dp = arduino_uno::Peripherals::take().unwrap();

let mut delay = arduino_uno::Delay::new();
let mut pins = arduino_uno::Pins::new(dp.PORTB, dp.PORTC, dp.PORTD);

// Digital pin 13 is also connected to an onboard LED marked "L"
//let mut led = pins.d13.into_output(&mut pins.ddr);

//led.set_high().void_unwrap();

//delay.delay_ms(500);

let mut lcd = HD44780::new_8bit(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is more likely that 4bit interface will be used with Arduino UNO


pins.d12.into_output(&mut pins.ddr), // Register Select pin
pins.d13.into_output(&mut pins.ddr), // Enable pin



pins.d11.into_output(&mut pins.ddr), // d0
pins.d10.into_output(&mut pins.ddr), // d1
pins.d9.into_output(&mut pins.ddr), // d2
pins.d8.into_output(&mut pins.ddr), // d3

pins.d4.into_output(&mut pins.ddr), // d4
pins.d5.into_output(&mut pins.ddr), // d5
pins.d6.into_output(&mut pins.ddr), // d6
pins.d7.into_output(&mut pins.ddr), // d7

&mut delay,
).unwrap();

// Unshift display and set cursor to 0
lcd.reset(&mut delay).unwrap();

// Clear existing characters
lcd.clear(&mut delay).unwrap();

// Display the following string
lcd.write_str("Hello, world!", &mut delay).unwrap();

loop { }

// loop {
// led.toggle().void_unwrap();
// delay.delay_ms(200);
// led.toggle().void_unwrap();
// delay.delay_ms(200);
// led.toggle().void_unwrap();
// delay.delay_ms(200);
// led.toggle().void_unwrap();
// delay.delay_ms(2800);
// }
}
4 changes: 2 additions & 2 deletions src/bus/eightbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ impl<
D7: OutputPin,
> DataBus for EightBitBus<RS, EN, D0, D1, D2, D3, D4, D5, D6, D7>
{
fn write<D: DelayUs<u16> + DelayMs<u8>>(
fn write<D: DelayUs<u16> + DelayMs<u16>>(
&mut self,
byte: u8,
data: bool,
Expand All @@ -159,7 +159,7 @@ impl<
self.set_bus_bits(byte)?;

self.en.set_high().map_err(|_| Error)?;
delay.delay_ms(2u8);
delay.delay_ms(2u16);
self.en.set_low().map_err(|_| Error)?;

if data {
Expand Down
6 changes: 3 additions & 3 deletions src/bus/fourbit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin,
impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin, D7: OutputPin>
DataBus for FourBitBus<RS, EN, D4, D5, D6, D7>
{
fn write<D: DelayUs<u16> + DelayMs<u8>>(
fn write<D: DelayUs<u16> + DelayMs<u16>>(
&mut self,
byte: u8,
data: bool,
Expand All @@ -126,14 +126,14 @@ impl<RS: OutputPin, EN: OutputPin, D4: OutputPin, D5: OutputPin, D6: OutputPin,

// Pulse the enable pin to recieve the upper nibble
self.en.set_high().map_err(|_| Error)?;
delay.delay_ms(2u8);
delay.delay_ms(2u16);
self.en.set_low().map_err(|_| Error)?;

self.write_lower_nibble(byte)?;

// Pulse the enable pin to recieve the lower nibble
self.en.set_high().map_err(|_| Error)?;
delay.delay_ms(2u8);
delay.delay_ms(2u16);
self.en.set_low().map_err(|_| Error)?;

if data {
Expand Down
6 changes: 3 additions & 3 deletions src/bus/i2c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl<I2C: Write> I2CBus<I2C> {

/// Write a nibble to the lcd
/// The nibble should be in the upper part of the byte
fn write_nibble<D: DelayUs<u16> + DelayMs<u8>>(
fn write_nibble<D: DelayUs<u16> + DelayMs<u16>>(
&mut self,
nibble: u8,
data: bool,
Expand All @@ -33,13 +33,13 @@ impl<I2C: Write> I2CBus<I2C> {
let byte = nibble | rs | BACKLIGHT;

let _ = self.i2c_bus.write(self.address, &[byte, byte | ENABLE]);
delay.delay_ms(2u8);
delay.delay_ms(2u16);
let _ = self.i2c_bus.write(self.address, &[byte]);
}
}

impl<I2C: Write> DataBus for I2CBus<I2C> {
fn write<D: DelayUs<u16> + DelayMs<u8>>(
fn write<D: DelayUs<u16> + DelayMs<u16>>(
&mut self,
byte: u8,
data: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/bus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub use self::i2c::I2CBus;
use crate::error::Result;

pub trait DataBus {
fn write<D: DelayUs<u16> + DelayMs<u8>>(
fn write<D: DelayUs<u16> + DelayMs<u16>>(
&mut self,
byte: u8,
data: bool,
Expand Down
1 change: 1 addition & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
#[derive(Debug)]
pub struct Error;
pub type Result<T> = core::result::Result<T, Error>;
Loading