Another sysclk_init issue

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

Greetings everyone:

New to the posting to the forums, though many a topic has answered my questions up to now - so thanks for that!!

I'm also new to programming AVRs.  This seems like this should be so simple, but I've searched the net and spent way too long on it to no avail, so here I am.  BTW, I'm programming for XMega32A4U.

I'm trying to set up system to use USB.  So, the first thing I do, aside from initialize some variables I'll need later, is call sysclk_init().  Here are the active lines of my conf_clock.h file (I based this on the examples and manuals on-line).
 

#define CONFIG_SYSCLK_SOURCE        SYSCLK_SRC_RC32MHZ
#define CONFIG_SYSCLK_PSADIV          SYSCLK_PSADIV_2
#define CONFIG_SYSCLK_PSBCDIV         SYSCLK_PSBCDIV_1_1
 
#define CONFIG_USBCLK_SOURCE                USBCLK_SRC_RCOSC
#define CONFIG_OSC_RC32_CAL                 48000000UL
#define CONFIG_OSC_AUTOCAL_RC32MHZ_REF_OSC  OSC_ID_USBSOF

Here's the odd thing:  Once sysclk_init() is called, the IO page shows no changes when writing to any of the SPI registers or the TCC0/TCC1 registers.  The port configuration registers seem to work fine (PORTC, PORTD, etc.)

If I comment out the sysclk_init() call, then those registers seem to work appropriately.  But, of course, my clock isn't set to speeds that are conducive to using the USB.

This is the same whether I'm using the simulator or using the ICE connected to the chip via PDI (with the chip's USB port connected to my computer).  In the simulator it shows clock as 1MHZ, the ICE says the clock rate is unavailable.

I am coding in C++, but this is one of the first things that I do in the main() function, so I don't think it's object-related (though I mention it here just in case).

Shortly after the sysclk_init() call, I call udc_start().  This always causes my computer to state that the new USB device is "not recognized."  I feel fairly certain the sysclk is the cause.

If someone could tell me what I'm missing from the conf_clock.h file (or any other step I'm missing), I would greatly appreciate it!!

Thanks in advance!

Here's the snippet of code, just in case...

 

int main(void)
{
    uint8_t pdc_framerate, pdc_clock_mhz;
    
    //Set up system clock for USB use.
    sysclk_init();
    irq_initialize_vectors();
    cpu_irq_enable();
    
    //Start USB
    udc_start();
    
    //Set default framerate
    pdc_framerate = PBCMD_DEFAULT_FRAMERATE;  //default = 15
    pdc_clock_mhz = PDC_DEFAULT_CLOCK_SPEED;  //default in MHZ = 24
    
    TCC1.CTRLA = TC_CLKSEL_DIV1024_gc;  //Divide by 1024 (24Mhz/1k = 24k/sec
    TCC1.CTRLB = 0; 
    TCC1.CTRLC = 0; 
    TCC1.CTRLD = 0;  
    TCC1.CTRLE = 0;  
    TCC1.CTRLFCLR = 0;  //Counting up (1=counting down)
    TCC1.INTFLAGS = 0x01;  //Writing a 1 clears the overflow register
    TCC1.PERBUF = ((uint16_t) pdc_clock_mhz * 1000 / (uint16_t) pdc_framerate);
    
    //More code follows...
}

 

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

I don't use ASF so I can't help you there.

 

If you are using full speed USB, the 32MHz osc. needs to run at 48MHz.  The CPU/PER clock must be at least 12MHz.  I don't know about slow speed USB.

 

You should also use DFLL, or the clock is apt to be too inaccurate. 

 

Maybe ASF sets all this up for you.

 

 

 

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

This is an old gotcha with ASF.

 

sysclk_init() turn on power management for ALL peripherals, i.e., it TURNS OFF the peripheral clock.  If you use ASF calls to init the peripherals, these calls enable the peripheral clock.  Otherwise you must use sysclk_enable_module() or write directly to the Power Management registers to enable peripheral clocks.  

Greg Muth

Portland, OR, US

Atmel Studio 7.0 on Windows 10

Xplained/Pro/Mini Boards mostly

 

 

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

Greg_Muth wrote:
write directly to the Power Management registers to enable peripheral clocks

 

That did it!  Thanks so much!