main.rs 9.6 KB

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