Browse Source

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

Mathias Gottschlag 5 years ago
parent
commit
f5e23486a9
1 changed files with 40 additions and 13 deletions
  1. 40
    13
      base-station/software/src/radio.rs

+ 40
- 13
base-station/software/src/radio.rs View File

4
 use std::thread::sleep;
4
 use std::thread::sleep;
5
 use std::time::{Duration, Instant};
5
 use std::time::{Duration, Instant};
6
 
6
 
7
+use embedded_hal::blocking::spi::Transfer;
8
+use embedded_hal::digital::v2::OutputPin;
7
 use embedded_nrf24l01::{Configuration, CrcMode, DataRate, RxMode, NRF24L01};
9
 use embedded_nrf24l01::{Configuration, CrcMode, DataRate, RxMode, NRF24L01};
8
 use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
10
 use linux_embedded_hal::spidev::{SpiModeFlags, Spidev, SpidevOptions};
9
 use linux_embedded_hal::sysfs_gpio::Direction;
11
 use linux_embedded_hal::sysfs_gpio::Direction;
62
         // - MOSI: PC0
64
         // - MOSI: PC0
63
         // - MISO: PC1
65
         // - MISO: PC1
64
         // - SCK: PC2
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
         // Configure the GPIOs.
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
         let ce_nr = get_pin_number('A', 1);
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
         ce.export().unwrap();
77
         ce.export().unwrap();
80
         ce.set_direction(Direction::Out).unwrap();
78
         ce.set_direction(Direction::Out).unwrap();
79
+        ce.set_high().unwrap();
81
         let cs_nr = get_pin_number('G', 8);
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
         cs.export().unwrap();
82
         cs.export().unwrap();
84
         cs.set_direction(Direction::Out).unwrap();
83
         cs.set_direction(Direction::Out).unwrap();
84
+        cs.set_high().unwrap();
85
         let irq_nr = get_pin_number('G', 9);
85
         let irq_nr = get_pin_number('G', 9);
86
         let irq = Pin::new(irq_nr);
86
         let irq = Pin::new(irq_nr);
87
         irq.export().unwrap();
87
         irq.export().unwrap();
88
         irq.set_direction(Direction::In).unwrap();
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
         // Initialize the radio module.
116
         // Initialize the radio module.
91
         let mut nrf24 = NRF24L01::new(ce, cs, spi)?;
117
         let mut nrf24 = NRF24L01::new(ce, cs, spi)?;
92
         nrf24.set_frequency(0x32)?;
118
         nrf24.set_frequency(0x32)?;
248
             .map_err(|(_, e)| Error::Radio(e))?;
274
             .map_err(|(_, e)| Error::Radio(e))?;
249
         let mut encoded = [0u8; 32];
275
         let mut encoded = [0u8; 32];
250
         let key = get_key(device_id);
276
         let key = get_key(device_id);
277
+        // TODO: Check whether the packet arrived.
251
         if packet.encode_and_encrypt(key.unwrap(), salt, &mut encoded) {
278
         if packet.encode_and_encrypt(key.unwrap(), salt, &mut encoded) {
252
             tx.set_tx_addr(&[0xB3, 0xB3, 0xB3, 0xB3, device_id])
279
             tx.set_tx_addr(&[0xB3, 0xB3, 0xB3, 0xB3, device_id])
253
                 .unwrap();
280
                 .unwrap();
254
             tx.set_rx_addr(0, &[0xB3, 0xB3, 0xB3, 0xB3, device_id])
281
             tx.set_rx_addr(0, &[0xB3, 0xB3, 0xB3, 0xB3, device_id])
255
                 .unwrap();
282
                 .unwrap();
256
             tx.send(&encoded).unwrap();
283
             tx.send(&encoded).unwrap();
257
-        // TODO: Check whether the packet arrived.
258
         } else {
284
         } else {
259
             info!("could not encode packet {:?}", packet);
285
             info!("could not encode packet {:?}", packet);
260
         }
286
         }
264
                 .rx()
290
                 .rx()
265
                 .map_err(|(_, e)| Error::Radio(e))?,
291
                 .map_err(|(_, e)| Error::Radio(e))?,
266
         );
292
         );
293
+        info!("packet sent: {:?}", packet);
267
         Ok(())
294
         Ok(())
268
     }
295
     }
269
 }
296
 }

Loading…
Cancel
Save