stm32-rs / stm32l4xx-hal

A Hardware abstraction layer for the stm32l432xx series chips written in rust.
157 stars 104 forks source link

Temperature values from HTS221 sensor using stm32l4xx-hal for stm32l475 #41

Closed tasosxak closed 5 years ago

tasosxak commented 5 years ago

Hi, I try to get temperature values using HTMS221 sensor using this HAL, but I get a compile error.

It appears pb9 and pb10 are SCL and SDA for I2C2

Code:

#![deny(unsafe_code)]
// #![deny(warnings)]
#![no_std]
#![no_main]

extern crate cortex_m;
extern crate hts221;

#[macro_use]
extern crate cortex_m_rt as rt;
extern crate cortex_m_semihosting as sh;
extern crate panic_semihosting;
extern crate stm32l4xx_hal as hal;
// #[macro_use(block)]
// extern crate nb;

use crate::hal::prelude::*;
use crate::hal::delay::Delay;
use crate::rt::ExceptionFrame;
use crate::rt::entry;
use crate::sh::hio;

#[entry]
fn main() -> ! {

    let mut hstdout = hio::hstdout().unwrap();
    let cp = cortex_m::Peripherals::take().unwrap();
    let dp = hal::stm32::Peripherals::take().unwrap();
    let mut flash = dp.FLASH.constrain(); // .constrain();
    let mut rcc = dp.RCC.constrain();
    let clocks = rcc.cfgr.hclk(8.mhz()).freeze(&mut flash.acr);
    let mut gpiob = dp.GPIOB.split(&mut rcc.ahb2);
    let scl = gpiob.pb8.into_af4(&mut gpiob.moder, &mut gpiob.afrh);
    let sda = gpiob.pb9.into_af4(&mut gpiob.moder, &mut gpiob.afrh);
    let i2c = hal::i2c::I2c::i2c1(
        dp.I2C1,
        (scl, sda),
        1.mhz(),
        rcc.cfgr.freeze(&mut dp.FLASH.constrain().acr),
&mut rcc.apb1r1);

    let mut hts221 = hts221::Builder::new(i2c)
        .with_avg_t(hts221::AvgT::Avg256)
        .with_avg_h(hts221::AvgH::Avg512)
        .build()
        .unwrap();
    let relative_humidity = hts221.humidity_x2();
let temp_c = hts221.temperature_x8();

    let mut timer = Delay::new(cp.SYST, clocks);

    //writeln!(hstdout, "{}",temp_c.unwrap()).unwrap();

    loop {

    }
}

#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
    panic!("{:#?}", ef);
}

Error:

tasosxak@tasosxak:~/Documents/RustProjects/stm32l4xx-hal$ cargo run --release --example temperature  --features="stm32l4x5,rt"
   Compiling stm32l4xx-hal v0.3.6 (/home/tasosxak/Documents/RustProjects/stm32l4xx-hal)
error[E0277]: the trait bound `hal::gpio::gpiob::PB8<hal::gpio::Alternate<hal::gpio::AF4, hal::gpio::Input<hal::gpio::Floating>>>: hal::i2c::SclPin<hal::stm32::I2C1>` is not satisfied
  --> examples/temperature.rs:35:15
   |
