纳入固件源码、屏幕组件、nand_xfer 工具和文档。忽略本机构建产物、sdkconfig,以及含本机路径的依赖锁。 Co-authored-by: Cursor <[email protected]>
133 lines
3.6 KiB
C
133 lines
3.6 KiB
C
#include "ns4168.h"
|
||
|
||
#include <stdint.h>
|
||
#include <stdlib.h>
|
||
|
||
#include "driver/gpio.h"
|
||
#include "driver/i2s.h"
|
||
#include "esp_log.h"
|
||
#include "esp_random.h"
|
||
#include "freertos/FreeRTOS.h"
|
||
#include "freertos/task.h"
|
||
|
||
static const char *TAG = "ns4168";
|
||
|
||
static void ns4168_white_noise_task(void *arg) //白噪音测试
|
||
{
|
||
uint8_t buffer[256];
|
||
size_t bytes_written = 0;
|
||
|
||
while (1) {
|
||
for (int i = 0; i < (int)sizeof(buffer); i += 2) {
|
||
uint16_t sample = (uint16_t)(esp_random() & 0xFFFF);
|
||
buffer[i] = (uint8_t)(sample & 0xFF);
|
||
buffer[i + 1] = (uint8_t)((sample >> 8) & 0xFF);
|
||
}
|
||
|
||
esp_err_t ret = i2s_write(NS4168_I2S_PORT, buffer, sizeof(buffer), &bytes_written, portMAX_DELAY);
|
||
if (ret != ESP_OK) {
|
||
ESP_LOGE(TAG, "i2s_write failed: %s", esp_err_to_name(ret));
|
||
}
|
||
vTaskDelay(pdMS_TO_TICKS(10));
|
||
}
|
||
}
|
||
|
||
|
||
|
||
|
||
/**
|
||
* @brief 初始化 NS4168 的 CTRL 引脚并将其拉高(选择右声道,且退出关断模式)
|
||
*/
|
||
void ns4168_ctrl_init(void)
|
||
{
|
||
// 1. 配置 GPIO 参数结构体
|
||
gpio_config_t io_conf = {
|
||
.pin_bit_mask = (1ULL << NS4168_CTRL_GPIO), // 选中 GPIO33
|
||
.mode = GPIO_MODE_OUTPUT, // 设置为推挽输出
|
||
.intr_type = GPIO_INTR_DISABLE, // 禁止中断
|
||
.pull_up_en = GPIO_PULLUP_DISABLE, // 关闭内部上拉(不需要)
|
||
.pull_down_en = GPIO_PULLDOWN_DISABLE, // 关闭内部下拉
|
||
};
|
||
|
||
// 2. 应用配置
|
||
esp_err_t ret = gpio_config(&io_conf);
|
||
if (ret != ESP_OK) {
|
||
ESP_LOGE("NS4168", "GPIO33 配置失败!");
|
||
return;
|
||
}
|
||
|
||
// 3. 输出高电平(3.3V)
|
||
gpio_set_level(NS4168_CTRL_GPIO, 1);
|
||
|
||
ESP_LOGI("NS4168", "NS4168 CTRL 引脚已拉高 (GPIO33),选择右声道");
|
||
}
|
||
|
||
|
||
esp_err_t ns4168_i2s_init(void)
|
||
{
|
||
ns4168_ctrl_init();
|
||
esp_err_t ret = ESP_OK;
|
||
|
||
i2s_config_t i2s_cfg = {
|
||
.mode = I2S_MODE_MASTER | I2S_MODE_TX,
|
||
.sample_rate = NS4168_I2S_SAMPLE_RATE,
|
||
.bits_per_sample = 16,
|
||
.channel_format = NS4168_I2S_CHANNELS,
|
||
.communication_format = I2S_COMM_FORMAT_STAND_I2S,
|
||
.intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
|
||
.dma_buf_count = 16,
|
||
.dma_buf_len = 1024,
|
||
.tx_desc_auto_clear = true,
|
||
.use_apll = false,
|
||
.fixed_mclk = 0,
|
||
};
|
||
|
||
i2s_pin_config_t pin_cfg = {
|
||
.mck_io_num = -1,
|
||
.bck_io_num = NS4168_I2S_BCK_PIN,
|
||
.ws_io_num = NS4168_I2S_WS_PIN,
|
||
.data_out_num = NS4168_I2S_DOUT_PIN,
|
||
.data_in_num = I2S_PIN_NO_CHANGE,
|
||
};
|
||
|
||
ret = i2s_driver_install(NS4168_I2S_PORT, &i2s_cfg, 0, NULL);
|
||
if (ret != ESP_OK) {
|
||
ESP_LOGE(TAG, "i2s_driver_install failed: %s", esp_err_to_name(ret));
|
||
return ret;
|
||
}
|
||
|
||
ret = i2s_set_pin(NS4168_I2S_PORT, &pin_cfg);
|
||
if (ret != ESP_OK) {
|
||
ESP_LOGE(TAG, "i2s_set_pin failed: %s", esp_err_to_name(ret));
|
||
return ret;
|
||
}
|
||
|
||
ret = i2s_zero_dma_buffer(NS4168_I2S_PORT);
|
||
if (ret != ESP_OK) {
|
||
ESP_LOGE(TAG, "i2s_zero_dma_buffer failed: %s", esp_err_to_name(ret));
|
||
return ret;
|
||
}
|
||
|
||
ESP_LOGI(TAG, "NS4168 I2S init OK");
|
||
return ESP_OK;
|
||
}
|
||
|
||
esp_err_t ns4168_start_white_noise_test(void)
|
||
{
|
||
esp_err_t ret = ns4168_i2s_init();
|
||
if (ret != ESP_OK) {
|
||
return ret;
|
||
}
|
||
|
||
BaseType_t task_ret = xTaskCreatePinnedToCore(ns4168_white_noise_task, "ns4168_noise", 4096, NULL, 5, NULL, 1);
|
||
if (task_ret != pdPASS) {
|
||
ESP_LOGE(TAG, "create white noise task failed");
|
||
return ESP_FAIL;
|
||
}
|
||
|
||
return ESP_OK;
|
||
}
|
||
|
||
|
||
|