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.
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 287 of file main.c.

00288 {
00289   // Import the Exception Vector Base Address.
00290   extern void _evba;
00291 
00292   // Load the Exception Vector Base Address in the corresponding system register.
00293   Set_system_register(AVR32_EVBA, (int)&_evba);
00294 
00295   // Enable exceptions.
00296   Enable_global_exception();
00297 
00298   // Initialize interrupt handling.
00299   INTC_init_interrupts();
00300 
00301   // Don't-care value for GCC.
00302   return 1;
00303 }

static void init_exceptions ( void   )  [static]

Initializes MCU exceptions.

Definition at line 129 of file main.c.

00130 {
00131 #if __GNUC__ && __AVR32__
00132   // Import the Exception Vector Base Address.
00133   extern void _evba;
00134 
00135   // Load the Exception Vector Base Address in the corresponding system
00136   // register.
00137   Set_system_register(AVR32_EVBA, (int)&_evba);
00138 #endif
00139 
00140   // Enable exceptions globally.
00141   Enable_global_exception();
00142 }

static void init_hmatrix ( void   )  [static]

Initializes the HSB bus matrix.

Definition at line 147 of file main.c.

Referenced by main().

00148 {
00149   union
00150   {
00151     unsigned long                 scfg;
00152     avr32_hmatrix_scfg_t          SCFG;
00153   } u_avr32_hmatrix_scfg;
00154 
00155   // For the internal-flash HMATRIX slave, use last master as default.
00156   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH];
00157   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00158   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_FLASH] = u_avr32_hmatrix_scfg.scfg;
00159 
00160   // For the internal-SRAM HMATRIX slave, use last master as default.
00161   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM];
00162   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00163   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_SRAM] = u_avr32_hmatrix_scfg.scfg;
00164 
00165   // For the USBB DPRAM HMATRIX slave, use last master as default.
00166   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_USBB_DPRAM];
00167   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00168   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_USBB_DPRAM] = u_avr32_hmatrix_scfg.scfg;
00169 
00170   // For the EBI HMATRIX slave, use last master as default.
00171   u_avr32_hmatrix_scfg.scfg = AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI];
00172   u_avr32_hmatrix_scfg.SCFG.defmstr_type = AVR32_HMATRIX_DEFMSTR_TYPE_LAST_DEFAULT;
00173   AVR32_HMATRIX.scfg[AVR32_HMATRIX_SLAVE_EBI] = u_avr32_hmatrix_scfg.scfg;
00174 }

static void init_stdio ( void   )  [static]

Initializes STDIO.

Definition at line 231 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().

