Skip to content

Commit

Permalink
Merge pull request #131 from K-Society/experimental
Browse files Browse the repository at this point in the history
Experimental
  • Loading branch information
maniglia authored Mar 11, 2024
2 parents 1fb0c36 + f068f5c commit 21e4f04
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 98 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Please make sure you have updated the firmware of your ST-LINK V2 / V3, you can
The STM32 CubeProgrammer_API is a C-library, created by ST for ST-Link access to micro-controllers
for the purpose of flash downloads or general memory access.
The ST-Link drivers is required, and can be downloaded from st.com and installed [(STSW-LINK009)](https://www.st.com/en/development-tools/stsw-link009.html).
This has been tested on Windows 10, you don't need to install STM32CubeProgrammer.
This has been tested on Windows 10 and Windows 11, you don't need to install STM32CubeProgrammer.

### KSociety.SharpCubeProgrammer
STM32CubeProgrammer_API C# wrapper, the first wrapper for C#. Any suggestions are welcome.
Expand Down Expand Up @@ -260,5 +260,4 @@ The project is under Microsoft Reciprocal License [(MS-RL)](http://www.opensourc

List of technologies, frameworks and libraries used for implementation:

- [Microsoft.Bcl.AsyncInterfaces](https://www.nuget.org/packages/Microsoft.Bcl.AsyncInterfaces) for .NET Standard 2.0 only.
- [Microsoft.Extensions.Logging.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Abstractions)
3 changes: 1 addition & 2 deletions docs/KSociety.SharpCubeProgrammer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ Please make sure you have updated the firmware of your ST-LINK V2 / V3, you can
The STM32 CubeProgrammer_API is a C-library, created by ST for ST-Link access to micro-controllers
for the purpose of flash downloads or general memory access.
The ST-Link drivers is required, and can be downloaded from st.com and installed [(STSW-LINK009)](https://www.st.com/en/development-tools/stsw-link009.html).
This has been tested on Windows 10, you don't need to install STM32CubeProgrammer.
This has been tested on Windows 10 and Windows 11, you don't need to install STM32CubeProgrammer.

### KSociety.SharpCubeProgrammer
STM32CubeProgrammer_API C# wrapper, the first wrapper for C#. Any suggestions are welcome.
Expand Down Expand Up @@ -255,5 +255,4 @@ The project is under Microsoft Reciprocal License [(MS-RL)](http://www.opensourc

List of technologies, frameworks and libraries used for implementation:

- [Microsoft.Bcl.AsyncInterfaces](https://www.nuget.org/packages/Microsoft.Bcl.AsyncInterfaces) for .NET Standard 2.0 only.
- [Microsoft.Extensions.Logging.Abstractions](https://www.nuget.org/packages/Microsoft.Extensions.Logging.Abstractions)
77 changes: 72 additions & 5 deletions src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,20 @@ namespace SharpCubeProgrammer
{
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using DeviceDataStructure;
using Enum;
using Interface;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions;
using Struct;
using Util;

public class CubeProgrammerApi : Disposable, ICubeProgrammerApi
public class CubeProgrammerApi : ICubeProgrammerApi
{
/// <summary>
/// Synchronization object to protect loading the native library and its functions. This field is read-only.
Expand All @@ -25,6 +27,9 @@ public class CubeProgrammerApi : Disposable, ICubeProgrammerApi
private Native.SafeLibraryHandle _handle;
private readonly ILogger<CubeProgrammerApi> _logger;

private const int DisposedFlag = 1;
private int _isDisposed;

#region [Constructor]

public CubeProgrammerApi(ILogger<CubeProgrammerApi> logger = default)
Expand Down Expand Up @@ -1517,8 +1522,27 @@ private CubeProgrammerError CheckResult(int result)

#region [Dispose]

/// <inheritdoc/>
protected override void Dispose(bool disposing)
/// <summary>
/// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
/// </summary>
[SuppressMessage("Microsoft.Design", "CA1063:ImplementIDisposableCorrectly", Justification = "Dispose is implemented correctly, FxCop just doesn't see it.")]
public void Dispose()
{
var wasDisposed = Interlocked.Exchange(ref this._isDisposed, DisposedFlag);
if (wasDisposed == DisposedFlag)
{
return;
}

this.Dispose(true);
GC.SuppressFinalize(this);
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected void Dispose(bool disposing)
{
if (disposing)
{
Expand All @@ -1528,8 +1552,51 @@ protected override void Dispose(bool disposing)
// Free any unmanaged objects here.
this._handle?.Dispose();
this._handle = null;
}

/// <summary>
/// Gets a value indicating whether the current instance has been disposed.
/// </summary>
protected bool IsDisposed
{
get
{
Interlocked.MemoryBarrier();
return this._isDisposed == DisposedFlag;
}
}

/// <inheritdoc/>
[SuppressMessage(
"Usage",
"CA1816:Dispose methods should call SuppressFinalize",
Justification = "DisposeAsync should also call SuppressFinalize (see various .NET internal implementations).")]
public ValueTask DisposeAsync()
{
// Still need to check if we've already disposed; can't do both.
var wasDisposed = Interlocked.Exchange(ref this._isDisposed, DisposedFlag);
if (wasDisposed != DisposedFlag)
{
GC.SuppressFinalize(this);

// Always true, but means we get the similar syntax as Dispose,
// and separates the two overloads.
return this.DisposeAsync(true);
}

return default;
}

/// <summary>
/// Releases unmanaged and - optionally - managed resources, asynchronously.
/// </summary>
/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
protected ValueTask DisposeAsync(bool disposing)
{
// Default implementation does a synchronous dispose.
this.Dispose(disposing);

base.Dispose(disposing);
return default;
}

#endregion
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Bcl.AsyncInterfaces" Version="8.0.0" Condition="'$(TargetFramework)' == 'netstandard2.0'" />
<PackageReference Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0">
<PrivateAssets>all</PrivateAssets>
Expand Down
87 changes: 0 additions & 87 deletions src/01/KSociety.SharpCubeProgrammer/Util/Disposable.cs

This file was deleted.

2 changes: 1 addition & 1 deletion src/Directory.csproj.props
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<Project ToolsVersion="Current" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
<TargetFrameworks>netstandard2.0</TargetFrameworks>

<AssemblyOriginatorKeyFile>$([System.IO.Path]::GetFullPath($(MSBuildThisFileDirectory)ksociety.snk))</AssemblyOriginatorKeyFile>
<SignAssembly>true</SignAssembly>
Expand Down

0 comments on commit 21e4f04

Please sign in to comment.