main.rs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #![cfg_attr(test, allow(unused_imports))]
  2. #![cfg_attr(not(test), no_std)]
  3. #![cfg_attr(not(test), no_main)]
  4. #![feature(half_open_range_patterns)]
  5. #![feature(exclusive_range_pattern)]
  6. #![allow(dead_code)]
  7. // custom panic handler
  8. #[cfg(not(test))]
  9. use core::panic::PanicInfo;
  10. use core::{borrow::Borrow, cell::RefCell, ops::DerefMut};
  11. use cortex_m::{asm, interrupt::free, interrupt::Mutex, peripheral::NVIC};
  12. use cortex_m_rt::entry;
  13. // use cortex_m_semihosting::hprintln;
  14. use stm32l4xx_hal::{
  15. delay::Delay,
  16. device::I2C1,
  17. gpio::{
  18. Alternate, Edge, Floating, Input, OpenDrain, Output, PullUp, PushPull, AF4, PA3, PB5, PC15,
  19. },
  20. gpio::{State, PA10, PA9},
  21. i2c::I2c,
  22. interrupt, pac,
  23. prelude::*,
  24. rcc,
  25. stm32::Interrupt,
  26. };
  27. mod ds3231;
  28. mod nixie;
  29. mod pca9685;
  30. mod tusb322;
  31. use nixie::*;
  32. static RTC_INT: Mutex<RefCell<Option<PB5<Input<Floating>>>>> = Mutex::new(RefCell::new(None));
  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. static I2C: Mutex<
  36. RefCell<
  37. Option<
  38. I2c<
  39. I2C1,
  40. (
  41. PA9<Alternate<AF4, Output<OpenDrain>>>,
  42. PA10<Alternate<AF4, Output<OpenDrain>>>,
  43. ),
  44. >,
  45. >,
  46. >,
  47. > = Mutex::new(RefCell::new(None));
  48. // unsafe fn any_as_u8_slice<T: Sized>(p: &T) -> &[u8] {
  49. // core::slice::from_raw_parts(
  50. // (p as *const T) as *const u8,
  51. // core::mem::size_of::<T>(),
  52. // )
  53. // }
  54. // pub fn concat<T: Copy + Default, const A: usize, const B: usize>(a: &[T; A], b: &[T; B]) -> [T; A+B] {
  55. // let mut whole: [T; A+B] = [Default::default(); A+B];
  56. // let (one, two) = whole.split_at_mut(A);
  57. // one.copy_from_slice(a);
  58. // two.copy_from_slice(b);
  59. // whole
  60. // }
  61. #[cfg(not(test))]
  62. #[entry]
  63. fn main() -> ! {
  64. // Semihosting only works if debugger is connected.
  65. // See https://github.com/rust-embedded/cortex-m/issues/289
  66. // hprintln!("Hello, world!").unwrap();
  67. // Acquire a singleton instance for the chip's peripherals
  68. let mut dp = pac::Peripherals::take().unwrap();
  69. let cp = pac::CorePeripherals::take().unwrap();
  70. // Consume the raw peripheral and return a new object that implements a higher level API
  71. let mut flash = dp.FLASH.constrain();
  72. let mut rcc = dp.RCC.constrain();
  73. let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1);
  74. // Configure clocks to run at maximum frequency off internal oscillator
  75. let clocks = rcc
  76. .cfgr
  77. .pll_source(rcc::PllSource::HSI16)
  78. .sysclk(64.mhz())
  79. .hclk(64.mhz())
  80. .pclk1(64.mhz())
  81. .pclk2(64.mhz())
  82. .freeze(&mut flash.acr, &mut pwr);
  83. // Configure abstract timer that operates off systick timer
  84. let mut timer = Delay::new(cp.SYST, clocks);
  85. // Split GPIO peripheral into independent pins and registers
  86. let mut gpioa = dp.GPIOA.split(&mut rcc.ahb2);
  87. let mut gpiob = dp.GPIOB.split(&mut rcc.ahb2);
  88. let mut gpioc = dp.GPIOC.split(&mut rcc.ahb2);
  89. // Configure high voltage PSU enable pin on PA2
  90. let mut hv_enable =
  91. gpioa
  92. .pa2
  93. .into_push_pull_output_with_state(&mut gpioa.moder, &mut gpioa.otyper, State::Low);
  94. // Configure fault LED output on PC15
  95. let fault_led = gpioc.pc15.into_push_pull_output_with_state(
  96. &mut gpioc.moder,
  97. &mut gpioc.otyper,
  98. State::Low,
  99. );
  100. // Store fault LED in static singleton so that interrupt has access to it
  101. free(|cs| {
  102. FAULT_LED.borrow(cs).replace(Some(fault_led));
  103. });
  104. // Configure fault input interrupt on PA3
  105. let mut fault_int = gpioa
  106. .pa3
  107. .into_pull_up_input(&mut gpioa.moder, &mut gpioa.pupdr);
  108. fault_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
  109. fault_int.enable_interrupt(&mut dp.EXTI);
  110. fault_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
  111. // Sanity check that fault pin isn't already set (active low) before enabling interrupt
  112. if fault_int.is_high().unwrap() {
  113. // Configure NVIC mask to enable interrupt source
  114. unsafe {
  115. NVIC::unmask(Interrupt::EXTI3);
  116. }
  117. // Store fault interrupt in static singleton so that interrupt has access to it
  118. free(|cs| {
  119. FAULT_INT.borrow(cs).replace(Some(fault_int));
  120. });
  121. } else {
  122. panic!();
  123. }
  124. // Configure I2C SCL
  125. let scl = gpioa
  126. .pa9
  127. .into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
  128. let scl = scl.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
  129. // Configure I2C SDA
  130. let sda = gpioa
  131. .pa10
  132. .into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
  133. let sda = sda.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
  134. // Initialize I2C (configured for 1Mhz, but actually runs at 600kHz)
  135. let mut i2c = I2c::i2c1(dp.I2C1, (scl, sda), 1.mhz(), clocks, &mut rcc.apb1r1);
  136. // Initialize TUSB322 (USB Type-C configuration chip)
  137. tusb322::init(TUSB322_ADDR, &mut i2c);
  138. // Initialize DS3231 (RTC)
  139. ds3231::init(DS3231_ADDR, &mut i2c);
  140. // Configure input interrupt pin from DS3231 on PB5
  141. // Interrupt is pulled high, with open drain on DS3231 to pull low
  142. let mut rtc_int = gpiob
  143. .pb5
  144. .into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr);
  145. rtc_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
  146. rtc_int.enable_interrupt(&mut dp.EXTI);
  147. rtc_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
  148. // Configure NVIC mask to enable interrupt source
  149. unsafe {
  150. NVIC::unmask(Interrupt::EXTI9_5);
  151. }
  152. // Store RTC interrupt in static singleton so that interrupt has access to it
  153. free(|cs| {
  154. RTC_INT.borrow(cs).replace(Some(rtc_int));
  155. });
  156. // Configure DAC AMP enable pin for AD8591 on PB1
  157. let mut _dac_enable = gpiob.pb1.into_push_pull_output_with_state(
  158. &mut gpiob.moder,
  159. &mut gpiob.otyper,
  160. State::High,
  161. );
  162. // Configure DAC VIN for AD8591 on PA5
  163. // Note that this pin should actually be configured as analog output (for DAC)
  164. // but stm32l4xx_hal doesn't have support for the DAC as of now. We also currently
  165. // set the output to only the highest possible voltage, so the same functionality
  166. // can be achieved by configuring the pin as a digital output set to high.
  167. let mut _dac_output = gpioa.pa5.into_push_pull_output_with_state(
  168. &mut gpioa.moder,
  169. &mut gpioa.otyper,
  170. State::High,
  171. );
  172. // Configure PWM enable pin (active low) for PCA9685 on PA7
  173. let mut pwm_enable = gpioa.pa7.into_push_pull_output_with_state(
  174. &mut gpioa.moder,
  175. &mut gpioa.otyper,
  176. State::High,
  177. );
  178. // Small delay to ensure that PCA9685 is fully powered on before writing to it
  179. timer.delay_us(10_u32);
  180. // Initialize PCA9685 (PWM driver)
  181. pca9685::init(PCA9685_ALL_CALL, &mut i2c);
  182. // Enable PWM output after PCA9685 has been initialized
  183. pwm_enable.set_low().unwrap();
  184. pca9685::set_digit(PCA9685_ADDR_1, &mut i2c, 0, 0, 4096);
  185. // Store I2C peripheral in global static variable as it is used in interrupt
  186. free(|cs| {
  187. I2C.borrow(cs).replace(Some(i2c));
  188. });
  189. // Enable the high voltage power supply last
  190. hv_enable.set_high().unwrap();
  191. loop {}
  192. }
  193. fn set_fault_led(state: State) {
  194. free(|cs| {
  195. let mut led_ref = FAULT_LED.borrow(cs).borrow_mut();
  196. if let Some(ref mut led) = led_ref.deref_mut() {
  197. match state {
  198. State::High => led.set_high().unwrap(),
  199. State::Low => led.set_low().unwrap(),
  200. };
  201. }
  202. });
  203. }
  204. #[interrupt]
  205. fn EXTI9_5() {
  206. free(|cs| {
  207. let mut rtc_int_ref = RTC_INT.borrow(cs).borrow_mut();
  208. let mut i2c_int_ref = I2C.borrow(cs).borrow_mut();
  209. if let Some(ref mut rtc_int) = rtc_int_ref.deref_mut() {
  210. if let Some(ref mut i2c) = i2c_int_ref.deref_mut() {
  211. if rtc_int.check_interrupt() {
  212. rtc_int.clear_interrupt_pending_bit();
  213. nixie::rtc_tick(i2c);
  214. // set_fault_led(State::High);
  215. // asm::delay(8_000_000);
  216. // set_fault_led(State::Low);
  217. }
  218. }
  219. }
  220. });
  221. }
  222. #[interrupt]
  223. fn EXTI3() {
  224. free(|cs| {
  225. let mut nfault_ref = FAULT_INT.borrow(cs).borrow_mut();
  226. if let Some(ref mut nfault) = nfault_ref.deref_mut() {
  227. if nfault.check_interrupt() {
  228. // hprintln!("Fault pin interrupt triggered!").unwrap();
  229. // nfault.clear_interrupt_pending_bit();
  230. panic!();
  231. }
  232. }
  233. });
  234. }
  235. #[panic_handler]
  236. #[cfg(not(test))]
  237. /// Custom panic handler
  238. fn panic(_info: &PanicInfo) -> ! {
  239. // if let Some(location) = info.location() {
  240. // hprintln!(
  241. // "Panic in file '{}' at line {}",
  242. // location.file(),
  243. // location.line()
  244. // )
  245. // .unwrap();
  246. // } else {
  247. // hprintln!("Panic'd!").unwrap();
  248. // }
  249. set_fault_led(State::High);
  250. loop {
  251. continue;
  252. }
  253. }