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

Update dependencies (fixes #13), fix bugs, add USB support #14

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
31 changes: 19 additions & 12 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,34 @@ repository = "https://github.com/stm32-rs/stm32l1xx-hal"
version = "0.1.0"

[dependencies]
cortex-m = "0.5.8"
nb = "0.1.1"
stm32l1 = "0.5.0"
cortex-m = "0.7.1"
nb = "1.0.0"
stm32l1 = "0.14.0"
bare-metal = "1.0.0"

[dependencies.bare-metal]
features = ["const-fn"]
version = "0.2.4"
[dependencies.stm32-usbd]
version = "0.6.0"
optional = true

[dependencies.cast]
default-features = false
version = "0.2.2"
version = "0.3.0"

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

[dependencies.void]
default-features = false
version = "1.0.2"

[dev-dependencies]
cortex-m-rt = "0.6.7"
cortex-m-semihosting = "0.3.2"
panic-semihosting = "0.5.1"
cortex-m-rtic = "0.5.5"
cortex-m-rt = "0.7.1"
cortex-m-semihosting = "0.3.7"
panic-semihosting = "0.5.6"
cortex-m-rtic = "1.0.0"
usb-device = "0.2.8"
usbd-serial = "0.1.1"

[features]
rt = ["stm32l1/rt"]
Expand All @@ -70,3 +73,7 @@ required-features = ["rt"]
[[example]]
name = "timer"
required-features = ["rt"]

[[example]]
name = "usb_serial"
required-features = ["rt", "stm32-usbd"]
6 changes: 4 additions & 2 deletions examples/button_irq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ static INT: Mutex<RefCell<Option<EXTI>>> = Mutex::new(RefCell::new(None));
#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().unwrap();
let mut cp = cortex_m::Peripherals::take().unwrap();

cp.NVIC.enable(Interrupt::EXTI0);
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::EXTI0);
}

dp.EXTI.listen(0, TriggerEdge::Falling);

