Skip to content

Commit

Permalink
Completed implementation (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
BWalti authored Oct 31, 2022
1 parent 775d105 commit 44df170
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 97 deletions.
1 change: 0 additions & 1 deletion System.Device.I2s/I2sBitsPerSample.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,5 @@ public enum I2sBitsPerSample
/// I2S bits per sample: 32-bits
/// </summary>
Bit32 = 32

}
}
23 changes: 0 additions & 23 deletions System.Device.I2s/I2sChannel.cs

This file was deleted.

21 changes: 8 additions & 13 deletions System.Device.I2s/I2sCommunicationFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,26 @@ public enum I2sCommunicationFormat
/// <summary>
/// I2S communication I2S Philips standard, data launch at second BCK
/// </summary>
StandardI2s = 0X01,

/// <summary>
/// I2S format LSB, (I2S_COMM_FORMAT_I2S |I2S_COMM_FORMAT_I2S_LSB) correspond to I2S_MSB
/// </summary>
Lsb = 0X02,
I2S = 0x01,

/// <summary>
/// I2S communication MSB alignment standard, data launch at first BCK
/// </summary>
Msb = 0X03,
Msb = 0x03,

/// <summary>
/// PCM Short standard, also known as DSP mode. The period of synchronization signal (WS) is 1 bck cycle.
/// </summary>
PcmShort = 0X04,
PcmShort = 0x04,

/// <summary>
/// PCM Long standard. The period of synchronization signal (WS) is channel_bit*bck cycles.
/// PCM Long standard. The period of synchronization signal (WS) is channel_bit * bck cycles.
/// </summary>
PcmLong = 0X0C,
PcmLong = 0x0C,

/// <summary>
/// standard max
/// Standard max
/// </summary>
StandMax,
Max
}
}
}
18 changes: 17 additions & 1 deletion System.Device.I2s/I2sConnectionSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public sealed class I2sConnectionSettings

private I2sChannelFormat _i2sChannelFormat = I2sChannelFormat.RightLeft;

private I2sCommunicationFormat _i2sConnectionFormat = I2sCommunicationFormat.StandardI2s;
private I2sCommunicationFormat _i2sConnectionFormat = I2sCommunicationFormat.I2S;

private int _bufferSize = 10000;

/// <summary>
/// Initializes new instance of I2sConnectionSettings.
Expand All @@ -49,6 +51,7 @@ internal I2sConnectionSettings(I2sConnectionSettings other)
CommunicationFormat = other.CommunicationFormat;
ChannelFormat = other.ChannelFormat;
BitsPerSample = other.BitsPerSample;
BufferSize = other.BufferSize;
}

/// <summary>
Expand Down Expand Up @@ -128,5 +131,18 @@ public int SampleRate
_sampleRate = value;
}
}

/// <summary>
/// Buffer Size used to define the DMA buffer.
/// This size is required to calculate the right amount of <c>dma_buf_count</c>.
/// </summary>
/// <remarks>
/// Defaults to <c>10000</c>.
/// </remarks>
public int BufferSize
{
get => _bufferSize;
set => _bufferSize = value;
}
}
}
64 changes: 23 additions & 41 deletions System.Device.I2s/I2sDevice.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//

using System.Diagnostics;
using System.Runtime.CompilerServices;

namespace System.Device.I2s
Expand All @@ -11,27 +13,29 @@ namespace System.Device.I2s
/// </summary>
public class I2sDevice : IDisposable
{
[Diagnostics.DebuggerBrowsable(Diagnostics.DebuggerBrowsableState.Never)]
private readonly I2sConnectionSettings _connectionSettings;

[Diagnostics.DebuggerBrowsable(Diagnostics.DebuggerBrowsableState.Never)]
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private bool _disposed;

private readonly I2sConnectionSettings _connectionSettings;

/// <summary>
/// The connection settings of a device on an I2s bus. The connection settings are immutable after the device is created
/// so the object returned will be a clone of the settings object.
/// Create an I2s Device
/// </summary>
public I2sConnectionSettings ConnectionSettings { get => _connectionSettings; }
/// <param name="settings">Connection settings</param>
public I2sDevice(I2sConnectionSettings settings)
{
_connectionSettings = settings;

// call native init to allow HAL/PAL inits related with I2s hardware
NativeInit();
}

/// <summary>
/// Reads data from the I2s device.
/// The connection settings of a device on an I2s bus. The connection settings are immutable after the device is
/// created
/// so the object returned will be a clone of the settings object.
/// </summary>
/// <param name="buffer">
/// The buffer to read the data from the I2s device.
/// The length of the buffer determines how much data to read from the I2s device.
/// </param>
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void Read(SpanByte buffer);
public I2sConnectionSettings ConnectionSettings => _connectionSettings;

/// <summary>
/// Reads data from the I2s device.
Expand All @@ -41,17 +45,7 @@ public class I2sDevice : IDisposable
/// The length of the buffer determines how much data to read from the I2s device.
/// </param>
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void Read(ushort[] buffer);

/// <summary>
/// Writes a byte to the I2s device.
/// </summary>
/// <param name="buffer">
/// The buffer that contains the data to be written to the I2s device.
/// The data should not include the I2s device address.
/// </param>
[MethodImpl(MethodImplOptions.InternalCall)]
public extern void Write(ushort[] buffer);
public extern void Read(SpanByte buffer);

/// <summary>
/// Writes data to the I2s device.
Expand All @@ -73,18 +67,6 @@ public static I2sDevice Create(I2sConnectionSettings settings)
return new I2sDevice(settings);
}

