Skip to content

Commit

Permalink
Move ds4432_voltage_to_reg to DS4432 driver
Browse files Browse the repository at this point in the history
The function is now generic and can be moved to the driver.

Signed-off-by: Robin van der Gracht <[email protected]>
  • Loading branch information
rvdgracht committed Jul 5, 2024
1 parent 88a884c commit dd2f094
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
27 changes: 27 additions & 0 deletions main/DS4432U.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,30 @@ bool DS4432U_test(void)
ESP_LOGI(TAG, "DS4432U+ OUT0 = 0x%02X", data);
return register_result == ESP_OK;
}

uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv,
uint32_t ra_ohm, uint32_t rb_ohm,
int32_t ifs_na, uint32_t vfb_mv)
{
uint8_t reg;
// Calculate current flowing though bottom resistor (Rb) in nA
int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm;
// Calculate current required through top resistor (Ra) to achieve vout in nA
int32_t ira_na = ((vout_mv - vfb_mv) * 1000 * 1000) / ra_ohm;
// Calculate the delta current the DAC needs to sink/source in nA
uint32_t dac_na = abs(irb_na - ira_na);
// Calculate required DAC steps to get dac_na (rounded)
uint32_t dac_steps = ((dac_na * 127) + (ifs_na / 2)) / ifs_na;

// make sure the requested voltage is in within range
if (dac_steps > 127)
return 0;

reg = dac_steps;

// dac_steps is absolute. For sink S = 0; for source S = 1.
if (vout_mv < vnom_mv)
reg |= 0x80;

return reg;
}
4 changes: 3 additions & 1 deletion main/DS4432U.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
bool DS4432U_test(void);
esp_err_t DS4432U_set_current_code(uint8_t output, uint8_t code);
esp_err_t DS4432U_get_current_code(uint8_t output, uint8_t *code);

uint8_t DS4432U_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv,
uint32_t ra_ohm, uint32_t rb_ohm,
int32_t ifs_na, uint32_t vfb_mv);
#endif /* DS4432U_H_ */
29 changes: 1 addition & 28 deletions main/vcore.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,33 +22,6 @@ void VCORE_init(GlobalState * global_state) {
ADC_ch_init(ADC_CHANNEL_1);
}

static uint8_t ds4432_voltage_to_reg(uint32_t vout_mv, uint32_t vnom_mv,
uint32_t ra_ohm, uint32_t rb_ohm,
int32_t ifs_na, uint32_t vfb_mv)
{
uint8_t reg;
// Calculate current flowing though bottom resistor (Rb) in nA
int32_t irb_na = (vfb_mv * 1000 * 1000) / rb_ohm;
// Calculate current required through top resistor (Ra) to achieve vout in nA
int32_t ira_na = ((vout_mv - vfb_mv) * 1000 * 1000) / ra_ohm;
// Calculate the delta current the DAC needs to sink/source in nA
uint32_t dac_na = abs(irb_na - ira_na);
// Calculate required DAC steps to get dac_na (rounded)
uint32_t dac_steps = ((dac_na * 127) + (ifs_na / 2)) / ifs_na;

// make sure the requested voltage is in within range
if (dac_steps > 127)
return 0;

reg = dac_steps;

// dac_steps is absolute. For sink S = 0; for source S = 1.
if (vout_mv < vnom_mv)
reg |= 0x80;

return reg;
}

bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state)
{
uint32_t sum_vcore_mv = vcore_mv * global_state->voltage_domain;
Expand All @@ -61,7 +34,7 @@ bool VCORE_set_voltage(uint16_t vcore_mv, GlobalState * global_state)
ESP_LOGI(TAG, "Set ASIC voltage = %umV", vcore_mv);
TPS546_set_vout((float)sum_vcore_mv / 1000.0);
} else {
uint8_t reg_setting = ds4432_voltage_to_reg(
uint8_t reg_setting = DS4432U_voltage_to_reg(
sum_vcore_mv, BITAXE_VNOM, BITAXE_RTOP, BITAXE_RBOT, BITAXE_IFS, 600);
ESP_LOGI(TAG, "Set ASIC voltage = %umV [0x%02X]", vcore_mv, reg_setting);
DS4432U_set_current_code(0, reg_setting); /// eek!
Expand Down

0 comments on commit dd2f094

Please sign in to comment.