SpenceKonde / megaTinyCore

Arduino core for the tinyAVR 0/1/2-series - Ones's digit 2,4,5,7 (pincount, 8,14,20,24), tens digit 0, 1, or 2 (featureset), preceded by flash in kb. Library maintainers: porting help available!
Other
546 stars 141 forks source link

Issue with baud rate calculation under certain circumstances #1123

Open tangiblewaves opened 1 week ago

tangiblewaves commented 1 week ago

I came across this issue: I used Serial0 (baudrate does not matter here) and found, that the effective baudrate was wrong after Serial0.begin(). Looking into the source of UART.cpp I saw that the baudrate divider is calculated as baud_setting = (((4 * F_CPU) / baud)) which is fine for the default value of the Periperal clock (PDIV in CLKCTRL.MCLKCTRLB). But I had changed this; therefore the calculation failed. Was no issue to workaraund, just by setting USART.BAUD after Serial0.begin(), but I wonder, if the peripheral clock should be considered in the baudrate calculation?

hmeijdam commented 1 week ago

Are you using setup() and loop() or just a main()

or can you show a minimal sketch that just demonstrates your problem?

tangiblewaves commented 1 week ago

I'm using setup() and loop(); good old Arduino style ;.)

Im setup(), I'm calling _PROTECTED_WRITE(CLKCTRL.MCLKCTRLB, CLKCTRL_PDIV_2X_gc | CLKCTRL_PEN_bm);

Later in setup() then Serial0.begin(MIDI_BAUDRATE);

I fix it by calling USART0.BAUD = .... after Serial0.begin(...)

Really, not a big issue, but in case somebody stumbles upon the same problem...

Thanks!