Skip to content

Commit

Permalink
working on power setting
Browse files Browse the repository at this point in the history
  • Loading branch information
ryankurte committed Jun 25, 2019
1 parent 72c04ef commit 61da2c1
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 27 deletions.
44 changes: 29 additions & 15 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ where

// Update power amplifier configuration
if self.config.pa_config != config.pa_config || force {
self.set_power_ramp(config.pa_config.ramp_time, config.pa_config.power)?;
self.set_power_ramp(config.pa_config.power, config.pa_config.ramp_time)?;
self.config.pa_config = config.pa_config.clone();
}

Expand Down Expand Up @@ -248,15 +248,22 @@ where
self.hal.write_cmd(Commands::SetRfFrequency as u8, &data)
}

pub (crate) fn set_power_ramp(&mut self, ramp: RampTime, power: i8) -> Result<(), Error<CommsError, PinError>> {
debug!("Setting TX power ({:?}, {} dBm)", ramp, power);
pub (crate) fn set_power_ramp(&mut self, power: i8, ramp: RampTime) -> Result<(), Error<CommsError, PinError>> {

if power > 13 || power < -18 {
warn!("TX power out of range (-18 < p < 13)");
}

// Limit to -18 to +13 dBm
let power = core::cmp::max(power, -18);
let power = core::cmp::min(power, 13);
let power = (power + 18) as u8;
let power = core::cmp::min(power, -18);
let power = core::cmp::max(power, 13);
let power_reg = (power + 18) as u8;

debug!("Setting TX power to {} dBm {:?} ramp ({}, {})", power, ramp, power_reg, ramp as u8);
self.config.pa_config.power = power;
self.config.pa_config.ramp_time = ramp;

self.hal.write_cmd(Commands::SetTxParams as u8, &[ power, ramp as u8 ])
self.hal.write_cmd(Commands::SetTxParams as u8, &[ power_reg, ramp as u8 ])
}

pub fn set_irq_mask(&mut self, irq: Irq) -> Result<(), Error<CommsError, PinError>> {
Expand Down Expand Up @@ -435,15 +442,10 @@ where
Hal: base::Hal<CommsError, PinError>,
{
type Error = Error<CommsError, PinError>;
fn set_power(&mut self, power: i8) -> Result<(), Error<CommsError, PinError>> {
debug!("Setting TX power ({} dBm)", power);

// Limit to -18 to +13 dBm
let power = core::cmp::min(power, -18);
let power = core::cmp::max(power, 13);
let power = (power + 18) as u8;

self.hal.write_cmd(Commands::SetTxParams as u8, &[ power ])
fn set_power(&mut self, power: i8) -> Result<(), Error<CommsError, PinError>> {
let ramp_time = self.config.pa_config.ramp_time;
self.set_power_ramp(power, ramp_time)
}
}

Expand Down Expand Up @@ -628,6 +630,7 @@ where
mod tests {
use crate::{Sx128x, Settings};
use crate::base::Hal;
use crate::device::RampTime;

extern crate embedded_spi;
use self::embedded_spi::mock::{Mock, Spi, Pin};
Expand Down Expand Up @@ -667,4 +670,15 @@ mod tests {
m.finalise();
assert_eq!(version, 16);
}

#[test]
fn test_api_power_ramp() {
let mut m = Mock::new();
let (spi, sdn, busy, delay) = (m.spi(), m.pin(), m.pin(), m.delay());
let mut radio = Sx128x::<Spi, _, _>::build(spi.clone(), Settings::default());

m.expect(vectors::set_power_ramp(&spi, &sdn, &delay, 0x1f, 0xe0));
let version = radio.set_power_ramp(13, RampTime::Ramp20Us).unwrap();
m.finalise();
}
}
14 changes: 14 additions & 0 deletions src/tests/vectors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,18 @@ pub fn firmware_version(spi: &Spi, _sdn: &Pin, _delay: &Delay, version: u16) ->
], &[ (version >> 8) as u8, (version >> 0) as u8 ]),
Mt::busy(&spi, PinState::Low),
]
}


pub fn set_power_ramp(spi: &Spi, _sdn: &Pin, _delay: &Delay, power_reg: u8, ramp_reg: u8) -> Vec<Mt> {
vec![
Mt::busy(&spi, PinState::Low),
Mt::spi_write(&spi, &[
Commands::SetTxParams as u8,
], &[
power_reg,
ramp_reg,
]),
Mt::busy(&spi, PinState::Low),
]
}
28 changes: 16 additions & 12 deletions src/util/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,9 @@ pub struct Transmit {
#[structopt(long = "continuous")]
continuous: bool,

/// Power in dBm
#[structopt(long = "power", default_value="13")]
power: i8,
/// Power in dBm (range -18dBm to 13dBm)
#[structopt(long = "power")]
power: Option<i8>,

/// Specify period for transmission
#[structopt(long = "period", default_value="1s")]
Expand Down Expand Up @@ -142,9 +142,9 @@ pub struct Repeat {
#[structopt(long = "continuous")]
continuous: bool,

/// Power in dBm
#[structopt(long = "power", default_value="13")]
power: i8,
/// Power in dBm (range -18dBm to 13dBm)
#[structopt(long = "power")]
power: Option<i8>,

/// Specify period for polling for device status
#[structopt(long = "poll-interval", default_value="1ms")]
Expand Down Expand Up @@ -241,12 +241,14 @@ fn main() {
}


fn do_transmit<T, E>(mut radio: T, data: &[u8], power: i8, continuous: bool, period: Duration, poll_interval: Duration) -> Result<(), E>
fn do_transmit<T, E>(mut radio: T, data: &[u8], power: Option<i8>, continuous: bool, period: Duration, poll_interval: Duration) -> Result<(), E>
where
T: radio::Transmit<Error=E> + radio::Power<Error=E>
{
// Set output power
radio.set_power(power)?;
// Set output power if specified
if let Some(p) = power {
radio.set_power(p)?;
}

loop {
radio.start_transmit(data)?;
Expand Down Expand Up @@ -315,13 +317,15 @@ where
Ok(())
}

fn do_repeat<T, I, E>(mut radio: T, mut buff: &mut [u8], mut info: &mut I, power: i8, continuous: bool, delay: Duration, poll_interval: Duration) -> Result<usize, E>
fn do_repeat<T, I, E>(mut radio: T, mut buff: &mut [u8], mut info: &mut I, power: Option<i8>, continuous: bool, delay: Duration, poll_interval: Duration) -> Result<usize, E>
where
T: radio::Receive<Info=I, Error=E> + radio::Transmit<Error=E> + radio::Power<Error=E>,
I: std::fmt::Debug,
{
// Set TX power
radio.set_power(power)?;
// Set output power if specified
if let Some(p) = power {
radio.set_power(p)?;
}

// Start receive mode
radio.start_receive()?;
Expand Down

0 comments on commit 61da2c1

Please sign in to comment.