Skip to content

Commit

Permalink
Re-organized setups by hal rather than by peripheral device.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdgilbert committed Apr 13, 2024
1 parent 9af2626 commit 461c946
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 685 deletions.
13 changes: 7 additions & 6 deletions examples/rtic/gps_rw_rtic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,8 @@ mod app {
//use nb::block;
//use rtt_target::{rprintln, rtt_init_print};

use rust_integration_testing_of_examples::usart;
use rust_integration_testing_of_examples::usart::{Tx1Type, Rx2Type};
use rust_integration_testing_of_examples::setup::{MONOCLOCK, Peripherals};
use rust_integration_testing_of_examples::setup;
use rust_integration_testing_of_examples::setup::{MONOCLOCK, Tx1Type, Rx2Type};

use embedded_io::{Read, Write};

Expand Down Expand Up @@ -117,9 +116,11 @@ mod app {
let mut buffer: heapless::Vec<u8, 80> = heapless::Vec::new();
hprintln!("buffer at {} of {}", buffer.len(), buffer.capacity()).unwrap(); //0 of 80
buffer.clear();

let (mut tx_con, mut _rx_con) = usart::setup_1_from_dp(Peripherals::take().unwrap()); // console
let (mut _tx_gps, rx_gps) = usart::setup_2_from_dp(Peripherals::take().unwrap()); // GPS

// transmit to console, receive from gps
//let (mut tx_con, mut _rx_con) = usart::setup_1_from_dp(Peripherals::take().unwrap()); // console
//let (mut _tx_gps, rx_gps) = usart::setup_2_from_dp(Peripherals::take().unwrap()); // GPS
let (mut tx_con, _rx1, _tx2, rx_gps) = setup::tx1_rx1_tx2_rx2_from_dp(cx.device);

//writeln(&mut tx_con, "\r\nconsole connect check.\r\n");
tx_con.write("\r\nconsole connect check.\r\n".as_bytes()).unwrap(); // does this need block!() ?
Expand Down
9 changes: 6 additions & 3 deletions src/PinMap.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ PB11 sda2 gpiob.pb11.into_alternate_open_drain(&mut gpiob.crh),
PA9 tx1 dp.USART1, (gpioa.pa9.into_alternate_push_pull(&mut gpioa.crh),
PA10 rx1 gpioa.pa10,

PA2 tx2 dp.USART2 gpiob.pa2.into_alternate_push_pull(&mut gpiob.crh),
PA3 rx2 gpiob.pa3, // may need alt

dp.SPI1,
(
PA5 sck gpioa.pa5.into_alternate_push_pull(&mut gpioa.crl), // sck
Expand All @@ -50,7 +53,7 @@ PA1 adc1 AdcSensor1Type ch: gpioa.pa1.into_analog(&mut gpioa.crl), //c

PC13
PB6 PB7 PB8 PB9 PB10 PB11
PA0 PA1 PA5 PA6 PA7 PA8 PA9 PA10 PA11
PA0 PA1 PA2 PA3 PA5 PA6 PA7 PA8 PA9 PA10 PA11


/////////////////////////////// stm32f4xx ///////////////////////////////
Expand Down Expand Up @@ -125,7 +128,7 @@ PA6 miso gpioa.pa6.into_alternate(), // miso
PA7 mosi gpioa.pa7.into_alternate(), // mosi
), MODE_0, 400.kHz(), &mut rcc, );

PA3 CsPin Sx127x gpioa.pa3.into_push_pull_output(), //CsPin
PA11 CsPin Sx127x gpioa.pa11.into_push_pull_output(), //CsPin
PB4 BusyPin gpiob.pb8.into_floating_input(), //BusyPin DI00 CHANGED
PB5 ReadyPin gpiob.pb9.into_floating_input(), //ReadyPin DI01 CHANGED
PA0 ResetPin gpioa.pa0.into_push_pull_output(), //ResetPin
Expand All @@ -135,7 +138,7 @@ PA1 adc1 AdcSensor1Type ch: gpioa.pa1.into_analog(),

PC4, PC13
PB4, PB5, PB7, PB8, PB9
PA0, PA1, PA2, PA3, PA5, PA6, PA7, PA8, PA9, PA10
PA0, PA1, PA2, PA3, PA5, PA6, PA7, PA8, PA9, PA10, PA11



Expand Down
2 changes: 0 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ pub mod alt_delay;
pub mod led; // has trait and default impl
pub mod lora; // has structures and constants

pub mod usart;

pub mod setup;

// Note, if feature gate is not set then compiling for the module will be
Expand Down
65 changes: 39 additions & 26 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,98 +44,111 @@ pub use crate::setup_all_stm32l4xx::*;

// This enforces a common pin usage in all examples, which simplifies rewiring for hardware tests

pub fn pin_i2c1_i2c2_led_tx_rx_spi_spiext_delay_clocks_adc1_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, TxType, RxType,
pub fn pin_i2c1_i2c2_led_tx1_rx1_tx2_rx2_spi_spiext_delay_clocks_adc1_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, Tx1Type, Rx1Type, Tx2Type, Rx2Type,
SpiType, SpiExt, Delay, Clocks, AdcSensor1Type) {
let (pin, i2c1, i2c2, led, tx, rx, spi, spiext, delay, clocks, adc1) = all_from_dp(dp);
(pin, i2c1, i2c2, led, tx, rx, spi, spiext, delay, clocks, adc1)
//(pin, i2c1, i2c2, led, tx1, rx1, tx2, rx2, spi, spiext, delay, clocks, adc1) =
all_from_dp(dp)
}

pub fn tx1_rx1_tx2_rx2_from_dp(dp: Peripherals) -> (Tx1Type, Rx1Type, Tx2Type, Rx2Type) {
let (_pin, _i2c1, _i2c2, _led, tx1, rx1, tx2, rx2, _spi, _spiext, _delay, _clocks, _adc1) = all_from_dp(dp);
(tx1, rx1, tx2, rx2)
}

// ///// filter no tx2, rx2 below

pub fn pin_i2c1_led_tx1_rx1_delay_adc1_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, LedType, TxType, RxType, Delay, AdcSensor1Type) {
let (pin, i2c1, _i2c2, led, tx1, rx1, _tx2, _rx2, _spi, _spiext, delay, _clocks, adc1) = all_from_dp(dp);
(pin, i2c1, led, tx1, rx1, delay, adc1)
}

