Skip to content

Commit

Permalink
Address feedback
Browse files Browse the repository at this point in the history
- make driver_num a constant
- remove parameters for R/W config and just use the default config
  • Loading branch information
mlvisaya committed Jan 3, 2025
1 parent edc2136 commit 4da9dce
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
22 changes: 12 additions & 10 deletions runtime/apps/apis/dma/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ use libtock_platform::{ErrorCode, Syscalls};
use libtockasync::TockSubscribe;

/// DMA interface.
pub struct AsyncDMA<const DRIVER_NUM: u32, S: Syscalls>(S);
pub struct AsyncDMA<S: Syscalls>(S);

const DMA_DRIVER_NUM: u32 = 0x8000_0008;
/// Configuration parameters for a DMA transfer.
#[derive(Debug, Copy, Clone)]
pub struct DMATransaction {
Expand All @@ -22,7 +23,7 @@ pub struct DMATransaction {
pub dest_addr: u64,
}

impl<const DRIVER_NUM: u32, S: Syscalls> AsyncDMA<DRIVER_NUM, S> {
impl<S: Syscalls> AsyncDMA<S> {
/// Do a DMA transfer.
///
/// This method executes a DMA transfer based on the provided `DMATransaction` configuration.
Expand All @@ -36,36 +37,37 @@ impl<const DRIVER_NUM: u32, S: Syscalls> AsyncDMA<DRIVER_NUM, S> {
pub async fn xfer(transaction: DMATransaction) -> Result<(), ErrorCode> {
Self::setup(transaction).await?;

let async_start = TockSubscribe::subscribe::<S>(DRIVER_NUM, dma_subscribe::XFER_DONE);
S::command(DRIVER_NUM, dma_cmd::START, 0, 0).to_result::<(), ErrorCode>()?;
let async_start = TockSubscribe::subscribe::<S>(DMA_DRIVER_NUM, dma_subscribe::XFER_DONE);
S::command(DMA_DRIVER_NUM, dma_cmd::START, 0, 0).to_result::<(), ErrorCode>()?;
async_start.await.map(|_| ())
}

async fn setup(config: DMATransaction) -> Result<(), ErrorCode> {
let async_configure =
TockSubscribe::subscribe::<S>(DRIVER_NUM, dma_subscribe::SET_BYTE_XFER_COUNT_DONE);
TockSubscribe::subscribe::<S>(DMA_DRIVER_NUM, dma_subscribe::SET_BYTE_XFER_COUNT_DONE);
S::command(
DRIVER_NUM,
DMA_DRIVER_NUM,
dma_cmd::SET_BYTE_XFER_COUNT,
config.byte_count as u32,
0,
)
.to_result::<(), ErrorCode>()?;
async_configure.await.map(|_| ())?;

let async_src = TockSubscribe::subscribe::<S>(DRIVER_NUM, dma_subscribe::SET_SRC_DONE);
let async_src = TockSubscribe::subscribe::<S>(DMA_DRIVER_NUM, dma_subscribe::SET_SRC_DONE);
S::command(
DRIVER_NUM,
DMA_DRIVER_NUM,
dma_cmd::SET_SRC_ADDR,
(config.src_addr & 0xFFFF_FFFF) as u32,
(config.src_addr >> 32) as u32,
)
.to_result::<(), ErrorCode>()?;
async_src.await.map(|_| ())?;

let async_dest = TockSubscribe::subscribe::<S>(DRIVER_NUM, dma_subscribe::SET_DEST_DONE);
let async_dest =
TockSubscribe::subscribe::<S>(DMA_DRIVER_NUM, dma_subscribe::SET_DEST_DONE);
S::command(
DRIVER_NUM,
DMA_DRIVER_NUM,
dma_cmd::SET_DEST_ADDR,
(config.dest_addr & 0xFFFF_FFFF) as u32,
(config.dest_addr >> 32) as u32,
Expand Down
34 changes: 17 additions & 17 deletions runtime/apps/apis/mailbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

//! # AsyncMailbox: Mailbox Interface
use libtock_platform::allow_ro::{AllowRo, Config as ReadOnlyConfig};
use libtock_platform::allow_rw::{AllowRw, Config as ReadWriteConfig};
use libtock_platform::allow_ro::AllowRo;
use libtock_platform::allow_rw::AllowRw;
use libtock_platform::{share, DefaultConfig, ErrorCode, Syscalls};
use libtockasync::TockSubscribe;
/// Mailbox interface user interface.
Expand All @@ -13,16 +13,11 @@ use libtockasync::TockSubscribe;
/// - `S`: The syscall implementation.
/// - `W`: Configuration for writable buffers (response).
/// - `R`: Configuration for read-only buffers (input), defaults to `DefaultConfig`.
pub struct AsyncMailbox<
const DRIVER_NUM: u32,
S: Syscalls,
W: ReadWriteConfig,
R: ReadOnlyConfig = DefaultConfig,
>(S, W, R);
pub struct AsyncMailbox<S: Syscalls>(S);

impl<const DRIVER_NUM: u32, S: Syscalls, W: ReadWriteConfig, R: ReadOnlyConfig>
AsyncMailbox<DRIVER_NUM, S, W, R>
{
const MAILBOX_DRIVER_NUM: u32 = 0x8000_0009;

impl<S: Syscalls> AsyncMailbox<S> {
/// Executes a mailbox command and returns the response.
///
/// This method sends a mailbox command to the kernel, then waits
Expand All @@ -44,26 +39,31 @@ impl<const DRIVER_NUM: u32, S: Syscalls, W: ReadWriteConfig, R: ReadOnlyConfig>
) -> Result<usize, ErrorCode> {
share::scope::<
(
AllowRo<_, DRIVER_NUM, { mailbox_buffer::INPUT }>,
AllowRw<_, DRIVER_NUM, { mailbox_buffer::RESPONSE }>,
AllowRo<_, MAILBOX_DRIVER_NUM, { mailbox_buffer::INPUT }>,
AllowRw<_, MAILBOX_DRIVER_NUM, { mailbox_buffer::RESPONSE }>,
),
_,
_,
>(|handle| {
let (allow_ro, allow_rw) = handle.split();

// Share the input buffer (read-only)
S::allow_ro::<R, DRIVER_NUM, { mailbox_buffer::INPUT }>(allow_ro, input_data)?;
S::allow_ro::<DefaultConfig, MAILBOX_DRIVER_NUM, { mailbox_buffer::INPUT }>(
allow_ro, input_data,
)?;

// Share the response buffer (read-write)
S::allow_rw::<W, DRIVER_NUM, { mailbox_buffer::RESPONSE }>(allow_rw, response_buffer)?;
S::allow_rw::<DefaultConfig, MAILBOX_DRIVER_NUM, { mailbox_buffer::RESPONSE }>(
allow_rw,
response_buffer,
)?;

// Subscribe to the asynchronous notification for when the command is processed
let async_command =
TockSubscribe::subscribe::<S>(DRIVER_NUM, mailbox_subscribe::COMMAND_DONE);
TockSubscribe::subscribe::<S>(MAILBOX_DRIVER_NUM, mailbox_subscribe::COMMAND_DONE);

// Issue the command to the kernel
S::command(DRIVER_NUM, mailbox_cmd::EXECUTE_COMMAND, command, 0)
S::command(MAILBOX_DRIVER_NUM, mailbox_cmd::EXECUTE_COMMAND, command, 0)
.to_result::<(), ErrorCode>()?;

// Return the subscription for further processing
Expand Down

0 comments on commit 4da9dce

Please sign in to comment.