Fix merged example Sonar warnings

This commit is contained in:
Zixun LI
2026-06-29 14:58:32 +02:00
committed by HiFiPhile
parent b1db17ce18
commit a333ccbc39
8 changed files with 256 additions and 251 deletions

View File

@ -111,6 +111,7 @@ int main(void) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init();
return 0;
#else
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {
@ -126,8 +127,6 @@ int main(void) {
audio_task(NULL);
}
#endif
return 0;
}
//--------------------------------------------------------------------+
@ -168,19 +167,19 @@ void audio_task(void *param) {
(void) param;
static uint32_t start_ms = 0;
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms != curr_ms) {
start_ms = curr_ms;
tud_audio_write(i2s_dummy_buffer, AUDIO_SAMPLE_RATE / 1000 * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX);
}
#endif
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms != curr_ms) {
start_ms = curr_ms;
tud_audio_write(i2s_dummy_buffer, AUDIO_SAMPLE_RATE / 1000 * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_TX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_TX);
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
#else
return;
#endif
vTaskDelay(1);
}
#endif
}
//--------------------------------------------------------------------+
@ -427,21 +426,24 @@ void led_blinking_task(void *param) {
(void) param;
static bool led_state = false;
while (1) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
static uint32_t start_ms = 0;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
static uint32_t start_ms = 0;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state;// toggle
board_led_write(led_state);
led_state = 1 - led_state;// toggle
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
//--------------------------------------------------------------------+

View File

@ -93,6 +93,7 @@ int main(void) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init();
return 0;
#else
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {
@ -108,8 +109,6 @@ int main(void) {
audio_task(NULL);
}
#endif
return 0;
}
//--------------------------------------------------------------------+
@ -151,22 +150,22 @@ void audio_task(void *param) {
(void) param;
static uint32_t start_ms = 0;
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms != curr_ms) {
start_ms = curr_ms;
for (size_t cnt = 0; cnt < sizeof(test_buffer_audio) / 2; cnt++) {
test_buffer_audio[cnt] = startVal++;
}
tud_audio_write((uint8_t *) test_buffer_audio, sizeof(test_buffer_audio));
#endif
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms != curr_ms) {
start_ms = curr_ms;
for (size_t cnt = 0; cnt < sizeof(test_buffer_audio) / 2; cnt++) {
test_buffer_audio[cnt] = startVal++;
}
tud_audio_write((uint8_t *) test_buffer_audio, sizeof(test_buffer_audio));
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
#else
return;
#endif
vTaskDelay(1);
}
#endif
}
//--------------------------------------------------------------------+
@ -421,21 +420,24 @@ void led_blinking_task(void *param) {
(void) param;
static bool led_state = false;
while (1) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
static uint32_t start_ms = 0;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
static uint32_t start_ms = 0;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state;// toggle
board_led_write(led_state);
led_state = 1 - led_state;// toggle
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
//--------------------------------------------------------------------+

View File

@ -59,6 +59,7 @@ int main(void) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init();
return 0;
#else
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {.role = TUSB_ROLE_DEVICE, .speed = TUSB_SPEED_AUTO};
@ -74,8 +75,6 @@ int main(void) {
cdc_task(NULL);
}
#endif
return 0;
}
//--------------------------------------------------------------------+
@ -112,47 +111,47 @@ void tud_resume_cb(void) {
void cdc_task(void *param) {
(void) param;
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
// connected() check for DTR bit
// Most but not all terminal client set this when making connection
// if ( tud_cdc_connected() )
{
// connected and there are data available
while (tud_cdc_available()) {
// read data
char buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
(void)count;
#endif
// connected() check for DTR bit
// Most but not all terminal client set this when making connection
// if ( tud_cdc_connected() )
{
// connected and there are data available
while (tud_cdc_available()) {
// read data
char buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
(void)count;
// Echo back
// Note: Skip echo by commenting out write() and write_flush()
// for throughput test e.g
// $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
tud_cdc_write(buf, count);
}
tud_cdc_write_flush();
// Press on-board button to send Uart status notification
static cdc_notify_uart_state_t uart_state = {.value = 0};
static uint32_t btn_prev = 0;
const uint32_t btn = board_button_read();
if ((btn_prev == 0u) && (btn != 0u)) {
uart_state.dsr ^= 1;
uart_state.dcd ^= 1;
tud_cdc_notify_uart_state(&uart_state);
}
btn_prev = btn;
// Echo back
// Note: Skip echo by commenting out write() and write_flush()
// for throughput test e.g
// $ dd if=/dev/zero of=/dev/ttyACM0 count=10000
tud_cdc_write(buf, count);
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
#else
break;
#endif
tud_cdc_write_flush();
// Press on-board button to send Uart status notification
static cdc_notify_uart_state_t uart_state = {.value = 0};
static uint32_t btn_prev = 0;
const uint32_t btn = board_button_read();
if ((btn_prev == 0u) && (btn != 0u)) {
uart_state.dsr ^= 1;
uart_state.dcd ^= 1;
tud_cdc_notify_uart_state(&uart_state);
}
btn_prev = btn;
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
}
#endif
}
// Invoked when cdc when line state changed e.g connected/disconnected
@ -185,33 +184,34 @@ void led_blinking_task(void *param) {
static uint32_t start_ms = 0;
#endif
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
if (!blink_enable) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
continue;
#else
return;
#endif
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = !led_state;
#if CFG_TUSB_OS != OPT_OS_FREERTOS
break;
#endif
if (!blink_enable) {
vTaskDelay(1);
continue;
}
#else
if (!blink_enable) {
return;
}
#endif
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = !led_state;
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
//--------------------------------------------------------------------+

View File

@ -63,6 +63,7 @@ int main(void) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init();
return 0;
#else
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {.role = TUSB_ROLE_DEVICE, .speed = TUSB_SPEED_AUTO};
@ -76,8 +77,6 @@ int main(void) {
hid_task(NULL);
}
#endif
return 0;
}
//--------------------------------------------------------------------+
@ -218,36 +217,35 @@ static void send_hid_report(uint8_t report_id, uint32_t btn) {
void hid_task(void *param) {
(void) param;
while (1) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
vTaskDelay(pdMS_TO_TICKS(10));
#else
// Poll every 10ms
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
// Poll every 10ms
const uint32_t interval_ms = 10;
static uint32_t start_ms = 0;
if (tusb_time_millis_api() - start_ms < interval_ms) {
return; // not enough time
}
start_ms += interval_ms;
#endif
uint32_t const btn = board_button_read();
// Remote wakeup
if (tud_suspended() && btn != 0u) {
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
tud_remote_wakeup();
} else {
// Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
send_hid_report(REPORT_ID_KEYBOARD, btn);
}
#if CFG_TUSB_OS != OPT_OS_FREERTOS
break;
#endif
if (tusb_time_millis_api() - start_ms < interval_ms) {
return; // not enough time
}
start_ms += interval_ms;
#endif
uint32_t const btn = board_button_read();
// Remote wakeup
if (tud_suspended() && btn != 0u) {
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
tud_remote_wakeup();
} else {
// Send the 1st of report chain, the rest will be sent by tud_hid_report_complete_cb()
send_hid_report(REPORT_ID_KEYBOARD, btn);
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
// Invoked when sent REPORT successfully to host
@ -318,34 +316,36 @@ void led_blinking_task(void *param) {
static uint32_t start_ms = 0;
#endif
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
// blink is disabled
if (0u == blink_interval_ms) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
continue;
#else
return;
#endif
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state; // toggle
#if CFG_TUSB_OS != OPT_OS_FREERTOS
break;
#endif
// blink is disabled
if (0u == blink_interval_ms) {
vTaskDelay(1);
continue;
}
#else
// blink is disabled
if (0u == blink_interval_ms) {
return;
}
#endif
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state; // toggle
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
//--------------------------------------------------------------------+

View File

@ -68,6 +68,7 @@ int main(void) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init();
return 0;
#else
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {
@ -84,8 +85,6 @@ int main(void) {
midi_task(NULL);
}
#endif
return 0;
}
//--------------------------------------------------------------------+
@ -134,55 +133,56 @@ void midi_task(void *param) {
uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
uint8_t const channel = 0; // 0 for channel 1
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
// The MIDI interface always creates input and output port/jack descriptors
// regardless of these being used or not. Therefore incoming traffic should be read
// (possibly just discarded) to avoid the sender blocking in IO
while (tud_midi_available()) {
uint8_t packet[4];
tud_midi_packet_read(packet);
}
#endif
// The MIDI interface always creates input and output port/jack descriptors
// regardless of these being used or not. Therefore incoming traffic should be read
// (possibly just discarded) to avoid the sender blocking in IO
while (tud_midi_available()) {
uint8_t packet[4];
tud_midi_packet_read(packet);
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(286 / portTICK_PERIOD_MS);
vTaskDelay(286 / portTICK_PERIOD_MS);
#else
static uint32_t start_ms = 0;
// send note periodically
if (tusb_time_millis_api() - start_ms < 286) {
return; // not enough time
}
start_ms += 286;
#endif
// Previous positions in the note sequence.
int previous = (int) (note_pos - 1);
// If we currently are at position 0, set the
// previous position to the last note in the sequence.
if (previous < 0) {
previous = sizeof(note_sequence) - 1;
}
// Send Note On for current position at full velocity (127) on channel 1.
uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
tud_midi_stream_write(cable_num, note_on, 3);
// Send Note Off for previous note.
uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
tud_midi_stream_write(cable_num, note_off, 3);
// Increment position
note_pos++;
// If we are at the end of the sequence, start over.
if (note_pos >= sizeof(note_sequence)) {
note_pos = 0;
}
#if CFG_TUSB_OS != OPT_OS_FREERTOS
break;
#endif
static uint32_t start_ms = 0;
// send note periodically
if (tusb_time_millis_api() - start_ms < 286) {
return; // not enough time
}
start_ms += 286;
#endif
// Previous positions in the note sequence.
int previous = (int) (note_pos - 1);
// If we currently are at position 0, set the
// previous position to the last note in the sequence.
if (previous < 0) {
previous = sizeof(note_sequence) - 1;
}
// Send Note On for current position at full velocity (127) on channel 1.
uint8_t note_on[3] = { 0x90 | channel, note_sequence[note_pos], 127 };
tud_midi_stream_write(cable_num, note_on, 3);
// Send Note Off for previous note.
uint8_t note_off[3] = { 0x80 | channel, note_sequence[previous], 0};
tud_midi_stream_write(cable_num, note_off, 3);
// Increment position
note_pos++;
// If we are at the end of the sequence, start over.
if (note_pos >= sizeof(note_sequence)) {
note_pos = 0;
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
//--------------------------------------------------------------------+
@ -195,24 +195,23 @@ void led_blinking_task(void *param) {
static uint32_t start_ms = 0;
#endif
while (1) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state; // toggle
#if CFG_TUSB_OS != OPT_OS_FREERTOS
break;
#endif
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) {
return; // not enough time
}
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state; // toggle
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
//--------------------------------------------------------------------+

View File

@ -95,6 +95,7 @@ int main(void) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
freertos_init();
return 0;
#else
// init device stack on configured roothub port
tusb_rhport_init_t dev_init = {
@ -115,8 +116,6 @@ int main(void) {
audio_task(NULL);
}
#endif
return 0;
}
//--------------------------------------------------------------------+
@ -602,32 +601,32 @@ void audio_task(void *param) {
(void) param;
static uint32_t start_ms = 0;
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms != curr_ms) {
start_ms = curr_ms;
#endif
uint32_t curr_ms = tusb_time_millis_api();
if (start_ms != curr_ms) {
start_ms = curr_ms;
uint16_t length = (uint16_t) (current_sample_rate / 1000 * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX);
uint16_t length = (uint16_t) (current_sample_rate / 1000 * CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX);
if (current_sample_rate == 44100 && (curr_ms % 10 == 0)) {
// Take one more sample every 10 cycles, to have a average reading speed of 44.1
// This correction is not needed in real world cases
length += CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX;
} else if (current_sample_rate == 88200 && (curr_ms % 5 == 0)) {
// Take one more sample every 5 cycles, to have a average reading speed of 88.2
// This correction is not needed in real world cases
length += CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX;
}
tud_audio_read(i2s_dummy_buffer, length);
if (current_sample_rate == 44100 && (curr_ms % 10 == 0)) {
// Take one more sample every 10 cycles, to have a average reading speed of 44.1
// This correction is not needed in real world cases
length += CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX;
} else if (current_sample_rate == 88200 && (curr_ms % 5 == 0)) {
// Take one more sample every 5 cycles, to have a average reading speed of 88.2
// This correction is not needed in real world cases
length += CFG_TUD_AUDIO_FUNC_1_N_BYTES_PER_SAMPLE_RX * CFG_TUD_AUDIO_FUNC_1_N_CHANNELS_RX;
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
#else
return;
#endif
tud_audio_read(i2s_dummy_buffer, length);
}
#if CFG_TUSB_OS == OPT_OS_FREERTOS
vTaskDelay(1);
}
#endif
}
//--------------------------------------------------------------------+
@ -637,19 +636,22 @@ void led_blinking_task(void *param) {
(void) param;
static bool led_state = false;
while (1) {
#if CFG_TUSB_OS == OPT_OS_FREERTOS
while (1) {
vTaskDelay(blink_interval_ms / portTICK_PERIOD_MS);
#else
static uint32_t start_ms = 0;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) return;
start_ms += blink_interval_ms;
static uint32_t start_ms = 0;
// Blink every interval ms
if (tusb_time_millis_api() - start_ms < blink_interval_ms) return;
start_ms += blink_interval_ms;
#endif
board_led_write(led_state);
led_state = 1 - led_state;
board_led_write(led_state);
led_state = 1 - led_state;
#if CFG_TUSB_OS == OPT_OS_FREERTOS
}
#endif
}
#if CFG_AUDIO_DEBUG

View File

@ -87,6 +87,8 @@ int main(void) {
#ifndef ESP_PLATFORM
vTaskStartScheduler();
#endif
return 0;
#else
// init host stack on configured roothub port
tusb_rhport_init_t host_init = {
@ -112,8 +114,6 @@ int main(void) {
hid_app_task();
}
#endif
return 0;
}
#ifdef ESP_PLATFORM

View File

@ -87,6 +87,8 @@ int main(void) {
#ifndef ESP_PLATFORM
vTaskStartScheduler();
#endif
return 0;
#else
// init host stack on configured roothub port
tusb_rhport_init_t host_init = {
@ -107,8 +109,6 @@ int main(void) {
led_blinking_task();
}
#endif
return 0;
}
#ifdef ESP_PLATFORM