ast_example2.c File Reference


Detailed Description

AST example driver for AVR32 UC3.

This file provides an example for the AST on AVR32 UC3 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file ast_example2.c.

#include <avr32/io.h>
#include "intc.h"
#include "board.h"
#include "compiler.h"
#include "ast.h"
#include "usart.h"
#include "gpio.h"
#include "power_clocks_lib.h"

Go to the source code of this file.

Defines

USART Settings
#define EXAMPLE_TARGET_DFLL_FREQ_HZ   96000000
#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ   12000000
#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   12000000
#define EXAMPLE_USART   (&AVR32_USART3)
#define EXAMPLE_USART_RX_FUNCTION   AVR32_USART3_RXD_0_0_FUNCTION
#define EXAMPLE_USART_RX_PIN   AVR32_USART3_RXD_0_0_PIN
#define EXAMPLE_USART_TX_FUNCTION   AVR32_USART3_TXD_0_0_FUNCTION
#define EXAMPLE_USART_TX_PIN   AVR32_USART3_TXD_0_0_PIN
#define FPBA   EXAMPLE_TARGET_PBACLK_FREQ_HZ

Functions

int main (void)
 main function : do init and loop (poll if configured so)
char * print_i (char *str, int n)
 print_i function : convert the given number to an ASCII decimal representation.

Variables

Parameters to pcl_configure_clocks().
static scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF }
static pcl_freq_param_t pcl_dfll_freq_param


Define Documentation

#define EXAMPLE_TARGET_DFLL_FREQ_HZ   96000000

Definition at line 117 of file ast_example2.c.

#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ   12000000

Definition at line 118 of file ast_example2.c.

#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   12000000

Definition at line 119 of file ast_example2.c.

#define EXAMPLE_USART   (&AVR32_USART3)

Definition at line 112 of file ast_example2.c.

Referenced by main().

#define EXAMPLE_USART_RX_FUNCTION   AVR32_USART3_RXD_0_0_FUNCTION

Definition at line 114 of file ast_example2.c.

Referenced by main().

#define EXAMPLE_USART_RX_PIN   AVR32_USART3_RXD_0_0_PIN

Definition at line 113 of file ast_example2.c.

Referenced by main().

#define EXAMPLE_USART_TX_FUNCTION   AVR32_USART3_TXD_0_0_FUNCTION

Definition at line 116 of file ast_example2.c.

Referenced by main().

#define EXAMPLE_USART_TX_PIN   AVR32_USART3_TXD_0_0_PIN

Definition at line 115 of file ast_example2.c.

Referenced by main().

#define FPBA   EXAMPLE_TARGET_PBACLK_FREQ_HZ

Definition at line 120 of file ast_example2.c.

Referenced by main().


Function Documentation

int main ( void   ) 

main function : do init and loop (poll if configured so)

Definition at line 166 of file ast_example2.c.

References ast_disable_alarm0(), ast_enable(), ast_enable_alarm0(), ast_get_counter_value(), ast_init_counter(), AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_set_alarm0_value(), EXAMPLE_USART, EXAMPLE_USART_RX_FUNCTION, EXAMPLE_USART_RX_PIN, EXAMPLE_USART_TX_FUNCTION, EXAMPLE_USART_TX_PIN, ast_calendar_t::FIELD, FPBA, pcl_dfll_freq_param, and print_i().

