Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Experimental #90

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions KSociety.SharpCubeProgrammer.sln
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ Global
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Debug|x64.ActiveCfg = Debug|x64
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Debug|x64.Build.0 = Debug|x64
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Debug|x86.ActiveCfg = Debug|Win32
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Debug|x86.Build.0 = Debug|Win32
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Release|Any CPU.ActiveCfg = Release|Win32
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Release|x64.ActiveCfg = Release|x64
{52455EFE-41B6-4C8B-97C0-DB796FE725B5}.Release|x64.Build.0 = Release|x64
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ You can get KSociety.SharpCubeProgrammer by [grabbing the latest NuGet package](
- ConnectDfuBootloader

## General purposes functions
- SetDisplayCallbacks
- SetVerbosityLevel
- CheckDeviceConnection
- GetDeviceGeneralInf
- ReadMemory
Expand Down
2 changes: 2 additions & 0 deletions docs/KSociety.SharpCubeProgrammer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ You can get KSociety.SharpCubeProgrammer by [grabbing the latest NuGet package](
- ConnectDfuBootloader

## General purposes functions
- SetDisplayCallbacks
- SetVerbosityLevel
- CheckDeviceConnection
- GetDeviceGeneralInf
- ReadMemory
Expand Down
206 changes: 175 additions & 31 deletions src/01/KSociety.SharpCubeProgrammer/CubeProgrammerApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ namespace KSociety.SharpCubeProgrammer
{
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using Base.InfraSub.Shared.Class;
Expand Down Expand Up @@ -396,6 +397,33 @@ public void ConnectI2cBootloader()

// General module groups general purposes functions used by any interface.

/// <inheritdoc />
public DisplayCallBacks SetDisplayCallbacks(InitProgressBar initProgressBar, LogMessageReceived messageReceived, ProgressBarUpdateReceived progressBarUpdate)
{
var callbacksHandle = new DisplayCallBacks
{
InitProgressBar = initProgressBar,
LogMessage = messageReceived,
LoadBar = progressBarUpdate
};

Native.ProgrammerApi.SetDisplayCallbacks(callbacksHandle);

return callbacksHandle;
}

/// <inheritdoc />
public void SetDisplayCallbacks(ref DisplayCallBacks callbacksHandle)
{
Native.ProgrammerApi.SetDisplayCallbacks(callbacksHandle);
}

/// <inheritdoc />
public void SetVerbosityLevel(CubeProgrammerVerbosityLevel level)
{
Native.ProgrammerApi.SetVerbosityLevel((int)level);
}

/// <inheritdoc />
public bool CheckDeviceConnection()
{
Expand Down Expand Up @@ -803,7 +831,8 @@ public void AutomaticMode(string filePath, string address, uint skipErase = 1U,
public (CubeProgrammerError, DeviceStorageStructure) GetStorageStructure()
{
var deviceStorageStructure = new DeviceStorageStructure();

var deviceBankSize = Marshal.SizeOf<DeviceBank>();
var bankSectorSize = Marshal.SizeOf<BankSector>();
var storageStructurePtr = new IntPtr();

var output = CubeProgrammerError.CubeprogrammerErrorOther;
Expand All @@ -820,12 +849,39 @@ public void AutomaticMode(string filePath, string address, uint skipErase = 1U,
var storageStructure = Marshal.PtrToStructure<StorageStructure>(storageStructurePtr);

deviceStorageStructure.BanksNumber = storageStructure.BanksNumber;
var deviceBankResult = Marshal.PtrToStructure<DeviceBank>(storageStructure.Banks);
deviceStorageStructure.SectorsNumber = deviceBankResult.SectorsNumber;
var bankSectors = Marshal.PtrToStructure<BankSector>(deviceBankResult.Sectors);
deviceStorageStructure.Index = bankSectors.Index;
deviceStorageStructure.Size = bankSectors.Size;
deviceStorageStructure.Address = bankSectors.Address;
var deviceBankList = new List<DeviceDeviceBank>();
for (var i = 0; i < storageStructure.BanksNumber; i++)
{

if (storageStructure.Banks != IntPtr.Zero)
{
var deviceBank = Marshal.PtrToStructure<DeviceBank>(storageStructure.Banks + (i * deviceBankSize));
var bankSectorList = new List<BankSector>();
if (deviceBank.Sectors != IntPtr.Zero)
{
for (var ii = 0; ii < deviceBank.SectorsNumber; ii++)
{
var bankSector = Marshal.PtrToStructure<BankSector>(deviceBank.Sectors + (ii * bankSectorSize));

bankSectorList.Add(bankSector);

Marshal.DestroyStructure<BankSector>(deviceBank.Sectors + (ii * bankSectorSize));
}
}

var deviceDeviceBank = new DeviceDeviceBank
{
SectorsNumber = deviceBank.SectorsNumber, Sectors = bankSectorList
};

deviceBankList.Add(deviceDeviceBank);

Marshal.DestroyStructure<DeviceBank>(storageStructure.Banks + (i * deviceBankSize));
}
}

deviceStorageStructure.Banks = deviceBankList;
Marshal.DestroyStructure<StorageStructure>(storageStructurePtr);
}
}
}
Expand All @@ -852,49 +908,137 @@ public CubeProgrammerError SendOptionBytesCmd(string command)
}

/// <inheritdoc />
public PeripheralC? InitOptionBytesInterface()
public DevicePeripheralC? InitOptionBytesInterface()
{
PeripheralC? peripheralC = null;

var pointer = Native.ProgrammerApi.InitOptionBytesInterface();

try
{
peripheralC = Marshal.PtrToStructure<PeripheralC>(pointer);
}
catch (Exception ex)
{
this._logger?.LogError(ex, "InitOptionBytesInterface: ");
}
finally
{
Marshal.DestroyStructure<PeripheralC>(pointer);
}

return peripheralC;
return pointer != IntPtr.Zero ? this.DevicePeripheralCHandler(pointer) : null;
}

/// <inheritdoc />
public PeripheralC? FastRomInitOptionBytesInterface(ushort deviceId)
public DevicePeripheralC? FastRomInitOptionBytesInterface(ushort deviceId)
{
PeripheralC? peripheralC = null;

var pointer = Native.ProgrammerApi.FastRomInitOptionBytesInterface(deviceId);

return pointer != IntPtr.Zero ? this.DevicePeripheralCHandler(pointer) : null;
}

private DevicePeripheralC? DevicePeripheralCHandler(IntPtr pointer)
{
var pointerSize = Marshal.SizeOf<IntPtr>();

try
{
peripheralC = Marshal.PtrToStructure<PeripheralC>(pointer);
PeripheralC? peripheralC = Marshal.PtrToStructure<PeripheralC>(pointer);

if (peripheralC.HasValue)
{
var bankCList = new List<DeviceBankC>();
for (var i = 0; i < peripheralC.Value.BanksNbr; i++)
{
if (peripheralC.Value.Banks != IntPtr.Zero)
{
var bankCItemPointer = Marshal.ReadIntPtr(peripheralC.Value.Banks + (i * pointerSize));
var bankCItem = Marshal.PtrToStructure<BankC>(bankCItemPointer);

if (bankCItem.Categories != IntPtr.Zero)
{
var categoryCList = new List<DeviceCategoryC>();
for (var ii = 0; ii < bankCItem.CategoriesNbr; ii++)
{
var categoryCItemPointer =
Marshal.ReadIntPtr(bankCItem.Categories + (ii * pointerSize));
var categoryCItem = Marshal.PtrToStructure<CategoryC>(categoryCItemPointer);

if (categoryCItem.Bits != IntPtr.Zero)
{
var bitCList = new List<DeviceBitC>();
for (var iii = 0; iii < categoryCItem.BitsNbr; iii++)
{
var bitCItemPointer =
Marshal.ReadIntPtr(categoryCItem.Bits + (iii * pointerSize));
var bitCItem = Marshal.PtrToStructure<BitC>(bitCItemPointer);

if (bitCItem.Values != IntPtr.Zero)
{
var bitValueCList = new List<BitValueC>();
for (var iiii = 0; iiii < bitCItem.ValuesNbr; iiii++)
{
var bitValueCItemPointer =
Marshal.ReadIntPtr(bitCItem.Values + (iiii * pointerSize));
var bitValueCItem =
Marshal.PtrToStructure<BitValueC>(bitValueCItemPointer);
bitValueCList.Add(bitValueCItem);

Marshal.DestroyStructure<BitValueC>(bitValueCItemPointer);
}

var deviceBitC = new DeviceBitC
{
Name = bitCItem.Name,
Description = bitCItem.Description,
WordOffset = bitCItem.WordOffset,
BitOffset = bitCItem.BitOffset,
BitWidth = bitCItem.BitWidth,
Access = bitCItem.Access,
ValuesNbr = bitCItem.ValuesNbr,
Values = bitValueCList,
Equation = bitCItem.Equation,
Reference = bitCItem.Reference,
BitValue = bitCItem.BitValue
};
bitCList.Add(deviceBitC);

Marshal.DestroyStructure<BitC>(bitCItemPointer);
}
}

var deviceCategoryC = new DeviceCategoryC
{
Name = categoryCItem.Name,
BitsNbr = categoryCItem.BitsNbr,
Bits = bitCList
};
categoryCList.Add(deviceCategoryC);
Marshal.DestroyStructure<CategoryC>(categoryCItemPointer);
}
}

var deviceBankC = new DeviceBankC
{
Size = bankCItem.Size,
Address = bankCItem.Address,
Access = bankCItem.Access,
CategoriesNbr = bankCItem.CategoriesNbr,
Categories = categoryCList
};
bankCList.Add(deviceBankC);
Marshal.DestroyStructure<BankC>(bankCItemPointer);
}
}
}

var devicePeripheralC = new DevicePeripheralC
{
Name = peripheralC.Value.Name,
Description = peripheralC.Value.Description,
BanksNbr = peripheralC.Value.BanksNbr,
Banks = bankCList
};

return devicePeripheralC;
}
}
catch (Exception ex)
{
this._logger?.LogError(ex, "FastRomInitOptionBytesInterface: ");
this._logger?.LogError(ex, "DevicePeripheralCHandler: ");
}
finally
{
Marshal.DestroyStructure<PeripheralC>(pointer);
}

return peripheralC;
return null;
}

/// <inheritdoc />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ namespace KSociety.SharpCubeProgrammer.DeviceDataStructure
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct BitC
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 64)]
public string Name;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 300)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1000)]
public string Description;

public uint WordOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public struct BitValueC
{
public uint Value;

[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1000)]
public string Description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// 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.DeviceDataStructure
{
using System.Collections.Generic;

public struct DeviceBankC
{
public uint Size;
public uint Address;
public byte Access;
public uint CategoriesNbr;
public List<DeviceCategoryC> Categories;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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.DeviceDataStructure
{
using System.Collections.Generic;

public struct DeviceBitC
{
public string Name;

public string Description;

public uint WordOffset;
public uint BitOffset;
public uint BitWidth;
public byte Access;

public uint ValuesNbr;

public List<BitValueC> Values;
public BitCoefficientC Equation;
public string Reference;
public uint BitValue;
}
}
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.DeviceDataStructure
{
using System.Collections.Generic;

public struct DeviceCategoryC
{
public string Name;
public uint BitsNbr;
public List<DeviceBitC> Bits;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// 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.DeviceDataStructure
{
using System.Collections.Generic;

public struct DeviceDeviceBank
{
public uint SectorsNumber;
public List<BankSector> Sectors;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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.DeviceDataStructure
{
using System.Collections.Generic;

public struct DevicePeripheralC
{
public string Name;

public string Description;

public uint BanksNbr;

public List<DeviceBankC> Banks;
}
}
Loading
Loading