update i2s settings

This commit is contained in:
Steven Dan
2026-03-21 16:58:45 +08:00
parent dbdec19fd0
commit 90df0a435a
2 changed files with 81 additions and 69 deletions

View File

@@ -62,8 +62,9 @@
<Port Location="XS1_PORT_1D" Name="PORT_MCLK_IN"/> <Port Location="XS1_PORT_1D" Name="PORT_MCLK_IN"/>
<Port Location="XS1_PORT_1C" Name="PORT_I2S_BCLK"/> <Port Location="XS1_PORT_1C" Name="PORT_I2S_BCLK"/>
<Port Location="XS1_PORT_1B" Name="PORT_I2S_LRCLK"/> <Port Location="XS1_PORT_1B" Name="PORT_I2S_LRCLK"/>
<Port Location="XS1_PORT_1K" Name="PORT_I2S_ADC0"/> <Port Location="XS1_PORT_1A" Name="PORT_I2S_ADC0"/>
<Port Location="XS1_PORT_1A" Name="PORT_I2S_DAC0"/> <Port Location="XS1_PORT_1K" Name="PORT_I2S_DAC0"/>
<Port Location="XS1_PORT_4D" Name="PORT_CTL_MUTE"/>
</Tile> </Tile>
</Node> </Node>
</Nodes> </Nodes>

View File

