Browse Source

base-station: Move radio code into module.

Mathias Gottschlag 5 years ago
parent
commit
bdb2e5fac6

+ 160
- 0
base-station/software/Cargo.lock View File

1
 # This file is automatically @generated by Cargo.
1
 # This file is automatically @generated by Cargo.
2
 # It is not intended for manual editing.
2
 # It is not intended for manual editing.
3
+[[package]]
4
+name = "aho-corasick"
5
+version = "0.7.10"
6
+source = "registry+https://github.com/rust-lang/crates.io-index"
7
+checksum = "8716408b8bc624ed7f65d223ddb9ac2d044c0547b6fa4b0d554f3a9540496ada"
8
+dependencies = [
9
+ "memchr",
10
+]
11
+
12
+[[package]]
13
+name = "atty"
14
+version = "0.2.14"
15
+source = "registry+https://github.com/rust-lang/crates.io-index"
16
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
17
+dependencies = [
18
+ "hermit-abi",
19
+ "libc",
20
+ "winapi",
21
+]
22
+
3
 [[package]]
23
 [[package]]
4
 name = "base-station"
24
 name = "base-station"
5
 version = "0.1.0"
25
 version = "0.1.0"
6
 dependencies = [
26
 dependencies = [
7
  "embedded-hal",
27
  "embedded-hal",
8
  "embedded-nrf24l01",
28
  "embedded-nrf24l01",
29
+ "env_logger",
9
  "linux-embedded-hal",
30
  "linux-embedded-hal",
31
+ "log",
32
+ "mqtt-protocol",
10
 ]
33
 ]
11
 
34
 
12
 [[package]]
35
 [[package]]
67
  "embedded-hal",
90
  "embedded-hal",
68
 ]
91
 ]
69
 
92
 
93
+[[package]]
94
+name = "env_logger"
95
+version = "0.7.1"
96
+source = "registry+https://github.com/rust-lang/crates.io-index"
97
+checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
98
+dependencies = [
99
+ "atty",
100
+ "humantime",
101
+ "log",
102
+ "regex",
103
+ "termcolor",
104
+]
105
+
106
+[[package]]
107
+name = "hermit-abi"
108
+version = "0.1.11"
109
+source = "registry+https://github.com/rust-lang/crates.io-index"
110
+checksum = "8a0d737e0f947a1864e93d33fdef4af8445a00d1ed8dc0c8ddb73139ea6abf15"
111
+dependencies = [
112
+ "libc",
113
+]
114
+
115
+[[package]]
116
+name = "humantime"
117
+version = "1.3.0"
118
+source = "registry+https://github.com/rust-lang/crates.io-index"
119
+checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
120
+dependencies = [
121
+ "quick-error",
122
+]
123
+
70
 [[package]]
124
 [[package]]
71
 name = "i2cdev"
125
 name = "i2cdev"
72
 version = "0.4.4"
126
 version = "0.4.4"
88
  "libc",
142
  "libc",
89
 ]
143
 ]
90
 
144
 
145
+[[package]]
146
+name = "lazy_static"
147
+version = "1.4.0"
148
+source = "registry+https://github.com/rust-lang/crates.io-index"
149
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
150
+
91
 [[package]]
151
 [[package]]
92
 name = "libc"
152
 name = "libc"
93
 version = "0.2.68"
153
 version = "0.2.68"
110
  "sysfs_gpio",
170
  "sysfs_gpio",
111
 ]
171
 ]
112
 
172
 
173
+[[package]]
174
+name = "log"
175
+version = "0.4.8"
176
+source = "registry+https://github.com/rust-lang/crates.io-index"
177
+checksum = "14b6052be84e6b71ab17edffc2eeabf5c2c3ae1fdb464aae35ac50c67a44e1f7"
178
+dependencies = [
179
+ "cfg-if",
180
+]
181
+
182
+[[package]]
183
+name = "memchr"
184
+version = "2.3.3"
185
+source = "registry+https://github.com/rust-lang/crates.io-index"
186
+checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
187
+
188
+[[package]]
189
+name = "mqtt-protocol"
190
+version = "0.8.1"
191
+source = "registry+https://github.com/rust-lang/crates.io-index"
192
+checksum = "0ad527bd1d7917664e1f35f30ac9d0a01fa1425c52c66494a5b875e8b8848058"
193
+dependencies = [
194
+ "byteorder",
195
+ "lazy_static",
196
+ "log",
197
+ "regex",
198
+]
199
+
113
 [[package]]
200
 [[package]]
114
 name = "nb"
201
 name = "nb"
115
 version = "0.1.2"
202
 version = "0.1.2"
129
  "void",
216
  "void",
130
 ]
217
 ]
