Files
cardputerfw/main/drivers/es8311.h
2026-05-08 23:46:39 +02:00

136 lines
3.2 KiB
C

#pragma once
#include <stddef.h>
#include <stdint.h>
// ----------------------
// I2C
// ----------------------
// ----------------------
// System
// ----------------------
#define ES8311_RESET 0x00
#define ES8311_CLK_MANAGE 0x01
#define ES8311_CLK_DIV 0x02
// ----------------------
// Power
// ----------------------
#define ES8311_PWR_UP_DOWN 0x0C
#define ES8311_PWR_ANALOG 0x0D
#define ES8311_PWR_DAC_EN (1 << 0)
#define ES8311_PWR_ADC_EN (1 << 1)
// ----------------------
// Audio format
// ----------------------
#define ES8311_SER_FMT 0x10
#define ES8311_FMT_I2S 0x18
#define ES8311_FMT_LEFT 0x10
#define ES8311_WORD_LEN_16 0x10
#define ES8311_WORD_LEN_24 0x20
// ----------------------
// ADC
// ----------------------
#define ES8311_ADC_CTRL1 0x14
#define ES8311_ADC_CTRL2 0x15
#define ES8311_ADC_VOL 0x16
// ----------------------
// DAC
// ----------------------
#define ES8311_DAC_CTRL1 0x17
#define ES8311_DAC_CTRL2 0x18
#define ES8311_DAC_VOL_L 0x33
#define ES8311_DAC_VOL_R 0x34
// ----------------------
// Output
// ----------------------
#define ES8311_HP_CTRL 0x31
#define ES8311_SPK_CTRL 0x32
// ----------------------
// Common
// ----------------------
#define ES8311_RESET_CMD 0x1F
#define ES8311_ALL_ON 0xFF
// ----------------------
// Clock Management Values
// ----------------------
#define ES8311_CLK_ALL_EN 0x3F // Enable all clocks
#define ES8311_CLK_DIV_DEFAULT 0x00 // Default divider
// ----------------------
// Power Analog Values
// ----------------------
#define ES8311_PWR_ANALOG_OFF 0x00 // All analog blocks off
#define ES8311_PWR_ANALOG_ON 0xBF // Power up analog circuits
// ----------------------
// ADC Control Values
// ----------------------
#define ES8311_ADC_CTRL1_DEFAULT 0x44 // TODO: Document bit meanings
#define ES8311_ADC_CTRL2_DEFAULT 0x0C // TODO: Document bit meanings
// ----------------------
// DAC Control Values
// ----------------------
#define ES8311_DAC_CTRL1_DEFAULT 0x04 // TODO: Document bit meanings
#define ES8311_DAC_CTRL2_DEFAULT 0x0C // TODO: Document bit meanings
// ----------------------
// Output Control Values
// ----------------------
#define ES8311_HP_CTRL_DEFAULT 0xF0 // TODO: Document bit meanings
#define ES8311_SPK_CTRL_MAX 0xFF // Maximum speaker output
// ----------------------
// Volume Defaults
// ----------------------
#define ES8311_DAC_VOL_DEFAULT 0xBF // ~75% volume
#define ES8311_ADC_VOL_DEFAULT 0x88 // ~50% gain
// ----------------------
// Timing Constants (milliseconds)
// ----------------------
#define ES8311_RESET_DELAY_MS 20
#define ES8311_POWER_DELAY_MS 10
#define ES8311_STARTUP_DELAY_MS 50
// ----------------------
// Beep Configuration
// ----------------------
#define ES8311_BEEP_DURATION_SAMPLES 4410 // 100ms at 44.1kHz
#define ES8311_BEEP_AMPLITUDE 12000 // ~36% of 16-bit max
#define ES8311_BEEP_HALF_PERIOD 20 // ~2.2kHz tone
void es8311_init(void);
void es8311_set_dac_volume(uint8_t vol);
void es8311_set_adc_volume(uint8_t vol);
int audio_write(const int16_t *samples, size_t count);
int audio_read(int16_t *samples, size_t count);
void audio_beep(void);
void audio_test_tone(void);
int es8311_write(uint8_t reg, uint8_t val);
uint8_t es8311_read(uint8_t reg);
void es8311_verify_init(void);