/* This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . * */ /** BusVoodoo high impedance (HiZ) default mode (code) * @file busvoodoo_hiz.c * @author King Kévin * @date 2018 */ /* standard libraries */ #include // standard integer types #include // string utilities #include // math utilities /* STM32 (including CM3) libraries */ #include // Cortex M3 utilities #include // general purpose input output library #include // real-time control clock library #include // debug utilities #include // design utilities #include // DAC utilities /* own libraries */ #include "global.h" // board definitions #include "print.h" // printing utilities #include "menu.h" // menu definitions #include "busvoodoo_global.h" // BusVoodoo definitions #include "busvoodoo_oled.h" // OLED utilities #include "busvoodoo_hiz.h" // own definitions #define BUSVOODOO_LV_DEFAULT (0.8*(1+30.0/10.0)) /**< default (when not driven) LV voltage regulator output voltage based on R1 and R2 */ #define BUSVOODOO_LV_TEST 2.5 /**< target LV output voltage to test if we can set control the LV voltage regulator */ #define BUSVOODOO_HV_DEFAULT (1.25*(1+100.0/10.0)) /**< default (when not driven) HV voltage regulator output voltage based on R1 and R2 */ #define BUSVOODOO_HV_TEST 12.0 /**< target HV output voltage to test if we can set control the HV voltage regulator */ /** setup HiZ mode * @param[out] prefix terminal prompt prefix * @param[in] line terminal prompt line to configure mode * @return if setup is complete */ static bool busvoodoo_hiz_setup(char** prefix, const char* line) { (void)line; // no configuration is required *prefix = "HiZ"; // set command line prefix busvoodoo_leds_off(); // switch off all LEDs busvoodoo_led_blue_on(); // switch on blue LED busvoodoo_oled_text_left(*prefix); // set mode title on OLED display const char* pinout_io[10] = {"GND", "5V", "3V3", "LV", NULL, NULL, NULL, NULL, NULL, NULL}; // HiZ mode I/O pinout for (uint8_t i=0; i5.5) { printf("5V power rail voltage is too high: %.2fV\n", voltage); goto error; } // check 3.3V power rail voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage if (voltage<3.0) { printf("3.3V power rail voltage is too low: %.2fV\n", voltage); goto error; } else if (voltage>3.6) { printf("3.3V power rail voltage is too high: %.2fV\n", voltage); goto error; } // test 5V and 3.3V outputs busvoodoo_vout_switch(true); // enable Vout voltage = busvoodoo_vreg_get(BUSVOODOO_5V_CHANNEL); // get 5V power rail voltage if (voltage<4.0) { printf("5V power rail voltage is too low when 5V output is enabled: %.2fV\n", voltage); goto error; } else if (voltage>5.5) { printf("5V power rail voltage is too high when 5V output is enabled: %.2fV\n", voltage); goto error; } voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get 3.3V power rail voltage if (voltage<3.0) { printf("3.3V power rail voltage is too low when 3V3 output is enabled: %.2fV\n", voltage); goto error; } else if (voltage>3.6) { printf("3.3V power rail voltage is too high when 3V3 is enabled: %.2fV\n", voltage); goto error; } busvoodoo_vout_switch(false); // disable Vout // check 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); goto error; } gpio_set(GPIO(BUSVOODOO_LVEN_PORT), GPIO(BUSVOODOO_LVEN_PIN)); // enable LV voltage regulator sleep_ms(5); // let the voltage regulator start and voltage settle voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get LV voltage // without being driven it should be around the default voltage if (voltageBUSVOODOO_LV_DEFAULT+0.2) { printf("LV voltage is higher (%.2fV) than expected (%.2fV) when the regulator is enabled\n", voltage, BUSVOODOO_LV_DEFAULT); goto error; } // check if we can control LV voltage = busvoodoo_lv_set(BUSVOODOO_LV_TEST); // get LV voltage if (voltageBUSVOODOO_LV_TEST+0.2) { printf("LV voltage is highed (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_LV_TEST); goto error; } busvoodoo_lv_set(0); // disable LV voltage regulator // check HV voltage regulator if (busvoodoo_full) { 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); goto error; } gpio_clear(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // enable HV voltage regulator sleep_ms(10); // let the voltage regulator start and voltage settle voltage = busvoodoo_vreg_get(BUSVOODOO_HV_CHANNEL); // get HV voltage // without being driven it should be around the default voltage if (voltageBUSVOODOO_HV_DEFAULT+0.4) { printf("HV voltage is higher (%.2fV) than expected (%.2fV) when regulator is enabled\n", voltage, BUSVOODOO_HV_DEFAULT); goto error; } // check if we can control HV voltage regulator voltage = busvoodoo_hv_set(BUSVOODOO_HV_TEST); // set voltage on HV voltage regulator if (voltageBUSVOODOO_HV_TEST+0.4) { printf("HV voltage is higher (%.2fV) than set (%.2fV)\n", voltage, BUSVOODOO_HV_TEST); goto error; } voltage = busvoodoo_hv_set(0); // disable HV voltage regulator } // pull all pins down and ensure they are low for (uint8_t pin=0; pin5.5) { printf("5V power rail voltage is too high when used to pull up: %.2fV\n", voltage); goto error; } for (uint8_t pin=0; pinBUSVOODOO_LV_DEFAULT+0.2) { printf("LV voltage is higher (%.2fV) than expected (%.2fV) when used to pull up\n", voltage, BUSVOODOO_LV_DEFAULT); goto error; } for (uint8_t pin=0; pin0.2 && !user_input_available); // wait until pin is shorted to ground busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_clear(GPIO(BUSVOODOO_LVCTL_PORT), GPIO(BUSVOODOO_LVCTL_PIN)); // set pin low gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV control pin as output sleep_ms(100); // wait for voltage to settle an debounce if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>0.2) { printf("I/O pin 4 is high while it should be low and shorted to ground\n"); goto error; } gpio_set_mode(GPIO(BUSVOODOO_LVCTL_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_ANALOG, GPIO(BUSVOODOO_LVCTL_PIN)); // set LV control pin back to analog input for DAC pinout[0] = NULL; // clear pin to test // test 5V output on pin 2 gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout printf("%sI/O pin 2\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[1] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, true); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout sleep_ms(100); // wait for voltage to settle and debounce if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>0.2) { printf("5V output is high while the power output should be switched off\n"); goto error; } pinout[1] = NULL; // clear pin to test // test 3.3V output on pin 3 gpio_clear(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // enable Vout printf("%sI/O pin 3\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[2] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, true); // display pins to user busvoodoo_oled_update(); // update screen do { do { sleep_ms(100); // wait for user to make connection } while ((busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 || busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>3.5) && !user_input_available); // wait until pin is connected sleep_ms(100); // wait for stable connection } while ((busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 || busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>3.5) && !user_input_available); // check to be sure it really is not on the 5V pin busvoodoo_led_red_on(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_VOUTEN_PORT), GPIO(BUSVOODOO_VOUTEN_PIN)); // disable Vout sleep_ms(100); // wait for voltage to settle and debounce if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>0.2) { printf("3V3 output is high while the power output should be switched off\n"); goto error; } pinout[2] = NULL; // clear pin to test // test I/O pins for (uint8_t io=1; io<=6; io++) { // test each I/O pin for (uint8_t pin=0; pin0.2) { printf("I/O pin %u is high while it should be low\n", io+4); goto error; } gpio_set_mode(busvoodoo_io_ports[pin], GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, busvoodoo_io_pins[pin]); // set pin back to input pinout[io+3] = NULL; // clear pin to test break; // stop looking for pin } } } if (busvoodoo_full) { pinout[3] = NULL; // reset testing pin // test HV output on RS/CAN pin 1 double voltage = busvoodoo_vreg_get(BUSVOODOO_3V3_CHANNEL); // get reference voltage uint16_t dac_set = BUSVOODOO_HV_SET(5.0)/voltage*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 printf("%sRS/CAN pin 1\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[0] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<0.2 && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_HVEN_PORT), GPIO(BUSVOODOO_HVEN_PIN)); // disable HV voltage regulator dac_disable(BUSVOODOO_HVCTL_CHANNEL); // disable HV control sleep_ms(100); // wait for voltage to settle (and debounce) if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>0.2) { printf("HV output is high while voltage regulator should be switched off\n"); goto error; } pinout[0] = NULL; // clear pin to test #if BUSVOODOO_HARDWARE_VERSION==0 // test RS-485 port pin B gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low -> B will be high printf("%sRS/CAN pin 4\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[6] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<2.0 && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high sleep_ms(100); // wait for voltage to settle (and debounce) if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>1.0) { printf("RS-485 output B is high while it should be low\n"); goto error; } gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating pinout[6] = NULL; // clear pin to test #else // test RS-485 output A gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high -> A will be high printf("%sRS/CAN pin 3\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[4] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<2.0 && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low sleep_ms(100); // wait for voltage to settle (and debounce) if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>1.0) { printf("RS-485 output A is high while it should be low\n"); goto error; } gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating pinout[4] = NULL; // clear pin to test #endif #if BUSVOODOO_HARDWARE_VERSION!=0 // test CAN output L // configure transceiver gpio_clear(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // pull low to power transceiver gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX as output (there is a pull-up resistor to 5V) // test recessive output gpio_set(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX high sleep_us(100); // let voltage settle voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage if (voltage<0.4) { // normally the lower limit is 2V, put pin 4 pulls it down because it is not strongly driven printf("CAN L output is low while it should be at recessive 2.5V\n"); goto error; } if (voltage>3.0) { // normally the upper limit is 3V printf("CAN L output is high while it should be at recessive 2.5V\n"); goto error; } // test dominant output gpio_clear(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX low sleep_us(100); // let voltage settle voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage if (voltage>2.5) { printf("CAN L output is high while it should at dominant <2.25V\n"); goto error; } // release transceiver gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating #endif #if BUSVOODOO_HARDWARE_VERSION==0 // test RS-485 output A gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high -> A will be high printf("%sRS/CAN pin 5\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[8] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<2.0 && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low sleep_ms(100); // wait for voltage to settle (and debounce) if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>1.0) { printf("RS-485 output A is high while it should be low\n"); goto error; } gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating pinout[8] = NULL; // clear pin to test #else // test RS-485 port pin B gpio_set(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // enable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX as output gpio_clear(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX low -> B will be high printf("%sRS/CAN pin 4\n", lv_to); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[6] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)<2.0 && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO(BUSVOODOO_RS485_TX_PIN)); // set TX high sleep_ms(100); // wait for voltage to settle (and debounce) if (busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL)>1.0) { printf("RS-485 output B is high while it should be low\n"); goto error; } gpio_clear(GPIO(BUSVOODOO_RS485_DE_PORT), GPIO(BUSVOODOO_RS485_DE_PIN)); // set low to disable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS485_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS485_TX_PIN)); // set pin to floating pinout[6] = NULL; // clear pin to test #endif #if BUSVOODOO_HARDWARE_VERSION!=0 // test CAN output H // configure transceiver gpio_clear(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // pull low to power transceiver gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX as output (there is a pull-up resistor to 5V) // test recessive output gpio_set(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX high sleep_us(100); // let voltage settle voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage if (voltage<0.5) { // normally the lower limit is 2V, put pin 4 pulls it down because it is not strongly driven printf("CAN H output is low while it should be at recessive 2.5V\n"); goto error; } if (voltage>3.0) { // normally the upper limit is 3V printf("CAN H output is high while it should be at recessive 2.5V\n"); goto error; } // test dominant output gpio_clear(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO(BUSVOODOO_CAN_TX_PIN)); // set TX low sleep_us(100); // let voltage settle voltage = busvoodoo_vreg_get(BUSVOODOO_LV_CHANNEL); // get output voltage if (voltage<2.5) { printf("CAN H output is low while it dominant >2.75V\n"); printf("%.02f\n", voltage); goto error; } // release transceiver gpio_set(GPIO(BUSVOODOO_CAN_EN_PORT), GPIO(BUSVOODOO_CAN_EN_PIN)); // set high to power off transceiver gpio_set_mode(GPIO(BUSVOODOO_CAN_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_CAN_TX_PIN)); // set pin to floating #endif // test RS-232 port (with itself) rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_EN_PORT)); // enable clock for GPIO domain gpio_clear(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set low to enable receiver gpio_set_mode(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO(BUSVOODOO_RS232_EN_PIN)); // set pin as output (open-drain pulled high to disable receiver) rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_SHDN_PORT)); // enable clock for GPIO domain gpio_set(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set high to enable transmitter gpio_set_mode(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set pin as output (push-pull pulled low to disable transmitter) rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_TX_PORT)); // enable clock for GPIO gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_TX_PIN)); // set pin as output (push-pull) rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_RX_PORT)); // enable clock for GPIO gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_RS232_RX_PIN)); // set pin as input (with pull resistors) // start by setting low since unconnected (pulled to ground by 3 kO) is considered as high gpio_clear(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO(BUSVOODOO_RS232_TX_PIN)); // set low gpio_set(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN)); // pull high to avoid false negative sleep_ms(5); printf("connect RS/CAN pin 2 to RS/CAN pin 3\n"); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[2] = "O"; // set target testing pin pinout[4] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (gpio_get(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN)) && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO(BUSVOODOO_RS232_TX_PIN)); // set high gpio_clear(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN)); // pull low to avoid false negative sleep_ms(100); // wait for voltage to settle and debounce if (!gpio_get(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO(BUSVOODOO_RS232_RX_PIN))) { // check if RX is set low by TX printf("RS-232 RX is high while it should be set low by RS-232 TX\n"); goto error; } gpio_set_mode(GPIO(BUSVOODOO_RS232_TX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_TX_PIN)); // free pin gpio_set_mode(GPIO(BUSVOODOO_RS232_RX_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RX_PIN)); // free pin pinout[2] = NULL; // clear pin to test pinout[4] = NULL; // clear pin to test rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_RTS_PORT)); // enable clock for GPIO gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO(BUSVOODOO_RS232_RTS_PIN)); // set pin as output (push-pull) rcc_periph_clock_enable(RCC_GPIO(BUSVOODOO_RS232_CTS_PORT)); // enable clock for GPIO gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_PULL_UPDOWN, GPIO(BUSVOODOO_RS232_CTS_PIN)); // set pin as input (with pull resistors) // start by setting low since unconnected (pulled to ground by 3 kO) is considered as high gpio_clear(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO(BUSVOODOO_RS232_RTS_PIN)); // set low gpio_set(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN)); // pull high to avoid false negative printf("connect RS/CAN pin 4 to RS/CAN pin 5\n"); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // notify user to perform action pinout[6] = "O"; // set target testing pin pinout[8] = "O"; // set target testing pin busvoodoo_oled_text_pinout((const char**)pinout, false); // display pins to user busvoodoo_oled_update(); // update screen do { sleep_ms(100); // wait for user to make connection } while (gpio_get(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN)) && !user_input_available); // wait until pin is connected busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // notify user test is running if (user_input_available) { // user interruption goto error; } gpio_set(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO(BUSVOODOO_RS232_RTS_PIN)); // set high gpio_clear(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN)); // pull low to avoid false negative sleep_ms(100); // wait for voltage to settle an debounce if (!gpio_get(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO(BUSVOODOO_RS232_CTS_PIN))) { // check if CTS is set high by RTS printf("RS-232 CTS is high while it should be set low by RS-232 RTS\n"); goto error; } gpio_set_mode(GPIO(BUSVOODOO_RS232_RTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_RTS_PIN)); // free pin gpio_set_mode(GPIO(BUSVOODOO_RS232_CTS_PORT), GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO(BUSVOODOO_RS232_CTS_PIN)); // free pin gpio_set(GPIO(BUSVOODOO_RS232_EN_PORT), GPIO(BUSVOODOO_RS232_EN_PIN)); // set high to disable receiver gpio_clear(GPIO(BUSVOODOO_RS232_SHDN_PORT), GPIO(BUSVOODOO_RS232_SHDN_PIN)); // set low to disable transmitter pinout[6] = NULL; // clear pin to test pinout[8] = NULL; // clear pin to test } to_return = true; // all tests passed error: if (!to_return) { if (user_input_available) { printf("user interrupted the test\n"); to_return = true; // we don't consider this as error } else if (halt) { // halt on error if requested busvoodoo_leds_blink(0.5, 0.5); // show error on LEDs while (!user_input_available); // wait for user input } else { printf("the test procedure has been aborted for safety reasons\n"); } } busvoodoo_safe_state(); // go back to safe state return to_return; } // command handlers /** switch to DFU bootloader * @param[in] argument no argument required * @note this handler is in HiZ mode only to save shortcut space and reduce menu size */ static void busvoodoo_hiz_bootloader(void* argument) { (void)argument; // we won't use the argument // set DFU magic to specific RAM location __dfu_magic[0] = 'D'; __dfu_magic[1] = 'F'; __dfu_magic[2] = 'U'; __dfu_magic[3] = '!'; scb_reset_system(); // reset system (core and peripherals) while (true); // wait for the reset to happen } /** show BusVoodoo version * @param[in] argument no argument required * @note this handler is in HiZ mode only to save shortcut space and reduce menu size */ static void busvoodoo_hiz_version(void* argument) { (void)argument; // we won't use the argument printf("BusVoodoo flavor: %s\n", busvoodoo_full ? "full" : "light"); printf("hardware version: %c\n", busvoodoo_version); printf("firmware date: %04u-%02u-%02u\n", BUILD_YEAR, BUILD_MONTH, BUILD_DAY); printf("device ID: %08x%08x%08x\n", DESIG_UNIQUE_ID0, DESIG_UNIQUE_ID1, DESIG_UNIQUE_ID2); } /** command to perform board self-test * @param[in] argument "halt" to halt on error */ static void busvoodoo_hiz_command_test_self(void* argument) { bool halt = false; // if we halt on error if (NULL!=argument && strlen(argument)>0) { if (0==strcmp(argument, "halt")) { halt = true; } else { printf("malformed argument\n"); return; } } printf("performing self-test\n"); if (halt) { printf("WARNING: halting on error can cause hardware damages (press any key to exit halt state)\n"); } printf("remove all cables from connectors and press space to start (or any other key to abort)\n"); busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // show red LED to indicate test started if (' '==user_input_get()) { // space entered printf("self-test running\n"); if (busvoodoo_hiz_test_self(halt)) { // perform self-test busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // show blue to indicate test passed } else { busvoodoo_leds_blink(0.5, 0.5); // show error on LEDs printf("self-test failed\n"); // notify user if (user_input_available) { user_input_get(); // clear user input } } } else { printf("self-test aborted\n"); } } /** command to perform pins test * @param[in] argument "halt" to halt on error */ static void busvoodoo_hiz_command_test_pins(void* argument) { bool halt = false; // if we halt on error if (NULL!=argument && strlen(argument)>0) { if (0==strcmp(argument, "halt")) { halt = true; } else { printf("malformed argument\n"); return; } } printf("performing pins test\n"); if (halt) { printf("WARNING: halting on error can cause hardware damages (press any key to exit halt state)\n"); } busvoodoo_leds_off(); // clear LEDs busvoodoo_led_red_on(); // show red LED to indicate test started char* pinout[10] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL}; // pinout to display pinout[3] = "O"; // set main testing pin busvoodoo_oled_text_right("pins test"); // display test on display busvoodoo_oled_text_pinout((const char**)pinout, true); // reset pinout busvoodoo_oled_update(); // update screen printf("connect one lead of jumper wire to I/O pin 4 and press space to start (or any other key to abort)\n"); if (' '==user_input_get()) { // space entered if (busvoodoo_hiz_test_pins(halt)) { // perform pin test busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // show blue OK LED printf("pins test succeeded\n"); // notify user } else { busvoodoo_leds_blink(0.5, 0.5); // show error on LEDs printf("pins test failed\n"); // notify user if (user_input_available) { user_input_get(); // clear user input } } } else { printf("pins test aborted\n"); busvoodoo_led_red_off(); // clear red LED busvoodoo_led_blue_on(); // switch back to main blue LED } busvoodoo_oled_text_left("HiZ"); // reset mode text busvoodoo_oled_text_pinout(busvoodoo_global_pinout_io, true); // reset pinout busvoodoo_oled_update(); // update display to show text and pinout } /** HiZ menu commands */ static const struct menu_command_t busvoodoo_hiz_commands[] = { { .shortcut = 'v', .name = "version", .command_description = "show hardware and firmware version", .argument = MENU_ARGUMENT_NONE, .argument_description = NULL, .command_handler = &busvoodoo_hiz_version, }, { .shortcut = 'b', .name = "bootloader", .command_description = "reboot into DFU bootloader", .argument = MENU_ARGUMENT_NONE, .argument_description = NULL, .command_handler = &busvoodoo_hiz_bootloader, }, { .shortcut = 's', .name = "self-test [halt]", .command_description = "perform board self-test (optional halt on error)", .argument = MENU_ARGUMENT_STRING, .argument_description = NULL, .command_handler = &busvoodoo_hiz_command_test_self, }, { .shortcut = 't', .name = "pins-test [halt]", .command_description = "perform connector pins test (optional halt on error)", .argument = MENU_ARGUMENT_STRING, .argument_description = NULL, .command_handler = &busvoodoo_hiz_command_test_pins, }, }; const struct busvoodoo_mode_t busvoodoo_hiz_mode = { .name = "hiz", .description = "High Impedance (Z)", .full_only = false, .setup = &busvoodoo_hiz_setup, .commands = busvoodoo_hiz_commands, .commands_nb = LENGTH(busvoodoo_hiz_commands), .exit = &busvoodoo_hiz_exit, };