Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fmu handle sensor_reset, peripheral_reset, i2c in board #11347

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 0 additions & 8 deletions ROMFS/px4fmu_common/init.d/rc.sensors
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@

if ! ver hwcmp AEROFC_V1 OMNIBUS_F4SD
then
if ! ver hwcmp BITCRAZE_CRAZYFLIE
then
# Configure all I2C buses to 100 KHz as they
# are all external or slow
fmu i2c 1 100000
fmu i2c 2 100000
fi

# External SPI
ms5611 -S start

Expand Down
9 changes: 0 additions & 9 deletions boards/px4/fmu-v4/init/rc.board_sensors
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@
# PX4 FMUv4 specific board sensors init
#------------------------------------------------------------------------------

# We know there are sketchy boards out there
# as chinese companies produce Pixracers without
# fully understanding the critical parts of the
# schematic and BOM, leading to sensor brownouts
# on boot. Original Pixracers following the
# open hardware design do not require this.
fmu sensor_reset 50


# External I2C bus
hmc5883 -C -T -X start
lis3mdl -X start
Expand Down
12 changes: 11 additions & 1 deletion boards/px4/fmu-v4/src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ stm32_boardinitialize(void)

#endif /* CONFIG_STM32_SPI4 */

// Configure SPI all interfaces GPIO.
// Configure SPI all interfaces GPIO.
stm32_spiinitialize(spi_init_mask);

// Configure heater GPIO.
Expand Down Expand Up @@ -399,5 +399,15 @@ __EXPORT int board_app_initialize(uintptr_t arg)

#endif

/*
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we are not doing the GPIO init and this just masks the root issues.

As a side note are we ok delaying Boot? We can Break the reset into 2 pieces, it is all GPIO manipulation. Make the arch init set things up correctly. Then use the delay 3+ ms. from the aching to the app init. Then set up a call back to finish the job.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes I figured this would prompt the discussion to check for what's actually needed.

* We know there are sketchy boards out there
* as chinese companies produce Pixracers without
* fully understanding the critical parts of the
* schematic and BOM, leading to sensor brownouts
* on boot. Original Pixracers following the
* open hardware design do not require this.
*/
board_spi_reset(50);

return OK;
}
1 change: 1 addition & 0 deletions boards/px4/fmu-v5/default.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ px4_add_board(

SYSTEMCMDS
bl_update
board_control
config
dumpfile
esc_calib
Expand Down
96 changes: 1 addition & 95 deletions src/drivers/px4fmu/fmu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@

#include <board_config.h>
#include <drivers/device/device.h>
#include <drivers/device/i2c.h>
#include <drivers/drv_hrt.h>
#include <drivers/drv_input_capture.h>
#include <drivers/drv_mixer.h>
Expand All @@ -50,6 +49,7 @@
#include <px4_getopt.h>
#include <px4_log.h>
#include <px4_module.h>
#include <px4_workqueue.h>
#include <circuit_breaker/circuit_breaker.h>
#include <lib/cdev/CDev.hpp>
#include <lib/mixer/mixer.h>
Expand Down Expand Up @@ -168,8 +168,6 @@ class PX4FMU : public cdev::CDev, public ModuleBase<PX4FMU>
int set_pwm_alt_rate(unsigned rate);
int set_pwm_alt_channels(uint32_t channels);

static int set_i2c_bus_clock(unsigned bus, unsigned clock_hz);

static void capture_trampoline(void *context, uint32_t chan_index,
hrt_abstime edge_time, uint32_t edge_state,
uint32_t overflow);
Expand Down Expand Up @@ -265,9 +263,6 @@ class PX4FMU : public cdev::CDev, public ModuleBase<PX4FMU>

void update_params();

static void sensor_reset(int ms);
static void peripheral_reset(int ms);

int capture_ioctl(file *filp, int cmd, unsigned long arg);

PX4FMU(const PX4FMU &) = delete;
Expand Down Expand Up @@ -809,12 +804,6 @@ PX4FMU::set_pwm_alt_channels(uint32_t channels)
return set_pwm_rate(channels, _pwm_default_rate, _pwm_alt_rate);
}

int
PX4FMU::set_i2c_bus_clock(unsigned bus, unsigned clock_hz)
{
return device::I2C::set_bus_clock(bus, clock_hz);
}

void
PX4FMU::subscribe()
{
Expand Down Expand Up @@ -2263,26 +2252,6 @@ PX4FMU::reorder_outputs(uint16_t values[MAX_ACTUATORS])
*/
}

void
PX4FMU::sensor_reset(int ms)
{
if (ms < 1) {
ms = 1;
}

board_spi_reset(ms);
}

void
PX4FMU::peripheral_reset(int ms)
{
if (ms < 1) {
ms = 10;
}

board_peripheral_reset(ms);
}

int
PX4FMU::capture_ioctl(struct file *filp, int cmd, unsigned long arg)
{
Expand Down Expand Up @@ -2548,17 +2517,6 @@ PX4FMU::fmu_new_mode(PortMode new_mode)
return OK;
}


namespace
{

int fmu_new_i2c_speed(unsigned bus, unsigned clock_hz)
{
return PX4FMU::set_i2c_bus_clock(bus, clock_hz);
}

} // namespace