pub fn pin_i2c1_led_delay_adc1_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, LedType, Delay, AdcSensor1Type) {
let (pin, i2c1, _i2c2, led, _tx, _rx, _spi, _spiext, delay, _clocks, adc1) = all_from_dp(dp);
let (pin, i2c1, _i2c2, led, _tx1, _rx1, _tx2, _rx2, _spi, _spiext, delay, _clocks, adc1) = all_from_dp(dp);
(pin, i2c1, led, delay, adc1)
}


// ///// filter no_adc below
// ///// filter no adc or tx2, rx2 below

pub fn no_adc_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, TxType, RxType, SpiType, SpiExt, Delay, Clocks) {
let (pin, i2c1, i2c2, led, tx, rx, spi, spiext, delay, clocks, _adc1) = all_from_dp(dp);
(pin, i2c1, i2c2, led, tx, rx, spi, spiext, delay, clocks)
let (pin, i2c1, i2c2, led, tx1, rx1, _tx2, _rx2, spi, spiext, delay, clocks, _adc1) = all_from_dp(dp);
(pin, i2c1, i2c2, led, tx1, rx1, spi, spiext, delay, clocks)
}

pub fn i2c1_i2c2_led_spi_spiext_delay_from_dp(dp: Peripherals) ->
(I2c1Type, I2c2Type, LedType, SpiType, SpiExt, Delay) {
let (_pin, i2c1, i2c2, led, _tx, _rx, spi, spiext, delay, _clocks) = no_adc_from_dp(dp);
let (_pin, i2c1, i2c2, led, _tx1, _rx1, spi, spiext, delay, _clocks) = no_adc_from_dp(dp);
(i2c1, i2c2, led, spi, spiext, delay)
}

pub fn led_tx_rx_spi_spiext_delay_from_dp(dp: Peripherals) ->
(LedType, TxType, RxType, SpiType, SpiExt, Delay) {
let (_pin, _i2c1, _i2c2, led, tx, rx, spi, spiext, delay, _clocks) = no_adc_from_dp(dp);
(led, tx, rx, spi, spiext, delay)
let (_pin, _i2c1, _i2c2, led, tx1, rx1, spi, spiext, delay, _clocks) = no_adc_from_dp(dp);
(led, tx1, rx1, spi, spiext, delay)
}

pub fn led_spi_spiext_delay_from_dp(dp: Peripherals) ->
(LedType, SpiType, SpiExt, Delay) {
let (_pin, _i2c1, _i2c2, led, _tx, _rx, spi, spiext, delay, _clocks) = no_adc_from_dp(dp);
let (_pin, _i2c1, _i2c2, led, _tx1, _rx1, spi, spiext, delay, _clocks) = no_adc_from_dp(dp);
(led, spi, spiext, delay)
}

