Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 20 of 20

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

  1. #1
    Junior Member
    Join Date
    Feb 2021
    Location
    NJ, USA
    Posts
    7

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

    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.

  2. #2
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,447
    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 by luni; 02-17-2021 at 05:14 PM.

  3. #3
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    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.

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,447
    Did a quick check, setting SNVS_HPCR_PI_FREQ to 0 calls the ISR every 1/32'768 s.

    Click image for larger version. 

Name:	Screenshot 2021-02-17 183528.jpg 
Views:	12 
Size:	45.1 KB 
ID:	23721

    Here the relevant section of the manual:

    Click image for larger version. 

Name:	Screenshot 2021-02-17 183930.jpg 
Views:	11 
Size:	88.9 KB 
ID:	23722

  5. #5
    Junior Member
    Join Date
    Feb 2021
    Location
    NJ, USA
    Posts
    7
    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.

  6. #6
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    1,447
    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.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,081
    Quote Originally Posted by Frank B View Post
    I remember there is indeed a pad which in theory could deliver the 32768khz directly
    Yes, it is GPIO_AD_B0_00, which is pin M14 on the BGA. On Teensy 4.0 it can be accessed at this bottom side test pad.

    Click image for larger version. 

Name:	ad_b0_00.png 
Views:	12 
Size:	45.3 KB 
ID:	23723

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,081
    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.

  9. #9
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    Oh, great! I'm waiting for a radio chip that needs exactly these 32768khz. That saves a pin or the crystal

  10. #10
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,081
    On Teensy 4.1, you can access it here:

    Click image for larger version. 

Name:	ad_b0_00_t41.png 
Views:	15 
Size:	62.7 KB 
ID:	23724

  11. #11
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    24,081
    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.

  12. #12
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    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)

  13. #13
    Junior Member
    Join Date
    Feb 2021
    Location
    NJ, USA
    Posts
    7
    Quote Originally Posted by PaulStoffregen View Post
    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.
    Thanks Paul - Exactly what I need. I will order pogo pins!

  14. #14
    Junior Member
    Join Date
    Feb 2021
    Location
    NJ, USA
    Posts
    7
    Quote Originally Posted by Frank B View Post
    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.

  15. #15
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    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!

  16. #16
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    I wrote a program that turns the Teensy into a FM Transmitter, Stereo + RDS - will be useful for testing

  17. #17
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    Quote Originally Posted by Frank B View Post
    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"

  18. #18
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    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);

  19. #19
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    Quote Originally Posted by Frank B View Post
    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 by Frank B; 02-21-2021 at 06:46 PM.

  20. #20
    Senior Member+ Frank B's Avatar
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    8,281
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •