-
Notifications
You must be signed in to change notification settings - Fork 143
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
Attiny3224: Serial baud calculation not working #1137
Comments
And you did not use "int main(void)" instead of "void setup()" and "void loop()" ? |
[edit, sketch replaced by another one] oops, that sketch is not working on the 2 series parts, as the ADC registers have changed. I made it for 0 and 1 parts. Here is a simpler one, using the baudrate formula. Can you give it a try?
|
This is screaming "main overridden" to me too? Because this definitely works in the general case.. And nothing works if you override main without replacing the functionality you overrode. (sometimes I wanna put init() or at least initClock() into .init9 or something, so even when people overrode main, the chip would still run at the speed they chose... but I think I'd get a ton of shit over that so... On sketch startup, the basic things that happen are: Callbacks were largely added as it became clear that not many people are going to have a good time if they needed to override main(), This ain't classic AVR, clock speed at startup is 2.66 MHz or 3.33 until initClock() (the first function called by init) runs to set the clock speed to match F_CPU |
Thanks, I've been doing some tests with that sketch and mine, and more investigations, and I think that I found the problem. And another issue or question.
Yes, as usual. The problem was my understanding of F_CPU and friends. In the datasheet they indeed talk about CLK_PER, because it's about the USART. But I understand that in this MCU's CLK_PER is always the same than CLK_CPU, and therefor the same than F_CPU . And all them are after the preescaler. So, to have everything correct, I have to set F_CPU (in platformio.ini) and set the preescaler myself in the code. What I was not doing. So that's OK and clear, I think. The problem I have is when I set some preescaler values like 8 or 6, then F_CPU is: 2.5M or 3.3M. But some macro in the libraries is complaining that it is not a valid F_CPU value. But all them are valid preescalers. And actually if I comment out the macro #error, everything works fine, including the delays and so on.
Probably I miss something else :-) But at least now works and makes sense. |
I had a problem to setup the serial communication with Serial.begin(), with the Attiny3224 MCU. After several investigations I found that the problem was the baud calculation. Finally I managed to make it working updating directly the USART0 register with the correct calculation out of the datasheet (and many try and error iterations).
So, I think that the calculation of the baud_setting in UART.cpp, HardwareSerial::begin, is not correct in this case. This line:
baud_setting = (((4 * F_CPU) / baud));
In the Attiny3224 datasheet (24.3.2.2.1 The Fractional Baud Rate Generator) this calculation is shown, but using CLK_PER (peripheral clock) instead of F_CPU. And in the case of this MCU:
CLK_PER = F_CPU / 6
It is mentioned in the clock controller section of the datasheet.
The question is that using the adapted calculation it works. This is my code:
Then Serial.println() works fine.
So the baud_setting should be divided by 6. And for some reason F_CPU/6 directly doesn't work if the clock frequency was reduced, e.g: 10MHz. It expects still 20Mhz for the baud calculation. I don't know yet how to calculate it properly.
I don't know if I miss something. The test program is not doing anything else, just blinking a led.
Thanks!
The text was updated successfully, but these errors were encountered: