Skip to content

Commit 85c45a5

Browse files
committed
[NUC472] Fix SetBaudRate 5% inaccuracy issue
1 parent ae12346 commit 85c45a5

File tree

1 file changed

+20
-0
lines changed
  • targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver

1 file changed

+20
-0
lines changed

targets/TARGET_NUVOTON/TARGET_NUC472/device/StdDriver/nuc472_can.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ uint32_t CAN_SetBaudRate(CAN_T *tCAN, uint32_t u32BaudRate)
419419

420420
SystemCoreClockUpdate();
421421

422+
#if 0 // original implementation got 5% inaccuracy.
422423
u32Value = SystemCoreClock;
423424

424425
if(u32BaudRate * 8 < (u32Value/2)) {
@@ -428,7 +429,26 @@ uint32_t CAN_SetBaudRate(CAN_T *tCAN, uint32_t u32BaudRate)
428429
u8Tseg1 = 2;
429430
u8Tseg2 = 1;
430431
}
432+
#else
433+
u32Value = SystemCoreClock / u32BaudRate;
434+
/* Fix for most standard baud rates, include 125K */
435+
436+
u8Tseg1 = 3;
437+
u8Tseg2 = 2;
438+
while(1)
439+
{
440+
if(((u32Value % (u8Tseg1 + u8Tseg2 + 3)) == 0) | (u8Tseg1 >= 15))
441+
break;
442+
443+
u8Tseg1++;
431444

445+
if((u32Value % (u8Tseg1 + u8Tseg2 + 3)) == 0)
446+
break;
447+
448+
if(u8Tseg2 < 7)
449+
u8Tseg2++;
450+
}
451+
#endif
432452
u32Brp = SystemCoreClock/(u32BaudRate) / (u8Tseg1 + u8Tseg2 + 3) -1;
433453

434454
u32Value = ((uint32_t)u8Tseg2 << CAN_BTIME_TSEG2_Pos) | ((uint32_t)u8Tseg1 << CAN_BTIME_TSEG1_Pos) |

0 commit comments

Comments
 (0)