int
PX4FMU::test()
{
Expand Down Expand Up @@ -2817,50 +2775,6 @@ int PX4FMU::custom_command(int argc, char *argv[])
PortMode new_mode = PORT_MODE_UNSET;
const char *verb = argv[0];

/* does not operate on a FMU instance */
if (!strcmp(verb, "i2c")) {
if (argc > 2) {
int bus = strtol(argv[1], 0, 0);
int clock_hz = strtol(argv[2], 0, 0);
int ret = fmu_new_i2c_speed(bus, clock_hz);

if (ret) {
PX4_ERR("setting I2C clock failed");
}

return ret;
}

return print_usage("not enough arguments");
}

if (!strcmp(verb, "sensor_reset")) {
if (argc > 1) {
int reset_time = strtol(argv[1], nullptr, 0);
sensor_reset(reset_time);

} else {
sensor_reset(0);
PX4_INFO("reset default time");
}

return 0;
}

if (!strcmp(verb, "peripheral_reset")) {
if (argc > 2) {
int reset_time = strtol(argv[2], 0, 0);
peripheral_reset(reset_time);

} else {
peripheral_reset(0);
PX4_INFO("reset default time");
}

return 0;
}


/* start the FMU if not running */
if (!is_running()) {
int ret = PX4FMU::task_spawn(argc, argv);
Expand Down Expand Up @@ -3019,14 +2933,6 @@ mixer files.
PRINT_MODULE_USAGE_COMMAND("mode_pwm1");
#endif

PRINT_MODULE_USAGE_COMMAND_DESCR("sensor_reset", "Do a sensor reset (SPI bus)");
PRINT_MODULE_USAGE_ARG("<ms>", "Delay time in ms between reset and re-enabling", true);
PRINT_MODULE_USAGE_COMMAND_DESCR("peripheral_reset", "Reset board peripherals");
PRINT_MODULE_USAGE_ARG("<ms>", "Delay time in ms between reset and re-enabling", true);

PRINT_MODULE_USAGE_COMMAND_DESCR("i2c", "Configure I2C clock rate");
PRINT_MODULE_USAGE_ARG("<bus_id> <rate>", "Specify the bus id (>=0) and rate in Hz", false);

PRINT_MODULE_USAGE_COMMAND_DESCR("test", "Test inputs and outputs");
PRINT_MODULE_USAGE_COMMAND_DESCR("fake", "Arm and send an actuator controls command");
PRINT_MODULE_USAGE_ARG("<roll> <pitch> <yaw> <thrust>", "Control values in range [-100, 100]", false);
Expand Down
43 changes: 43 additions & 0 deletions src/systemcmds/board_control/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
############################################################################
#
# Copyright (c) 2019 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# 1. Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in
# the documentation and/or other materials provided with the
# distribution.
# 3. Neither the name PX4 nor the names of its contributors may be
# used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
############################################################################

px4_add_module(
MODULE systemcmds__board_control
MAIN board_control
STACK_MAIN 1200
COMPILE_FLAGS
SRCS
board_control.cpp
DEPENDS

)
116 changes: 116 additions & 0 deletions src/systemcmds/board_control/board_control.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
/****************************************************************************
*
* Copyright (c) 2019 PX4 Development Team. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name PX4 nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/

#include <px4_config.h>
#include <px4_log.h>
#include <px4_module.h>

#include <drivers/device/device.h>
#include <drivers/device/i2c.h>

extern "C" __EXPORT int board_control_main(int argc, char *argv[]);

static int print_usage(const char *reason = nullptr);

int
board_control_main(int argc, char *argv[])
{
const char *verb = argv[0];

/* does not operate on a FMU instance */
if (!strcmp(verb, "i2c")) {
if (argc > 2) {
int bus = strtol(argv[1], 0, 0);
int clock_hz = strtol(argv[2], 0, 0);
int ret = device::I2C::set_bus_clock(bus, clock_hz);

if (ret) {
PX4_ERR("setting I2C clock failed");
}

return ret;
}

return print_usage("not enough arguments");
}

if (!strcmp(verb, "sensor_reset")) {
if (argc > 1) {
int reset_time = strtol(argv[1], nullptr, 0);
board_spi_reset(reset_time);

} else {
board_spi_reset(10);
PX4_INFO("reset default time");
}

return 0;
}

if (!strcmp(verb, "peripheral_reset")) {
if (argc > 2) {
int reset_time = strtol(argv[2], 0, 0);
board_peripheral_reset(reset_time);

} else {
board_peripheral_reset(10);
PX4_INFO("reset default time");
}

return 0;
}

return print_usage();
}

static int print_usage(const char *reason)
{
if (reason) {
PX4_WARN("%s\n", reason);
}

PRINT_MODULE_DESCRIPTION("board control (sensor bus power, etc)");

PRINT_MODULE_USAGE_NAME("board", "command");

PRINT_MODULE_USAGE_COMMAND_DESCR("sensor_reset", "Do a sensor reset (SPI bus)");
PRINT_MODULE_USAGE_ARG("<ms>", "Delay time in ms between reset and re-enabling", true);

PRINT_MODULE_USAGE_COMMAND_DESCR("peripheral_reset", "Reset board peripherals");
PRINT_MODULE_USAGE_ARG("<ms>", "Delay time in ms between reset and re-enabling", true);

PRINT_MODULE_USAGE_COMMAND_DESCR("i2c", "Configure I2C clock rate");
PRINT_MODULE_USAGE_ARG("<bus_id> <rate>", "Specify the bus id (>=0) and rate in Hz", false);

return 0;
}