AVR32 Clocks

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

I'm trying to write my own UC3 application on an EVK1104, using the EVK1104 User Project template as a base. I've noticed that different ASF examples use different methods to setup the clocks. For example, some use the "pm_configure_clocks" function, yet others use "sysclk_init" to set up the clocks.

In my own project I tried using sysclk_init, but it tells me that I have an "implicit declaration of function 'sysclk_init'" even though it's showing up as red lettering (indicating it's a function). If I include sysclk.h in my main function the error goes away...but shouldn't ASF be handling that automatically in asf.h? Or am I completely missing something?

While I'm on the topic, is there a better way to initialize UC3 clocks?

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

In my experience the asf.h is not handled by the avrstudio5 as expected. So i would advise you never to trust it, and ad the required header files to your source code as you need it.

I guess the reason why there is a sysclk_init and a pm_configure_clocks function as well is, that the asf might be written parallel by 2 groups, one working for xmega, the other for uc32, and both thought it would be nice, that their timer init worked for both 8bit and 32bit uC-s, so now you have 2 timer init functions.

By the way, I use the pcl_configure_clocks function on my AT32UC3A0512.

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

sikolymester wrote:
In my experience the asf.h is not handled by the avrstudio5 as expected. So i would advise you never to trust it, and ad the required header files to your source code as you need it.

Fair enough.

sikolymester wrote:
By the way, I use the pcl_configure_clocks function on my AT32UC3A0512.

Where did you find that function? I'm only familiar with the pm_configure_clocks and sysclk_init functions.

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

It is in the power_clocks_lib.c file.

This is its explanation:
/*! \brief Automatically configure the CPU, PBA, PBB, and HSB clocks
*
* This function needs some parameters stored in a pcl_freq_param_t structure:
* - main_clk_src is the id of the main clock source to use,
* - cpu_f and pba_f and pbb_f are the wanted frequencies,
* - osc0_f is the oscillator 0's external crystal (or external clock) on-board frequency (e.g. FOSC0),
* - osc0_startup is the oscillator 0's external crystal (or external clock) startup time (e.g. OSC0_STARTUP).
* - dfll_f is the target DFLL frequency to set-up if main_clk_src is the dfll.
*
* The CPU, HSB and PBA frequencies programmed after configuration are stored back into cpu_f and pba_f.
*
* \note: since it is dynamically computing the appropriate field values of the
* configuration registers from the parameters structure, this function is not
* optimal in terms of code size. For a code size optimal solution, it is better
* to create a new function from pcl_configure_clocks() and modify it to use
* preprocessor computation from pre-defined target frequencies.
*
* \param param pointer on the configuration structure.
*
* \retval 0 Success.
* \retval <0 The configuration cannot be performed.
*/
extern long int pcl_configure_clocks(pcl_freq_param_t *param);

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

Hello friends, I am new to AVR microcontrollers and I am writing a code for acessing temperature of 4 different LM35 and for minimum and maximum limit for certain application. Also I had tried out code based on ASF for temperature to glow certain indication of LED but I am struck with error such as 1) implicit declaration of function sysclk_init and 2) undefined reference to board_init. And if I didn't mentioned sysclk_init(); and board_init(); declarations then the program has been compiled with zero errors and generate .hex and elf files which I tried to flash in the application board consisting AT32UC3A1256 controller, but the board is dead off. I know that for the board to start and to function properly and with clock pulse we need sysclk_init(); board_init(); declarations. I had tried many different ASF files to add which ever related and necessary and also config_board.h file from other projects but I has been end with same error. So Kindly any body help me to sort out the error , or else if some body had attempted temperature measurement on board with AT32UC3A1256 , then send me the code and necessary project files.

 

Last Edited: Thu. Aug 13, 2015 - 10:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here is my code for temperature detection:

 

 

//_____  I N C L U D E S ___________________________________________________

#include <string.h>

#include "pm.h"
#include "flashc.h"
#include "gpio.h"
#include "spi.h"
// #include "wdt.h"

#include "ctrl_access.h"
#include "fsaccess.h"

//! Scheduler include files.
#include "FreeRTOS.h"
#include "task.h"

//! Demo file headers.
#include "serial.h"
#include "navigation.h"
#include "supervisor.h"
#include "shell.h"
#include "cptime.h"

#include "conf_ctrlpanel.h"
#include "conf_at45dbx.h"

#include "pm.h"
#include "adc.h"
#include "conf_board.h"
 

