ast_example1.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_example1.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_example1.c.

#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ   12000000

Definition at line 118 of file ast_example1.c.

#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   12000000

Definition at line 119 of file ast_example1.c.

#define EXAMPLE_USART   (&AVR32_USART3)

Definition at line 112 of file ast_example1.c.

Referenced by main().

#define EXAMPLE_USART_RX_FUNCTION   AVR32_USART3_RXD_0_0_FUNCTION

Definition at line 114 of file ast_example1.c.

Referenced by main().

#define EXAMPLE_USART_RX_PIN   AVR32_USART3_RXD_0_0_PIN

Definition at line 113 of file ast_example1.c.

Referenced by main().

#define EXAMPLE_USART_TX_FUNCTION   AVR32_USART3_TXD_0_0_FUNCTION

Definition at line 116 of file ast_example1.c.

Referenced by main().

#define EXAMPLE_USART_TX_PIN   AVR32_USART3_TXD_0_0_PIN

Definition at line 115 of file ast_example1.c.

Referenced by main().

#define FPBA   EXAMPLE_TARGET_PBACLK_FREQ_HZ

Definition at line 120 of file ast_example1.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_example1.c.

References ast_enable(), ast_get_calendar_value(), ast_init_calendar(), AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, 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 
00171   static const gpio_map_t USART_GPIO_MAP =
00172   {
00173     {EXAMPLE_USART_RX_PIN, EXAMPLE_USART_RX_FUNCTION},
00174     {EXAMPLE_USART_TX_PIN, EXAMPLE_USART_TX_FUNCTION}
00175   };
00176 
00177   // USART options
00178   static const usart_options_t USART_OPTIONS =
00179   {
00180     .baudrate     = 57600,
00181     .charlength   = 8,
00182     .paritytype   = USART_NO_PARITY,
00183     .stopbits     = USART_1_STOPBIT,
00184     .channelmode  = 0
00185   };
00186 
00187 #if BOARD == UC3L_EK
00188   scif_osc32_opt_t  opt = {
00189     SCIF_OSC_MODE_2PIN_CRYSTAL_HICUR, // 2-pin Crystal and high current mode. Crystal is connected to XIN32/XOUT32.
00190     AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC,                    // oscillator startup time
00191     true,                             // select the alternate xin32_2 and xout32_2 for the 32kHz crystal oscillator
00192     false,                            // disable the 1kHz output
00193     true                              // enable the 32kHz output
00194   };
00195   
00196 #else
00197   scif_osc32_opt_t opt;
00198   opt.mode = SCIF_OSC_MODE_2PIN_CRYSTAL;
00199   opt.startup = AVR32_SCIF_OSCCTRL32_STARTUP_0_RCOSC;
00200 #endif  
00201 
00202 #if BOARD == UC3L_EK
00203   // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected
00204   // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the
00205   // main clock source to the DFLL.
00206   pcl_configure_clocks(&pcl_dfll_freq_param);
00207   // Note: since it is dynamically computing the appropriate field values of the
00208   // configuration registers from the parameters structure, this function is not
00209   // optimal in terms of code size. For a code size optimal solution, it is better
00210   // to create a new function from pcl_configure_clocks_dfll0() and modify it
00211   // to use preprocessor computation from pre-defined target frequencies.
00212 #else
00213   pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00214 #endif
00215 
00216   // Start OSC_32KHZ
00217   scif_start_osc32(&opt,true);
00218 
00219   // Assign GPIO pins to USART0.
00220   gpio_enable_module(USART_GPIO_MAP,
00221                      sizeof(USART_GPIO_MAP) / sizeof(USART_GPIO_MAP[0]));
00222 
00223   // Initialize USART in RS232 mode
00224   usart_init_rs232(EXAMPLE_USART, &USART_OPTIONS, FPBA);
00225 
00226   // Welcome message
00227   usart_write_line(EXAMPLE_USART, "\x1B[2J\x1B[H\r\nATMEL\r\n");
00228   usart_write_line(EXAMPLE_USART, "AVR32 UC3 - AST example\r\n");
00229 
00230   usart_write_line(EXAMPLE_USART, "AST 32 KHz oscillator program test.\r\n");
00231 
00232   ast_calendar_t ast_calendar;
00233   ast_calendar.FIELD.sec  = 0;
00234   ast_calendar.FIELD.min  = 15;
00235   ast_calendar.FIELD.hour = 12;
00236   ast_calendar.FIELD.day  = 5;
00237   ast_calendar.FIELD.month= 6;
00238   ast_calendar.FIELD.year = 9;    
00239   
00240   // Initialize the AST
00241   if (!ast_init_calendar(&AVR32_AST, AST_OSC_32KHZ, AST_PSEL_32KHZ_1HZ, ast_calendar))
00242   {
00243     usart_write_line(EXAMPLE_USART, "Error initializing the AST\r\n");
00244     while(1);
00245   }
00246   // Enable the AST
00247   ast_enable(&AVR32_AST);
00248   
00249   volatile int i;
00250   while(1)
00251   {
00252       // slow down operations
00253       for ( i=0 ; i < 10000 ; i++);  
00254       gpio_tgl_gpio_pin(LED0_GPIO);
00255       
00256       // Set cursor to the position (1; 5)
00257       usart_write_line(EXAMPLE_USART, "\x1B[5;1H");
00258       ast_calendar = ast_get_calendar_value(&AVR32_AST);
00259       usart_write_line(EXAMPLE_USART, "Timer: ");
00260       ptemp = print_i(temp, ast_calendar.FIELD.sec);
00261       usart_write_line(EXAMPLE_USART, ptemp);
00262       usart_write_line(EXAMPLE_USART, " sec ");
00263   }
00264 }

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_example1.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_example1.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_example1.c.

Referenced by main().


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