📄 io.cs
字号:
// Copyright (c) David Vescovi. All rights reserved.
// Part of Project DrumStix
// Windows Embedded Developers Interest Group (WE-DIG) community project.
// http://www.we-dig.org
#region Using directives
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
#endregion
namespace Gumstix
{
public static class IO
{
private const Int32 CODE_IOCTL_PHY_ADR = 4005;
private const Int32 CODE_IOCTL_FCS = 4050;
private const Int32 CODE_IOCTL_BOOTLOADER_VER = 4051;
private const Int32 CODE_IOCTL_HARDWARE_CNFG = 4052;
private const Int32 FILE_DEVICE_HAL = 0x00000101;
private const Int32 FILE_ANY_ACCESS = 0x0;
private const Int32 METHOD_BUFFERED = 0x0;
private static byte[] inbuffer = new byte[16];
private static byte[] outbuffer = new byte[4];
private const Int32 IOCTL_PHY_ADR =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
| ((CODE_IOCTL_PHY_ADR) << 2) | (METHOD_BUFFERED);
private const Int32 IOCTL_FCS =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
| ((CODE_IOCTL_FCS) << 2) | (METHOD_BUFFERED);
private const Int32 IOCTL_BOOTLOADER_VER =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
| ((CODE_IOCTL_BOOTLOADER_VER) << 2) | (METHOD_BUFFERED);
private const Int32 IOCTL_HARDWARE_CNFG =
((FILE_DEVICE_HAL) << 16) | ((FILE_ANY_ACCESS) << 14)
| ((CODE_IOCTL_HARDWARE_CNFG) << 2) | (METHOD_BUFFERED);
/// <summary>
/// Memory or memory mapped register access
/// </summary>
public static class Register
{
#region Read/Write
/// <summary>
/// Read a 32bit register or memory location
/// </summary>
/// <remarks>Thread safe</remarks>
public static uint Read(uint address)
{
lock (typeof(IO))
{
int cb = 0;
// subcode
inbuffer[0] = 1;
//address
outbuffer = BitConverter.GetBytes(address);
inbuffer[7] = outbuffer[3];
inbuffer[6] = outbuffer[2];
inbuffer[5] = outbuffer[1];
inbuffer[4] = outbuffer[0];
if (!SafeNativeMethods.KernelIoControl(IOCTL_PHY_ADR, inbuffer, 8, outbuffer, 4, ref cb))
{
throw new Exception("Unmanaged Error");
}
return (BitConverter.ToUInt32(outbuffer, 0));
}
}
/// <summary>
/// Write a 32bit register or memory location
/// </summary>
/// <remarks>Thread safe</remarks>
public static void Write(uint address, uint data)
{
lock (typeof(IO))
{
int cb = 0;
// subcode
inbuffer[0] = 2;
//address
outbuffer = BitConverter.GetBytes(address);
inbuffer[7] = outbuffer[3];
inbuffer[6] = outbuffer[2];
inbuffer[5] = outbuffer[1];
inbuffer[4] = outbuffer[0];
//data
outbuffer = BitConverter.GetBytes(data);
inbuffer[11] = outbuffer[3];
inbuffer[10] = outbuffer[2];
inbuffer[9] = outbuffer[1];
inbuffer[8] = outbuffer[0];
//mask
outbuffer = BitConverter.GetBytes(0xffffffff);
inbuffer[15] = outbuffer[3];
inbuffer[14] = outbuffer[2];
inbuffer[13] = outbuffer[1];
inbuffer[12] = outbuffer[0];
if (!SafeNativeMethods.KernelIoControl(IOCTL_PHY_ADR, inbuffer, 16, outbuffer, 4, ref cb))
{
throw new Exception("Unmanaged Error");
}
}
}
#endregion
#region Bit set/clear
/// <summary>
/// Set a bit in a 32bit register or memory location
/// </summary>
/// <remarks>Thread safe</remarks>
public static void BitSet(uint address, uint data)
{
lock (typeof(IO))
{
int cb = 0;
// subcode
inbuffer[0] = 2;
//address
outbuffer = BitConverter.GetBytes(address);
inbuffer[7] = outbuffer[3];
inbuffer[6] = outbuffer[2];
inbuffer[5] = outbuffer[1];
inbuffer[4] = outbuffer[0];
//data
outbuffer = BitConverter.GetBytes(data);
inbuffer[11] = outbuffer[3];
inbuffer[10] = outbuffer[2];
inbuffer[9] = outbuffer[1];
inbuffer[8] = outbuffer[0];
//mask
outbuffer = BitConverter.GetBytes(data);
inbuffer[15] = outbuffer[3];
inbuffer[14] = outbuffer[2];
inbuffer[13] = outbuffer[1];
inbuffer[12] = outbuffer[0];
if (!SafeNativeMethods.KernelIoControl(IOCTL_PHY_ADR, inbuffer, 16, outbuffer, 4, ref cb))
{
throw new Exception("Unmanaged Error");
}
}
}
/// <summary>
/// Clear a bit in a 32bit register or memory location
/// </summary>
/// <remarks>Thread safe</remarks>
public static void BitClear(uint address, uint data)
{
lock (typeof(IO))
{
int cb = 0;
// subcode
inbuffer[0] = 2;
//address
outbuffer = BitConverter.GetBytes(address);
inbuffer[7] = outbuffer[3];
inbuffer[6] = outbuffer[2];
inbuffer[5] = outbuffer[1];
inbuffer[4] = outbuffer[0];
//data
outbuffer = BitConverter.GetBytes(~data);
inbuffer[11] = outbuffer[3];
inbuffer[10] = outbuffer[2];
inbuffer[9] = outbuffer[1];
inbuffer[8] = outbuffer[0];
//mask
outbuffer = BitConverter.GetBytes(data);
inbuffer[15] = outbuffer[3];
inbuffer[14] = outbuffer[2];
inbuffer[13] = outbuffer[1];
inbuffer[12] = outbuffer[0];
if (!SafeNativeMethods.KernelIoControl(IOCTL_PHY_ADR, inbuffer, 16, outbuffer, 4, ref cb))
{
throw new Exception("Unmanaged Error");
}
}
}
#endregion
}
/// <summary>
/// CPU frequency information
/// </summary>
public static class Frequency
{
[Flags()]
public enum RunSpeed : uint
{
SP100MHZ = 0x00000121,
SP150MHZ = 0x000001A1,
SP200MHZ = 0x00000141,
SP300MHZ = 0x000001c1,
SP400MHZ = 0x00000161
}
/// <summary>
/// Do a Frequency Change Sequence
/// </summary>
private static void DoFCS(bool turbo)
{
int cb = 0;
byte[] inbuffer = new byte[1];
byte[] outbuffer = new byte[1];
if (turbo)
inbuffer[0] = 1;
else
inbuffer[0] = 0;
if (!SafeNativeMethods.KernelIoControl(IOCTL_FCS, inbuffer, 1, outbuffer, 0, ref cb))
{
throw new Exception("Unmanaged Error");
}
}
/// <summary>
/// Gets or sets the platforms running speed
/// </summary>
public static RunSpeed runSpeed
{
set
{
Register.Write(0x41300000, (uint)value);
if (value == RunSpeed.SP300MHZ || value == RunSpeed.SP150MHZ)
DoFCS(true); // (M) w/1.5(N) and turbo on
else
DoFCS(false);
}
get
{
return ((RunSpeed)Register.Read(0x41300000));
}
}
}
/// <summary>
/// Retrieves information from the boot loader
/// </summary>
public static class Bootloader
{
private static uint ReadIOCTL(Int32 code)
{
int cb = 0;
byte[] outbuffer = new byte[4];
if (!SafeNativeMethods.KernelIoControl(code, null, 0, outbuffer, 4, ref cb))
{
throw new Exception("Unmanaged Error");
}
return (BitConverter.ToUInt32(outbuffer, 0));
}
/// <summary>
/// The Hardware Configuration as set in the bootloader
/// </summary>
public static UInt32 HardwareConfiguration
{
get
{
return (ReadIOCTL(IOCTL_HARDWARE_CNFG));
}
}
/// <summary>
/// The bootloader version string X.X
/// </summary>
public static string Version
{
get
{
UInt32 version = ReadIOCTL(IOCTL_BOOTLOADER_VER);
return (Convert.ToString(version >> 16) + "." + Convert.ToString(version & 0x0000ffff));
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -