Skip to content

Commit

Permalink
examples: rp-pico: Showcase remote I/O instead of encoder
Browse files Browse the repository at this point in the history
Make the example more general purpose by showcasing communication with a
remote I/O station instead of an encoder.
  • Loading branch information
Rahix committed Jan 17, 2025
1 parent 901d422 commit e525dbe
Showing 1 changed file with 36 additions and 26 deletions.
62 changes: 36 additions & 26 deletions examples/rp-pico/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ mod logger;
mod panic_handler;
mod time;

// Encoder Parameters
const ENCODER_ADDRESS: u8 = 6;
// I/O Parameters
const IO_ADDRESS: u8 = 13;

// Bus Parameters
const MASTER_ADDRESS: u8 = 2;
const BAUDRATE: Baudrate = Baudrate::B19200;
const BAUDRATE: Baudrate = Baudrate::B500000;

#[bsp::entry]
fn main() -> ! {
Expand Down Expand Up @@ -105,53 +105,63 @@ fn main() -> ! {
let mut storage: [dp::PeripheralStorage; 2] = Default::default();
let mut dp_master = dp::DpMaster::new(&mut storage[..]);

// Options generated by `gsdtool` using "FRAB4711.gsd"
// Options generated by `gsdtool` using "et200b-8di8do.gsd"
let options = profirust::dp::PeripheralOptions {
// "FRABA Encoder" by "FRABA"
ident_number: 0x4711,
// "B-8DI/8DO DP " by "SIEMENS "
ident_number: 0x000b,

// Global Parameters:
// (none)
//
// Selected Modules:
// [0] Class 1 Singleturn
// - Code sequence: Increasing clockwise (0)
user_parameters: Some(&[0x00, 0x00]),
config: Some(&[0xd0]),
// [1] 1 Byte Out, 1 Byte In
user_parameters: Some(&[0x00, 0x00, 0x00, 0x00, 0x00]),
config: Some(&[0x20, 0x10]),

// Set max_tsdr depending on baudrate and assert
// that a supported baudrate is used.
max_tsdr: match BAUDRATE {
profirust::Baudrate::B9600 => 60,
profirust::Baudrate::B19200 => 60,
profirust::Baudrate::B45450 => 250,
profirust::Baudrate::B93750 => 60,
profirust::Baudrate::B187500 => 60,
profirust::Baudrate::B500000 => 100,
b => panic!("Peripheral \"FRABA Encoder\" does not support baudrate {b:?}!"),
profirust::Baudrate::B1500000 => 150,
profirust::Baudrate::B3000000 => 250,
profirust::Baudrate::B6000000 => 450,
profirust::Baudrate::B12000000 => 800,
b => panic!(
"Peripheral \"B-8DI/8DO DP \" does not support baudrate {b:?}!"
),
},

fail_safe: false,
..Default::default()
};
let mut buffer_inputs = [0u8; 2];
let mut buffer_outputs = [0u8; 0];
let encoder_handle = dp_master.add(dp::Peripheral::new(
ENCODER_ADDRESS,
options,
&mut buffer_inputs[..],
&mut buffer_outputs[..],
));
let mut buffer_inputs = [0u8; 1];
let mut buffer_outputs = [0u8; 1];
let mut buffer_diagnostics = [0u8; 13];
let io_handle = dp_master.add(
dp::Peripheral::new(
IO_ADDRESS,
options,
&mut buffer_inputs[..],
&mut buffer_outputs[..],
)
.with_diag_buffer(&mut buffer_diagnostics[..]),
);

let mut fdl_master = fdl::FdlActiveStation::new(
fdl::ParametersBuilder::new(MASTER_ADDRESS, BAUDRATE)
.watchdog_timeout(profirust::time::Duration::from_secs(1))
.slot_bits(1920)
.slot_bits(4000)
.max_retry_limit(3)
.build_verified(&dp_master),
);

let mut init = false;
let mut last = profirust::time::Instant::ZERO;
let mut last_value = None;
loop {
let now = time::now().unwrap();

Expand All @@ -164,14 +174,14 @@ fn main() -> ! {

let events = dp_master.take_last_events();

let encoder = dp_master.get_mut(encoder_handle);
if events.cycle_completed && encoder.is_running() {
last_value = Some(u16::from_be_bytes(encoder.pi_i().try_into().unwrap()));
let io_station = dp_master.get_mut(io_handle);
if events.cycle_completed && io_station.is_running() {
io_station.pi_q_mut()[0] = if now.secs() % 2 == 0 { 0x55 } else { 0xAA };
}

if last.secs() != now.secs() {
if encoder.is_running() {
log::info!("Encoder Counts: {:5}", last_value.unwrap());
if io_station.is_running() {
log::info!("Inputs: {:08b}", io_station.pi_i()[0]);
}
let _ = led_pin.toggle();
}
Expand Down

0 comments on commit e525dbe

Please sign in to comment.