diff --git a/README.md b/README.md index fc342d2..32adff8 100644 --- a/README.md +++ b/README.md @@ -151,7 +151,7 @@ builder.RegisterModule(new Bindings.ProgrammerApi()); ```csharp var stLinkList = _cubeProgrammerApi.GetStLinkList(); -var stLink = (KSociety.SharpCubeProgrammer.Struct.DebugConnectParameters)stLinkList.First().Clone(); +var stLink = stLinkList.First(); var connectionResult = _cubeProgrammerApi.ConnectStLink(stLink); ``` diff --git a/docs/KSociety.SharpCubeProgrammer/README.md b/docs/KSociety.SharpCubeProgrammer/README.md index 0bad86c..35b5d9f 100644 --- a/docs/KSociety.SharpCubeProgrammer/README.md +++ b/docs/KSociety.SharpCubeProgrammer/README.md @@ -150,7 +150,7 @@ builder.RegisterModule(new Bindings.ProgrammerApi()); ```csharp var stLinkList = _cubeProgrammerApi.GetStLinkList(); -var stLink = (KSociety.SharpCubeProgrammer.Struct.DebugConnectParameters)stLinkList.First().Clone(); +var stLink = stLinkList.First(); var connectionResult = _cubeProgrammerApi.ConnectStLink(stLink); ``` diff --git a/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs index e3b08db..1ee0470 100644 --- a/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs @@ -262,10 +262,10 @@ public IEnumerable GetStLinkEnumerationList(bool shared { this._logger?.LogError(ex, "GetStLinkEnumerationList: "); } - finally - { - Marshal.FreeCoTaskMem(listPtr);//.FreeHGlobal(listPtr); - } + //finally + //{ + // Marshal.FreeCoTaskMem(listPtr);//.FreeHGlobal(listPtr); + //} return parametersList; } @@ -1093,54 +1093,105 @@ public void SetLoadersPath(string path) } /// - public ExternalLoader SetExternalLoaderPath(string path) + public DeviceExternalLoader? SetExternalLoaderPath(string path) { var pathAdapted = path.Replace(@"\", "/"); - var externalLoaderStructure = new ExternalLoader(); var externalLoaderPtr = new IntPtr(); + var deviceSectorSize = Marshal.SizeOf(); + var output = new DeviceExternalLoader(); try { Native.ProgrammerApi.SetExternalLoaderPath(pathAdapted, ref externalLoaderPtr); if (externalLoaderPtr != IntPtr.Zero) { - externalLoaderStructure = Marshal.PtrToStructure(externalLoaderPtr); + var externalLoaderStructure = Marshal.PtrToStructure(externalLoaderPtr); + + output.filePath = externalLoaderStructure.filePath; + output.deviceName = externalLoaderStructure.deviceName; + output.deviceType = externalLoaderStructure.deviceType; + output.deviceStartAddress = externalLoaderStructure.deviceStartAddress; + output.deviceSize = externalLoaderStructure.deviceSize; + output.pageSize = externalLoaderStructure.pageSize; + output.sectorsTypeNbr = externalLoaderStructure.sectorsTypeNbr; + + if (externalLoaderStructure.sectors != IntPtr.Zero) + { + var deviceSectorList = new List(); + for (var i = 0; i < externalLoaderStructure.sectorsTypeNbr; i++) + { + var deviceSectorItem = Marshal.PtrToStructure(externalLoaderStructure.sectors + (i * deviceSectorSize)); + deviceSectorList.Add(deviceSectorItem); + } + + output.sectors = deviceSectorList; + + return output; + } } } catch (Exception ex) { this._logger?.LogError(ex, "SetExternalLoaderPath: "); } - //finally - //{ - // if (externalLoaderPtr != IntPtr.Zero) - // { - // Marshal.DestroyStructure(externalLoaderPtr); - // } - //} - return externalLoaderStructure; + return null; } /// - public IEnumerable GetExternalLoaders(string path = @".\st\Programmer") + public DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer") { var pathAdapted = path.Replace(@"\", "/"); - var externalLoaderList = new List(); + var externalLoaderList = new List(); var externalStorageInfoPtr = new IntPtr(); + var deviceSectorSize = Marshal.SizeOf(); + var output = new DeviceExternalStorageInfo(); try { var result = Native.ProgrammerApi.GetExternalLoaders(pathAdapted, ref externalStorageInfoPtr); if (result.Equals(0)) { - var size = Marshal.SizeOf(); + var externalLoaderSize = Marshal.SizeOf(); var externalStorageInfoStructure = Marshal.PtrToStructure(externalStorageInfoPtr); - for (var i = 0; i < externalStorageInfoStructure.ExternalLoaderNbr; i++) + + output.ExternalLoaderNbr = externalStorageInfoStructure.ExternalLoaderNbr; + + if (externalStorageInfoStructure.ExternalLoader != IntPtr.Zero) { - var currentItem = Marshal.PtrToStructure(externalStorageInfoStructure.ExternalLoader + (i * size)); - //var deviceSectors = Marshal.PtrToStructure(currentItem.sectors); - externalLoaderList.Add(currentItem); + for (var i = 0; i < externalStorageInfoStructure.ExternalLoaderNbr; i++) + { + var externalLoaderStructure = Marshal.PtrToStructure(externalStorageInfoStructure.ExternalLoader + (i * externalLoaderSize)); + + var deviceExternalLoader = new DeviceExternalLoader + { + filePath = externalLoaderStructure.filePath, + deviceName = externalLoaderStructure.deviceName, + deviceType = externalLoaderStructure.deviceType, + deviceStartAddress = externalLoaderStructure.deviceStartAddress, + deviceSize = externalLoaderStructure.deviceSize, + pageSize = externalLoaderStructure.pageSize, + sectorsTypeNbr = externalLoaderStructure.sectorsTypeNbr + }; + + if (externalLoaderStructure.sectors != IntPtr.Zero) + { + var deviceSectorList = new List(); + for (var ii = 0; ii < externalLoaderStructure.sectorsTypeNbr; ii++) + { + var deviceSectorItem = Marshal.PtrToStructure(externalLoaderStructure.sectors + (ii * deviceSectorSize)); + deviceSectorList.Add(deviceSectorItem); + } + + deviceExternalLoader.sectors = deviceSectorList; + } + + externalLoaderList.Add(deviceExternalLoader); + } + + output.ExternalLoader = externalLoaderList; + + return output; } } } @@ -1149,7 +1200,7 @@ public IEnumerable GetExternalLoaders(string path = @".\st\Progr this._logger?.LogError(ex, "GetExternalLoaders: "); } - return externalLoaderList; + return null; } /// diff --git a/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs b/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs index db1cb09..545d320 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Interface/ICubeProgrammerApi.cs @@ -302,12 +302,12 @@ public interface ICubeProgrammerApi : IDisposable /// This routine allows to specify the path of the external Loaders to be loaded. /// /// - ExternalLoader SetExternalLoaderPath(string path); + DeviceExternalLoader? SetExternalLoaderPath(string path); /// /// This routine allows to get available external Loaders in th mentioned path. /// - IEnumerable GetExternalLoaders(string path = @".\st\Programmer"); + DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer"); /// /// This routine allows to unload an external Loaders. @@ -324,7 +324,7 @@ public interface ICubeProgrammerApi : IDisposable #region [STM32WB specific] /// Specific APIs used exclusively for STM32WB series to manage BLE Stack, and they are available only through USB DFU and UART bootloader interfaces, - /// except for the “firmwareDelete" and the “firmwareUpgrade", available through USB DFU, UART and SWD interfaces. + /// except for the "firmwareDelete" and the "firmwareUpgrade", available through USB DFU, UART and SWD interfaces. /// Connection under Reset is mandatory. /// diff --git a/src/01/KSociety.SharpCubeProgrammer/Struct/DebugConnectParameters.cs b/src/01/KSociety.SharpCubeProgrammer/Struct/DebugConnectParameters.cs index bda7c79..fb7c389 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Struct/DebugConnectParameters.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Struct/DebugConnectParameters.cs @@ -2,7 +2,6 @@ namespace KSociety.SharpCubeProgrammer.Struct { - using System; using System.Runtime.InteropServices; using Enum; @@ -11,7 +10,7 @@ namespace KSociety.SharpCubeProgrammer.Struct /// [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] - public struct DebugConnectParameters : ICloneable + public struct DebugConnectParameters //: ICloneable { /// /// Select the type of debug interface #debugPort. @@ -99,9 +98,9 @@ public struct DebugConnectParameters : ICloneable /// public int Speed; - public object Clone() - { - return this.MemberwiseClone(); - } + //public object Clone() + //{ + // return this.MemberwiseClone(); + //} } } diff --git a/src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalLoader.cs b/src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalLoader.cs new file mode 100644 index 0000000..9ef100b --- /dev/null +++ b/src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalLoader.cs @@ -0,0 +1,57 @@ +// Copyright © K-Society and contributors. All rights reserved. Licensed under the K-Society License. See LICENSE.TXT file in the project root for full license information. + +namespace KSociety.SharpCubeProgrammer.Struct +{ + using System.Collections.Generic; + + /// + /// Get external Loader parameters to launch the process of programming an external flash memory. + /// + public struct DeviceExternalLoader + { + /// + /// FlashLoader file path. + /// + public string filePath; + + /// + /// Device Name and Description. + /// + public string deviceName; + + /// + /// Device Type: ONCHIP, EXT8BIT, EXT16BIT, ... + /// + public int deviceType; + + /// + /// Default Device Start Address. + /// + public uint deviceStartAddress; + + /// + /// Total Size of Device. + /// + public uint deviceSize; + + /// + /// Programming Page Size. + /// + public uint pageSize; + + /// + /// Content of Erased Memory. + /// + // unsigned char EraseValue; + + /// + /// Type number. + /// + public uint sectorsTypeNbr; + + /// + /// Device sectors. + /// + public List sectors; + } +} diff --git a/src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalStorageInfo.cs b/src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalStorageInfo.cs new file mode 100644 index 0000000..11fdca0 --- /dev/null +++ b/src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalStorageInfo.cs @@ -0,0 +1,13 @@ +// Copyright © K-Society and contributors. All rights reserved. Licensed under the K-Society License. See LICENSE.TXT file in the project root for full license information. + +namespace KSociety.SharpCubeProgrammer.Struct +{ + using System.Collections.Generic; + + public struct DeviceExternalStorageInfo + { + public uint ExternalLoaderNbr; + + public List ExternalLoader; + } +} diff --git a/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalLoader.cs b/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalLoader.cs index 718a33f..f2ceaa4 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalLoader.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalLoader.cs @@ -43,7 +43,10 @@ public struct ExternalLoader /// public uint pageSize; - // unsigned char EraseValue; /**< Content of Erased Memory. */ + /// + /// Content of Erased Memory. + /// + // unsigned char EraseValue; /// /// Type number. @@ -53,7 +56,6 @@ public struct ExternalLoader /// /// Device sectors. /// - //public DeviceSector sectors; public IntPtr sectors; } } diff --git a/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalStorageInfo.cs b/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalStorageInfo.cs index 1b99e3e..2e54ccc 100644 --- a/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalStorageInfo.cs +++ b/src/01/KSociety.SharpCubeProgrammer/Struct/ExternalStorageInfo.cs @@ -9,7 +9,7 @@ namespace KSociety.SharpCubeProgrammer.Struct public struct ExternalStorageInfo { public uint ExternalLoaderNbr; - //public ExternalLoader ExternalLoader; + public IntPtr ExternalLoader; } } diff --git a/src/01/Samples/Programming/Program.cs b/src/01/Samples/Programming/Program.cs index 3f1f751..1ac8874 100644 --- a/src/01/Samples/Programming/Program.cs +++ b/src/01/Samples/Programming/Program.cs @@ -7,6 +7,7 @@ namespace Programming using System.Runtime.InteropServices; using System.Text.RegularExpressions; using Autofac; + using KSociety.SharpCubeProgrammer; using KSociety.SharpCubeProgrammer.Enum; using KSociety.SharpCubeProgrammer.Interface; using KSociety.SharpCubeProgrammer.Struct; @@ -16,9 +17,9 @@ namespace Programming internal class Program { - private static IConfigurationRoot Configuration; - private static ILogger Logger; - private static ICubeProgrammerApi CubeProgrammerApi; + private static IConfigurationRoot? Configuration; + private static ILogger? Logger; + private static ICubeProgrammerApi? CubeProgrammerApi; private static void Main(string[] args) { @@ -37,56 +38,7 @@ private static void Main(string[] args) Console.WriteLine("Press a button to continue."); Console.ReadLine(); - //var testStLink = CubeProgrammerApi.TryConnectStLink(0, 0, DebugConnectionMode.UnderResetMode); - - //if (testStLink.Equals(CubeProgrammerError.CubeprogrammerNoError)) - //{ - // var generalInfo = CubeProgrammerApi.GetDeviceGeneralInf(); - // if (generalInfo != null) - // { - // Logger.LogInformation("INFO: \n" + - // "Board: {0} \n" + - // "Bootloader Version: {1} \n" + - // "Cpu: {2} \n" + - // "Description: {3} \n" + - // "DeviceId: {4} \n" + - // "FlashSize: {5} \n" + - // "RevisionId: {6} \n" + - // "Name: {7} \n" + - // "Series: {8} \n" + - // "Type: {9}", - // generalInfo.Board, - // generalInfo.BootloaderVersion, - // generalInfo.Cpu, - // generalInfo.Description, - // generalInfo.DeviceId, - // generalInfo.FlashSize, - // generalInfo.RevisionId, - // generalInfo.Name, - // generalInfo.Series, - // generalInfo.Type); - // } - // CubeProgrammerApi.Disconnect(); - //} - //else - //{ - // Logger.LogWarning(testStLink.ToString()); - //} - - //var path = @".\st\Programmer"; - //var result2 = CubeProgrammerApi.GetExternalLoaders(path); - - //Logger?.LogInformation("GetExternalLoaders: {0}", result2.Count()); - - //foreach (var currentItem in result2) - //{ - // Logger?.LogTrace("GetExternalLoaders: device name: {0}, file path: {1}, device type: {2}, device size: {3}, start address: {4}, page size: {5}, sectors type: {6}", - // currentItem.deviceName, currentItem.filePath, currentItem.deviceType, CubeProgrammerApi.HexConverterToString(currentItem.deviceSize), - // CubeProgrammerApi.HexConverterToString(currentItem.deviceStartAddress), CubeProgrammerApi.HexConverterToString(currentItem.pageSize), - // currentItem.sectorsTypeNbr); - //} - - + #region [Log Testing] var displayCallBacks = new DisplayCallBacks @@ -97,17 +49,17 @@ private static void Main(string[] args) }; CubeProgrammerApi.SetDisplayCallbacks(displayCallBacks); - //CubeProgrammerApi.SetDisplayCallbacks(InitProgressBar, ReceiveMessage, ProgressBarUpdate); - CubeProgrammerApi.SetVerbosityLevel(CubeProgrammerVerbosityLevel.CubeprogrammerVerLevelDebug); #endregion + var deviceExternalStorageInfo = CubeProgrammerApi.GetExternalLoaders(); + var stLinkList = CubeProgrammerApi.GetStLinkEnumerationList(); if (stLinkList.Any()) { - var stLink = (DebugConnectParameters)stLinkList.First().Clone(); + var stLink = stLinkList.First(); stLink.ConnectionMode = KSociety.SharpCubeProgrammer.Enum.DebugConnectionMode.UnderResetMode; stLink.Shared = 0; @@ -303,7 +255,7 @@ private static void ProgressBarUpdate(int currentProgress, int total) //operation, erase operation does not produce advance var current = (currentProgress * 100F) / total; - Logger.LogInformation("ProgressBarUpdate: {0}", current); + Logger?.LogInformation("ProgressBarUpdate: {0}", current); } } } diff --git a/src/01/Samples/ProgrammingLegacy/Program.cs b/src/01/Samples/ProgrammingLegacy/Program.cs index 5103a42..0572c59 100644 --- a/src/01/Samples/ProgrammingLegacy/Program.cs +++ b/src/01/Samples/ProgrammingLegacy/Program.cs @@ -34,7 +34,7 @@ static void Main(string[] args) var stLinkList = CubeProgrammerApi.GetStLinkEnumerationList(); if (stLinkList.Any()) { - var stLink = (DebugConnectParameters)stLinkList.First().Clone(); + var stLink = stLinkList.First(); stLink.ConnectionMode = KSociety.SharpCubeProgrammer.Enum.DebugConnectionMode.UnderResetMode; stLink.Shared = 0;