main.c File Reference


Detailed Description

Main file of the CS2200 example.

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

Definition in file main.c.

#include "nlao_cpu.h"
#include "nlao_usart.h"
#include <stdio.h>
#include "compiler.h"
#include "preprocessor.h"
#include "board.h"
#include "print_funcs.h"
#include "flashc.h"
#include "gpio.h"
#include "intc.h"
#include "pm.h"
#include "twi.h"
#include "usart.h"
#include "main.h"
#include "conf_cs2200.h"
#include "cycle_counter.h"
#include "cs2200.h"
#include "et024006dhu.h"

Go to the source code of this file.

Functions

int _init_startup (void)
 Low-level initialization routine called during startup, before the main function.
static void init_exceptions (void)
 Initializes MCU exceptions.
static void init_hmatrix (void)
 Initializes the HSB bus matrix.
static void init_stdio (void)
 Initializes STDIO.
static void init_sys_clocks (void)
 Initializes the MCU system clocks.
static void init_twi (U32 fpba_hz)
 Initializes the two-wire interface using the internal RCOSC.
int main (void)
 Main function. Execution starts here.
static void mmi_print_line (char *string, et024006_color_t fg_color, et024006_color_t bg_color)
 Prints status information to the display and to stdio.
size_t wcstombs (char *s, const wchar_t *pwcs, size_t n)
 Replaces Newlib's implementation of the standard wcstombs function because of portability issues with wchar_t.

Variables

System Clock Frequencies
pm_freq_param_t pm_freq_param


Function Documentation

int _init_startup ( void   ) 

Low-level initialization routine called during startup, before the main function.

This version comes in replacement to the default one provided by the Newlib add-ons library. Newlib add-ons' _init_startup only calls init_exceptions, but Newlib add-ons' exception and interrupt vectors are defined in the same section and Newlib add-ons' interrupt vectors are not compatible with the interrupt management of the INTC module. More low-level initializations are besides added here.

Definition at line 256 of file main.c.

00257 {
00258   // Import the Exception Vector Base Address.
00259   extern void _evba;
00260 
00261   // Load the Exception Vector Base Address in the corresponding system register.
00262   Set_system_register(AVR32_EVBA, (int)&_evba);
00263 
00264   // Enable exceptions.
00265   Enable_global_exception();
00266 
00267   // Initialize interrupt handling.
00268   INTC_init_interrupts();
00269 
00270   // Don't-care value for GCC.
00271   return 1;
00272 }

static void init_exceptions ( void   )  [static]

Initializes MCU exceptions.

Definition at line 98 of file main.c.

00099 {
00100 #if __GNUC__ && __AVR32__
00101   // Import the Exception Vector Base Address.
00102   extern void _evba;
00103 
00104   // Load the Exception Vector Base Address in the corresponding system
00105   // register.
00106   Set_system_register(AVR32_EVBA, (int)&_evba);
00107 #endif
00108 
00109   // Enable exceptions globally.
00110   Enable_global_exception();
00111 }

static void init_hmatrix ( void   )  [static]

Initializes the HSB bus matrix.

Definition at line 116 of file main.c.

Referenced by main().

00117 {
00118   union
00119   {
00120     unsigned long                 scfg;
00121     avr32_hmatrix_scfg_t          SCFG;
00122   } u_avr32_hmatrix_scfg;
00123 
00124   // For the internal-flash HMATRIX slave, use last master as default.
00125   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH];
00126   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00127   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg;
00128 
00129   // For the internal-SRAM HMATRIX slave, use last master as default.
00130   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM];
00131   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00132   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM] = u_avr32_hmatrix_scfg.scfg;
00133 
00134   // For the USBB DPRAM HMATRIX slave, use last master as default.
00135   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_USBB_DPRAM];
00136   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00137   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_USBB_DPRAM] = u_avr32_hmatrix_scfg.scfg;
00138 
00139   // For the EBI HMATRIX slave, use last master as default.
00140   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI];
00141   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00142   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI] = u_avr32_hmatrix_scfg.scfg;
00143 }

static void init_stdio ( void   )  [static]

Initializes STDIO.

Definition at line 200 of file main.c.

References FPBA_HZ, STDIO_USART, STDIO_USART_BAUDRATE, STDIO_USART_RX_FUNCTION, STDIO_USART_RX_PIN, STDIO_USART_TX_FUNCTION, and STDIO_USART_TX_PIN.

Referenced by main().