131
 
218
 
219
+[[package]]
220
+name = "quick-error"
221
+version = "1.2.3"
222
+source = "registry+https://github.com/rust-lang/crates.io-index"
223
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
224
+
225
+[[package]]
226
+name = "regex"
227
+version = "1.3.7"
228
+source = "registry+https://github.com/rust-lang/crates.io-index"
229
+checksum = "a6020f034922e3194c711b82a627453881bc4682166cabb07134a10c26ba7692"
230
+dependencies = [
231
+ "aho-corasick",
232
+ "memchr",
233
+ "regex-syntax",
234
+ "thread_local",
235
+]
236
+
237
+[[package]]
238
+name = "regex-syntax"
239
+version = "0.6.17"
240
+source = "registry+https://github.com/rust-lang/crates.io-index"
241
+checksum = "7fe5bd57d1d7414c6b5ed48563a2c855d995ff777729dcd91c369ec7fea395ae"
242
+
132
 [[package]]
243
 [[package]]
133
 name = "rustc_version"
244
 name = "rustc_version"
134
 version = "0.2.3"
245
 version = "0.2.3"
194
  "nix",
305
  "nix",
195
 ]
306
 ]
196
 
307
 
308
+[[package]]
309
+name = "termcolor"
310
+version = "1.1.0"
311
+source = "registry+https://github.com/rust-lang/crates.io-index"
312
+checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
313
+dependencies = [
314
+ "winapi-util",
315
+]
316
+
197
 [[package]]
317
 [[package]]
198
 name = "termios"
318
 name = "termios"
199
 version = "0.2.2"
319
 version = "0.2.2"
203
  "libc",
323
  "libc",
204
 ]
324
 ]
205
 
325
 
326
+[[package]]
327
+name = "thread_local"
328
+version = "1.0.1"
329
+source = "registry+https://github.com/rust-lang/crates.io-index"
330
+checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
331
+dependencies = [
332
+ "lazy_static",
333
+]
334
+
206
 [[package]]
335
 [[package]]
207
 name = "void"
336
 name = "void"
208
 version = "1.0.2"
337
 version = "1.0.2"
209
 source = "registry+https://github.com/rust-lang/crates.io-index"
338
 source = "registry+https://github.com/rust-lang/crates.io-index"
210
 checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
339
 checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
340
+
341
+[[package]]
342
+name = "winapi"
343
+version = "0.3.8"
344
+source = "registry+https://github.com/rust-lang/crates.io-index"
345
+checksum = "8093091eeb260906a183e6ae1abdba2ef5ef2257a21801128899c3fc699229c6"
346
+dependencies = [
347
+ "winapi-i686-pc-windows-gnu",
348
+ "winapi-x86_64-pc-windows-gnu",
349
+]
350
+
351
+[[package]]
352
+name = "winapi-i686-pc-windows-gnu"
353
+version = "0.4.0"
354
+source = "registry+https://github.com/rust-lang/crates.io-index"
355
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
356
+
357
+[[package]]
358
+name = "winapi-util"
359
+version = "0.1.4"
360
+source = "registry+https://github.com/rust-lang/crates.io-index"
361
+checksum = "fa515c5163a99cc82bab70fd3bfdd36d827be85de63737b40fcef2ce084a436e"
362
+dependencies = [
363
+ "winapi",
364
+]
365
+
366
+[[package]]
367
+name = "winapi-x86_64-pc-windows-gnu"
368
+version = "0.4.0"
369
+source = "registry+https://github.com/rust-lang/crates.io-index"
370
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"

+ 3
- 0
base-station/software/Cargo.toml View File

10
 linux-embedded-hal = "0.3"
10
 linux-embedded-hal = "0.3"
11
 embedded-nrf24l01 = { git = "https://github.com/astro/embedded-nrf24l01.git" }
11
 embedded-nrf24l01 = { git = "https://github.com/astro/embedded-nrf24l01.git" }
12
 embedded-hal = "0.2.3"
12
 embedded-hal = "0.2.3"
13
+mqtt-protocol = "0.8"
14
+env_logger = "0.7"
15
+log = "0.4"

+ 12
- 81
base-station/software/src/main.rs View File

1
-use embedded_nrf24l01::{Configuration, CrcMode, DataRate, NRF24L01};
2
-use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
3
-use linux_embedded_hal::sysfs_gpio::Direction;
4
-use linux_embedded_hal::Pin;
5
-use std::convert::TryInto;
6
-use std::time::Instant;
7
-
8
-use spi::EmbeddedHalSpidev;
1
+use std::env;
2
+use std::thread;
3
+use std::time::Duration;
9
 
4
 
5
+mod radio;
10
 mod spi;
