暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.rs 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #![no_main]
  2. #![no_std]
  3. mod pins;
  4. mod assets {
  5. include!(concat!(env!("OUT_DIR"), "/assets.rs"));
  6. }
  7. use cortex_m_semihosting::{debug, hprintln};
  8. use embedded_epd as epd;
  9. use embedded_epd::{gui, Display};
  10. use embedded_hal::blocking::delay::DelayMs;
  11. use embedded_hal::digital::v2::OutputPin;
  12. use embedded_hal::timer::CountDown;
  13. use epd_waveshare::{epd4in2::*, prelude::*};
  14. use mkl25z4_hal as hal;
  15. use mkl25z4_hal::clocks::ClockConfiguration;
  16. use mkl25z4_hal::time::NonCopyableMonoTimer;
  17. use mkl25z4_hal::time::U32Ext;
  18. use mkl25z4_hal::timer::Timer;
  19. use nb::block;
  20. use panic_semihosting as _;
  21. struct EPDTimer<Timer> {
  22. timer: Timer,
  23. }
  24. impl<Timer> CountDown for EPDTimer<Timer>
  25. where
  26. Timer: CountDown<Time = hal::time::Hertz>,
  27. {
  28. type Time = epd::Hertz;
  29. fn start<T>(&mut self, timeout: T)
  30. where
  31. T: Into<epd::Hertz>,
  32. {
  33. self.timer.start(hal::time::Hertz(timeout.into().0))
  34. }
  35. fn wait(&mut self) -> nb::Result<(), void::Void> {
  36. self.timer.wait()
  37. }
  38. }
  39. #[rtfm::app(device = mkl25z4_hal::mkl25z4, peripherals = true)]
  40. const APP: () = {
  41. struct Resources {}
  42. #[init]
  43. fn init(ctx: init::Context) {
  44. let mut sim = ctx.device.SIM;
  45. mkl25z4_hal::watchdog::disable(&mut sim);
  46. let clocks = ClockConfiguration::new()
  47. .use_irc()
  48. .core_clock(24.mhz())
  49. .bus_clock(24.mhz())
  50. .apply(&mut sim, ctx.device.OSC0, ctx.device.MCG);
  51. let mut pins = pins::Pins::configure(
  52. &mut sim,
  53. clocks,
  54. ctx.device.GPIOA,
  55. ctx.device.GPIOB,
  56. ctx.device.GPIOC,
  57. ctx.device.GPIOD,
  58. ctx.device.GPIOE,
  59. ctx.device.SPI0,
  60. ctx.device.SPI1,
  61. );
  62. // Enable power.
  63. pins.display_pwr.set_low().ok();
  64. pins.display_rst.set_high().unwrap();
  65. pins.display_dc.set_high().unwrap();
  66. pins.display_cs.set_high().unwrap();
  67. let mut timer = NonCopyableMonoTimer::new(ctx.device.PIT, clocks, &mut sim);
  68. let mut epd = EPD4in2::new(
  69. &mut pins.display_bme_spi,
  70. pins.display_cs,
  71. pins.display_busy,
  72. pins.display_dc,
  73. pins.display_rst,
  74. &mut timer,
  75. )
  76. .unwrap();
  77. epd.set_background_color(Color::Black);
  78. let checkerboard = Checkerboard::new(400, 300, true);
  79. epd.update_frame_stream(&mut pins.display_bme_spi, checkerboard)
  80. .unwrap();
  81. epd.display_frame(&mut pins.display_bme_spi).unwrap();
  82. }
  83. #[idle]
  84. fn idle(_cx: idle::Context) -> ! {
  85. // Sleep
  86. loop {
  87. // wfi needs to be disabled for GDB
  88. /*// add some side effect to prevent this from turning into a UDF instruction
  89. // see rust-lang/rust#28728 for details
  90. atomic::compiler_fence(Ordering::SeqCst);*/
  91. cortex_m::asm::wfi();
  92. }
  93. }
  94. };
  95. struct Checkerboard {
  96. width: usize,
  97. height: usize,
  98. index: usize,
  99. buffer: [u8; 1],
  100. }
  101. impl Checkerboard {
  102. fn new(width: usize, height: usize, start_black: bool) -> Self {
  103. Self {
  104. width: width / 8,
  105. height,
  106. index: 0,
  107. buffer: [if start_black { 0x55 } else { 0xaa }],
  108. }
  109. }
  110. }
  111. impl DisplayStream for Checkerboard {
  112. fn next<'a>(&'a mut self) -> Option<&'a [u8]> {
  113. if self.index != self.width * self.height {
  114. self.index += 1;
  115. if (self.index - 1) % self.width == 0 {
  116. self.buffer[0] = !self.buffer[0];
  117. }
  118. Some(&self.buffer)
  119. } else {
  120. None
  121. }
  122. }
  123. }