| 139 | } |
| 140 | |
| 141 | static error_t initSound(::Device* i2c_controller, ::Device* io_expander0 = nullptr) { |
| 142 | // Init data from M5Unified: |
| 143 | // https://github.com/m5stack/M5Unified/blob/master/src/M5Unified.cpp |
| 144 | static constexpr uint8_t ES8388_I2C_ADDR = 0x10; |
| 145 | static constexpr uint8_t ENABLED_BULK_DATA[] = { |
| 146 | 0, 0x80, // RESET/ CSM POWER ON |
| 147 | 0, 0x00, |
| 148 | 0, 0x00, |
| 149 | 0, 0x0E, |
| 150 | 1, 0x00, |
| 151 | 2, 0x0A, // CHIP POWER: power up all |
| 152 | 3, 0xFF, // ADC POWER: power down all |
| 153 | 4, 0x3C, // DAC POWER: power up and LOUT1/ROUT1/LOUT2/ROUT2 enable |
| 154 | 5, 0x00, // ChipLowPower1 |
| 155 | 6, 0x00, // ChipLowPower2 |
| 156 | 7, 0x7C, // VSEL |
| 157 | 8, 0x00, // set I2S slave mode |
| 158 | // reg9-22 == adc |
| 159 | 23, 0x18, // I2S format (16bit) |
| 160 | 24, 0x00, // I2S MCLK ratio (128) |
| 161 | 25, 0x20, // DAC unmute |
| 162 | 26, 0x00, // LDACVOL 0x00~0xC0 |
| 163 | 27, 0x00, // RDACVOL 0x00~0xC0 |
| 164 | 28, 0x08, // enable digital click free power up and down |
| 165 | 29, 0x00, |
| 166 | 38, 0x00, // DAC CTRL16 |
| 167 | 39, 0xB8, // LEFT Ch MIX |
| 168 | 42, 0xB8, // RIGHTCh MIX |
| 169 | 43, 0x08, // ADC and DAC separate |
| 170 | 45, 0x00, // 0x00=1.5k VREF analog output / 0x10=40kVREF analog output |
| 171 | 46, 0x21, |
| 172 | 47, 0x21, |
| 173 | 48, 0x21, |
| 174 | 49, 0x21 |
| 175 | }; |
| 176 | |
| 177 | error_t error = i2c_controller_write_register_array( |
| 178 | i2c_controller, |
| 179 | ES8388_I2C_ADDR, |
| 180 | ENABLED_BULK_DATA, |
| 181 | sizeof(ENABLED_BULK_DATA), |
| 182 | pdMS_TO_TICKS(1000) |
| 183 | ); |
| 184 | if (error != ERROR_NONE) { |
| 185 | LOG_E(TAG, "Failed to enable ES8388: %s", error_to_string(error)); |
| 186 | return error; |
| 187 | } |
| 188 | |
| 189 | auto* speaker_enable_pin = gpio_descriptor_acquire(io_expander0, GPIO_EXP0_PIN_SPEAKER_ENABLE, GPIO_OWNER_GPIO); |
| 190 | check(speaker_enable_pin, "Failed to acquire speaker enable pin"); |
| 191 | error = gpio_descriptor_set_level(speaker_enable_pin, true); |
| 192 | gpio_descriptor_release(speaker_enable_pin); |
| 193 | if (error != ERROR_NONE) { |
| 194 | LOG_E(TAG, "Failed to enable amplifier: %s", error_to_string(error)); |
| 195 | return ERROR_RESOURCE; |
| 196 | } |
| 197 | |
| 198 | return ERROR_NONE; |
no test coverage detected