1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087 |
- #include "AP_HAL_ChibiOS.h"
- #if HAL_WITH_UAVCAN
- #include <cassert>
- #include <cstring>
- #include "CANClock.h"
- #include "CANInternal.h"
- #include "CANSerialRouter.h"
- #include <AP_UAVCAN/AP_UAVCAN_SLCAN.h>
- #include <AP_Math/AP_Math.h>
- # include <hal.h>
- # if defined(STM32H7XX)
- #include "CANFDIface.h"
- #define FDCAN1_IT0_IRQHandler STM32_FDCAN1_IT0_HANDLER
- #define FDCAN1_IT1_IRQHandler STM32_FDCAN1_IT1_HANDLER
- #define FDCAN2_IT0_IRQHandler STM32_FDCAN2_IT0_HANDLER
- #define FDCAN2_IT1_IRQHandler STM32_FDCAN2_IT1_HANDLER
- #define FDCAN_FRAME_BUFFER_SIZE 4
- #define MAX_FILTER_LIST_SIZE 80U
- #define FDCAN_NUM_RXFIFO0_SIZE 104U
- #define FDCAN_TX_FIFO_BUFFER_SIZE 128U
- #define MESSAGE_RAM_END_ADDR 0x4000B5FC
- extern const AP_HAL::HAL& hal;
- namespace ChibiOS_CAN
- {
- namespace
- {
- CanIface* ifaces[UAVCAN_STM32_NUM_IFACES] = {
- UAVCAN_NULLPTR
- #if UAVCAN_STM32_NUM_IFACES > 1
- , UAVCAN_NULLPTR
- #endif
- };
- inline void handleInterrupt(uavcan::uint8_t iface_index, uavcan::uint8_t line_index)
- {
- UAVCAN_ASSERT(iface_index < UAVCAN_STM32_NUM_IFACES);
- if (ifaces[iface_index] == UAVCAN_NULLPTR) {
-
- ifaces[iface_index]->can_reg()->IR = FDCAN_IR_RF0N;
- ifaces[iface_index]->can_reg()->IR = FDCAN_IR_RF1N;
- ifaces[iface_index]->can_reg()->IR = FDCAN_IR_TEFN;
- UAVCAN_ASSERT(0);
- return;
- }
- if (line_index == 0) {
- if ((ifaces[iface_index]->can_reg()->IR & FDCAN_IR_RF0N) ||
- (ifaces[iface_index]->can_reg()->IR & FDCAN_IR_RF0F)) {
- ifaces[iface_index]->can_reg()->IR = FDCAN_IR_RF0N | FDCAN_IR_RF0F;
- ifaces[iface_index]->handleRxInterrupt(0);
- }
- if ((ifaces[iface_index]->can_reg()->IR & FDCAN_IR_RF1N) ||
- (ifaces[iface_index]->can_reg()->IR & FDCAN_IR_RF1F)) {
- ifaces[iface_index]->can_reg()->IR = FDCAN_IR_RF1N | FDCAN_IR_RF1F;
- ifaces[iface_index]->handleRxInterrupt(1);
- }
- } else {
- if (ifaces[iface_index]->can_reg()->IR & FDCAN_IR_TC) {
- ifaces[iface_index]->can_reg()->IR = FDCAN_IR_TC;
- uavcan::uint64_t utc_usec = clock::getUtcUSecFromCanInterrupt();
- if (utc_usec > 0) {
- utc_usec--;
- }
- ifaces[iface_index]->handleTxCompleteInterrupt(utc_usec);
- }
- }
- ifaces[iface_index]->pollErrorFlagsFromISR();
- }
- }
- uint32_t CanIface::FDCANMessageRAMOffset_ = 0;
- #if AP_UAVCAN_SLCAN_ENABLED
- SLCANRouter CanIface::_slcan_router;
- #endif
- CanIface::CanIface(fdcan::CanType* can, BusEvent& update_event, uavcan::uint8_t self_index,
- CanRxItem* rx_queue_buffer, uavcan::uint8_t rx_queue_capacity)
- : rx_queue_(rx_queue_buffer, rx_queue_capacity)
- , can_(can)
- , error_cnt_(0)
- , served_aborts_cnt_(0)
- , update_event_(update_event)
- , peak_tx_mailbox_index_(0)
- , self_index_(self_index)
- , had_activity_(false)
- {
- UAVCAN_ASSERT(self_index_ < UAVCAN_STM32_NUM_IFACES);
- }
- void CanIface::RxQueue::registerOverflow()
- {
- if (overflow_cnt_ < 0xFFFFFFFF) {
- overflow_cnt_++;
- }
- }
- void CanIface::RxQueue::push(const uavcan::CanFrame& frame, const uint64_t& utc_usec, uavcan::CanIOFlags flags)
- {
- buf_[in_].frame = frame;
- buf_[in_].utc_usec = utc_usec;
- buf_[in_].flags = flags;
- in_++;
- if (in_ >= capacity_) {
- in_ = 0;
- }
- len_++;
- if (len_ > capacity_) {
- len_ = capacity_;
- registerOverflow();
- out_++;
- if (out_ >= capacity_) {
- out_ = 0;
- }
- }
- }
- void CanIface::RxQueue::pop(uavcan::CanFrame& out_frame, uavcan::uint64_t& out_utc_usec, uavcan::CanIOFlags& out_flags)
- {
- if (len_ > 0) {
- out_frame = buf_[out_].frame;
- out_utc_usec = buf_[out_].utc_usec;
- out_flags = buf_[out_].flags;
- out_++;
- if (out_ >= capacity_) {
- out_ = 0;
- }
- len_--;
- } else {
- UAVCAN_ASSERT(0);
- }
- }
- void CanIface::RxQueue::reset()
- {
- in_ = 0;
- out_ = 0;
- len_ = 0;
- overflow_cnt_ = 0;
- }
- int CanIface::computeTimings(const uavcan::uint32_t target_bitrate, Timings& out_timings)
- {
- if (target_bitrate < 1) {
- return -ErrInvalidBitRate;
- }
-
- const uavcan::uint32_t pclk = STM32_PLL1_Q_CK;
- static const int MaxBS1 = 16;
- static const int MaxBS2 = 8;
-
- const int max_quanta_per_bit = (target_bitrate >= 1000000) ? 10 : 17;
- UAVCAN_ASSERT(max_quanta_per_bit <= (MaxBS1 + MaxBS2));
- static const int MaxSamplePointLocation = 900;
-
- const uavcan::uint32_t prescaler_bs = pclk / target_bitrate;
-
- uavcan::uint8_t bs1_bs2_sum = uavcan::uint8_t(max_quanta_per_bit - 1);
- while ((prescaler_bs % (1 + bs1_bs2_sum)) != 0) {
- if (bs1_bs2_sum <= 2) {
- return -ErrInvalidBitRate;
- }
- bs1_bs2_sum--;
- }
- const uavcan::uint32_t prescaler = prescaler_bs / (1 + bs1_bs2_sum);
- if ((prescaler < 1U) || (prescaler > 1024U)) {
- return -ErrInvalidBitRate;
- }
-
- struct BsPair {
- uavcan::uint8_t bs1;
- uavcan::uint8_t bs2;
- uavcan::uint16_t sample_point_permill;
- BsPair() :
- bs1(0),
- bs2(0),
- sample_point_permill(0)
- { }
- BsPair(uavcan::uint8_t bs1_bs2_sum, uavcan::uint8_t arg_bs1) :
- bs1(arg_bs1),
- bs2(uavcan::uint8_t(bs1_bs2_sum - bs1)),
- sample_point_permill(uavcan::uint16_t(1000 * (1 + bs1) / (1 + bs1 + bs2)))
- {
- UAVCAN_ASSERT(bs1_bs2_sum > arg_bs1);
- }
- bool isValid() const
- {
- return (bs1 >= 1) && (bs1 <= MaxBS1) && (bs2 >= 1) && (bs2 <= MaxBS2);
- }
- };
-
- BsPair solution(bs1_bs2_sum, uavcan::uint8_t(((7 * bs1_bs2_sum - 1) + 4) / 8));
- if (solution.sample_point_permill > MaxSamplePointLocation) {
-
- solution = BsPair(bs1_bs2_sum, uavcan::uint8_t((7 * bs1_bs2_sum - 1) / 8));
- }
-
- if ((target_bitrate != (pclk / (prescaler * (1 + solution.bs1 + solution.bs2)))) || !solution.isValid()) {
- UAVCAN_ASSERT(0);
- return -ErrLogic;
- }
- UAVCAN_STM32_LOG("Timings: quanta/bit: %d, sample point location: %.1f%%",
- int(1 + solution.bs1 + solution.bs2), float(solution.sample_point_permill) / 10.F);
- out_timings.prescaler = uavcan::uint16_t(prescaler - 1U);
- out_timings.sjw = 0;
- out_timings.bs1 = uavcan::uint8_t(solution.bs1 - 1);
- out_timings.bs2 = uavcan::uint8_t(solution.bs2 - 1);
- return 0;
- }
- uavcan::int16_t CanIface::send(const uavcan::CanFrame& frame, uavcan::MonotonicTime tx_deadline,
- uavcan::CanIOFlags flags)
- {
- if (frame.isErrorFrame() || frame.dlc > 8) {
- return -ErrUnsupportedFrame;
- }
-
- CriticalSectionLocker lock;
-
- uavcan::uint8_t index;
- if ((can_->TXFQS & FDCAN_TXFQS_TFQF) != 0) {
- return false;
- }
- index = ((can_->TXFQS & FDCAN_TXFQS_TFQPI) >> FDCAN_TXFQS_TFQPI_Pos);
-
-
- uint32_t* buffer = (uint32_t *)(MessageRam_.TxFIFOQSA + (index * FDCAN_FRAME_BUFFER_SIZE * 4));
-
- if (frame.isExtended2()) {
- buffer[0] = (fdcan::IDE | frame.id);
- } else {
- buffer[0] = (frame.id << 18);
- }
- if (frame.isRemoteTransmissionRequest()) {
- buffer[0] |= fdcan::RTR;
- }
-
- buffer[1] = frame.dlc << 16 | index << 24;
-
- buffer[2] = (uavcan::uint32_t(frame.data[3]) << 24) |
- (uavcan::uint32_t(frame.data[2]) << 16) |
- (uavcan::uint32_t(frame.data[1]) << 8) |
- (uavcan::uint32_t(frame.data[0]) << 0);
- buffer[3] = (uavcan::uint32_t(frame.data[7]) << 24) |
- (uavcan::uint32_t(frame.data[6]) << 16) |
- (uavcan::uint32_t(frame.data[5]) << 8) |
- (uavcan::uint32_t(frame.data[4]) << 0);
-
- can_->TXBAR = (1 << index);
-
- pending_tx_[index].deadline = tx_deadline;
- pending_tx_[index].frame = frame;
- pending_tx_[index].loopback = (flags & uavcan::CanIOFlagLoopback) != 0;
- pending_tx_[index].abort_on_error = (flags & uavcan::CanIOFlagAbortOnError) != 0;
- pending_tx_[index].index = index;
- return 1;
- }
- uavcan::int16_t CanIface::receive(uavcan::CanFrame& out_frame, uavcan::MonotonicTime& out_ts_monotonic,
- uavcan::UtcTime& out_ts_utc, uavcan::CanIOFlags& out_flags)
- {
- out_ts_monotonic = clock::getMonotonic();
- uavcan::uint64_t utc_usec = 0;
- {
- CriticalSectionLocker lock;
- if (rx_queue_.getLength() == 0) {
- return 0;
- }
- rx_queue_.pop(out_frame, utc_usec, out_flags);
- }
- out_ts_utc = uavcan::UtcTime::fromUSec(utc_usec);
- return 1;
- }
- uavcan::int16_t CanIface::configureFilters(const uavcan::CanFilterConfig* filter_configs,
- uavcan::uint16_t num_configs)
- {
- uint32_t num_extid = 0, num_stdid = 0;
- uint32_t total_available_list_size = MAX_FILTER_LIST_SIZE;
- uint32_t* filter_ptr;
-
- for (uint8_t i = 0; i < num_configs; i++) {
- const uavcan::CanFilterConfig* const cfg = filter_configs + i;
- if ((cfg->id & uavcan::CanFrame::FlagEFF) || !(cfg->mask & uavcan::CanFrame::FlagEFF)) {
- num_extid++;
- } else {
- num_stdid++;
- }
- }
- CriticalSectionLocker lock;
- can_->CCCR |= FDCAN_CCCR_INIT;
- while ((can_->CCCR & FDCAN_CCCR_INIT) == 0) {}
- can_->CCCR |= FDCAN_CCCR_CCE;
-
- if (num_stdid == 0) {
- can_->GFC |= 0x2;
- } else if ((num_stdid < total_available_list_size) && (num_stdid <= 128)) {
- can_->SIDFC = (FDCANMessageRAMOffset_ << 2) | (num_stdid << 16);
- MessageRam_.StandardFilterSA = SRAMCAN_BASE + (FDCANMessageRAMOffset_ * 4U);
- FDCANMessageRAMOffset_ += num_stdid;
- total_available_list_size -= num_stdid;
- can_->GFC |= (0x3U << 4);
- } else {
- can_->CCCR &= ~FDCAN_CCCR_INIT;
- return -ErrFilterNumConfigs;
- }
- if (num_stdid) {
- num_stdid = 0;
- filter_ptr = (uint32_t*)MessageRam_.StandardFilterSA;
-
- for (uint8_t i = 0; i < num_configs; i++) {
- uint32_t id = 0;
- uint32_t mask = 0;
- const uavcan::CanFilterConfig* const cfg = filter_configs + i;
- if (!((cfg->id & uavcan::CanFrame::FlagEFF) || !(cfg->mask & uavcan::CanFrame::FlagEFF))) {
- id = (cfg->id & uavcan::CanFrame::MaskStdID);
- mask = (cfg->mask & 0x7F);
- filter_ptr[num_stdid] = 0x2U << 30 |
- 0x1U << 27 |
- id << 16 |
- mask;
- num_stdid++;
- }
- }
- }
-
- if (num_extid == 0) {
- can_->GFC |= 0x1;
- } else if ((num_extid < (total_available_list_size/2)) && (num_extid <= 64)) {
- can_->XIDFC = (FDCANMessageRAMOffset_ << 2) | (num_extid << 16);
- MessageRam_.ExtendedFilterSA = SRAMCAN_BASE + (FDCANMessageRAMOffset_ * 4U);
- FDCANMessageRAMOffset_ += num_extid*2;
- can_->GFC = (0x3U << 2);
- } else {
- can_->CCCR &= ~FDCAN_CCCR_INIT;
- return -ErrFilterNumConfigs;
- }
- if (num_extid) {
- num_extid = 0;
- filter_ptr = (uint32_t*)MessageRam_.ExtendedFilterSA;
-
- for (uint8_t i = 0; i < num_configs; i++) {
- uint32_t id = 0;
- uint32_t mask = 0;
- const uavcan::CanFilterConfig* const cfg = filter_configs + i;
- if ((cfg->id & uavcan::CanFrame::FlagEFF) || !(cfg->mask & uavcan::CanFrame::FlagEFF)) {
- id = (cfg->id & uavcan::CanFrame::MaskExtID);
- mask = (cfg->mask & uavcan::CanFrame::MaskExtID);
- filter_ptr[num_extid*2] = 0x1U << 29 | id;
- filter_ptr[num_extid*2 + 1] = 0x2U << 30 | mask;
- num_extid++;
- }
- }
- }
- MessageRam_.EndAddress = SRAMCAN_BASE + (FDCANMessageRAMOffset_ * 4U);
- if (MessageRam_.EndAddress > MESSAGE_RAM_END_ADDR) {
-
- AP_HAL::panic("CANFDIface: Message RAM Overflow!");
- }
- can_->CCCR &= ~FDCAN_CCCR_INIT;
- return 0;
- }
- uavcan::uint16_t CanIface::getNumFilters() const
- {
- return MAX_FILTER_LIST_SIZE;
- }
- int CanIface::init(const uavcan::uint32_t bitrate, const OperatingMode mode)
- {
-
- {
- CriticalSectionLocker lock;
- can_->CCCR &= ~FDCAN_CCCR_CSR;
- while ((can_->CCCR & FDCAN_CCCR_CSA) == FDCAN_CCCR_CSA) {}
- can_->CCCR |= FDCAN_CCCR_INIT;
- while ((can_->CCCR & FDCAN_CCCR_INIT) == 0) {}
- can_->CCCR |= FDCAN_CCCR_CCE;
- can_->IE = 0;
- }
-
- rx_queue_.reset();
- error_cnt_ = 0;
- served_aborts_cnt_ = 0;
- uavcan::fill_n(pending_tx_, NumTxMailboxes, TxItem());
- peak_tx_mailbox_index_ = 0;
- had_activity_ = false;
-
- Timings timings;
- const int timings_res = computeTimings(bitrate, timings);
- if (timings_res < 0) {
- can_->CCCR &= ~FDCAN_CCCR_INIT;
- return timings_res;
- }
- UAVCAN_STM32_LOG("Timings: presc=%u sjw=%u bs1=%u bs2=%u",
- unsigned(timings.prescaler), unsigned(timings.sjw), unsigned(timings.bs1), unsigned(timings.bs2));
-
-
- can_->NBTP = ((timings.sjw << FDCAN_NBTP_NSJW_Pos) |
- (timings.bs1 << FDCAN_NBTP_NTSEG1_Pos) |
- (timings.bs2 << FDCAN_NBTP_TSEG2_Pos) |
- (timings.prescaler << FDCAN_NBTP_NBRP_Pos));
-
- can_->RXESC = 0;
-
- setupMessageRam();
-
- can_->IR = 0x3FFFFFFF;
-
- can_->IE = FDCAN_IE_TCE |
- FDCAN_IE_RF0NE |
- FDCAN_IE_RF0FE |
- FDCAN_IE_RF1NE |
- FDCAN_IE_RF1FE;
- can_->ILS = FDCAN_ILS_TCL;
- can_->TXBTIE = 0xFFFFFFFF;
- can_->ILE = 0x3;
-
- can_->CCCR &= ~FDCAN_CCCR_INIT;
- return 0;
- }
- void CanIface::setupMessageRam()
- {
- uint32_t num_elements = 0;
-
- num_elements = MIN((FDCAN_NUM_RXFIFO0_SIZE/FDCAN_FRAME_BUFFER_SIZE), 64U);
- if (num_elements) {
- can_->RXF0C = (FDCANMessageRAMOffset_ << 2) | (num_elements << 16);
- MessageRam_.RxFIFO0SA = SRAMCAN_BASE + (FDCANMessageRAMOffset_ * 4U);
- FDCANMessageRAMOffset_ += num_elements*FDCAN_FRAME_BUFFER_SIZE;
- }
-
- num_elements = MIN((FDCAN_TX_FIFO_BUFFER_SIZE/FDCAN_FRAME_BUFFER_SIZE), 32U);
- if (num_elements) {
- can_->TXBC = (FDCANMessageRAMOffset_ << 2) | (num_elements << 24);
- can_->TXBC |= 1U << 30;
- MessageRam_.TxFIFOQSA = SRAMCAN_BASE + (FDCANMessageRAMOffset_ * 4U);
- FDCANMessageRAMOffset_ += num_elements*FDCAN_FRAME_BUFFER_SIZE;
- }
- MessageRam_.EndAddress = SRAMCAN_BASE + (FDCANMessageRAMOffset_ * 4U);
- if (MessageRam_.EndAddress > MESSAGE_RAM_END_ADDR) {
-
- AP_HAL::panic("CANFDIface: Message RAM Overflow!");
- return;
- }
- }
- void CanIface::handleTxCompleteInterrupt(const uavcan::uint64_t utc_usec)
- {
- for (uint8_t i = 0; i < NumTxMailboxes; i++) {
- if ((can_->TXBTO & (1UL << i))) {
- if (pending_tx_[i].loopback && had_activity_) {
- rx_queue_.push(pending_tx_[i].frame, utc_usec, uavcan::CanIOFlagLoopback);
- }
- }
- }
- }
- bool CanIface::readRxFIFO(uavcan::uint8_t fifo_index)
- {
- UAVCAN_ASSERT(fifo_index < 2);
- uint32_t *frame_ptr;
- uint32_t index;
- uavcan::uint64_t utc_usec = clock::getUtcUSecFromCanInterrupt();
- if (fifo_index == 0) {
-
- if ((can_->RXF0C & FDCAN_RXF0C_F0S) == 0) {
- UAVCAN_ASSERT(0);
- return false;
- }
-
- if ((can_->RXF0S & FDCAN_RXF0S_RF0L) != 0) {
- error_cnt_++;
- }
- if ((can_->RXF0S & FDCAN_RXF0S_F0FL) == 0) {
- return false;
- } else {
- index = ((can_->RXF0S & FDCAN_RXF0S_F0GI) >> 8);
- frame_ptr = (uint32_t *)(MessageRam_.RxFIFO0SA + (index * FDCAN_FRAME_BUFFER_SIZE * 4));
- }
- } else if (fifo_index == 1) {
-
- if ((can_->RXF1C & FDCAN_RXF1C_F1S) == 0) {
- UAVCAN_ASSERT(0);
- return false;
- }
-
- if ((can_->RXF1S & FDCAN_RXF1S_RF1L) != 0) {
- error_cnt_++;
- }
- if ((can_->RXF1S & FDCAN_RXF1S_F1FL) == 0) {
- return false;
- } else {
- index = ((can_->RXF1S & FDCAN_RXF1S_F1GI) >> 8);
- frame_ptr = (uint32_t *)(MessageRam_.RxFIFO1SA + (index * FDCAN_FRAME_BUFFER_SIZE * 4));
- }
- } else {
- return false;
- }
-
- uavcan::CanFrame frame;
- uint32_t id = frame_ptr[0];
- if ((id & fdcan::IDE) == 0) {
-
- frame.id = ((id & fdcan::STID_MASK) >> 18) & uavcan::CanFrame::MaskStdID;
- } else {
-
- frame.id = (id & fdcan::EXID_MASK) & uavcan::CanFrame::MaskExtID;
- frame.id |= uavcan::CanFrame::FlagEFF;
- }
- if ((id & fdcan::RTR) != 0) {
- frame.id |= uavcan::CanFrame::FlagRTR;
- }
- frame.dlc = (frame_ptr[1] & fdcan::DLC_MASK) >> 16;
- uint8_t *data = (uint8_t*)&frame_ptr[2];
-
- for (uint8_t i = 0; i < 8; i++) {
- frame.data[i] = data[i];
- }
-
- if (fifo_index == 0) {
- can_->RXF0A = index;
- } else if (fifo_index == 1) {
- can_->RXF1A = index;
- }
-
- rx_queue_.push(frame, utc_usec, 0);
- #if AP_UAVCAN_SLCAN_ENABLED
- _slcan_router.route_frame_to_slcan(this, frame, utc_usec);
- #endif
- return true;
- }
- void CanIface::handleRxInterrupt(uavcan::uint8_t fifo_index)
- {
- while (readRxFIFO(fifo_index)) {
- had_activity_ = true;
- }
- update_event_.signalFromInterrupt();
- }
- void CanIface::pollErrorFlagsFromISR()
- {
- const uavcan::uint8_t cel = can_->ECR >> 16;
- if (cel != 0) {
- for (int i = 0; i < NumTxMailboxes; i++) {
- if (!pending_tx_[i].abort_on_error) {
- continue;
- }
- if (((1 << pending_tx_[i].index) & can_->TXBRP)) {
- can_->TXBCR = 1 << pending_tx_[i].index;
- error_cnt_++;
- served_aborts_cnt_++;
- }
- }
- }
- }
- void CanIface::discardTimedOutTxMailboxes(uavcan::MonotonicTime current_time)
- {
- CriticalSectionLocker lock;
- for (int i = 0; i < NumTxMailboxes; i++) {
- if (((1 << pending_tx_[i].index) & can_->TXBRP) && pending_tx_[i].deadline < current_time) {
- can_->TXBCR = 1 << pending_tx_[i].index;
- error_cnt_++;
- }
- }
- }
- bool CanIface::canAcceptNewTxFrame(const uavcan::CanFrame& frame) const
- {
-
- if ((can_->TXBC & FDCAN_TXBC_TFQS) == 0) {
- return false;
- }
- if ((can_->TXFQS & FDCAN_TXFQS_TFQF) != 0) {
- return false;
- }
- return true;
- }
- bool CanIface::isRxBufferEmpty() const
- {
- CriticalSectionLocker lock;
- return rx_queue_.getLength() == 0;
- }
- uavcan::uint64_t CanIface::getErrorCount() const
- {
- CriticalSectionLocker lock;
- return error_cnt_ + rx_queue_.getOverflowCount();
- }
- unsigned CanIface::getRxQueueLength() const
- {
- CriticalSectionLocker lock;
- return rx_queue_.getLength();
- }
- bool CanIface::hadActivity()
- {
- CriticalSectionLocker lock;
- const bool ret = had_activity_;
- had_activity_ = false;
- return ret;
- }
- uavcan::CanSelectMasks CanDriver::makeSelectMasks(const uavcan::CanFrame* (& pending_tx)[uavcan::MaxCanIfaces]) const
- {
- uavcan::CanSelectMasks msk;
- for (uavcan::uint8_t i = 0; i < num_ifaces_; i++) {
- CanIface* iface = ifaces[if_int_to_gl_index_[i]];
- msk.read |= (iface->isRxBufferEmpty() ? 0 : 1) << i;
- if (pending_tx[i] != UAVCAN_NULLPTR) {
- msk.write |= (iface->canAcceptNewTxFrame(*pending_tx[i]) ? 1 : 0) << i;
- }
- }
- return msk;
- }
- bool CanDriver::hasReadableInterfaces() const
- {
- for (uavcan::uint8_t i = 0; i < num_ifaces_; i++) {
- if (!ifaces[if_int_to_gl_index_[i]]->isRxBufferEmpty()) {
- return true;
- }
- }
- return false;
- }
- uavcan::int16_t CanDriver::select(uavcan::CanSelectMasks& inout_masks,
- const uavcan::CanFrame* (& pending_tx)[uavcan::MaxCanIfaces],
- const uavcan::MonotonicTime blocking_deadline)
- {
- const uavcan::CanSelectMasks in_masks = inout_masks;
- const uavcan::MonotonicTime time = clock::getMonotonic();
- for (uavcan::uint8_t i = 0; i < num_ifaces_; i++) {
- CanIface* iface = ifaces[if_int_to_gl_index_[i]];
- iface->discardTimedOutTxMailboxes(time);
- {
- CriticalSectionLocker cs_locker;
- iface->pollErrorFlagsFromISR();
- }
- }
- inout_masks = makeSelectMasks(pending_tx);
- if ((inout_masks.read & in_masks.read) != 0 ||
- (inout_masks.write & in_masks.write) != 0) {
- return 1;
- }
- (void)update_event_.wait(blocking_deadline - time);
- inout_masks = makeSelectMasks(pending_tx);
- return 1;
- }
- void CanDriver::initOnce()
- {
- {
- CriticalSectionLocker lock;
- RCC->APB1HRSTR |= RCC_APB1HRSTR_FDCANRST;
- RCC->APB1HRSTR &= ~RCC_APB1HRSTR_FDCANRST;
- RCC->APB1HENR |= RCC_APB1HENR_FDCANEN;
- }
-
- {
- CriticalSectionLocker lock;
- nvicEnableVector(FDCAN1_IT0_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- nvicEnableVector(FDCAN1_IT1_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- # if UAVCAN_STM32_NUM_IFACES > 1
- nvicEnableVector(FDCAN2_IT0_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- nvicEnableVector(FDCAN2_IT1_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- # endif
- }
- }
- int CanDriver::init(const uavcan::uint32_t bitrate, const CanIface::OperatingMode mode)
- {
- int res = 0;
- UAVCAN_STM32_LOG("Bitrate %lu mode %d", static_cast<unsigned long>(bitrate), static_cast<int>(mode));
- static bool initialized_once = false;
- if (!initialized_once) {
- initialized_once = true;
- UAVCAN_STM32_LOG("First initialization");
- initOnce();
- }
-
- UAVCAN_STM32_LOG("Initing iface 0...");
- ifaces[0] = &if0_;
- res = if0_.init(bitrate, mode);
- if (res < 0) {
- UAVCAN_STM32_LOG("Iface 0 init failed %i", res);
- ifaces[0] = UAVCAN_NULLPTR;
- goto fail;
- }
-
- #if UAVCAN_STM32_NUM_IFACES > 1
- UAVCAN_STM32_LOG("Initing iface 1...");
- ifaces[1] = &if1_;
- res = if1_.init(bitrate, mode);
- if (res < 0) {
- UAVCAN_STM32_LOG("Iface 1 init failed %i", res);
- ifaces[1] = UAVCAN_NULLPTR;
- goto fail;
- }
- #endif
- UAVCAN_STM32_LOG("CAN drv init OK");
- UAVCAN_ASSERT(res >= 0);
- return res;
- fail:
- UAVCAN_STM32_LOG("CAN drv init failed %i", res);
- UAVCAN_ASSERT(res < 0);
- return res;
- }
- bool CanDriver::clock_init_ = false;
- void CanDriver::initOnce(uavcan::uint8_t can_number, bool enable_irqs)
- {
-
-
- if (!clock_init_) {
- CriticalSectionLocker lock;
- RCC->APB1HENR |= RCC_APB1HENR_FDCANEN;
- RCC->APB1HRSTR |= RCC_APB1HRSTR_FDCANRST;
- RCC->APB1HRSTR &= ~RCC_APB1HRSTR_FDCANRST;
- clock_init_ = true;
- }
- if (!enable_irqs) {
- return;
- }
-
- {
- CriticalSectionLocker lock;
- if (can_number == 0) {
- nvicEnableVector(FDCAN1_IT0_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- nvicEnableVector(FDCAN1_IT1_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- }
- # if UAVCAN_STM32_NUM_IFACES > 1
- else if (can_number == 1) {
- nvicEnableVector(FDCAN2_IT0_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- nvicEnableVector(FDCAN2_IT1_IRQn, UAVCAN_STM32_IRQ_PRIORITY_MASK);
- }
- # endif
- }
- }
- int CanDriver::init(const uavcan::uint32_t bitrate, const CanIface::OperatingMode mode, uavcan::uint8_t can_number)
- {
- int res = 0;
- UAVCAN_STM32_LOG("Bitrate %lu mode %d", static_cast<unsigned long>(bitrate), static_cast<int>(mode));
- if (can_number > UAVCAN_STM32_NUM_IFACES) {
- res = -1;
- goto fail;
- }
- static bool initialized_once[UAVCAN_STM32_NUM_IFACES] = {false};
- if (!initialized_once[can_number]) {
- initialized_once[can_number] = true;
- initialized_by_me_[can_number] = true;
- if (can_number == 1 && !initialized_once[0]) {
- UAVCAN_STM32_LOG("Iface 0 is not initialized yet but we need it for Iface 1, trying to init it");
- UAVCAN_STM32_LOG("Enabling CAN iface 0");
- initOnce(0, false);
- UAVCAN_STM32_LOG("Initing iface 0...");
- res = if0_.init(bitrate, mode);
- if (res < 0) {
- UAVCAN_STM32_LOG("Iface 0 init failed %i", res);
- goto fail;
- }
- }
- UAVCAN_STM32_LOG("Enabling CAN iface %d", can_number);
- initOnce(can_number, true);
- } else if (!initialized_by_me_[can_number]) {
- UAVCAN_STM32_LOG("CAN iface %d initialized in another CANDriver!", can_number);
- res = -2;
- goto fail;
- }
- if (can_number == 0) {
-
- UAVCAN_STM32_LOG("Initing iface 0...");
- ifaces[0] = &if0_;
- res = if0_.init(bitrate, mode);
- if (res < 0) {
- UAVCAN_STM32_LOG("Iface 0 init failed %i", res);
- ifaces[0] = UAVCAN_NULLPTR;
- goto fail;
- }
- } else if (can_number == 1) {
-
- #if UAVCAN_STM32_NUM_IFACES > 1
- UAVCAN_STM32_LOG("Initing iface 1...");
- ifaces[1] = &if1_;
- res = if1_.init(bitrate, mode);
- if (res < 0) {
- UAVCAN_STM32_LOG("Iface 1 init failed %i", res);
- ifaces[1] = UAVCAN_NULLPTR;
- goto fail;
- }
- #endif
- }
- if_int_to_gl_index_[num_ifaces_++] = can_number;
- UAVCAN_STM32_LOG("CAN drv init OK");
- UAVCAN_ASSERT(res >= 0);
- return res;
- fail:
- UAVCAN_STM32_LOG("CAN drv init failed %i", res);
- UAVCAN_ASSERT(res < 0);
- return res;
- }
- CanIface* CanDriver::getIface(uavcan::uint8_t iface_index)
- {
- if (iface_index < num_ifaces_) {
- return ifaces[if_int_to_gl_index_[iface_index]];
- }
- return UAVCAN_NULLPTR;
- }
- bool CanDriver::hadActivity()
- {
- for (uavcan::uint8_t i = 0; i < num_ifaces_; i++) {
- if (ifaces[if_int_to_gl_index_[i]]->hadActivity()) {
- return true;
- }
- }
- return false;
- }
- }
- extern "C"
- {
- UAVCAN_STM32_IRQ_HANDLER(FDCAN1_IT0_IRQHandler);
- UAVCAN_STM32_IRQ_HANDLER(FDCAN1_IT0_IRQHandler)
- {
- UAVCAN_STM32_IRQ_PROLOGUE();
- ChibiOS_CAN::handleInterrupt(0, 0);
- UAVCAN_STM32_IRQ_EPILOGUE();
- }
- UAVCAN_STM32_IRQ_HANDLER(FDCAN1_IT1_IRQHandler);
- UAVCAN_STM32_IRQ_HANDLER(FDCAN1_IT1_IRQHandler)
- {
- UAVCAN_STM32_IRQ_PROLOGUE();
- ChibiOS_CAN::handleInterrupt(0, 1);
- UAVCAN_STM32_IRQ_EPILOGUE();
- }
- # if UAVCAN_STM32_NUM_IFACES > 1
- UAVCAN_STM32_IRQ_HANDLER(FDCAN2_IT0_IRQHandler);
- UAVCAN_STM32_IRQ_HANDLER(FDCAN2_IT0_IRQHandler)
- {
- UAVCAN_STM32_IRQ_PROLOGUE();
- ChibiOS_CAN::handleInterrupt(1, 0);
- UAVCAN_STM32_IRQ_EPILOGUE();
- }
- UAVCAN_STM32_IRQ_HANDLER(FDCAN2_IT1_IRQHandler);
- UAVCAN_STM32_IRQ_HANDLER(FDCAN2_IT1_IRQHandler)
- {
- UAVCAN_STM32_IRQ_PROLOGUE();
- ChibiOS_CAN::handleInterrupt(1, 1);
- UAVCAN_STM32_IRQ_EPILOGUE();
- }
- # endif
- }
- #endif
- #endif
|