00167 {
00168   char temp[20];
00169   char *ptemp;
00170   ast_calendar_t ast_alarm;
00171 
00172   static const gpio_map_t USART_GPIO_MAP =
00173   {
00174     {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
00175     {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
00176   };
00177 
00178   // USART options
00179   static const usart_options_t USART_OPTIONS =
00180   {
00181     .baudrate     = 57600,
00182     .charlength   = 8,
00183     .paritytype   = USART_NO_PARITY,
00184     .stopbits     = USART_1_STOPBIT,
00185     .channelmode  = 0
00186   };
00187 
00188 #if BOARD == UC3L_EK
00189   scif_osc32_opt_t  opt = {
00190     SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR, // 2-pin Crystal and high current mode. Crystal is connected to XIN32/XOUT32.
00191     AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC,                    // oscillator startup time
00192     true,                             // select the alternate xin32_2 and xout32_2 for the 32kHz crystal oscillator
00193     false,                            // disable the 1kHz output
00194     true                              // enable the 32kHz output
00195   };
00196   
00197 #else
00198   scif_osc32_opt_t opt;
00199   opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL;
00200   opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC;
00201 #endif
00202 
00203 
00204 #if BOARD == UC3L_EK
00205   // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected
00206   // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the
00207   // main clock source to the DFLL.
00208   pcl_configure_clocks(&pcl_dfll_freq_param);
00209   // Note: since it is dynamically computing the appropriate field values of the
00210   // configuration registers from the parameters structure, this function is not
00211   // optimal in terms of code size. For a code size optimal solution, it is better
00212   // to create a new function from pcl_configure_clocks_dfll0() and modify it
00213   // to use preprocessor computation from pre-defined target frequencies.
00214 #else
00215   pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00216 #endif
00217 
00218   // Start OSC_32KHZ
00219   scif_start_osc32(&opt,true);
00220   
00221   // Assign GPIO pins to USART0.
00222   gpio_enable_module(USART_GPIO_MAP,
00223                      sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00224 
00225   // Initialize USART in RS232 mode
00226   usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA);
00227 
00228   // Welcome sentence
00229   usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n");
00230   usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example 2\r\n");
00231   usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator counter example.\r\n");
00232   usart_write_line(EXAMPLE_USART, "Alarm0 wakeup from static sleep mode every second.\r\n");
00233 
00234   // Unsing counter mode and set it to 0
00235   unsigned long ast_counter = 0;
00236   
00237   // Initialize the AST
00238   if (!ast_init_counter(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_counter))
00239   {
00240     usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n");
00241     while(1);
00242   }
00243   
00244   // Alarm 0 sends a wakeup signal to the Power manager
00245   AVR32_AST.WER.alarm0=1;
00246  
00247   // Enable the AST
00248   ast_enable(&AVR32_AST);
00249    
00250   while(1)
00251   {
00252       // disable alarm 0
00253       ast_disable_alarm0(&AVR32_AST);
00254     
00255       //ast_init_counter Set Alarm to current time+30 seconds
00256       ast_alarm.FIELD.sec = ast_alarm.FIELD.sec+1;
00257       ast_set_alarm0_value(&AVR32_AST,ast_alarm);
00258       
00259       // Enable alarm 0
00260       ast_enable_alarm0(&AVR32_AST);
00261 
00262       // Go into static sleep mode 
00263       SLEEP(AVR32_PM_SMODE_STATIC);      
00264       
00265       // After wake up, clear the Alarm0
00266       AVR32_AST.SCR.alarm0=1;
00267 
00268       // Toggle Led0
00269       gpio_tgl_gpio_pin(LED0_GPIO);
00270       
00271       // Set cursor to the position (1; 6)
00272       usart_write_line(EXAMPLE_USART, "\x1B[6;1H");
00273       ast_counter = ast_get_counter_value(&AVR32_AST);
00274       usart_write_line(EXAMPLE_USART, "Timer: ");
00275       ptemp = print_i(temp, ast_counter);
00276       usart_write_line(EXAMPLE_USART, ptemp);
00277       usart_write_line(EXAMPLE_USART, " sec ");
00278   }
00279 }

char* print_i ( char *  str,
int  n 
)

print_i function : convert the given number to an ASCII decimal representation.

Definition at line 149 of file ast_example2.c.

Referenced by main().

00150 {
00151   int i = 10;
00152 
00153   str[i] = '\0';
00154   do
00155   {
00156     str[--i] = '0' + n%10;
00157     n /= 10;
00158   }while(n);
00159 
00160   return &str[i];
00161 }


Variable Documentation

scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF } [static]

Definition at line 124 of file ast_example2.c.

pcl_freq_param_t pcl_dfll_freq_param [static]

Initial value:

    {
      .main_clk_src = PCL_MC_DFLL0,
      .cpu_f        = EXAMPLE_TARGET_MCUCLK_FREQ_HZ,
      .pba_f        = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
      .pbb_f        = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
      .dfll_f       = EXAMPLE_TARGET_DFLL_FREQ_HZ,
      .pextra_params = &gc_dfllif_ref_opt
    }

Definition at line 125 of file ast_example2.c.

Referenced by main().


Generated on Fri Feb 19 02:24:16 2010 for AVR32 - AST Driver Example 2 by  doxygen 1.5.5