Skip to content

Commit

Permalink
Merge pull request #173 from K-Society/experimental
Browse files Browse the repository at this point in the history
Experimental
  • Loading branch information
maniglia authored Dec 26, 2024
2 parents 594eb4b + dcef64c commit 2947e94
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 19 deletions.
80 changes: 66 additions & 14 deletions src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public class CubeProgrammerApi : ICubeProgrammerApi
private readonly object _syncRoot = new object();

private Native.SafeLibraryHandle _handleSTLinkDriver;
private Native.SafeLibraryHandle _handleProgrammer;
//private readonly Dictionary<string, Native.SafeLibraryHandle> _libraryHandleDictionary;
private readonly ILogger<CubeProgrammerApi> _logger;

private const int DisposedFlag = 1;
Expand All @@ -43,6 +45,7 @@ public CubeProgrammerApi(ILogger<CubeProgrammerApi> logger = default)
}

this._logger = logger;
//this._libraryHandleDictionary = new Dictionary<string, Native.SafeLibraryHandle>();
this.Init();
}

Expand All @@ -53,27 +56,65 @@ private void Init()
var currentDirectory = GetAssemblyDirectory();
var target = Path.Combine(currentDirectory, "dll", Environment.Is64BitProcess ? "x64" : "x86");

if (this._handleSTLinkDriver == null)
var files = Directory.GetFiles(target, "*.dll");
//if (this._handleSTLinkDriver == null)
//{
lock (this._syncRoot)
{
lock (this._syncRoot)
foreach (var file in files)
{
if (this._handleSTLinkDriver == null)
this._logger?.LogInformation(file);
// if (!this._libraryHandleDictionary.ContainsKey(file))
// {
// var handle = Native.Utility.LoadNativeLibrary(file, IntPtr.Zero, 0);
// if (handle.IsInvalid)
// {
// var error = Marshal.GetLastWin32Error();
// handle = null;
// this._logger?.LogError("Loading {0} {1} - {2} library error: {3} !", target, file, Environment.Is64BitProcess ? "x64" : "x86", error);
// }
// else
// {
// this._logger?.LogInformation("Loading {0} - {1} library.", file, Environment.Is64BitProcess ? "x64" : "x86");
// this._libraryHandleDictionary.Add(file, handle);
// }
// }
}


if (this._handleSTLinkDriver == null)
{
this._handleSTLinkDriver = Native.Utility.LoadNativeLibrary(target + @"\STLinkUSBDriver.dll", IntPtr.Zero, 0);

if (this._handleSTLinkDriver.IsInvalid)
{
this._handleSTLinkDriver = Native.Utility.LoadNativeLibrary(target + @"\STLinkUSBDriver.dll", IntPtr.Zero, 0);
var error = Marshal.GetLastWin32Error();
this._handleSTLinkDriver = null;
this._logger?.LogError("Loading {0} {1} - {2} library error: {3} !", target, "STLinkUSBDriver.dll", Environment.Is64BitProcess ? "x64" : "x86", error);
}
else
{
this._logger?.LogInformation("Loading {0} - {1} library.", "STLinkUSBDriver.dll", Environment.Is64BitProcess ? "x64" : "x86");
}
}

if (this._handleSTLinkDriver.IsInvalid)
{
var error = Marshal.GetLastWin32Error();
this._handleSTLinkDriver = null;
this._logger?.LogError("Loading {0} {1} - {2} library error: {3} !", target, "STLinkUSBDriver.dll", Environment.Is64BitProcess ? "x64" : "x86", error);
}
else
{
this._logger?.LogInformation("Loading {0} - {1} library.", "STLinkUSBDriver.dll", Environment.Is64BitProcess ? "x64" : "x86");
}
if (this._handleProgrammer == null)
{
this._handleProgrammer = Native.Utility.LoadNativeLibrary(target + @"\Programmer.dll", IntPtr.Zero, 0);

if (this._handleProgrammer.IsInvalid)
{
var error = Marshal.GetLastWin32Error();
this._handleProgrammer = null;
this._logger?.LogError("Loading {0} {1} - {2} library error: {3} !", target, "Programmer.dll", Environment.Is64BitProcess ? "x64" : "x86", error);
}
else
{
this._logger?.LogInformation("Loading {0} - {1} library.", "Programmer.dll", Environment.Is64BitProcess ? "x64" : "x86");
}
}
}
//}
}

private static string GetAssemblyDirectory()
Expand Down Expand Up @@ -1668,6 +1709,17 @@ protected void Dispose(bool disposing)
// Free any unmanaged objects here.
this._handleSTLinkDriver?.Dispose();
this._handleSTLinkDriver = null;

this._handleProgrammer?.Dispose();
this._handleProgrammer = null;
//if (this._libraryHandleDictionary.Count != 0)
//{
// foreach (var libraryHandle in this._libraryHandleDictionary)
// {
// libraryHandle.Value.ReleaseHandle();
// libraryHandle.Value = null;
// }
//}
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions src/01/KSociety.SharpCubeProgrammer/Enum/FlashSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ namespace SharpCubeProgrammer.Enum
{
public enum FlashSize
{
FlashSize1Kb = (1024),
FlashSize512Kb = (512 * 1024),
FlashSize256Kb = (256 * 1024),
FlashSize1Kb = 1024,
FlashSize512Kb = 512 * 1024,
FlashSize256Kb = 256 * 1024,
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ namespace SharpCubeProgrammer.Native
{
using System;
using System.Runtime.InteropServices;
using System.Text;
using Enum;
using Struct;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ namespace SharpCubeProgrammer.Struct
public struct DfuConnectParameters
{
public string usb_index;
public string rdu;

/// <summary>
/// Request a read unprotect: value in {0,1}.
/// </summary>
public string rdu;

/// <summary>
/// Request a TZEN regression: value in {0,1}.
/// </summary>
public string tzenreg;
Expand Down

0 comments on commit 2947e94

Please sign in to comment.