ft232h c# spi 通信
using Iot.Device.Ft232H;
using Iot.Device.FtCommon;
using System.Device.Gpio;
using System.Device.Spi;
// CS42448 寄存器地址
const byte CS42448_CHIP_ID = 0x01;
const byte CS42448_POWER_CONTROL1 = 0x02;
const byte CS42448_POWER_CONTROL2 = 0x03;
const byte CS42448_INTERFACE_FORMAT = 0x04;
const byte CS42448_SAMPLE_RATE = 0x13;
// 配置值
const byte CS42448_I2S_MODE_24BIT = 0x05;
// ----------------------
// 第一步:找到 FT232H
// ----------------------
var devices = FtCommon.GetDevices();
if (devices.Count == 0)
{
Console.WriteLine("找不到 FT232H");
return;
}
var ftDevice = devices[0];
Console.WriteLine($"已找到: {ftDevice.Description}");
// ----------------------
// 第二步:打开 FT232H
// ----------------------
using var ft232h = new Ft232HDevice(ftDevice);
GpioController gpio = ft232h.CreateGpioController();
int LED_PIN = 0; // 使用 D0 引脚
gpio.OpenPin(LED_PIN, PinMode.Output);
while (!Console.KeyAvailable)
{
gpio.Write(LED_PIN, PinValue.High); // 亮
Thread.Sleep(500);
gpio.Write(LED_PIN, PinValue.Low); // 灭
Thread.Sleep(500);
// 只要不断发送字节,SCLK 就会一直有波形
//spi.Write(new byte[] { 0x55 });
}
//// ----------------------
//// 第三步:配置 SPI
//// ----------------------
//var settings = new SpiConnectionSettings(3) // CS = D3
//{
// Mode = SpiMode.Mode0, // CS42448 必须 Mode0
// ClockFrequency = 2_00_000, // 1MHz 时钟
// DataBitLength = 8,
// ChipSelectLineActiveState = PinValue.Low,
// ChipSelectLine = 3 // //D3 片选引脚
//};
//using var spi = ft232h.CreateSpiDevice(settings);
//// ----------------------
//// 关键:循环发数据 → 产生连续时钟
//// ----------------------
//Console.WriteLine("SPI 时钟开始输出...按任意键停止");
//while (!Console.KeyAvailable)
//{
// // 只要不断发送字节,SCLK 就会一直有波形
// //spi.Write(new byte[] { 0x55 });
//}
//// ======================
//// 3. 封装 读/写 函数(适配你只有 Write() 的库)
//// ======================
//// 写寄存器
//void WriteReg(byte addr, byte value)
//{
// spi.Write(new byte[] { addr, value });
// Thread.Sleep(1); // 稳定通信
//}
//// 读寄存器(纯靠 Write 实现)
//byte ReadReg(byte addr)
//{
// byte[] buf = { (byte)(0x80 | addr), 0x00 };
// spi.Write(buf);
// return buf[1];
//}
//// ======================
//// 4. 开始配置 CS42448
//// ======================
//Console.WriteLine("=== 读取芯片 ID ===");
//byte chipId = ReadReg(CS42448_CHIP_ID);
//Console.WriteLine($"CHIP_ID = 0x{chipId:X2} (正常应为 0xD8)\n");
//Console.WriteLine("=== 上电初始化 ===");
//WriteReg(CS42448_POWER_CONTROL1, 0x00); // 上电
//WriteReg(CS42448_POWER_CONTROL2, 0x00); // 所有通道上电
//Console.WriteLine("=== 配置 I2S 24bit 模式 ===");
//WriteReg(CS42448_INTERFACE_FORMAT, CS42448_I2S_MODE_24BIT);
//Console.WriteLine("=== 配置采样速率 ===");
//WriteReg(CS42448_SAMPLE_RATE, 0x00); // 基础默认配置
//// ======================
//// 5. 读回验证配置是否成功
//// ======================
//Console.WriteLine("\n=== 读回配置验证 ===");
//byte ifFormat = ReadReg(CS42448_INTERFACE_FORMAT);
//Console.WriteLine($"INTERFACE_FORMAT = 0x{ifFormat:X2} (应等于 0x05)");
//if (ifFormat == CS42448_I2S_MODE_24BIT)
// Console.WriteLine("\n CS42448 配置成功:I2S 24bit 模式已启用!");
//else
// Console.WriteLine("\n 配置失败");
Console.WriteLine("停止");
Console.ReadLine();
// 释放资源
//spi?.Dispose();
ft232h.Dispose();
CS42448 SPI debug
using Iot.Device.Ft232H;
using Iot.Device.FtCommon;
using System;
using System.Device.Gpio;
using System.Device.Spi;
using System.Threading;
namespace FT232H_CS42448_Debug
{
class Program
{
// ====================
// 引脚定义(严格按文档)
// ====================
private const int PIN_CS = 1; // D1 = AD0/CS
private const int PIN_RST = 2; // D2 = RST
private static GpioController _gpio;
private static SpiDevice _spi;
// ====================
// CS42448 寄存器定义
// ====================
private const byte REG_CHIP_ID = 0x01;
private const byte REG_POWER_CTRL = 0x02;
private const byte REG_INTERFACE_FORMAT = 0x04;
// 配置值(I2S 24bit 模式)
private const byte VAL_POWER_CTRL = 0x00; // 上电配置
private const byte VAL_INTERFACE_FORMAT = 0x05; // I2S 24bit
static void Main(string[] args)
{
try
{
// 1. 初始化 FT232H
var ftDevices = FtCommon.GetDevices();
if (ftDevices.Count == 0)
{
Console.WriteLine("未找到 FT232H 设备");
return;
}
using var ft232h = ftDevices[0];
// 2. 初始化 GPIO(手动控制 CS/RST,不使用SPI自带CS)
var _gpio = ft232h.CreateGpioController();
_gpio.OpenPin(PIN_CS, PinMode.Output);
_gpio.OpenPin(PIN_RST, PinMode.Output);
// 3. 初始化 SPI(Mode0,2MHz 稳定,关闭自带CS)
var spiSettings = new SpiConnectionSettings(0) // 0只是占位,不用SPI自带CS
{
Mode = SpiMode.Mode0, // 严格按文档要求 Mode0
ClockFrequency = 200000, // 200KHz 稳定,避免高频失真
DataBitLength = 8,
ChipSelectLineActiveState = PinValue.Low,
ChipSelectLine = -1 // 禁用SPI自带CS,手动控制
};
_spi = ft232h.CreateSpiDevice(spiSettings);
// 4. 严格按文档时序初始化 CS42448
InitCS42448Timing();
// 配置CS42448
Console.WriteLine("\n=== 写入 CS42448 配置寄存器 ===");
WriteCS42448Register(REG_POWER_CTRL, VAL_POWER_CTRL);
Console.WriteLine($"写入 Power Control (0x02) = 0x{VAL_POWER_CTRL:X2}");
WriteCS42448Register(REG_INTERFACE_FORMAT, VAL_INTERFACE_FORMAT);
Console.WriteLine($"写入 Interface Format (0x04) = 0x{VAL_INTERFACE_FORMAT:X2}");
// 5. 读取关键寄存器(验证写配置是否生效)
Console.WriteLine("\n=== 读取 CS42448 关键寄存器(验证配置) ===");
byte chipId = ReadCS42448Register(REG_CHIP_ID);
byte powerCtrl = ReadCS42448Register(REG_POWER_CTRL);
byte ifFormat = ReadCS42448Register(REG_INTERFACE_FORMAT);
Console.WriteLine($"Chip ID (0x01): 0x{chipId:X2} (正常应为 0xD8)");
Console.WriteLine($"Power Control (0x02): 0x{powerCtrl:X2} (应等于 0x{VAL_POWER_CTRL:X2})");
Console.WriteLine($"Interface Format (0x04): 0x{ifFormat:X2} (应等于 0x{VAL_INTERFACE_FORMAT:X2})");
// 6. 验证:只要不是全0xFF,说明SPI模式已进入;配置值匹配则写成功
if (chipId != 0xFF && powerCtrl != 0xFF && ifFormat != 0xFF)
{
Console.WriteLine("\n✅ SPI 模式已进入!");
if (powerCtrl == VAL_POWER_CTRL && ifFormat == VAL_INTERFACE_FORMAT)
{
Console.WriteLine("✅ 配置写入成功!CS42448 已设为 I2S 24bit 模式");
}
else
{
Console.WriteLine("⚠️ SPI模式正常,但配置写入未生效,检查写时序");
}
}
else
{
Console.WriteLine("\n 失败!仍在I2C模式,检查时序/接线");
}
}
catch (Exception ex)
{
Console.WriteLine($"错误:{ex.Message}");
}
finally
{
_gpio?.Dispose();
_spi?.Dispose();
}
Console.ReadLine();
}
// ====================
// 核心:按文档要求初始化时序
// ====================
private static void InitCS42448Timing()
{
Console.WriteLine("执行 CS42448 初始化时序...");
// 1. CS 先保持高电平(文档要求)
_gpio.Write(PIN_CS, PinValue.High);
Thread.Sleep(10);
// 2. RST 拉低(复位)
_gpio.Write(PIN_RST, PinValue.Low);
Thread.Sleep(50);
// 3. RST 拉高(释放复位)
_gpio.Write(PIN_RST, PinValue.High);
Thread.Sleep(100);
// 4. RST拉高后,CS 出现一次 高→低→高(文档核心要求)
_gpio.Write(PIN_CS, PinValue.Low);
Thread.Sleep(10);
_gpio.Write(PIN_CS, PinValue.High);
Thread.Sleep(10);
Console.WriteLine("初始化时序执行完成");
}
// ====================
// 写 CS42448 寄存器(按文档流程)
// ====================
private static void WriteCS42448Register(byte regAddr, byte value)
{
// 1. CS 拉低
_gpio.Write(PIN_CS, PinValue.Low);
Thread.Sleep(1);
// 2. 先发 write control byte (寄存器地址) + 数据
byte[] writeBuffer = { regAddr, value };
_spi.Write(writeBuffer);
Thread.Sleep(1);
// 3. CS 拉高
_gpio.Write(PIN_CS, PinValue.High);
Thread.Sleep(5);
}
// ====================
// 读 CS42448 寄存器(按文档流程)
// ====================
private static byte ReadCS42448Register(byte regAddr)
{
byte readValue = 0xFF;
// 1. CS 拉低
_gpio.Write(PIN_CS, PinValue.Low);
Thread.Sleep(1);
// 2. 先发 write control byte (寄存器地址)
_spi.Write(new byte[] { regAddr });
Thread.Sleep(1);
// 3. 再发 read control byte (0x80 | 地址)
byte readCmd = (byte)(0x80 | regAddr);
_spi.Write(new byte[] { readCmd });
Thread.Sleep(1);
// 4. 时钟出数据(发0x00换数据)
byte[] readBuffer = { 0x00 };
_spi.Write(readBuffer);
readValue = readBuffer[0];
Thread.Sleep(1);
// 5. CS 拉高
_gpio.Write(PIN_CS, PinValue.High);
Thread.Sleep(5);
return readValue;
}
}
}

浙公网安备 33010602011771号