Route the 32.768 kHz Teensy 4.0/4.1 clock to an output pin

Status
Not open for further replies.

sarexpert

Member
Here is a noobie question.
What is the least load way to route the 32.768 kHz low speed clock to an output pin on a Teensy 4.0 / 4.1? It does not need to be gated, just always present from boot.
I would prefer not to tie up a timer, but that would be oK if needed.
 
The RTC can generate periodic interrupts. Below an example which toggles the LED every second. You can also set it up for other frequencies derived from the 32'768Hz clock (see code below). I don't remember if it allows for the full 32kHz call frequency, but this is well documented in the IMXRT manual.

Code:
// rtc interrupt code ..............................................
using callback_t = void(void);

void setupRTC_Interrupt(callback_t isr)
{
    // disable periodic interrupt
    SNVS_HPCR &= ~SNVS_HPCR_PI_EN;
    while ((SNVS_HPCR & SNVS_HPCR_PI_EN)) {} // spin until PI_EN is reset...

    // set interrupt frequency to 1Hz
    SNVS_HPCR = SNVS_HPCR_PI_FREQ(0b1111);   // once per second, change for other frequencies (see IMXRT1062 manual for possible values)

    // enable periodic interrupt
    SNVS_HPCR |= SNVS_HPCR_PI_EN;
    while (!(SNVS_HPCR & SNVS_HPCR_PI_EN)) {} // spin until PI_EN is set...

    // attach a callback
    attachInterruptVector(IRQ_SNVS_IRQ, isr);
    NVIC_SET_PRIORITY(IRQ_SNVS_IRQ, 255);    // lowest priority
    NVIC_ENABLE_IRQ(IRQ_SNVS_IRQ);
}

void RTC_ISR()
{
    SNVS_HPSR |= 0b11; // reset interrupt flag
    
    // do whatever needs to be done in the ISR...
    digitalToggleFast(LED_BUILTIN);
 
    asm("dsb"); // wait until flag is synced over the busses to prevent double calls of the isr
}

//............................................................

void setup()
{
    pinMode(LED_BUILTIN, OUTPUT);  
    setupRTC_Interrupt(RTC_ISR);
}

void loop()
{  
}
 
Last edited:
I remember there is indeed a pad which in theory could deliver the 32768khz directly - in theory - it's not available on any of the Teensys pins.
The 24MHz main oscilator does not give a chance for 32768kHz because there is no valid divisor. 32000khz would be doable, I think.
But there are the PLLs. The video PLL is unused. You would have to program it for a multiple of 32768kHz - something above 600MHz minimum if I remember correctly and divide it down.
 
Did a quick check, setting SNVS_HPCR_PI_FREQ to 0 calls the ISR every 1/32'768 s.

Screenshot 2021-02-17 183528.jpg

Here the relevant section of the manual:

Screenshot 2021-02-17 183930.jpg
 
This looks like it would function. It would call the ISR every 30.5 microseconds which would keep the Teensy pretty busy. At once per second, that is easy. at once per 30 uS it probably isn't the best way.
I don't know if the counters can divide by 1, but I was hoping there was a hardware switch that would just put the clock on an output.
 
32kHz interrupt frequency is not really a problem for a 600MHz processor.
Most of the timers have a possibility to set/toggle a pin on timer overflow. I don't know if the RTC timer is also able to do this. You'd probably need to read the manual... Or follow Franks route if you really can't afford the 32kHz interrupt rate.
 
To turn on the 32 kHz clock output at that pin, use this:

Code:
  IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_00 = 2;  // AD_B0_00 = REF_CLK_32

Details on page 472-473.
 
Oh, great! I'm waiting for a radio chip that needs exactly these 32768khz. That saves a pin :) or the crystal
 
You can also use analogWriteFrequency with any of the PWM pins. But you'll get 32765.4 Hz, because it has to use an integer division of the 150 MHz peripheral clock. That peripheral clock also comes from the ARM clock with is created by PLL1 locked to the 24 MHz crystal.

