mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-11-24 16:01:53 +08:00
fix(uavcan): report FlexCAN bus state and apply UAVCAN_BITRATE on SocketCAN (#28457)
* fix(uavcan): report FlexCAN bus state and error counters on SocketCAN The SocketCAN platform driver returned 0 from getErrorCount(), getRxQueueOverflowCount() and isInBusOffState(), so a controller sitting error-passive or bus-off was invisible from `uavcan status` and the can_interface_status topic on every i.MX RT board. Read them through NuttX's SIOCGCANERRORS instead, and print fault confinement, TEC/REC and RX overruns per interface. Builds without the ioctl keep the old zeros. * fix(uavcan): apply UAVCAN_BITRATE on SocketCAN CanDriver::init() ignored its bitrate argument, so the controller stayed at the Kconfig rate and a bus configured for anything but 1 Mbit/s never came up. Program the nominal rate through SIOCSCANBITRATE when it differs from what the driver reports, keeping the data-phase settings untouched. The driver applies the timing at the next ifup, so the interface is taken down around the request. Older PX4/NuttX restarted a running controller from inside the ioctl instead, which on FlexCAN with ECC RAM initialisation is a bus fault; against that NuttX the rate is left as configured with a warning. * fix(boards): switch SocketCAN defconfigs to NETDEV_CAN_IOCTL PX4/NuttX#401 merges the per-command CAN ioctl options into a single NETDEV_CAN_IOCTL, so the old BITRATE/FILTER/ERROR names no longer exist there and would be silently dropped, disabling the whole CAN ioctl block. Requires the NuttX gitlink to include PX4/NuttX#401; until that bump the option is dropped against the old tree and the bitrate/error ioctls are inert at runtime (the code builds either way). mr-canhubk3 is included so its bitrate ioctl survives the bump. Assisted-by: Claude:claude-fable-5 Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com> * chore(nuttx): bump NuttX to the px4_firmware_nuttx-10.3.0+ tip Picks up PX4/NuttX#401 so SIOCGCANERRORS, SIOCSCANBITRATE-requires-ifdown and NETDEV_CAN_IOCTL are in the tree the SocketCAN changes need. Also includes PX4/NuttX#399 (STM32F412VG/CG chip selections), which landed between main's gitlink and #401. Assisted-by: Grok:grok-4.6 Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com> --------- Signed-off-by: Jacob Dahl <dahl.jakejacob@gmail.com>
This commit is contained in:
1 parent
b182e523d1
commit
0bc69038da
10 files changed
+225
-14
No files matched your search
@@ -208,7 +208,7 @@ CONFIG_NAME_MAX=40
|
||||
CONFIG_NET=y
|
||||
CONFIG_NETDB_DNSCLIENT=y
|
||||
CONFIG_NETDB_DNSSERVER_NOADDR=y
|
||||
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_IOCTL=y
|
||||
CONFIG_NETDEV_LATEINIT=y
|
||||
CONFIG_NETDEV_PHY_IOCTL=y
|
||||
CONFIG_NETINIT_DNS=y
|
||||
|
||||
@@ -163,7 +163,7 @@ CONFIG_NET=y
|
||||
CONFIG_NETDB_DNSCLIENT=y
|
||||
CONFIG_NETDB_DNSCLIENT_ENTRIES=8
|
||||
CONFIG_NETDB_DNSSERVER_NOADDR=y
|
||||
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_IOCTL=y
|
||||
CONFIG_NETDEV_LATEINIT=y
|
||||
CONFIG_NETDEV_PHY_IOCTL=y
|
||||
CONFIG_NETINIT_DHCPC=y
|
||||
|
||||
@@ -154,7 +154,7 @@ CONFIG_NET=y
|
||||
CONFIG_NETDB_DNSCLIENT=y
|
||||
CONFIG_NETDB_DNSCLIENT_ENTRIES=8
|
||||
CONFIG_NETDB_DNSSERVER_NOADDR=y
|
||||
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_IOCTL=y
|
||||
CONFIG_NETDEV_LATEINIT=y
|
||||
CONFIG_NETDEV_PHY_IOCTL=y
|
||||
CONFIG_NETINIT_DHCPC=y
|
||||
|
||||
@@ -175,8 +175,7 @@ CONFIG_NAME_MAX=40
|
||||
CONFIG_NET=y
|
||||
CONFIG_NETDB_DNSCLIENT=y
|
||||
CONFIG_NETDB_DNSSERVER_NOADDR=y
|
||||
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_FILTER_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_IOCTL=y
|
||||
CONFIG_NETDEV_LATEINIT=y
|
||||
CONFIG_NETDEV_PHY_IOCTL=y
|
||||
CONFIG_NETINIT_DHCPC=y
|
||||
|
||||
@@ -152,8 +152,7 @@ CONFIG_NAME_MAX=40
|
||||
CONFIG_NET=y
|
||||
CONFIG_NETDB_DNSCLIENT=y
|
||||
CONFIG_NETDB_DNSSERVER_NOADDR=y
|
||||
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_FILTER_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_IOCTL=y
|
||||
CONFIG_NETDEV_LATEINIT=y
|
||||
CONFIG_NETDEV_PHY_IOCTL=y
|
||||
CONFIG_NETINIT_DHCPC=y
|
||||
|
||||
@@ -205,7 +205,7 @@ CONFIG_NAME_MAX=40
|
||||
CONFIG_NET=y
|
||||
CONFIG_NETDB_DNSCLIENT=y
|
||||
CONFIG_NETDB_DNSSERVER_NOADDR=y
|
||||
CONFIG_NETDEV_CAN_BITRATE_IOCTL=y
|
||||
CONFIG_NETDEV_CAN_IOCTL=y
|
||||
CONFIG_NETDEV_LATEINIT=y
|
||||
CONFIG_NETDEV_PHY_IOCTL=y
|
||||
CONFIG_NETINIT_DHCPC=y
|
||||
|
||||
Submodule platforms/nuttx/NuttX/nuttx updated: 05b3b5180d...b0e70b2ef8.
@@ -50,9 +50,34 @@ namespace uavcan_socketcan
|
||||
class CanIface : public uavcan::ICanIface
|
||||
, uavcan::Noncopyable
|
||||
{
|
||||
public:
|
||||
/**
|
||||
* Controller fault confinement and error counters, from the NuttX
|
||||
* SIOCGCANERRORS ioctl. `errors` counts bus errors the driver has seen
|
||||
* since the interface came up and only ever grows; `tec`/`rec` are the
|
||||
* live CAN error counters, which fall again as the bus recovers.
|
||||
*/
|
||||
struct BusErrors {
|
||||
uint8_t state{0};
|
||||
uint8_t tec{0};
|
||||
uint8_t rec{0};
|
||||
uint32_t errors{0};
|
||||
uint32_t rx_overruns{0};
|
||||
|
||||
static constexpr uint8_t Active = 0;
|
||||
static constexpr uint8_t Warning = 1;
|
||||
static constexpr uint8_t Passive = 2;
|
||||
static constexpr uint8_t BusOff = 3;
|
||||
};
|
||||
|
||||
private:
|
||||
int _fd{-1};
|
||||
uint32_t _index{0};
|
||||
bool _can_fd{false};
|
||||
|
||||
mutable BusErrors _bus_errors;
|
||||
mutable bool _bus_errors_valid{false};
|
||||
|
||||
//// Send msg structure
|
||||
struct iovec _send_iov {};
|
||||
struct canfd_frame _send_frame {};
|
||||
@@ -90,6 +115,34 @@ public:
|
||||
uavcan::uint16_t getNumFilters() const override;
|
||||
|
||||
int getFD();
|
||||
|
||||
|
||||
/**
|
||||
* Read the counters from the driver into the cache. NuttX descriptor
|
||||
* tables are per task group, so this must run on the uavcan work queue
|
||||
* that owns the socket; getErrorCount() does that every status period.
|
||||
* Returns 0 on success, -1 if the ioctl is unavailable on this build.
|
||||
*/
|
||||
int pollErrors() const;
|
||||
|
||||
/**
|
||||
* Last polled counters, readable from any task (`uavcan status` runs in
|
||||
* the nsh task). Returns false until the first successful poll.
|
||||
*/
|
||||
bool lastErrors(BusErrors &out) const
|
||||
{
|
||||
out = _bus_errors;
|
||||
return _bus_errors_valid;
|
||||
}
|
||||
|
||||
static const char *busStateName(uint8_t state);
|
||||
|
||||
/**
|
||||
* Program the controller's nominal bit rate if it differs from what is
|
||||
* running. Returns 0 on success or when the ioctl is unavailable,
|
||||
* negative if the controller rejected the rate.
|
||||
*/
|
||||
int setBitRate(uint32_t bitrate);
|
||||
};
|
||||
|
||||
/**
|
||||
|
||||
@@ -46,6 +46,7 @@
|
||||
|
||||
#include <net/if.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <inttypes.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
@@ -80,6 +81,7 @@ uavcan::uint32_t CanIface::socketInit(uint32_t index)
|
||||
bool can_fd = 0;
|
||||
|
||||
_can_fd = can_fd;
|
||||
_index = index;
|
||||
|
||||
/* open socket */
|
||||
if ((_fd = socket(PF_CAN, SOCK_RAW, CAN_RAW)) < 0) {
|
||||
@@ -276,8 +278,8 @@ uavcan::int16_t CanIface::configureFilters(const uavcan::CanFilterConfig *filter
|
||||
|
||||
uavcan::uint64_t CanIface::getErrorCount() const
|
||||
{
|
||||
//FIXME query SocketCAN network stack
|
||||
return 0;
|
||||
pollErrors();
|
||||
return _bus_errors_valid ? _bus_errors.errors : 0;
|
||||
}
|
||||
|
||||
uavcan::uint16_t CanIface::getNumFilters() const
|
||||
@@ -291,6 +293,130 @@ int CanIface::getFD()
|
||||
return _fd;
|
||||
}
|
||||
|
||||
int CanIface::pollErrors() const
|
||||
{
|
||||
#ifdef SIOCGCANERRORS
|
||||
|
||||
if (_fd < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ifreq ifr {};
|
||||
|
||||
snprintf(ifr.ifr_name, IFNAMSIZ, "can%" PRIu32, _index);
|
||||
|
||||
if (ioctl(_fd, SIOCGCANERRORS, &ifr) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
const struct can_ioctl_errors_s &e = ifr.ifr_ifru.ifru_can_errors;
|
||||
|
||||
_bus_errors.state = e.state;
|
||||
_bus_errors.tec = e.txerr;
|
||||
_bus_errors.rec = e.rxerr;
|
||||
_bus_errors.errors = e.errors;
|
||||
_bus_errors.rx_overruns = e.rx_overruns;
|
||||
_bus_errors_valid = true;
|
||||
return 0;
|
||||
#else
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
int CanIface::setBitRate(uint32_t bitrate)
|
||||
{
|
||||
if (_fd < 0 || bitrate == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct ifreq ifr {};
|
||||
|
||||
snprintf(ifr.ifr_name, IFNAMSIZ, "can%" PRIu32, _index);
|
||||
|
||||
const uint16_t kbps = bitrate / 1000;
|
||||
|
||||
if (ioctl(_fd, SIOCGCANBITRATE, &ifr) < 0) {
|
||||
PX4_WARN("can%" PRIu32 ": cannot read bit rate (%d), leaving it as configured", _index, errno);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (ifr.ifr_ifru.ifru_can_data.arbi_bitrate == kbps) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
#ifndef SIOCGCANERRORS
|
||||
/* Before the PX4/NuttX change that added SIOCGCANERRORS, SIOCSCANBITRATE
|
||||
* restarted a running controller from inside the driver, which on FlexCAN
|
||||
* with ECC RAM initialisation is a bus fault. Keep the configured rate.
|
||||
*/
|
||||
PX4_WARN("can%" PRIu32 ": UAVCAN_BITRATE %u kbit/s needs a newer NuttX, staying at %u kbit/s",
|
||||
_index, kbps, ifr.ifr_ifru.ifru_can_data.arbi_bitrate);
|
||||
return 0;
|
||||
#else
|
||||
/* Only the nominal rate changes; the data phase keeps the driver's
|
||||
* settings so an FD-capable controller is never told data_bitrate 0.
|
||||
* The driver applies the timing at the next ifup, so the interface is
|
||||
* taken down around the request and brought back up whatever happens.
|
||||
*/
|
||||
ifr.ifr_ifru.ifru_can_data.arbi_bitrate = kbps;
|
||||
|
||||
struct ifreq flags {};
|
||||
|
||||
snprintf(flags.ifr_name, IFNAMSIZ, "can%" PRIu32, _index);
|
||||
|
||||
if (ioctl(_fd, SIOCGIFFLAGS, &flags) < 0) {
|
||||
PX4_ERR("can%" PRIu32 ": cannot read interface flags (%d)", _index, errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const bool was_up = flags.ifr_flags & IFF_UP;
|
||||
|
||||
/* NuttX takes IFF_DOWN / IFF_UP as requests, not as a state mask */
|
||||
if (was_up) {
|
||||
flags.ifr_flags = IFF_DOWN;
|
||||
|
||||
if (ioctl(_fd, SIOCSIFFLAGS, &flags) < 0) {
|
||||
PX4_ERR("can%" PRIu32 ": cannot take the interface down (%d)", _index, errno);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
const int res = ioctl(_fd, SIOCSCANBITRATE, &ifr);
|
||||
const int set_errno = errno;
|
||||
|
||||
if (was_up) {
|
||||
flags.ifr_flags = IFF_UP;
|
||||
|
||||
if (ioctl(_fd, SIOCSIFFLAGS, &flags) < 0) {
|
||||
PX4_ERR("can%" PRIu32 ": cannot bring the interface back up (%d)", _index, errno);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (res < 0) {
|
||||
PX4_ERR("can%" PRIu32 ": %u kbit/s rejected (%d)", _index, kbps, set_errno);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
#endif
|
||||
}
|
||||
|
||||
const char *CanIface::busStateName(uint8_t state)
|
||||
{
|
||||
switch (state) {
|
||||
case BusErrors::Active: return "error-active";
|
||||
|
||||
case BusErrors::Warning: return "error-warning";
|
||||
|
||||
case BusErrors::Passive: return "error-passive";
|
||||
|
||||
case BusErrors::BusOff: return "bus-off";
|
||||
}
|
||||
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
uavcan::uint32_t CanDriver::detectBitRate(void (*idle_callback)())
|
||||
{
|
||||
//FIXME
|
||||
@@ -302,6 +428,10 @@ int CanDriver::init(uavcan::uint32_t bitrate)
|
||||
for (int i = 0; i < UAVCAN_SOCKETCAN_NUM_IFACES; i++) {
|
||||
pfds[i].fd = if_[i].getFD();
|
||||
pfds[i].events = POLLIN | POLLOUT;
|
||||
|
||||
if (if_[i].getFD() >= 0 && if_[i].setBitRate(bitrate) < 0) {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -313,13 +443,29 @@ int CanDriver::init(uavcan::uint32_t bitrate)
|
||||
|
||||
uavcan::uint32_t CanDriver::getRxQueueOverflowCount() const
|
||||
{
|
||||
//FIXME query SocketCAN network stack
|
||||
return 0;
|
||||
uint32_t total = 0;
|
||||
|
||||
for (int i = 0; i < UAVCAN_SOCKETCAN_NUM_IFACES; i++) {
|
||||
CanIface::BusErrors e;
|
||||
|
||||
if (if_[i].lastErrors(e)) {
|
||||
total += e.rx_overruns;
|
||||
}
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
|
||||
bool CanDriver::isInBusOffState() const
|
||||
{
|
||||
//FIXME no interface available yet, maybe make a NuttX ioctl
|
||||
for (int i = 0; i < UAVCAN_SOCKETCAN_NUM_IFACES; i++) {
|
||||
CanIface::BusErrors e;
|
||||
|
||||
if (if_[i].lastErrors(e) && e.state == CanIface::BusErrors::BusOff) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
@@ -1268,10 +1268,12 @@ UavcanNode::print_info()
|
||||
|
||||
printf("\n");
|
||||
|
||||
#if !defined(UAVCAN_SOCKETCAN_NUTTX)
|
||||
// See https://github.com/PX4/PX4-Autopilot/issues/22871
|
||||
printf("WARNING: CAN error counter values below may increase during this function call due to internal counter reading implementation.\n");
|
||||
printf("Do not fully trust these counters until this issue is fixed.\n");
|
||||
printf("\n");
|
||||
#endif
|
||||
|
||||
// UAVCAN node perfcounters
|
||||
printf("UAVCAN node status:\n");
|
||||
@@ -1289,6 +1291,18 @@ UavcanNode::print_info()
|
||||
auto iface = _node.getDispatcher().getCanIOManager().getCanDriver().getIface(i);
|
||||
|
||||
if (iface) {
|
||||
#if defined(UAVCAN_SOCKETCAN_NUTTX)
|
||||
UAVCAN_DRIVER::CanIface::BusErrors bus;
|
||||
|
||||
if (static_cast<UAVCAN_DRIVER::CanIface *>(iface)->lastErrors(bus)) {
|
||||
printf("\tBus state: %s (TEC %u, REC %u)\n", UAVCAN_DRIVER::CanIface::busStateName(bus.state), bus.tec, bus.rec);
|
||||
printf("\tRX overruns: %" PRIu32 "\n", bus.rx_overruns);
|
||||
|
||||
} else {
|
||||
printf("\tBus state: unavailable\n");
|
||||
}
|
||||
|
||||
#endif
|
||||
printf("\tHW errors: %" PRIu64 "\n", iface->getErrorCount());
|
||||
|
||||
auto iface_perf_cnt = _node.getDispatcher().getCanIOManager().getIfacePerfCounters(i);
|
||||
|
||||
Reference in new issue
Block a user