2019-10-20 19:18:52 +02:00
|
|
|
/*
|
|
|
|
* bd3491.c
|
|
|
|
*
|
|
|
|
* Created on: 20.10.2019
|
|
|
|
* Author: julian
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "bd3491.h"
|
|
|
|
|
|
|
|
// BD3491 driver
|
|
|
|
void bd_write_reg(I2C_HandleTypeDef * handle, uint8_t reg, uint8_t data) {
|
|
|
|
uint8_t out[] = {
|
|
|
|
reg,
|
|
|
|
data
|
|
|
|
};
|
|
|
|
|
|
|
|
HAL_I2C_Master_Transmit(handle, 0b10000010, out, sizeof(out), HAL_MAX_DELAY);
|
|
|
|
}
|
|
|
|
|
2019-10-21 23:05:00 +02:00
|
|
|
|
|
|
|
#define BD_INPUT_TABLE(a) \
|
|
|
|
a(0, 0) \
|
|
|
|
a(1, 1) \
|
|
|
|
a(2, 2) \
|
|
|
|
a(3, 3) \
|
|
|
|
a(4, 4) \
|
|
|
|
a(5, 6) \
|
|
|
|
a(BD_INPUT_ALL, 5) \
|
|
|
|
a(BD_INPUT_MUTE, 7)
|
|
|
|
|
|
|
|
|
2019-10-20 19:18:52 +02:00
|
|
|
void bd_set_input(I2C_HandleTypeDef * handle, uint8_t number) {
|
2019-10-21 23:05:00 +02:00
|
|
|
#define INPUT_CASE(num, out) case num: bd_write_reg(handle, 0x04, out); break;
|
|
|
|
switch(number) {
|
|
|
|
BD_INPUT_TABLE(INPUT_CASE)
|
2019-10-20 19:18:52 +02:00
|
|
|
}
|
2019-10-21 23:05:00 +02:00
|
|
|
#undef INPUT_CASE
|
2019-10-20 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#define BD_GAIN_TABLE(a) \
|
|
|
|
a( 0, 0) \
|
|
|
|
a( 2, 1) \
|
|
|
|
a( 4, 2) \
|
|
|
|
a( 6, 3) \
|
|
|
|
a( 8, 4) \
|
|
|
|
a(12, 6) \
|
|
|
|
a(16, 8) \
|
|
|
|
a(20, 10)
|
|
|
|
|
|
|
|
int bd_set_gain(I2C_HandleTypeDef * handle, uint8_t gain_in_db) {
|
|
|
|
uint8_t regvalue = 0;
|
|
|
|
if (gain_in_db > BD_MAX_GAIN) gain_in_db = BD_MAX_GAIN;
|
|
|
|
|
|
|
|
#define FINDREG(db, reg) if (gain_in_db <= db) { gain_in_db = db; regvalue = reg; } else
|
|
|
|
BD_GAIN_TABLE(FINDREG) {
|
|
|
|
// gain not in table (should not happen)
|
|
|
|
}
|
|
|
|
#undef FINDREG
|
|
|
|
|
|
|
|
bd_write_reg(handle, 0x06, regvalue<<1);
|
|
|
|
return gain_in_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t bd_set_attenuation(I2C_HandleTypeDef * handle, uint8_t right_channel, uint8_t attenuation_in_db) {
|
|
|
|
if ((attenuation_in_db&0x7F) > 87)
|
|
|
|
attenuation_in_db = BD_INF_ATTENUATION;
|
2019-10-22 00:09:51 +02:00
|
|
|
|
|
|
|
if (right_channel)
|
|
|
|
right_channel = 1;
|
|
|
|
|
|
|
|
bd_write_reg(handle, 0x22 - right_channel, attenuation_in_db | 0x80);
|
2019-10-20 19:18:52 +02:00
|
|
|
return attenuation_in_db;
|
|
|
|
}
|
|
|
|
|
|
|
|
// when gain == 0, cut bass boost
|
2019-10-22 00:04:30 +02:00
|
|
|
uint8_t bd_set_bass_boost(I2C_HandleTypeDef * handle, uint8_t gain) {
|
|
|
|
if (gain > 0x07) gain = 0x07;
|
|
|
|
|
2019-10-20 19:18:52 +02:00
|
|
|
bd_write_reg(handle, 0x51, (gain == 0)?(0x80):(0x00) | (gain&0x07) << 1);
|
2019-10-22 00:04:30 +02:00
|
|
|
|
|
|
|
return gain;
|
2019-10-20 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// when gain == 0, cut treble boost
|
2019-10-22 00:04:30 +02:00
|
|
|
uint8_t bd_set_treble_boost(I2C_HandleTypeDef * handle, uint8_t gain) {
|
|
|
|
if (gain > 0x07) gain = 0x07;
|
|
|
|
|
2019-10-20 19:18:52 +02:00
|
|
|
bd_write_reg(handle, 0x57, (gain == 0)?(0x80):(0x00) | (gain&0x07) << 1);
|
2019-10-22 00:09:51 +02:00
|
|
|
|
2019-10-22 00:04:30 +02:00
|
|
|
return gain;
|
2019-10-20 19:18:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void bd_set_sourround(I2C_HandleTypeDef * handle, enum BD_SOURROUND_GAIN gain) {
|
|
|
|
uint8_t enable = (gain != BD_SOURROUND_OFF)?0x80:0;
|
|
|
|
|
|
|
|
bd_write_reg(handle, 0x78, enable | gain);
|
|
|
|
}
|