|
1 | 1 | #![no_std]
|
2 | 2 | #![no_main]
|
3 |
| -// For allocator |
4 |
| -#![feature(lang_items)] |
5 |
| -#![feature(alloc_error_handler)] |
6 | 3 |
|
| 4 | +use core::panic::PanicInfo; |
7 | 5 | use cortex_m::asm;
|
8 | 6 | use cortex_m_rt::exception;
|
9 | 7 | use cortex_m_rt::{entry, ExceptionFrame};
|
10 |
| -use embedded_hal::digital::v2::OutputPin; |
| 8 | +use embedded_hal::digital::OutputPin; |
11 | 9 | use freertos_rust::*;
|
12 |
| -use core::alloc::Layout; |
13 | 10 | use stm32f4xx_hal::gpio::*;
|
14 | 11 |
|
15 |
| -use cortex_m; |
16 | 12 | use stm32f4xx_hal as hal;
|
17 | 13 |
|
18 |
| -use crate::hal::{ |
19 |
| - stm32::{Peripherals}, |
20 |
| -}; |
21 |
| - |
22 |
| -extern crate panic_halt; // panic handler |
| 14 | +use crate::hal::{pac, prelude::*}; |
23 | 15 |
|
24 | 16 | #[global_allocator]
|
25 | 17 | static GLOBAL: FreeRtosAllocator = FreeRtosAllocator;
|
26 | 18 |
|
27 |
| -fn delay() { |
28 |
| - let mut _i = 0; |
29 |
| - for _ in 0..2_00 { |
30 |
| - _i += 1; |
31 |
| - } |
| 19 | +#[entry] |
| 20 | +fn main() -> ! { |
| 21 | + Task::new() |
| 22 | + .name("default") |
| 23 | + .stack_size(1000) |
| 24 | + .start(move |_| { |
| 25 | + app_main(); |
| 26 | + }) |
| 27 | + .unwrap(); |
| 28 | + FreeRtosUtils::start_scheduler(); |
32 | 29 | }
|
33 | 30 |
|
34 |
| -fn delay_n(n: i32) { |
35 |
| - for _ in 0..n { |
36 |
| - delay(); |
| 31 | +fn app_main() -> ! { |
| 32 | + let dp = pac::Peripherals::take().unwrap(); |
| 33 | + let rcc = dp.RCC.constrain(); |
| 34 | + let _clocks = rcc.cfgr.use_hse(25.MHz()).sysclk(100.MHz()).freeze(); |
| 35 | + |
| 36 | + let gpioc = dp.GPIOC.split(); |
| 37 | + let mut device = MyDevice::from_pins(gpioc.pc13.into_open_drain_output()); |
| 38 | + device.set_led(false); |
| 39 | + Task::new() |
| 40 | + .name("hello") |
| 41 | + .stack_size(128) |
| 42 | + .priority(TaskPriority(2)) |
| 43 | + .start(move |_| loop { |
| 44 | + CurrentTask::delay(Duration::ms(500)); |
| 45 | + device.set_led(true); |
| 46 | + CurrentTask::delay(Duration::ms(500)); |
| 47 | + device.set_led(false); |
| 48 | + }) |
| 49 | + .unwrap(); |
| 50 | + |
| 51 | + loop { |
| 52 | + CurrentTask::delay(Duration::ms(1000)); |
37 | 53 | }
|
38 | 54 | }
|
39 | 55 |
|
40 | 56 | pub struct MyDevice<D1: OutputPin> {
|
41 | 57 | d1: D1,
|
42 | 58 | }
|
43 | 59 |
|
44 |
| -impl<D1: OutputPin> MyDevice<D1> |
45 |
| -{ |
| 60 | +impl<D1: OutputPin> MyDevice<D1> { |
46 | 61 | pub fn from_pins(d1: D1) -> MyDevice<D1> {
|
47 |
| - MyDevice { |
48 |
| - d1 |
49 |
| - } |
| 62 | + MyDevice { d1 } |
50 | 63 | }
|
51 |
| - pub fn set_led(&mut self,on:bool){ |
| 64 | + |
| 65 | + pub fn set_led(&mut self, on: bool) { |
52 | 66 | if on {
|
53 |
| - self.d1.set_high(); |
| 67 | + self.d1.set_low().ok(); |
54 | 68 | } else {
|
55 |
| - self.d1.set_low(); |
| 69 | + self.d1.set_high().ok(); |
56 | 70 | }
|
57 | 71 | }
|
58 | 72 | }
|
59 | 73 |
|
60 |
| -#[entry] |
61 |
| -fn main() -> ! { |
62 |
| - let dp = Peripherals::take().unwrap(); |
63 |
| - let gpioc = dp.GPIOC.split(); |
64 |
| - let mut device = MyDevice::from_pins(gpioc.pc13.into_push_pull_output()); |
65 |
| - device.set_led(false); |
66 |
| - Task::new().name("hello").stack_size(128).priority(TaskPriority(2)).start(move |_| { |
67 |
| - loop{ |
68 |
| - freertos_rust::CurrentTask::delay(Duration::ms(1000)); |
69 |
| - device.set_led(true); |
70 |
| - freertos_rust::CurrentTask::delay(Duration::ms(1000)); |
71 |
| - device.set_led(false); |
72 |
| - } |
73 |
| - }).unwrap(); |
74 |
| - FreeRtosUtils::start_scheduler(); |
75 |
| -} |
76 |
| - |
| 74 | +#[allow(non_snake_case)] |
77 | 75 | #[exception]
|
78 |
| -fn DefaultHandler(_irqn: i16) { |
79 |
| -// custom default handler |
80 |
| -// irqn is negative for Cortex-M exceptions |
81 |
| -// irqn is positive for device specific (line IRQ) |
82 |
| -// set_led(true);(true); |
83 |
| -// panic!("Exception: {}", irqn); |
| 76 | +unsafe fn DefaultHandler(_irqn: i16) { |
| 77 | + // custom default handler |
| 78 | + // irqn is negative for Cortex-M exceptions |
| 79 | + // irqn is positive for device specific (line IRQ) |
| 80 | + // set_led(true);(true); |
| 81 | + // panic!("Exception: {}", irqn); |
| 82 | + asm::bkpt(); |
| 83 | + loop {} |
84 | 84 | }
|
85 | 85 |
|
| 86 | +#[allow(non_snake_case)] |
86 | 87 | #[exception]
|
87 |
| -fn HardFault(_ef: &ExceptionFrame) -> ! { |
88 |
| -// Blink 3 times long when exception occures |
89 |
| - delay_n(10); |
90 |
| - for _ in 0..3 { |
91 |
| - // set_led(true); |
92 |
| - // delay_n(1000); |
93 |
| - // set_led(false); |
94 |
| - // delay_n(555); |
95 |
| - } |
| 88 | +unsafe fn HardFault(_ef: &ExceptionFrame) -> ! { |
| 89 | + // Blink 3 times long when exception occures |
| 90 | + asm::bkpt(); |
96 | 91 | loop {}
|
97 | 92 | }
|
98 | 93 |
|
99 |
| -// define what happens in an Out Of Memory (OOM) condition |
100 |
| -#[alloc_error_handler] |
101 |
| -fn alloc_error(_layout: Layout) -> ! { |
102 |
| - //set_led(true); |
| 94 | +// We no longer need to use #[alloc_error_handler] since v1.68. |
| 95 | +// It will automatically call the panic handler. |
| 96 | +#[panic_handler] |
| 97 | +fn panic(_info: &PanicInfo) -> ! { |
103 | 98 | asm::bkpt();
|
104 | 99 | loop {}
|
105 | 100 | }
|
106 | 101 |
|
| 102 | +#[allow(non_snake_case)] |
107 | 103 | #[no_mangle]
|
108 |
| -fn vApplicationStackOverflowHook(pxTask: FreeRtosTaskHandle, pcTaskName: FreeRtosCharPtr) { |
| 104 | +fn vApplicationStackOverflowHook(_pxTask: FreeRtosTaskHandle, _pcTaskName: FreeRtosCharPtr) { |
109 | 105 | asm::bkpt();
|
110 | 106 | }
|
0 commit comments