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

FDCAN support for STM32H7 #38

Closed
wants to merge 5 commits 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 25 additions & 2 deletions os/hal/ports/STM32/LLD/FDCANv1/hal_can_lld.c
Expand Up @@ -134,7 +134,7 @@ static bool fdcan_clock_stop(CANDriver *canp) {
canp->fdcan->CCCR |= FDCAN_CCCR_CSR;
start = osalOsGetSystemTimeX();
end = osalTimeAddX(start, TIME_MS2I(TIMEOUT_INIT_MS));
while ((canp->fdcan->CCCR & FDCAN_CCCR_CSA) != 0U) {
while ((canp->fdcan->CCCR & FDCAN_CCCR_CSA) == 0U) {
Copy link
Author

Choose a reason for hiding this comment

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

This was a bug - the datasheet states that the CSA bit will be set when the device is ready for clock stop. Thus, we should spin while it's zero.

if (!osalTimeIsInRangeX(osalOsGetSystemTimeX(), start, end)) {
return true;
}
Expand Down Expand Up @@ -276,15 +276,22 @@ bool can_lld_start(CANDriver *canp) {
}

/* Configuration can be performed now.*/
canp->fdcan->CCCR = FDCAN_CCCR_CCE;
canp->fdcan->CCCR |= FDCAN_CCCR_CCE;
canp->fdcan->CCCR &= ~(FDCAN_CCCR_CSR | FDCAN_CCCR_CSA);
Copy link
Author

Choose a reason for hiding this comment

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

another bug - this meant that any bits passed in to the TEST or CCCR registers didn't get applied because this line was previously clearing the INIT flag that allows writing those registers.


/* Setting up operation mode except driver-controlled bits.*/
canp->fdcan->NBTP = canp->config->NBTP;
canp->fdcan->DBTP = canp->config->DBTP;
canp->fdcan->CCCR |= canp->config->CCCR & ~(FDCAN_CCCR_CSR | FDCAN_CCCR_CSA |
FDCAN_CCCR_CCE | FDCAN_CCCR_INIT);
canp->fdcan->TEST = canp->config->TEST;
#ifdef STM32G4XX
canp->fdcan->RXGFC = canp->config->RXGFC;
#elif defined(STM32H7XX)
canp->fdcan->GFC = canp->config->RXGFC;
Copy link
Author

Choose a reason for hiding this comment

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

this register changed name for no apparent reason?

Copy link
Author

Choose a reason for hiding this comment

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

I'm also not sure if this is the ChibiOS standard way to do something like this - might make more sense to put it in the registry instead.

#else
#error "Unsupported STM32 for FDCAN LLD driver"
#endif

/* Enabling interrupts, only using interrupt zero.*/
canp->fdcan->IR = (uint32_t)-1;
Expand All @@ -294,6 +301,22 @@ bool can_lld_start(CANDriver *canp) {
canp->fdcan->TXBTIE = FDCAN_TXBTIE_TIE;
canp->fdcan->ILE = FDCAN_ILE_EINT0;

#ifdef STM32H7XX
/* H7 version of FDCAN has configurable memory layout, so configure it */
canp->fdcan->SIDFC = STM32_FDCAN_FLS_NBR << 16 | SRAMCAN_FLSSA;
canp->fdcan->XIDFC = STM32_FDCAN_FLE_NBR << 16 | SRAMCAN_FLESA;
canp->fdcan->RXF0C = STM32_FDCAN_RF0_NBR << 16 | SRAMCAN_RF0SA;
canp->fdcan->RXF1C = STM32_FDCAN_RF1_NBR << 16 | SRAMCAN_RF1SA;
canp->fdcan->RXBC = SRAMCAN_RBSA;
canp->fdcan->TXEFC = STM32_FDCAN_TEF_NBR << 16 | SRAMCAN_TEFSA;
/* NB: this doesn't set NDTB, but sets TFQS to run in queue mode with no dedicated buffers */
canp->fdcan->TXBC = STM32_FDCAN_TB_NBR << 24 | SRAMCAN_TBSA;

/* set to use the full 18-byte size buffer elements */
canp->fdcan->TXESC = 0x007;
canp->fdcan->RXESC = 0x777;
#endif /* STM32H7XX */

/* Going in active mode.*/
if (fdcan_active_mode(canp)) {
osalDbgAssert(false, "CAN initialization failed, check clocks and pin config");
Expand Down
14 changes: 7 additions & 7 deletions os/hal/ports/STM32/STM32H7xx/stm32_registry.h
Expand Up @@ -123,14 +123,14 @@
#define STM32_HAS_FDCAN1 TRUE
#define STM32_HAS_FDCAN2 TRUE
#define STM32_HAS_FDCAN3 FALSE
#define STM32_FDCAN_FLS_NBR 128U
#define STM32_FDCAN_FLE_NBR 128U
#define STM32_FDCAN_RF0_NBR 64U
#define STM32_FDCAN_RF1_NBR 64U
#define STM32_FDCAN_RB_NBR 64U
#define STM32_FDCAN_TEF_NBR 32U
#define STM32_FDCAN_FLS_NBR 64U
#define STM32_FDCAN_FLE_NBR 64U
#define STM32_FDCAN_RF0_NBR 56U
#define STM32_FDCAN_RF1_NBR 56U
#define STM32_FDCAN_RB_NBR 0U
#define STM32_FDCAN_TEF_NBR 0U
#define STM32_FDCAN_TB_NBR 32U
#define STM32_FDCAN_TM_NBR 64U
#define STM32_FDCAN_TM_NBR 0U

/* DAC attributes.*/
#define STM32_HAS_DAC1_CH1 TRUE
Expand Down