summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDeterminant <[email protected]>2017-09-25 21:17:18 -0400
committerDeterminant <[email protected]>2017-09-25 21:17:18 -0400
commite3a0899abde145a2212082041ffeaf0ca6529300 (patch)
tree13f7a49a9139c86b35f3a413a4e03b10d88b37df
parent33063316e02911b1d2e0d02b53043b89ce87dea1 (diff)
...
-rw-r--r--src/ds3231.rs24
-rw-r--r--src/i2c.rs12
-rw-r--r--src/main.rs8
3 files changed, 20 insertions, 24 deletions
diff --git a/src/ds3231.rs b/src/ds3231.rs
index caf4825..6800f6a 100644
--- a/src/ds3231.rs
+++ b/src/ds3231.rs
@@ -1,13 +1,11 @@
extern crate stm32f103xx;
-use ::i2c::{I2C, TransDir, DutyType};
+use i2c::{I2C, TransDir, DutyType};
const DS3231_ADDR: u8 = 0b1101000;
const DS3231_REG_SEC: u8 = 0x00;
const DS3231_REG_CTL: u8 = 0x0e;
-pub struct DS3231<'a> {
- i2c: I2C<'a>
-}
+pub struct DS3231<'a, 'b>(I2C<'a, 'b>);
pub struct Date {
pub second: u8,
@@ -21,10 +19,10 @@ pub struct Date {
pub am_enabled: bool
}
-impl<'a> DS3231<'a> {
+impl<'a, 'b> DS3231<'a, 'b> {
pub fn new(i2c_reg: &'a stm32f103xx::i2c1::RegisterBlock,
- rcc_reg: &'a stm32f103xx::rcc::RegisterBlock) -> DS3231<'a> {
- DS3231{i2c: I2C::new(i2c_reg, rcc_reg)}
+ rcc_reg: &'b stm32f103xx::rcc::RegisterBlock) -> DS3231<'a, 'b> {
+ DS3231(I2C::new(i2c_reg, rcc_reg))
}
fn bcd2dec(bcd: u8) -> u8 {
@@ -36,7 +34,7 @@ impl<'a> DS3231<'a> {
}
pub fn init(&self) {
- let i2c = &self.i2c;
+ let &DS3231(ref i2c) = self;
i2c.init(0x01, 400_000, DutyType::DUTY1);
i2c.start(true, true);
i2c.send_addr(DS3231_ADDR, TransDir::TRANSMITTER, true);
@@ -48,7 +46,7 @@ impl<'a> DS3231<'a> {
pub fn read_fulldate(&self) -> Date {
let mut buf: [u8; 7] = [0; 7];
- let i2c = &self.i2c;
+ let &DS3231(ref i2c) = self;
i2c.conf_ack(true); /* enable ack */
i2c.start(true, true); /* start condition (for writing reg addr) */
i2c.send_addr(DS3231_ADDR, TransDir::TRANSMITTER, true);
@@ -71,17 +69,17 @@ impl<'a> DS3231<'a> {
let am = if am_enabled {(buf[2] >> 5) & 1 == 0} else {hour < 12};
Date{second: DS3231::bcd2dec(buf[0]),
minute: DS3231::bcd2dec(buf[1]),
- hour: hour,
+ hour,
day: DS3231::bcd2dec(buf[3]),
date: DS3231::bcd2dec(buf[4]),
month: DS3231::bcd2dec(buf[5]),
year: DS3231::bcd2dec(buf[6]),
- am: am,
- am_enabled: am_enabled}
+ am,
+ am_enabled}
}
pub fn write_fulldate(&self, date: &Date) {
- let i2c = &self.i2c;
+ let &DS3231(ref i2c) = self;
let hour = if date.am_enabled {
(1 << 6) | ((if date.am {0} else {1}) << 5) |
((date.hour / 10) << 4) | (date.hour % 10)
diff --git a/src/i2c.rs b/src/i2c.rs
index 73a8f3b..473442c 100644
--- a/src/i2c.rs
+++ b/src/i2c.rs
@@ -14,9 +14,9 @@ const FLAGS_MASK: u32 = 0x00ffffff;
const HSI_VALUE: u32 = 8000000;
const HSE_VALUE: u32 = 8000000;
-pub struct I2C<'a> {
+pub struct I2C<'a, 'b> {
i2c: &'a stm32f103xx::i2c1::RegisterBlock,
- rcc: &'a stm32f103xx::rcc::RegisterBlock,
+ rcc: &'b stm32f103xx::rcc::RegisterBlock,
}
pub enum TransDir {
@@ -29,10 +29,10 @@ pub enum DutyType {
DUTY1
}
-impl<'a> I2C<'a> {
- pub fn new(i2c_reg: &'a stm32f103xx::i2c1::RegisterBlock,
- rcc_reg: &'a stm32f103xx::rcc::RegisterBlock) -> I2C<'a> {
- I2C{i2c: i2c_reg, rcc: rcc_reg}
+impl<'a, 'b> I2C<'a, 'b> {
+ pub fn new(i2c: &'a stm32f103xx::i2c1::RegisterBlock,
+ rcc: &'b stm32f103xx::rcc::RegisterBlock) -> I2C<'a, 'b> {
+ I2C{i2c, rcc}
}
fn get_pclk1(&self) -> u32 {
diff --git a/src/main.rs b/src/main.rs
index a79107c..0b63e58 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -53,9 +53,7 @@ fn update_clock() {
minute: min,
hour: hr, ..} = RTC.as_mut().unwrap()
.read_fulldate();
- TIME = Clock{sec: sec,
- min: min,
- hr: hr,
+ TIME = Clock{sec, min, hr,
reset: RESET_PERIOD};
}
@@ -74,9 +72,9 @@ fn systick_handler() {
exception!(SYS_TICK, systick_handler);
impl<'a> ShiftRegister<'a> {
- fn new(g: &'a stm32f103xx::gpioa::RegisterBlock,
+ fn new(gpioa: &'a stm32f103xx::gpioa::RegisterBlock,
width: u8) -> ShiftRegister<'a> {
- let this = ShiftRegister{gpioa: g, width: width};
+ let this = ShiftRegister{gpioa, width};
this
}