pub fn pin_i2c1_i2c2_led_tx_delay_clocks_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, TxType, Delay, Clocks) {
let (pin, i2c1, i2c2, led, tx, _rx, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(pin, i2c1, i2c2, led, tx, delay, clocks)
let (pin, i2c1, i2c2, led, tx1, _rx1, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(pin, i2c1, i2c2, led, tx1, delay, clocks)
}


pub fn i2c1_i2c2_led_delay_clocks_from_dp(dp: Peripherals) -> (I2c1Type, I2c2Type, LedType, Delay, Clocks) {
let (_pin, i2c1, i2c2, led, _tx, _rx, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
let (_pin, i2c1, i2c2, led, _tx1, _rx1, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(i2c1, i2c2, led, delay, clocks)
}

pub fn i2c1_i2c2_delay_clocks_from_dp(dp: Peripherals) -> (I2c1Type, I2c2Type, Delay, Clocks) {
let (_pin, i2c1, i2c2, _led, _tx, _rx, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
let (_pin, i2c1, i2c2, _led, _tx1, _rx1, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(i2c1, i2c2, delay, clocks)
}

pub fn i2c1_i2c2_led_delay_from_dp(dp: Peripherals) -> (I2c1Type, I2c2Type, LedType, Delay) {
let (_pin, i2c1, i2c2, led, _tx, _rx, _spi, _spiext, delay, _clocks) = no_adc_from_dp(dp);
let (_pin, i2c1, i2c2, led, _tx1, _rx1, _spi, _spiext, delay, _clocks) = no_adc_from_dp(dp);
(i2c1, i2c2, led, delay)
}

pub fn i2c1_i2c2_led_from_dp(dp: Peripherals) -> (I2c1Type, I2c2Type, LedType) {
let (_pin, i2c1, i2c2, led, _tx, _rx, _spi, _spiext, _delay, _clocks) = no_adc_from_dp(dp);
let (_pin, i2c1, i2c2, led, _tx1, _rx1, _spi, _spiext, _delay, _clocks) = no_adc_from_dp(dp);
(i2c1, i2c2, led)
}

pub fn i2c1_i2c2_from_dp(dp: Peripherals) -> (I2c1Type, I2c2Type) {
let (_pin, i2c1, i2c2, _led, _tx, _rx, _spi, _spiext, _delay, _clocks) = no_adc_from_dp(dp);
let (_pin, i2c1, i2c2, _led, _tx1, _rx1, _spi, _spiext, _delay, _clocks) = no_adc_from_dp(dp);
(i2c1, i2c2)
}

pub fn pin_i2c_led_tx_delay_clocks_from_dp(dp: Peripherals) -> (OpenDrainType, I2cType, LedType, TxType, Delay, Clocks) {
let (pin, i2c, _i2c2, led, tx, _rx, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(pin, i2c, led, tx, delay, clocks)
let (pin, i2c, _i2c2, led, tx1, _rx1, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(pin, i2c, led, tx1, delay, clocks)
}

pub fn pin_i2c_led_tx_delay_from_dp(dp: Peripherals) -> (OpenDrainType, I2cType, LedType, TxType, Delay) {
let (pin, i2c, _i2c2, led, tx, _rx, _spi, _spiext, delay, _clocks) = no_adc_from_dp(dp);
(pin, i2c, led, tx, delay)
let (pin, i2c, _i2c2, led, tx1, _rx1, _spi, _spiext, delay, _clocks) = no_adc_from_dp(dp);
(pin, i2c, led, tx1, delay)
}

pub fn i2c_led_tx_from_dp(dp: Peripherals) -> (I2cType, LedType, TxType) {
let (_pin, i2c, _i2c2, led, tx, _rx, _spi, _spiext, _delay, _clocks) = no_adc_from_dp(dp);
let (_pin, i2c, _i2c2, led, tx, _rx1, _spi, _spiext, _delay, _clocks) = no_adc_from_dp(dp);
(i2c, led, tx)
}


// ///// filter no_tx_rx_spi below
// ///// filter no adc or spi or tx_rx or tx2, rx2 below

fn no_tx_rx_spi_from_dp(dp: Peripherals) -> (OpenDrainType, I2c1Type, I2c2Type, LedType, Delay, Clocks) {
let (pin, i2c1, i2c2, led, _tx, _rx, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
let (pin, i2c1, i2c2, led, _tx1, _rx1, _spi, _spiext, delay, clocks) = no_adc_from_dp(dp);
(pin, i2c1, i2c2, led, delay, clocks)
}

Expand Down
25 changes: 19 additions & 6 deletions src/setup_all_stm32f0xx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,13 @@ pub use crate::led::LED; // defines trait and default methods
pub type LedType = LEDPIN<Output<PushPull>>;
impl LED for LedType {}

pub type TxType = Tx<USART1>;
pub type RxType = Rx<USART1>;
pub type Tx1Type = Tx<USART1>;
pub type Rx1Type = Rx<USART1>;
pub type Tx2Type = Tx<USART2>;
pub type Rx2Type = Rx<USART2>;

pub type TxType = Tx1Type;
pub type RxType = Rx1Type;

pub type SpiType = Spi<SPI1>;
pub struct SpiExt { pub cs: Pin<'A', 11, Output>, //pa11 UNTESTED
Expand Down Expand Up @@ -76,7 +81,7 @@ pub type AdcSensor1Type = AdcSensor<PA1<Analog>, Adc>;
// //////////////////////////////////////////////////////////////////////

pub fn all_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, TxType, RxType,
(OpenDrainType, I2c1Type, I2c2Type, LedType, Tx1Type, Rx1Type, Tx2Type, Rx2Type,
SpiType, SpiExt, Delay, Clocks, AdcSensor1Type) {
let mut rcc = dp.RCC.configure().freeze(&mut dp.FLASH);
let gpioa = dp.GPIOA.split(&mut rcc);
Expand Down Expand Up @@ -131,10 +136,18 @@ pub fn all_from_dp(dp: Peripherals) ->
gpioa.pa10.into_alternate_af1(cs), //rx pa10
)
});
let (tx1, rx1, tx2, rx2) = cortex_m::interrupt::free(move |cs| {
(
gpioa.pa9.into_alternate_af1(cs), //tx pa9
gpioa.pa10.into_alternate_af1(cs), //rx pa10
gpioa.pa2.into_alternate_af1(cs), //tx pa2
gpioa.pa3.into_alternate_af1(cs), //rx pa3
)
});

let (tx, _rx) = Serial::usart1(dp.USART1, (tx, rx), 9600.bps(), &mut rcc).split();

let (tx1, rx1) = Serial::usart1(p.USART1, (tx1, rx1), 9600.bps(), &mut rcc).split();

let (tx2, rx2) = Serial::usart2(p.USART2, (tx2, rx2), 9600.bps(), &mut rcc).split();

let adc1: AdcSensor1Type = AdcSensor {
ch: gpioa.pa1.into_analog(),
Expand All @@ -144,6 +157,6 @@ pub fn all_from_dp(dp: Peripherals) ->
fn read_mv(&mut self) -> u32 { self.adc.read(&mut self.ch).unwrap() }
}

(pin, i2c1, i2c2, led, tx, rx, spi1, spiext, delay, clocks, adc1)
(pin, i2c1, i2c2, led, tx1, rx1, tx2, rx2, spi1, spiext, delay, clocks, adc1)
}

28 changes: 22 additions & 6 deletions src/setup_all_stm32f1xx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,13 @@ pub use crate::led::LED; // defines trait and default methods
pub type LedType = LEDPIN<Output<PushPull>>;
impl LED for LedType {}

pub type TxType = Tx<USART1>;
pub type RxType = Rx<USART1>;
pub type Tx1Type = Tx<USART1>;
pub type Rx1Type = Rx<USART1>;
pub type Tx2Type = Tx<USART2>;
pub type Rx2Type = Rx<USART2>;

pub type TxType = Tx1Type;
pub type RxType = Rx1Type;

pub type SpiType = Spi<SPI1, Spi1NoRemap, impl SpiPins<Spi1NoRemap>, u8>;
pub struct SpiExt { pub cs: Pin<'A', 11, Output>, //UNTESTED
Expand Down Expand Up @@ -86,7 +91,7 @@ pub type AdcSensor1Type = AdcSensor<PA1<Analog>, Adc<ADC1>>;
// //////////////////////////////////////////////////////////////////////

pub fn all_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, TxType, RxType,
(OpenDrainType, I2c1Type, I2c2Type, LedType, Tx1Type, Rx1Type, Tx2Type, Rx2Type,
SpiType, SpiExt, Delay, Clocks, AdcSensor1Type) {
let mut flash = dp.FLASH.constrain();
let rcc = dp.RCC.constrain();
Expand Down Expand Up @@ -181,7 +186,7 @@ pub fn all_from_dp(dp: Peripherals) ->
// (There can also a move problem with afio if setup_i2c1() takes afio rather than &mut afio,
// but that can be resolved by just doing serial() before setup_i2c1().)

let (tx, rx) = Serial::new(
let (tx1, rx1) = Serial::new(
dp.USART1,
// (gpiob.pb6.into_alternate_push_pull(&mut gpiob.crl),
// gpiob.pb7,
Expand All @@ -192,7 +197,18 @@ pub fn all_from_dp(dp: Peripherals) ->
Config::default().baudrate(115200.bps()),
&clocks,
).split();


let (tx2, rx2) = Serial::new(
p.USART2,
(
gpiob.pa2.into_alternate_push_pull(&mut gpiob.crh),
gpiob.pa3, // probably need alt
),
&mut afio.mapr,
Config::default().baudrate(9_600.bps()),
&clocks,
)
.split();


let adc1: AdcSensor1Type = AdcSensor {
Expand All @@ -205,6 +221,6 @@ pub fn all_from_dp(dp: Peripherals) ->
}
}

(pin, i2c1, i2c2, led, tx, rx, spi1, spiext, delay, clocks, adc1)
(pin, i2c1, i2c2, led, tx1, rx1, tx2, rx2, spi1, spiext, delay, clocks, adc1)
}

31 changes: 26 additions & 5 deletions src/setup_all_stm32f3xx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ impl LED for LedType { // not default
//pub type TxType = Tx<USART1, impl TxPin<USART1>>; // impl is unstable in type alias
// See https://github.com/stm32-rs/stm32f3xx-hal/issues/288
// regarding why it is necessary to specify the concrete pin here.
pub type TxType = Tx<USART1, PA9<AF7<PushPull>>>;
pub type RxType = Rx<USART1>;
pub type Tx1Type = Tx<USART1, PA9<AF7<PushPull>>>;
pub type Rx1Type = Rx<USART1>;
pub type Tx2Type = Tx<USART2>;
pub type Rx2Type = Rx<USART2>;

pub type TxType = Tx1Type;
pub type RxType = Rx1Type;

pub type SpiType = Spi<SPI1>;
pub struct SpiExt { pub cs: Pin<'A', 1, Output>,
Expand Down Expand Up @@ -94,7 +99,7 @@ pub type AdcSensor1Type = AdcSensor<PA1<Analog>, Adc<ADC1, Disabled>>; // possib
// //////////////////////////////////////////////////////////////////////

pub fn all_from_dp(dp: Peripherals) ->
(OpenDrainType, I2c1Type, I2c2Type, LedType, TxType, RxType,
(OpenDrainType, I2c1Type, I2c2Type, LedType, Tx1Type, Rx1Type, Tx2Type, Rx2Type,
SpiType, SpiExt, Delay, Clocks, AdcSensor1Type) {
let mut flash = dp.FLASH.constrain();
let mut rcc = dp.RCC.constrain();
Expand Down Expand Up @@ -144,7 +149,7 @@ pub fn all_from_dp(dp: Peripherals) ->

let delay = DelayType{};

let (tx, _rx) = Serial::new(
let (tx1, rx1) = Serial::new(
dp.USART1,
(
gpioa
Expand All @@ -160,6 +165,22 @@ pub fn all_from_dp(dp: Peripherals) ->
)
.split();

let (tx2, rx2) = Serial::new(
dp.USART2,
(
gpioa
.pa2
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl), //tx pa2
gpioa
.pa3
.into_af_push_pull(&mut gpioa.moder, &mut gpioa.otyper, &mut gpioa.afrl), //rx pa3
),
115_200.Bd(), // 9600.bps(),
clocks,
&mut rcc.apb1,
)
.split();



let adc1: AdcSensor1Type = AdcSensor {
Expand All @@ -170,6 +191,6 @@ pub fn all_from_dp(dp: Peripherals) ->
fn read_mv(&mut self) -> u32 { self.adc.read(&mut self.ch).unwrap() }
}

(pin, i2c1, i2c2, led, tx, rx, spi1, spiext, delay, clocks, adc1)
(pin, i2c1, i2c2, led, tx1, rx1, tx2, rx2, spi1, spiext, delay, clocks, adc1)
}

Loading

0 comments on commit 461c946

Please sign in to comment.