00001
00116 #include <string.h>
00117 #include "compiler.h"
00118 #include "preprocessor.h"
00119 #include "board.h"
00120 #include "power_clocks_lib.h"
00121 #include "gpio.h"
00122 #include "spi.h"
00123 #include "print_funcs.h"
00124 #include "conf_at45dbx.h"
00125 #include "at45dbx.h"
00126
00127
00129 #define MSG_WELCOME "\n ---------- Welcome to AT45DBX example ---------- \n"
00130 #define MSG_GOODBYE "\n ---------- End of AT45DBX example ---------- \n"
00131
00132
00135
00136 #define TEST_SUCCESS "\t[PASS]\n"
00137 #define TEST_FAIL "\t[FAIL]\n"
00139
00140
00142
00143 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ FOSC0 // PBA clock target frequency
00144 #if BOARD == UC3L_EK
00145 #undef EXAMPLE_TARGET_PBACLK_FREQ_HZ
00146 # define EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 // PBA clock target frequency
00147 # define EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 // DFLL target frequency, in Hz.
00148 # define EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 // MCU clock target frequency, in Hz.
00149 #endif
00151
00153 static const Union32 PATTERN_MULTIPLE_SECTOR = {0xDEADBEEF};
00154
00157 static U32 at45dbx_example_error_cnt;
00158
00159
00162 static void at45dbx_example_check_mem(void)
00163 {
00164 if (at45dbx_mem_check() == OK)
00165 {
00166 print_dbg("\tSize:\t");
00167 print_dbg_ulong(AT45DBX_MEM_CNT << (AT45DBX_MEM_SIZE - 20));
00168 print_dbg(" MB\t" TEST_SUCCESS);
00169 }
00170 else
00171 {
00172 print_dbg(TEST_FAIL);
00173 }
00174 }
00175
00176
00179 static void at45dbx_example_test_byte_mem(void)
00180 {
00181 U8 Pattern = 0x55;
00182 U8 j = 0xA5;
00183 print_dbg("\tUsing Pattern 0x55");
00184
00185 if (at45dbx_write_open(Pattern) == OK)
00186 {
00187 at45dbx_write_byte(Pattern);
00188 at45dbx_write_close();
00189 }
00190
00191 if (at45dbx_read_open(Pattern) == OK)
00192 {
00193 j = at45dbx_read_byte();
00194 at45dbx_read_close();
00195 }
00196
00197 if (j == Pattern)
00198 {
00199 print_dbg(TEST_SUCCESS);
00200 }
00201 else
00202 {
00203 print_dbg(TEST_FAIL);
00204 }
00205
00206
00207 Pattern = 0xAA;
00208 j = 0xA5;
00209 print_dbg("\tUsing Pattern 0xAA");
00210
00211 if (at45dbx_write_open(Pattern) == OK)
00212 {
00213 at45dbx_write_byte(Pattern);
00214 at45dbx_write_close();
00215 }
00216
00217 if (at45dbx_read_open(Pattern) == OK)
00218 {
00219 j = at45dbx_read_byte();
00220 at45dbx_read_close();
00221 }
00222
00223 if (j == Pattern)
00224 {
00225 print_dbg(TEST_SUCCESS);
00226 }
00227 else
00228 {
00229 print_dbg(TEST_FAIL);
00230 }
00231 }
00232
00233
00236 static void at45dbx_example_test_RAM_mem(void)
00237 {
00238 static U8 PatternTable[AT45DBX_SECTOR_SIZE];
00239 static U8 ReceiveTable[AT45DBX_SECTOR_SIZE];
00240
00241 U8 Pattern = 0x55;
00242 memset(PatternTable, Pattern, AT45DBX_SECTOR_SIZE);
00243 memset(ReceiveTable, 0xA5, AT45DBX_SECTOR_SIZE);
00244 print_dbg("\tUsing Pattern 0x55");
00245
00246 if (at45dbx_write_open(Pattern) == OK)
00247 {
00248 at45dbx_write_sector_from_ram(PatternTable);
00249 at45dbx_write_close();
00250 }
00251
00252 if (at45dbx_read_open(Pattern) == OK)
00253 {
00254 at45dbx_read_sector_2_ram(ReceiveTable);
00255 at45dbx_read_close();
00256 }
00257
00258 if (!memcmp(ReceiveTable, PatternTable, AT45DBX_SECTOR_SIZE))
00259 {
00260 print_dbg(TEST_SUCCESS);
00261 }
00262 else
00263 {
00264 print_dbg(TEST_FAIL);
00265 }
00266
00267
00268 Pattern = 0xAA;
00269 memset(PatternTable, Pattern, AT45DBX_SECTOR_SIZE);
00270 memset(ReceiveTable, 0xA5, AT45DBX_SECTOR_SIZE);
00271 print_dbg("\tUsing Pattern 0xAA");
00272
00273 if (at45dbx_write_open(Pattern) == OK)
00274 {
00275 at45dbx_write_sector_from_ram(PatternTable);
00276 at45dbx_write_close();
00277 }
00278
00279 if (at45dbx_read_open(Pattern) == OK)
00280 {
00281 at45dbx_read_sector_2_ram(ReceiveTable);
00282 at45dbx_read_close();
00283 }
00284
00285 if (!memcmp(ReceiveTable, PatternTable, AT45DBX_SECTOR_SIZE))
00286 {
00287 print_dbg(TEST_SUCCESS);
00288 }
00289 else
00290 {
00291 print_dbg(TEST_FAIL);
00292 }
00293 }
00294
00295
00298 static void at45dbx_example_test_multiple_sector(void)
00299 {
00300 U32 position = 252;
00301 U32 nb_sector = 4;
00302
00303
00304 at45dbx_example_error_cnt = 0;
00305
00306
00307 print_dbg("\tWriting sectors\n");
00308 at45dbx_write_open(position);
00309 at45dbx_write_multiple_sector(nb_sector);
00310 at45dbx_write_close();
00311
00312
00313 print_dbg("\tReading sectors\t");
00314 at45dbx_read_open(position);
00315 at45dbx_read_multiple_sector(nb_sector);
00316 at45dbx_read_close();
00317
00318 if (!at45dbx_example_error_cnt)
00319 {
00320 print_dbg(TEST_SUCCESS);
00321 }
00322 else
00323 {
00324 print_dbg(TEST_FAIL "\t");
00325 print_dbg_ulong(at45dbx_example_error_cnt);
00326 print_dbg(" errors\n");
00327 }
00328 }
00329
00330
00331 void at45dbx_write_multiple_sector_callback(void *psector)
00332 {
00333 U32 *pdata = psector;
00334 psector = (U8 *)psector + AT45DBX_SECTOR_SIZE;
00335 while ((void *)pdata < psector) *pdata++ = PATTERN_MULTIPLE_SECTOR.u32;
00336 }
00337
00338
00339 void at45dbx_read_multiple_sector_callback(const void *psector)
00340 {
00341 const Union32 *pdata = psector;
00342 psector = (const U8 *)psector + AT45DBX_SECTOR_SIZE;
00343 while ((const void *)pdata < psector)
00344 {
00345 if (pdata->u32 != PATTERN_MULTIPLE_SECTOR.u32)
00346 {
00347 at45dbx_example_error_cnt += (pdata->u8[0] != PATTERN_MULTIPLE_SECTOR.u8[0]) +
00348 (pdata->u8[1] != PATTERN_MULTIPLE_SECTOR.u8[1]) +
00349 (pdata->u8[2] != PATTERN_MULTIPLE_SECTOR.u8[2]) +
00350 (pdata->u8[3] != PATTERN_MULTIPLE_SECTOR.u8[3]);
00351 }
00352 pdata++;
00353 }
00354 }
00355
00356
00359 static void at45dbx_resources_init(void)
00360 {
00361 static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00362 {
00363 {AT45DBX_SPI_SCK_PIN, AT45DBX_SPI_SCK_FUNCTION },
00364 {AT45DBX_SPI_MISO_PIN, AT45DBX_SPI_MISO_FUNCTION },
00365 {AT45DBX_SPI_MOSI_PIN, AT45DBX_SPI_MOSI_FUNCTION },
00366 #define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused) \
00367 {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION}, // Chip Select NPCS.
00368 MREPEAT(AT45DBX_MEM_CNT, AT45DBX_ENABLE_NPCS_PIN, ~)
00369 #undef AT45DBX_ENABLE_NPCS_PIN
00370 };
00371
00372
00373 spi_options_t spiOptions =
00374 {
00375 .reg = AT45DBX_SPI_FIRST_NPCS,
00376 .baudrate = AT45DBX_SPI_MASTER_SPEED,
00377 .bits = AT45DBX_SPI_BITS,
00378 .spck_delay = 0,
00379 .trans_delay = 0,
00380 .stay_act = 1,
00381 .spi_mode = 0,
00382 .modfdis = 1
00383 };
00384
00385
00386 gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00387 sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00388
00389
00390 spi_initMaster(AT45DBX_SPI, &spiOptions);
00391
00392
00393 spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00394
00395
00396 spi_enable(AT45DBX_SPI);
00397
00398
00399 at45dbx_init(spiOptions, EXAMPLE_TARGET_PBACLK_FREQ_HZ);
00400 }
00401
00402
00403 #if BOARD == UC3L_EK
00404
00406
00407 static scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF};
00408 static pcl_freq_param_t pcl_dfll_freq_param =
00409 {
00410 .main_clk_src = PCL_MC_DFLL0,
00411 .cpu_f = EXAMPLE_TARGET_MCUCLK_FREQ_HZ,
00412 .pba_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
00413 .pbb_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
00414 .dfll_f = EXAMPLE_TARGET_DFLL_FREQ_HZ,
00415 .pextra_params = &gc_dfllif_ref_opt
00416 };
00418 #endif
00419
00422 int main(void)
00423 {
00424
00425 #if BOARD == UC3L_EK
00426
00427
00428
00429 pcl_configure_clocks(&pcl_dfll_freq_param);
00430
00431
00432
00433
00434
00435 #else
00436
00437
00438
00439 pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00440 #endif
00441
00442
00443 init_dbg_rs232(EXAMPLE_TARGET_PBACLK_FREQ_HZ);
00444 print_dbg(MSG_WELCOME);
00445
00446
00447 at45dbx_resources_init();
00448
00449
00450 print_dbg("Entering Memory Check:\n");
00451 at45dbx_example_check_mem();
00452
00453
00454 print_dbg("Entering Memory Test (Byte Access):\n");
00455 at45dbx_example_test_byte_mem();
00456
00457
00458 print_dbg("Entering Memory Test (RAM Access):\n");
00459 at45dbx_example_test_RAM_mem();
00460
00461
00462 print_dbg("Entering Multiple Sector Test:\n");
00463 at45dbx_example_test_multiple_sector();
00464
00465 print_dbg(MSG_GOODBYE);
00466 while (TRUE);
00467 }