Skip to content

Commit caa6d63

Browse files
committed
fix(dronecan): address Copilot review comments on PR iNavFlight#11560
1 parent 70063a3 commit caa6d63

2 files changed

Lines changed: 16 additions & 6 deletions

File tree

src/main/drivers/dronecan/libcanard/canard_stm32f7xx_driver.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
/* --- Internal types and state --- */
2424

2525
#define RX_BUFFER_SIZE 32
26-
#define TX_QUEUE_SIZE 32
26+
#define TX_QUEUE_SIZE 32 // usable capacity is TX_QUEUE_SIZE - 1 (31) due to one-slot-empty scheme
2727

2828
struct Timings {
2929
uint16_t prescaler;
@@ -545,14 +545,20 @@ int16_t canardSTM32Transmit(const CanardCANFrame* const tx_frame) {
545545
return -CANARD_ERROR_INVALID_ARGUMENT;
546546
}
547547

548+
// Classic CAN only, max payload is 8 bytes
549+
if (tx_frame->data_len > 8) {
550+
return -CANARD_ERROR_INVALID_ARGUMENT;
551+
}
552+
548553
if (!canTxQueuePush(tx_frame)) {
549554
return 0; // SW queue full - caller retries next cycle
550555
}
551556

552557
// If all mailboxes are idle, RQCP will never fire to start the ISR chain.
553-
// Seed the HW via canTxDrainQueue while blocking only the CAN TX ISR (not
554-
// higher-priority IRQs like the gyro timer) to preserve the SPSC contract —
555-
// ISR is the sole consumer; we become the consumer only during this window.
558+
// Seed the HW via canTxDrainQueue while masking all interrupts at priority
559+
// >= NVIC_PRIO_CAN via BASEPRI (higher-priority IRQs like gyro timer are
560+
// unaffected) to preserve the SPSC contract — ISR is the sole consumer;
561+
// we become the consumer only during this window.
556562
ATOMIC_BLOCK(NVIC_PRIO_CAN) {
557563
if (HAL_CAN_GetTxMailboxesFreeLevel(&hcan1) > 0) {
558564
canTxDrainQueue(&hcan1);

src/main/drivers/dronecan/libcanard/canard_stm32h7xx_driver.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,8 +310,8 @@ int16_t canardSTM32Receive(CanardCANFrame *const rx_frame) {
310310
rx_frame->id |= CANARD_CAN_FRAME_RTR;
311311
}
312312

313-
rx_frame->data_len = RxHeader.DataLength;
314-
memcpy(rx_frame->data, RxData, RxHeader.DataLength);
313+
rx_frame->data_len = RxHeader.DataLength > 8 ? 8 : RxHeader.DataLength;
314+
memcpy(rx_frame->data, RxData, rx_frame->data_len);
315315

316316
// assume a single interface
317317
rx_frame->iface_id = 0;
@@ -340,6 +340,10 @@ int16_t canardSTM32Transmit(const CanardCANFrame* const tx_frame) {
340340
return -CANARD_ERROR_INVALID_ARGUMENT; // unsupported frame format
341341
}
342342

343+
if (tx_frame->data_len > 8) {
344+
return -CANARD_ERROR_INVALID_ARGUMENT; // classic CAN only, max 8 bytes
345+
}
346+
343347
FDCAN_TxHeaderTypeDef TxHeader;
344348
uint8_t TxData[8];
345349

0 commit comments

Comments
 (0)