35 |     let i2c = hal::i2c::I2c::i2c1(
   |               ^^^^^^^^^^^^^^^^^^^ the trait `hal::i2c::SclPin<hal::stm32::I2C1>` is not implemented for `hal::gpio::gpiob::PB8<hal::gpio::Alternate<hal::gpio::AF4, hal::gpio::Input<hal::gpio::Floating>>>`
   |
   = note: required by `<hal::i2c::I2c<hal::stm32::I2C1, (SCL, SDA)>>::i2c1`

error[E0277]: the trait bound `hal::gpio::gpiob::PB9<hal::gpio::Alternate<hal::gpio::AF4, hal::gpio::Input<hal::gpio::Floating>>>: hal::i2c::SdaPin<hal::stm32::I2C1>` is not satisfied
  --> examples/temperature.rs:35:15
   |
35 |     let i2c = hal::i2c::I2c::i2c1(
   |               ^^^^^^^^^^^^^^^^^^^ the trait `hal::i2c::SdaPin<hal::stm32::I2C1>` is not implemented for `hal::gpio::gpiob::PB9<hal::gpio::Alternate<hal::gpio::AF4, hal::gpio::Input<hal::gpio::Floating>>>`
   |
   = note: required by `<hal::i2c::I2c<hal::stm32::I2C1, (SCL, SDA)>>::i2c1`

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0277`.
error: Could not compile `stm32l4xx-hal`.

To learn more, run the command again with --verbose.
tasosxak commented 5 years ago
#![deny(unsafe_code)]
#![no_main]
#![no_std]
extern crate stm32l4xx_hal;
extern crate hts221;

extern crate cortex_m;
#[macro_use(entry, exception)]
extern crate cortex_m_rt as rt;
#[macro_use(block)]
extern crate nb;
extern crate panic_semihosting;

use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use hts221::Builder;
use stm32l4xx_hal::rcc::RccExt;
use stm32l4xx_hal::gpio::GpioExt;
use stm32l4::stm32l4x5::interrupt;
use stm32l4xx_hal::i2c::SdaPin;
use stm32l4xx_hal::i2c::SclPin;
use stm32l4xx_hal::i2c::Error;
use stm32l4xx_hal::i2c::I2c;
use stm32l4xx_hal::stm32::I2C2;
use stm32l4xx_hal::flash::FlashExt;
use stm32l4xx_hal::time::U32Ext;
use stm32l4xx_hal::prelude::*;
use crate::stm32l4xx_hal::serial::Serial;

const PERIOD: u32 = 5_000_000;

#[entry]
fn main() -> ! {
    //hprintln!("Hello, world!").unwrap();

        let peripherals = stm32l4xx_hal::stm32l4::stm32l4x5::Peripherals::take().unwrap();
        let mut flash = peripherals.FLASH.constrain();
        let mut rcc = peripherals.RCC.constrain();
        let mut gpiob = peripherals.GPIOB.split(&mut rcc.ahb2);
        let clocks = rcc.cfgr.freeze(&mut flash.acr);
        let tx = gpiob.pb6.into_af7(&mut gpiob.moder, &mut gpiob.afrl);
        let rx = gpiob.pb7.into_af7(&mut gpiob.moder, &mut gpiob.afrl);

        let serial = Serial::usart1( peripherals.USART1, (tx, rx), 115_200.bps(), clocks, &mut rcc.apb2);
        let (mut tx, mut rx) = serial.split();
        let sent = b'A';
        block!(tx.write(sent)).ok();

        //let p = stm32l4xx_hal::stm32l4::stm32l4x5::Peripherals::take().unwrap();
block!(tx.write(b'h')).ok();
        let scl = gpiob.pb10.into_open_drain_output(&mut gpiob.moder, &mut gpiob.otyper).into_af4(&mut gpiob.moder, &mut gpiob.afrh);
        let sda = gpiob.pb11.into_open_drain_output(&mut gpiob.moder, &mut gpiob.otyper).into_af4(&mut gpiob.moder, &mut gpiob.afrh);
         block!(tx.write(b'k')).ok();
        let i2c = I2c::i2c2(
            peripherals.I2C2,
            (scl,sda),
            1.mhz(),
            rcc.cfgr.freeze(&mut flash.acr),
           &mut rcc.apb1r1
            );
         block!(tx.write(b'z')).ok();
       let mut hts = hts221::Builder::new(i2c)
        .with_avg_t(hts221::AvgT::Avg256)
        .with_avg_h(hts221::AvgH::Avg512)
        .build()
        .unwrap();

        let x  = 4;//hts.temperature_x8().unwrap() / 8;
        block!(tx.write(b'Y')).ok();
        block!(tx.write(x as u8)).ok();

      //hprintln!("temperature: {}",hts.temperature_x8().unwrap() / 8);

    loop {}
}

I get runtime error, after this command block!(tx.write(b'k')).ok();

openocd :

Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
        http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 500 kHz
adapter_nsrst_delay: 100
none separate
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : clock speed 480 kHz
Info : STLINK v2 JTAG v28 API v2 SWIM v18 VID 0x0483 PID 0x374B
Info : using stlink api v2
Info : Target voltage: 3.242300
Info : stm32l4x.cpu: hardware has 6 breakpoints, 4 watchpoints
Info : accepting 'gdb' connection on tcp/3333
Info : device id = 0x10076415
Info : flash size = 1024kbytes
undefined debug reason 7 - target needs reset
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
adapter speed: 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080097ca msp: 0x20018000
adapter speed: 4000 kHz
Info : Padding image section 0 with 20 bytes
target halted due to breakpoint, current mode: Thread
xPSR: 0x61000000 pc: 0x20000050 msp: 0x20018000
Warn : block write succeeded
Info : Unable to match requested speed 500 kHz, using 480 kHz
Info : Unable to match requested speed 500 kHz, using 480 kHz
adapter speed: 480 kHz
target halted due to debug-request, current mode: Thread
xPSR: 0x01000000 pc: 0x080097ca msp: 0x20018000 

gdb session:

tasosxak@tasosxak:~/Documents/RustProjects/app$ cargo run --example hello
    Finished dev [unoptimized + debuginfo] target(s) in 8.11s
     Running `gdb-multiarch -q -x openocd.gdb target/thumbv7em-none-eabi/debug/examples/hello`
Reading symbols from target/thumbv7em-none-eabi/debug/examples/hello...done.
0x00000000 in ?? ()
Breakpoint 1 at 0x8009810: file /home/tasosxak/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-rt-0.6.7/src/lib.rs,line 553.
Breakpoint 2 at 0x8009a1c: file /home/tasosxak/.cargo/registry/src/github.com-1ecc6299db9ec823/cortex-m-rt-0.6.7/src/lib.rs,line 543.
Breakpoint 3 at 0x8008c2e: file /home/tasosxak/.cargo/registry/src/github.com-1ecc6299db9ec823/panic-semihosting-0.5.1/src/lib.rs, line 78.
Loading section .vector_table, size 0x188 lma 0x8000000
Loading section .text, size 0x98a4 lma 0x8000188
Loading section .rodata, size 0x1248 lma 0x8009a40
Start address 0x80097ca, load size 44148
Transfer rate: 33 KB/sec, 8829 bytes/write.
(gdb) break main
Breakpoint 4 at 0x800268e: file examples/hello.rs, line 39.
(gdb) continue
Continuing.
Note: automatically using hardware breakpoints for read-only addresses.

Breakpoint 4, main () at examples/hello.rs:39
39              let peripherals = stm32l4xx_hal::stm32l4::stm32l4x5::Peripherals::take().unwrap();
(gdb) continue
Continuing.

Breakpoint 3, rust_begin_unwind (info=0x20017dd4)
    at /home/tasosxak/.cargo/registry/src/github.com-1ecc6299db9ec823/panic-semihosting-0.5.1/src/lib.rs:78
78          interrupt::disable();
(gdb)

minicom:

Welcome to minicom 2.7.1

OPTIONS: I18n
Compiled on Aug 13 2017, 15:25:34.
Port /dev/ttyACM0, 15:28:48

Press CTRL-A Z for help on special keys

Ahkz
tasosxak commented 5 years ago

Bump

MabezDev commented 5 years ago

Hi @tasosxak when you get to rust_begin_unwind if you continue again in gdb you will get the panic message which will help diagnose the issue.