grlib/occan: Fix baud rate calculation

Fixes #5219
This commit is contained in:
Jan Sommer
2024-12-03 09:10:02 +01:00
parent ef6e6e3087
commit 13cbd83444
2 changed files with 6 additions and 3 deletions

View File

@@ -62,7 +62,8 @@ int grlib_canbtrs_calc_timing(
tseg++) {
/* calculate scaler */
tmp = ((br->divfactor + tseg) * baud);
sc = (core_hz * 2)/ tmp - core_hz / tmp;
/* Core frequency is always divided by 2 before scaler */
sc = core_hz / (2 * tmp);
if (sc <= 0 || sc > br->max_scaler)
continue;
if (br->has_bpr &&
@@ -71,7 +72,7 @@ int grlib_canbtrs_calc_timing(
((sc > 256 * 4) && (sc <= 256 * 8) && (sc & 0x7))))
continue;
error = baud - core_hz / (sc * (br->divfactor + tseg));
error = baud - core_hz / (2 * sc * (br->divfactor + tseg));
#ifdef GRLIB_CANBTRS_DEBUG
printf(" baud=%d, tseg=%d, sc=%d, error=%d\n",
baud, tseg, sc, error);

View File

@@ -1000,7 +1000,9 @@ static void convert_timing_to_btrs(
{
btrs->btr0 = (t->rsj << OCCAN_BUSTIM_SJW_BIT) |
(t->scaler & OCCAN_BUSTIM_BRP);
btrs->btr1 = (0<<7) | (t->ps2 << OCCAN_BUSTIM_TSEG2_BIT) | t->ps1;
/* Core adds +1 to the register values, so compensate here by decrementing */
btrs->btr1 = (0<<7) | ((t->ps2-1) << OCCAN_BUSTIM_TSEG2_BIT) | (t->ps1-1);
}
static int occan_set_speedregs(occan_priv *priv, occan_speed_regs *timing)