#include "main.h" #include "tpa6130a2.h" tpa6130_ret_t tpa_impl_read_reg(void *handle, uint8_t addr, uint8_t reg, uint8_t *value); tpa6130_ret_t tpa_impl_write_reg(void *handle, uint8_t addr, uint8_t reg, uint8_t value); extern I2C_HandleTypeDef hi2c4; static tpa6130_t s_tpa = { .cb = { .write_reg_cb = tpa_impl_write_reg, .read_reg_cb = tpa_impl_read_reg, }, .user_data = &hi2c4, }; /** * @brief Bring the amplifier out of shutdown mode, * In shutdown mode, the AMP's registers will be resetted. * * @param up 0 - power down, everything else - power up * @return uint8_t 0 - ERROR, everything else - OK */ uint8_t audio_amp_power(uint8_t up) { if (up) { HAL_GPIO_WritePin(A_AMP_UP_GPIO_Port, A_AMP_UP_Pin, GPIO_PIN_SET); } else { HAL_GPIO_WritePin(A_AMP_UP_GPIO_Port, A_AMP_UP_Pin, GPIO_PIN_RESET); } return 1; } /** * @brief Check if amplifier is ready * * @return uint8_t 0 - disconnected, 1 - connected */ uint8_t audio_amp_is_connected(void) { uint8_t rev = 0; if (tpa6130a2_revision(&s_tpa, &rev) == TPA6130_OK) { if (rev == 2) return 1; /* TPA6130A2's rev is 0b0010(2) */ } return 0; } /** * @brief Enable amplifiers and set volume. * * @param volume volume to be set (1 - 100) * @return uint8_t 0 - ERROR, everything else - OK */ uint8_t audio_amp_set_volume(uint8_t volume) { uint8_t converted_vol = 0; if(volume > 0) { converted_vol = (volume - 1) * 64 / 100; } if (tpa6130a2_enable_channel(&s_tpa, TPA6130_CH_L | TPA6130_CH_R) != TPA6130_OK) { return 0; } tpa6130_ch_t mute_ch = TPA6130_CH_NONE; if (volume == 0) { mute_ch = TPA6130_CH_L | TPA6130_CH_R; } if (tpa6130a2_set_mute(&s_tpa, mute_ch) != TPA6130_OK) { return 0; } if (tpa6130a2_set_volume(&s_tpa, converted_vol) != TPA6130_OK) { return 0; } return 1; }