Pārlūkot izejas kodu

base-station: Power-cycle module until it responds.

Mathias Gottschlag 5 gadus atpakaļ
vecāks
revīzija
f5e23486a9
1 mainītis faili ar 40 papildinājumiem un 13 dzēšanām
  1. 40
    13
      base-station/software/src/radio.rs

+ 40
- 13
base-station/software/src/radio.rs Parādīt failu

@@ -4,6 +4,8 @@ use std::thread;
4 4
 use std::thread::sleep;
5 5
 use std::time::{Duration, Instant};
6 6
 
7
+use embedded_hal::blocking::spi::Transfer;
8
+use embedded_hal::digital::v2::OutputPin;
7 9
 use embedded_nrf24l01::{Configuration, CrcMode, DataRate, RxMode, NRF24L01};
8 10
 use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
9 11
 use linux_embedded_hal::sysfs_gpio::Direction;
@@ -62,31 +64,55 @@ impl<'a> Radio<'a> {
62 64
         // - MOSI: PC0
63 65
         // - MISO: PC1
64 66
         // - SCK: PC2
65
-
66
-        // Configure SPI.
67
-        let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
68
-        let options = SpidevOptions::new()
69
-            .bits_per_word(8)
70
-            .max_speed_hz(500_000)
71
-            .mode(SpiModeFlags::SPI_MODE_0)
72
-            .build();
73
-        spi.configure(&options).unwrap();
74
-        let spi = EmbeddedHalSpidev::from(spi);
67
+        // - Power (active low): PG11
75 68
 
76 69
         // Configure the GPIOs.
70
+        let pwr_nr = get_pin_number('G', 11);
71
+        let mut pwr = Pin::new(pwr_nr);
72
+        pwr.export().unwrap();
73
+        pwr.set_direction(Direction::Out).unwrap();
74
+
77 75
         let ce_nr = get_pin_number('A', 1);
78
-        let ce = Pin::new(ce_nr);
76
+        let mut ce = Pin::new(ce_nr);
79 77
         ce.export().unwrap();
80 78
         ce.set_direction(Direction::Out).unwrap();
79
+        ce.set_high().unwrap();
81 80
         let cs_nr = get_pin_number('G', 8);
82
-        let cs = Pin::new(cs_nr);
81
+        let mut cs = Pin::new(cs_nr);
83 82
         cs.export().unwrap();
84 83
         cs.set_direction(Direction::Out).unwrap();
84
+        cs.set_high().unwrap();
85 85
         let irq_nr = get_pin_number('G', 9);
86 86
         let irq = Pin::new(irq_nr);
87 87
         irq.export().unwrap();
88 88
         irq.set_direction(Direction::In).unwrap();
89 89
 
90
+        // Configure SPI.
91
+        let mut spi = Spidev::open("/dev/spidev0.0").unwrap();
92
+        let options = SpidevOptions::new()
93
+            .bits_per_word(8)
94
+            .max_speed_hz(500_000)
95
+            .mode(SpiModeFlags::SPI_MODE_0)
96
+            .build();
97
+        spi.configure(&options).unwrap();
98
+        let mut spi = EmbeddedHalSpidev::from(spi);
99
+
100
+        // HACK: Cycle power until transfers to the module seem to work.
101
+        loop {
102
+            pwr.set_high().unwrap();
103
+            sleep(Duration::from_millis(10));
104
+            pwr.set_low().unwrap();
105
+            sleep(Duration::from_millis(10));
106
+            cs.set_low().unwrap();
107
+            let mut read_aw = [0x03, 0x00];
108
+            if spi.transfer(&mut read_aw).is_ok() {
109
+                // Correct address field width?
110
+                if read_aw[1] >= 0x1 && read_aw[1] <= 0x3 {
111
+                    break;
112
+                }
113
+            }
114
+        }
115
+
90 116
         // Initialize the radio module.
91 117
         let mut nrf24 = NRF24L01::new(ce, cs, spi)?;
92 118
         nrf24.set_frequency(0x32)?;
@@ -248,13 +274,13 @@ impl<'a> Radio<'a> {
248 274
             .map_err(|(_, e)| Error::Radio(e))?;
249 275
         let mut encoded = [0u8; 32];
250 276
         let key = get_key(device_id);
277
+        // TODO: Check whether the packet arrived.
251 278
         if packet.encode_and_encrypt(key.unwrap(), salt, &mut encoded) {
252 279
             tx.set_tx_addr(&[0xB3, 0xB3, 0xB3, 0xB3, device_id])
253 280
                 .unwrap();
254 281
             tx.set_rx_addr(0, &[0xB3, 0xB3, 0xB3, 0xB3, device_id])
255 282
                 .unwrap();
256 283
             tx.send(&encoded).unwrap();
257
-        // TODO: Check whether the packet arrived.
258 284
         } else {
259 285
             info!("could not encode packet {:?}", packet);
260 286
         }
@@ -264,6 +290,7 @@ impl<'a> Radio<'a> {
264 290
                 .rx()
265 291
                 .map_err(|(_, e)| Error::Radio(e))?,
266 292
         );
293
+        info!("packet sent: {:?}", packet);
267 294
         Ok(())
268 295
     }
269 296
 }

Notiek ielāde…
Atcelt
Saglabāt