6
 mod spi;
11
 
7
 
12
 fn main() {
8
 fn main() {
13
-    // The NRF module is connected as follows:
14
-    // - CE: PA1
15
-    // - CS: PG8
16
-    // - IRQ: PG9
17
-    // - MOSI: PC0
18
-    // - MISO: PC1
19
-    // - SCK: PC2
20
-
21
-    // Configure SPI.
22
-    let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
23
-    let options = SpidevOptions::new()
24
-        .bits_per_word(8)
25
-        .max_speed_hz(500_000)
26
-        .mode(SpiModeFlags::SPI_MODE_0)
27
-        .build();
28
-    spi.configure(&options).unwrap();
29
-    let spi = EmbeddedHalSpidev::from(spi);
30
-
31
-    // Configure the GPIOs.
32
-    let ce_nr = get_pin_number('A', 1);
33
-    let ce = Pin::new(ce_nr);
34
-    ce.export().unwrap();
35
-    ce.set_direction(Direction::Out).unwrap();
36
-    let cs_nr = get_pin_number('G', 8);
37
-    let cs = Pin::new(cs_nr);
38
-    cs.export().unwrap();
39
-    cs.set_direction(Direction::Out).unwrap();
40
-    let irq_nr = get_pin_number('G', 9);
41
-    let irq = Pin::new(irq_nr);
42
-    irq.export().unwrap();
43
-    irq.set_direction(Direction::In).unwrap();
44
-
45
-    // Initialize the radio module.
46
-    let mut nrf24 = NRF24L01::new(ce, cs, spi).unwrap();
47
-    nrf24.set_frequency(0x32).unwrap();
48
-    nrf24.set_rf(DataRate::R2Mbps, 3).unwrap();
49
-    nrf24.set_crc(Some(CrcMode::OneByte)).unwrap();
50
-    nrf24.set_auto_retransmit(250, 3).unwrap();
51
-    nrf24
52
-        .set_pipes_rx_enable(&[true, false, false, false, false, false])
53
-        .unwrap();
54
-    nrf24
55
-        .set_rx_addr(0, &[0x56, 0x34, 0x12, 0x00, 0x00])
56
-        .unwrap();
57
-    nrf24.flush_rx().unwrap();
58
-    nrf24.flush_tx().unwrap();
59
-    //nrf24.set_auto_ack(&[true; 6]).unwrap();
60
-    println!("auto ack: {:?}", nrf24.get_auto_ack().unwrap());
61
-    nrf24.set_pipes_rx_lengths(&[None; 6]).unwrap();
9
+    env::set_var(
10
+        "RUST_LOG",
11
+        env::var_os("RUST_LOG").unwrap_or_else(|| "info".into()),
12
+    );
13
+    env_logger::init();
62
 
14
 
63
-    println!("width: {}", nrf24.get_address_width().unwrap());
15
+    radio::start();
64
 
16
 
65
-    let mut start = Instant::now();
66
-
67
-    // Receive data.
68
-    let mut nrf24 = nrf24.rx().unwrap();
69
-    println!("Starting to receive:");
17
+    // TODO
70
     loop {
18
     loop {
71
-        if let Some(pipe) = nrf24.can_read().unwrap() {
72
-            let payload = nrf24.read().unwrap();
73
-            println!("packet received on pipe {}: {:?}", pipe, payload.as_ref());
74
-            let pressure = u32::from_le_bytes(payload.as_ref()[0..4].try_into().unwrap());
75
-            let temperature = u32::from_le_bytes(payload.as_ref()[4..8].try_into().unwrap());
76
-            let humidity = u32::from_le_bytes(payload.as_ref()[8..12].try_into().unwrap());
77
-            println!("pressure: {}", pressure as f32 / 100.0);
78
-            println!("temperature: {}", temperature as f32 / 100.0);
79
-            println!("humidity: {}", humidity as f32 / 1024.0);
80
-            let end = Instant::now();
81
-            let elapsed = end.duration_since(start);
82
-            println!("Debug: {:?}", elapsed);
83
-            start = end;
84
-        }
19
+        thread::sleep(Duration::from_secs(10));
85
     }
20
     }
86
 }
21
 }
87
-
88
-fn get_pin_number(c: char, n: u64) -> u64 {
89
-    (c as u64 - 'A' as u64) * 32 + n
90
-}

+ 110
- 0
base-station/software/src/radio.rs View File

