From 9d31e9b6a477f8f64341d6d57e8c6e498cf4d228 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Tue, 6 Feb 2018 21:34:29 +0100 Subject: [PATCH] BV: split vreg_set to lv and hv --- lib/busvoodoo_global.c | 98 +++++++++++++++++++++--------------------- lib/busvoodoo_global.h | 17 +++++--- lib/busvoodoo_hiz.c | 20 ++++----- 3 files changed, 69 insertions(+), 66 deletions(-) diff --git a/lib/busvoodoo_global.c b/lib/busvoodoo_global.c index ac9be30..2047354 100644 --- a/lib/busvoodoo_global.c +++ b/lib/busvoodoo_global.c @@ -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]", diff --git a/lib/busvoodoo_global.h b/lib/busvoodoo_global.h index c89f304..a9b6a34 100644 --- a/lib/busvoodoo_global.h +++ b/lib/busvoodoo_global.h @@ -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 diff --git a/lib/busvoodoo_hiz.c b/lib/busvoodoo_hiz.c index b3565fe..93828ba 100644 --- a/lib/busvoodoo_hiz.c +++ b/lib/busvoodoo_hiz.c @@ -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 (voltage0.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