REF_CLK_32 on that test pad is a digital signal created directly from the output of the 32.768 kHz crystal oscillator.
 
I'd say the video PLL can be used, too (with the 2nd I2S misused as clock generator). But I have not tested this - have used it for much higher frequencies only (100MHz range)
 
Oh, great! I'm waiting for a radio chip that needs exactly these 32768khz. That saves a pin :) or the crystal

SI4735? I just got a couple of breakout boards from ak-modul-bus.dk
Perhaps we can share experiences.
I am working on a receiver board that will be used in a multi receiver for broadcast stations. Up to four channels of receiver boards will feed a icecast type streamer for station monitoring. The teensy 4.0 will accept the audio from the SI4735 I2S and convert it into a compressed audio data stream for that receiver. The streams will go to an Icecast streamer on a Teensy 4.0 on the main board. Each receiver will have a 1.2 inch display with channel info and RDS, and the main board will have a 2.0 inch display and joystick to control them. The whole thing will be in a 1 RU rack box. there will also be a couple of little monitor speakers, and a headphone jack on the front. The back will have a loss of signal data output, balanced analog and AES-3 outputs and Antenna input jacks. If I can figure how to get the Si4735 to spit out the FM stereo composite on the I2S, I plan to make a converter board to receive VHF or UHF Studio transmitter link signals.
 
Yes, exactly this :)
In the mean time I read the datasheet - it does not have to be 32768Hz - it takes almost anything.... like 4MHz or so... you just have to tell it the frequency.
My Chip (yes, akmodul-bus) arrived yesterday - have not done anything with it, so far.
I want to use its I2S interface to drive filters and decoders on the Teensy.

An Icecast streamer is a really nice idea!
 
I wrote a program that turns the Teensy into a FM Transmitter, Stereo + RDS - will be useful for testing :)
 
it does not have to be 32768Hz - it takes almost anything..

Maybe there is nothing against simply using the 44.1kHz I2S Framesync for this as well. That would be a good opportunity!

I soldered the pins to my Si4735 board, and put it on a breadboard. I have the I2C communication working - just the I2C scanner.
Have not that much time today. Have you seen this library? Looks very good. I'll install it.
It also allows to download the SSB Firmware (wow, 16kB?) to the chip.

It would be better to create a new thread "Si7435 experiments"
 
It does not want 44.100 Hz :)
But this works (with the library mentioned above):
Code:
  tone(0, 32000); // Output 32kHz on Pin 0 - connect Teensy Pin 0 with RCLK on Si4735
  si4735.setup(RESET_PIN, -1, FM_FUNCTION, SI473X_ANALOG_AUDIO, 0);
  si4735.setRefClockPrescaler(1, 0);
  si4735.setRefClock(32000);
 
But this works

...not more.

I am quite disappointed and frustrated with this Si4735 stuff. This happens to me very very rarely.

After working wonderfully for a short time, it is now pretty much "dead". I2C works, but there is no reception at all. Absolutely nothing. I had only a wire connected as an antenna.
Probably something in the input stage is broken?

Sometimes the reset doesn't work either.

I'm also no longer sure if AK module bus provides a Si7435-D60.
The Datasheet you can download @ AK-Modul-Bus is for a - D20 (should have looked earlier - but who assumes they they sell such an old version? i hope its not the d20.. but.. does not matter...)
Since you can (of course) only guess the imprint, I'm trying to find out now if you can find out the exact model evtl via I2C.

But it does not really matter. Does not work anyway!
When I consider the price of this board (oh man, how much more do you get with the T4, which is even cheaper?) I do not intend to risk another one.

Well.. shit happens.
 
Last edited:
Well, it's a B-20

Code:
  Serial.printf("Chip:  Si47%d-%c%c%c\n", si4735.getFirmwarePN(), si4735.getFirmwareCHIPREV(),  si4735.getFirmwareFWMAJOR(), si4735.getFirmwareFWMINOR());
Prints:
Code:
Chip: Si7435-B20
No wonder that nothing works.
I'll send it back.
 
Status
Not open for further replies.
Back
Top