cortex_m::interrupt::free(move |cs| {
Expand Down
102 changes: 50 additions & 52 deletions examples/rtfm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,34 @@ extern crate panic_semihosting;
extern crate rtic;
extern crate stm32l1xx_hal as hal;

use cortex_m_semihosting::hprintln;
use rtic::app;
#[rtic::app(device = hal::stm32, peripherals = true)]
mod app {
use cortex_m_semihosting::hprintln;
use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::v2::ToggleableOutputPin;
use hal::exti::TriggerEdge;
use hal::gpio::gpiob::{PB6, PB7};
use hal::gpio::{Output, PushPull};
use hal::prelude::*;
use hal::rcc::Config;
use hal::stm32;
use hal::timer::Timer;

use embedded_hal::digital::v2::OutputPin;
use embedded_hal::digital::v2::ToggleableOutputPin;
use hal::exti::TriggerEdge;
use hal::gpio::gpiob::{PB6, PB7};
use hal::gpio::{Output, PushPull};
use hal::prelude::*;
use hal::rcc::Config;
use hal::stm32;
use hal::timer::Timer;

#[app(device = hal::stm32, peripherals = true)]
const APP: () = {
struct Resources {
// resources
#[init(0)]
DELTA: u32,
#[shared]
struct Shared {
delta: u32,
}

// late resources
TIMER: Timer<stm32::TIM2>,
TICKS_LED: PB6<Output<PushPull>>,
BUSY_LED: PB7<Output<PushPull>>,
EXTI: stm32::EXTI,
#[local]
struct Local {
timer: Timer<stm32::TIM2>,
ticks_led: PB6<Output<PushPull>>,
busy_led: PB7<Output<PushPull>>,
exti: stm32::EXTI,
}

#[init]
fn init(cx: init::Context) -> init::LateResources {
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
let mut rcc = cx.device.RCC.freeze(Config::hsi());

let gpiob = cx.device.GPIOB.split();
Expand All @@ -46,39 +45,38 @@ const APP: () = {
timer.listen();
cx.device.EXTI.listen(0, TriggerEdge::Rising);

let TICKS_LED = gpiob.pb6.into_push_pull_output();
let BUSY_LED = gpiob.pb7.into_push_pull_output();
let TIMER = timer;
let EXTI = cx.device.EXTI;
let ticks_led = gpiob.pb6.into_push_pull_output();
let busy_led = gpiob.pb7.into_push_pull_output();
let timer = timer;
let exti = cx.device.EXTI;

init::LateResources {
TIMER,
TICKS_LED,
BUSY_LED,
EXTI,
}
(
Shared { delta: 0 },
Local {
timer,
ticks_led,
busy_led,
exti,
},
init::Monotonics(),
)
}

#[task(binds = TIM2, resources = [TIMER, TICKS_LED, DELTA])]
fn tim2_handler(cx: tim2_handler::Context) {
*cx.resources.DELTA += 1;
#[task(binds = TIM2, shared = [delta], local = [timer, ticks_led])]
fn tim2_handler(mut cx: tim2_handler::Context) {
cx.shared.delta.lock(|d| *d += 1);

cx.resources.TICKS_LED.toggle().unwrap();
cx.resources.TIMER.clear_irq();
cx.local.ticks_led.toggle().unwrap();
cx.local.timer.clear_irq();
}

#[task(binds = EXTI0, resources = [EXTI, BUSY_LED, DELTA])]
fn exti0_handler(cx: exti0_handler::Context) {
cx.resources.BUSY_LED.set_high().unwrap();
hprintln!("Δ: {}", cx.resources.DELTA).unwrap();
cx.resources.BUSY_LED.set_low().unwrap();

*cx.resources.DELTA = 0;
cx.resources.EXTI.clear_irq(0);
}
#[task(binds = EXTI0, shared = [delta], local = [exti, busy_led])]
fn exti0_handler(mut cx: exti0_handler::Context) {
cx.local.busy_led.set_high().unwrap();
hprintln!("Δ: {}", cx.shared.delta.lock(|d| *d)).unwrap();
cx.local.busy_led.set_low().unwrap();

#[idle]
fn idle(_: idle::Context) -> ! {
loop {}
cx.shared.delta.lock(|d| *d = 0);
cx.local.exti.clear_irq(0);
}
};
}
5 changes: 3 additions & 2 deletions examples/timer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ static TIMER: Mutex<RefCell<Option<Timer<stm32::TIM2>>>> = Mutex::new(RefCell::n
#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().unwrap();
let mut cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.freeze(Config::hsi());

let mut timer = dp.TIM2.timer(1.hz(), &mut rcc);
timer.listen();

cp.NVIC.enable(Interrupt::TIM2);
unsafe {
cortex_m::peripheral::NVIC::unmask(Interrupt::TIM2);
}

cortex_m::interrupt::free(move |cs| {
*TIMER.borrow(cs).borrow_mut() = Some(timer);
Expand Down
106 changes: 106 additions & 0 deletions examples/usb_serial.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//! CDC-ACM serial port example using polling in a busy loop.
//!
//! Note:
//! When building this since this is a larger program,
//! one would need to build it using release profile
//! since debug profiles generates artifacts that
//! cause FLASH overflow errors due to their size
#![no_std]
#![no_main]

extern crate cortex_m;
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;
extern crate stm32l1xx_hal as hal;

use embedded_hal::digital::v2::OutputPin;
use hal::prelude::*;
use hal::rcc::{Config, PLLDiv, PLLMul, PLLSource};
use hal::stm32;
use hal::usb::{Peripheral, UsbBus};
use rt::entry;
use usb_device::prelude::*;
use usbd_serial::{SerialPort, USB_CLASS_CDC};

#[entry]
fn main() -> ! {
let dp = stm32::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();

// dp.RCC.apb1enr.modify(|_, w| w.pwren().set_bit());
// dp.PWR.cr.write(|w| unsafe { w.vos().bits(1) });
// while dp.PWR.csr.read().vosf().bit_is_set() {}

dp.FLASH.acr.write(|w| w.acc64().set_bit());
dp.FLASH.acr.modify(|_, w| w.prften().set_bit());
dp.FLASH.acr.modify(|_, w| w.latency().set_bit());

let mut rcc = dp.RCC.freeze(Config::pll(
PLLSource::HSE(16.mhz()),
PLLMul::Mul6,
PLLDiv::Div4,
));
let mut delay = cp.SYST.delay(rcc.clocks);

let mut gpioa = dp.GPIOA.split();

let mut led = gpioa.pa1.into_push_pull_output();
led.set_low().unwrap();

// Pull the D+ pin down to send a RESET condition to the USB bus.
// This forced reset is needed only for development, without it host
// will not reset your device when you upload new firmware.
let mut usb_dp = gpioa.pa12.into_push_pull_output();
usb_dp.set_low().unwrap();
delay.delay(10.ms());

let usb = Peripheral {
usb: dp.USB,
pin_dm: gpioa.pa11,
pin_dp: usb_dp.into_floating_input(),
};
let usb_bus = UsbBus::new(usb);

let mut serial = SerialPort::new(&usb_bus);

let mut usb_dev = UsbDeviceBuilder::new(&usb_bus, UsbVidPid(0x16c0, 0x27dd))
.manufacturer("stm32-rs")
.product("Serial port")
.serial_number("TEST")
.device_class(USB_CLASS_CDC)
.build();

loop {
if !usb_dev.poll(&mut [&mut serial]) {
continue;
}

let mut buf = [0u8; 64];

match serial.read(&mut buf) {
Ok(count) if count > 0 => {
led.set_high().unwrap();

// Echo back in upper case
for c in buf[0..count].iter_mut() {
if 0x61 <= *c && *c <= 0x7a {
*c &= !0x20;
}
}

let mut write_offset = 0;
while write_offset < count {
match serial.write(&buf[write_offset..count]) {
Ok(len) if len > 0 => {
write_offset += len;
}
_ => {}
}
}
}
_ => {}
}

led.set_low().unwrap();
}
}
6 changes: 3 additions & 3 deletions memory.x
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* Linker script for the STM32L152RCT6 */
/* Linker script for the STM32L151C8 */
MEMORY
{
FLASH : ORIGIN = 0x08000000, LENGTH = 256K
RAM : ORIGIN = 0x20000000, LENGTH = 32K
FLASH : ORIGIN = 0x08000000, LENGTH = 128K
RAM : ORIGIN = 0x20000000, LENGTH = 16K
}
Loading