SAMD20 with external crystal

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

 

I'm use the crystal oscillators on a custom board that has a 32.768kHz crystal and a 14.45MHz. I've managed to turn both of them on via the conf_clocks.h settings (ASF3.4), and I can see them buzzing with an oscilloscope. The issue is I can't get them to do anything in my code.

 

With the 32kHz, I wanted to feed to into an 8-bit timer to toggle an LED in a callback function. I got the timer working with the internal 32kHz, but when I try and use the external crystal the code latches up whenever this code is called in the tc_init function: 

/* Write configuration to register */
	while (tc_is_syncing(module_inst)) {
		/* Wait for sync */
	}

Which suggests to me that the oscillator isn't producing a 32kHz signal, hence why it can't synchronize.

 

The second issue I have is changing the CPU clock to the 14MHz external crystal. It's not clear in the datasheet or the ASF documentation how to do this.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Without the conf_clocks.h and the timer setup code it's hard to have any opinion about your issue.

Changing the CPU clock is done in conf_clocks.h (GCLK0 is the CPU clock), source would have to be GCLK_SOURCE_XOSC (with suitable CONF_CLOCK_XOSC* settings).

/Lars

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Regarding the 32kHz crystal, here is my configuration from conf_clocks.h

 

/* SYSTEM_CLOCK_SOURCE_XOSC32K configuration - External 32KHz crystal/clock oscillator */
#  define CONF_CLOCK_XOSC32K_ENABLE               true
#  define CONF_CLOCK_XOSC32K_EXTERNAL_CRYSTAL     SYSTEM_CLOCK_EXTERNAL_CRYSTAL//SYSTEM_CLOCK_SOURCE_XOSC32K
#  define CONF_CLOCK_XOSC32K_STARTUP_TIME         SYSTEM_XOSC32K_STARTUP_4096
#  define CONF_CLOCK_XOSC32K_AUTO_AMPLITUDE_CONTROL  false
#  define CONF_CLOCK_XOSC32K_ENABLE_1KHZ_OUPUT    false
#  define CONF_CLOCK_XOSC32K_ENABLE_32KHZ_OUTPUT  false                            
#  define CONF_CLOCK_XOSC32K_ON_DEMAND            false
#  define CONF_CLOCK_XOSC32K_RUN_IN_STANDBY       true 

/* SYSTEM_CLOCK_SOURCE_OSC32K configuration - Internal 32KHz oscillator */
#  define CONF_CLOCK_OSC32K_ENABLE                true
#  define CONF_CLOCK_OSC32K_STARTUP_TIME          SYSTEM_OSC32K_STARTUP_130
#  define CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT    false
#  define CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT   false
#  define CONF_CLOCK_OSC32K_ON_DEMAND             false
#  define CONF_CLOCK_OSC32K_RUN_IN_STANDBY        true

/* Configure GCLK generator 2 (RTC) */
#  define CONF_CLOCK_GCLK_2_ENABLE                true
#  define CONF_CLOCK_GCLK_2_RUN_IN_STANDBY        true
#  define CONF_CLOCK_GCLK_2_CLOCK_SOURCE          SYSTEM_CLOCK_SOURCE_XOSC32K
#  define CONF_CLOCK_GCLK_2_PRESCALER             32
#  define CONF_CLOCK_GCLK_2_OUTPUT_ENABLE         false

 

And here is my timer config (based on example in an application note):

void configure_tc(void)
{
	struct tc_config config_tc;
	tc_get_config_defaults(&config_tc);
	config_tc.counter_size = TC_COUNTER_SIZE_8BIT;
	config_tc.clock_source = GCLK_GENERATOR_2;
	config_tc.clock_prescaler = TC_CLOCK_PRESCALER_DIV8;
	config_tc.counter_8_bit.period = 100;
	//config_tc.counter_8_bit.compare_capture_channel[0] = 50;
	//config_tc.counter_8_bit.compare_capture_channel[1] = 54;
	tc_init(&tc_instance, CONF_TC_MODULE, &config_tc);
	tc_enable(&tc_instance);
}

 

The timer runs if I set GLCK to use OSC32K, but not XOSC32K. As I mentioned above, the oscillator hardware appears to be working electrically.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've managed to get it working. The field

CONF_CLOCK_OSC32K_ENABLE_32KHZ_OUTPUT

 

Needs to be set as true. I presumed this turned turned a digital output into a clock output, but that's not the case. Interestingly, the other field,

CONF_CLOCK_OSC32K_ENABLE_1KHZ_OUTPUT

Causes a bit to be set in the register that doesn't exist in the datasheet. There's no mention of turning a 1kHz output on in the datasheet for the SAMD20 so I'd say this is a bug in ASF.

 

 

Cheers,

Trevor.

Last Edited: Thu. May 16, 2019 - 05:13 AM