0

External Oscillator with Qt Py and CircuitPython
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

External Oscillator with Qt Py and CircuitPython

by mre2007 on Mon Mar 29, 2021 1:10 pm

Hello all! I am wondering what needs to be done to get CircuitPython working with an external crystal on a Qt Py. I saw a reference to an old issue where this seemed to have been fixed but no documentation on whether or not I need to spin a new board file and then recompile CircuitPython. I am planning on using pins A0 and A1 with a MS1V-T1K-32.768kHz crystal as I need to do very precise timing.

mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by tannewt on Mon Mar 29, 2021 4:36 pm

How are you planning on doing the timing from CircuitPython? CircuitPython isn't very precise itself. What are you timing?

You can take a look at other SAMD21 boards with a crystal to see what the definition needs to be.

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Re: External Oscillator with Qt Py and CircuitPython

by mre2007 on Mon Mar 29, 2021 5:39 pm

Hi, thank you for your help. I see code in SAMD core, where the clock is being set. So, to confirm I can’t do this directly within CircuitPython, or via a board file?

I was hoping to increase the precision of Timer callbacks. I need repeatable timing when taking measurements. I will be using a reflective sensor to measure and compute rotational speed.

mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by tannewt on Mon Mar 29, 2021 8:03 pm

What Timer callback? CircuitPython doesn't have a callback API.

The Metro M0 Express has a crystal. You can see it enabled here: https://github.com/adafruit/circuitpyth ... oard.h#L24 There are specific pins it needs to connect to. I'm not sure if A0 and A1 are the correct pins.

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Re: External Oscillator with Qt Py and CircuitPython

by mre2007 on Mon Mar 29, 2021 8:07 pm


mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by tannewt on Tue Mar 30, 2021 12:53 pm

That is someone elses fork of CircuitPython and is a MicroPython API. The official CP APIs are documented here: https://circuitpython.readthedocs.io/en ... index.html

I think you'll want to use PulseIn to measure the input signal. It has a default resolution of one microsecond-ish.

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Re: External Oscillator with Qt Py and CircuitPython

by mre2007 on Tue Mar 30, 2021 1:20 pm

Thanks! I didn’t realize I was in a fork :(

PulseIn is blocking though, I would like to do stuff while I am waiting for the next signal. I am thinking more that CircuitPython won’t be my best choice.

mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by tannewt on Tue Mar 30, 2021 1:27 pm

PulseIn isn't blocking. You can read how many pulses have been read and then read them with `popleft` or [] like a list. https://circuitpython.readthedocs.io/en ... io.PulseIn

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Re: External Oscillator with Qt Py and CircuitPython

by mre2007 on Thu Apr 01, 2021 11:13 pm

I played with this today and it doesn’t seem to work in this use-case. PulseIn gives the duration of a positive pulse, or of a negative pulse, and doesn't give the time between pulses. I think I will swap over to Arduino IDE though I really wanted CircuitPython to work :/

mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by bludin on Fri Apr 02, 2021 6:40 am

hhmmm....PulseIn gives the duration of both low and high states, so it should work for you, no?

bludin
 
Posts: 53
Joined: Thu Apr 16, 2020 8:57 am

Re: External Oscillator with Qt Py and CircuitPython

by mre2007 on Fri Apr 02, 2021 10:51 am

I need the duration between two occurrences of a high state to low state transition. I have a hall-effect sensor that I want to measure as it passes by.

mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by tannewt on Fri Apr 02, 2021 12:05 pm

This should be doable because you want low time + high time. The value setting dictates what the first number returned is for (high or low). Once you do that, you can read two values at a time to get the duration you want to measure.

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Re: External Oscillator with Qt Py and CircuitPython

by mre2007 on Fri Apr 02, 2021 12:35 pm

It looks like though it maxes out at 65535 ms - so it can’t measure anything longer than that? Ok I will play around more :)

mre2007
 
Posts: 9
Joined: Tue Mar 23, 2021 10:24 pm

Re: External Oscillator with Qt Py and CircuitPython

by tannewt on Fri Apr 02, 2021 1:12 pm

Yup, 16bits of microseconds. There is a draft PR to allow for changing the resolution here: https://github.com/adafruit/circuitpython/pull/4457

tannewt
 
Posts: 2349
Joined: Thu Oct 06, 2016 8:48 pm

Please be positive and constructive with your questions and comments.