main.rs 6.0 KB

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