1
+use std::convert::TryInto;
2
+use std::io;
3
+use std::thread;
4
+use std::time::{Duration, Instant};
5
+
6
+use embedded_nrf24l01::{Configuration, CrcMode, DataRate, NRF24L01};
7
+use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
8
+use linux_embedded_hal::sysfs_gpio::Direction;
9
+use linux_embedded_hal::Pin;
10
+use log::{error, info};
11
+
12
+use crate::spi::EmbeddedHalSpidev;
13
+
14
+pub fn start() {
15
+    // TODO: Channels for sensor readings.
16
+    thread::spawn(move || {
17
+        // Whenever a error occurs, reinitialize the radio module shortly later.
18
+        loop {
19
+            match radio_thread() {
20
+                Ok(()) => {}
21
+                Err(e) => {
22
+                    error!("Radio error: {:?}", e);
23
+                }
24
+            }
25
+            thread::sleep(Duration::from_secs(3));
26
+        }
27
+    });
28
+}
29
+
30
+fn radio_thread() -> Result<(), io::Error> {
31
+    info!("Initializing radio...");
32
+
33
+    // The NRF module is connected as follows:
34
+    // - CE: PA1
35
+    // - CS: PG8
36
+    // - IRQ: PG9
37
+    // - MOSI: PC0
38
+    // - MISO: PC1
39
+    // - SCK: PC2
40
+
41
+    // Configure SPI.
42
+    let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
43
+    let options = SpidevOptions::new()
44
+        .bits_per_word(8)
45
+        .max_speed_hz(500_000)
46
+        .mode(SpiModeFlags::SPI_MODE_0)
47
+        .build();
48
+    spi.configure(&options).unwrap();
49
+    let spi = EmbeddedHalSpidev::from(spi);
50
+
51
+    // Configure the GPIOs.
52
+    let ce_nr = get_pin_number('A', 1);
53
+    let ce = Pin::new(ce_nr);
54
+    ce.export().unwrap();
55
+    ce.set_direction(Direction::Out).unwrap();
56
+    let cs_nr = get_pin_number('G', 8);
57
+    let cs = Pin::new(cs_nr);
58
+    cs.export().unwrap();
59
+    cs.set_direction(Direction::Out).unwrap();
60
+    let irq_nr = get_pin_number('G', 9);
61
+    let irq = Pin::new(irq_nr);
62
+    irq.export().unwrap();
63
+    irq.set_direction(Direction::In).unwrap();
64
+
65
+    // Initialize the radio module.
66
+    let mut nrf24 = NRF24L01::new(ce, cs, spi).unwrap();
67
+    nrf24.set_frequency(0x32).unwrap();
68
+    nrf24.set_rf(DataRate::R2Mbps, 3).unwrap();
69
+    nrf24.set_crc(Some(CrcMode::OneByte)).unwrap();
70
+    nrf24.set_auto_retransmit(250, 3).unwrap();
71
+    nrf24
72
+        .set_pipes_rx_enable(&[true, false, false, false, false, false])
73
+        .unwrap();
74
+    nrf24
75
+        .set_rx_addr(0, &[0x56, 0x34, 0x12, 0x00, 0x00])
76
+        .unwrap();
77
+    nrf24.flush_rx().unwrap();
78
+    nrf24.flush_tx().unwrap();
79
+    //nrf24.set_auto_ack(&[true; 6]).unwrap();
80
+    info!("auto ack: {:?}", nrf24.get_auto_ack().unwrap());
81
+    nrf24.set_pipes_rx_lengths(&[None; 6]).unwrap();
82
+
83
+    info!("width: {}", nrf24.get_address_width().unwrap());
84
+
85
+    let mut start = Instant::now();
86
+
87
+    // Receive data.
88
+    let mut nrf24 = nrf24.rx().unwrap();
89
+    info!("Starting to receive:");
90
+    loop {
91
+        if let Some(pipe) = nrf24.can_read().unwrap() {
92
+            let payload = nrf24.read().unwrap();
93
+            info!("packet received on pipe {}: {:?}", pipe, payload.as_ref());
94
+            let pressure = u32::from_le_bytes(payload.as_ref()[0..4].try_into().unwrap());
95
+            let temperature = u32::from_le_bytes(payload.as_ref()[4..8].try_into().unwrap());
96
+            let humidity = u32::from_le_bytes(payload.as_ref()[8..12].try_into().unwrap());
97
+            info!("pressure: {}", pressure as f32 / 100.0);
98
+            info!("temperature: {}", temperature as f32 / 100.0);
99
+            info!("humidity: {}", humidity as f32 / 1024.0);
100
+            let end = Instant::now();
101
+            let elapsed = end.duration_since(start);
102
+            info!("Debug: {:?}", elapsed);
103
+            start = end;
104
+        }
105
+    }
106
+}
107
+
108
+fn get_pin_number(c: char, n: u64) -> u64 {
109
+    (c as u64 - 'A' as u64) * 32 + n
110
+}

Loading…
Cancel
Save