#include <string.h>
#include "compiler.h"
#include "FreeRTOS.h"
#include "temperature.h"
#include "sensor.h"
#include "config_file.h"
#include "fsaccess.h"

 

//----------------User Defined------------

 

#define TRUE 1
#define FALSE 0
#define true_var 1
#define TR 0x258
#define FR 0x244
#define N  0x230
#define M  0x21C
#define L  0x208
#define R  0X1F4
#define W  0X1E0

 

 

// Connection of the temperature sensor
#  define EXAMPLE_ADC_TEMPERATURE_CHANNEL     0
#  define EXAMPLE_ADC_TEMPERATURE_PIN         AVR32_ADC_AD_0_PIN
#  define EXAMPLE_ADC_TEMPERATURE_FUNCTION    AVR32_ADC_AD_0_FUNCTION

static void counter_init(void);
static void VPX_module_int(void );
static void DIP_switch_init(void);
__attribute__((__interrupt__)) void counter1_irq(void);
__attribute__((__interrupt__)) void counter2_irq(void);

#if SD_MMC_SPI_MEM == ENABLE
#include "conf_sd_mmc_spi.h"
#endif

//_____ M A C R O S ________________________________________________________

//! Priority definitions for the supervisor task.
#define mainSUPERVISOR_TASK_PRIORITY   ( tskIDLE_PRIORITY + 1 )

//_____ D E F I N I T I O N S ______________________________________________

static void prv_at45dbx_resources_init( void ); // FORWARD
#if SD_MMC_SPI_MEM == ENABLE
static void prv_sd_mmc_resources_init(void); // FORWARD
#endif
static void prv_clk_gen_start(void);        // FORWARD

void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName );

#if defined(__GNUC__)
const char *const pcCtrlPanelVersion = "GCC "__VERSION__" "__DATE__" "__TIME__" ATMEL_CONTROLPANEL_AVR32_UC3_10400"CRLF; // 1.04.00
#elif defined(__ICCAVR32__)
const char *const pcCtrlPanelVersion = "IAR "ASTRINGZ(__VER__)"G "__DATE__" "__TIME__" ATMEL_CONTROLPANEL_AVR32_UC3_10400"CRLF; // 1.04.00
#endif
/* xyyzz
   x = major version number (0-9)
   yy = minor version number (00-99)
   zz = minor release number (00-99)
*/

//_____ D E C L A R A T I O N S ____________________________________________

//!
//! \fn     main
//! \brief  1) Initialize the microcontroller and the shared hardware resources
//!         of the board.
//!         2) Launch the Ctrl Panel modules.
//!         3) Start FreeRTOS.
//! \return Should never occur.
//! \note
//!