00232 {
00233 #if __GNUC__ && __AVR32__
00234 
00235   static const gpio_map_t STDIO_USART_GPIO_MAP =
00236   {
00237     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00238     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00239   };
00240 
00241   // Initialize the USART used for STDIO.
00242   set_usart_base((void *)STDIO_USART);
00243   gpio_enable_module(STDIO_USART_GPIO_MAP,
00244                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00245   usart_init(STDIO_USART_BAUDRATE);
00246 
00247 #elif __ICCAVR32__
00248 
00249   static const gpio_map_t STDIO_USART_GPIO_MAP =
00250   {
00251     {STDIO_USART_RX_PIN, STDIO_USART_RX_FUNCTION},
00252     {STDIO_USART_TX_PIN, STDIO_USART_TX_FUNCTION}
00253   };
00254 
00255   static const usart_options_t STDIO_USART_OPTIONS =
00256   {
00257     .baudrate     = STDIO_USART_BAUDRATE,
00258     .charlength   = 8,
00259     .paritytype   = USART_NO_PARITY,
00260     .stopbits     = USART_1_STOPBIT,
00261     .channelmode  = USART_NORMAL_CHMODE
00262   };
00263 
00264   // Initialize the USART used for STDIO.
00265   extern volatile avr32_usart_t *volatile stdio_usart_base;
00266   stdio_usart_base = STDIO_USART;
00267   gpio_enable_module(STDIO_USART_GPIO_MAP,
00268                      sizeof(STDIO_USART_GPIO_MAP) / sizeof(STDIO_USART_GPIO_MAP[0]));
00269   usart_init_rs232(STDIO_USART, &STDIO_USART_OPTIONS, FPBA_HZ);
00270 
00271 #endif
00272 }

static void init_sys_clocks ( void   )  [static]

Initializes the MCU system clocks.

Definition at line 179 of file main.c.

References FPBA_HZ.

Referenced by main().

00180 {
00181   // Switch to OSC0 to speed up the booting
00182   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00183 
00184   // Start oscillator1
00185   pm_enable_osc1_crystal(&AVR32_PM, FOSC0);
00186   //
00187   pm_enable_clk1(&AVR32_PM, OSC0_STARTUP);
00188 
00189   // Set PLL0 (fed from OSC0 = 12 MHz) to 132 MHz
00190   pm_pll_setup(&AVR32_PM, 0,  // pll.
00191       10,  // mul.
00192       1,   // div.
00193       0,   // osc.
00194       16); // lockcount.
00195 
00196   // Set PLL operating range and divider (fpll = fvco/2)
00197   // -> PLL0 output = 66 MHz
00198   pm_pll_set_option(&AVR32_PM, 0, // pll.
00199       1,  // pll_freq.
00200       1,  // pll_div2.
00201       0); // pll_wbwdisable.
00202 
00203   // start PLL0 and wait for the lock
00204   pm_pll_enable(&AVR32_PM, 0);
00205   pm_wait_for_pll0_locked(&AVR32_PM);
00206   // Set all peripheral clocks torun at master clock rate
00207   pm_cksel(&AVR32_PM,
00208       0,   // pbadiv.
00209       0,   // pbasel.
00210       0,   // pbbdiv.
00211       0,   // pbbsel.
00212       0,   // hsbdiv.
00213       0);  // hsbsel.
00214 
00215   // Set one waitstate for the flash
00216   flashc_set_wait_state(1);
00217 
00218   // Switch to PLL0 as the master clock
00219   pm_switch_to_clock(&AVR32_PM, AVR32_PM_MCCTRL_MCSEL_PLL0);
00220 
00221 
00222 #if __GNUC__ && __AVR32__
00223   // Give the used PBA clock frequency to Newlib Addon library, so it can work properly.
00224   set_cpu_hz(FPBA_HZ);
00225 #endif
00226 }

static void init_twi ( U32  fpba_hz  )  [static]

Initializes the two-wire interface.

Definition at line 327 of file main.c.

References CS2200_TWI, CS2200_TWI_MASTER_SPEED, and CS2200_TWI_SLAVE_ADDRESS.

Referenced by main().

00328 {
00329   volatile U32 i;
00330 
00331   static const gpio_map_t CS2200_TWI_GPIO_MAP =
00332   {
00333     {AVR32_TWI_SCL_0_0_PIN, AVR32_TWI_SCL_0_0_FUNCTION},
00334     {AVR32_TWI_SDA_0_0_PIN, AVR32_TWI_SDA_0_0_FUNCTION}
00335   };
00336 
00337   static twi_options_t CS2200_TWI_OPTIONS =
00338   {
00339     .speed  = CS2200_TWI_MASTER_SPEED,
00340     .chip   = CS2200_TWI_SLAVE_ADDRESS
00341   };
00342   CS2200_TWI_OPTIONS.pba_hz = fpba_hz;
00343 
00344   gpio_enable_module(CS2200_TWI_GPIO_MAP,
00345                      sizeof(CS2200_TWI_GPIO_MAP) / sizeof(CS2200_TWI_GPIO_MAP[0]));
00346   twi_master_init(CS2200_TWI, &CS2200_TWI_OPTIONS);
00347 }

int main ( void   ) 

Main function. Execution starts here.

Return values:
42 Fatal error.

Definition at line 354 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.

00355 {
00356   U32 iter=0;
00357   U32 cs2200_out_freq=11289600;
00358   static Bool b_sweep_up=TRUE;
00359   static U32 freq_step=0;
00360 
00361   // Initialize the TWI using the internal RCOSC
00362   init_twi(AVR32_PM_RCOSC_FREQUENCY);
00363 
00364   // Initialize the CS2200 and produce a default 12MHz frequency.
00365   cs2200_setup(11289600);
00366 
00367   // Initializes the MCU system clocks
00368   init_sys_clocks();
00369 
00370   // Initialize the TWI
00371   init_twi(FPBA_HZ);
00372 
00373   // Set CPU and PBA clock
00374   if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00375      return 42;
00376 
00377   // Initialize the USART for debug purpose.
00378   init_stdio();
00379 
00380   // Initialize the HMatrix.
00381   init_hmatrix();
00382 
00383   // Initialize USART link.
00384   init_dbg_rs232(pm_freq_param.pba_f);
00385   
00386   // Initialize the LCD.
00387   et024006_Init( pm_freq_param.cpu_f, pm_freq_param.cpu_f /*HSB*/);
00388 
00389   // Clear the display i.e. make it black
00390   et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK );
00391 
00392   // Set the backlight.
00393   gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00394 
00395   mmi_print_line("CS2200 example", WHITE, BLACK);
00396 
00397   // Generate a 12.288 MHz frequency out of the CS2200.
00398   mmi_print_line("Output 12.288 MHz", WHITE, BLACK);
00399   cs2200_freq_clk_out(_32_BITS_RATIO(12288000));
00400   cpu_delay_ms( 10000, FCPU_HZ);
00401 
00402   // Generate a 11.2896 MHz frequency out of the CS2200.
00403   mmi_print_line("Output 11.2896 MHz", WHITE, BLACK);
00404   cs2200_freq_clk_out(_32_BITS_RATIO(cs2200_out_freq));
00405   cpu_delay_ms( 10000, FCPU_HZ);
00406 
00407   mmi_print_line("Sweep from 11.2896 MHz steps of 100 PPM", WHITE, BLACK);
00408   freq_step = PPM(cs2200_out_freq, 100);
00409 
00410   while(1)
00411   {
00412     U32 ratio;
00413  
00414     if(b_sweep_up)
00415     {
00416       if( iter<=10 )
00417       {
00418         mmi_print_line("Add 100 PPM", WHITE, BLACK);
00419         iter++;
00420         cs2200_out_freq += freq_step;
00421         ratio = _32_BITS_RATIO(cs2200_out_freq);
00422         cs2200_freq_clk_adjust((U16)ratio);
00423         cpu_delay_ms( 1000, FCPU_HZ);
00424         while( twi_is_busy() ); 
00425       }
00426       else
00427         b_sweep_up=FALSE;
00428     }
00429 
00430     if(!b_sweep_up)
00431     {
00432       if( iter>0 )
00433       {
00434         mmi_print_line("Sub 100 PPM", WHITE, BLACK);
00435         iter--;
00436         cs2200_out_freq -= freq_step;
00437         ratio = _32_BITS_RATIO(cs2200_out_freq);
00438         cs2200_freq_clk_adjust((U16)ratio);
00439         cpu_delay_ms( 1000, FCPU_HZ);
00440         while( twi_is_busy() ); 
00441       }
00442       else
00443         b_sweep_up=TRUE;
00444     }
00445   }
00446 }

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 450 of file main.c.

Referenced by main().

00451 {
00452     static U8 current_line = 1;
00453     static U8 clear_lines = 0;
00454 
00455     if(clear_lines || current_line == 23)
00456     {
00457       et024006_DrawFilledRect(10, (current_line) * 10, 320, 10, bg_color);
00458       clear_lines = 1;
00459     }
00460 
00461     et024006_PrintString(string, (const unsigned char*) &FONT6x8,
00462                          10, 10 * current_line, fg_color, bg_color);
00463     
00464     current_line = current_line % 23;
00465     current_line++;
00466 
00467     print_dbg(string);
00468     print_dbg("\n");
00469 }

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 110 of file main.c.

00111 {
00112   size_t count = 0;
00113 
00114   while (n--)
00115   {
00116     if ((*s++ = (char)*pwcs++) == '\0')
00117       break;
00118     count++;
00119   }
00120 
00121   return count;
00122 }


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 90 of file main.c.

Referenced by main().


Generated on Fri Feb 19 02:23:29 2010 for AVR32 - CLOCK_SYNTHESIZER CS2200 by  doxygen 1.5.5