|
1 | 1 | #![no_std]
|
2 | 2 | #![no_main]
|
3 | 3 |
|
4 |
| -use esp32_hal as hal; |
5 |
| - |
6 |
| -use { |
7 |
| - core::panic::PanicInfo, |
8 |
| - embedded_graphics::{ |
9 |
| - fonts::{Font8x16, Text}, |
10 |
| - pixelcolor::BinaryColor, |
11 |
| - prelude::*, |
12 |
| - style::TextStyle, |
13 |
| - }, |
14 |
| - hal::{ |
15 |
| - clock_control::{self, sleep, CPUSource, ClockControl, ClockControlConfig}, |
16 |
| - dport::Split, |
17 |
| - dprintln, i2c, |
18 |
| - prelude::*, |
19 |
| - timer::Timer, |
20 |
| - }, |
21 |
| - // mpu6050::Mpu6050, |
22 |
| - ssd1306::{prelude::*, Builder}, |
| 4 | +use core::panic::PanicInfo; |
| 5 | +use embedded_graphics::{ |
| 6 | + fonts::{Font8x16, Text}, |
| 7 | + pixelcolor::BinaryColor, |
| 8 | + prelude::*, |
| 9 | + style::TextStyle, |
23 | 10 | };
|
| 11 | +use embedded_hal::blocking::i2c::{Write, WriteRead}; |
| 12 | +use esp32_hal::{ |
| 13 | + clock_control::{self, sleep, CPUSource, ClockControl}, |
| 14 | + delay::Delay, |
| 15 | + dport::Split, |
| 16 | + dprintln, |
| 17 | + interrupt::{self, Interrupt}, |
| 18 | + i2c::{self, Error, I2C}, |
| 19 | + prelude::*, |
| 20 | + target::{I2C0, Peripherals}, |
| 21 | + timer::Timer, |
| 22 | +}; |
| 23 | +use mpu6050::Mpu6050; |
| 24 | +use ssd1306::{prelude::*, Builder}; |
| 25 | +use xtensa_lx::mutex::SpinLockMutex; |
24 | 26 |
|
25 | 27 | #[entry]
|
26 | 28 | fn main() -> ! {
|
27 |
| - let dp = esp32::Peripherals::take().unwrap(); |
| 29 | + let dp = Peripherals::take().unwrap(); |
28 | 30 |
|
29 | 31 | let (mut dport, dport_clock_control) = dp.DPORT.split();
|
30 | 32 |
|
@@ -58,102 +60,101 @@ fn main() -> ! {
|
58 | 60 | let (.., mut watchdog1) = Timer::new(dp.TIMG1, clkcntrl_config);
|
59 | 61 | watchdog0.disable();
|
60 | 62 | watchdog1.disable();
|
| 63 | + dprintln!("Watchdogs disabled"); |
| 64 | + sleep(2.s()); |
| 65 | + |
| 66 | + interrupt::enable(Interrupt::I2C_EXT0_INTR).unwrap(); |
61 | 67 |
|
62 | 68 | let pins = dp.GPIO.split();
|
| 69 | + let i2c0 = i2c::I2C::new( |
| 70 | + dp.I2C0, |
| 71 | + i2c::Pins { |
| 72 | + sda: pins.gpio4, |
| 73 | + scl: pins.gpio15, |
| 74 | + }, |
| 75 | + 400_000, |
| 76 | + &mut dport, |
| 77 | + ); |
| 78 | + let i2c0 = SpinLockMutex::new(i2c0); |
63 | 79 |
|
64 | 80 | // Display
|
65 | 81 | let mut display = {
|
66 |
| - let i2c0 = i2c::I2C::new( |
67 |
| - dp.I2C0, |
68 |
| - i2c::Pins { |
69 |
| - sda: pins.gpio4, |
70 |
| - scl: pins.gpio15, |
71 |
| - }, |
72 |
| - 400_000, |
73 |
| - &mut dport, |
74 |
| - ); |
75 |
| - |
76 |
| - let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c0).into(); |
| 82 | + let i2c_wrapper = I2CWrapper::new(&i2c0); |
| 83 | + let mut display: GraphicsMode<_> = Builder::new().connect_i2c(i2c_wrapper).into(); |
77 | 84 |
|
78 | 85 | let mut rst = pins.gpio16.into_push_pull_output();
|
79 | 86 | rst.set_low().unwrap();
|
80 | 87 | sleep(10.ms());
|
81 | 88 | rst.set_high().unwrap();
|
82 | 89 |
|
| 90 | + dprintln!("starting display init"); |
83 | 91 | display.init().unwrap();
|
| 92 | + dprintln!("finished display init"); |
84 | 93 | display.clear();
|
85 | 94 | display.flush().unwrap();
|
86 | 95 |
|
87 | 96 | display
|
88 | 97 | };
|
| 98 | + dprintln!("Set up display"); |
89 | 99 |
|
90 |
| - /*// IMU |
| 100 | + // IMU |
91 | 101 | let mut imu = {
|
92 |
| - let i2c1 = i2c::I2C::new( |
93 |
| - dp.I2C1, |
94 |
| - i2c::Pins { |
95 |
| - sda: pins.gpio22, |
96 |
| - scl: pins.gpio23, |
97 |
| - }, |
98 |
| - 200_000, |
99 |
| - &mut dport, |
100 |
| - ); |
101 |
| -
|
102 |
| - let mut imu = Mpu6050::new(i2c1, Delay); |
103 |
| -
|
104 |
| - imu.verify().unwrap(); |
105 |
| -
|
106 |
| - imu.init().unwrap(); |
107 |
| - imu.soft_calib(mpu6050::Steps(100)).unwrap(); |
108 |
| - imu.calc_variance(mpu6050::Steps(50)).unwrap(); |
| 102 | + let i2c_wrapper = I2CWrapper::new(&i2c0); |
| 103 | + let mut imu = Mpu6050::new(i2c_wrapper); |
109 | 104 |
|
| 105 | + let mut delay = Delay::new(); |
| 106 | + imu.init(&mut delay).unwrap(); |
110 | 107 | imu
|
111 | 108 | };
|
112 |
| -
|
113 |
| - let temp = imu.get_temp().unwrap(); |
114 |
| - let gyro = imu.get_gyro().unwrap(); |
115 |
| - let acc = imu.get_acc().unwrap(); |
116 |
| - dprintln!("temp: {}, gyro: {:?}, acc: {:?}", temp, gyro, acc);*/ |
117 |
| - |
118 |
| - // let mut sensor = { |
119 |
| - // let i2c1 = i2c::I2C::new( |
120 |
| - // dp.I2C1, |
121 |
| - // i2c::Pins { |
122 |
| - // sda: pins.gpio22, |
123 |
| - // scl: pins.gpio23, |
124 |
| - // }, |
125 |
| - // 200_000, |
126 |
| - // &mut dport, |
127 |
| - // ); |
128 |
| - // |
129 |
| - // let mut sensor = sgp30::Sgp30::new(i2c1, 0x58, Delay); |
130 |
| - // |
131 |
| - // dprintln!("serial: {:?}", sensor.serial().unwrap()); |
132 |
| - // |
133 |
| - // sensor |
134 |
| - // }; |
| 109 | + dprintln!("Set up IMU"); |
135 | 110 |
|
136 | 111 | Text::new("Hello world!", Point::new(2, 28))
|
137 | 112 | .into_styled(TextStyle::new(Font8x16, BinaryColor::On))
|
138 | 113 | .draw(&mut display)
|
139 | 114 | .unwrap();
|
140 | 115 | display.flush().unwrap();
|
141 |
| - loop {} |
| 116 | + dprintln!("Wrote to display"); |
| 117 | + |
| 118 | + sleep(3.s()); |
| 119 | + |
| 120 | + loop { |
| 121 | + let temp = imu.get_temp().unwrap(); |
| 122 | + let gyro = imu.get_gyro().unwrap(); |
| 123 | + let acc = imu.get_acc().unwrap(); |
| 124 | + dprintln!("temp: {}, gyro: {:?}, acc: {:?}", temp, gyro, acc); |
| 125 | + sleep(1.s()); |
| 126 | + } |
142 | 127 | }
|
143 | 128 |
|
144 |
| -#[panic_handler] |
145 |
| -fn panic(info: &PanicInfo) -> ! { |
146 |
| - // park the other core |
147 |
| - unsafe { ClockControlConfig {}.park_core(esp32_hal::get_other_core()) }; |
| 129 | +struct I2CWrapper<'a> { |
| 130 | + i2c: &'a SpinLockMutex<I2C<I2C0>>, |
| 131 | +} |
148 | 132 |
|
149 |
| - // print panic message |
150 |
| - dprintln!("\n\n*** {:?}", info); |
| 133 | +impl<'a> I2CWrapper<'a> { |
| 134 | + fn new(i2c: &'a SpinLockMutex<I2C<I2C0>>) -> Self { |
| 135 | + Self { i2c } |
| 136 | + } |
| 137 | +} |
151 | 138 |
|
152 |
| - // park this core |
153 |
| - unsafe { ClockControlConfig {}.park_core(esp32_hal::get_core()) }; |
| 139 | +impl<'a> Write for I2CWrapper<'a> { |
| 140 | + type Error = Error; |
| 141 | + |
| 142 | + fn write(&mut self, addr: u8, bytes: &[u8]) -> Result<(), Self::Error> { |
| 143 | + self.i2c.lock(|x| x.write(addr, bytes)) |
| 144 | + } |
| 145 | +} |
154 | 146 |
|
155 |
| - dprintln!("Not reached because core is parked."); |
| 147 | +impl<'a> WriteRead for I2CWrapper<'a> { |
| 148 | + type Error = Error; |
156 | 149 |
|
157 |
| - // this statement will not be reached, but is needed to make this a diverging function |
| 150 | + fn write_read(&mut self, address: u8, bytes: &[u8], buffer: &mut [u8]) -> Result<(), Self::Error> { |
| 151 | + self.i2c.lock(|x| x.write_read(address, bytes, buffer)) |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +#[panic_handler] |
| 156 | +fn panic(info: &PanicInfo) -> ! { |
| 157 | + dprintln!("----- PANIC -----"); |
| 158 | + dprintln!("{:?}", info); |
158 | 159 | loop {}
|
159 | 160 | }
|
0 commit comments