Skip to content

Commit

Permalink
Merge pull request #104 from K-Society/experimental
Browse files Browse the repository at this point in the history
SetExternalLoaderPath and GetExternalLoaders enhancement.
  • Loading branch information
maniglia authored Jan 15, 2024
2 parents fdfded3 + decf808 commit 799e4ff
Show file tree
Hide file tree
Showing 11 changed files with 169 additions and 95 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

Expand Down
2 changes: 1 addition & 1 deletion docs/KSociety.SharpCubeProgrammer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

Expand Down
97 changes: 74 additions & 23 deletions src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -262,10 +262,10 @@ public IEnumerable<DebugConnectParameters> GetStLinkEnumerationList(bool shared
{
this._logger?.LogError(ex, "GetStLinkEnumerationList: ");
}
finally
{
Marshal.FreeCoTaskMem(listPtr);//.FreeHGlobal(listPtr);
}
//finally
//{
// Marshal.FreeCoTaskMem(listPtr);//.FreeHGlobal(listPtr);
//}

return parametersList;
}
Expand Down Expand Up @@ -1093,54 +1093,105 @@ public void SetLoadersPath(string path)
}

/// <inheritdoc />
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<DeviceSector>();
var output = new DeviceExternalLoader();

try
{
Native.ProgrammerApi.SetExternalLoaderPath(pathAdapted, ref externalLoaderPtr);
if (externalLoaderPtr != IntPtr.Zero)
{
externalLoaderStructure = Marshal.PtrToStructure<ExternalLoader>(externalLoaderPtr);
var externalLoaderStructure = Marshal.PtrToStructure<ExternalLoader>(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<DeviceSector>();
for (var i = 0; i < externalLoaderStructure.sectorsTypeNbr; i++)
{
var deviceSectorItem = Marshal.PtrToStructure<DeviceSector>(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<ExternalLoader>(externalLoaderPtr);
// }
//}

return externalLoaderStructure;
return null;
}

/// <inheritdoc />
public IEnumerable<ExternalLoader> GetExternalLoaders(string path = @".\st\Programmer")
public DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer")
{
var pathAdapted = path.Replace(@"\", "/");
var externalLoaderList = new List<ExternalLoader>();
var externalLoaderList = new List<DeviceExternalLoader>();
var externalStorageInfoPtr = new IntPtr();
var deviceSectorSize = Marshal.SizeOf<DeviceSector>();
var output = new DeviceExternalStorageInfo();

try
{
var result = Native.ProgrammerApi.GetExternalLoaders(pathAdapted, ref externalStorageInfoPtr);
if (result.Equals(0))
{
var size = Marshal.SizeOf<ExternalLoader>();
var externalLoaderSize = Marshal.SizeOf<ExternalLoader>();
var externalStorageInfoStructure = Marshal.PtrToStructure<ExternalStorageInfo>(externalStorageInfoPtr);
for (var i = 0; i < externalStorageInfoStructure.ExternalLoaderNbr; i++)

output.ExternalLoaderNbr = externalStorageInfoStructure.ExternalLoaderNbr;

if (externalStorageInfoStructure.ExternalLoader != IntPtr.Zero)
{
var currentItem = Marshal.PtrToStructure<ExternalLoader>(externalStorageInfoStructure.ExternalLoader + (i * size));
//var deviceSectors = Marshal.PtrToStructure<DeviceSector>(currentItem.sectors);
externalLoaderList.Add(currentItem);
for (var i = 0; i < externalStorageInfoStructure.ExternalLoaderNbr; i++)
{
var externalLoaderStructure = Marshal.PtrToStructure<ExternalLoader>(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<DeviceSector>();
for (var ii = 0; ii < externalLoaderStructure.sectorsTypeNbr; ii++)
{
var deviceSectorItem = Marshal.PtrToStructure<DeviceSector>(externalLoaderStructure.sectors + (ii * deviceSectorSize));
deviceSectorList.Add(deviceSectorItem);
}

deviceExternalLoader.sectors = deviceSectorList;
}

externalLoaderList.Add(deviceExternalLoader);
}

output.ExternalLoader = externalLoaderList;

return output;
}
}
}
Expand All @@ -1149,7 +1200,7 @@ public IEnumerable<ExternalLoader> GetExternalLoaders(string path = @".\st\Progr
this._logger?.LogError(ex, "GetExternalLoaders: ");
}

return externalLoaderList;
return null;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,12 +302,12 @@ public interface ICubeProgrammerApi : IDisposable
/// This routine allows to specify the path of the external Loaders to be loaded.
/// </summary>
/// <param name="path"></param>
ExternalLoader SetExternalLoaderPath(string path);
DeviceExternalLoader? SetExternalLoaderPath(string path);

/// <summary>
/// This routine allows to get available external Loaders in th mentioned path.
/// </summary>
IEnumerable<ExternalLoader> GetExternalLoaders(string path = @".\st\Programmer");
DeviceExternalStorageInfo? GetExternalLoaders(string path = @".\st\Programmer");

/// <summary>
/// This routine allows to unload an external Loaders.
Expand All @@ -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.

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace KSociety.SharpCubeProgrammer.Struct
{
using System;
using System.Runtime.InteropServices;
using Enum;

Expand All @@ -11,7 +10,7 @@ namespace KSociety.SharpCubeProgrammer.Struct
/// </summary>

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct DebugConnectParameters : ICloneable
public struct DebugConnectParameters //: ICloneable
{
/// <summary>
/// Select the type of debug interface #debugPort.
Expand Down Expand Up @@ -99,9 +98,9 @@ public struct DebugConnectParameters : ICloneable
/// </summary>
public int Speed;

public object Clone()
{
return this.MemberwiseClone();
}
//public object Clone()
//{
// return this.MemberwiseClone();
//}
}
}
57 changes: 57 additions & 0 deletions src/01/KSociety.SharpCubeProgrammer/Struct/DeviceExternalLoader.cs
Original file line number Diff line number Diff line change
@@ -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;

/// <summary>
/// Get external Loader parameters to launch the process of programming an external flash memory.
/// </summary>
public struct DeviceExternalLoader
{
/// <summary>
/// FlashLoader file path.
/// </summary>
public string filePath;

/// <summary>
/// Device Name and Description.
/// </summary>
public string deviceName;

/// <summary>
/// Device Type: ONCHIP, EXT8BIT, EXT16BIT, ...
/// </summary>
public int deviceType;

/// <summary>
/// Default Device Start Address.
/// </summary>
public uint deviceStartAddress;

/// <summary>
/// Total Size of Device.
/// </summary>
public uint deviceSize;

/// <summary>
/// Programming Page Size.
/// </summary>
public uint pageSize;

/// <summary>
/// Content of Erased Memory.
/// <summary>
// unsigned char EraseValue;

/// <summary>
/// Type number.
/// </summary>
public uint sectorsTypeNbr;

/// <summary>
/// Device sectors.
/// </summary>
public List<DeviceSector> sectors;
}
}
Original file line number Diff line number Diff line change
@@ -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<DeviceExternalLoader> ExternalLoader;
}
}
6 changes: 4 additions & 2 deletions src/01/KSociety.SharpCubeProgrammer/Struct/ExternalLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,10 @@ public struct ExternalLoader
/// </summary>
public uint pageSize;

// unsigned char EraseValue; /**< Content of Erased Memory. */
/// <summary>
/// Content of Erased Memory.
/// <summary>
// unsigned char EraseValue;

/// <summary>
/// Type number.
Expand All @@ -53,7 +56,6 @@ public struct ExternalLoader
/// <summary>
/// Device sectors.
/// </summary>
//public DeviceSector sectors;
public IntPtr sectors;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace KSociety.SharpCubeProgrammer.Struct
public struct ExternalStorageInfo
{
public uint ExternalLoaderNbr;
//public ExternalLoader ExternalLoader;

public IntPtr ExternalLoader;
}
}
Loading

0 comments on commit 799e4ff

Please sign in to comment.