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
551 stars 143 forks source link

External crystal #199

Closed avandalen closed 3 years ago

avandalen commented 4 years ago

Hi Spence

Can you add the option to use a crystal instead of the internal RC oscillator?

SpenceKonde commented 4 years ago

Per datasheet, these parts do not support external high frequency crystal. Only a 32KHz watch crystal, or an external CLOCK source. In the case of the former - like, sure it could be done, but you lose all timekeeping functions (for obvious reasons), and I can't imagine there is very much interest in that speed among Arduino people - a much more realistic use case (and one which is at the top of my priority list after I finish all the super-urgent things, which I am months behind on) is an investigation of what people are missing when they report high sleep state power consumption on these parts, and also leverage the realtime clock and the hooks I added in 2.0.0 to enable a library to stop, set, and restart millis, to offer a wrapper around the sleep functions which will stop millis timing, enable the RTC with external crystal or 32KULP and an interrupt to wake on that overflowing, but if that is what wakes it, it will immediately go back to sleep, until whatever the USER configured to wake it does so, at which time it will stop the RTC, copy it's timer value, use that plus cumulative overflows of RTC since it went to sleep as well as the previous millis value to set the current millis time, and hence keep time while in an extremely low power sleep. Like you know, all sorts of consumer electronics today do, without sacrificing micros while awake like the RTC millis option does (which I now regret adding because of this idea). Adding support for an external crystal is a low priority new feature that has been planned for some time. The main problem with it, though, is as soon as you offer an external clock source, you are no longer tied to the frequencies that the internal oscillator can use (hey, how about 12MHz, that's popular?) Okay, but that requires an implementation of micros for TCA, TCB, AND TCD! Why is there a specific implementation? If you can't divide 64 by the speed in MHz (us/clock cycle) evenly (ie. at compiletime), micros will be wrong, and jump around every millis rollover... You can just FORGET about doing floating point division - or ANY division of a long - in a time sensitive function like micros (on ATTinyCore when I realized this, someone had complainted that micros() was way wrong at certain speeds. So then I fixed it, and someone else complained that it was taking ~100us to return. And then I wrote specific bitshift implementations to approximate the division for every supported crystal speed that's not a power of two.) But yeah, my point there is that supporting an external clock source opens a real can of worms in terms of development effort, when I currently have a ton of new issues here I haven't even triaged, a new core that I promised people two weeks ago, and a whole bunch of other cool stuff in the works that will be much bigger impact.

The internal oscillator on these parts is actually pretty good, especially with the ~3v and ~5v oscillator adjustments for UART baud rates and the fractional baud rate generator (which makes all those wacky UART frequencies unnecessary). Heck, if people want the main timebase more accurate, at some point I will probably implement a thing to tune the internal HF oscillator from an external watch crystal connected to the RTC, like the new DA-series parts can do automatically.

avandalen commented 4 years ago

I think the 32kHz crystal is ok, since the clock frequency of 5MHz is derived from it by an internal PLL just like the SAMD21, which use a 32kHz cystal also. https://www.avdweb.nl/arduino/samd21/sam-15x15

avandalen commented 4 years ago

Hi Spence,

Thank you for your detailed explanation. It is not easy I see..

I use the ATtiny in this battery dongle: https://www.avdweb.nl/solar-bike/batteries/battery-hack It could be that the Yamaha motor is sensitive to frequency deviations of the 2400 baud rate with which the data is sent to the Yamaha. I don't know what happens if it is not excact 2400 baud, because I dont know how sensitive (future) Yamaha motors are. Therefore, the 2400 baud rate must be precise and the internal RC oscillator may not be precise enough. To be sure, I have to connect an external crystal.

Do you have an idea how that can be done?

What is the DA series?

Would you like to manufacture and sell my Dily3217 for your own account on your Tindie shop? I have no time anymore.

have a nice day

Albert

SpenceKonde commented 4 years ago

(posts edited to remove duplicates of my post above in order to improve readability of this issue).

What do you mean the 5MHz clock is derived from it via internal PLL? On the tinyAVR 0/1-series and megaAVR 0-series, all the MHz-tier clock speeds (barring an external CLOCK) are derived from the internal high-speed oscillator, which runs at either 16MHz or 20MHz depending on the OSCCFG fuse, by division by the prescaler. If you use the an external 32KHz crystal as the clock source, the clock speed can only be 32KHz at most. Which really limits what you can do with it...

Regarding the Dily3217 - I don't have capability to manufacture doublesided boards currently, so would have to be sending it out, and I don't have time to take something like this on right now either :-P

avandalen commented 4 years ago

Hi Spence

For other deives, e.g. SAMD21, the 48MHz clock is derived from the 32768Khz crystel:

From: Spence Konde (aka Dr. Azzy) Sent: Thursday, June 18, 2020 8:59 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

(posts edited to remove duplicates of my post above in order to improve readability of this issue).

What do you mean the 5MHz clock is derived from it via internal PLL? On the tinyAVR 0/1-series and megaAVR 0-series, all the MHz-tier clock speeds (barring an external CLOCK) are derived from the internal high-speed oscillator, which runs at either 16MHz or 20MHz depending on the OSCCFG fuse, by division by the prescaler. If you use the an external 32KHz crystal as the clock source, the clock speed can only be 32KHz at most. Which really limits what you can do with it... I

Regarding the Dily3217 - I don't have capability to manufacture doublesided boards currently, so would have to be sending it out, and I don't have time to take something like this on right now either :-P

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

avandalen commented 4 years ago

Dear Spence

For another atmel chip, the SAMD21, the 48MHz can be derived from the 32768kHz crystal by the use of a PLL, see the datasheet of the SAMD21 (Digital Frequency Locked Loop (DFLL48M) Operation)

But I suppose the ATtiny3217 doesnt have this? Yes I know, selling of small cheap Arduino boards takes a much of shipping work and does not make a lot of money. Even if you outsource manufacturing and testing Cheers Albert

From: Spence Konde (aka Dr. Azzy) Sent: Thursday, June 18, 2020 8:59 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

(posts edited to remove duplicates of my post above in order to improve readability of this issue).

What do you mean the 5MHz clock is derived from it via internal PLL? On the tinyAVR 0/1-series and megaAVR 0-series, all the MHz-tier clock speeds (barring an external CLOCK) are derived from the internal high-speed oscillator, which runs at either 16MHz or 20MHz depending on the OSCCFG fuse, by division by the prescaler. If you use the an external 32KHz crystal as the clock source, the clock speed can only be 32KHz at most. Which really limits what you can do with it... I

Regarding the Dily3217 - I don't have capability to manufacture doublesided boards currently, so would have to be sending it out, and I don't have time to take something like this on right now either :-P

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

SpenceKonde commented 4 years ago

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

avandalen commented 4 years ago

Hi Spence,

See here: https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32 https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

SpenceKonde commented 4 years ago

Yeah, that's the same thing the DA-series parts will have: an automatic mechanism to adjust OSCCAL (or equiv) based on crystal - it's not a PLL.

You can do that yourself on any "modern AVR" part with the RTC or PIT and crystal and one of the timers derived from system clock. But I was saying, you have less granularity that way that you do if you instead use that timing difference to calculate the perfect BAUD divisor directly - you'd get a factor to multiply the baud rate calculated with the standard methods (minus the correction factors in the SIGROW, which are supposed to do exactly this at certain known conditions.

Again I ask, have you measured how close the baud rate you need is (eg, using a scope)? I think you are overestimating the magnitude of the error you will have to deal with - the internal oscillators and their cal options are WAY better than the classic AVRs. That said, if it's being run on varying voltage and/or varying temperatures, that doesn't work as well...

On Fri, Jun 19, 2020 at 4:28 PM Albert van Dalen notifications@github.com wrote:

Hi Spence,

See here:

https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32

https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SpenceKonde/megaTinyCore/issues/199#issuecomment-646848939, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTXEW4W3ERBUZPP3DD2TZ3RXPC7XANCNFSM4N5MJSDQ .

--


Spence Konde Azzy’S Electronics

New products! Check them out at tindie.com/stores/DrAzzy GitHub: github.com/SpenceKonde ATTinyCore: Arduino support for almost every ATTiny microcontroller Contact: spencekonde@gmail.com

avandalen commented 4 years ago

im on holiday, and come back later

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 11:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

Yeah, that's the same thing the DA-series parts will have: an automatic mechanism to adjust OSCCAL (or equiv) based on crystal - it's not a PLL.

You can do that yourself on any "modern AVR" part with the RTC or PIT and crystal and one of the timers derived from system clock. But I was saying, you have less granularity that way that you do if you instead use that timing difference to calculate the perfect BAUD divisor directly - you'd get a factor to multiply the baud rate calculated with the standard methods (minus the correction factors in the SIGROW, which are supposed to do exactly this at certain known conditions.

Again I ask, have you measured how close the baud rate you need is (eg, using a scope)? I think you are overestimating the magnitude of the error you will have to deal with - the internal oscillators and their cal options are WAY better than the classic AVRs. That said, if it's being run on varying voltage and/or varying temperatures, that doesn't work as well...

On Fri, Jun 19, 2020 at 4:28 PM Albert van Dalen notifications@github.com wrote:

Hi Spence,

See here:

https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32

https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SpenceKonde/megaTinyCore/issues/199#issuecomment-646848939, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTXEW4W3ERBUZPP3DD2TZ3RXPC7XANCNFSM4N5MJSDQ .

--


Spence Konde Azzy’S Electronics

New products! Check them out at tindie.com/stores/DrAzzy GitHub: github.com/SpenceKonde ATTinyCore: Arduino support for almost every ATTiny microcontroller Contact: spencekonde@gmail.com

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

avandalen commented 4 years ago

I come back soon..

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 11:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

Yeah, that's the same thing the DA-series parts will have: an automatic mechanism to adjust OSCCAL (or equiv) based on crystal - it's not a PLL.

You can do that yourself on any "modern AVR" part with the RTC or PIT and crystal and one of the timers derived from system clock. But I was saying, you have less granularity that way that you do if you instead use that timing difference to calculate the perfect BAUD divisor directly - you'd get a factor to multiply the baud rate calculated with the standard methods (minus the correction factors in the SIGROW, which are supposed to do exactly this at certain known conditions.

Again I ask, have you measured how close the baud rate you need is (eg, using a scope)? I think you are overestimating the magnitude of the error you will have to deal with - the internal oscillators and their cal options are WAY better than the classic AVRs. That said, if it's being run on varying voltage and/or varying temperatures, that doesn't work as well...

On Fri, Jun 19, 2020 at 4:28 PM Albert van Dalen notifications@github.com wrote:

Hi Spence,

See here:

https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32

https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SpenceKonde/megaTinyCore/issues/199#issuecomment-646848939, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTXEW4W3ERBUZPP3DD2TZ3RXPC7XANCNFSM4N5MJSDQ .

--


Spence Konde Azzy’S Electronics

New products! Check them out at tindie.com/stores/DrAzzy GitHub: github.com/SpenceKonde ATTinyCore: Arduino support for almost every ATTiny microcontroller Contact: spencekonde@gmail.com

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

avandalen commented 4 years ago

I still think it is a pll / fll with the SAMD21: 17.6.7 Digital Frequency Locked Loop (DFLL48M) Operation The DFLL48M can operate in both open-loop mode and closed-loop mode. In closed-loop mode, a low-frequency clock with high accuracy can be used as the reference clock to get high accuracy on the output clock (CLK_DFLL48M). The DFLL48M can be used as a source for the generic clock generators, as described in the GCLK – Generic Clock Controller. Related Link

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 11:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

Yeah, that's the same thing the DA-series parts will have: an automatic mechanism to adjust OSCCAL (or equiv) based on crystal - it's not a PLL.

You can do that yourself on any "modern AVR" part with the RTC or PIT and crystal and one of the timers derived from system clock. But I was saying, you have less granularity that way that you do if you instead use that timing difference to calculate the perfect BAUD divisor directly - you'd get a factor to multiply the baud rate calculated with the standard methods (minus the correction factors in the SIGROW, which are supposed to do exactly this at certain known conditions.

Again I ask, have you measured how close the baud rate you need is (eg, using a scope)? I think you are overestimating the magnitude of the error you will have to deal with - the internal oscillators and their cal options are WAY better than the classic AVRs. That said, if it's being run on varying voltage and/or varying temperatures, that doesn't work as well...

On Fri, Jun 19, 2020 at 4:28 PM Albert van Dalen notifications@github.com wrote:

Hi Spence,

See here:

https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32

https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SpenceKonde/megaTinyCore/issues/199#issuecomment-646848939, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTXEW4W3ERBUZPP3DD2TZ3RXPC7XANCNFSM4N5MJSDQ .

--


Spence Konde Azzy’S Electronics

New products! Check them out at tindie.com/stores/DrAzzy GitHub: github.com/SpenceKonde ATTinyCore: Arduino support for almost every ATTiny microcontroller Contact: spencekonde@gmail.com

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

avandalen commented 4 years ago

thank you for you good help, i will read this soon.. I was a bit overworked with my hobby projects and need to slow down.

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 11:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

Yeah, that's the same thing the DA-series parts will have: an automatic mechanism to adjust OSCCAL (or equiv) based on crystal - it's not a PLL.

You can do that yourself on any "modern AVR" part with the RTC or PIT and crystal and one of the timers derived from system clock. But I was saying, you have less granularity that way that you do if you instead use that timing difference to calculate the perfect BAUD divisor directly - you'd get a factor to multiply the baud rate calculated with the standard methods (minus the correction factors in the SIGROW, which are supposed to do exactly this at certain known conditions.

Again I ask, have you measured how close the baud rate you need is (eg, using a scope)? I think you are overestimating the magnitude of the error you will have to deal with - the internal oscillators and their cal options are WAY better than the classic AVRs. That said, if it's being run on varying voltage and/or varying temperatures, that doesn't work as well...

On Fri, Jun 19, 2020 at 4:28 PM Albert van Dalen notifications@github.com wrote:

Hi Spence,

See here:

https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32

https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SpenceKonde/megaTinyCore/issues/199#issuecomment-646848939, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTXEW4W3ERBUZPP3DD2TZ3RXPC7XANCNFSM4N5MJSDQ .

--


Spence Konde Azzy’S Electronics

New products! Check them out at tindie.com/stores/DrAzzy GitHub: github.com/SpenceKonde ATTinyCore: Arduino support for almost every ATTiny microcontroller Contact: spencekonde@gmail.com

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

avandalen commented 4 years ago

Hi Spence

Again I ask, have you measured how close the baud rate you need is I cant measure this myself, I use an ancient tektronic scope 100MHz from 40 years old.

Normally the baud rate frequency is not very critical. But the ATtiny is used in a Yamaha battery hack, where the Yamaha motor is fooled because a different battery is used than the original one. The battery sends serial data to the motor and if the motor receives erroneous data it will not work. It turns out that the baud rate in this case must be very precise, therefore a crystal is needed.

I will use another processor ATMEGA328PB-AU with crystal.

See here the baud rate errors: https://cache.amobbs.com/bbs_upload782111/files_22/ourdev_508497.html

Have a nice day Albert

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 11:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

Yeah, that's the same thing the DA-series parts will have: an automatic mechanism to adjust OSCCAL (or equiv) based on crystal - it's not a PLL.

You can do that yourself on any "modern AVR" part with the RTC or PIT and crystal and one of the timers derived from system clock. But I was saying, you have less granularity that way that you do if you instead use that timing difference to calculate the perfect BAUD divisor directly - you'd get a factor to multiply the baud rate calculated with the standard methods (minus the correction factors in the SIGROW, which are supposed to do exactly this at certain known conditions.

Again I ask, have you measured how close the baud rate you need is (eg, using a scope)? I think you are overestimating the magnitude of the error you will have to deal with - the internal oscillators and their cal options are WAY better than the classic AVRs. That said, if it's being run on varying voltage and/or varying temperatures, that doesn't work as well...

On Fri, Jun 19, 2020 at 4:28 PM Albert van Dalen notifications@github.com wrote:

Hi Spence,

See here:

https://microchipdeveloper.com/32arm:samd21-code-gcc-clock-system-dfll48m-init

Im thinking to switch to the STM8 and STM32. These microcontrollers are made Arduino compatible and also accept an external crystal, that is easy for me. https://github.com/stm32duino https://github.com/stm32duino/Arduino_Core_STM8

see for instance the Blue Pill SMT32

https://nl.aliexpress.com/item/32326304541.html?src=google&src=google&albch=shopping&acnt=494-037-6276&isdl=y&slnk=&plac=&mtctp=&albbt=Gploogle_7_shopping&aff_atform=google&aff_short_key=UneMJZVf&&albagn=888888&albcp=6459980570&albag=76980386066&trgt=743612850714&crea=nl32326304541&netw=u&device=c&albpg=743612850714&albpd=nl32326304541&gclid=CjwKCAjwxLH3BRApEiwAqX9arTwy53rD0bMycQcDffKUmKwX3C8BI4R85LXQvKMNCH0YTLyklJA-YxoCzewQAvD_BwE&gclsrc=aw.ds

Albert

STM32 "Blue Pill

From: Spence Konde (aka Dr. Azzy) Sent: Friday, June 19, 2020 12:55 PM To: SpenceKonde/megaTinyCore Cc: Albert van Dalen ; Author Subject: Re: [SpenceKonde/megaTinyCore] External crystal (#199)

That's one hell of a PLL! You sure it's actually a PLL as opposed to using the crystal to autotune an RC oscillator?

A better solution here might be an external CLOCK? (like these, you give them power, and they output a clock signal that can be used as the clock source, those little shiny silvery-golden SMD parts, real short and close to the PCB that look kind of like SMD crystals - and indeed many crystals use the same package, just with 2 of the pins being GND. https://www.digikey.com/products/en/crystals-oscillators-resonators/oscillators/172 )

So that's one approach

Another would be to use an external crystal for the RTC, and use that and a TCB to see how far off it is and adjust. For your purposes, it's probably best to tweak the UART baud value instead of using it to cal the oscillator because you have much finer granularity there thanks to the fractional baud rate generator....

But like, how far off even are you? The internal oscillator, combined with the UART error values in the sigrow for 3v and 5v, and the fractional buad rate generator.... it's pretty damned good. It's worlds apart from the classic AVRs!

The DA-series is what probably would have been the ATmega 1-series if microchip marketeers hadn't taken over. Check out the datasheet AVR128DA64 is the top end part (lean back while reading it so you don't drool into your keyboard). One of the features it has is "autotune" with a watch crystal - it uses that to time the main clock, and then automatically adjusts the calibration bits appropriately.; the version of my boards with the crystal for the 32/48-pin parts will be here next week, and I'll play with it then if I haven't gotten the 64-pin parts working yet. I'm pretty close to releasing DACore to add Arduino support :-)

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/SpenceKonde/megaTinyCore/issues/199#issuecomment-646848939, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABTXEW4W3ERBUZPP3DD2TZ3RXPC7XANCNFSM4N5MJSDQ .

--


Spence Konde Azzy’S Electronics

New products! Check them out at tindie.com/stores/DrAzzy GitHub: github.com/SpenceKonde ATTinyCore: Arduino support for almost every ATTiny microcontroller Contact: spencekonde@gmail.com

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.

SpenceKonde commented 3 years ago

External clock (eg an external clock going to the CLKIN pin) will be added in 3.0.0, coming sooner than one thinks.

Those baud rate errors have nothing to do with clock accuracy. Those are the error assuming a perfect (100% accurate) clock, on a "classic" AVR, due to the granularity of the baud rate divisor on those parts. This just inspired me to make such a spreadsheet, if you can open .xlsx files... BaudRatesAVR.xlsx

That shows the same thing as the wormfood tables (on the bottom);

But like the wormfood tables, they don't account for clock accuracy - clock accuracy errors are on top of that....

SpenceKonde commented 3 years ago

That said, with an external watch crystal, a TCB, and the PIT event source, you have all the tools to correct the USARTn.BAUD value to the optimal for what the clock is running at under any given conditions....

Clock the TCB off system clock, and configure for input capture using PIT_DIV_64 as the event source.... You expect 39062 or 39063... but OFC it won't be that, because the internal oscillator isn't perfect (though IME it's usually within a percent under normal conditions at least).... say you measure 39500, indicating the oscillator is running just over 1% fast....

Call Serial.begin as usual, before sending anything, correct the baud rate... USART0.BAUD=( (((unsigned long) USART0.BAUD)*(MeasuredValueFromTCB) ) + 19531)/39063; (the +19531 is to ensure it's rounded correctly)

Don't have input capture code handy, but it's easy-peasy on the TCB's - they are everything you could wish for in an input capture timer..... input capture pulse width measurement mode, all just makes sense. Very well designed timer IMO...

SpenceKonde commented 3 years ago

External clock support is added will be in 2.10 (there's no 3.0.0 yet - that change got scrubbed)

Implementing autotune off 32.768kHz crystal is not currently planned (and might not be good enough for your purposes - but what I described above would be)

SpenceKonde commented 3 years ago

If you're still looking at this, by any chance:

That all said - I think the ideal course of action here is for you to actually investigate how accurate the baud rate on the official hardware you're trying to copy is. A hardware teardown looking for a crystal would be a good start. If you don't find one,, or if all you find is a watch crystal (it's unlikely they'd derive the baud rate from that, though it's possible), then it's using an RC oscillator to generate it's baud rate, and their accuracy is probably 1-2% at best, meaning trying to do better than that is unnecessary. If you find one of those blue 2-terminal things marked as a crystal from the reference designators, that's a ceramic resonator, not a crystal, so 1-2% accuracy.

SpenceKonde commented 3 years ago

One note for the hardware teardown: I have seen 6 MHz crystals in the "garbage can" form factor (commonly used for watch crystals) in commercial hardware, so just because the crystal you find is small and cylindrical doesn't mean it's not a MHz crystal used as the system clock.

avandalen commented 3 years ago

Hi Spence, thank you very much for your comprehensive help, this will help solve the baud rate problem.