diff --git a/emulation/ec/betrusted-ec.repl b/emulation/ec/betrusted-ec.repl index bfcc3653a..2fe97aa86 100644 --- a/emulation/ec/betrusted-ec.repl +++ b/emulation/ec/betrusted-ec.repl @@ -31,7 +31,11 @@ flash_mem: Memory.MappedMemory @ { } size: 0x8000000 +lm3509: Mocks.LM3509 @ i2c 0x36 +tusb320lai: Mocks.TUSB320LAI @ i2c 0x47 bq27421: Sensors.BQ27421 @ i2c 0x55 +bq24157: Mocks.BQ24157 @ i2c 0x6a +lsm6ds3: Mocks.LSM6DS3 @ i2c 0x6b sysbus: init: diff --git a/emulation/ec/kernel.bin b/emulation/ec/kernel.bin index 8ccae6f40..c4153ad37 100644 Binary files a/emulation/ec/kernel.bin and b/emulation/ec/kernel.bin differ diff --git a/emulation/peripherals/BQ24157.cs b/emulation/peripherals/BQ24157.cs new file mode 100644 index 000000000..1a2dd9d94 --- /dev/null +++ b/emulation/peripherals/BQ24157.cs @@ -0,0 +1,53 @@ +// +// Copyright (c) 2010-2021 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// + +using Antmicro.Renode.Logging; +using Antmicro.Renode.Utilities; +using Antmicro.Renode.Peripherals.I2C; + +namespace Antmicro.Renode.Peripherals.Mocks +{ + public class BQ24157 : II2CPeripheral + { + public BQ24157() + { + Reset(); + } + + public void Write(byte[] data) + { + this.Log(LogLevel.Debug, "Written {0} bytes of data: {1}", data.Length, Misc.PrettyPrintCollectionHex(data)); + buffer = data; + } + + public byte[] Read(int count = 1) + { + this.Log(LogLevel.Debug, "Reading {0} bytes", count); + var result = new byte[count]; + for(var i = 0; i < result.Length; i++) + { + result[i] = (i < buffer.Length) + ? buffer[i] + : (byte)0; + } + + return result; + } + + public void FinishTransmission() + { + this.Log(LogLevel.Debug, "Finishing transmission"); + } + + public void Reset() + { + buffer = new byte[0]; + } + + private byte[] buffer; + } +} diff --git a/emulation/peripherals/BQ27421.cs b/emulation/peripherals/BQ27421.cs index 7afd0c4bc..12b9b0247 100644 --- a/emulation/peripherals/BQ27421.cs +++ b/emulation/peripherals/BQ27421.cs @@ -48,40 +48,24 @@ namespace Antmicro.Renode.Peripherals.Sensors // The first byte contains our address. this.Log(LogLevel.Noisy, "Write with {0} bytes of data: {1}", data.Length, Misc.PrettyPrintCollectionHex(data)); - // The second byte contains the register address - regAddress = (Registers)data[1]; + // // The second byte contains the register address + // regAddress = (Registers)data[1]; - if(data.Length == 2) - { - this.Log(LogLevel.Noisy, "Preparing to read register {0} (0x{0:X})", regAddress); - readyPending.Value = true; - UpdateInterrupts(); - return; - } - - // // skip the two bytes as they contain our address and the register address - // foreach(var b in data.Skip(2)) + // if(data.Length == 2) // { - // this.Log(LogLevel.Noisy, "Writing 0x{0:X} to register {1} (0x{1:X})", b, regAddress); - // RegistersCollection.Write((byte)regAddress, b); + // this.Log(LogLevel.Noisy, "Preparing to read register {0} (0x{0:X})", regAddress); + // readyPending.Value = true; + // UpdateInterrupts(); + // return; // } - if (data[1] == 0x00) { - var cmd = (data[2] << 0) | (data[3] << 8); - this.Log(LogLevel.Noisy, "Issuing control command (0x{0:X})", cmd); - switch (cmd) { - case 0x0000: response = controlStatus; break; - case 0x0012: controlStatus &= 0xffbf; break; - default: this.Log(LogLevel.Error, "Unhandled Control code: 0x{0:X}", cmd); break; - } - } } public byte[] Read(int count) { this.Log(LogLevel.Error, "Reading {0} bytes from register {1} (0x{1:X})", count, regAddress); var result = new byte[count]; - readyPending.Value = false; - UpdateInterrupts(); + // readyPending.Value = false; + // UpdateInterrupts(); if (count >= 1) { result[0] = (byte)(response & 0xff); } @@ -96,46 +80,6 @@ namespace Antmicro.Renode.Peripherals.Sensors // } return result; } - - // public decimal AccelerationX - // { - // get => accelarationX; - // set - // { - // if (!IsAccelerationOutOfRange(value)) - // { - // accelarationX = value; - // this.Log(LogLevel.Noisy, "AccelerationX set to {0}", accelarationX); - // } - // } - // } - - // public decimal AccelerationY - // { - // get => accelarationY; - // set - // { - // if (!IsAccelerationOutOfRange(value)) - // { - // accelarationY = value; - // this.Log(LogLevel.Noisy, "AccelerationY set to {0}", accelarationY); - // } - // } - // } - - // public decimal AccelerationZ - // { - // get => accelarationZ; - // set - // { - // if (!IsAccelerationOutOfRange(value)) - // { - // accelarationZ = value; - // this.Log(LogLevel.Noisy, "AccelerationZ set to {0}", accelarationZ); - // } - // } - // } - public decimal Temperature { get; set; } public GPIO IRQ { get; } @@ -143,150 +87,19 @@ namespace Antmicro.Renode.Peripherals.Sensors private void DefineRegisters() { - // Registers.WhoAmI.Define(this, 0x43); - - // Registers.Control1.Define(this) //RW - // .WithTaggedFlag("BLOCK_DATA_UPDATE", 0) - // .WithFlag(1, out highFreqDataRateMode, name: "HIGH_FREQ_MODE_ENABLE") - // .WithValueField(2, 2, out fullScale, name: "FULL_SCALE_SELECT") - // .WithValueField(4, 4, out outDataRate, name: "OUTPUT_DATA_RATE"); - - // Registers.Control4.Define(this, 0x01) //RW - // .WithFlag(0, out readyEnabled, name: "DATA_READY_IRQ1_ENABLE") - // .WithTaggedFlag("FIFO_THRESHOLD_IRQ1_ENABLE", 1) - // .WithTaggedFlag("6D_RECON_IRQ1_ENABLE", 2) - // .WithTaggedFlag("DOUBLE_TAP_RECON_IRQ1_ENABLE", 3) - // .WithTaggedFlag("FREE_FALL_RECON_IRQ1_ENABLE", 4) - // .WithTaggedFlag("WAKEUP_RECON_IRQ1_ENABLE", 5) - // .WithTaggedFlag("SINGLE_TAP_RECON_IRQ1_ENABLE", 6) - // .WithTaggedFlag("MASTER_DATA_READY_IRQ1_ENABLE", 7) - // .WithWriteCallback((_, __) => UpdateInterrupts()); - Registers.Temp.Define(this) .WithValueField(0, 8, FieldMode.Read, name: "TEMPERATURE_SENSOR", valueProviderCallback: _ => TwoComplementSignConvert(Temperature)); - - // Registers.Status.Define(this) //RO - // .WithFlag(0, out readyPending, FieldMode.Read, name: "XYZ_DATA_AVAILABLE") - // .WithTaggedFlag("FREE_FALL_EVENT_DETECT", 1) - // .WithTaggedFlag("CHANGE_IN_POSITION_DETECT", 2) - // .WithTaggedFlag("SINGLE_TAP_EVENT_DETECT", 3) - // .WithTaggedFlag("DOUBLE_TAP_EVENT_DETECT", 4) - // .WithTaggedFlag("SLEEP_EVENT_DETECT", 5) - // .WithTaggedFlag("WAKEUP_EVENT_DETECT", 6) - // .WithTaggedFlag("FIFO_REACH_THRESHOLD", 7); - - // Registers.DataOutXLow.Define(this) - // .WithValueField(0, 8, FieldMode.Read, name: "X_ACCEL_DATA[7:2]", valueProviderCallback: _ => Convert(AccelerationX, upperByte: false)); - - // Registers.DataOutXHigh.Define(this) - // .WithValueField(0, 8, FieldMode.Read, name: "X_ACCEL_DATA[15:8]", valueProviderCallback: _ => Convert(AccelerationX, upperByte: true)); - - // Registers.DataOutYLow.Define(this) - // .WithValueField(0, 8, FieldMode.Read, name: "Y_ACCEL_DATA[7:2]", valueProviderCallback: _ => Convert(AccelerationY, upperByte: false)); - - // Registers.DataOutYHigh.Define(this) - // .WithValueField(0, 8, FieldMode.Read, name: "Y_ACCEL_DATA[15:8]", valueProviderCallback: _ => Convert(AccelerationY, upperByte: true)); - - // Registers.DataOutZLow.Define(this) - // .WithValueField(0, 8, FieldMode.Read, name: "Z_ACCEL_DATA[7:2]", valueProviderCallback: _ => Convert(AccelerationZ, upperByte: false)); - - // Registers.DataOutZHigh.Define(this) - // .WithValueField(0, 8, FieldMode.Read, name: "Z_ACCEL_DATA[15:8]", valueProviderCallback: _ => Convert(AccelerationZ, upperByte: true)); - - // Registers.StatusEventDetection.Define(this) //RO - // .WithFlag(0, out readyPending, FieldMode.Read, name: "XYZ_DATA_AVAILABLE") - // .WithTaggedFlag("FREE_FALL_EVENT_DETECT", 1) - // .WithTaggedFlag("CHANGE_IN_POSITION_DETECT", 2) - // .WithTaggedFlag("SINGLE_TAP_EVENT_DETECT", 3) - // .WithTaggedFlag("DOUBLE_TAP_EVENT_DETECT", 4) - // .WithTaggedFlag("SLEEP_EVENT_DETECT", 5) - // .WithTaggedFlag("WAKEUP_EVENT_DETECT", 6) - // .WithTaggedFlag("FIFO_REACH_THRESHOLD", 7); } private void RegistersAutoIncrement() { - // if(regAddress >= Registers.DataOutXLow && regAddress < Registers.DataOutZHigh) - // { - // regAddress = (Registers)((int)regAddress + 1); - // this.Log(LogLevel.Noisy, "Auto-incrementing to the next register 0x{0:X} - {0}", regAddress); - // } } - private void UpdateInterrupts() - { - var status = readyEnabled.Value && readyPending.Value; - this.Log(LogLevel.Noisy, "Setting IRQ to {0}", status); - IRQ.Set(status); - } - - // private decimal GetSenesorSensitivity() + // private void UpdateInterrupts() // { - // decimal gain = SensorSensitivity; - // switch(fullScale.Value) - // { - // case (uint)FullScaleSelect.fullScale2g: - // gain = SensorSensitivity; - // break; - // case (uint)FullScaleSelect.fullScale16g: - // gain = 8 * SensorSensitivity; - // break; - // case (uint)FullScaleSelect.fullScale4g: - // gain = 2 * SensorSensitivity; - // break; - // case (uint)FullScaleSelect.fullScale8g: - // gain = 4 * SensorSensitivity; - // break; - // default: - // gain = SensorSensitivity; - // break; - // } - // return gain; - // } - - // private bool IsAccelerationOutOfRange(decimal acceleration) - // { - // // This range protects from the overflow of the short variables in the 'Convert' function. - // if (acceleration < MinAcceleration || acceleration > MaxAcceleration) - // { - // this.Log(LogLevel.Warning, "Acceleration is out of range, use value from the range <-19.5;19.5>"); - // return true; - // } - // return false; - // } - - // private byte Convert(decimal value, bool upperByte) - // { - // byte result = 0; - // decimal gain = GetSenesorSensitivity(); - // value = (value * 1000 / gain) / GravitationalConst; - // var valueAsShort = (short)value; - - // if(upperByte) - // { - // result = (byte)(valueAsShort >> 8); - // } - // else - // { - // if(highFreqDataRateMode.Value && - // outDataRate.Value >= (byte)DataRateModeStartRange.HighFreqDataRateStartRange && - // outDataRate.Value < (byte)DataRateModeStartRange.LowPowerDataRateStartRange) - // { - // result = (byte)(valueAsShort & (byte)CoverBytes.HighFreqMode); - // this.Log(LogLevel.Noisy, "High frequencies mode is selected."); - // } - // else if(outDataRate.Value >= (byte)DataRateModeStartRange.LowPowerDataRateStartRange) - // { - // result = (byte)(valueAsShort & (byte)CoverBytes.LowPowerMode); - // this.Log(LogLevel.Noisy, "Low power mode is selected."); - // } - // else - // { - // result = (byte)(valueAsShort & (byte)CoverBytes.NoneExtraModes); - // this.Log(LogLevel.Noisy, "High frequencies and low power modes aren't selected."); - // } - // } - // return result; + // var status = readyEnabled.Value && readyPending.Value; + // this.Log(LogLevel.Noisy, "Setting IRQ to {0}", status); + // IRQ.Set(status); // } private byte TwoComplementSignConvert(decimal temp) diff --git a/emulation/peripherals/BetrustedEcI2C.cs b/emulation/peripherals/BetrustedEcI2C.cs index be6f6dc7b..94ddfec8a 100644 --- a/emulation/peripherals/BetrustedEcI2C.cs +++ b/emulation/peripherals/BetrustedEcI2C.cs @@ -55,7 +55,10 @@ namespace Antmicro.Renode.Peripherals.I2C .WithFlag(6, FieldMode.Read, valueProviderCallback: _ => false, name: "Busy") .WithFlag(5, FieldMode.Read, valueProviderCallback: _ => false, name: "Arbitration lost") .WithReservedBits(2, 3) - .WithFlag(1, FieldMode.Read, valueProviderCallback: _ => false, name: "Transfer in progress") + .WithFlag(1, name: "Transfer in progress", valueProviderCallback: _ => { + fakeTip = !fakeTip; + return fakeTip; + }) .WithFlag(0, out i2cIrqStatus, FieldMode.Read) }, @@ -65,7 +68,6 @@ namespace Antmicro.Renode.Peripherals.I2C .WithFlag(5, out readFromSlave, FieldMode.Write) .WithFlag(4, out writeToSlave, FieldMode.Write) .WithFlag(3, FieldMode.Read, name: "ACK", valueProviderCallback: _ => true) - // .WithTag("ACK", 3, 1) .WithReservedBits(1, 2) .WithFlag(0, FieldMode.Write, writeCallback: (_, __) => i2cIrqStatus.Value = false) .WithWriteCallback((_, __) => @@ -248,6 +250,14 @@ namespace Antmicro.Renode.Peripherals.I2C private void HandleReadFromSlaveCommand() { + if (dataFromSlave == null) { + this.Log(LogLevel.Error, "dataFromSlave is NULL!"); + return; + } + if (selectedSlave == null) { + this.Log(LogLevel.Error, "selectedSlave is NULL!"); + return; + } if (dataFromSlave.Count == 0) { foreach (var b in selectedSlave.Read()) @@ -263,6 +273,10 @@ namespace Antmicro.Renode.Peripherals.I2C } } + if (receiveBuffer == null) { + this.Log(LogLevel.Error, "receiveBuffer is NULL!"); + dataFromSlave.Dequeue(); + } receiveBuffer.Value = dataFromSlave.Dequeue(); UpdateInterrupts(); } @@ -293,6 +307,8 @@ namespace Antmicro.Renode.Peripherals.I2C } private bool transactionInProgress; + // Just toggle TIP here to fake it. + private bool fakeTip; private II2CPeripheral selectedSlave; private readonly Queue dataToSlave; diff --git a/emulation/peripherals/BetrustedI2S.cs b/emulation/peripherals/BetrustedI2S.cs new file mode 100644 index 000000000..9354e4a31 --- /dev/null +++ b/emulation/peripherals/BetrustedI2S.cs @@ -0,0 +1,556 @@ +// +// Copyright (c) 2010-2020 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using Antmicro.Renode.Core; +using Antmicro.Renode.Core.Structure.Registers; +using Antmicro.Renode.Exceptions; +using Antmicro.Renode.Logging; +using Antmicro.Renode.Peripherals.Bus; +using Antmicro.Renode.Sound; +using Antmicro.Renode.Utilities; + +namespace Antmicro.Renode.Peripherals.Sound +{ + public class AudioRam : IBytePeripheral, IDoubleWordPeripheral, IWordPeripheral, IKnownSize + { + public AudioRam(Machine machine, BetrustedI2S audio, long size) + { + this.machine = machine; + this.audio = audio; + this.size = size; + this.data = new byte[size]; + } + + private Machine machine; + private BetrustedI2S audio; + private long size; + public byte[] data; + public long Size { get { return size; } } + + public void WriteDoubleWord(long address, uint value) + { + var bytes = BitConverter.GetBytes(value); + var i = 0; + for (i = 0; i < bytes.Length; i++) + { + this.data[address + i] = bytes[i]; + } + } + + public uint ReadDoubleWord(long offset) + { + return (((uint)this.data[offset]) << 0) | (((uint)this.data[offset + 1]) << 8) | (((uint)this.data[offset + 2]) << 16) | ((uint)(this.data[offset + 3]) << 24); + } + + public void WriteWord(long address, ushort value) + { + var bytes = BitConverter.GetBytes(value); + var i = 0; + for (i = 0; i < bytes.Length; i++) + { + this.data[address + i] = bytes[i]; + } + } + + public ushort ReadWord(long address) + { + return (ushort)((((ushort)this.data[address]) << 0) | (((ushort)this.data[address + 1]) << 8)); + } + + public byte ReadByte(long offset) + { + return this.data[offset]; + } + + public void WriteByte(long offset, byte value) + { + this.data[offset] = value; + } + + public void Reset() + { + for (var i = 0; i < this.data.Length; i++) + { + this.data[i] = 0; + } + } + } + + public class BetrustedI2S : BasicDoubleWordPeripheral, IDisposable, IKnownSize + { + public BetrustedI2S(Machine machine, uint memAddr, uint memSize) : base(machine) + { + this.audioRam = new AudioRam(machine, this, memSize); + this.bufferAddress = memAddr; + machine.SystemBus.Register(this.audioRam, new BusRangeRegistration(memAddr, memSize)); + + CreateRegisters(); + IRQ = new GPIO(); + Reset(); + } + + public void Dispose() + { + encoder?.Dispose(); + } + + public override void Reset() + { + base.Reset(); + IRQ.Unset(); + decoder?.Reset(); + encoder?.FlushBuffer(); + + sampleRatio = 256; + sampleWidth = 8; + numberOfChannels = 2; + masterFrequency = 4000000; + samplesPerDoubleWord = 4; + } + + public GPIO IRQ { get; } + public string InputFile { get; set; } + public string OutputFile { get; set; } + public long Size => 0x1000; + + private void UpdateInterrupts() + { + var rxReady = eventRxReady.Value && interruptEnableRxReady.Value; + var rxError = eventRxError.Value && interruptEnableRxError.Value; + var txReady = eventTxReady.Value && interruptEnableTxReady.Value; + var txError = eventTxError.Value && interruptEnableTxError.Value; + IRQ.Set(rxReady || rxError || txReady || txError); + } + + private void Start() + { + if(enableTx.Value) + { + if(OutputFile == "") + { + this.Log(LogLevel.Error, "Starting transmission without an output file!"); + return; + } + encoder = new PCMEncoder(sampleWidth, sampleFrequency, numberOfChannels, false); + encoder.SetBufferingBySamplesCount(maxSamplesCount.Value); + encoder.Output = OutputFile; + } + + if(enableRx.Value) + { + if(InputFile == "") + { + this.Log(LogLevel.Error, "Starting reception without an input file!"); + return; + } + decoder = new PCMDecoder(sampleWidth, sampleFrequency, numberOfChannels, false, this); + decoder.LoadFile(InputFile); + } + StartRxTxThread(); + } + + private void Stop() + { + encoder?.FlushBuffer(); + StopRxTxThread(); + eventStopped.Value = true; + UpdateInterrupts(); + } + + private void StartRxTxThread() + { + if(!enableI2S.Value) + { + this.Log(LogLevel.Error, "Trying to start aquisition, when peripheral is disabled (ENABLE==0). Will not start"); + return; + } + rxTxThread = machine.ObtainManagedThread(ProcessFrames, (int)(sampleFrequency / (maxSamplesCount.Value * samplesPerDoubleWord))); + rxTxThread.Start(); + } + + private void StopRxTxThread() + { + if(rxTxThread == null) + { + this.Log(LogLevel.Debug, "Trying to stop sampling when it is not active"); + return; + } + rxTxThread.Stop(); + rxTxThread = null; + } + + private void ProcessFrames() + { + if(enableRx.Value) + { + InputFrames(); + } + if(enableTx.Value) + { + OutputFrames(); + } + } + + private void OutputFrames() + { + var currentPointer = txdPointer.Value; + // The TXD.PTR register has been copied to internal double-buffers + eventTxPointerUpdated.Value = true; + UpdateInterrupts(); + + // RxTxMaxCnt denotes number of DoubleWords, we need to calculate samples number + for(var samples = 0u; samples < maxSamplesCount.Value * samplesPerDoubleWord; samples++) + { + var thisSample = machine.SystemBus.ReadDoubleWord(currentPointer + samples * sampleWidth / 8); + BitHelper.ClearBits(ref thisSample, (int)sampleWidth, (int)(32 - sampleWidth)); + encoder.AcceptSample(thisSample); + } + } + + private void InputFrames() + { + var currentPointer = rxdPointer.Value; + // The RXD.PTR register has been copied to internal double-buffers + eventRxPointerUpdated.Value = true; + UpdateInterrupts(); + + for(var doubleWords = 0u; doubleWords < maxSamplesCount.Value; doubleWords++) + { + // Double word may consist on many samples when sampleWidth is not equal 32bit + uint valueToStore = 0; + for(var sampleOffset = samplesPerDoubleWord; sampleOffset > 0; sampleOffset--) + { + valueToStore |= decoder.GetSingleSample() << (int)(sampleWidth * (sampleOffset - 1)); + } + machine.SystemBus.WriteDoubleWord(currentPointer + doubleWords * 4, valueToStore); + } + } + + private void SetMasterClockLrckRatio(uint value) + { + switch((MasterLrClockRatio)value) + { + case MasterLrClockRatio.X32: + sampleRatio = 32; + break; + case MasterLrClockRatio.X48: + sampleRatio = 48; + break; + case MasterLrClockRatio.X64: + sampleRatio = 64; + break; + case MasterLrClockRatio.X96: + sampleRatio = 96; + break; + case MasterLrClockRatio.X128: + sampleRatio = 128; + break; + case MasterLrClockRatio.X192: + sampleRatio = 192; + break; + case MasterLrClockRatio.X256: + sampleRatio = 256; + break; + case MasterLrClockRatio.X384: + sampleRatio = 384; + break; + case MasterLrClockRatio.X512: + sampleRatio = 512; + break; + default: + this.Log(LogLevel.Error, "Wrong CONFIG.RATIO value"); + break; + } + SetSampleFrequency(); + } + + private void SetMasterClockFrequency(uint val) + { + switch((MasterClockFrequency)val) + { + case MasterClockFrequency.Mhz32Div8: + masterFrequency = 32000000 / 8; + break; + case MasterClockFrequency.Mhz32Div10: + masterFrequency = 32000000 / 10; + break; + case MasterClockFrequency.Mhz32Div11: + masterFrequency = 32000000 / 11; + break; + case MasterClockFrequency.Mhz32Div15: + masterFrequency = 32000000 / 15; + break; + case MasterClockFrequency.Mhz32Div16: + masterFrequency = 32000000 / 16; + break; + case MasterClockFrequency.Mhz32Div21: + masterFrequency = 32000000 / 21; + break; + case MasterClockFrequency.Mhz32Div23: + masterFrequency = 32000000 / 23; + break; + case MasterClockFrequency.Mhz32Div30: + masterFrequency = 32000000 / 30; + break; + case MasterClockFrequency.Mhz32Div31: + masterFrequency = 32000000 / 31; + break; + case MasterClockFrequency.Mhz32Div32: + masterFrequency = 32000000 / 32; + break; + case MasterClockFrequency.Mhz32Div42: + masterFrequency = 32000000 / 42; + break; + case MasterClockFrequency.Mhz32Div63: + masterFrequency = 32000000 / 63; + break; + case MasterClockFrequency.Mhz32Div125: + masterFrequency = 32000000 / 125; + break; + default: + this.Log(LogLevel.Error, "Wrong CONFIG.MCK value"); + break; + } + SetSampleFrequency(); + } + + private void SetSampleWidth(uint value) + { + // Only 3 values possible: + // 0 - 8 Bit + // 1 - 16 Bit (Default) + // 2 - 32 Bit + if(value > 2) + { + this.Log(LogLevel.Warning, "Sample width set to invalid value : 0x{0:X}. Setting default value.", value); + value = 1; + } + sampleWidth = (uint)(8 * (1 << (int)value)); + samplesPerDoubleWord = 32 / sampleWidth; + SetSampleFrequency(); + } + + private void SetSampleFrequency() + { + if(sampleRatio < 2 * sampleWidth) + { + this.Log(LogLevel.Error, "Invalid CONFIG.RATIO value, it cannot exceed `2* CONFIG.SWIDTH`"); + } + sampleFrequency = GetClosestValue(masterFrequency / sampleRatio, possibleSamplingRates); + this.Log(LogLevel.Debug, "Set sample frequency to {0}Hz, {1}Bit", sampleFrequency, sampleWidth); + } + + private uint GetClosestValue(uint freq, uint[] possibleVals) + { + var closest = possibleVals.OrderBy(x => Math.Abs((long) x - freq)).First(); + return closest; + } + + private void CreateRegisters() + { + Registers.EventEnable.Define(this) + .WithFlag(0, out interruptEnableRxReady, changeCallback: (_, __) => UpdateInterrupts(), name: "RX_READY") + .WithFlag(1, out interruptEnableRxError, changeCallback: (_, __) => UpdateInterrupts(), name: "RX_ERROR") + .WithFlag(2, out interruptEnableTxReady, changeCallback: (_, __) => UpdateInterrupts(), name: "TX_READY") + .WithFlag(3, out interruptEnableTxError, changeCallback: (_, __) => UpdateInterrupts(), name: "TX_ERROR") + ; + Registers.EventStatus.Define(this) + .WithFlag(0, FieldMode.Read, valueProviderCallback: (_) => false, changeCallback: (_, __) => UpdateInterrupts(), name: "RX_READY") + .WithFlag(1, FieldMode.Read, valueProviderCallback: (_) => false, changeCallback: (_, __) => UpdateInterrupts(), name: "RX_ERROR") + .WithFlag(2, FieldMode.Read, valueProviderCallback: (_) => false, changeCallback: (_, __) => UpdateInterrupts(), name: "TX_READY") + .WithFlag(3, FieldMode.Read, valueProviderCallback: (_) => false, changeCallback: (_, __) => UpdateInterrupts(), name: "TX_ERROR") + ; + Registers.EventPending.Define(this) + .WithFlag(0, out eventRxReady, FieldMode.WriteOneToClear, name: "RX_READY") + .WithFlag(1, out eventRxError, FieldMode.WriteOneToClear, name: "RX_ERROR") + .WithFlag(2, out eventTxReady, FieldMode.WriteOneToClear, name: "TX_READY") + .WithFlag(3, out eventTxError, FieldMode.WriteOneToClear, name: "TX_ERROR") + ; + // Registers.TasksStart.Define(this) + // .WithFlag(0, FieldMode.Write, writeCallback: (_, val) => { if(val) Start(); }, name: "TASKS_START") + // .WithReservedBits(1,31); + // Registers.TasksStop.Define(this) + // .WithFlag(0, FieldMode.Write, writeCallback: (_, val) => { if(val) Stop(); }, name: "TASKS_STOP") + // .WithReservedBits(1,31); + // Registers.EventsRxptrUpdated .Define(this) + // .WithFlag(0, out eventRxPointerUpdated, changeCallback: (_, __) => UpdateInterrupts(), name: "EVENTS_RXPTRUPD") + // .WithReservedBits(1,31); + // Registers.EventsStopped.Define(this) + // .WithFlag(0, out eventStopped, changeCallback: (_, __) => UpdateInterrupts(), name: "EVENTS_STOPPED") + // .WithReservedBits(1,31); + // Registers.EventsTxptrUpdated.Define(this) + // .WithFlag(0, out eventTxPointerUpdated, changeCallback: (_, __) => UpdateInterrupts(), name: "EVENTS_TXPTRUPD") + // .WithReservedBits(1,31); + // Registers.InterruptEnable.Define(this) + // .WithReservedBits(0,1) + // .WithFlag(1, out interruptEnableRxPointerUpdated, name: "RXPTRUPD") + // .WithFlag(2, out interruptEnableStopped, name: "STOPPED") + // .WithReservedBits(3,2) + // .WithFlag(5, out interruptEnableTxPointerUpdated, name: "TXPTRUPD") + // .WithReservedBits(6,25); + // Registers.InterruptEnableSet.Define(this) + // .WithReservedBits(0,1) + // .WithFlag(1, + // writeCallback: (_, val) => { interruptEnableRxPointerUpdated.Value |= val; }, + // valueProviderCallback: (_) => { return interruptEnableRxPointerUpdated.Value; }, + // name: "RXPTRUPD") + // .WithFlag(2, + // writeCallback: (_, val) => { interruptEnableStopped.Value |= val; }, + // valueProviderCallback: (_) => { return interruptEnableStopped.Value; }, + // name: "STOPPED") + // .WithReservedBits(3,2) + // .WithFlag(5, + // writeCallback: (_, val) => { interruptEnableTxPointerUpdated.Value |= val; }, + // valueProviderCallback: (_) => { return interruptEnableTxPointerUpdated.Value; }, + // name: "TXPTRUPD") + // .WithReservedBits(6,25); + // Registers.InterruptEnableClear.Define(this) + // .WithReservedBits(0,1) + // .WithFlag(1, + // writeCallback: (_, val) => { interruptEnableRxPointerUpdated.Value &= !val; }, + // valueProviderCallback: (_) => { return interruptEnableRxPointerUpdated.Value; }, + // name: "RXPTRUPD") + // .WithFlag(2, + // writeCallback: (_, val) => { interruptEnableStopped.Value &= !val; }, + // valueProviderCallback: (_) => { return interruptEnableStopped.Value; }, + // name: "STOPPED") + // .WithReservedBits(3,2) + // .WithFlag(5, + // writeCallback: (_, val) => { interruptEnableTxPointerUpdated.Value &= !val; }, + // valueProviderCallback: (_) => { return interruptEnableTxPointerUpdated.Value; }, + // name: "TXPTRUPD") + // .WithReservedBits(6,25); + // Registers.Enable.Define(this) + // .WithFlag(0, out enableI2S, name: "ENABLE") + // .WithReservedBits(1,31); + // Registers.ConfigMode.Define(this) + // .WithValueField(0, 1, + // writeCallback: (_, val) => + // { + // if((Mode)val == Mode.Slave) + // { + // //This requires ability to use master device clock configuration and handling alignment / format properly + // this.Log(LogLevel.Error, "Slave mode unimplemented"); + // } + // }, + // name: "MODE") + // .WithReservedBits(1,31); + // Registers.ConfigRxEnable.Define(this) + // .WithFlag(0, out enableRx, name: "RXEN") + // .WithReservedBits(1,31); + // Registers.ConfigTxEnable.Define(this, 0x1) + // .WithFlag(0, out enableTx, name: "TXEN") + // .WithReservedBits(1,31); + // Registers.ConfigMasterClockEnable.Define(this, 0x1) + // .WithFlag(0, name: "MCKEN") + // .WithReservedBits(1,31); + // Registers.ConfigMasterClockFrequency.Define(this, 0x20000000) + // .WithValueField(0, 32, writeCallback: (_, val) => SetMasterClockFrequency(val), name: "MCKFREQ"); + // Registers.ConfigRatio.Define(this, 0x6) + // .WithValueField(0, 4, writeCallback: (_, val) => SetMasterClockLrckRatio(val), name: "RATIO") + // .WithReservedBits(4,28); + // Registers.ConfigSwidth.Define(this, 0x1) + // .WithValueField(0, 2, writeCallback: (_, val) => SetSampleWidth(val), name: "SWIDTH") + // .WithReservedBits(2,30); + // Registers.ConfigAlign.Define(this) + // .WithTaggedFlag("ALIGN", 0) + // .WithReservedBits(1, 31); + // Registers.ConfigFormat.Define(this) + // .WithTaggedFlag("FORMAT",0) + // .WithReservedBits(1, 31); + // Registers.ConfigChannels.Define(this) + // .WithValueField(0, 2, + // writeCallback: (_, val) => { numberOfChannels = (Channels)val == Channels.Stereo ? 2u : 1u;}, + // name: "CHANNELS") + // .WithReservedBits(2, 30); + // Registers.RxdPointer.Define(this) + // .WithValueField(0, 32, out rxdPointer, name: "PTR"); + // Registers.TxdPointer.Define(this) + // .WithValueField(0, 32, out txdPointer, name: "PTR"); + // Registers.RxTxBufferSize.Define(this) + // .WithValueField(0, 14, out maxSamplesCount, name: "MAXCNT") + // .WithReservedBits(14, 18); + // Registers.PinSelectMasterClock.Define(this, 0xFFFFFFFF) + // .WithTag("PIN", 0, 5) + // .WithTag("PORT", 5, 1) + // .WithReservedBits(6, 25) + // .WithTaggedFlag("CONNECT", 31); + // Registers.PinSelectSCK.Define(this, 0xFFFFFFFF) + // .WithTag("PIN", 0, 5) + // .WithTag("PORT", 5, 1) + // .WithReservedBits(6, 25) + // .WithTaggedFlag("CONNECT", 31); + // Registers.PinSelectLRCK.Define(this, 0xFFFFFFFF) + // .WithTag("PIN", 0, 5) + // .WithTag("PORT", 5, 1) + // .WithReservedBits(6, 25) + // .WithTaggedFlag("CONNECT", 31); + // Registers.PinSelectSDIN.Define(this, 0xFFFFFFFF) + // .WithTag("PIN", 0, 5) + // .WithTag("PORT", 5, 1) + // .WithReservedBits(6, 25) + // .WithTag("CONNECT", 31, 1); + // Registers.PinSelectSDOUT.Define(this, 0xFFFFFFFF) + // .WithValueField(0, 5, name: "PIN") + // .WithValueField(5, 1, name: "PORT") + // .WithReservedBits(6, 25) + // .WithTaggedFlag("CONNECT", 31); + } + + private IFlagRegisterField enableI2S; + private IFlagRegisterField enableRx; + private IFlagRegisterField enableTx; + + private IFlagRegisterField eventRxReady; + private IFlagRegisterField eventRxError; + private IFlagRegisterField eventTxReady; + private IFlagRegisterField eventTxError; + private IFlagRegisterField interruptEnableRxReady; + private IFlagRegisterField interruptEnableRxError; + private IFlagRegisterField interruptEnableTxReady; + private IFlagRegisterField interruptEnableTxError; + + private IValueRegisterField maxSamplesCount; + private IValueRegisterField rxdPointer; + private IValueRegisterField txdPointer; + + private uint masterFrequency; + private uint numberOfChannels; + private uint sampleFrequency; + private uint sampleRatio; + private uint samplesPerDoubleWord; + private uint sampleWidth; + + private IManagedThread rxTxThread; + private PCMDecoder decoder; + private PCMEncoder encoder; + private AudioRam audioRam; + private uint bufferAddress; + + private enum DataFormat + { + Standard = 0, + LeftJustified = 1, + } + private enum Registers :long + { + EventStatus = 0x00, + EventPending = 0x04, + EventEnable = 0x08, + RxCtl = 0x0c, + RxStat = 0x10, + RxConf = 0x14, + TxCtl = 0x18, + TxStat = 0x1c, + TxConf = 0x20, + } + } +} diff --git a/emulation/peripherals/LM3509.cs b/emulation/peripherals/LM3509.cs new file mode 100644 index 000000000..522e3148c --- /dev/null +++ b/emulation/peripherals/LM3509.cs @@ -0,0 +1,53 @@ +// +// Copyright (c) 2010-2021 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// + +using Antmicro.Renode.Logging; +using Antmicro.Renode.Utilities; +using Antmicro.Renode.Peripherals.I2C; + +namespace Antmicro.Renode.Peripherals.Mocks +{ + public class LM3509 : II2CPeripheral + { + public LM3509() + { + Reset(); + } + + public void Write(byte[] data) + { + this.Log(LogLevel.Debug, "Written {0} bytes of data: {1}", data.Length, Misc.PrettyPrintCollectionHex(data)); + buffer = data; + } + + public byte[] Read(int count = 1) + { + this.Log(LogLevel.Debug, "Reading {0} bytes", count); + var result = new byte[count]; + for(var i = 0; i < result.Length; i++) + { + result[i] = (i < buffer.Length) + ? buffer[i] + : (byte)0; + } + + return result; + } + + public void FinishTransmission() + { + this.Log(LogLevel.Debug, "Finishing transmission"); + } + + public void Reset() + { + buffer = new byte[0]; + } + + private byte[] buffer; + } +} diff --git a/emulation/peripherals/LSM6DS3.cs b/emulation/peripherals/LSM6DS3.cs new file mode 100644 index 000000000..d5bd6178a --- /dev/null +++ b/emulation/peripherals/LSM6DS3.cs @@ -0,0 +1,53 @@ +// +// Copyright (c) 2010-2021 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// + +using Antmicro.Renode.Logging; +using Antmicro.Renode.Utilities; +using Antmicro.Renode.Peripherals.I2C; + +namespace Antmicro.Renode.Peripherals.Mocks +{ + public class LSM6DS3 : II2CPeripheral + { + public LSM6DS3() + { + Reset(); + } + + public void Write(byte[] data) + { + this.Log(LogLevel.Debug, "Written {0} bytes of data: {1}", data.Length, Misc.PrettyPrintCollectionHex(data)); + buffer = data; + } + + public byte[] Read(int count = 1) + { + this.Log(LogLevel.Debug, "Reading {0} bytes", count); + var result = new byte[count]; + for(var i = 0; i < result.Length; i++) + { + result[i] = (i < buffer.Length) + ? buffer[i] + : (byte)0; + } + + return result; + } + + public void FinishTransmission() + { + this.Log(LogLevel.Debug, "Finishing transmission"); + } + + public void Reset() + { + buffer = new byte[0]; + } + + private byte[] buffer; + } +} diff --git a/emulation/peripherals/TUSB320LAI.cs b/emulation/peripherals/TUSB320LAI.cs new file mode 100644 index 000000000..ca4a3936a --- /dev/null +++ b/emulation/peripherals/TUSB320LAI.cs @@ -0,0 +1,96 @@ +// +// Copyright (c) 2010-2021 Antmicro +// +// This file is licensed under the MIT License. +// Full license text is available in 'licenses/MIT.txt'. +// + +using Antmicro.Renode.Logging; +using Antmicro.Renode.Utilities; +using Antmicro.Renode.Peripherals.I2C; + +namespace Antmicro.Renode.Peripherals.Mocks +{ + public class TUSB320LAI : II2CPeripheral + { + public TUSB320LAI() + { + Reset(); + } + + public void Write(byte[] data) + { + this.Log(LogLevel.Debug, "Written {0} bytes of data: {1}", data.Length, Misc.PrettyPrintCollectionHex(data)); + if (data.Length == 2) + { + this.lastAddress = data[1]; + } + buffer = data; + } + + public byte[] Read(int count = 1) + { + this.Log(LogLevel.Debug, "Reading {0} bytes", count); + var result = new byte[count]; + + if (lastAddress == 0) + { + result[0] = 0x30; + } + else if (lastAddress == 1) + { + result[0] = 0x32; + } + else if (lastAddress == 2) + { + result[0] = 0x33; + } + else if (lastAddress == 3) + { + result[0] = 0x42; + } + else if (lastAddress == 4) + { + result[0] = 0x53; + } + else if (lastAddress == 5) + { + result[0] = 0x55; + } + else if (lastAddress == 6) + { + result[0] = 0x54; + } + else if (lastAddress == 7) + { + result[0] = 0x00; + } + else + { + this.Log(LogLevel.Error, "Reading register {0} ({1} bytes)", lastAddress, count); + for (var i = 0; i < result.Length; i++) + { + result[i] = (i < buffer.Length) + ? buffer[i] + : (byte)0; + } + } + lastAddress += 1; + + return result; + } + + public void FinishTransmission() + { + this.Log(LogLevel.Debug, "Finishing transmission"); + } + + public void Reset() + { + buffer = new byte[0]; + } + + private byte[] buffer; + private byte lastAddress = 0; + } +} diff --git a/emulation/xous-release.resc b/emulation/xous-release.resc index 682f6b219..a70cbf84a 100644 --- a/emulation/xous-release.resc +++ b/emulation/xous-release.resc @@ -12,17 +12,21 @@ i @peripherals/ABRTCMC.cs i @peripherals/BetrustedEcI2C.cs i @peripherals/BetrustedSocI2C.cs i @peripherals/BetrustedWatchdog.cs +i @peripherals/BQ24157.cs i @peripherals/BQ27421.cs i @peripherals/engine.cs i @peripherals/keyboard.cs i @peripherals/keyrom.cs i @peripherals/LiteX_Timer_32.cs +i @peripherals/LM3509.cs +i @peripherals/LSM6DS3.cs i @peripherals/memlcd.cs i @peripherals/sha512.cs i @peripherals/spinor_soft_int.cs i @peripherals/ticktimer.cs i @peripherals/trng_kernel.cs i @peripherals/trng_server.cs +i @peripherals/TUSB320LAI.cs i @peripherals/wfi.cs ############### Define the Betrusted SoC ############### @@ -80,9 +84,15 @@ mach clear mach create "EC" machine LoadPlatformDescription @ec/betrusted-ec.repl +# logLevel -1 i2c # logLevel -1 i2c.bq27421 +machine StartGdbServer 2222 true showAnalyzer uart + +# Silence the COM bus, which is currently unimplemented +sysbus SilenceRange <0xE0005800, 0xE0005FFF> + macro reset """ sysbus LoadBinary @ec/kernel.bin 0x10000000