Skip to content

Commit ce1d454

Browse files
DummySPISlave: Add events and minor documentation
Added events so that DummySPISlave can be extended with user implementation. Reference taken from DummyI2CSlave.
1 parent d23ea81 commit ce1d454

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/Emulator/Peripherals/Peripherals/Mocks/DummySPISlave.cs

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
1-
//
1+
//
22
// Copyright (c) 2010-2021 Antmicro
33
//
44
// This file is licensed under the MIT License.
55
// Full license text is available in 'licenses/MIT.txt'.
66
//
7+
using System;
78
using Antmicro.Renode.Logging;
89
using Antmicro.Renode.Peripherals.SPI;
910
using System.Collections.Generic;
1011

1112
namespace Antmicro.Renode.Peripherals.Mocks
1213
{
14+
/// <summary>
15+
/// A dummy SPI slave peripheral implementing <see cref="ISPIPeripheral"> for mocking communication and testing SPI controllers.
16+
/// Model supports queuing data to its buffer, that will be return upon SPI Transmit. Additionally it exposes events that allow mocking more complex models.
17+
/// Example:
18+
/// python """
19+
/// dummy = monitor.Machine["sysbus.spi.dummy"]
20+
/// dummy.DataReceived += lambda data: dummy.EnqueueValue(data)
21+
/// """
22+
/// </summary>
1323
public class DummySPISlave : ISPIPeripheral
1424
{
1525
public DummySPISlave()
@@ -33,8 +43,17 @@ public void Reset()
3343
idx = 0;
3444
}
3545

46+
47+
/// <summary>
48+
/// Implements <see cref="ISPIPeripheral">.
49+
/// Logs the received <paramref name="data"/>.
50+
/// </summary>
51+
/// <remarks>
52+
/// This method invokes <see cref="DataReceived"> event.
53+
/// </remarks>
3654
public byte Transmit(byte data)
3755
{
56+
DataReceived?.Invoke(data);
3857
this.Log(LogLevel.Debug, "Data received: 0x{0:X} (idx: {1})", data, idx);
3958
idx++;
4059
if(buffer.Count == 0)
@@ -45,8 +64,17 @@ public byte Transmit(byte data)
4564
return buffer.Dequeue();
4665
}
4766

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

72+
/// <summary>
73+
/// Informs about the <see cref="FinishTransmission"> method being called.
74+
/// </summary>
75+
public event Action TransmissionFinished;
76+
77+
private int idx;
5078
private readonly Queue<byte> buffer;
5179
}
5280
}

0 commit comments

Comments
 (0)