📄 aardvark.cs
字号:
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_port(aardvark);
}
/*
* Return the device features as a bit-mask of values, or
* an error code if the handle is not valid.
*/
public const int AA_FEATURE_SPI = 0x00000001;
public const int AA_FEATURE_I2C = 0x00000002;
public const int AA_FEATURE_GPIO = 0x00000008;
public const int AA_FEATURE_I2C_MONITOR = 0x00000010;
public static int aa_features (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_features(aardvark);
}
/*
* Return the unique ID for this Aardvark adapter.
* IDs are guaranteed to be non-zero if valid.
* The ID is the unsigned integer representation of the
* 10-digit serial number.
*/
public static int aa_unique_id (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_unique_id(aardvark);
}
/*
* Return the status string for the given status code.
* If the code is not valid or the library function cannot
* be loaded, return a NULL string.
*/
public static string aa_status_string (
int status
)
{
if (!AA_LIBRARY_LOADED) return null;
return Marshal.PtrToStringAnsi(c_aa_status_string(status));
}
/*
* Enable logging to a file. The handle must be standard file
* descriptor. In C, a file descriptor can be obtained by using
* the ANSI C function "open" or by using the function "fileno"
* on a FILE* stream. A FILE* stream can be obtained using "fopen"
* or can correspond to the common "stdout" or "stderr" --
* available when including stdlib.h
*/
public static int aa_log (
int aardvark,
int level,
int handle
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_log(aardvark, level, handle);
}
/*
* Return the version matrix for the device attached to the
* given handle. If the handle is 0 or invalid, only the
* software and required api versions are set.
*/
public static int aa_version (
int aardvark,
ref AardvarkVersion version
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_version(aardvark, ref version);
}
/*
* Configure the device by enabling/disabling I2C, SPI, and
* GPIO functions.
*/
// enum AardvarkConfig (from declaration above)
// AA_CONFIG_GPIO_ONLY = 0x00
// AA_CONFIG_SPI_GPIO = 0x01
// AA_CONFIG_GPIO_I2C = 0x02
// AA_CONFIG_SPI_I2C = 0x03
// AA_CONFIG_QUERY = 0x80
public const int AA_CONFIG_SPI_MASK = 0x00000001;
public const int AA_CONFIG_I2C_MASK = 0x00000002;
public static int aa_configure (
int aardvark,
AardvarkConfig config
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_configure(aardvark, config);
}
/*
* Configure the target power pins.
* This is only supported on hardware versions >= 2.00
*/
public const byte AA_TARGET_POWER_NONE = 0x00;
public const byte AA_TARGET_POWER_BOTH = 0x03;
public const byte AA_TARGET_POWER_QUERY = 0x80;
public static int aa_target_power (
int aardvark,
byte power_mask
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_target_power(aardvark, power_mask);
}
/*
* Sleep for the specified number of milliseconds
* Accuracy depends on the operating system scheduler
* Returns the number of milliseconds slept
*/
public static int aa_sleep_ms (
int milliseconds
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_sleep_ms(milliseconds);
}
/*=========================================================================
| ASYNC MESSAGE POLLING
========================================================================*/
/*
* Polling function to check if there are any asynchronous
* messages pending for processing. The function takes a timeout
* value in units of milliseconds. If the timeout is < 0, the
* function will block until data is received. If the timeout is 0,
* the function will perform a non-blocking check.
*/
public const int AA_ASYNC_NO_DATA = 0x00000000;
public const int AA_ASYNC_I2C_READ = 0x00000001;
public const int AA_ASYNC_I2C_WRITE = 0x00000002;
public const int AA_ASYNC_SPI = 0x00000004;
public const int AA_ASYNC_I2C_MONITOR = 0x00000008;
public static int aa_async_poll (
int aardvark,
int timeout
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_async_poll(aardvark, timeout);
}
/*=========================================================================
| I2C API
========================================================================*/
/* Free the I2C bus. */
public static int aa_i2c_free_bus (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_free_bus(aardvark);
}
/*
* Set the I2C bit rate in kilohertz. If a zero is passed as the
* bitrate, the bitrate is unchanged and the current bitrate is
* returned.
*/
public static int aa_i2c_bitrate (
int aardvark,
int bitrate_khz
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_bitrate(aardvark, bitrate_khz);
}
// enum AardvarkI2cFlags (from declaration above)
// AA_I2C_NO_FLAGS = 0x00
// AA_I2C_10_BIT_ADDR = 0x01
// AA_I2C_COMBINED_FMT = 0x02
// AA_I2C_NO_STOP = 0x04
/* Read a stream of bytes from the I2C slave device. */
public static int aa_i2c_read (
int aardvark,
short slave_addr,
AardvarkI2cFlags flags,
short num_bytes,
ref byte[] data_in
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_read(aardvark, slave_addr, flags, num_bytes, ref data_in[0]);
}
// enum AardvarkI2cStatus (from declaration above)
// AA_I2C_STATUS_OK = 0
// AA_I2C_STATUS_BUS_ERROR = 1
// AA_I2C_STATUS_SLA_ACK = 2
// AA_I2C_STATUS_SLA_NACK = 3
// AA_I2C_STATUS_DATA_NACK = 4
// AA_I2C_STATUS_ARB_LOST = 5
// AA_I2C_STATUS_BUS_LOCKED = 6
// AA_I2C_STATUS_LAST_DATA_ACK = 7
/*
* Read a stream of bytes from the I2C slave device.
* This API function returns the number of bytes read into
* the num_read variable. The return value of the function
* is a status code.
*/
public static int aa_i2c_read_ext (
int aardvark,
short slave_addr,
AardvarkI2cFlags flags,
short num_bytes,
ref byte[] data_in,
ref short num_read
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_read_ext(aardvark, slave_addr, flags, num_bytes, ref data_in[0], ref num_read);
}
/* Write a stream of bytes to the I2C slave device. */
public static int aa_i2c_write (
int aardvark,
short slave_addr,
AardvarkI2cFlags flags,
byte[] data_out
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
short num_bytes = ((short)data_out.Length);
return c_aa_i2c_write(aardvark, slave_addr, flags, num_bytes, data_out);
}
/*
* Write a stream of bytes to the I2C slave device.
* This API function returns the number of bytes written into
* the num_written variable. The return value of the function
* is a status code.
*/
public static int aa_i2c_write_ext (
int aardvark,
short slave_addr,
AardvarkI2cFlags flags,
byte[] data_out,
ref short num_written
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
short num_bytes = ((short)data_out.Length);
return c_aa_i2c_write_ext(aardvark, slave_addr, flags, num_bytes, data_out, ref num_written);
}
/* Enable/Disable the Aardvark as an I2C slave device */
public static int aa_i2c_slave_enable (
int aardvark,
byte addr,
short maxTxBytes,
short maxRxBytes
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_slave_enable(aardvark, addr, maxTxBytes, maxRxBytes);
}
public static int aa_i2c_slave_disable (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_slave_disable(aardvark);
}
/*
* Set the slave response in the event the Aardvark is put
* into slave mode and contacted by a Master.
*/
public static int aa_i2c_slave_set_response (
int aardvark,
byte[] data_out
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
byte num_bytes = ((byte)data_out.Length);
return c_aa_i2c_slave_set_response(aardvark, num_bytes, data_out);
}
/*
* Return number of bytes written from a previous
* Aardvark->I2C_master transmission. Since the transmission is
* happening asynchronously with respect to the PC host
* software, there could be responses queued up from many
* previous write transactions.
*/
public static int aa_i2c_slave_write_stats (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_slave_write_stats(aardvark);
}
/* Read the bytes from an I2C slave reception */
public static int aa_i2c_slave_read (
int aardvark,
ref byte addr,
short num_bytes,
ref byte[] data_in
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_slave_read(aardvark, ref addr, num_bytes, ref data_in[0]);
}
/* Extended functions that return status code */
public static int aa_i2c_slave_write_stats_ext (
int aardvark,
ref short num_written
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_slave_write_stats_ext(aardvark, ref num_written);
}
public static int aa_i2c_slave_read_ext (
int aardvark,
ref byte addr,
short num_bytes,
ref byte[] data_in,
ref short num_read
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_slave_read_ext(aardvark, ref addr, num_bytes, ref data_in[0], ref num_read);
}
/*
* Enable the I2C bus monitor
* This disables all other functions on the Aardvark adapter
*/
public static int aa_i2c_monitor_enable (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_monitor_enable(aardvark);
}
/* Disable the I2C bus monitor */
public static int aa_i2c_monitor_disable (
int aardvark
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_monitor_disable(aardvark);
}
/* Read the data collected by the bus monitor */
public const short AA_I2C_MONITOR_DATA = 0x00ff;
public const short AA_I2C_MONITOR_NACK = 0x0100;
public const short AA_I2C_MONITOR_CMD_START = unchecked((short)0xff00);
public const short AA_I2C_MONITOR_CMD_STOP = unchecked((short)0xff01);
public static int aa_i2c_monitor_read (
int aardvark,
short num_bytes,
ref short[] data
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_monitor_read(aardvark, num_bytes, ref data[0]);
}
/*
* Configure the I2C pullup resistors.
* This is only supported on hardware versions >= 2.00
*/
public const byte AA_I2C_PULLUP_NONE = 0x00;
public const byte AA_I2C_PULLUP_BOTH = 0x03;
public const byte AA_I2C_PULLUP_QUERY = 0x80;
public static int aa_i2c_pullup (
int aardvark,
byte pullup_mask
)
{
if (!AA_LIBRARY_LOADED) return (int)AardvarkStatus.AA_INCOMPATIBLE_LIBRARY;
return c_aa_i2c_pullup(aardvark, pullup_mask);
}
/*=========================================================================
| SPI API
========================================================================*/
/*
* Set the SPI bit rate in kilohertz. If a zero is passed as the
* bitrate, the bitrate is unchanged and the current bitrate is
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -