Skip to content

Commit

Permalink
Nct677x esio mutex (LibreHardwareMonitor#1254)
Browse files Browse the repository at this point in the history
* Nct677X fix mutex acess with other devices an multiple instances
Fix the access with multiple devices.
Issue: LibreHardwareMonitor#1243
Improper accessing the Nuvoton eSIO causes problems with other monitoring software

* Fix timeout behavior,
ensure the wait loop abort after a defined time

* Update timeout behavior and Code Formatting

* Update timeout behavior
  • Loading branch information
sebastian-dev authored Jul 20, 2024
1 parent d3a7db7 commit 6edb1bc
Showing 1 changed file with 53 additions and 5 deletions.
58 changes: 53 additions & 5 deletions LibreHardwareMonitorLib/Hardware/Motherboard/Lpc/Nct677X.cs
Original file line number Diff line number Diff line change
Expand Up @@ -817,11 +817,36 @@ private byte ReadByte(ushort address)
}

byte page = (byte)(address >> 8);
byte index = (byte)(address & 0xFF);
Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, EC_SPACE_PAGE_SELECT);
byte index = (byte)(address & 0xFF);

//wait for access, access == EC_SPACE_PAGE_SELECT
//timeout: after 500ms, abort and force access
byte access;

DateTime timeout = DateTime.UtcNow.AddMilliseconds(500);
while (true)
{
access = Ring0.ReadIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET);
if (access == EC_SPACE_PAGE_SELECT || DateTime.UtcNow > timeout)
break;

System.Threading.Thread.Sleep(1);
}

if (access != EC_SPACE_PAGE_SELECT)
{
// Failed to gain access: force register access
Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, EC_SPACE_PAGE_SELECT);
}

Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, page);
Ring0.WriteIoPort(_port + EC_SPACE_INDEX_REGISTER_OFFSET, index);
return Ring0.ReadIoPort(_port + EC_SPACE_DATA_REGISTER_OFFSET);
byte result = Ring0.ReadIoPort(_port + EC_SPACE_DATA_REGISTER_OFFSET);

//free access for other instances
Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, EC_SPACE_PAGE_SELECT);

return result;
}

private void WriteByte(ushort address, byte value)
Expand All @@ -838,11 +863,34 @@ private void WriteByte(ushort address, byte value)
else
{
byte page = (byte)(address >> 8);
byte index = (byte)(address & 0xFF);
Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, EC_SPACE_PAGE_SELECT);
byte index = (byte)(address & 0xFF);

//wait for access, access == EC_SPACE_PAGE_SELECT
//timeout: after 500ms, abort and force access
byte access;

DateTime timeout = DateTime.UtcNow.AddMilliseconds(500);
while (true)
{
access = Ring0.ReadIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET);
if (access == EC_SPACE_PAGE_SELECT || DateTime.UtcNow > timeout)
break;

System.Threading.Thread.Sleep(1);
}

if (access != EC_SPACE_PAGE_SELECT)
{
// Failed to gain access: force register access
Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, EC_SPACE_PAGE_SELECT);
}

Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, page);
Ring0.WriteIoPort(_port + EC_SPACE_INDEX_REGISTER_OFFSET, index);
Ring0.WriteIoPort(_port + EC_SPACE_DATA_REGISTER_OFFSET, value);

//free access for other instances
Ring0.WriteIoPort(_port + EC_SPACE_PAGE_REGISTER_OFFSET, EC_SPACE_PAGE_SELECT);
}
}

Expand Down

0 comments on commit 6edb1bc

Please sign in to comment.