diff --git a/src/drivers/boards/nxphlite-v3/board_config.h b/src/drivers/boards/nxphlite-v3/board_config.h index 239267573a..fa9530a418 100644 --- a/src/drivers/boards/nxphlite-v3/board_config.h +++ b/src/drivers/boards/nxphlite-v3/board_config.h @@ -416,7 +416,7 @@ __BEGIN_DECLS * this board support the ADC system_power interface, and therefore * provides the true logic GPIO BOARD_ADC_xxxx macros. */ -#define BOARD_ADC_USB_CONNECTED board_read_VBUS_state() +#define BOARD_ADC_USB_CONNECTED ADC_USB_VBUS_VALID /* VBUS valid is read on an ADC pin */ #define BOARD_ADC_BRICK_VALID (1) #define BOARD_ADC_SERVO_VALID (1) #define BOARD_ADC_PERIPH_5V_OC (0) diff --git a/src/drivers/boards/nxphlite-v3/nxphlite_init.c b/src/drivers/boards/nxphlite-v3/nxphlite_init.c index 338a424f1d..b0bb7d20f3 100644 --- a/src/drivers/boards/nxphlite-v3/nxphlite_init.c +++ b/src/drivers/boards/nxphlite-v3/nxphlite_init.c @@ -115,6 +115,7 @@ __BEGIN_DECLS extern void led_init(void); extern void led_on(int led); extern void led_off(int led); +extern bool g_board_usb_connected; __END_DECLS /**************************************************************************** @@ -139,7 +140,7 @@ __END_DECLS int board_read_VBUS_state(void) { // read * 36 ADC0_SE21 USB_VBUS_VALID - return 0; + return g_board_usb_connected ? 0 : 1; } /************************************************************************************ diff --git a/src/drivers/kinetis/adc/adc.cpp b/src/drivers/kinetis/adc/adc.cpp index 3331db6e4b..a70436dccf 100644 --- a/src/drivers/kinetis/adc/adc.cpp +++ b/src/drivers/kinetis/adc/adc.cpp @@ -72,7 +72,7 @@ #include #include -#if 1|defined(ADC_CHANNELS) +#if defined(ADC_CHANNELS) typedef uint32_t adc_chan_t; #define ADC_TOTAL_CHANNELS 32 @@ -111,6 +111,8 @@ typedef uint32_t adc_chan_t; #define rCLM1(adc) REG(adc, KINETIS_ADC_CLM1_OFFSET) /* ADC minus-side general calibration value register */ #define rCLM0(adc) REG(adc, KINETIS_ADC_CLM0_OFFSET) /* ADC minus-side general calibration value register */ +bool g_board_usb_connected = false; + class ADC : public device::CDev { public: @@ -370,13 +372,25 @@ ADC::update_system_power(hrt_abstime now) system_power.voltage5V_v = 0; -#if defined(ADC_5V_RAIL_SENSE) +#if defined(ADC_5V_RAIL_SENSE) || defined(BOARD_ADC_USB_CONNECTED) for (unsigned i = 0; i < _channel_count; i++) { +#if defined(ADC_5V_RAIL_SENSE) + if (_samples[i].am_channel == ADC_5V_RAIL_SENSE) { // it is 2:1 scaled system_power.voltage5V_v = _samples[i].am_data * (6.6f / 4096); } + +#endif +#if defined(BOARD_ADC_USB_CONNECTED) + + if (_samples[i].am_channel == BOARD_ADC_USB_CONNECTED) { + // > that 50% assum connected + system_power.usb_connected = _samples[i].am_data > (4096 / 2); + } + +#endif } #endif @@ -387,7 +401,7 @@ ADC::update_system_power(hrt_abstime now) // these are not ADC related, but it is convenient to // publish these to the same topic - system_power.usb_connected = BOARD_ADC_USB_CONNECTED; + g_board_usb_connected = system_power.usb_connected; system_power.brick_valid = BOARD_ADC_BRICK_VALID; system_power.servo_valid = BOARD_ADC_SERVO_VALID; @@ -459,7 +473,7 @@ test(void) } for (unsigned i = 0; i < 50; i++) { - adc_msg_s data[12]; + adc_msg_s data[ADC_TOTAL_CHANNELS]; ssize_t count = read(fd, data, sizeof(data)); if (count < 0) {