BV: split vreg_set to lv and hv

This commit is contained in:
King Kévin 2018-02-06 21:34:29 +01:00
parent 2df0fbf96f
commit 9d31e9b6a4
3 changed files with 69 additions and 66 deletions

View File

@ -223,61 +223,61 @@ float busvoodoo_vreg_get(uint8_t channel)
return to_return;
}
float busvoodoo_vreg_set(uint8_t channel, float voltage)
float busvoodoo_lv_set(float voltage)
{
if (channel!=BUSVOODOO_LV_CHANNEL && channel!=BUSVOODOO_HV_CHANNEL) { // check channel
return NAN;
}
if (BUSVOODOO_HV_CHANNEL==channel && !busvoodoo_full) { // the HV voltage regulator is only present on the full version
return NAN;
}
float volt = NAN; // common variable for voltages
if (BUSVOODOO_LV_CHANNEL==channel) { // set voltage on LV low drop-out voltage regulator
if (voltage<=0.3) { // disable voltage regulator
gpio_clear(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // disable LV voltage regulator
dac_disable(BUSVOODOO_LVCTL_CHANNEL); // disable LV control
} else { // enable voltage regulator
if (voltage>4.85) { // use the 5V directly
gpio_clear(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // put 5V on LV line
} else { // use adjustable voltage regulator (5.0V rail minus LDO and diodes)
gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // disable 5V input
volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
return NAN;
}
uint16_t dac_set = BUSVOODOO_LV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_LVCTL_CHANNEL); // set output so the voltage regulator is set to 2.5V
dac_software_trigger(BUSVOODOO_LVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_LVCTL_CHANNEL); // enable DAC
gpio_set(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // enable LV voltage regulator
}
}
sleep_ms(10); // let voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage to return
} else if (BUSVOODOO_HV_CHANNEL==channel && busvoodoo_full) {// set voltage on HV boost voltage regulator
if (voltage<3.29) { // disable voltage regulator
gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable HV control
} else {
if (voltage>24.0) { // enforce upper voltage limit (diodes limit is 30V, ADC input limit is 25V)
voltage = 24.0;
}
if (voltage<=0.3) { // disable voltage regulator
gpio_clear(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // disable LV voltage regulator
dac_disable(BUSVOODOO_LVCTL_CHANNEL); // disable LV control
} else { // enable voltage regulator
if (voltage>4.85) { // use the 5V directly
gpio_clear(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // put 5V on LV line
} else { // use adjustable voltage regulator (5.0V rail minus LDO and diodes)
gpio_set(GPIO(BUSVOODOO_5VPULLUP_PORT), GPIO(BUSVOODOO_5VPULLUP_PIN)); // disable 5V input
volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
return NAN;
}
uint16_t dac_set = BUSVOODOO_HV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_HVCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
dac_software_trigger(BUSVOODOO_HVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_HVCTL_CHANNEL); // enable DAC
gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator
uint16_t dac_set = BUSVOODOO_LV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_LVCTL_CHANNEL); // set output so the voltage regulator is set to 2.5V
dac_software_trigger(BUSVOODOO_LVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_LVCTL_CHANNEL); // enable DAC
gpio_set(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // enable LV voltage regulator
}
sleep_ms(100); // let the voltage regulator start and voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
} else { // don't know about other voltage regulators
volt = NAN;
}
sleep_ms(10); // let voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage to return
return volt; // return measured voltage
}
float busvoodoo_hv_set(float voltage)
{
if (!busvoodoo_full) { // the HV voltage regulator is only present on the full version
return NAN;
}
float volt = NAN; // common variable for voltages
if (voltage<3.29) { // disable voltage regulator
gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator
dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable HV control
} else {
if (voltage>24.0) { // enforce upper voltage limit (diodes limit is 30V, ADC input limit is 25V)
voltage = 24.0;
}
volt = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage
if (isnan(voltage)) {
return NAN;
}
uint16_t dac_set = BUSVOODOO_HV_SET(voltage)/volt*4095; // DAC value corresponding to the voltage
dac_load_data_buffer_single(dac_set, RIGHT12, BUSVOODOO_HVCTL_CHANNEL); // set output so the voltage regulator is set to desired output voltage
dac_software_trigger(BUSVOODOO_HVCTL_CHANNEL); // transfer the value to the DAC
dac_enable(BUSVOODOO_HVCTL_CHANNEL); // enable DAC
gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator
}
sleep_ms(100); // let the voltage regulator start and voltage settle
volt = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage
return volt; // return measured voltage
}
@ -407,7 +407,7 @@ static void busvoodoo_global_lv(void* argument)
} else {
printf("LV rail set to %.2fV", voltage);
}
voltage = busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, voltage); // set LV voltage
voltage = busvoodoo_lv_set(voltage); // set LV voltage
// print LV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
@ -443,7 +443,7 @@ static void busvoodoo_global_hv(void* argument)
} else {
printf("set to %.2fV", voltage);
}
voltage = busvoodoo_vreg_set(BUSVOODOO_HV_CHANNEL, voltage); // set HV voltage
voltage = busvoodoo_hv_set(voltage); // set HV voltage
// print HV voltage
if (voltage < 0.1) {
printf(": 0.00V\n");
@ -612,7 +612,7 @@ const struct menu_command_t busvoodoo_global_commands[] = {
},
{
'l',
"lV",
"LV",
"set voltage on low voltage power rail (0, 0.3-4.8, 5V)",
MENU_ARGUMENT_FLOAT,
"[voltage]",

View File

@ -129,14 +129,17 @@ bool busvoodoo_vout_switch(bool on);
* @return voltage, or NaN if channel is invalid (or error happened)
*/
float busvoodoo_vreg_get(uint8_t channel);
/** set voltage on adjustable voltage regulator
* @warning only LV and 12V are adjustable voltage regulators
* @note for voltages above 4.7V on LV it will use the 5.0V from USB
* @param[in] channel which voltage regulator to set voltage on @ref busvoodoo_adc
* @param[in] voltage voltage to set on adjustable voltage regulator
* @return voltage output, or NaN if channel is invalid (or error happened)
/** set voltage on low voltage adjustable voltage regulator
* @note for voltages above 4.85V it will use the 5.0V from USB
* @param[in] voltage voltage to set on adjustable voltage regulator (0, 0.3-4.8, 5V)
* @return voltage output, or NaN if error happened
*/
float busvoodoo_vreg_set(uint8_t channel, float voltage);
float busvoodoo_lv_set(float voltage);
/** set voltage on high voltage adjustable voltage regulator
* @param[in] voltage voltage to set on adjustable voltage regulator (0, 3.3-24V)
* @return voltage output, or NaN if error happened
*/
float busvoodoo_hv_set(float voltage);
/** enable embedded pull-up resistors
* @note the embedded pull-up resistor use the voltage on the LV power rail (set or externally provided)
* @param[in] on enable (true) or disable (false) embedded pull-up resistors

View File

@ -166,7 +166,7 @@ static bool busvoodoo_hiz_test_self(void)
busvoodoo_vout_switch(false); // disable Vout
// check LV voltage regulator
voltage = busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, 0); // disable LV voltage regulator
voltage = busvoodoo_lv_set(0); // disable LV voltage regulator
if (voltage>0.2) { // ensure the output is at 0V when the regulator is not enabled
printf("LV voltage is %.2fV instead of 0V when the regulator is disabled\n", voltage);
#if DEBUG
@ -196,7 +196,7 @@ static bool busvoodoo_hiz_test_self(void)
}
// check if we can control LV
voltage = busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, BUSVOODOO_LV_TEST); // get LV voltage
voltage = busvoodoo_lv_set(BUSVOODOO_LV_TEST); // get LV voltage
if (voltage<BUSVOODOO_LV_TEST-0.2) {
printf("LV voltage is lower (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_LV_TEST);
#if DEBUG
@ -212,11 +212,11 @@ static bool busvoodoo_hiz_test_self(void)
goto error;
#endif
}
busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, 0); // disable LV voltage regulator
busvoodoo_lv_set(0); // disable LV voltage regulator
// check HV voltage regulator
if (busvoodoo_full) {
voltage = busvoodoo_vreg_set(BUSVOODOO_HV_CHANNEL, 0); // disable HV voltage regulator
voltage = busvoodoo_hv_set(0); // disable HV voltage regulator
if (voltage>0.2) { // ensure the output is at 0V when the regulator is not enabled
printf("HV voltage is %.2fV instead of 0V when the regulator is disabled\n", voltage);
#if DEBUG
@ -246,7 +246,7 @@ static bool busvoodoo_hiz_test_self(void)
}
// check if we can control HV voltage regulator
voltage = busvoodoo_vreg_set(BUSVOODOO_HV_CHANNEL, BUSVOODOO_HV_TEST); // set voltage on HV voltage regulator
voltage = busvoodoo_hv_set(BUSVOODOO_HV_TEST); // set voltage on HV voltage regulator
if (voltage<BUSVOODOO_HV_TEST-0.3) {
printf("HV voltage is lower (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_HV_TEST);
#if DEBUG
@ -262,7 +262,7 @@ static bool busvoodoo_hiz_test_self(void)
goto error;
#endif
}
voltage = busvoodoo_vreg_set(BUSVOODOO_HV_CHANNEL, 0); // disable HV voltage regulator
voltage = busvoodoo_hv_set(0); // disable HV voltage regulator
}
// pull all pins down and ensure they are low
@ -358,7 +358,7 @@ static bool busvoodoo_hiz_test_self(void)
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
}
busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, 5.0); // set LV voltage regulator to use the 5.0V power rail
busvoodoo_lv_set(5.0); // set LV voltage regulator to use the 5.0V power rail
voltage = busvoodoo_embedded_pullup(true); // enable 5V pull-up
if (voltage<4.0) {
printf("5V power rail voltage is too low when used to pull up: %.2fV\n", voltage);
@ -386,14 +386,14 @@ static bool busvoodoo_hiz_test_self(void)
}
}
busvoodoo_embedded_pullup(false); // disable pull-ups
voltage = busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, 0); // disable LV voltage regulator
voltage = busvoodoo_lv_set(0); // disable LV voltage regulator
// test LV pull-up set to 3.3V
for (uint8_t pin=0; pin<LENGTH(busvoodoo_io_ports) && pin<LENGTH(busvoodoo_io_pins); pin++) {
gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, busvoodoo_io_pins[pin]); // set pin to input
gpio_clear(busvoodoo_io_ports[pin], busvoodoo_io_pins[pin]); // pull down to ensure it is not high by accident
}
busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, 3.3); // set LV voltage regulator
busvoodoo_lv_set(3.3); // set LV voltage regulator
voltage = busvoodoo_embedded_pullup(3.3); // enable pull-up with adjustable regulator
if (voltage<BUSVOODOO_LV_DEFAULT-0.2) {
printf("LV voltage is lower (%.2fV) than expected (%.2fV) when used to pull up\n", voltage, BUSVOODOO_LV_DEFAULT);
@ -420,7 +420,7 @@ static bool busvoodoo_hiz_test_self(void)
#endif
}
}
busvoodoo_vreg_set(BUSVOODOO_LV_CHANNEL, 0); // disable LV voltage regulator
busvoodoo_lv_set(0); // disable LV voltage regulator
busvoodoo_embedded_pullup(false); // disable pull-ups
to_return = true; // all tests are successful