Skip to content

Commit

Permalink
[voice_kit] Add VNR and pipeline stage I2C commands (#120)
Browse files Browse the repository at this point in the history
* add vnr and pipeline stage commands

* use new xmos firmware
  • Loading branch information
kahrendt authored Sep 18, 2024
1 parent a9c664a commit 9d6f2e2
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 3 deletions.
54 changes: 54 additions & 0 deletions esphome/components/voice_kit/voice_kit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,60 @@ void VoiceKit::loop() {
}
}

uint8_t VoiceKit::read_vnr() {
const uint8_t vnr_req[] = {CONFIGURATION_SERVICER_RESID,
CONFIGURATION_SERVICER_RESID_VNR_VALUE | CONFIGURATION_COMMAND_READ_BIT, 2};
uint8_t vnr_resp[2];

auto error_code = this->write(vnr_req, sizeof(vnr_req));
if (error_code != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Request status failed");
return 0;
}
error_code = this->read(vnr_resp, sizeof(vnr_resp));
if (error_code != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to read VNR");
return 0;
}
return vnr_resp[1];
}

PipelineStages VoiceKit::read_pipeline_stage(MicrophoneChannels channel) {
uint8_t channel_register = CONFIGURATION_SERVICER_RESID_CHANNEL_0_PIPELINE_STAGE | CONFIGURATION_COMMAND_READ_BIT;
if (channel == MICROPHONE_CHANNEL_1) {
channel_register = CONFIGURATION_SERVICER_RESID_CHANNEL_1_PIPELINE_STAGE | CONFIGURATION_COMMAND_READ_BIT;
}

const uint8_t stage_req[] = {CONFIGURATION_SERVICER_RESID, channel_register, 2};

uint8_t stage_resp[2];

auto error_code = this->write(stage_req, sizeof(stage_req));
if (error_code != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to read stage");
}
error_code = this->read(stage_resp, sizeof(stage_resp));
if (error_code != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to read stage");
}

return static_cast<PipelineStages>(stage_resp[1]);
}

void VoiceKit::write_pipeline_stage(MicrophoneChannels channel, PipelineStages stage) {
uint8_t channel_register = CONFIGURATION_SERVICER_RESID_CHANNEL_0_PIPELINE_STAGE;
if (channel == MICROPHONE_CHANNEL_1) {
channel_register = CONFIGURATION_SERVICER_RESID_CHANNEL_1_PIPELINE_STAGE;
}

const uint8_t stage_set[] = {CONFIGURATION_SERVICER_RESID, channel_register, 1, stage};

auto error_code = this->write(stage_set, sizeof(stage_set));
if (error_code != i2c::ERROR_OK) {
ESP_LOGE(TAG, "Failed to set stage");
}
}

void VoiceKit::start_dfu_update() {
if (this->firmware_bin_ == nullptr || !this->firmware_bin_length_) {
ESP_LOGE(TAG, "Firmware invalid");
Expand Down
27 changes: 27 additions & 0 deletions esphome/components/voice_kit/voice_kit.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
namespace esphome {
namespace voice_kit {

static const uint8_t REGISTER_CHANNEL_1_STAGE = 0x40;

// Configuration servicer resource IDs
//
static const uint8_t DFU_CONTROLLER_SERVICER_RESID = 240;
static const uint8_t CONFIGURATION_SERVICER_RESID = 241;
static const uint8_t CONFIGURATION_COMMAND_READ_BIT = 0x80;
static const uint8_t DFU_COMMAND_READ_BIT = 0x80;

static const uint16_t DFU_TIMEOUT_MS = 1000;
Expand All @@ -36,6 +39,26 @@ enum VoiceKitUpdaterStatus : uint8_t {
UPDATE_VERIFY_NEW_VERSION,
};

// Configuration enums from the XMOS firmware's src/configuration/configuration_servicer.h
enum ConfCommands : uint8_t {
CONFIGURATION_SERVICER_RESID_VNR_VALUE = 0x00,
CONFIGURATION_SERVICER_RESID_CHANNEL_0_PIPELINE_STAGE = 0x30,
CONFIGURATION_SERVICER_RESID_CHANNEL_1_PIPELINE_STAGE = 0x40,
};

enum PipelineStages : uint8_t {
PIPELINE_STAGE_NONE = 0,
PIPELINE_STAGE_AEC = 1,
PIPELINE_STAGE_IC = 2,
PIPELINE_STAGE_NS = 3,
PIPELINE_STAGE_AGC = 4,
};

enum MicrophoneChannels : uint8_t {
MICROPHONE_CHANNEL_0 = 0,
MICROPHONE_CHANNEL_1 = 1,
};

// DFU enums from https://github.com/xmos/sln_voice/blob/develop/examples/ffva/src/dfu_int/dfu_state_machine.h

enum DfuIntAltSetting : uint8_t {
Expand Down Expand Up @@ -126,6 +149,10 @@ class VoiceKit : public Component, public i2c::I2CDevice {

void start_dfu_update();

uint8_t read_vnr();
void write_pipeline_stage(MicrophoneChannels channel, PipelineStages stage);
PipelineStages read_pipeline_stage(MicrophoneChannels channel);

protected:
#ifdef USE_VOICE_KIT_STATE_CALLBACK
CallbackManager<void(DFUAutomationState, float, VoiceKitUpdaterStatus)> state_callback_{};
Expand Down
6 changes: 3 additions & 3 deletions home-assistant-voice.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1313,9 +1313,9 @@ media_player:
voice_kit:
reset_pin: GPIO4
firmware:
url: https://github.com/esphome/voice-kit-xmos-firmware/releases/download/v1.2.0/ffva_v1.2.0_upgrade.bin
version: "1.2.0"
md5: 912b84ea15df4a0081e39ffd9740b884
url: https://github.com/esphome/voice-kit-xmos-firmware/releases/download/v1.3.1/ffva_v1.3.1_upgrade.bin
version: "1.3.1"
md5: 964635c5bf125529dab14a2472a15401

external_components:
- source:
Expand Down

0 comments on commit 9d6f2e2

Please sign in to comment.