@@ -1,7 +1,7 @@
#if UART_DEBUG || DEBUG_MEMORY_LOG_ENABLED #if UART_DEBUG || DEBUG_MEMORY_LOG_ENABLED
#define DEBUG_PRINT_ENABLE 1 #define DEBUG_PRINT_ENABLE 1
#else #else
#define DEBUG_PRINT_ENABLE 0 #define DEBUG_PRINT_ENABLE 1
#endif #endif
#include <xs1.h> #include <xs1.h>
@@ -54,7 +54,7 @@ static const uint16_t nau88c22_registers[][4] = {
{0x00, 0x28, 0x00, 0x00}, {0x00, 0x28, 0x00, 0x00},
{0x00, 0x29, 0x00, 0x00}, {0x00, 0x29, 0x00, 0x00},
{0x00, 0x2A, 0x00, 0x00}, {0x00, 0x2A, 0x00, 0x00},
{0x00, 0x2B, 0x40, 0x02}, {0x00, 0x2B, 0x40, 0x02}, // ADC Right Channel Source Select, bit 3, set to 1, 3rd byte 0x40 = Latch left channel analog data input into the right channel filter
{0x00, 0x2C, 0x00, 0x82}, {0x00, 0x2C, 0x00, 0x82},
{0x00, 0x2D, 0x00, 0x00}, {0x00, 0x2D, 0x00, 0x00},
{0x00, 0x2F, 0x00, 0x00}, {0x00, 0x2F, 0x00, 0x00},
@@ -209,6 +209,7 @@ on tile[0]: in port p_hp_enc = PORT_HP_GAIN_ENCODER; /* XS1_PORT_8D */
on tile[0]: in port p_btn_music = PORT_BUTTON_MUSIC_MODE; /* XS1_PORT_1A */ on tile[0]: in port p_btn_music = PORT_BUTTON_MUSIC_MODE; /* XS1_PORT_1A */
on tile[0]: in port p_btn_game = PORT_BUTTON_GAME_MODE; /* XS1_PORT_1L */ on tile[0]: in port p_btn_game = PORT_BUTTON_GAME_MODE; /* XS1_PORT_1L */
on tile[0]: in port p_btn_ai71 = PORT_BUTTON_AI71_ONOFF; /* XS1_PORT_1M */ on tile[0]: in port p_btn_ai71 = PORT_BUTTON_AI71_ONOFF; /* XS1_PORT_1M */
on tile[1]: out port p_ctl_mute = PORT_CTL_MUTE; /* XS1_PORT_4D */
/* Board setup for SY102 hardware */ /* Board setup for SY102 hardware */
void board_setup() void board_setup()
@@ -222,14 +223,18 @@ void board_setup()
} }
/* Write a 16-bit register on the NAU88C21 codec (reg and val are both 16-bit) */ /* Write a 16-bit register on the NAU88C21 codec (reg and val are both 16-bit) */
static void CODEC_NAU_REGWRITE(uint16_t reg, uint16_t val) static void CODEC_NAU_REGWRITE(client interface i2c_master_if i2c, uint16_t reg, uint16_t val)
{ {
uint8_t buf[4] = {(uint8_t)(reg >> 8), (uint8_t)(reg & 0xFF), unsafe {
(uint8_t)(val >> 8), (uint8_t)(val & 0xFF)}; i2c.write_reg16(NAU88C21_I2C_ADDR, reg, val);
size_t n; }
unsafe { i_i2c_client.write(NAU88C21_I2C_ADDR, buf, 4, n, 1); }
} }
static void CODEC_NAU_REGREAD(client interface i2c_master_if i2c, unsigned reg, uint16_t &val)
{
i2c_regop_res_t result;
val = i2c.read_reg16(NAU88C21_I2C_ADDR, reg, result);
}
/* NAU88C21 mic PGA gain: reg 0x7E, value = (level << 8), range 1..38 */ /* NAU88C21 mic PGA gain: reg 0x7E, value = (level << 8), range 1..38 */
uint8_t mic_vol = 19; /* default mid gain (~+18dB) */ uint8_t mic_vol = 19; /* default mid gain (~+18dB) */
@@ -238,48 +243,48 @@ uint8_t mic_vol = 19; /* default mid gain (~+18dB) */
#define MIC_VOL_STEP 1 #define MIC_VOL_STEP 1
#define MIC_VOL_THRD 0 #define MIC_VOL_THRD 0
int i2c_codec_mic_vol_up(void) int i2c_codec_mic_vol_up(client interface i2c_master_if i2c)
{ {
if (mic_vol < MAX_MIC_VOL_LEVEL) { if (mic_vol < MAX_MIC_VOL_LEVEL) {
mic_vol++; mic_vol++;
CODEC_NAU_REGWRITE(0x007E, (uint16_t)mic_vol << 8); CODEC_NAU_REGWRITE(i2c, 0x007E, (uint16_t)mic_vol << 8);
} }
return 0; return 0;
} }
int i2c_codec_mic_vol_down(void) int i2c_codec_mic_vol_down(client interface i2c_master_if i2c)
{ {
if (mic_vol > MIN_MIC_VOL_LEVEL) { if (mic_vol > MIN_MIC_VOL_LEVEL) {
mic_vol--; mic_vol--;
CODEC_NAU_REGWRITE(0x007E, (uint16_t)mic_vol << 8); CODEC_NAU_REGWRITE(i2c, 0x007E, (uint16_t)mic_vol << 8);
} }
return 0; return 0;
} }
/* ========================================================================= /* =========================================================================
* HTR3236 helper functions (use shared i_i2c_client) * HTR3236 helper functions (use shared i_i2c_client)
* ========================================================================= */ * ========================================================================= */
static void htr3236_write_reg(uint8_t reg, uint8_t val) static void htr3236_write_reg(client interface i2c_master_if i2c, uint8_t reg, uint8_t val)
{ {
uint8_t buf[2] = {reg, val}; uint8_t buf[2] = {reg, val};
size_t n; size_t n;
unsafe { i_i2c_client.write(HTR3236_I2C_ADDR, buf, 2, n, 1); } unsafe { i2c.write(HTR3236_I2C_ADDR, buf, 2, n, 1); }
} }
static void htr3236_led_init(void) static void htr3236_led_init(client interface i2c_master_if i2c)
{ {
htr3236_write_reg(0x00, 0x01); /* software wake: normal op */ htr3236_write_reg(i2c, 0x00, 0x01); /* software wake: normal op */
htr3236_write_reg(0x4B, 0x01); /* set PWM frequency to 22kHz */ htr3236_write_reg(i2c, 0x4B, 0x01); /* set PWM frequency to 22kHz */
/* Configure all 35 channels: IMAX/2 current, enable=1, PWM=0 */ /* Configure all 35 channels: IMAX/2 current, enable=1, PWM=0 */
for (int ch = 1; ch <= 35; ch++) { for (int ch = 1; ch <= 35; ch++) {
htr3236_write_reg((uint8_t)(0x25 + ch), 0x03); /* LED ctrl reg */ htr3236_write_reg(i2c, (uint8_t)(0x25 + ch), 0x03); /* LED ctrl reg */
htr3236_write_reg((uint8_t)ch, 0); /* PWM = 0 */ htr3236_write_reg(i2c, (uint8_t)ch, 0); /* PWM = 0 */
} }
htr3236_write_reg(0x25, 0x00); /* latch update */ htr3236_write_reg(i2c, 0x25, 0x00); /* latch update */
} }
static void htr3236_led_on(uint8_t ch) { htr3236_write_reg(ch, 1); } static void htr3236_led_on(client interface i2c_master_if i2c, uint8_t ch) { htr3236_write_reg(i2c, ch, 1); }
static void htr3236_led_off(uint8_t ch) { htr3236_write_reg(ch, 0); } static void htr3236_led_off(client interface i2c_master_if i2c, uint8_t ch) { htr3236_write_reg(i2c, ch, 0); }
static void htr3236_do_update(void) { htr3236_write_reg(0x25, 0x00); } static void htr3236_do_update(client interface i2c_master_if i2c) { htr3236_write_reg(i2c, 0x25, 0x00); }
/* Map mic_vol (0..MAX_MIC_VOL_LEVEL) to LED bar count (0..15) */ /* Map mic_vol (0..MAX_MIC_VOL_LEVEL) to LED bar count (0..15) */
static int mic_vol_to_led_count(int v) static int mic_vol_to_led_count(int v)
@@ -326,17 +331,22 @@ extern "C" {
void xc_chan_out_byte(chanend c, unsigned char b); void xc_chan_out_byte(chanend c, unsigned char b);
} }
void codec_init(void) void codec_init(client interface i2c_master_if i2c)
{ {
/* Write all NAU88C21 registers from the init table */ /* Write all NAU88C21 registers from the init table */
int n = sizeof(nau88c22_registers) / sizeof(nau88c22_registers[0]); int n = sizeof(nau88c22_registers) / sizeof(nau88c22_registers[0]);
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
uint16_t reg = nau88c22_registers[i][1]; uint16_t reg = nau88c22_registers[i][1];
uint16_t val = ((uint16_t)nau88c22_registers[i][2] << 8) | nau88c22_registers[i][3]; uint16_t val = ((uint16_t)nau88c22_registers[i][2] << 8) | nau88c22_registers[i][3];
CODEC_NAU_REGWRITE(reg, val); uint16_t val_read;
debug_printf("NAU88C22 reg write 0x%04x = 0x%04x\n", reg, val);
CODEC_NAU_REGWRITE(i2c, reg, val);
CODEC_NAU_REGREAD(i2c, reg, val_read);
debug_printf("NAU88C22 reg read 0x%04x = 0x%04x\n", reg, val_read);
if (i == 0) delay_milliseconds(10); /* wait for software reset to complete */
} }
/* Set default mic PGA gain */ /* Set default mic PGA gain */
CODEC_NAU_REGWRITE(0x007E, (uint16_t)mic_vol << 8); CODEC_NAU_REGWRITE(i2c, 0x007E, (uint16_t)mic_vol << 8);
delay_milliseconds(20); delay_milliseconds(20);
} }
@@ -356,7 +366,7 @@ void codec_init(void)
* p_btn_game (PORT_1L) = game/spatial mode * p_btn_game (PORT_1L) = game/spatial mode
* p_btn_ai71 (PORT_1M) = AI 7.1 virtual surround on/off * p_btn_ai71 (PORT_1M) = AI 7.1 virtual surround on/off
*/ */
void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol) void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol, client interface i2c_master_if i2c)
{ {
timer tmr; timer tmr;
uint32_t t_val; uint32_t t_val;
@@ -385,11 +395,11 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
static unsigned isMicMute = 0; static unsigned isMicMute = 0;
static unsigned isHpMute = 0; static unsigned isHpMute = 0;
delay_milliseconds(20); delay_milliseconds(100);
codec_init(); codec_init(i2c);
/* Initialize HTR3236 LED driver over shared I2C bus */ /* Initialize HTR3236 LED driver over shared I2C bus */
htr3236_led_init(); htr3236_led_init(i2c);
delay_milliseconds(500); delay_milliseconds(500);
debug_printf("button task start\n"); debug_printf("button task start\n");
@@ -418,15 +428,15 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
debug_printf("Sent audio_mode %d to hid_button_task via cc_mic_level channel\n", saved_mode); debug_printf("Sent audio_mode %d to hid_button_task via cc_mic_level channel\n", saved_mode);
/* Light initial mode LED (exclusive: only one mode LED on) */ /* Light initial mode LED (exclusive: only one mode LED on) */
if (saved_mode == 0) { htr3236_led_on(LED_MUSIC_CH); flag_music_mode = 1; } if (saved_mode == 0) { htr3236_led_on(i2c, LED_MUSIC_CH); flag_music_mode = 1; }
else if (saved_mode == 1) { htr3236_led_on(LED_GAME_CH); flag_game_mode = 1; } else if (saved_mode == 1) { htr3236_led_on(i2c, LED_GAME_CH); flag_game_mode = 1; }
else if (saved_mode == 2) { htr3236_led_on(LED_AI71_CH); flag_ai71_onoff = 1; } else if (saved_mode == 2) { htr3236_led_on(i2c, LED_AI71_CH); flag_ai71_onoff = 1; }
/* Light initial mic volume LED bar */ /* Light initial mic volume LED bar */
{ {
int led_cnt = mic_vol_to_led_count(mic_vol); int led_cnt = mic_vol_to_led_count(mic_vol);
for (int i = 0; i < led_cnt; i++) htr3236_led_on(led_l_map[i]); for (int i = 0; i < led_cnt; i++) htr3236_led_on(i2c, led_l_map[i]);
} }
htr3236_do_update(); htr3236_do_update(i2c);
/* Read initial mic mute state from hardware */ /* Read initial mic mute state from hardware */
{ {
@@ -503,21 +513,21 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
if (mic_a) { /* CW: vol up */ if (mic_a) { /* CW: vol up */
if (isMicMute == 0) { if (isMicMute == 0) {
uint8_t old_cnt = (uint8_t)mic_vol_to_led_count(mic_vol); uint8_t old_cnt = (uint8_t)mic_vol_to_led_count(mic_vol);
i2c_codec_mic_vol_up(); i2c_codec_mic_vol_up(i2c);
uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(mic_vol); uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(mic_vol);
if (new_cnt > old_cnt) { if (new_cnt > old_cnt) {
htr3236_led_on(led_l_map[new_cnt - 1]); htr3236_led_on(i2c, led_l_map[new_cnt - 1]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
} }
} else { /* CCW: vol down */ } else { /* CCW: vol down */
if (isMicMute == 0) { if (isMicMute == 0) {
uint8_t old_cnt = (uint8_t)mic_vol_to_led_count(mic_vol); uint8_t old_cnt = (uint8_t)mic_vol_to_led_count(mic_vol);
i2c_codec_mic_vol_down(); i2c_codec_mic_vol_down(i2c);
uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(mic_vol); uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(mic_vol);
if (new_cnt < old_cnt) { if (new_cnt < old_cnt) {
htr3236_led_off(led_l_map[old_cnt - 1]); htr3236_led_off(i2c, led_l_map[old_cnt - 1]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
} }
} }
@@ -551,8 +561,8 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
if (hp_vol > MAX_MIC_VOL_LEVEL) hp_vol = MAX_MIC_VOL_LEVEL; if (hp_vol > MAX_MIC_VOL_LEVEL) hp_vol = MAX_MIC_VOL_LEVEL;
uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(hp_vol); uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(hp_vol);
if (new_cnt > old_cnt) { if (new_cnt > old_cnt) {
htr3236_led_on(led_d_map[new_cnt - 1]); htr3236_led_on(i2c, led_d_map[new_cnt - 1]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
} }
#if (HID_CONTROLS == 1) #if (HID_CONTROLS == 1)
@@ -565,8 +575,8 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
if (hp_vol < 0) hp_vol = 0; if (hp_vol < 0) hp_vol = 0;
uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(hp_vol); uint8_t new_cnt = (uint8_t)mic_vol_to_led_count(hp_vol);
if (new_cnt < old_cnt) { if (new_cnt < old_cnt) {
htr3236_led_off(led_d_map[old_cnt - 1]); htr3236_led_off(i2c, led_d_map[old_cnt - 1]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
} }
#if (HID_CONTROLS == 1) #if (HID_CONTROLS == 1)
@@ -594,8 +604,8 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
/* Turn off L-series bar */ /* Turn off L-series bar */
{ {
int led_cnt = mic_vol_to_led_count(mic_vol); int led_cnt = mic_vol_to_led_count(mic_vol);
for (int i = 0; i < led_cnt; i++) htr3236_led_off(led_l_map[i]); for (int i = 0; i < led_cnt; i++) htr3236_led_off(i2c, led_l_map[i]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
debug_printf("Mic MUTED\n"); debug_printf("Mic MUTED\n");
} else { } else {
@@ -604,8 +614,8 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
/* Restore L-series bar */ /* Restore L-series bar */
{ {
int led_cnt = mic_vol_to_led_count(mic_vol); int led_cnt = mic_vol_to_led_count(mic_vol);
for (int i = 0; i < led_cnt; i++) htr3236_led_on(led_l_map[i]); for (int i = 0; i < led_cnt; i++) htr3236_led_on(i2c, led_l_map[i]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
debug_printf("Mic UNMUTED\n"); debug_printf("Mic UNMUTED\n");
} }
@@ -621,12 +631,12 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
isHpMute = !isHpMute; isHpMute = !isHpMute;
if (isHpMute) { if (isHpMute) {
int led_cnt = mic_vol_to_led_count(hp_vol); int led_cnt = mic_vol_to_led_count(hp_vol);
for (int i = 0; i < led_cnt; i++) htr3236_led_off(led_d_map[i]); for (int i = 0; i < led_cnt; i++) htr3236_led_off(i2c, led_d_map[i]);
htr3236_do_update(); htr3236_do_update(i2c);
} else { } else {
int led_cnt = mic_vol_to_led_count(hp_vol); int led_cnt = mic_vol_to_led_count(hp_vol);
for (int i = 0; i < led_cnt; i++) htr3236_led_on(led_d_map[i]); for (int i = 0; i < led_cnt; i++) htr3236_led_on(i2c, led_d_map[i]);
htr3236_do_update(); htr3236_do_update(i2c);
} }
#if (HID_CONTROLS == 1) #if (HID_CONTROLS == 1)
update_button(HID_CONTROL_MUTE); update_button(HID_CONTROL_MUTE);
@@ -643,10 +653,10 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
btn_music_cnt++; btn_music_cnt++;
if (btn_music_cnt == 2) { if (btn_music_cnt == 2) {
flag_music_mode = 1; flag_game_mode = 0; flag_ai71_onoff = 0; flag_music_mode = 1; flag_game_mode = 0; flag_ai71_onoff = 0;
htr3236_led_on(LED_MUSIC_CH); htr3236_led_on(i2c, LED_MUSIC_CH);
htr3236_led_off(LED_GAME_CH); htr3236_led_off(i2c, LED_GAME_CH);
htr3236_led_off(LED_AI71_CH); htr3236_led_off(i2c, LED_AI71_CH);
htr3236_do_update(); htr3236_do_update(i2c);
unsigned new_mode = 0; unsigned new_mode = 0;
SET_SHARED_GLOBAL(g_game_mode, new_mode); SET_SHARED_GLOBAL(g_game_mode, new_mode);
{ {
@@ -666,10 +676,10 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
btn_game_cnt++; btn_game_cnt++;
if (btn_game_cnt == 2) { if (btn_game_cnt == 2) {
flag_music_mode = 0; flag_game_mode = 1; flag_ai71_onoff = 0; flag_music_mode = 0; flag_game_mode = 1; flag_ai71_onoff = 0;
htr3236_led_off(LED_MUSIC_CH); htr3236_led_off(i2c, LED_MUSIC_CH);
htr3236_led_on(LED_GAME_CH); htr3236_led_on(i2c, LED_GAME_CH);
htr3236_led_off(LED_AI71_CH); htr3236_led_off(i2c, LED_AI71_CH);
htr3236_do_update(); htr3236_do_update(i2c);
unsigned new_mode = 1; unsigned new_mode = 1;
SET_SHARED_GLOBAL(g_game_mode, new_mode); SET_SHARED_GLOBAL(g_game_mode, new_mode);
{ {
@@ -689,10 +699,10 @@ void button_task(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vol)
btn_ai71_cnt++; btn_ai71_cnt++;
if (btn_ai71_cnt == 2) { if (btn_ai71_cnt == 2) {
flag_music_mode = 0; flag_game_mode = 0; flag_ai71_onoff = 1; flag_music_mode = 0; flag_game_mode = 0; flag_ai71_onoff = 1;
htr3236_led_off(LED_MUSIC_CH); htr3236_led_off(i2c, LED_MUSIC_CH);
htr3236_led_off(LED_GAME_CH); htr3236_led_off(i2c, LED_GAME_CH);
htr3236_led_on(LED_AI71_CH); htr3236_led_on(i2c, LED_AI71_CH);
htr3236_do_update(); htr3236_do_update(i2c);
unsigned new_mode = 2; unsigned new_mode = 2;
SET_SHARED_GLOBAL(g_game_mode, new_mode); SET_SHARED_GLOBAL(g_game_mode, new_mode);
{ {
@@ -752,10 +762,9 @@ void AudioHwRemote(chanend c_hidSendData, chanend cc_mic_level, chanend c_uac_vo
i2c_master_if i2c[1]; i2c_master_if i2c[1];
board_setup(); board_setup();
par { par {
i2c_master(i2c, 1, p_scl, p_sda, 300); i2c_master(i2c, 1, p_scl, p_sda, 100);
{ {
unsafe {i_i2c_client = i2c[0];} button_task(c_hidSendData, cc_mic_level, c_uac_vol, i2c[0]);
button_task(c_hidSendData, cc_mic_level, c_uac_vol);
} }
} }
} }
@@ -770,6 +779,7 @@ void AudioHwInit()
// Wait for power supply to come up. // Wait for power supply to come up.
delay_milliseconds(200); delay_milliseconds(200);
p_ctl_mute <: 1;
} }
@@ -780,5 +790,6 @@ void AudioHwConfig(unsigned samFreq, unsigned mClk, unsigned dsdMode, unsigned s
{ {
sw_pll_fixed_clock(mClk); sw_pll_fixed_clock(mClk);
} }
p_ctl_mute <: 1;
} }