Skip to content

Commit dc19c5c

Browse files
committed
Init screen
1 parent c0e7df1 commit dc19c5c

9 files changed

Lines changed: 174 additions & 58 deletions

File tree

ch32v305/src/feature/fm_audio_out/fm_audio_out.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
#include "hw/dac.h"
44

5-
#define FM_AUDIO_OUT_GAIN_DEFAULT_Q16 (16UL << 8)
5+
#define FM_AUDIO_OUT_GAIN_DEFAULT_Q16 0UL
66

77
/* State mirrors the fixed-point path: discriminator -> 4-sample CIC -> deemph -> DAC DMA. */
88
static int32_t s_i_prev = 0;

ch32v305/src/hw/display/st7789.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,7 @@ void ST7789_Init(void)
223223
ST7789_UnSelect();
224224

225225
Delay_Ms(50);
226-
ST7789_Fill_Color(WHITE); // Fill with Black.
226+
ST7789_Fill_Color(BLACK); // Fill with Black.
227227
}
228228

229229
/**

ch32v305/src/hw/tlv320adc6120.c

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@
108108

109109
/*
110110
* CHx_CFG1 (0x3D / 0x42):
111-
* - bit7: CHx_GAIN_SIGN_BIT = 0, positive gain
112-
* - bits6:0: CHx_GAIN = 0d, 0.0 dB in 0.5-dB steps
111+
* - bits7:1: CHx_GAIN = 0..84, 0.0 dB to 42.0 dB in 0.5-dB steps
112+
* - bit0: CHx_GAIN_SIGN_BIT = 0 for positive gain, 1 for negative gain
113113
*/
114114
#define TLV320_CH_CFG1_GAIN_0DB 0x00U
115115

@@ -187,6 +187,26 @@ ErrorStatus tlv320adc6120_hw_set_ch_gain_raw(uint8_t gain_raw)
187187
return READY;
188188
}
189189

190+
ErrorStatus tlv320adc6120_hw_set_ch_gain_db_x2(int8_t gain_db_x2)
191+
{
192+
uint8_t magnitude_db_x2;
193+
uint8_t sign_bit;
194+
195+
if(gain_db_x2 < TLV320ADC6120_CH_GAIN_MIN_DB_X2)
196+
{
197+
gain_db_x2 = TLV320ADC6120_CH_GAIN_MIN_DB_X2;
198+
}
199+
if(gain_db_x2 > (int8_t)TLV320ADC6120_CH_GAIN_MAX_DB_X2)
200+
{
201+
gain_db_x2 = (int8_t)TLV320ADC6120_CH_GAIN_MAX_DB_X2;
202+
}
203+
204+
sign_bit = (gain_db_x2 < 0) ? 1U : 0U;
205+
magnitude_db_x2 = (uint8_t)((gain_db_x2 < 0) ? -gain_db_x2 : gain_db_x2);
206+
207+
return tlv320adc6120_hw_set_ch_gain_raw((uint8_t)((magnitude_db_x2 << 1U) | sign_bit));
208+
}
209+
190210
ErrorStatus tlv320adc6120_hw_init(void)
191211
{
192212
/* Page 0 (default after POR; set explicitly after any page use). */

ch32v305/src/hw/tlv320adc6120.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,20 @@
1313
* Use 0 for 1.8 V AVDD with AREG shorted to AVDD (external AREG path).
1414
*/
1515

16+
#include <stdint.h>
17+
1618
#include "debug.h"
1719

1820
#ifndef TLV320ADC6120_USE_INTERNAL_AREG
1921
#define TLV320ADC6120_USE_INTERNAL_AREG 1
2022
#endif
2123

2224
#define TLV320ADC6120_I2C_ADDR_7BIT 0x4EU
25+
#define TLV320ADC6120_CH_GAIN_MIN_DB_X2 (-22)
26+
#define TLV320ADC6120_CH_GAIN_MAX_DB_X2 84U
2327

2428
[[nodiscard]] ErrorStatus tlv320adc6120_hw_init(void);
2529
[[nodiscard]] ErrorStatus tlv320adc6120_hw_set_ch_gain_raw(uint8_t gain_raw);
30+
[[nodiscard]] ErrorStatus tlv320adc6120_hw_set_ch_gain_db_x2(int8_t gain_db_x2);
2631

2732
#endif

ch32v305/src/hw/usb.c

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ static uint8_t usb_hw_tlv320_gain_req[USB_HW_TLV320_GAIN_REQ_SIZE];
2424
static uint8_t usb_hw_pll_lock_state[USB_HW_PLL_LOCK_STATE_SIZE];
2525
static ErrorStatus usb_hw_clk_freq_status = NoREADY;
2626
static uint8_t usb_hw_tlv320_gain_raw = 0x00U;
27+
static int8_t usb_hw_tlv320_gain_db_x2 = 0;
2728
static ErrorStatus usb_hw_tlv320_gain_status = NoREADY;
2829
static volatile uint32_t usb_hw_vendor_total_word_count = 0U;
2930
static volatile uint32_t usb_hw_vendor_dropped_word_count = 0U;
@@ -160,6 +161,32 @@ ErrorStatus usb_hw_set_tlv320_gain_raw(uint8_t gain_raw)
160161
return usb_hw_tlv320_gain_status;
161162
}
162163