/// <summary>
/// Create an I2s Device
/// </summary>
/// <param name="settings">Connection settings</param>
public I2sDevice(I2sConnectionSettings settings)
{
_connectionSettings = settings;

// call native init to allow HAL/PAL inits related with I2s hardware
NativeInit();
}

#region IDisposable Support

private void Dispose(bool disposing)
Expand All @@ -104,7 +86,7 @@ private void Dispose(bool disposing)
}

/// <summary>
/// <inheritdoc cref="IDisposable.Dispose"/>
/// <inheritdoc cref="IDisposable.Dispose" />
/// </summary>
public void Dispose()
{
Expand Down
19 changes: 10 additions & 9 deletions System.Device.I2s/I2sMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,42 +8,43 @@ namespace System.Device.I2s
/// <summary>
/// Defines how data is synchronized between devices on a I2s bus.
/// </summary>
[Flags]
public enum I2sMode
{
/// <summary>
/// Master mode
/// </summary>
Master = 1,
Master = 1 << 0,

/// <summary>
/// Slave mode
/// </summary>
Slave = 2,
Slave = 1 << 1,

/// <summary>
/// TX mode
/// </summary>
Tx = 4,
Tx = 1 << 2,

/// <summary>
/// RX mode
/// </summary>
Rx = 8,
Rx = 1 << 3,

/// <summary>
/// Output I2S data to built-in DAC, no matter the data format is 16bit or 32 bit, the DAC module will only take the 8bits from MSB
/// Output I2S data to built-in DAC, no matter the data format is 16bit or 32 bit, the DAC module will only take the
/// 8bits from MSB
/// </summary>
DacBuiltIn = 16,
DacBuiltIn = 1 << 4,

/// <summary>
/// Input I2S data from built-in ADC, each data can be 12-bit width at most
/// </summary>
AdcBuiltIn = 32,
AdcBuiltIn = 1 << 5,

/// <summary>
/// PDM mode
/// </summary>
Pdm = 64

Pdm = 1 << 6
}
}
8 changes: 5 additions & 3 deletions System.Device.I2s/System.Device.I2s.nfproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
<ItemGroup>
<Compile Include="I2sBitsPerSample.cs" />
<Compile Include="I2sChannel.cs" />
<Compile Include="I2sChannelFormat.cs" />
<Compile Include="I2sCommunicationFormat.cs" />
<Compile Include="I2sConnectionSettings.cs" />
Expand All @@ -59,17 +58,20 @@
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Content Include="packages.lock.json" />
</ItemGroup>
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
<ProjectExtensions>
<ProjectCapabilities>
<ProjectConfigurationsDeclaredAsItems />
</ProjectCapabilities>
</ProjectExtensions>
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" />
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets')" />
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
<PropertyGroup>
<ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText>
</PropertyGroup>
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets'))" />
<Error Condition="!Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets'))" />
</Target>
</Project>
2 changes: 1 addition & 1 deletion System.Device.I2s/packages.config
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnanoframework10" />
<package id="Nerdbank.GitVersioning" version="3.5.107" developmentDependency="true" targetFramework="netnanoframework10" />
<package id="Nerdbank.GitVersioning" version="3.5.113" developmentDependency="true" targetFramework="netnano1.0" />
</packages>
6 changes: 3 additions & 3 deletions System.Device.I2s/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
},
"Nerdbank.GitVersioning": {
"type": "Direct",
"requested": "[3.5.107, 3.5.107]",
"resolved": "3.5.107",
"contentHash": "oMWWTe9aTUJ1CwU4fpfpduqx7Hzve320PU2SUyFFDzlHXTdjEouWHtFrRbaXV4LysL0lBtlzJM/nmHm47p2KWw=="
"requested": "[3.5.113, 3.5.113]",
"resolved": "3.5.113",
"contentHash": "4fBSMkqhi410qlkjPm+Mxfk8iO3C7dmgdVS7ljsfVO21WEzZCHP1VCOqB6rlOPfPidR/oxX+/Do/I7meCAz+Jg=="
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion nanoFramework.System.Device.I2s.DELIVERABLES.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<tags>
</tags>
<dependencies>
<dependency id="nanoFramework.CoreLibrary" version="1.10.5" />
<dependency id="nanoFramework.CoreLibrary" version="1.12.0" />
</dependencies>
</metadata>
<files>
Expand Down
2 changes: 1 addition & 1 deletion nanoFramework.System.Device.I2s.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
<description>This package includes the System.Device.I2s assembly for .NET nanoFramework C# projects.&#10;This package requires a target with System.Device.I2s v$nativeVersion$ (checksum $checksum$).</description>
<tags>nanoFramework C# csharp netmf netnf System.Device.I2s</tags>
<dependencies>
<dependency id="nanoFramework.CoreLibrary" version="1.10.3-preview.7" />
<dependency id="nanoFramework.CoreLibrary" version="1.12.0" />
</dependencies>
</metadata>
<files>
Expand Down

0 comments on commit 44df170

Please sign in to comment.