Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion src/Emulator/Peripherals/Peripherals/Mocks/DummySPISlave.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@

namespace Antmicro.Renode.Peripherals.Mocks
{
/// <summary>
/// A dummy SPI slave peripheral implementing <see cref="ISPIPeripheral"> for mocking communication and testing SPI controllers.
/// Model supports queuing data to its buffer, that will be return upon SPI Transmit. Additionally it exposes events that allow mocking more complex models.
/// Example:
/// python """
/// dummy = monitor.Machine["sysbus.spi.dummy"]
/// dummy.DataReceived += lambda data: dummy.EnqueueValue(data)
/// """
/// </summary>
public class DummySPISlave : ISPIPeripheral
{
public DummySPISlave()
Expand All @@ -25,6 +34,7 @@ public void EnqueueValue(byte val)

public void FinishTransmission()
{
TransmissionFinished?.Invoke();
idx = 0;
}

Expand All @@ -34,8 +44,16 @@ public void Reset()
idx = 0;
}

/// <summary>
/// Implements <see cref="ISPIPeripheral">.
/// Logs the received <paramref name="data"/>.
/// </summary>
/// <remarks>
/// This method invokes <see cref="DataReceived"> event.
/// </remarks>
public byte Transmit(byte data)
{
DataReceived?.Invoke(data);
this.Log(LogLevel.Debug, "Data received: 0x{0:X} (idx: {1})", data, idx);
idx++;
if(buffer.Count == 0)
Expand All @@ -46,8 +64,17 @@ public byte Transmit(byte data)
return buffer.Dequeue();
}

private int idx;
/// <summary>
/// Informs about the <see cref="Write"> method being called with the first argument being the data written to the model.
/// </summary>
public event Action<byte> DataReceived;

/// <summary>
/// Informs about the <see cref="FinishTransmission"> method being called.
/// </summary>
public event Action TransmissionFinished;

private int idx;
private readonly Queue<byte> buffer;
}
}