00001
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00061
00062
00063 #if (defined __GNUC__)
00064 # include "nlao_cpu.h"
00065 # include "nlao_usart.h"
00066 #endif
00067 #include <stdio.h>
00068 #include "compiler.h"
00069 #include "preprocessor.h"
00070 #include "board.h"
00071 #include "print_funcs.h"
00072 #include "flashc.h"
00073 #include "gpio.h"
00074 #include "intc.h"
00075 #include "pm.h"
00076 #include "twi.h"
00077 #include "usart.h"
00078 #include "main.h"
00079 #include "conf_cs2200.h"
00080 #include "cycle_counter.h"
00081 #include "cs2200.h"
00082 #include "et024006dhu.h"
00083
00084
00085
00086
00089
00090 pm_freq_param_t pm_freq_param=
00091 {
00092 .cpu_f = FCPU_HZ
00093 , .pba_f = FPBA_HZ
00094 , .osc0_f = FOSC0
00095 , .osc0_startup = OSC0_STARTUP
00096 };
00098
00099 static void mmi_print_line(char *string, et024006_color_t fg_color, et024006_color_t bg_color);
00100
00101
00102 #if __GNUC__ && __AVR32__
00103
00110 size_t wcstombs(char *s, const wchar_t *pwcs, size_t n)
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 }
00123
00124 #endif
00125
00126
00129 static void init_exceptions(void)
00130 {
00131 #if __GNUC__ && __AVR32__
00132
00133 extern void _evba;
00134
00135
00136
00137 Set_system_register(AVR32_EVBA, (int)&_evba);
00138 #endif
00139
00140
00141 Enable_global_exception();
00142 }
00143
00144
00147 static void init_hmatrix(void)
00148 {
00149 union
00150 {
00151 unsigned long scfg;
00152 avr32_hmatrix_scfg_t SCFG;
00153 } u_avr32_hmatrix_scfg;
00154
00155
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
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
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
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 }
00175
00176
00179 static void init_sys_clocks(void)
00180 {
00181
00182 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00183
00184
00185 pm_enable_osc1_crystal(&AVR32_PM, FOSC0);
00186
00187 pm_enable_clk1(&AVR32_PM, OSC0_STARTUP);
00188
00189
00190 pm_pll_setup(&AVR32_PM, 0,
00191 10,
00192 1,
00193 0,
00194 16);
00195
00196
00197
00198 pm_pll_set_option(&AVR32_PM, 0,
00199 1,
00200 1,
00201 0);
00202
00203
00204 pm_pll_enable(&AVR32_PM, 0);
00205 pm_wait_for_pll0_locked(&AVR32_PM);
00206
00207 pm_cksel(&AVR32_PM,
00208 0,
00209 0,
00210 0,
00211 0,
00212 0,
00213 0);
00214
00215
00216 flashc_set_wait_state(1);
00217
00218
00219 pm_switch_to_clock(&AVR32_PM, AVR32_PM_MCCTRL_MCSEL_PLL0);
00220
00221
00222 #if __GNUC__ && __AVR32__
00223
00224 set_cpu_hz(FPBA_HZ);
00225 #endif
00226 }
00227
00228
00231 static void init_stdio(void)
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
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
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 }
00273
00274 #if __GNUC__
00275
00287 int _init_startup(void)
00288 {
00289
00290 extern void _evba;
00291
00292
00293 Set_system_register(AVR32_EVBA, (int)&_evba);
00294
00295
00296 Enable_global_exception();
00297
00298
00299 INTC_init_interrupts();
00300
00301
00302 return 1;
00303 }
00304
00305 #elif __ICCAVR32__
00306
00310 int __low_level_init(void)
00311 {
00312
00313 Enable_global_exception();
00314
00315
00316 INTC_init_interrupts();
00317
00318
00319 return 1;
00320 }
00321
00322 #endif // Compiler
00323
00324
00327 static void init_twi(U32 fpba_hz)
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 }
00348
00349
00354 int main(void)
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
00362 init_twi(AVR32_PM_RCOSC_FREQUENCY);
00363
00364
00365 cs2200_setup(11289600);
00366
00367
00368 init_sys_clocks();
00369
00370
00371 init_twi(FPBA_HZ);
00372
00373
00374 if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00375 return 42;
00376
00377
00378 init_stdio();
00379
00380
00381 init_hmatrix();
00382
00383
00384 init_dbg_rs232(pm_freq_param.pba_f);
00385
00386
00387 et024006_Init( pm_freq_param.cpu_f, pm_freq_param.cpu_f );
00388
00389
00390 et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK );
00391
00392
00393 gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00394
00395 mmi_print_line("CS2200 example", WHITE, BLACK);
00396
00397
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
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 }
00447
00450 static void mmi_print_line(char *string, et024006_color_t fg_color, et024006_color_t bg_color)
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 }