Skip to content

Commit

Permalink
Fix compile error
Browse files Browse the repository at this point in the history
fix(09-clocks-and-timers): Compile failed in release mode

docs: Update putting-it-all-together.md

fix: compile error in chapter 11

fix: compile error in chapter 14

fix: compile error in chapter 15

fix: compile error in chapter 16

chore: handle `Result` with unwrap rather than ok

chore: remove useless dependencies

fix: restore main.rs
  • Loading branch information
dzvon committed May 29, 2021
1 parent 3132ecd commit b6b9054
Show file tree
Hide file tree
Showing 22 changed files with 160 additions and 99 deletions.
7 changes: 4 additions & 3 deletions src/09-clocks-and-timers/auxiliary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ version = "0.1.0"
cortex-m = "0.6.3"
cortex-m-rt = "0.6.3"
panic-itm = "0.4.0"
stm32f3-discovery = "0.6.0"

[dependencies.f3]
features = ["rt"]
version = "0.6.1"
[dependencies.stm32f3]
version = "0.12.1"
features = ["stm32f303", "rt"]
29 changes: 20 additions & 9 deletions src/09-clocks-and-timers/auxiliary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,37 @@ extern crate panic_itm; // panic handler

pub use cortex_m::asm::{bkpt, nop};
pub use cortex_m_rt::entry;
pub use f3::{
hal::stm32f30x::{rcc, tim6},
led::Leds,
};
pub use stm32f3::stm32f303::{rcc, tim6, RCC, TIM6};
pub use stm32f3_discovery::switch_hal;

