Skip to content

Commit 39afe0e

Browse files
clean up selection of timing config from tables
1 parent 143cd77 commit 39afe0e

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

STM32_CAN.cpp

+19-28
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,21 @@ void STM32_CAN::setBaudRateValues(CAN_HandleTypeDef *CanHandle, uint16_t prescal
665665
CanHandle->Init.Prescaler = _Prescaler;
666666
}
667667

668+
template <typename T, size_t N>
669+
bool STM32_CAN::lookupBaudrate(CAN_HandleTypeDef *CanHandle, int baud, const T(&table)[N]) {
670+
for (size_t i = 0; i < N; i++) {
671+
if (baud != (int)table[i].baudrate) {
672+
continue;
673+
}
674+
675+
/* for the best chance at interoperability, use the widest SJW possible */
676+
setBaudRateValues(CanHandle, table[i].prescaler, table[i].timeseg1, table[i].timeseg2, 4);
677+
return true;
678+
}
679+
680+
return false;
681+
}
682+
668683
void STM32_CAN::calculateBaudrate(CAN_HandleTypeDef *CanHandle, int baud)
669684
{
670685
/* this function calculates the needed Sync Jump Width, Time segments 1 and 2 and prescaler values based on the set baud rate and APB1 clock.
@@ -676,37 +691,13 @@ void STM32_CAN::calculateBaudrate(CAN_HandleTypeDef *CanHandle, int baud)
676691
int bs1 = 5; // optimization. bs1 smaller than 5 does give too small sample-point percentages.
677692
int bs2 = 1;
678693
int prescaler = 1;
679-
uint16_t i = 0;
680694

681695
uint32_t frequency = getAPB1Clock();
682696

683-
if(frequency == 48000000) {
684-
for(i=0; i<sizeof(BAUD_RATE_TABLE_48M)/sizeof(Baudrate_entry_t); i++) {
685-
if(baud == (int)BAUD_RATE_TABLE_48M[i].baudrate) {
686-
break;
687-
}
688-
}
689-
if(i < sizeof(BAUD_RATE_TABLE_48M)/sizeof(Baudrate_entry_t)) {
690-
setBaudRateValues(CanHandle, BAUD_RATE_TABLE_48M[i].prescaler,
691-
BAUD_RATE_TABLE_48M[i].timeseg1,
692-
BAUD_RATE_TABLE_48M[i].timeseg2,
693-
4);
694-
return;
695-
}
696-
}
697-
else if(frequency == 45000000) {
698-
for(i=0; i<sizeof(BAUD_RATE_TABLE_45M)/sizeof(Baudrate_entry_t); i++) {
699-
if(baud == (int)BAUD_RATE_TABLE_45M[i].baudrate) {
700-
break;
701-
}
702-
}
703-
if(i < sizeof(BAUD_RATE_TABLE_45M)/sizeof(Baudrate_entry_t)) {
704-
setBaudRateValues(CanHandle, BAUD_RATE_TABLE_45M[i].prescaler,
705-
BAUD_RATE_TABLE_45M[i].timeseg1,
706-
BAUD_RATE_TABLE_45M[i].timeseg2,
707-
4);
708-
return;
709-
}
697+
if (frequency == 48000000) {
698+
if (lookupBaudrate(CanHandle, baud, BAUD_RATE_TABLE_48M)) return;
699+
} else if (frequency == 45000000) {
700+
if (lookupBaudrate(CanHandle, baud, BAUD_RATE_TABLE_45M)) return;
710701
}
711702

712703
while (sjw <= 4) {

STM32_CAN.h

+3
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,9 @@ class STM32_CAN {
188188
void initializeBuffers(void);
189189
bool isRingBufferEmpty(RingbufferTypeDef &ring);
190190
uint32_t ringBufferCount(RingbufferTypeDef &ring);
191+
192+
template <typename T, size_t N>
193+
bool lookupBaudrate(CAN_HandleTypeDef *CanHandle, int Baudrate, const T(&table)[N]);
191194
void calculateBaudrate(CAN_HandleTypeDef *CanHandle, int Baudrate);
192195
void setBaudRateValues(CAN_HandleTypeDef *CanHandle, uint16_t prescaler, uint8_t timeseg1,
193196
uint8_t timeseg2, uint8_t sjw);

0 commit comments

Comments
 (0)