main.rs 5.9 KB

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