Skip to content

Commit 44df170

Browse files
authored
Completed implementation (#3)
1 parent 775d105 commit 44df170

11 files changed

+69
-97
lines changed

System.Device.I2s/I2sBitsPerSample.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ public enum I2sBitsPerSample
2929
/// I2S bits per sample: 32-bits
3030
/// </summary>
3131
Bit32 = 32
32-
3332
}
3433
}

System.Device.I2s/I2sChannel.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

System.Device.I2s/I2sCommunicationFormat.cs

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,31 +13,26 @@ public enum I2sCommunicationFormat
1313
/// <summary>
1414
/// I2S communication I2S Philips standard, data launch at second BCK
1515
/// </summary>
16-
StandardI2s = 0X01,
17-
18-
/// <summary>
19-
/// I2S format LSB, (I2S_COMM_FORMAT_I2S |I2S_COMM_FORMAT_I2S_LSB) correspond to I2S_MSB
20-
/// </summary>
21-
Lsb = 0X02,
16+
I2S = 0x01,
2217

2318
/// <summary>
2419
/// I2S communication MSB alignment standard, data launch at first BCK
2520
/// </summary>
26-
Msb = 0X03,
21+
Msb = 0x03,
2722

2823
/// <summary>
2924
/// PCM Short standard, also known as DSP mode. The period of synchronization signal (WS) is 1 bck cycle.
3025
/// </summary>
31-
PcmShort = 0X04,
26+
PcmShort = 0x04,
3227

3328
/// <summary>
34-
/// PCM Long standard. The period of synchronization signal (WS) is channel_bit*bck cycles.
29+
/// PCM Long standard. The period of synchronization signal (WS) is channel_bit * bck cycles.
3530
/// </summary>
36-
PcmLong = 0X0C,
31+
PcmLong = 0x0C,
3732

3833
/// <summary>
39-
/// standard max
34+
/// Standard max
4035
/// </summary>
41-
StandMax,
36+
Max
4237
}
43-
}
38+
}

System.Device.I2s/I2sConnectionSettings.cs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ public sealed class I2sConnectionSettings
2222

2323
private I2sChannelFormat _i2sChannelFormat = I2sChannelFormat.RightLeft;
2424

25-
private I2sCommunicationFormat _i2sConnectionFormat = I2sCommunicationFormat.StandardI2s;
25+
private I2sCommunicationFormat _i2sConnectionFormat = I2sCommunicationFormat.I2S;
26+
27+
private int _bufferSize = 10000;
2628

2729
/// <summary>
2830
/// Initializes new instance of I2sConnectionSettings.
@@ -49,6 +51,7 @@ internal I2sConnectionSettings(I2sConnectionSettings other)
4951
CommunicationFormat = other.CommunicationFormat;
5052
ChannelFormat = other.ChannelFormat;
5153
BitsPerSample = other.BitsPerSample;
54+
BufferSize = other.BufferSize;
5255
}
5356

5457
/// <summary>
@@ -128,5 +131,18 @@ public int SampleRate
128131
_sampleRate = value;
129132
}
130133
}
134+
135+
/// <summary>
136+
/// Buffer Size used to define the DMA buffer.
137+
/// This size is required to calculate the right amount of <c>dma_buf_count</c>.
138+
/// </summary>
139+
/// <remarks>
140+
/// Defaults to <c>10000</c>.
141+
/// </remarks>
142+
public int BufferSize
143+
{
144+
get => _bufferSize;
145+
set => _bufferSize = value;
146+
}
131147
}
132148
}

System.Device.I2s/I2sDevice.cs

Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
2-
// The .NET Foundation licenses this file to you under the MIT license.
3-
// See the LICENSE file in the project root for more information.
1+
//
2+
// Copyright (c) .NET Foundation and Contributors
3+
// See LICENSE file in the project root for full license information.
4+
//
45

6+
using System.Diagnostics;
57
using System.Runtime.CompilerServices;
68

79
namespace System.Device.I2s
@@ -11,27 +13,29 @@ namespace System.Device.I2s
1113
/// </summary>
1214
public class I2sDevice : IDisposable
1315
{
14-
[Diagnostics.DebuggerBrowsable(Diagnostics.DebuggerBrowsableState.Never)]
15-
private readonly I2sConnectionSettings _connectionSettings;
16-
17-
[Diagnostics.DebuggerBrowsable(Diagnostics.DebuggerBrowsableState.Never)]
16+
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
1817
private bool _disposed;
1918

19+
private readonly I2sConnectionSettings _connectionSettings;
20+
2021
/// <summary>
21-
/// The connection settings of a device on an I2s bus. The connection settings are immutable after the device is created
22-
/// so the object returned will be a clone of the settings object.
22+
/// Create an I2s Device
2323
/// </summary>
24-
public I2sConnectionSettings ConnectionSettings { get => _connectionSettings; }
24+
/// <param name="settings">Connection settings</param>
25+
public I2sDevice(I2sConnectionSettings settings)
26+
{
27+
_connectionSettings = settings;
28+
29+
// call native init to allow HAL/PAL inits related with I2s hardware
30+
NativeInit();
31+
}
2532

2633
/// <summary>
27-
/// Reads data from the I2s device.
34+
/// The connection settings of a device on an I2s bus. The connection settings are immutable after the device is
35+
/// created
36+
/// so the object returned will be a clone of the settings object.
2837
/// </summary>
29-
/// <param name="buffer">
30-
/// The buffer to read the data from the I2s device.
31-
/// The length of the buffer determines how much data to read from the I2s device.
32-
/// </param>
33-
[MethodImpl(MethodImplOptions.InternalCall)]
34-
public extern void Read(SpanByte buffer);
38+
public I2sConnectionSettings ConnectionSettings => _connectionSettings;
3539

3640
/// <summary>
3741
/// Reads data from the I2s device.
@@ -41,17 +45,7 @@ public class I2sDevice : IDisposable
4145
/// The length of the buffer determines how much data to read from the I2s device.
4246
/// </param>
4347
[MethodImpl(MethodImplOptions.InternalCall)]
44-
public extern void Read(ushort[] buffer);
45-
46-
/// <summary>
47-
/// Writes a byte to the I2s device.
48-
/// </summary>
49-
/// <param name="buffer">
50-
/// The buffer that contains the data to be written to the I2s device.
51-
/// The data should not include the I2s device address.
52-
/// </param>
53-
[MethodImpl(MethodImplOptions.InternalCall)]
54-
public extern void Write(ushort[] buffer);
48+
public extern void Read(SpanByte buffer);
5549

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

76-
/// <summary>
77-
/// Create an I2s Device
78-
/// </summary>
79-
/// <param name="settings">Connection settings</param>
80-
public I2sDevice(I2sConnectionSettings settings)
81-
{
82-
_connectionSettings = settings;
83-
84-
// call native init to allow HAL/PAL inits related with I2s hardware
85-
NativeInit();
86-
}
87-
8870
#region IDisposable Support
8971

9072
private void Dispose(bool disposing)
@@ -104,7 +86,7 @@ private void Dispose(bool disposing)
10486
}
10587

10688
/// <summary>
107-
/// <inheritdoc cref="IDisposable.Dispose"/>
89+
/// <inheritdoc cref="IDisposable.Dispose" />
10890
/// </summary>
10991
public void Dispose()
11092
{

System.Device.I2s/I2sMode.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,42 +8,43 @@ namespace System.Device.I2s
88
/// <summary>
99
/// Defines how data is synchronized between devices on a I2s bus.
1010
/// </summary>
11+
[Flags]
1112
public enum I2sMode
1213
{
1314
/// <summary>
1415
/// Master mode
1516
/// </summary>
16-
Master = 1,
17+
Master = 1 << 0,
1718

1819
/// <summary>
1920
/// Slave mode
2021
/// </summary>
21-
Slave = 2,
22+
Slave = 1 << 1,
2223

2324
/// <summary>
2425
/// TX mode
2526
/// </summary>
26-
Tx = 4,
27+
Tx = 1 << 2,
2728

2829
/// <summary>
2930
/// RX mode
3031
/// </summary>
31-
Rx = 8,
32+
Rx = 1 << 3,
3233

3334
/// <summary>
34-
/// 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
35+
/// Output I2S data to built-in DAC, no matter the data format is 16bit or 32 bit, the DAC module will only take the
36+
/// 8bits from MSB
3537
/// </summary>
36-
DacBuiltIn = 16,
38+
DacBuiltIn = 1 << 4,
3739

3840
/// <summary>
3941
/// Input I2S data from built-in ADC, each data can be 12-bit width at most
4042
/// </summary>
41-
AdcBuiltIn = 32,
43+
AdcBuiltIn = 1 << 5,
4244

4345
/// <summary>
4446
/// PDM mode
4547
/// </summary>
46-
Pdm = 64
47-
48+
Pdm = 1 << 6
4849
}
4950
}

System.Device.I2s/System.Device.I2s.nfproj

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@
4242
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.props" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.props')" />
4343
<ItemGroup>
4444
<Compile Include="I2sBitsPerSample.cs" />
45-
<Compile Include="I2sChannel.cs" />
4645
<Compile Include="I2sChannelFormat.cs" />
4746
<Compile Include="I2sCommunicationFormat.cs" />
4847
<Compile Include="I2sConnectionSettings.cs" />
@@ -59,17 +58,20 @@
5958
<HintPath>..\packages\nanoFramework.CoreLibrary.1.12.0\lib\mscorlib.dll</HintPath>
6059
</Reference>
6160
</ItemGroup>
61+
<ItemGroup>
62+
<Content Include="packages.lock.json" />
63+
</ItemGroup>
6264
<Import Project="$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets" Condition="Exists('$(NanoFrameworkProjectSystemPath)NFProjectSystem.CSharp.targets')" />
6365
<ProjectExtensions>
6466
<ProjectCapabilities>
6567
<ProjectConfigurationsDeclaredAsItems />
6668
</ProjectCapabilities>
6769
</ProjectExtensions>
68-
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.107\build\Nerdbank.GitVersioning.targets')" />
70+
<Import Project="..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets" Condition="Exists('..\packages\Nerdbank.GitVersioning.3.5.113\build\Nerdbank.GitVersioning.targets')" />
6971
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
7072
<PropertyGroup>
7173
<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>
7274
</PropertyGroup>
73-
<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'))" />
75+
<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'))" />
7476
</Target>
7577
</Project>

System.Device.I2s/packages.config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
33
<package id="nanoFramework.CoreLibrary" version="1.12.0" targetFramework="netnanoframework10" />
4-
<package id="Nerdbank.GitVersioning" version="3.5.107" developmentDependency="true" targetFramework="netnanoframework10" />
4+
<package id="Nerdbank.GitVersioning" version="3.5.113" developmentDependency="true" targetFramework="netnano1.0" />
55
</packages>

System.Device.I2s/packages.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
},
1111
"Nerdbank.GitVersioning": {
1212
"type": "Direct",
13-
"requested": "[3.5.107, 3.5.107]",
14-
"resolved": "3.5.107",
15-
"contentHash": "oMWWTe9aTUJ1CwU4fpfpduqx7Hzve320PU2SUyFFDzlHXTdjEouWHtFrRbaXV4LysL0lBtlzJM/nmHm47p2KWw=="
13+
"requested": "[3.5.113, 3.5.113]",
14+
"resolved": "3.5.113",
15+
"contentHash": "4fBSMkqhi410qlkjPm+Mxfk8iO3C7dmgdVS7ljsfVO21WEzZCHP1VCOqB6rlOPfPidR/oxX+/Do/I7meCAz+Jg=="
1616
}
1717
}
1818
}

nanoFramework.System.Device.I2s.DELIVERABLES.nuspec

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<tags>
1919
</tags>
2020
<dependencies>
21-
<dependency id="nanoFramework.CoreLibrary" version="1.10.5" />
21+
<dependency id="nanoFramework.CoreLibrary" version="1.12.0" />
2222
</dependencies>
2323
</metadata>
2424
<files>

0 commit comments

Comments
 (0)