main.rs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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 stm32l4xx_hal::{
  14. delay::Delay,
  15. device::{I2C1, TIM2, TIM7},
  16. gpio::{
  17. Alternate, Edge, Floating, Input, OpenDrain, Output, PullUp, PushPull, AF4, PA3, PB5, PC15,
  18. },
  19. gpio::{State, PA10, PA9},
  20. i2c::I2c,
  21. interrupt, pac,
  22. prelude::*,
  23. rcc,
  24. stm32::Interrupt,
  25. timer::Timer,
  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. static FPS_TIMER: Mutex<RefCell<Option<Timer<TIM2>>>> = Mutex::new(RefCell::new(None));
  49. static CYCLE_TIMER: Mutex<RefCell<Option<Timer<TIM7>>>> = 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. // Acquire a singleton instance for the chip's peripherals
  67. let mut dp = pac::Peripherals::take().unwrap();
  68. let cp = pac::CorePeripherals::take().unwrap();
  69. // Consume the raw peripheral and return a new object that implements a higher level API
  70. let mut flash = dp.FLASH.constrain();
  71. let mut rcc = dp.RCC.constrain();
  72. let mut pwr = dp.PWR.constrain(&mut rcc.apb1r1);
  73. // Configure clocks to run at maximum frequency off internal oscillator
  74. let clocks = rcc
  75. .cfgr
  76. .pll_source(rcc::PllSource::HSI16)
  77. .sysclk(64.mhz())
  78. .hclk(64.mhz())
  79. .pclk1(64.mhz())
  80. .pclk2(64.mhz())
  81. .hsi48(true)
  82. .freeze(&mut flash.acr, &mut pwr);
  83. // Configure delay timer that operates off systick timer
  84. let mut delay_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 serial port
  95. // let tx = gpiob.pb6.into_af7(&mut gpiob.moder, &mut gpiob.afrl);
  96. // let rx = gpiob.pb7.into_af7(&mut gpiob.moder, &mut gpiob.afrl);
  97. // let _serial = Serial::usart1(
  98. // dp.USART1,
  99. // (tx, rx),
  100. // Config::default().baudrate(115_200.bps()),
  101. // clocks,
  102. // &mut rcc.apb2,
  103. // );
  104. // Configure fault LED output on PC15
  105. let fault_led = gpioc.pc15.into_push_pull_output_with_state(
  106. &mut gpioc.moder,
  107. &mut gpioc.otyper,
  108. State::Low,
  109. );
  110. // Store fault LED in static singleton so that interrupt has access to it
  111. free(|cs| {
  112. FAULT_LED.borrow(cs).replace(Some(fault_led));
  113. });
  114. // Configure fault input interrupt on PA3
  115. let mut fault_int = gpioa
  116. .pa3
  117. .into_pull_up_input(&mut gpioa.moder, &mut gpioa.pupdr);
  118. fault_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
  119. fault_int.enable_interrupt(&mut dp.EXTI);
  120. fault_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
  121. // Sanity check that fault pin isn't already set (active low) before enabling interrupt
  122. if fault_int.is_high().unwrap() {
  123. // Configure NVIC mask to enable interrupt source
  124. unsafe {
  125. NVIC::unmask(Interrupt::EXTI3);
  126. }
  127. // Store fault interrupt in static singleton so that interrupt has access to it
  128. free(|cs| {
  129. FAULT_INT.borrow(cs).replace(Some(fault_int));
  130. });
  131. } else {
  132. panic!();
  133. }
  134. // Configure I2C SCL
  135. let scl = gpioa
  136. .pa9
  137. .into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
  138. let scl = scl.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
  139. // Configure I2C SDA
  140. let sda = gpioa
  141. .pa10
  142. .into_open_drain_output(&mut gpioa.moder, &mut gpioa.otyper);
  143. let sda = sda.into_af4(&mut gpioa.moder, &mut gpioa.afrh);
  144. // Initialize I2C (configured for 1Mhz, but actually runs at 600kHz)
  145. let mut i2c = I2c::i2c1(dp.I2C1, (scl, sda), 1.mhz(), clocks, &mut rcc.apb1r1);
  146. // Initialize TUSB322 (USB Type-C configuration chip)
  147. tusb322::init(TUSB322_ADDR, &mut i2c);
  148. // Initialize DS3231 (RTC)
  149. ds3231::init(DS3231_ADDR, &mut i2c);
  150. // Configure input interrupt pin from DS3231 on PB5
  151. // Interrupt is pulled high, with open drain on DS3231 to pull low
  152. let mut rtc_int = gpiob
  153. .pb5
  154. .into_floating_input(&mut gpiob.moder, &mut gpiob.pupdr);
  155. rtc_int.make_interrupt_source(&mut dp.SYSCFG, &mut rcc.apb2);
  156. rtc_int.enable_interrupt(&mut dp.EXTI);
  157. rtc_int.trigger_on_edge(&mut dp.EXTI, Edge::FALLING);
  158. // Configure NVIC mask to enable interrupt source
  159. unsafe { NVIC::unmask(Interrupt::EXTI9_5); }
  160. // Store RTC interrupt in static singleton so that interrupt has access to it
  161. free(|cs| {
  162. RTC_INT.borrow(cs).replace(Some(rtc_int));
  163. });
  164. // Configure DAC AMP enable pin for AD8591 on PB1
  165. let mut _dac_enable = gpiob.pb1.into_push_pull_output_with_state(
  166. &mut gpiob.moder,
  167. &mut gpiob.otyper,
  168. State::High,
  169. );
  170. // Configure DAC VIN for AD8591 on PA5
  171. // Note that this pin should actually be configured as analog output (for DAC)
  172. // but stm32l4xx_hal doesn't have support for the DAC as of now. We also currently
  173. // set the output to only the highest possible voltage, so the same functionality
  174. // can be achieved by configuring the pin as a digital output set to high.
  175. let mut _dac_output = gpioa.pa5.into_push_pull_output_with_state(
  176. &mut gpioa.moder,
  177. &mut gpioa.otyper,
  178. State::High,
  179. );
  180. // Configure PWM enable pin (active low) for PCA9685 on PA7
  181. let mut pwm_enable = gpioa.pa7.into_push_pull_output_with_state(
  182. &mut gpioa.moder,
  183. &mut gpioa.otyper,
  184. State::High,
  185. );
  186. // Initialize the PCA9685 display refresh timer
  187. let fps_timer = Timer::tim2(
  188. dp.TIM2,
  189. nixie::DISPLAY_REFRESH_FPS.hz(),
  190. clocks,
  191. &mut rcc.apb1r1,
  192. );
  193. // Configure NVIC mask to enable interrupt for TIM2
  194. unsafe { NVIC::unmask(Interrupt::TIM2) };
  195. // Save display refresh timer in static singleton so that interrupt has access to it
  196. free(|cs| {
  197. FPS_TIMER.borrow(cs).replace(Some(fps_timer));
  198. });
  199. // Small delay to ensure that PCA9685 is fully powered on before writing to it
  200. delay_timer.delay_us(10_u32);
  201. // Initialize PCA9685 (PWM driver)
  202. pca9685::init(PCA9685_ALL_CALL, &mut i2c);
  203. // Enable PWM output after PCA9685 has been initialized
  204. pwm_enable.set_low().unwrap();
  205. // Store I2C peripheral in global static variable as it is used in interrupt
  206. free(|cs| {
  207. I2C.borrow(cs).replace(Some(i2c));
  208. });
  209. let cycle_timer = Timer::tim7(
  210. dp.TIM7,
  211. (1000 / nixie::CYCLE_FADE_DURATION_MS).hz(),
  212. clocks,
  213. &mut rcc.apb1r1,
  214. );
  215. unsafe { NVIC::unmask(Interrupt::TIM7) };
  216. // Save display refresh timer in static singleton so that interrupt has access to it
  217. free(|cs| {
  218. CYCLE_TIMER.borrow(cs).replace(Some(cycle_timer));
  219. });
  220. let rng = dp.RNG.enable(&mut rcc.ahb2, clocks);
  221. // Enable the high voltage power supply last
  222. hv_enable.set_high().unwrap();
  223. nixie::cycle_start(0);
  224. nixie::cycle_start(1);
  225. nixie::cycle_start(2);
  226. nixie::cycle_start(3);
  227. loop {
  228. // let rng_delay = nixie::CYCLE_REFRESH_INTERVAL + (rng.get_random_data() % nixie::CYCLE_REFRESH_VARIANCE);
  229. let rng_delay = nixie::CYCLE_REFRESH_INTERVAL;
  230. delay_timer.delay_ms(rng_delay * 1000);
  231. let tube = (rng.get_random_data() % 4) as usize;
  232. nixie::cycle_start(tube);
  233. }
  234. }
  235. fn set_fault_led(state: State) {
  236. free(|cs| {
  237. let mut led_ref = FAULT_LED.borrow(cs).borrow_mut();
  238. if let Some(ref mut led) = led_ref.deref_mut() {
  239. match state {
  240. State::High => led.set_high().unwrap(),
  241. State::Low => led.set_low().unwrap(),
  242. };
  243. }
  244. });
  245. }
  246. #[interrupt]
  247. fn EXTI9_5() {
  248. free(|cs| {
  249. let mut rtc_int_ref = RTC_INT.borrow(cs).borrow_mut();
  250. let mut i2c_int_ref = I2C.borrow(cs).borrow_mut();
  251. if let Some(ref mut rtc_int) = rtc_int_ref.deref_mut() {
  252. if let Some(ref mut i2c) = i2c_int_ref.deref_mut() {
  253. if rtc_int.check_interrupt() {
  254. nixie::rtc_interrupt(i2c);
  255. rtc_int.clear_interrupt_pending_bit();
  256. }
  257. }
  258. }
  259. });
  260. }
  261. #[interrupt]
  262. fn EXTI3() {
  263. free(|cs| {
  264. let mut nfault_ref = FAULT_INT.borrow(cs).borrow_mut();
  265. if let Some(ref mut nfault) = nfault_ref.deref_mut() {
  266. if nfault.check_interrupt() {
  267. nfault.clear_interrupt_pending_bit();
  268. panic!();
  269. }
  270. }
  271. });
  272. }
  273. #[interrupt]
  274. fn TIM2() {
  275. free(|cs| {
  276. let mut i2c_int_ref = I2C.borrow(cs).borrow_mut();
  277. if let Some(ref mut i2c) = i2c_int_ref.deref_mut() {
  278. nixie::fps_interrupt(i2c);
  279. }
  280. });
  281. }
  282. #[interrupt]
  283. fn TIM7() {
  284. nixie::cycle_interrupt();
  285. }
  286. #[panic_handler]
  287. #[cfg(not(test))]
  288. /// Custom panic handler
  289. fn panic(_info: &PanicInfo) -> ! {
  290. set_fault_led(State::High);
  291. loop {
  292. continue;
  293. }
  294. }