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
36 changes: 26 additions & 10 deletions src/Emulator/Peripherals/Peripherals/SPI/IMXRT_LPSPI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ private void DefineRegisters()
.WithFlag(20, out continuingCommand, name: "CONTC - Continuing Command", valueProviderCallback: _ => currentCommand?.ContinuingCommand ?? false)
.WithFlag(21, out continuousTransfer, name: "CONT - Continuous Transfer", valueProviderCallback: _ => currentCommand?.Continuous ?? false)
.WithTaggedFlag("BYSW - Byte Swap", 22)
.WithTaggedFlag("LSBF - LSB First", 23)
.WithFlag(23, out lsbFirst, name: "LSBF - LSB First", valueProviderCallback: _ => currentCommand?.LsbFirst ?? false)
.WithValueField(24, 2, name: "PCS - Peripheral Chip Select", writeCallback: (_, value) =>
{
if(!TryGetByAddress((int)value, out selectedDevice))
Expand All @@ -119,7 +119,8 @@ private void DefineRegisters()
TxMask = transmitDataMask.Value,
RxMask = receiveDataMask.Value,
Continuous = continuousTransfer.Value,
ContinuingCommand = continuingCommand.Value
ContinuingCommand = continuingCommand.Value,
LsbFirst = lsbFirst.Value
});
UpdateTransmitter();
});
Expand Down Expand Up @@ -368,24 +369,36 @@ private bool TrySendDataInner(uint value, ISPIPeripheral device)
dataMatcher.Match1 = (uint)match1.Value;
}

// we can read up to 4 bytes at a time
var byteIdx = 0;
// We can transfer up to 4 bytes at a time.
var byteCount = (int)Math.Min(sizeLeft / 8, 4u);
uint receivedWord = 0;

var lsbFirst = currentCommand.LsbFirst;

this.Log(LogLevel.Debug, "Sending 0x{0:X} to the device", value);
while(sizeLeft != 0 && byteIdx < 4)
for(var i = 0; i < byteCount; i++)
{
var resp = device.Transmit((byte)value);
var bytePos = lsbFirst ? i : byteCount - 1 - i;

var txByte = (byte)(value >> (bytePos * 8));
if(lsbFirst)
{
txByte = BitHelper.ReverseBits(txByte);
}

receivedWord |= (uint)resp << (byteIdx * 8);
var resp = device.Transmit(txByte);
if(lsbFirst)
{
resp = BitHelper.ReverseBits(resp);
}

receivedWord |= (uint)resp << (bytePos * 8);

value >>= 8;
sizeLeft -= 8;
byteIdx++;
}
this.Log(LogLevel.Debug, "Received response 0x{0:X} from the device", receivedWord);

if(!receiveDataMask.Value && dataMatcher.MatchAndPush(receiveFifo, receivedWord, byteIdx * 8, rxDataMatchOnly.Value))
if(!receiveDataMask.Value && dataMatcher.MatchAndPush(receiveFifo, receivedWord, byteCount * 8, rxDataMatchOnly.Value))
{
dataMatch.Value = true;
}
Expand Down Expand Up @@ -567,6 +580,7 @@ private void UpdateTransmitter(TCFifoEntry transmitEntry = null)
private IValueRegisterField rxWatermark;
private IFlagRegisterField continuingCommand;
private IFlagRegisterField continuousTransfer;
private IFlagRegisterField lsbFirst;
private IFlagRegisterField circularFifoEnabled;

private IFlagRegisterField receiveDataInterruptEnable;
Expand Down Expand Up @@ -596,6 +610,8 @@ private class TCFifoCmd : TCFifoEntry
public bool ContinuingCommand { get; set; }

public bool ByteSwap { get; set; }

public bool LsbFirst { get; set; }
}

private class TCFifoData : TCFifoEntry
Expand Down