int clockspeed;
#define sysclk_get_main_hz() BOARD_OSC0_HZ;
int main(void)
{    
    /* Enable one wait state for flash access */
    AVR32_FLASHC.fcr = AVR32_FLASHC_FWS_MASK;
    // sysclk_init();
     board_init();
     clockspeed = sysclk_get_main_hz();
     // GPIO pin/adc-function map.
     static const gpio_map_t ADC_GPIO_MAP =
     {
         {EXAMPLE_ADC_TEMPERATURE_PIN, EXAMPLE_ADC_TEMPERATURE_FUNCTION},
     };
     volatile avr32_adc_t *adc = &AVR32_ADC; // ADC IP registers address

     signed short adc_value_temp = -1;

     // Assign the on-board sensors to their ADC channel.
     unsigned short adc_channel_temp = EXAMPLE_ADC_TEMPERATURE_CHANNEL;
     
     int i;
     
     
     // switch to oscillator 0
     pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);

     // init debug serial line
     // init_dbg_rs232(FOSC0);

     // Assign and enable GPIO pins to the ADC function.
     gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));

     // configure ADC
     // Lower the ADC clock to match the ADC characteristics (because we configured
     // the CPU clock to 12MHz, and the ADC clock characteristics are usually lower;
     // cf. the ADC Characteristic section in the datasheet).
     AVR32_ADC.mr |=  0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
     adc_configure(adc);

     // Enable the ADC channels.
     
     adc_enable(adc,adc_channel_temp);
     
      // do an infinite loop
 while (true_var)    // use a volatile true variable to avoid warning on unreachable code
 {
     // slow down operations
    // for ( i=0 ; i < adc_value_pot ; i++);

 

     // display a header to user
//     print_dbg("\x1B[2J\x1B[H\r\nADC Example\r\n");

     // launch conversion on all enabled channels
     adc_start(adc);
     
     // get value for the temperature adc channel
     adc_value_temp = adc_get_value(adc, adc_channel_temp);     
     
     if ( adc_value_temp > 0x260 )
     {
         
         gpio_set_gpio_pin(LED0_GPIO);
         gpio_set_gpio_pin(LED1_GPIO);
         
     }
     
     
     if ( adc_value_temp > TR )
     {
         
         gpio_clr_gpio_pin(LED0_GPIO);
         gpio_clr_gpio_pin(LED1_GPIO);
     }
    
    
     if ( adc_value_temp < FR )
     {
         
         gpio_set_gpio_pin(LED0_GPIO);
         gpio_set_gpio_pin(LED1_GPIO);
     }
    
    
     if ( adc_value_temp > FR )
     {
         gpio_set_gpio_pin(LED2_GPIO);
         gpio_set_gpio_pin(LED3_GPIO);
     }
     
     if ( adc_value_temp > N )
     {
         gpio_clr_gpio_pin(LED2_GPIO);
         gpio_clr_gpio_pin(LED3_GPIO);
     }
     
     if ( adc_value_temp < M)
     {
         gpio_set_gpio_pin(LED2_GPIO);
         gpio_set_gpio_pin(LED3_GPIO);
     }
     
     
     
     if ( adc_value_temp > M )
     {
         gpio_set_gpio_pin(LED4_GPIO);
         gpio_set_gpio_pin(LED5_GPIO);
     }
     if ( adc_value_temp > L )
     {
         gpio_clr_gpio_pin(LED4_GPIO);
         gpio_clr_gpio_pin(LED5_GPIO);
     }
     if ( adc_value_temp < R )
     {
         gpio_set_gpio_pin(LED4_GPIO);
         gpio_set_gpio_pin(LED5_GPIO);
     }

     if ( adc_value_temp > R )
     {
         gpio_set_gpio_pin(LED6_GPIO);
         gpio_set_gpio_pin(LED7_GPIO);
     }
     if ( adc_value_temp > W )
     {
         gpio_clr_gpio_pin(LED6_GPIO);
         gpio_clr_gpio_pin(LED7_GPIO);
     }
     if ( adc_value_temp < 0x1D9)
     {
         gpio_set_gpio_pin(LED6_GPIO);
         gpio_set_gpio_pin(LED7_GPIO);
     }
    
/*
     if ( adc_value_temp > R)
     {
         gpio_set_gpio_pin(LED4_GPIO);
         
     }
     if ( adc_value_temp > L)
     {
         gpio_clr_gpio_pin(LED4_GPIO);
         
     }
     if ( adc_value_temp < M)
     {
         gpio_set_gpio_pin(LED4_GPIO);
         
     }
    
     
     
     if ( adc_value_temp > L)
     {
         gpio_set_gpio_pin(LED5_GPIO);
         
     }
     if ( adc_value_temp > M)
     {
         gpio_clr_gpio_pin(LED5_GPIO);
         
     }
     if ( adc_value_temp < N)
     {
         gpio_set_gpio_pin(LED5_GPIO);
         
     }
     

     if ( adc_value_temp > M)
     {
         gpio_set_gpio_pin(LED6_GPIO);
         
     }
     if ( adc_value_temp > N)
     {
         gpio_clr_gpio_pin(LED6_GPIO);
         
     }
     if ( adc_value_temp < 0x1D9)
     {
         gpio_set_gpio_pin(LED6_GPIO);
         
     }
     
     */
}

     
    idlecount = 0;
//    incr=0;
    counter1_30ms=OK_TIME;
    counter2_30ms=OK_TIME;
    prev_cnt1=0;
    prev_cnt2=0;
    counter_flag[CNTR1] = 0;  //IF 30ms over & ra=1
    counter_flag[CNTR2] = 0;  //IF 30ms over & ra=1
    counter_flag[CNTR3] = 0;  //IF 30ms over & ra=1
    counter_flag[CNTR4] = 0;  //IF 30ms over & ra=1

    VPX_module_int();
    DIP_switch_init();

    AVR32_GPIO.port[0].gpers = (1<<CARD_OK_LED);
    AVR32_GPIO.port[0].oders = (1<<CARD_OK_LED);
    AVR32_GPIO.port[0].ovrs  = (1<<CARD_OK_LED);

    // switch to external oscillator 0
    pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);

    // Init USB & MACB clock.
    prv_clk_gen_start();

    // Setup the LED's for output.
    LED_Off( LED0 ); LED_Off( LED1 ); LED_Off( LED2 ); LED_Off( LED3 );
    LED_Off( LED4 ); LED_Off( LED5 ); LED_Off( LED6 ); LED_Off( LED7 );
    
    // Init the time module.
    v_cptime_Init();
    
    //Init counter module for 4 fans
    counter_init();
    // 2) Launch the Control Panel supervisor task that will in turn create all the necessary tasks.
    
    vSupervisor_Start( mainSUPERVISOR_TASK_PRIORITY );
    // 3) Start FreeRTOS.Use preemptive scheduler define configUSE_PREEMPTION as 1 in portmacro.
    vTaskStartScheduler();

    /* Should never reach this point. */
    while (true);
    return 0;
}

 

 

