Skip to content

Commit a089676

Browse files
ojobianrrees
andauthored
bsp(arduino_mkr1000)!: Update to hal v0.16 (#878)
* chore(arduino_mkr1000): update to hal v0.16 Bring examples from arduino_mkrzero based on same hal version. * fix: remove unexisting SD pins These exist on MKRZERO, but not on MKR1000. * fix: board led is D6 * feat: set pin for pwm sample * Bump version --------- Co-authored-by: Ian <[email protected]>
1 parent b23e2eb commit a089676

File tree

9 files changed

+582
-127
lines changed

9 files changed

+582
-127
lines changed

boards/arduino_mkr1000/Cargo.toml

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
[package]
22
name = "arduino_mkr1000"
3-
version = "0.5.1"
4-
authors = ["Eric Rushing <[email protected]>"]
3+
version = "0.6.0"
4+
authors = [
5+
"Eric Rushing <[email protected]>",
6+
"Joël Bourgault <[email protected]>",
7+
]
58
description = "Board Support crate for the Arduino MKR 1000 WiFi"
69
keywords = ["no-std", "arm", "cortex-m", "embedded-hal"]
710
categories = ["embedded", "hardware-support", "no-std"]
@@ -15,25 +18,47 @@ version = "0.7"
1518
optional = true
1619

1720
[dependencies.atsamd-hal]
18-
version = "0.14"
21+
version = "0.16"
1922
default-features = false
2023

2124
[dependencies.usb-device]
2225
version = "0.2"
2326
optional = true
2427

2528
[dev-dependencies]
29+
cortex-m = "0.7"
30+
usbd-serial = "0.1"
2631
panic-halt = "0.2"
2732
panic-semihosting = "0.5"
33+
cortex-m-rtic = "1.0"
2834

2935
[features]
3036
# ask the HAL to enable atsamd21g support
31-
default = ["rt", "atsamd-hal/samd21g"]
37+
default = ["rt", "atsamd-hal/samd21g", "usb"]
3238
rt = ["cortex-m-rt", "atsamd-hal/samd21g-rt"]
3339
usb = ["atsamd-hal/usb", "usb-device"]
3440
unproven = ["atsamd-hal/unproven"]
3541
use_semihosting = []
42+
rtic = ["atsamd-hal/rtic"]
3643

3744
# for cargo flash
3845
[package.metadata]
3946
chip = "ATSAMD21G18A"
47+
48+
[dependencies]
49+
embedded-hal = "0.2.7"
50+
51+
[[example]]
52+
name = "blinky_basic"
53+
54+
[[example]]
55+
name = "blinky_rtic"
56+
required-features = ["rtic", "unproven"]
57+
58+
[[example]]
59+
name = "usb_logging"
60+
required-features = ["usb"]
61+
62+
[[example]]
63+
name = "pwm"
64+
required-features = ["unproven"]

boards/arduino_mkr1000/examples/blinky_basic.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
#![no_std]
22
#![no_main]
33

4-
use arduino_mkr1000 as bsp;
5-
use bsp::hal;
6-
74
#[cfg(not(feature = "use_semihosting"))]
85
use panic_halt as _;
96
#[cfg(feature = "use_semihosting")]
107
use panic_semihosting as _;
118

12-
use bsp::entry;
9+
use arduino_mkr1000 as bsp;
10+
use bsp::hal;
11+
12+
use bsp::{entry, pin_alias};
1313
use hal::clock::GenericClockController;
1414
use hal::delay::Delay;
1515
use hal::pac::{CorePeripherals, Peripherals};
@@ -25,9 +25,10 @@ fn main() -> ! {
2525
&mut peripherals.SYSCTRL,
2626
&mut peripherals.NVMCTRL,
2727
);
28-
let mut pins = bsp::Pins::new(peripherals.PORT);
29-
let mut led = pins.d6.into_open_drain_output(&mut pins.port);
28+
let pins = bsp::pins::Pins::new(peripherals.PORT);
29+
let mut led = pin_alias!(pins.led).into_push_pull_output();
3030
let mut delay = Delay::new(core.SYST, &mut clocks);
31+
3132
loop {
3233
delay.delay_ms(200u8);
3334
led.set_high().unwrap();
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//! Uses RTIC with the RTC as time source to blink an LED.
2+
//!
3+
//! The idle task is sleeping the CPU, so in practice this gives similar power
4+
//! figure as the "sleeping_timer_rtc" example.
5+
#![no_std]
6+
#![no_main]
7+
8+
use arduino_mkr1000 as bsp;
9+
10+
#[cfg(not(feature = "use_semihosting"))]
11+
use panic_halt as _;
12+
#[cfg(feature = "use_semihosting")]
13+
use panic_semihosting as _;
14+
use rtic::app;
15+
16+
#[app(device = bsp::pac, peripherals = true, dispatchers = [EVSYS])]
17+
mod app {
18+
use super::*;
19+
use bsp::hal;
20+
use hal::clock::{ClockGenId, ClockSource, GenericClockController};
21+
use hal::pac::Peripherals;
22+
use hal::prelude::*;
23+
use hal::rtc::{Count32Mode, Duration, Rtc};
24+
25+
#[local]
26+
struct Local {}
27+
28+
#[shared]
29+
struct Shared {
30+
// The LED could be a local resource, since it is only used in one task
31+
// But we want to showcase shared resources and locking
32+
led: bsp::pins::Led,
33+
}
34+
35+
#[monotonic(binds = RTC, default = true)]
36+
type RtcMonotonic = Rtc<Count32Mode>;
37+
38+
#[init]
39+
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
40+
let mut peripherals: Peripherals = cx.device;
41+
let pins = bsp::pins::Pins::new(peripherals.PORT);
42+
let mut core: rtic::export::Peripherals = cx.core;
43+
let mut clocks = GenericClockController::with_external_32kosc(
44+
peripherals.GCLK,
45+
&mut peripherals.PM,
46+
&mut peripherals.SYSCTRL,
47+
&mut peripherals.NVMCTRL,
48+
);
49+
let _gclk = clocks.gclk0();
50+
let rtc_clock_src = clocks
51+
.configure_gclk_divider_and_source(ClockGenId::GCLK2, 1, ClockSource::XOSC32K, false)
52+
.unwrap();
53+
clocks.configure_standby(ClockGenId::GCLK2, true);
54+
let rtc_clock = clocks.rtc(&rtc_clock_src).unwrap();
55+
let rtc = Rtc::count32_mode(peripherals.RTC, rtc_clock.freq(), &mut peripherals.PM);
56+
let led = bsp::pin_alias!(pins.led).into();
57+
58+
// We can use the RTC in standby for maximum power savings
59+
core.SCB.set_sleepdeep();
60+
61+
// Start the blink task
62+
blink::spawn().unwrap();
63+
64+
(Shared { led }, Local {}, init::Monotonics(rtc))
65+
}
66+
67+
#[task(shared = [led])]
68+
fn blink(mut cx: blink::Context) {
69+
// If the LED were a local resource, the lock would not be necessary
70+
let _ = cx.shared.led.lock(|led| led.toggle());
71+
blink::spawn_after(Duration::secs(3)).ok();
72+
}
73+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use arduino_mkr1000 as bsp;
5+
use bsp::hal;
6+
7+
#[cfg(not(feature = "use_semihosting"))]
8+
use panic_halt as _;
9+
#[cfg(feature = "use_semihosting")]
10+
use panic_semihosting as _;
11+
12+
use bsp::entry;
13+
use hal::clock::{GenericClockController, Tcc0Tcc1Clock};
14+
use hal::delay::Delay;
15+
use hal::gpio::AlternateE;
16+
use hal::pac::{CorePeripherals, Peripherals};
17+
use hal::prelude::*;
18+
use hal::pwm::{Channel, Pwm0};
19+
20+
#[entry]
21+
fn main() -> ! {
22+
let mut peripherals = Peripherals::take().unwrap();
23+
let core = CorePeripherals::take().unwrap();
24+
let mut clocks = GenericClockController::with_external_32kosc(
25+
peripherals.GCLK,
26+
&mut peripherals.PM,
27+
&mut peripherals.SYSCTRL,
28+
&mut peripherals.NVMCTRL,
29+
);
30+
let pins = bsp::pins::Pins::new(peripherals.PORT);
31+
32+
// PWM0_CH0 is D11 on the board - pin PA08
33+
let _d11 = pins.pa08.into_mode::<AlternateE>();
34+
let mut delay = Delay::new(core.SYST, &mut clocks);
35+
let gclk0 = clocks.gclk0();
36+
let tcc0_tcc1_clock: &Tcc0Tcc1Clock = &clocks.tcc0_tcc1(&gclk0).unwrap();
37+
38+
let mut pwm0 = Pwm0::new(
39+
&tcc0_tcc1_clock,
40+
1u32.kHz(),
41+
peripherals.TCC0,
42+
&mut peripherals.PM,
43+
);
44+
let max_duty = pwm0.get_max_duty();
45+
let channel = Channel::_0;
46+
pwm0.enable(channel);
47+
loop {
48+
pwm0.set_duty(channel, max_duty);
49+
delay.delay_ms(500u16);
50+
pwm0.set_duty(channel, max_duty / 4);
51+
delay.delay_ms(500u16);
52+
}
53+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#![no_std]
2+
#![no_main]
3+
4+
use arduino_mkr1000 as bsp;
5+
use bsp::hal;
6+
7+
#[cfg(not(feature = "use_semihosting"))]
8+
use panic_halt as _;
9+
#[cfg(feature = "use_semihosting")]
10+
use panic_semihosting as _;
11+
12+
use bsp::entry;
13+
use hal::clock::GenericClockController;
14+
use hal::delay::Delay;
15+
use hal::pac::{interrupt, CorePeripherals, Peripherals};
16+
use hal::prelude::*;
17+
18+
use hal::usb::UsbBus;
19+
use usb_device::bus::UsbBusAllocator;
20+
use usb_device::prelude::*;
21+
use usbd_serial::{SerialPort, USB_CLASS_CDC};
22+
23+
use cortex_m::peripheral::NVIC;
24+
25+
static mut USB_ALLOCATOR: Option<UsbBusAllocator<UsbBus>> = None;
26+
static mut USB_BUS: Option<UsbDevice<UsbBus>> = None;
27+
static mut USB_SERIAL: Option<SerialPort<UsbBus>> = None;
28+
29+
#[entry]
30+
fn main() -> ! {
31+
let mut peripherals = Peripherals::take().unwrap();
32+
let mut core = CorePeripherals::take().unwrap();
33+
let mut clocks = GenericClockController::with_external_32kosc(
34+
peripherals.GCLK,
35+
&mut peripherals.PM,
36+
&mut peripherals.SYSCTRL,
37+
&mut peripherals.NVMCTRL,
38+
);
39+
let pins = bsp::pins::Pins::new(peripherals.PORT);
40+
let mut led = bsp::pin_alias!(pins.led).into_push_pull_output();
41+
let mut delay = Delay::new(core.SYST, &mut clocks);
42+
43+
let usb_n = bsp::pin_alias!(pins.usb_n);
44+
let usb_p = bsp::pin_alias!(pins.usb_p);
45+
46+
let bus_allocator = unsafe {
47+
USB_ALLOCATOR = Some(bsp::usb::usb_allocator(
48+
peripherals.USB,
49+
&mut clocks,
50+
&mut peripherals.PM,
51+
usb_n.into(),
52+
usb_p.into(),
53+
));
54+
USB_ALLOCATOR.as_ref().unwrap()
55+
};
56+
57+
unsafe {
58+
USB_SERIAL = Some(SerialPort::new(bus_allocator));
59+
USB_BUS = Some(
60+
UsbDeviceBuilder::new(bus_allocator, UsbVidPid(0x2222, 0x3333))
61+
.manufacturer("Fake company")
62+
.product("Serial port")
63+
.serial_number("TEST")
64+
.device_class(USB_CLASS_CDC)
65+
.build(),
66+
);
67+
}
68+
69+
unsafe {
70+
core.NVIC.set_priority(interrupt::USB, 1);
71+
NVIC::unmask(interrupt::USB);
72+
}
73+
74+
loop {
75+
delay.delay_ms(200u8);
76+
led.set_high().unwrap();
77+
delay.delay_ms(200u8);
78+
led.set_low().unwrap();
79+
80+
// Turn off interrupts so we don't fight with the interrupt
81+
cortex_m::interrupt::free(|_| unsafe {
82+
if let Some(serial) = USB_SERIAL.as_mut() {
83+
let _ = serial.write("log line\r\n".as_bytes());
84+
}
85+
});
86+
}
87+
}
88+
89+
fn poll_usb() {
90+
unsafe {
91+
if let Some(usb_dev) = USB_BUS.as_mut() {
92+
if let Some(serial) = USB_SERIAL.as_mut() {
93+
usb_dev.poll(&mut [serial]);
94+
// Make the other side happy
95+
let mut buf = [0u8; 16];
96+
let _ = serial.read(&mut buf);
97+
}
98+
}
99+
};
100+
}
101+
102+
#[interrupt]
103+
fn USB() {
104+
poll_usb();
105+
}

0 commit comments

Comments
 (0)