164+
ErrorStatus usb_hw_set_tlv320_gain_db_x2(int8_t gain_db_x2)
165+
{
166+
uint8_t magnitude_db_x2 = (uint8_t)((gain_db_x2 < 0) ? -gain_db_x2 : gain_db_x2);
167+
char sign = (gain_db_x2 < 0) ? '-' : '+';
168+
169+
usb_hw_tlv320_gain_status = tlv320adc6120_hw_set_ch_gain_db_x2(gain_db_x2);
170+
if(usb_hw_tlv320_gain_status == READY)
171+
{
172+
usb_hw_tlv320_gain_db_x2 = gain_db_x2;
173+
printf("TLV320: channel gain set to %c%lu.%lu dB\r\n",
174+
sign,
175+
(unsigned long)(magnitude_db_x2 / 2U),
176+
(unsigned long)((magnitude_db_x2 & 1U) * 5U));
177+
}
178+
else
179+
{
180+
printf("TLV320: channel gain set failed for %c%lu.%lu dB (status %u)\r\n",
181+
sign,
182+
(unsigned long)(magnitude_db_x2 / 2U),
183+
(unsigned long)((magnitude_db_x2 & 1U) * 5U),
184+
(unsigned int)usb_hw_tlv320_gain_status);
185+
}
186+
187+
return usb_hw_tlv320_gain_status;
188+
}
189+
163190
ErrorStatus usb_hw_get_clk_freq_status(void)
164191
{
165192
return usb_hw_clk_freq_status;
@@ -298,7 +325,7 @@ bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_requ
298325
}
299326
if(stage == CONTROL_STAGE_DATA)
300327
{
301-
return (usb_hw_set_tlv320_gain_raw(usb_hw_tlv320_gain_req[0]) == READY);
328+
return (usb_hw_set_tlv320_gain_db_x2((int8_t)usb_hw_tlv320_gain_req[0]) == READY);
302329
}
303330
return true;
304331

ch32v305/src/hw/usb.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ uint32_t usb_receive_data(uint8_t *buffer, uint32_t len);
1919
ErrorStatus usb_hw_set_clk_freq_hz(uint64_t hz);
2020
ErrorStatus usb_hw_get_clk_freq_status(void);
2121
ErrorStatus usb_hw_set_tlv320_gain_raw(uint8_t gain_raw);
22+
ErrorStatus usb_hw_set_tlv320_gain_db_x2(int8_t gain_db_x2);
2223
ErrorStatus usb_hw_get_pll_lock(uint8_t *locked);
2324

2425
#endif

ch32v305/src/main.cpp

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,35 +161,56 @@ static void TLV320_I2S_Poll(void)
161161
}
162162
}
163163

164+
static uint8_t s_i2s_bitslip_check = 1U;
165+
static uint8_t s_i2s_bitslip_false_checks_in_a_row = 0U;
166+
static uint8_t s_i2s_bitslip_sync_displayed_count = UINT8_MAX;
167+
164168
static void TLV320_I2S_CheckBitslip(void)
165169
{
166-
static uint8_t enabled = 1U;
167-
static uint8_t false_checks_in_a_row = 0U;
168170

169-
if(enabled == 0U)
171+
if(s_i2s_bitslip_check == 0U)
170172
{
171173
return;
172174
}
173175

174176
if(i2s_needs_reset())
175177
{
176-
false_checks_in_a_row = 0U;
178+
s_i2s_bitslip_false_checks_in_a_row = 0U;
177179
printf("bitslipped, resetting\n");
178180
i2s_hw_enable(DISABLE);
179181
i2s_hw_deinit();
180182
Delay_Ms(10);
181183
i2s_hw_init();
182184
i2s_hw_enable(ENABLE);
183185
s_tlv320_i2s_report_initialized = 0U;
184-
enabled = 1U;
186+
s_i2s_bitslip_check = 1U;
185187
return;
186188
}
187189

188-
++false_checks_in_a_row;
189-
if(false_checks_in_a_row >= 20U)
190+
++s_i2s_bitslip_false_checks_in_a_row;
191+
if(s_i2s_bitslip_false_checks_in_a_row >= 20U)
192+
{
193+
s_i2s_bitslip_check = 0U;
194+
}
195+
}
196+
197+
static void Draw_I2S_Sync_Status(void)
198+
{
199+
char sync_text[24];
200+
201+
if(s_i2s_bitslip_sync_displayed_count == s_i2s_bitslip_false_checks_in_a_row)
190202
{
191-
enabled = 0U;
203+
return;
192204
}
205+
206+
s_i2s_bitslip_sync_displayed_count = s_i2s_bitslip_false_checks_in_a_row;
207+
snprintf(sync_text, sizeof(sync_text),
208+
"Synced: %u/20",
209+
(unsigned int)s_i2s_bitslip_false_checks_in_a_row);
210+
211+
ST7789_Fill(0U, 0U, ST7789_WIDTH - 1U, 49U, BLACK);
212+
ST7789_WriteString(0U, 5U, "Initializing...", Font_11x18, WHITE, BLACK);
213+
ST7789_WriteString(0U, 27U, sync_text, Font_11x18, WHITE, BLACK);
193214
}
194215

195216
static uint64_t ticks_from_ms(uint32_t ms)
@@ -256,8 +277,6 @@ int main(void)
256277
printf("FM audio out: enabled (fixed-point FM demod to DAC)\r\n");
257278

258279
blinky_init();
259-
UI_FFT_Init();
260-
UI_Init();
261280

262281
//watchdog_init();
263282

@@ -267,6 +286,16 @@ int main(void)
267286
PeriodicTrigger SysTickReportUSB{1000U, SysTick_Report_USB_EverySecond};
268287
PeriodicTrigger FFTDraw{1000U / 60U, UI_FFT_Draw};
269288

289+
while(s_i2s_bitslip_check)
290+
{
291+
Draw_I2S_Sync_Status();
292+
I2SBitslipCheck();
293+
tud_task();
294+
}
295+
296+
UI_FFT_Init();
297+
UI_Init();
298+
270299
while(1)
271300
{
272301
I2SBitslipCheck();

ch32v305/src/ui/fft.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,6 @@ void UI_FFT_Init(void)
162162
s_fft_interp_instance.pData = i2s_fft_sample_arr;
163163
i2s_fft_sample_arr_reset();
164164

165-
ST7789_Fill_Color(BLACK);
166165
s_waterfall_line = ST7789_ScrollRows(FFT_WATERFALL_TOP, FFT_WATERFALL_BOTTOM, 0);
167166
}
168167

0 commit comments

Comments
 (0)