00201 {
00202 #if __GNUC__ && __AVR32__
00203 
00204   static const gpio_map_t STDIO_USART_GPIO_MAP =
00205   {
00206     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00207     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00208   };
00209 
00210   // Initialize the USART used for STDIO.
00211   set_usart_base((void *)STDIO_USART);
00212   gpio_enable_module(STDIO_USART_GPIO_MAP,
00213                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00214   usart_init(STDIO_USART_BAUDRATE);
00215 
00216 #elif __ICCAVR32__
00217 
00218   static const gpio_map_t STDIO_USART_GPIO_MAP =
00219   {
00220     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00221     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00222   };
00223 
00224   static const usart_options_t STDIO_USART_OPTIONS =
00225   {
00226     .baudrate     = STDIO_USART_BAUDRATE,
00227     .charlength   = 8,
00228     .paritytype   = USART_NO_PARITY,
00229     .stopbits     = USART_1_STOPBIT,
00230     .channelmode  = USART_NORMAL_CHMODE
00231   };
00232 
00233   // Initialize the USART used for STDIO.
00234   extern volatile avr32_usart_t *volatile stdio_usart_base;
00235   stdio_usart_base = STDIO_USART;
00236   gpio_enable_module(STDIO_USART_GPIO_MAP,
00237                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00238   usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, FPBA_HZ);
00239 
00240 #endif
00241 }

static void init_sys_clocks ( void   )  [static]

Initializes the MCU system clocks.

Definition at line 148 of file main.c.

References FPBA_HZ.

Referenced by main().

00149 {
00150   // Switch to OSC0 to speed up the booting
00151   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00152 
00153   // Start oscillator1
00154   pm_enable_osc1_crystal(&AVR32_PM, FOSC0);
00155   //
00156   pm_enable_clk1(&AVR32_PM, OSC0_STARTUP);
00157 
00158   // Set PLL0 (fed from OSC0 = 12 MHz) to 132 MHz
00159   pm_pll_setup(&AVR32_PM, 0,  // pll.
00160       10,  // mul.
00161       1,   // div.
00162       0,   // osc.
00163       16); // lockcount.
00164 
00165   // Set PLL operating range and divider (fpll = fvco/2)
00166   // -> PLL0 output = 66 MHz
00167   pm_pll_set_option(&AVR32_PM, 0, // pll.
00168       1,  // pll_freq.
00169       1,  // pll_div2.
00170       0); // pll_wbwdisable.
00171 
00172   // start PLL0 and wait for the lock
00173   pm_pll_enable(&AVR32_PM, 0);
00174   pm_wait_for_pll0_locked(&AVR32_PM);
00175   // Set all peripheral clocks torun at master clock rate
00176   pm_cksel(&AVR32_PM,
00177       0,   // pbadiv.
00178       0,   // pbasel.
00179       0,   // pbbdiv.
00180       0,   // pbbsel.
00181       0,   // hsbdiv.
00182       0);  // hsbsel.
00183 
00184   // Set one waitstate for the flash
00185   flashc_set_wait_state(1);
00186 
00187   // Switch to PLL0 as the master clock
00188   pm_switch_to_clock(&AVR32_PM, AVR32_PM_MCCTRL_MCSEL_PLL0);
00189 
00190 
00191 #if __GNUC__ && __AVR32__
00192   // Give the used PBA clock frequency to Newlib Addon library, so it can work properly.
00193   set_cpu_hz(FPBA_HZ);
00194 #endif
00195 }

static void init_twi ( U32  fpba_hz  )  [static]

Initializes the two-wire interface using the internal RCOSC.

Definition at line 296 of file main.c.

References CS2200_TWI, CS2200_TWI_MASTER_SPEED, and CS2200_TWI_SLAVE_ADDRESS.

Referenced by main().

00297 {
00298   volatile U32 i;
00299 
00300   static const gpio_map_t CS2200_TWI_GPIO_MAP =
00301   {
00302     {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION},
00303     {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION}
00304   };
00305 
00306   static twi_options_t CS2200_TWI_OPTIONS =
00307   {
00308     .speed  = CS2200_TWI_MASTER_SPEED,
00309     .chip   = CS2200_TWI_SLAVE_ADDRESS
00310   };
00311   CS2200_TWI_OPTIONS.pba_hz = fpba_hz;
00312 
00313   gpio_enable_module(CS2200_TWI_GPIO_MAP,
00314                      sizeof(CS2200_TWI_GPIO_MAP) / sizeof(CS2200_TWI_GPIO_MAP[0]));
00315   twi_master_init(CS2200_TWI, &CS2200_TWI_OPTIONS);
00316 
00317 }

int main ( void   ) 

Main function. Execution starts here.

Return values:
42 Fatal error.

Definition at line 324 of file main.c.

References _32_BITS_RATIO, cs2200_freq_clk_adjust(), cs2200_freq_clk_out(), cs2200_setup(), FCPU_HZ, FPBA_HZ, init_hmatrix(), init_stdio(), init_sys_clocks(), init_twi(), mmi_print_line(), pm_freq_param, and PPM.

00325 {
00326   U32 iter=0;
00327   U32 cs2200_out_freq=11289600;
00328   static Bool b_sweep_up=TRUE;
00329   static U32 freq_step=0;
00330 
00331   // Initialize the TWI using the internal RCOSC
00332   init_twi(AVR32_PM_RCOSC_FREQUENCY);
00333 
00334   // Initialize the CS2200 and produce a default 12MHz frequency.
00335   cs2200_setup(11289600);
00336 
00337   // Initializes the MCU system clocks
00338   init_sys_clocks();
00339 
00340   // Initialize the TWI
00341   init_twi(FPBA_HZ);
00342 
00343   // Set CPU and PBA clock
00344   if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00345      return 42;
00346 
00347   // Initialize the USART for debug purpose.
00348   init_stdio();
00349 
00350   // Initialize the HMatrix.
00351   init_hmatrix();
00352 
00353   // Initialize USART link.
00354   init_dbg_rs232(pm_freq_param.pba_f);
00355   
00356   // Initialize the LCD.
00357   et024006_Init( pm_freq_param.cpu_f, pm_freq_param.cpu_f /*HSB*/);
00358 
00359   // Clear the display i.e. make it black
00360   et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK );
00361 
00362   // Set the backlight.
00363   gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00364 
00365   mmi_print_line("CS2200 example", WHITE, BLACK);
00366 
00367   // Generate a 12.288 MHz frequency out of the CS2200.
00368   mmi_print_line("Output 12.288 MHz", WHITE, BLACK);
00369   cs2200_freq_clk_out(_32_BITS_RATIO(12288000));
00370   cpu_delay_ms( 10000, FCPU_HZ);
00371 
00372   // Generate a 11.2896 MHz frequency out of the CS2200.
00373   mmi_print_line("Output 11.2896 MHz", WHITE, BLACK);
00374   cs2200_freq_clk_out(_32_BITS_RATIO(cs2200_out_freq));
00375   cpu_delay_ms( 10000, FCPU_HZ);
00376 
00377   mmi_print_line("Sweep from 11.2896 MHz steps of 100 PPM", WHITE, BLACK);
00378   freq_step = PPM(cs2200_out_freq, 100);
00379 
00380   while(1)
00381   {
00382     U32 ratio;
00383  
00384     if(b_sweep_up)
00385     {
00386       if( iter<=10 )
00387       {
00388         mmi_print_line("Add 100 PPM", WHITE, BLACK);
00389         iter++;
00390         cs2200_out_freq += freq_step;
00391         ratio = _32_BITS_RATIO(cs2200_out_freq);
00392         cs2200_freq_clk_adjust((U16)ratio);
00393         cpu_delay_ms( 1000, FCPU_HZ);
00394         while( twi_is_busy() ); 
00395       }
00396       else
00397         b_sweep_up=FALSE;
00398     }
00399 
00400     if(!b_sweep_up)
00401     {
00402       if( iter>0 )
00403       {
00404         mmi_print_line("Sub 100 PPM", WHITE, BLACK);
00405         iter--;
00406         cs2200_out_freq -= freq_step;
00407         ratio = _32_BITS_RATIO(cs2200_out_freq);
00408         cs2200_freq_clk_adjust((U16)ratio);
00409         cpu_delay_ms( 1000, FCPU_HZ);
00410         while( twi_is_busy() ); 
00411       }
00412       else
00413         b_sweep_up=TRUE;
00414     }
00415   }
00416 }