use f3::hal::{
prelude::*,
stm32f30x::{self, RCC, TIM6},
use stm32f3_discovery::{
leds::Leds,
stm32f3xx_hal::{prelude::*, stm32},
};

pub fn init() -> (
Leds,
&'static rcc::RegisterBlock,
&'static tim6::RegisterBlock,
) {
let p = stm32f30x::Peripherals::take().unwrap();
let p = stm32::Peripherals::take().unwrap();

let mut rcc = p.RCC.constrain();

let leds = Leds::new(p.GPIOE.split(&mut rcc.ahb));
let mut gpioe = p.GPIOE.split(&mut rcc.ahb);

let leds = Leds::new(
gpioe.pe8,
gpioe.pe9,
gpioe.pe10,
gpioe.pe11,
gpioe.pe12,
gpioe.pe13,
gpioe.pe14,
gpioe.pe15,
&mut gpioe.moder,
&mut gpioe.otyper,
);

(leds, unsafe { &*RCC::ptr() }, unsafe { &*TIM6::ptr() })
}
9 changes: 5 additions & 4 deletions src/09-clocks-and-timers/putting-it-all-together.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#![no_main]
#![no_std]

use aux9::{entry, tim6};
use aux9::{entry, switch_hal::OutputSwitch, tim6};

#[inline(never)]
fn delay(tim6: &tim6::RegisterBlock, ms: u16) {
Expand All @@ -24,7 +24,8 @@ fn delay(tim6: &tim6::RegisterBlock, ms: u16) {

#[entry]
fn main() -> ! {
let (mut leds, rcc, tim6) = aux9::init();
let (leds, rcc, tim6) = aux9::init();
let mut leds = leds.into_array();

// Power on the TIM6 timer
rcc.apb1enr.modify(|_, w| w.tim6en().set_bit());
Expand All @@ -45,9 +46,9 @@ fn main() -> ! {
for curr in 0..8 {
let next = (curr + 1) % 8;

leds[next].on();
leds[next].on().unwrap();
delay(tim6, ms);
leds[curr].off();
leds[curr].off().unwrap();
delay(tim6, ms);
}
}
Expand Down
9 changes: 5 additions & 4 deletions src/09-clocks-and-timers/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![no_main]
#![no_std]

use aux9::{entry, tim6};
use aux9::{entry, switch_hal::OutputSwitch, tim6};

#[inline(never)]
fn delay(tim6: &tim6::RegisterBlock, ms: u16) {
Expand All @@ -10,7 +10,8 @@ fn delay(tim6: &tim6::RegisterBlock, ms: u16) {

#[entry]
fn main() -> ! {
let (mut leds, rcc, tim6) = aux9::init();
let (leds, rcc, tim6) = aux9::init();
let mut leds = leds.into_array();

// TODO initialize TIM6

Expand All @@ -19,9 +20,9 @@ fn main() -> ! {
for curr in 0..8 {
let next = (curr + 1) % 8;

leds[next].on();
leds[next].on().unwrap();
delay(tim6, ms);
leds[curr].off();
leds[curr].off().unwrap();
delay(tim6, ms);
}
}
Expand Down
9 changes: 3 additions & 6 deletions src/11-usart/auxiliary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,10 @@ name = "aux11"
version = "0.1.0"

[dependencies]
cortex-m = "=0.5.6" # 0.5.11 introduces a breaking change. Use 0.5.6, since we know it works for this example
cortex-m = "0.6.3"
cortex-m-rt = "0.6.3"
panic-itm = "0.4.0"

[dependencies.f3]
features = ["rt"]
version = "0.6.1"
stm32f3-discovery = "0.6.0"

[features]
adapter = []
adapter = []
10 changes: 6 additions & 4 deletions src/11-usart/auxiliary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,18 @@ extern crate panic_itm; // panic handler

pub use cortex_m::{asm::bkpt, iprint, iprintln, peripheral::ITM};
pub use cortex_m_rt::entry;
pub use f3::hal::{prelude, serial::Serial, stm32f30x::usart1, time::MonoTimer};
pub use stm32f3_discovery::stm32f3xx_hal::stm32::usart1;

use f3::hal::{
use stm32f3_discovery::stm32f3xx_hal::{
prelude::*,
stm32f30x::{self, USART1},
serial::Serial,
stm32::{self, USART1},
time::MonoTimer,
};

pub fn init() -> (&'static mut usart1::RegisterBlock, MonoTimer, ITM) {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32f30x::Peripherals::take().unwrap();
let dp = stm32::Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
Expand Down
10 changes: 6 additions & 4 deletions src/11-usart/buffer-overrun.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
If you wrote your program like this:

``` rust
#![deny(unsafe_code)]
#![no_main]
#![no_std]

Expand All @@ -16,7 +15,9 @@ fn main() -> ! {

// Send a string
for byte in b"The quick brown fox jumps over the lazy dog.".iter() {
usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
usart1
.tdr
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
}

loop {}
Expand Down Expand Up @@ -108,7 +109,6 @@ to write to the `TDR` register without incurring in data loss.
Let's use that to slowdown the processor.

``` rust
#![deny(unsafe_code)]
#![no_main]
#![no_std]

Expand All @@ -125,7 +125,9 @@ fn main() -> ! {
// wait until it's safe to write to TDR
while usart1.isr.read().txe().bit_is_clear() {} // <- NEW!

usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
usart1
.tdr
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
}
let elapsed = instant.elapsed(); // in ticks

Expand Down
9 changes: 6 additions & 3 deletions src/11-usart/my-solution.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# My solution

```rust
#![deny(unsafe_code)]
#![no_main]
#![no_std]

Expand All @@ -27,7 +26,9 @@ fn main() -> ! {
// buffer full
for byte in b"error: buffer full\n\r" {
while usart1.isr.read().txe().bit_is_clear() {}
usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
usart1
.tdr
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
}

break;
Expand All @@ -38,7 +39,9 @@ fn main() -> ! {
// Respond
for byte in buffer.iter().rev().chain(&[b'\n', b'\r']) {
while usart1.isr.read().txe().bit_is_clear() {}
usart1.tdr.write(|w| w.tdr().bits(u16::from(*byte)));
usart1
.tdr
.write(|w| unsafe { w.tdr().bits(u16::from(*byte)) });
}

break;
Expand Down
5 changes: 3 additions & 2 deletions src/11-usart/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#![deny(unsafe_code)]
#![no_main]
#![no_std]

Expand All @@ -10,7 +9,9 @@ fn main() -> ! {
let (usart1, mono_timer, itm) = aux11::init();

// Send a single character
usart1.tdr.write(|w| w.tdr().bits(u16::from(b'X')));
usart1
.tdr
.write(|w| unsafe { w.tdr().bits(u16::from(b'X')) });

loop {}
}
7 changes: 2 additions & 5 deletions src/14-i2c/auxiliary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ name = "aux14"
version = "0.1.0"

[dependencies]
cortex-m = "=0.5.6"
cortex-m = "0.6.3"
cortex-m-rt = "0.6.3"
panic-itm = "0.4.0"

[dependencies.f3]
features = ["rt"]
version = "0.6.1"
stm32f3-discovery = "0.6.0"
14 changes: 7 additions & 7 deletions src/14-i2c/auxiliary/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,21 @@ extern crate panic_itm; // panic handler

pub use cortex_m::{asm::bkpt, iprint, iprintln};
pub use cortex_m_rt::entry;
pub use f3::hal::{delay::Delay, prelude, stm32f30x::i2c1};
pub use stm32f3_discovery::stm32f3xx_hal::{delay::Delay, prelude, stm32::i2c1};

use cortex_m::peripheral::ITM;
use f3::{
hal::{
use stm32f3_discovery::{
lsm303dlhc::Lsm303dlhc,
stm32f3xx_hal::{
i2c::I2c,
prelude::*,
stm32f30x::{self, I2C1},
stm32::{self, I2C1},
},
Lsm303dlhc,
};

pub fn init() -> (&'static i2c1::RegisterBlock, Delay, ITM) {
let cp = cortex_m::Peripherals::take().unwrap();
let dp = stm32f30x::Peripherals::take().unwrap();
let dp = stm32::Peripherals::take().unwrap();

let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
Expand All @@ -32,7 +32,7 @@ pub fn init() -> (&'static i2c1::RegisterBlock, Delay, ITM) {
let scl = gpiob.pb6.into_af4(&mut gpiob.moder, &mut gpiob.afrl);
let sda = gpiob.pb7.into_af4(&mut gpiob.moder, &mut gpiob.afrl);

let i2c = I2c::i2c1(dp.I2C1, (scl, sda), 400.khz(), clocks, &mut rcc.apb1);
let i2c = I2c::new(dp.I2C1, (scl, sda), 400.khz(), clocks, &mut rcc.apb1);

Lsm303dlhc::new(i2c).unwrap();

Expand Down
4 changes: 2 additions & 2 deletions src/14-i2c/read-several-registers.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Putting it all together inside a loop alongside a delay to reduce the data throu
use aux14::{entry, iprint, iprintln, prelude::*};

// Slave address
const MAGNETOMETER: u8 = 0b001_1110;
const MAGNETOMETER: u16 = 0b0011_1100;

// Addresses of the magnetometer's registers
const OUT_X_H_M: u8 = 0x03;
Expand All @@ -68,7 +68,7 @@ fn main() -> ! {
// Broadcast the MAGNETOMETER address with the R/W bit set to Write
i2c1.cr2.write(|w| {
w.start().set_bit();
w.sadd1().bits(MAGNETOMETER);
w.sadd().bits(MAGNETOMETER);
w.rd_wrn().clear_bit();
w.nbytes().bits(1);
w.autoend().clear_bit()
Expand Down
2 changes: 1 addition & 1 deletion src/14-i2c/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use aux14::{entry, iprint, iprintln, prelude::*};

// Slave address
const MAGNETOMETER: u8 = 0b001_1110;
const MAGNETOMETER: u16 = 0b0011_1100;

// Addresses of the magnetometer's registers
const OUT_X_H_M: u8 = 0x03;
Expand Down
4 changes: 2 additions & 2 deletions src/14-i2c/the-solution.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use aux14::{entry, iprint, iprintln, prelude::*};

// Slave address
const MAGNETOMETER: u8 = 0b001_1110;
const MAGNETOMETER: u16 = 0b0011_1100;

// Addresses of the magnetometer's registers
const OUT_X_H_M: u8 = 0x03;
Expand All @@ -26,7 +26,7 @@ fn main() -> ! {
// Broadcast the MAGNETOMETER address with the R/W bit set to Write
i2c1.cr2.write(|w| {
w.start().set_bit();
w.sadd1().bits(MAGNETOMETER);
w.sadd().bits(MAGNETOMETER);
w.rd_wrn().clear_bit();
w.nbytes().bits(1);
w.autoend().clear_bit()
Expand Down
7 changes: 2 additions & 5 deletions src/15-led-compass/auxiliary/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ name = "aux15"
version = "0.1.0"

[dependencies]
cortex-m = "=0.5.6"
cortex-m = "0.6.3"
cortex-m-rt = "0.6.3"
panic-itm = "0.4.0"

[dependencies.f3]
features = ["rt"]
version = "0.6.1"
stm32f3-discovery = "0.6.0"
Loading

0 comments on commit b6b9054

Please sign in to comment.