Crystal on Original Espruino

Posted on
  • Starting a new post based on http://forum.espruino.com/conversations/­346029/#comment15260762

    Espruino revision 1.4 boards already have a low speed oscillator on board. It's only 1.3 where you might want to add one.

    But if you have an earlier board there's some info on fitting a crystal here: http://www.espruino.com/Clocks#espruino-­original

  • Yes, I have found that article. I would like to know if it is possible to check if the crystal is working fine. I have two crystals lying around which I have 'harvested' from old electronic devices, but don't know for sure if they work...

  • This bit of code seems to do the trick:

    function getClockSource() {
      return ["NONE","LSE","LSI","HSE"][((peek32(0x40­021020)&768)>>8)];
    }
    getClockSource();
    

    It'll report what the clock source for the oscillator is currently set to.

    Is it one of the older boards you have then?

  • function getClockSource() {

    return ["NONE","LSE","LSI","HSE"][((peek32(0x40­021020)&768)>>8)];
    }
    getClockSource();

    Yes, I have a version 1.3b. One of the first boards I guess?

    Your code reports "LSI" so I guess that means the Low-speed internal (LSI) RC oscillator is active?

    I will solder the crystal now and see what happens.

    Thanks!

  • Your code reports "LSI" so I guess that means the Low-speed internal (LSI) RC oscillator is active?

    That's right - yep!

  • So, I soldered the crystal:

    Your code now returns "LSE", so it's looking good. Now I'll wait if the time will be more accurate...

  • Great!

  • I wondered why we use the LSE with a specific value of 32.768 kHz

    Found this answer:

    The frequency of a real time clock varies with the application. The frequency 32768 Hz (32.768 kHz) is commonly used, because it is a power of 2 (215) value. And, you can get a precise 1 second period (1 Hz frequency) by using a 15 stage binary counter.

    Practically, in majority of the applications, particularly digital, the current consumption has to be as low as possible to preserve battery life. So, this frequency is selected as a best compromise between low frequency and convenient manufacture with market availability and real estate in term of physical dimensions while designing board, where low frequency generally means the quartz is physically bigger.

  • Damn, I've been away so long...

    Once it gets going, the popularity is self-sustaining because everyone and their mom is making them in uber-mega-bulk. If you see a garbage-can shaped crystal in basically anything you scrap, you're basically guaranteed that it will be that frequency, even the el-cheapo ones from china with no markings on them. I think I have only twice (out of pulling like 20-30 of those things) found ones that weren't, and those had the frequency clearly marked. Also, the black 4-pin SMD ones (you know the ones I'm talking about) that you see all over the place are also, almost always, 32.768k too - just depending on whether their process is better suited to SMD. You also see garbage-cans on their side with the pins bent into a shape such that they can be surface mounted pretty often too (though I wouldn't use one in a board I designed - I'd use a black 4-pin one (note that there are only two unique pins - I forget whether they're tied together in pairs, or if two are NC - they're not a powered oscillator like many of the flat ones with 4 pins and that distinct shiny metal case (though some of those are just crystals too; for a while I used to pull those - they're surprisingly expensive, actually). But since I started making and selling boards, I have turned against using pulls - literally the only time I will use pulls now is if something mission critical failed, and I had no choice. Like the time I brought everything I needed to do a bunch of AVR development over my 1 week summer vacation, specifically some stuff regarding external crystals - but then realized I'd forgotten the 22pf caps for the crystals! So we went to the dump and raided the e-waste trailer (we = my father and I... thinking back, I'll bet back then he was just getting into his obsessive focus on SMPS's, so he wanted to get stuff to examine the SMPS modules anyway - dear god, at one point he literally had 30 computer power supplies stacked up. Eventually he decided he understood it well enough after obsessing for a year or so, and disassembling a dozen or two SMPS's - the computer supplies are kind of boring, because they're all the same inside anyway - what was a lot more interesting was random consumer electronics), and tore them apart when we got home, and I found my pair of 22pf caps and proceeded with my project... or the time when I was going from the summer place straight to my first job interview, but we lost the charger for my shaver... so we pulled a pot and a 5v supply and manually adjusted it while monitoring voltage to charge the battery so I could look sharp for the interview... (now we have a stash of parts there, of course - including at least one Espruino!)

    And yeah, the fact that a binary counter can be used with them and easily convert to seconds is HUGE.

    The one annoying thing is that there are 1000ms in a second instead of 1024; I do one thing where I prescale the clock by 32, then (since it's on an 8-bit AVR - no hardware division (like, I was thanking my lucky stars we had hardware multiplication :-P), so in a timing function, doing that to an unsigned long is right out the window! I think I right-shifted it 6 places, subtracted that from the original, then rightshifted the already right-shifted one one more place, and subtracted it again, and that gave me my millis() implementation...
    But what was interesting when I tested it... it was WAY off, and I deduced that the half-assed way in which I;d connected the crystal was killing the accuracy because the stray capacitance was way wrong. That's one thing I wish I had a heuristic for, calculating how much stray capacitance I have, so I know what value caps to use (the datasheet warns several times of the importance of getting the capacitance right - at least for parts I'm working with now) - since I'm now making and selling boards, I kind of want to not completely bung it!

    And the final aside I was getting to... the part I am focusing on now, it doesn't have a high-speed external crystal, it's internal oscillator only (atmel-now-microchip's AVR team broke up with MHz crystals a couple of years ago), but obviously they have watch crystals, and a proper 16-bit RTC timer now so they can use them (instead of the old atmega halfassery with the async 8-bit timer2)... naturally, customers buying their new flagship product want accurate clocks... they have what they call "autotune" (lol) - you put a watch crystal on it, and it counts clocks, and dynamically recalibrates the internal oscillator! Haven't played with it, since I went down a rathole with programming tools that I'm just emerging from - but if it works, it's damned cool. My breakout boards for those take garbage-can-shaped through-hole ones (all I could route) but I really wish I was able to calculate whether I had the right caps on it...

    My god, why did I write this essay in a 2-week old thread, lol... I think I wanted a break from what I was doing (I feel bad for switching to Arduino for most tasks - I do still use Espruino for web stuff - but I'd have never gotten into the hobby and had all this fun if it weren't for Espruino. It is such a great way to get comfortable with how digital electronics work... but I've come to absolutely love the parts where I can work really close to metal, which requires the parts be simple enough that I can hold the concept of them in my head. Those who remember me may recall that quickly gravitated to getting low level within the Espruino VM....)

  • wow! that was indeed a long reply! :-)

  • I think I wanted a break from what I was doing

    ...sticking with the excessive hours of screen time due to the pandemic restrictions... changing from one screen time thing to another one - what about that?

    Hi, @DrAzzy, your post explains the dim Espruino publication frequency... and no wonder, I got some nice devices for the two closest women in my life - https://www.tindie.com/products/svdbor/a­rt-palette-colored-led-earrings-pair/ - and there I found https://www.tindie.com/products/svdbor/t­iniest-arduino-compatible-board-with-mic­ronucleus/ - micronucleus from @DrAzzy!

  • Interesting - he's using my core (though he's not associated with me).

    I should ping him though, because an upcoming change to the IDE, I think, will break the way he gets his programmer entries in (or maybe not - I didn't bother downloading it to examine it - too much else to do)

  • Oh, lol - it's the guy who I worked with at the start of megaTinyCore, and who draws the pinout diagrams for me!

    I thought the style of those boards looked familiar!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Crystal on Original Espruino

Posted by Avatar for Gordon @Gordon

Actions