static void mmi_print_line ( char *  string,
et024006_color_t  fg_color,
et024006_color_t  bg_color 
) [static]

Prints status information to the display and to stdio.

Definition at line 420 of file main.c.

Referenced by main().

00421 {
00422     static U8 current_line = 1;
00423     static U8 clear_lines = 0;
00424 
00425     if(clear_lines || current_line == 23)
00426     {
00427       et024006_DrawFilledRect(10, (current_line) * 10, 320, 10, bg_color);
00428       clear_lines = 1;
00429     }
00430 
00431     et024006_PrintString(string, (const unsigned char*) &FONT6x8,
00432                          10, 10 * current_line, fg_color, bg_color);
00433     
00434     current_line = current_line % 23;
00435     current_line++;
00436 
00437     print_dbg(string);
00438     print_dbg("\n");
00439 }

size_t wcstombs ( char *  s,
const wchar_t *  pwcs,
size_t  n 
)

Replaces Newlib's implementation of the standard wcstombs function because of portability issues with wchar_t.

Warning:
This function must be used with the `-fshort-wchar' code generation option.

Definition at line 79 of file main.c.

00080 {
00081   size_t count = 0;
00082 
00083   while (n--)
00084   {
00085     if ((*s++ = (char)*pwcs++) == '\0')
00086       break;
00087     count++;
00088   }
00089 
00090   return count;
00091 }


Variable Documentation

pm_freq_param_t pm_freq_param

Initial value:

{
   .cpu_f  =       FCPU_HZ
,  .pba_f    =     FPBA_HZ
,  .osc0_f     =   FOSC0
,  .osc0_startup = OSC0_STARTUP
}

Definition at line 59 of file main.c.

Referenced by main().


Generated on Thu Dec 17 19:57:30 2009 for AVR32 - CLOCK_SYNTHESIZER CS2200 by  doxygen 1.5.5