Skip to content

Commit

Permalink
Gh-10 rework pin controller
Browse files Browse the repository at this point in the history
Rework to accomodate changes in the linux subsystem by #49.
Took the opportunity to go over the code once more and clean up the
public methods of pin_controller and bmc_application.
  • Loading branch information
svenrademakers committed Jun 29, 2023
1 parent 7b6e3a1 commit 005a1c7
Show file tree
Hide file tree
Showing 8 changed files with 351 additions and 417 deletions.
350 changes: 131 additions & 219 deletions src/app/bmc_application.rs

Large diffs are not rendered by default.

33 changes: 24 additions & 9 deletions src/c_interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ use tokio::sync::mpsc::channel;
use tokio::sync::mpsc::Receiver;
use tokio::{runtime::Runtime, sync::Mutex};

use crate::app::bmc_application::{BmcApplication, UsbConfig};
use crate::middleware::usbboot::FlashingError;
use crate::{app::bmc_application::BmcApplication, middleware::NodeId};
use crate::middleware::{UsbMode, UsbRoute};

/// we need means to synchronize async call to the outside. This runtime
/// enables us to execute async calls in a blocking fashion.
Expand Down Expand Up @@ -43,7 +44,9 @@ pub extern "C" fn tpi_initialize() {
log::info!("{} v{}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"));

APP.set(Mutex::new(
BmcApplication::new().await.expect("unable to initialize"),
BmcApplication::new()
.await
.expect("unable to initialize bmc app"),
))
.expect("initialize to be called once");
});
Expand All @@ -63,11 +66,18 @@ where

#[no_mangle]
pub extern "C" fn tpi_node_power(num: c_int, status: c_int) {
let Ok(node_id): Result<NodeId,()> = num.try_into().map_err(|e| log::error!("{}", e)) else {
return;
};

execute_routine(|bmc| Box::pin(bmc.activate_slot(node_id, status != 0)));
num.try_into().map_or_else(
|e| log::error!("{}", e),
|x: u8| {
// if status == 0, node == off.
// if status == 1, node == on.
let mut node = 1 << x;
if status == 0 {
node = !node;
}
execute_routine(|bmc| Box::pin(bmc.activate_slot(node, 1 << x)));
},
);
}

#[no_mangle]
Expand All @@ -78,7 +88,12 @@ pub extern "C" fn tpi_usb_mode(mode: c_int, node: c_int) -> c_int {
let Ok(mode) = mode.try_into().map_err(|e| log::error!("{}", e)) else {
return -1;
};
execute_routine(|bmc| Box::pin(bmc.usb_mode(mode, node_id)));

let config = match mode {
UsbMode::Device => UsbConfig::UsbA(node_id, true),
UsbMode::Host => UsbConfig::Node(node_id, UsbRoute::UsbA),
};
execute_routine(|bmc| Box::pin(bmc.configure_usb(config)));
0
}

Expand Down Expand Up @@ -144,7 +159,7 @@ pub extern "C" fn tpi_node_to_msd(node: c_int) {
let handle = tokio::spawn(async move {
bmc.set_node_in_msd(
node.try_into().unwrap(),
crate::middleware::UsbRoute::BMC,
crate::middleware::UsbRoute::Bmc,
sender,
)
.await
Expand Down
3 changes: 2 additions & 1 deletion src/middleware/event_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};

use evdev::InputEventKind;
use evdev::{Device, Key};
use log::{debug, warn};
use log::{debug, trace, warn};

type ActionFn<T> = Box<dyn Fn(&'_ mut T) + Send + Sync>;

Expand Down Expand Up @@ -39,6 +39,7 @@ impl<T: Send + Sync + 'static> EventListener<T> {
let mut event_stream = device.into_event_stream()?;
tokio::spawn(async move {
while let Ok(event) = event_stream.next_event().await {
trace!("processing event {:?}", event);
if let InputEventKind::Key(x) = event.kind() {
if let Some(action) = self.map.get(&(x, event.value())) {
action(&mut self.context);
Expand Down
50 changes: 21 additions & 29 deletions src/middleware/gpio_definitions.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
//! This module contains pin numbers of the connected gpios. A next step would
//! be to configure it in the dst as a new gpiochip

#![allow(dead_code)]
#![allow(clippy::identity_op)]

pub const GPIO_PIN_PG: u32 = 192;
pub const GPIO_PIN_PD: u32 = 96;

pub const RTL_RESET: u32 = GPIO_PIN_PG + 13;
pub const SYS_RESET: u32 = GPIO_PIN_PG + 11;
pub const POWER_DETECT: u32 = GPIO_PIN_PG + 10;
pub const POWER_BOARD: u32 = GPIO_PIN_PG + 15;

pub const PORT1_EN: u32 = GPIO_PIN_PD + 11;
pub const PORT2_EN: u32 = GPIO_PIN_PD + 10;
pub const PORT3_EN: u32 = GPIO_PIN_PD + 9;
pub const PORT4_EN: u32 = GPIO_PIN_PD + 8;

pub const MODE1_EN: u32 = GPIO_PIN_PD + 7;
pub const MODE2_EN: u32 = GPIO_PIN_PD + 6;
pub const MODE3_EN: u32 = GPIO_PIN_PD + 5;
pub const MODE4_EN: u32 = GPIO_PIN_PD + 4;
pub const POWER_EN: u32 = GPIO_PIN_PD + 3;

pub const PORT1_RST: u32 = GPIO_PIN_PD + 0;
pub const PORT2_RST: u32 = GPIO_PIN_PD + 20;
pub const PORT3_RST: u32 = GPIO_PIN_PD + 21;
pub const PORT4_RST: u32 = GPIO_PIN_PD + 22;

pub const PORT1_USB_VBUS: u32 = GPIO_PIN_PD + 19;
pub const PORT2_USB_VBUS: u32 = GPIO_PIN_PD + 18;
pub const PORT3_USB_VBUS: u32 = GPIO_PIN_PD + 17;
pub const PORT4_USB_VBUS: u32 = GPIO_PIN_PD + 16;

pub const PORT1_RPIBOOT: u32 = GPIO_PIN_PD + 15;
pub const PORT2_RPIBOOT: u32 = GPIO_PIN_PD + 14;
pub const PORT3_RPIBOOT: u32 = GPIO_PIN_PD + 12;
pub const PORT4_RPIBOOT: u32 = GPIO_PIN_PD + 13;

pub const USB_SEL1: u32 = GPIO_PIN_PG + 1;
pub const USB_SEL2: u32 = GPIO_PIN_PG + 0;
pub const USB_OE1: u32 = GPIO_PIN_PG + 2;
pub const USB_OE2: u32 = GPIO_PIN_PG + 3;

pub const USB_SWITCH: u32 = GPIO_PIN_PG + 5;
pub const USB_PWEN: u32 = GPIO_PIN_PG + 4;

// gpiochip0 aggregator
pub const PORT1_USB_VBUS: u32 = 2;
pub const PORT2_USB_VBUS: u32 = 6;
pub const PORT3_USB_VBUS: u32 = 10;
pub const PORT4_USB_VBUS: u32 = 14;

pub const PORT1_EN: u32 = 0;
pub const PORT2_EN: u32 = 4;
pub const PORT3_EN: u32 = 8;
pub const PORT4_EN: u32 = 12;

pub const PORT1_RST: u32 = 1;
pub const PORT2_RST: u32 = 5;
pub const PORT3_RST: u32 = 9;
pub const PORT4_RST: u32 = 13;

pub const PORT1_RPIBOOT: u32 = 3;
pub const PORT2_RPIBOOT: u32 = 7;
pub const PORT3_RPIBOOT: u32 = 11;
pub const PORT4_RPIBOOT: u32 = 15;
21 changes: 21 additions & 0 deletions src/middleware/helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/// small helper macro which handles the code duplication of declaring gpio lines.
#[macro_export]
macro_rules! setup_output_array {
// this macro has 2 match patterns, the differenciator is the 'arr' prefix.
// which is used to switch from creating a output object with arr of values
// to an array of output objects containing one value.
($chip:ident, arr [$($pin:ident),+], $active: expr) => {
[
$(
setup_output_array!($chip, [$pin], $active)
),*
]
};

($chip:ident, $output:expr, $active: expr) => {
$chip
.request_lines(gpiod::Options::output($output).active($active))
.context(concat!("error initializing pin ", stringify!($pin)))?
};

}
6 changes: 4 additions & 2 deletions src/middleware/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod app_persistency;
pub mod event_listener;
mod gpio_definitions;
pub(crate) mod helpers;
pub mod pin_controller;
pub mod power_controller;
pub mod usbboot;

#[repr(C)]
Expand Down Expand Up @@ -47,7 +49,7 @@ pub enum NodeType {

#[derive(Debug, Eq, PartialEq, Clone, Copy, serde::Serialize, serde::Deserialize)]
pub enum UsbRoute {
BMC,
Bmc,
UsbA,
}

Expand All @@ -56,7 +58,7 @@ impl TryFrom<i32> for UsbRoute {

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
0 => Ok(UsbRoute::BMC),
0 => Ok(UsbRoute::Bmc),
1 => Ok(UsbRoute::UsbA),
x => Err(format!("usb route{} does not exist", x)),
}
Expand Down
Loading

0 comments on commit 005a1c7

Please sign in to comment.