Skip to content

Commit

Permalink
Merge pull request #111 from flipperzero-rs/i2c
Browse files Browse the repository at this point in the history
flipperzero: I2C interface
  • Loading branch information
str4d authored Oct 15, 2023
2 parents c140de8 + c42d5ca commit 1fcf63d
Show file tree
Hide file tree
Showing 5 changed files with 435 additions and 0 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).

## Unreleased

### Added

- `flipperzero::gpio::i2c`, providing a Rust interface to the external 3.3V I2C
bus over GPIO pins C0 and C1, as well as the internal (power) I2C bus.

### Changed

- Migrated to SDK 39.1 (firmware 0.92.2).

## [0.11.0]
Expand Down
81 changes: 81 additions & 0 deletions crates/flipperzero/examples/i2c-ds3231.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
//! Example of communicating with a [DS3231] Real-Time Clock.
//!
//! [DS3231]: https://www.analog.com/media/en/technical-documentation/data-sheets/DS3231.pdf
#![no_main]
#![no_std]

// Required for panic handler
extern crate flipperzero_rt;

// Required for allocator
#[cfg(feature = "alloc")]
extern crate flipperzero_alloc;

use flipperzero::{error, furi::time::Duration, gpio::i2c, println};
use flipperzero_rt::{entry, manifest};
use ufmt::derive::uDebug;

manifest!(name = "I2C DS3231 Example");
entry!(main);

#[derive(Debug, uDebug)]
enum Hour {
F12 { hour: u8, pm: bool },
F24(u8),
}

#[derive(Debug, uDebug)]
struct RtcTime {
year: u8,
month: u8,
date: u8,
day: u8,
hour: Hour,
minutes: u8,
seconds: u8,
}

impl RtcTime {
fn parse(data: [u8; 7]) -> Self {
let unbcd = |b: u8| 10 * (b >> 4) + (b & 0x0F);

const FORMAT_12HR: u8 = 0b0100_0000;

Self {
year: unbcd(data[6]),
month: unbcd(data[5] & 0x1F),
date: unbcd(data[4] & 0x3F),
day: data[3] & 0x07,
hour: if data[2] & FORMAT_12HR != 0 {
Hour::F12 {
hour: unbcd(data[2] & 0x1F),
pm: data[2] & 0b0010_0000 != 0,
}
} else {
Hour::F24(unbcd(data[2] & 0x3F))
},
minutes: unbcd(data[1] & 0x7F),
seconds: unbcd(data[0] & 0x7F),
}
}
}

fn main(_args: *mut u8) -> i32 {
let mut bus = i2c::Bus::EXTERNAL.acquire();
let rtc = i2c::DeviceAddress::new(0x68);
let timeout = Duration::from_millis(50);

if bus.is_device_ready(rtc, timeout) {
let mut data = [0; 7];
if bus.read_exact(rtc, 0x00, &mut data, timeout).is_ok() {
println!("Time: {:?}", RtcTime::parse(data));
} else {
error!("Could not read from DS3231");
}
} else {
error!("DS3231 is not connected and ready");
}

0
}
Loading

0 comments on commit 1fcf63d

Please sign in to comment.