106 lines
2.9 KiB
C
106 lines
2.9 KiB
C
|
|
/*
|
|||
|
|
* AW816 数字功放驱动
|
|||
|
|
* 从 STM32 HAL 库移植到 AT32F403A,使用硬件 I2C1(PB6=SCL / PB7=SDA)。
|
|||
|
|
* - HAL_I2C_Master_Transmit/Receive -> i2c_master_transmit/receive(官方 i2c_application 库)
|
|||
|
|
* - HAL_Delay -> wk_delay_ms
|
|||
|
|
* 依赖:middlewares/i2c_application_library(已加入工程)
|
|||
|
|
*/
|
|||
|
|
#include "Drv_AW816.h"
|
|||
|
|
#include "i2c_application.h"
|
|||
|
|
#include "wk_system.h"
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 硬件 I2C1 发送(Master 无寄存器发送)
|
|||
|
|
* dev_addr 传 8 位格式地址(AW816_WR_ADDR=0xB4)
|
|||
|
|
* ============================== */
|
|||
|
|
static int AW816_HW_I2C_Send(uint16_t dev_addr, uint8_t *buf, uint8_t len)
|
|||
|
|
{
|
|||
|
|
i2c_handle_type hi2c;
|
|||
|
|
i2c_status_type status;
|
|||
|
|
|
|||
|
|
hi2c.i2cx = AW816_I2C;
|
|||
|
|
|
|||
|
|
status = i2c_master_transmit(&hi2c, dev_addr, buf, len, AW816_I2C_TIMEOUT);
|
|||
|
|
return (status == I2C_OK) ? 0 : -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 硬件 I2C1 读取(Master 标准接收)
|
|||
|
|
* dev_addr 传 8 位格式地址(AW816_RD_ADDR=0xB5)
|
|||
|
|
* ============================== */
|
|||
|
|
static int AW816_HW_I2C_Read(uint16_t dev_addr, uint8_t *buf, uint8_t len)
|
|||
|
|
{
|
|||
|
|
i2c_handle_type hi2c;
|
|||
|
|
i2c_status_type status;
|
|||
|
|
|
|||
|
|
hi2c.i2cx = AW816_I2C;
|
|||
|
|
|
|||
|
|
status = i2c_master_receive(&hi2c, dev_addr, buf, len, AW816_I2C_TIMEOUT);
|
|||
|
|
return (status == I2C_OK) ? 0 : -1;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 发送指令帧
|
|||
|
|
* ============================== */
|
|||
|
|
static void AW816_Send_Cmd(uint8_t ctrl, uint8_t index, uint8_t lo, uint8_t hi)
|
|||
|
|
{
|
|||
|
|
uint8_t tx_buf[6] = {ctrl, 0x03, index, lo, hi, 0x16};
|
|||
|
|
AW816_HW_I2C_Send(AW816_WR_ADDR, tx_buf, 6);
|
|||
|
|
wk_delay_ms(5);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 初始化
|
|||
|
|
* ============================== */
|
|||
|
|
void AW816_Init(void)
|
|||
|
|
{
|
|||
|
|
/* I2C1 由 wk_i2c1_init() 或 AD85020B 的 I2C 初始化配置好,
|
|||
|
|
AW816 与其共用同一 I2C1 总线,此处无需重复初始化 GPIO/I2C。 */
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 设置音量
|
|||
|
|
* ============================== */
|
|||
|
|
void AW816_Set_Volume(int16_t vol)
|
|||
|
|
{
|
|||
|
|
if(vol < -9000) vol = -9000;
|
|||
|
|
if(vol > 1200) vol = 1200;
|
|||
|
|
|
|||
|
|
uint16_t val;
|
|||
|
|
if(vol < 0)
|
|||
|
|
{
|
|||
|
|
val = 65536U + (uint16_t)vol;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
val = (uint16_t)vol;
|
|||
|
|
}
|
|||
|
|
AW816_Send_Cmd(0x92, 0x02, val & 0xFF, val >> 8);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 设置混响
|
|||
|
|
* ============================== */
|
|||
|
|
void AW816_Set_Reverb(uint8_t rev)
|
|||
|
|
{
|
|||
|
|
if(rev > 100) rev = 100;
|
|||
|
|
AW816_Send_Cmd(0x95, 0x07, rev, 0x00);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
/* ==============================
|
|||
|
|
* 读取寄存器数据
|
|||
|
|
* ============================== */
|
|||
|
|
uint8_t AW816_Read_Cmd(uint8_t ctrl, uint8_t *out_lo, uint8_t *out_hi)
|
|||
|
|
{
|
|||
|
|
uint8_t tx_buf[3] = {ctrl, 0x00, 0x16};
|
|||
|
|
uint8_t rx_buf[2] = {0};
|
|||
|
|
|
|||
|
|
AW816_HW_I2C_Send(AW816_WR_ADDR, tx_buf, 3);
|
|||
|
|
wk_delay_ms(5);
|
|||
|
|
AW816_HW_I2C_Read(AW816_RD_ADDR, rx_buf, 2);
|
|||
|
|
|
|||
|
|
*out_lo = rx_buf[0];
|
|||
|
|
*out_hi = rx_buf[1];
|
|||
|
|
return 0;
|
|||
|
|
}
|