static void prv_clk_gen_start(void)
{
    volatile avr32_pm_t *pm = &AVR32_PM;

    /** USB **/
#ifdef USB_ENABLE
    // Set PLL1 @ 96 MHz from Osc0: 12MHz*(7+1)/1 = 96MHz
    // In order to work, we need to go above 80MHz, then divide.
    pm_pll_setup(pm, 1,   // pll
            7,            // mul
            1,            // div
            0,            // osc
            16);          // lockcount

    pm_pll_set_option( pm, 1,   // pll1
            1,                  // Choose the range 80-180MHz.
            1,                  // div2
            0 );                // wbwdisable

    // Enable PLL1
    pm_pll_enable(pm, 1);

    // Wait for PLL1 locked
    pm_wait_for_pll1_locked(pm);

    // Setup USB GCLK
    pm_gc_setup(pm, AVR32_PM_GCLK_USBB, // gc
            1,                          // osc_or_pll: use Osc (if 0) or PLL (if 1)
            1,                          // pll_osc: select Osc0/PLL0 or Osc1/PLL1
            0,                          // diven
            0);                         // div

    // Enable USB GCLK
    pm_gc_enable(pm, AVR32_PM_GCLK_USBB);
#endif

    /** MACB **/
#if CP_CPU_SPEED == 60000000

    /* Setup PLL0 on OSC0, mul+1=10 ,divisor by 1, lockcount=16, ie. 12Mhzx10/1 = 120MHz output.
        Extra div by 2 => 60MHz */
    pm_pll_setup(pm,  // volatile avr32_pm_t* pm
            0,        // unsigned int pll
            9,        // unsigned int mul
            1,        // unsigned int div: Sel Osc0/PLL0 or Osc1/Pll1
            0,        // unsigned int osc
            16);      // unsigned int lockcount

    pm_pll_set_option(pm, 0,    // pll0
            1,                  // Choose the range 80-180MHz.
            1,                  // div2
            0 );                // wbwdisable

    /* Enable PLL0 */
    pm_pll_enable(pm,0);

    /* Wait for PLL0 locked. */
    pm_wait_for_pll0_locked(pm) ;
#endif

#if (CP_PBA_SPEED == 27000000) && (CP_CPU_SPEED == 54000000)
    /** Reduce speed Bus. **/
    // Set divider to 4 for PBA bus (so that PBA_SPEED == 108/2pow(sel+1) == 27),
    // to 2 for PBB and HSB (so that HSB_SPEED == 108/2pow(sel+1) == 54.
    // Note that HSB_SPEED == CPU_SPEED always.
    pm_cksel(pm,1,1,1,0,1,0);
#elif (CP_PBA_SPEED == 15000000) && (CP_CPU_SPEED == 60000000)
    /** Reduce speed Bus. **/
    // Set divider to 4 for PBA bus (so that PBA_SPEED == 60/2pow(sel+1) == 15).
    // Note that HSB_SPEED == CPU_SPEED always.
    pm_cksel(pm,1,1,0,0,0,0);
#endif

#if CP_CPU_SPEED > 33000000
    /* Add one wait state for flash management. */
    flashc_set_wait_state(1);

    /* switch to clock. */
    pm_switch_to_clock(pm, AVR32_PM_MCCTRL_MCSEL_PLL0);
#endif
}

void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed portCHAR *pcTaskName )
{
    volatile int i = 10000;
    
    LED_On(LED0 | LED1 | LED2 | LED3 | LED4 | LED5 | LED6 | LED7);
    while (1) {
        while (i--);
        LED_Toggle(LED0 | LED1 | LED2 | LED3 | LED4 | LED5 | LED6 | LED7);
        i = 10000;
    }
}

Last Edited: Thu. Aug 13, 2015 - 10:53 AM