c# iot 控制 cs2448

using Iot.Device.Ft232H;
using Iot.Device.FtCommon;
using Iot.Device.Rtc;
using System;
using System.Device.I2c;

namespace Ft232h_Audio_RTC
{
    class Program
    {
        static void Main(string[] args)
        {
            // 1. 打开 FT232H
            var devices = FtCommon.GetDevices();
            if (devices.Count == 0)
            {
                Console.WriteLine("未找到 FT232H");
                return;
            }

            using var ft232h = new Ft232HDevice(devices[0]);

            // ==============================================
            // 2. 初始化 CS42448 (I2C 0x4F)
            // ==============================================
            var audioSettings = new I2cConnectionSettings(0, Cs42448.DefaultI2CAddress);
            using var audioI2c = ft232h.CreateI2cDevice(audioSettings);
            var cs42448 = new Cs42448(audioI2c);
            Console.WriteLine(" CS42448 初始化完成(已解除静音)");

            // ==============================================
            // 4. 测试:静音 / 取消静音
            // ==============================================
            while (true)
            {
                Console.WriteLine("\n按 M 静音,U 取消静音,Q 退出");
                var key = Console.ReadKey(true);

                if (key.Key == ConsoleKey.M)
                {
                    cs42448.SetMute(true);
                    Console.WriteLine(" 全局静音");
                }
                else if (key.Key == ConsoleKey.U)
                {
                    cs42448.SetMute(false);
                    Console.WriteLine(" 取消静音");
                }
                else if (key.Key == ConsoleKey.Q)
                {
                    break;
                }
            }
        }
    }
}

Cs42448.cs

using System;
using System.Device.I2c;
using System.Threading;

/// <summary>
/// CS42448 6-in/8-out Audio Codec (I2C control only)
/// </summary>
public class Cs42448
{
    // I2C 地址(AD0 pin 接地=0x4F,接VCC=0x4E)
    public const byte DefaultI2CAddress = 0x4F;

    private readonly I2cDevice _i2c;

    // 寄存器定义(来自 datasheet)
    public enum Reg : byte
    {
        ChipControl1 = 0x01,
        ChipControl2 = 0x02,
        MiscControl = 0x03,
        VolumeA = 0x04, // AOUT1-AOUT4
        VolumeB = 0x05, // AOUT5-AOUT8
        AdcConfig = 0x06,
        DacConfig = 0x07,
        // 其余寄存器可继续扩展
    }

    public Cs42448(I2cDevice i2cDevice)
    {
        _i2c = i2cDevice ?? throw new ArgumentNullException(nameof(i2cDevice));
        Initialize();
    }

    /// <summary>
    /// 上电初始化:复位、启动、解静音、标准配置
    /// </summary>
    private void Initialize()
    {
        // 1. 软复位
        WriteReg(Reg.ChipControl1, 0x80);
        Thread.Sleep(10);

        // 2. 正常运行,不省电
        WriteReg(Reg.ChipControl1, 0x00);

        // 3. 解静音(所有通道)
        WriteReg(Reg.ChipControl2, 0x00);

        // 4. 音频格式:I2S, 256x MCLK, 16/24bit auto
        WriteReg(Reg.MiscControl, 0x00);

        // 5. DAC 音量 0dB
        WriteReg(Reg.VolumeA, 0x00);
        WriteReg(Reg.VolumeB, 0x00);

        // 6. ADC/DAC 正常模式
        WriteReg(Reg.AdcConfig, 0x00);
        WriteReg(Reg.DacConfig, 0x00);
    }

    /// <summary>
    /// 写寄存器
    /// </summary>
    public void WriteReg(Reg reg, byte value)
    {
        Span<byte> buf = stackalloc byte[] { (byte)reg, value };
        _i2c.Write(buf);
    }

    /// <summary>
    /// 读寄存器
    /// </summary>
    public byte ReadReg(Reg reg)
    {
        _i2c.WriteByte((byte)reg);
        return _i2c.ReadByte();
    }

    /// <summary>
    /// 全局静音开关
    /// </summary>
    public void SetMute(bool mute)
    {
        byte val = mute ? (byte)0x80 : (byte)0x00;
        WriteReg(Reg.ChipControl2, val);
    }

    /// <summary>
    /// 设置输出音量(-127 ~ 0 dB)
    /// </summary>
    public void SetVolume(int db)
    {
        if (db < -127) db = -127;
        if (db > 0) db = 0;
        byte v = (byte)(-db); // 寄存器是反码
        WriteReg(Reg.VolumeA, v);
        WriteReg(Reg.VolumeB, v);
    }
}

 

posted @ 2026-04-16 19:19  微笑的''80  阅读(9)  评论(0)    收藏  举报