Guest User

Untitled

a guest
Jan 15th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. //! Blinks an LED
  2.  
  3. #![deny(unsafe_code)]
  4. #![no_std]
  5. #![no_main]
  6. //MCU: STM32F103C8T699
  7. extern crate panic_halt;
  8.  
  9. use rtfm::{app, Instant, Duration};
  10. use stm32f103xx_hal::{
  11. gpio::gpioc::PC13,
  12. gpio::gpioa::PA0,
  13. gpio::Output,
  14. gpio::PushPull,
  15. prelude::*,
  16. time::Hertz,
  17. };
  18.  
  19. const STEP_MIN_S: f32 = 1.1e-6;
  20. const DIR_MIN_S: f32 = 201e-9;
  21.  
  22. fn hertz_to_cycles(sysclock: Hertz, hertz: Hertz) -> u32 {
  23. sysclock.0 / hertz.0
  24. }
  25.  
  26. fn seconds_to_cycles(sysclock: Hertz, seconds: f32) -> u32 {
  27. (sysclock.0 as f32 * seconds) as u32
  28. }
  29.  
  30. #[app(device = stm32f103xx)]
  31. const APP: () = {
  32. static mut LED_GLOBAL: PC13<Output<PushPull>> = ();
  33. static mut STEP_PIN: PA0<Output<PushPull>> = ();
  34. static mut PERIOD: u32 = ();
  35. static STEP_MIN_DURATION: Duration = ();
  36. static DIR_MIN_DURATION: Duration = ();
  37.  
  38.  
  39. #[init(schedule = [toggle])]
  40. unsafe fn init() {
  41. let mut rcc = device.RCC.constrain();
  42. let mut gpioa = device.GPIOA.split(&mut rcc.apb2);
  43. let mut gpioc = device.GPIOC.split(&mut rcc.apb2);
  44. let mut flash = device.FLASH.constrain();
  45. let clocks = rcc.cfgr.freeze(&mut flash.acr);
  46. let sysclock = clocks.sysclk();
  47. let period = hertz_to_cycles(sysclock, 50.hz());
  48. schedule.toggle(Instant::now() + period.cycles()).unwrap();
  49. PERIOD = period;
  50. LED_GLOBAL = gpioc.pc13.into_push_pull_output(&mut gpioc.crh);
  51. STEP_PIN = gpioa.pa0.into_push_pull_output(&mut gpioa.crl);
  52. STEP_MIN_DURATION = seconds_to_cycles(sysclock, STEP_MIN_S).cycles();
  53. DIR_MIN_DURATION = seconds_to_cycles(sysclock, DIR_MIN_S).cycles();
  54. }
  55.  
  56. #[task(schedule = [toggle, set_step_high, set_step_low], resources = [LED_GLOBAL, PERIOD, STEP_MIN_DURATION])]
  57. fn toggle() {
  58. resources.LED_GLOBAL.toggle();
  59. *resources.PERIOD = (*resources.PERIOD as f32 * 1.1) as u32;
  60. schedule.toggle(scheduled + resources.PERIOD.cycles()).unwrap();
  61. schedule.set_step_high(scheduled + resources.PERIOD.cycles()).unwrap();
  62. schedule.set_step_low(scheduled + resources.PERIOD.cycles() + *resources.STEP_MIN_DURATION).unwrap();
  63. }
  64.  
  65. fn schedule_step() { // <--- error: this item must live outside the `#[app]` module
  66. }
  67.  
  68. #[task(resources = [STEP_PIN])]
  69. fn set_step_high() {
  70. resources.STEP_PIN.set_high();
  71. }
  72.  
  73. #[task(resources = [STEP_PIN])]
  74. fn set_step_low() {
  75. resources.STEP_PIN.set_low();
  76. }
  77.  
  78. extern "C" {
  79. fn USART1();
  80. }
  81. };
Add Comment
Please, Sign In to add comment