Bläddra i källkod

Implement reading values from the BME280.

Mathias Gottschlag 5 år sedan
förälder
incheckning
8f05bab10e

+ 11
- 11
display/firmware/Cargo.lock Visa fil

@@ -44,7 +44,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
44 44
 [[package]]
45 45
 name = "bme280"
46 46
 version = "0.2.1"
47
-source = "registry+https://github.com/rust-lang/crates.io-index"
47
+source = "git+https://github.com/mgottschlag/bme280-rs?branch=destroy#e3c3f39a32261bd29b94431b97b214dff027e9bb"
48 48
 dependencies = [
49 49
  "embedded-hal 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
50 50
 ]
@@ -91,7 +91,7 @@ name = "cortex-m-rt-macros"
91 91
 version = "0.1.8"
92 92
 source = "registry+https://github.com/rust-lang/crates.io-index"
93 93
 dependencies = [
94
- "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
94
+ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
95 95
  "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
96 96
  "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
97 97
 ]
@@ -113,7 +113,7 @@ name = "cortex-m-rtfm-macros"
113 113
 version = "0.5.0"
114 114
 source = "registry+https://github.com/rust-lang/crates.io-index"
115 115
 dependencies = [
116
- "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
116
+ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
117 117
  "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
118 118
  "rtfm-syntax 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)",
119 119
  "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -131,7 +131,7 @@ dependencies = [
131 131
 name = "display-firmware"
132 132
 version = "0.0.1"
133 133
 dependencies = [
134
- "bme280 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
134
+ "bme280 0.2.1 (git+https://github.com/mgottschlag/bme280-rs?branch=destroy)",
135 135
  "cortex-m 0.6.2 (registry+https://github.com/rust-lang/crates.io-index)",
136 136
  "cortex-m-rt 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)",
137 137
  "cortex-m-rtfm 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -304,7 +304,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
304 304
 
305 305
 [[package]]
306 306
 name = "proc-macro2"
307
-version = "1.0.10"
307
+version = "1.0.12"
308 308
 source = "registry+https://github.com/rust-lang/crates.io-index"
309 309
 dependencies = [
310 310
  "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
@@ -315,7 +315,7 @@ name = "quote"
315 315
 version = "1.0.4"
316 316
 source = "registry+https://github.com/rust-lang/crates.io-index"
317 317
 dependencies = [
318
- "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
318
+ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
319 319
 ]
320 320
 
321 321
 [[package]]
@@ -339,7 +339,7 @@ version = "0.4.0"
339 339
 source = "registry+https://github.com/rust-lang/crates.io-index"
340 340
 dependencies = [
341 341
  "indexmap 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
342
- "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
342
+ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
343 343
  "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
344 344
 ]
345 345
 
@@ -374,7 +374,7 @@ name = "syn"
374 374
 version = "1.0.18"
375 375
 source = "registry+https://github.com/rust-lang/crates.io-index"
376 376
 dependencies = [
377
- "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
377
+ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
378 378
  "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
379 379
  "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)",
380 380
 ]
@@ -392,7 +392,7 @@ name = "thiserror-impl"
392 392
 version = "1.0.16"
393 393
 source = "registry+https://github.com/rust-lang/crates.io-index"
394 394
 dependencies = [
395
- "proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)",
395
+ "proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)",
396 396
  "quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
397 397
  "syn 1.0.18 (registry+https://github.com/rust-lang/crates.io-index)",
398 398
 ]
@@ -447,7 +447,7 @@ dependencies = [
447 447
 "checksum bare-metal 0.2.5 (registry+https://github.com/rust-lang/crates.io-index)" = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
448 448
 "checksum bitfield 0.13.2 (registry+https://github.com/rust-lang/crates.io-index)" = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
449 449
 "checksum bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
450
-"checksum bme280 0.2.1 (registry+https://github.com/rust-lang/crates.io-index)" = "bc93357f2a5603736e2c1f1ba8e18e9ec80efa7fdade5d6a37c11362b76363f4"
450
+"checksum bme280 0.2.1 (git+https://github.com/mgottschlag/bme280-rs?branch=destroy)" = "<none>"
451 451
 "checksum byteorder 1.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "08c48aae112d48ed9f069b33538ea9e3e90aa263cfa3d1c24309612b1f7472de"
452 452
 "checksum cc 1.0.52 (registry+https://github.com/rust-lang/crates.io-index)" = "c3d87b23d6a92cd03af510a5ade527033f6aa6fa92161e2d5863a907d4c5e31d"
453 453
 "checksum cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "81fb25b677f8bf1eb325017cb6bb8452f87969db0fedb4f757b297bee78a7c62"
@@ -475,7 +475,7 @@ dependencies = [
475 475
 "checksum nb 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "b1411551beb3c11dedfb0a90a0fa256b47d28b9ec2cdff34c25a2fa59e45dbdc"
476 476
 "checksum panic-semihosting 0.5.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c03864ac862876c16a308f5286f4aa217f1a69ac45df87ad3cd2847f818a642c"
477 477
 "checksum pkg-config 0.3.17 (registry+https://github.com/rust-lang/crates.io-index)" = "05da548ad6865900e60eaba7f589cc0783590a92e940c26953ff81ddbab2d677"
478
-"checksum proc-macro2 1.0.10 (registry+https://github.com/rust-lang/crates.io-index)" = "df246d292ff63439fea9bc8c0a270bed0e390d5ebd4db4ba15aba81111b5abe3"
478
+"checksum proc-macro2 1.0.12 (registry+https://github.com/rust-lang/crates.io-index)" = "8872cf6f48eee44265156c111456a700ab3483686b3f96df4cf5481c89157319"
479 479
 "checksum quote 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "4c1f4b0efa5fc5e8ceb705136bfee52cfdb6a4e3509f770b478cd6ed434232a7"
480 480
 "checksum r0 0.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "e2a38df5b15c8d5c7e8654189744d8e396bddc18ad48041a500ce52d6948941f"
481 481
 "checksum rgb 0.8.17 (registry+https://github.com/rust-lang/crates.io-index)" = "a85b83fd629b0ce765f45316774fa6aaa95947fd74c8e4bbf3c6d1e349701d95"

+ 1
- 1
display/firmware/Cargo.toml Visa fil

@@ -20,7 +20,7 @@ cortex-m-semihosting = "0.3.5"
20 20
 mkl25z4-hal = { git = "https://github.com/mgottschlag/mkl25z4-hal" }
21 21
 embedded-hal = "0.2.3"
22 22
 nb = "0.1.2"
23
-bme280 = "0.2.1"
23
+bme280 = { git = "https://github.com/mgottschlag/bme280-rs", branch = "destroy" }
24 24
 embedded-nrf24l01 = { git = "https://github.com/astro/embedded-nrf24l01" }
25 25
 epd-waveshare = { git = "https://github.com/mgottschlag/epd-waveshare.git", branch = "streaming" }
26 26
 tinygfx = { git = "https://github.com/mgottschlag/tinygfx" }

+ 17
- 6
display/firmware/src/main.rs Visa fil

@@ -4,6 +4,7 @@
4 4
 mod display;
5 5
 mod information;
6 6
 mod pins;
7
+mod sensors;
7 8
 
8 9
 // TODO: Enable warnings again.
9 10
 #[allow(unused)]
@@ -17,7 +18,7 @@ use panic_semihosting as _;
17 18
 
18 19
 use display::{Display, DisplayState};
19 20
 use information::{Information, TemperatureHumidity};
20
-use pins::DisplayBmeSpi;
21
+use pins::{BME280Pins, DisplayBmeSpi};
21 22
 
22 23
 #[rtfm::app(device = mkl25z4_hal::mkl25z4, peripherals = true)]
23 24
 const APP: () = {
@@ -26,6 +27,7 @@ const APP: () = {
26 27
         display_state: DisplayState,
27 28
         display: Display,
28 29
         display_bme_spi: Option<DisplayBmeSpi>,
30
+        bme_pins: Option<BME280Pins>,
29 31
         time: CopyableMonoTimer,
30 32
     }
31 33
 
@@ -73,6 +75,7 @@ const APP: () = {
73 75
             },
74 76
             display,
75 77
             display_bme_spi: Some(pins.display_bme_spi),
78
+            bme_pins: Some(pins.bme),
76 79
             time,
77 80
         }
78 81
     }
@@ -91,10 +94,18 @@ const APP: () = {
91 94
         // TODO
92 95
     }
93 96
 
94
-    #[task(priority = 1, spawn = [update_display], resources = [info])]
97
+    #[task(priority = 1, spawn = [update_display], resources = [info, bme_pins, display_bme_spi, &time])]
95 98
     fn fetch_update(mut ctx: fetch_update::Context) {
96 99
         // Fetch local updates.
97
-        // TODO
100
+        let time = ctx.resources.time.clone();
101
+        let pins = ctx.resources.bme_pins.take().unwrap();
102
+        let (pins, values) = ctx.resources.display_bme_spi.lock(|display_bme_spi| {
103
+            let spi = display_bme_spi.take().unwrap();
104
+            let (pins, spi, values) = sensors::bme280(pins, spi, time);
105
+            *display_bme_spi = Some(spi);
106
+            (pins, values)
107
+        });
108
+        *ctx.resources.bme_pins = Some(pins);
98 109
 
99 110
         // Send local measurements to the base station. This needs to be done before fetching data
100 111
         // from the base station as the base station might return the data to us.
@@ -104,10 +115,10 @@ const APP: () = {
104 115
         // TODO
105 116
         ctx.resources.info.lock(|info| {
106 117
             info.inside = Some(TemperatureHumidity {
107
-                temperature: -212,
108
-                humidity: 345,
118
+                temperature: values.temperature,
119
+                humidity: values.humidity,
109 120
             });
110
-            info.pressure = Some(100000);
121
+            info.pressure = Some(values.pressure);
111 122
         });
112 123
 
113 124
         // If the update failed, simply increment the time and place a hint that

+ 41
- 0
display/firmware/src/sensors.rs Visa fil

@@ -0,0 +1,41 @@
1
+use bme280::spi::BME280;
2
+use embedded_hal::digital::v2::OutputPin;
3
+use mkl25z4_hal::time::CopyableMonoTimer;
4
+
5
+use super::pins::{BME280Pins, DisplayBmeSpi};
6
+
7
+pub fn bme280(
8
+    mut pins: BME280Pins,
9
+    spi: DisplayBmeSpi,
10
+    delay: CopyableMonoTimer,
11
+) -> (BME280Pins, DisplayBmeSpi, BME280Data) {
12
+    // Enable power.
13
+    pins.pwr.set_low().ok();
14
+
15
+    // Read the sensor.
16
+    let mut bme = BME280::new(spi, pins.cs, delay).unwrap();
17
+    bme.init().unwrap();
18
+    let values = bme.measure().unwrap();
19
+    let (spi, cs, _) = bme.destroy();
20
+
21
+    // Disable power again.
22
+    pins.pwr.set_high().ok();
23
+    (
24
+        BME280Pins {
25
+            pwr: pins.pwr,
26
+            cs: cs,
27
+        },
28
+        spi,
29
+        BME280Data {
30
+            temperature: (values.temperature * 10.0) as i32,
31
+            humidity: (values.humidity * 10.0) as u32,
32
+            pressure: values.pressure as u32,
33
+        },
34
+    )
35
+}
36
+
37
+pub struct BME280Data {
38
+    pub temperature: i32,
39
+    pub humidity: u32,
40
+    pub pressure: u32,
41
+}

Loading…
Avbryt
Spara