main.rs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. #![cfg_attr(test, allow(unused_imports))]
  2. #![cfg_attr(not(test), no_std)]
  3. #![cfg_attr(not(test), no_main)]
  4. // #![feature(generic_const_exprs)]
  5. #![feature(half_open_range_patterns)]
  6. #![feature(exclusive_range_pattern)]
  7. #![allow(dead_code)]
  8. // custom panic handler
  9. #[cfg(not(test))]
  10. use core::panic::PanicInfo;
  11. use core::{cell::RefCell, ops::DerefMut};
  12. // use cortex_m::asm;
  13. use cortex_m::{interrupt::free, interrupt::Mutex, peripheral::NVIC};
  14. use cortex_m_rt::entry;
  15. // use cortex_m_semihosting::hprintln;
  16. use stm32l4xx_hal::{
  17. delay::Delay,
  18. gpio::State,
  19. gpio::{Edge, Input, Output, PullUp, PushPull, PA3, PC15},
  20. i2c::I2c,
  21. interrupt, pac,
  22. prelude::*,
  23. rcc,
  24. stm32::Interrupt,
  25. };
  26. mod tusb322;
  27. mod ds3231;
  28. mod pca9685;
  29. static FAULT_INT: Mutex<RefCell<Option<PA3<Input<PullUp>>>>> = Mutex::new(RefCell::new(None));
  30. static FAULT_LED: Mutex<RefCell<Option<PC15<Output<PushPull>>>>> = Mutex::new(RefCell::new(None));
  31. // unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
  32. // core::slice::from_raw_parts(
  33. // (p as *const T) as *const u8,
  34. // core::mem::size_of::<T>(),
  35. // )
  36. // }
  37. // pub fn concat<T: Copy + Default, const A: usize, const B: usize>(a: &[T; A], b: &[T; B]) -> [T; A+B] {
  38. // let mut whole: [T; A+B] = [Default::default(); A+B];
  39. // let (one, two) = whole.split_at_mut(A);
  40. // one.copy_from_slice(a);
  41. // two.copy_from_slice(b);
  42. // whole
  43. // }
  44. #[cfg(not(test))]
  45. #[entry]
  46. fn main() -> ! {
  47. // Semihosting only works if debugger is connected.
  48. // See https://github.com/rust-embedded/cortex-m/issues/289
  49. // hprintln!("Hello, world!").unwrap();
  50. // Acquire a singleton instance for the chip's peripherals
  51. let mut dp = pac::Peripherals::take().unwrap();
  52. let cp = pac::CorePeripherals::take().unwrap();
  53. // Consume the raw peripheral and return a new object that implements a higher level API
  54. let mut flash = dp.FLASH.constrain();
  55. let mut rcc = dp.RCC.constrain();
  56. let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1);
  57. // Configure clocks to run at maximum frequency off internal oscillator
  58. let clocks = rcc
  59. .cfgr
  60. .pll_source(rcc::PllSource::HSI16)
  61. .sysclk(80.mhz())
  62. .hclk(80.mhz())
  63. .pclk1(80.mhz())
  64. .pclk2(80.mhz())
  65. .freeze(&mut flash.acr, &mut pwr);
  66. // Split GPIO peripheral into independent pins and registers
  67. // let mut gpiob = dp.GPIOB.split(&mut rcc.ahb2);
  68. let mut gpioa = dp.GPIOA.split(&mut rcc.ahb2);
  69. let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2);
  70. // Configure fault LED output on PC15
  71. let fault_led = gpioc.pc15.into_push_pull_output_with_state(
  72. &mut gpioc.moder,
  73. &mut gpioc.otyper,
  74. State::Low,
  75. );
  76. // Store fault LED in global static variable as it is accessed in interrupts
  77. free(|cs| {
  78. FAULT_LED.borrow(cs).replace(Some(fault_led));
  79. });
  80. // Configure fault input interrupt on PA3
  81. let mut fault_int = gpioa
  82. .pa3
  83. .into_pull_up_input(&mut gpioa.moder, &mut gpioa.pupdr);
  84. fault_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
  85. fault_int.enable_interrupt(&mut dp.EXTI);
  86. fault_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
  87. // Sanity check that fault pin isn't already set (active low) before enabling interrupt
  88. if fault_int.is_high().unwrap() {
  89. // Configure NVIC mask to enable interrupt source
  90. unsafe {
  91. NVIC::unmask(Interrupt::EXTI3);
  92. }
  93. // Store fault input in global static variable as it is accessed in interrupt
  94. free(|cs| {
  95. FAULT_INT.borrow(cs).replace(Some(fault_int));
  96. });
  97. } else {
  98. panic!();
  99. }
  100. // Start with HV PSU disabled (enable pin on PA2)
  101. let mut _hv_en =
  102. gpioa
  103. .pa2
  104. .into_push_pull_output_with_state(&mut gpioa.moder, &mut gpioa.otyper, State::Low);
  105. // Configure I2C SCL
  106. let scl = gpioa
  107. .pa9
  108. .into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
  109. let scl = scl.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
  110. // Configure I2C SDA
  111. let sda = gpioa
  112. .pa10
  113. .into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
  114. let sda = sda.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
  115. // Initialize I2C
  116. let mut i2c = I2c::i2c1(dp.I2C1, (scl, sda), 100.khz(), clocks, &mut rcc.apb1r1);
  117. tusb322::init(&mut i2c);
  118. ds3231::init(&mut i2c);
  119. // Configure abstract timer that operates off systick timer
  120. let mut timer = Delay::new(cp.SYST, clocks);
  121. loop {
  122. timer.delay_ms(1000_u32);
  123. set_fault_led(State::High);
  124. timer.delay_ms(1000_u32);
  125. set_fault_led(State::Low);
  126. }
  127. }
  128. fn set_fault_led(state: State) {
  129. free(|cs| {
  130. let mut led_ref = FAULT_LED.borrow(cs).borrow_mut();
  131. if let Some(ref mut led) = led_ref.deref_mut() {
  132. match state {
  133. State::High => led.set_high().unwrap(),
  134. State::Low => led.set_low().unwrap(),
  135. };
  136. }
  137. });
  138. }
  139. #[interrupt]
  140. fn EXTI3() {
  141. free(|cs| {
  142. let mut nfault_ref = FAULT_INT.borrow(cs).borrow_mut();
  143. if let Some(ref mut nfault) = nfault_ref.deref_mut() {
  144. if nfault.check_interrupt() {
  145. // hprintln!("Fault pin interrupt triggered!").unwrap();
  146. // nfault.clear_interrupt_pending_bit();
  147. panic!();
  148. }
  149. }
  150. });
  151. }
  152. #[panic_handler]
  153. #[cfg(not(test))]
  154. /// Custom panic handler
  155. fn panic(_info: &PanicInfo) -> ! {
  156. // if let Some(location) = info.location() {
  157. // hprintln!(
  158. // "Panic in file '{}' at line {}",
  159. // location.file(),
  160. // location.line()
  161. // )
  162. // .unwrap();
  163. // } else {
  164. // hprintln!("Panic'd!").unwrap();
  165. // }
  166. set_fault_led(State::High);
  167. loop {
  168. continue;
  169. }
  170. }