00001
00030
00031
00032 #if (defined __GNUC__)
00033 # include "nlao_cpu.h"
00034 # include "nlao_usart.h"
00035 #endif
00036 #include <stdio.h>
00037 #include "compiler.h"
00038 #include "preprocessor.h"
00039 #include "board.h"
00040 #include "print_funcs.h"
00041 #include "flashc.h"
00042 #include "gpio.h"
00043 #include "intc.h"
00044 #include "pm.h"
00045 #include "twi.h"
00046 #include "usart.h"
00047 #include "main.h"
00048 #include "conf_cs2200.h"
00049 #include "cycle_counter.h"
00050 #include "cs2200.h"
00051 #include "et024006dhu.h"
00052
00053
00054
00055
00058
00059 pm_freq_param_t pm_freq_param=
00060 {
00061 .cpu_f = FCPU_HZ
00062 , .pba_f = FPBA_HZ
00063 , .osc0_f = FOSC0
00064 , .osc0_startup = OSC0_STARTUP
00065 };
00067
00068 static void mmi_print_line(char *string, et024006_color_t fg_color, et024006_color_t bg_color);
00069
00070
00071 #if __GNUC__ && __AVR32__
00072
00079 size_t wcstombs(char *s, const wchar_t *pwcs, size_t n)
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 }
00092
00093 #endif
00094
00095
00098 static void init_exceptions(void)
00099 {
00100 #if __GNUC__ && __AVR32__
00101
00102 extern void _evba;
00103
00104
00105
00106 Set_system_register(AVR32_EVBA, (int)&_evba);
00107 #endif
00108
00109
00110 Enable_global_exception();
00111 }
00112
00113
00116 static void init_hmatrix(void)
00117 {
00118 union
00119 {
00120 unsigned long scfg;
00121 avr32_hmatrix_scfg_t SCFG;
00122 } u_avr32_hmatrix_scfg;
00123
00124
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
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
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
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 }
00144
00145
00148 static void init_sys_clocks(void)
00149 {
00150
00151 pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00152
00153
00154 pm_enable_osc1_crystal(&AVR32_PM, FOSC0);
00155
00156 pm_enable_clk1(&AVR32_PM, OSC0_STARTUP);
00157
00158
00159 pm_pll_setup(&AVR32_PM, 0,
00160 10,
00161 1,
00162 0,
00163 16);
00164
00165
00166
00167 pm_pll_set_option(&AVR32_PM, 0,
00168 1,
00169 1,
00170 0);
00171
00172
00173 pm_pll_enable(&AVR32_PM, 0);
00174 pm_wait_for_pll0_locked(&AVR32_PM);
00175
00176 pm_cksel(&AVR32_PM,
00177 0,
00178 0,
00179 0,
00180 0,
00181 0,
00182 0);
00183
00184
00185 flashc_set_wait_state(1);
00186
00187
00188 pm_switch_to_clock(&AVR32_PM, AVR32_PM_MCCTRL_MCSEL_PLL0);
00189
00190
00191 #if __GNUC__ && __AVR32__
00192
00193 set_cpu_hz(FPBA_HZ);
00194 #endif
00195 }
00196
00197
00200 static void init_stdio(void)
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
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
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 }
00242
00243 #if __GNUC__
00244
00256 int _init_startup(void)
00257 {
00258
00259 extern void _evba;
00260
00261
00262 Set_system_register(AVR32_EVBA, (int)&_evba);
00263
00264
00265 Enable_global_exception();
00266
00267
00268 INTC_init_interrupts();
00269
00270
00271 return 1;
00272 }
00273
00274 #elif __ICCAVR32__
00275
00279 int __low_level_init(void)
00280 {
00281
00282 Enable_global_exception();
00283
00284
00285 INTC_init_interrupts();
00286
00287
00288 return 1;
00289 }
00290
00291 #endif // Compiler
00292
00293
00296 static void init_twi(U32 fpba_hz)
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 }
00318
00319
00324 int main(void)
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
00332 init_twi(AVR32_PM_RCOSC_FREQUENCY);
00333
00334
00335 cs2200_setup(11289600);
00336
00337
00338 init_sys_clocks();
00339
00340
00341 init_twi(FPBA_HZ);
00342
00343
00344 if( PM_FREQ_STATUS_FAIL==pm_configure_clocks(&pm_freq_param) )
00345 return 42;
00346
00347
00348 init_stdio();
00349
00350
00351 init_hmatrix();
00352
00353
00354 init_dbg_rs232(pm_freq_param.pba_f);
00355
00356
00357 et024006_Init( pm_freq_param.cpu_f, pm_freq_param.cpu_f );
00358
00359
00360 et024006_DrawFilledRect(0, 0, ET024006_WIDTH, ET024006_HEIGHT, BLACK );
00361
00362
00363 gpio_set_gpio_pin(ET024006DHU_BL_PIN);
00364
00365 mmi_print_line("CS2200 example", WHITE, BLACK);
00366
00367
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
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 }
00417
00420 static void mmi_print_line(char *string, et024006_color_t fg_color, et024006_color_t bg_color)
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 }