usart_example2.c

Go to the documentation of this file.
00001 #include <avr32/io.h>
00002 
00003 #include "usart.h"
00004 #include "../pio/pio.h"
00005 #include "../debug/print_funcs.h"
00006 #include "../inc/macro.h"
00007 #include "../power_manager/pm.h"
00008 #include "../pdc/pdc.h"
00009 
00010 /*
00011 Huskeliste for å forandre innstans:
00012 - USART_ADDR må forandres
00013 - PIOMAP må oppdateres
00014 - Riktig usart modul på apba
00015 */
00016 
00017 #define USART_ADDR AVR32_USART2_ADDRESS;
00018 #define APBA_USART_INSTANCE PM_APBA_USART2
00019 /* Prototype */
00020 int print_pm_stats(volatile avr32_usart_t * usart);
00021 
00022 int main( void )
00023 {
00024   volatile avr32_usart_t * usart = (void *) USART_ADDR;
00025   struct usart_options_t usart_opt;
00026   struct pll_opt_t pll_opt;
00027 
00028   struct clk_sel_opt_t cpu_opt;
00029   struct clk_sel_opt_t ahb_opt;
00030   struct clk_sel_opt_t apba_opt;
00031   struct clk_sel_opt_t apbb_opt;
00032 
00033   avr32_piomap_t usart_piomap = {                               \
00034     {AVR32_USART2_RXD_0_PIN, AVR32_USART2_RXD_0_FUNCTION},      \
00035     {AVR32_USART2_TXD_0_PIN, AVR32_USART2_TXD_0_FUNCTION},      \
00036     {AVR32_USART0_CLK_0_PIN, AVR32_USART0_CLK_0_FUNCTION},      \
00037     {AVR32_USART0_CTS_0_PIN, AVR32_USART0_CTS_0_FUNCTION},      \
00038     {AVR32_USART0_RTS_0_PIN, AVR32_USART0_RTS_0_FUNCTION}       \
00039   };
00040 
00041   /* Set options for the USART */
00042   usart_opt.baudrate = 57600;
00043   usart_opt.charlength = 8;
00044   usart_opt.paritytype = USART_NO_PARITY;
00045   usart_opt.stopbits = USART_1_STOPBIT;
00046   usart_opt.channelmode = USART_NORMAL_CHMODE;
00047 
00048 
00049   /* Set options for the PLL altering*/
00050   pll_opt.pll=PM_PLL0;
00051   pll_opt.oscillator=PM_OSC0;
00052   pll_opt.multiplier=3;
00053   pll_opt.divider=2;
00054 
00055   /* CPU options */
00056   cpu_opt.clock = PM_CPU_DOMAIN;
00057   cpu_opt.div_enable = 1;
00058   cpu_opt.divider = 2;
00059 
00060   /* AHB options */
00061   ahb_opt.clock = PM_AHB_DOMAIN;
00062   ahb_opt.div_enable = 1;
00063   ahb_opt.divider = 2;
00064 
00065   /* APBA options */
00066   apba_opt.clock = PM_APBA_DOMAIN;
00067   apba_opt.div_enable = 1;
00068   apba_opt.divider = 2;
00069 
00070   /* APBB options */
00071   apbb_opt.clock = PM_APBB_DOMAIN;
00072   apbb_opt.div_enable = 1;
00073   apbb_opt.divider = 3;
00074 
00075   pm_reset();
00076 
00077   /* Initialize the usart */
00078   pio_enable_module( usart_piomap, 5 );
00079   usart_init_rs232( usart, &usart_opt, pm_read_module_frequency(APBA_USART_INSTANCE));
00080 
00081   print(usart, "\nSo many assholes, so few... bullets\n");
00082   print_pm_stats(usart);
00083 
00084   //Set pll0
00085   print(usart, "Setting pll0...\n");
00086   if(pm_set_pll(&pll_opt) == PM_LOCK_ERROR ){
00087         print(usart, "pll0 did not lock\n");
00088   }
00089   else
00090         print(usart, "pll0 successfully locked\n");
00091 
00092   // Set pll0 to mclk
00093   print(usart, "Setting pll0 to mclk...");
00094   pm_set_mclk_source(PM_PLL0);
00095   usart_init_rs232( usart, &usart_opt, pm_read_module_frequency(APBA_USART_INSTANCE) );
00096   print(usart,"\n");
00097 
00098   // Modifying clock domain
00099   print(usart, "Modifying clock domain...\n");
00100   pm_set_clock_domain_scaler( &apbb_opt );
00101   usart_init_rs232( usart, &usart_opt, pm_read_module_frequency(APBA_USART_INSTANCE) );
00102   print_pm_stats(usart);
00103   usart_init_rs232( usart, &usart_opt, pm_read_module_frequency(APBA_USART_INSTANCE) );
00104 
00105   // Modifying clock domain
00106   print(usart, "\nModifying clock domain...\n");
00107   pm_set_clock_domain_scaler( &ahb_opt );
00108   usart_init_rs232( usart, &usart_opt, pm_read_module_frequency(APBA_USART_INSTANCE) );
00109   print(usart,"\n");
00110   print_pm_stats(usart);
00111 
00112   return 42;
00113 }
00114 
00115 int print_pm_stats(volatile avr32_usart_t * usart)
00116 {
00117 
00118   // Print out ALL the stats
00119   print(usart, "\n\r-> The current MCLK frequency is ");
00120   print_ulong(usart, pm_read_mclk()/1000000 );
00121   print(usart, "MHZ\n");
00122   print(usart, "-> The current pll0 frequency is ");
00123   print_ulong(usart, pm_read_pll_frequency(PM_PLL0)/1000000);
00124   print(usart, "MHZ\n");
00125   print(usart, "-> The current CPU divider is ");
00126   print_ulong(usart, pm_read_clock_domain_scaler(PM_CPU_DOMAIN) );
00127   print(usart, "\n");
00128   print(usart, "-> The current AHB divider is ");
00129   print_ulong(usart, pm_read_clock_domain_scaler(PM_AHB_DOMAIN) );
00130   print(usart, "\n");
00131   print(usart, "-> The current APBA divider is ");
00132   print_ulong(usart, pm_read_clock_domain_scaler(PM_APBA_DOMAIN) );
00133   print(usart, "\n");
00134   print(usart, "-> The current APBB divider is ");
00135   print_ulong(usart, pm_read_clock_domain_scaler(PM_APBB_DOMAIN) );
00136   print(usart, "\n");
00137   print(usart, "-> The current USART frequency is ");
00138   print_ulong(usart, pm_read_module_frequency(APBA_USART_INSTANCE));
00139   print(usart, "HZ\n");
00140   print(usart, "-> The current PWM frequency is ");
00141   print_ulong(usart, pm_read_module_frequency(PM_APBB_PWM));
00142   print(usart, "HZ\n\n");
00143 
00144   return 0;
00145 
00146 }

Generated on Thu May 10 14:14:48 2007 for AVR321000 Communication with the AVR32 USART by  doxygen 1.5.1