This commit is contained in:
2026-02-26 15:44:43 +01:00
parent b8b3e6ea44
commit 5934bba4af
7 changed files with 159 additions and 16 deletions

View File

@@ -3,6 +3,7 @@
#include <zephyr/device.h>
#include <zephyr/drivers/i2s.h>
#include <zephyr/pm/device.h>
#include <zephyr/drivers/gpio.h>
#include <string.h>
#include <audio.h>
@@ -29,13 +30,42 @@ K_SEM_DEFINE(audio_ready_sem, 0, 1);
#error "Audio I2S alias not defined in devicetree"
#endif
#define AUDIO_AMP_ENABLE_NODE DT_ALIAS(audio_amp_en)
#if !DT_NODE_EXISTS(AUDIO_AMP_ENABLE_NODE)
#error "Audio Amplifier Enable alias not defined in devicetree"
#endif
static const struct device *const i2s_dev = DEVICE_DT_GET(I2S_NODE);
static const struct gpio_dt_spec amp_en_dev = GPIO_DT_SPEC_GET(AUDIO_AMP_ENABLE_NODE, gpios);
static volatile bool abort_playback = false;
static char next_random_filename[64] = {0};
static uint32_t audio_file_count = 0;
static char cached_404_path[] = "/lfs/sys/404";
void i2s_suspend(void)
{
LOG_DBG("Suspending I2S interface for power saving");
i2s_trigger(i2s_dev, I2S_DIR_TX, I2S_TRIGGER_DROP);
/* Nutzt jetzt die korrekte Spezifikation */
gpio_pin_set_dt(&amp_en_dev, 0);
pm_device_action_run(i2s_dev, PM_DEVICE_ACTION_SUSPEND);
}
void i2s_resume(void)
{
LOG_DBG("Resuming I2S interface");
/* Zuerst Pin auf High, dann Hardware wecken */
gpio_pin_set_dt(&amp_en_dev, 1);
pm_device_action_run(i2s_dev, PM_DEVICE_ACTION_RESUME);
}
void audio_refresh_file_count(void)
{
struct fs_dir_t dirp;
@@ -142,6 +172,7 @@ void audio_thread(void *arg1, void *arg2, void *arg3)
{
LOG_DBG("Audio thread started");
k_sem_take(&audio_ready_sem, K_FOREVER);
i2s_suspend();
/* Ersten zufälligen Dateinamen beim Booten vorab cachen */
get_random_file(next_random_filename, sizeof(next_random_filename));
@@ -152,9 +183,8 @@ void audio_thread(void *arg1, void *arg2, void *arg3)
{
if (k_msgq_get(&audio_play_msgq, &filename, K_FOREVER) == 0)
{
fs_pm_flash_resume();
abort_playback = false;
i2s_resume();
/* 2. Datei bestimmen (aus Cache oder synchron als Fallback) */
if (filename[0] == '\0')
@@ -178,7 +208,7 @@ void audio_thread(void *arg1, void *arg2, void *arg3)
struct fs_file_t file;
fs_file_t_init(&file);
if (fs_open(&file, filename, FS_O_READ) < 0)
if (fs_pm_open(&file, filename, FS_O_READ) < 0)
{
LOG_ERR("Failed to open %s", filename);
continue;
@@ -284,10 +314,10 @@ void audio_thread(void *arg1, void *arg2, void *arg3)
}
}
fs_close(&file);
fs_pm_flash_suspend();
fs_pm_close(&file);
if (k_msgq_num_used_get(&audio_play_msgq) == 0)
{
i2s_suspend();
io_status(false);
}
@@ -325,6 +355,19 @@ int audio_init(void)
if (ret < 0)
return ret;
if (!gpio_is_ready_dt(&amp_en_dev)) {
LOG_DBG("Amplifier enable GPIO device not ready");
return -ENODEV;
}
ret = gpio_pin_configure_dt(&amp_en_dev, GPIO_OUTPUT_ACTIVE);
if (ret < 0) {
LOG_ERR("Failed to configure amplifier enable GPIO: %d", ret);
return ret;
}
gpio_pin_configure_dt(&amp_en_dev, 0);
audio_refresh_file_count();
LOG_INF("Audio initialized: %u bits, %u.%03u kHz", config.word_size, config.frame_clk_freq / 1000, config.frame_clk_freq % 1000);
return 0;