at45dbx_example.c File Reference


Detailed Description

Example application of the AT45DBX data flash controller.

This software uses AT45DBX interfaces to check memory capacity. Then it performs:

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file at45dbx_example.c.

#include <string.h>
#include "compiler.h"
#include "preprocessor.h"
#include "board.h"
#include "power_clocks_lib.h"
#include "gpio.h"
#include "spi.h"
#include "print_funcs.h"
#include "conf_at45dbx.h"
#include "at45dbx.h"

Go to the source code of this file.

Defines

#define AT45DBX_ENABLE_NPCS_PIN(NPCS, unused)   {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},
#define MSG_GOODBYE   "\n ---------- End of AT45DBX example ---------- \n"
#define MSG_WELCOME   "\n ---------- Welcome to AT45DBX example ---------- \n"
 Welcome message to display.
Clock Settings
#define EXAMPLE_TARGET_DFLL_FREQ_HZ   96000000
#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ   12000000
#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   12000000
#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   FOSC0
Test Result Messages
#define TEST_FAIL   "\t[FAIL]\n"
#define TEST_SUCCESS   "\t[PASS]\n"

Functions

static void at45dbx_example_check_mem (void)
 Performs a memory check on all DFs.
static void at45dbx_example_test_byte_mem (void)
 Tests single-byte access functions.
static void at45dbx_example_test_multiple_sector (void)
 Tests multiple-sector access functions.
static void at45dbx_example_test_RAM_mem (void)
 Tests single-sector access functions.
void at45dbx_read_multiple_sector_callback (const void *psector)
 Callback function invoked after each sector read during at45dbx_read_multiple_sector.
static void at45dbx_resources_init (void)
 Initializes AT45DBX resources: GPIO, SPI and AT45DBX.
void at45dbx_write_multiple_sector_callback (void *psector)
 Callback function invoked before each sector write during at45dbx_write_multiple_sector.
int main (void)
 Main function. Execution starts here.

Variables

static U32 at45dbx_example_error_cnt
 Number of erroneous bytes detected during the test of the AT45DBX multiple- sector access functions.
static const Union32 PATTERN_MULTIPLE_SECTOR = {0xDEADBEEF}
 Pattern to test the AT45DBX multiple-sector access functions with.
Parameters to pcl_configure_clocks().
static scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF}
static pcl_freq_param_t pcl_dfll_freq_param


Define Documentation

#define AT45DBX_ENABLE_NPCS_PIN ( NPCS,
unused   )     {AT45DBX_SPI_NPCS##NPCS##_PIN, AT45DBX_SPI_NPCS##NPCS##_FUNCTION},

Referenced by at45dbx_resources_init().

#define EXAMPLE_TARGET_DFLL_FREQ_HZ   96000000

Definition at line 147 of file at45dbx_example.c.

#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ   12000000

Definition at line 148 of file at45dbx_example.c.

#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   12000000

Definition at line 146 of file at45dbx_example.c.

#define EXAMPLE_TARGET_PBACLK_FREQ_HZ   FOSC0

Definition at line 146 of file at45dbx_example.c.

Referenced by at45dbx_resources_init(), and main().

#define MSG_GOODBYE   "\n ---------- End of AT45DBX example ---------- \n"

Definition at line 130 of file at45dbx_example.c.

Referenced by main().

#define MSG_WELCOME   "\n ---------- Welcome to AT45DBX example ---------- \n"

Welcome message to display.

Definition at line 129 of file at45dbx_example.c.

Referenced by main().

#define TEST_FAIL   "\t[FAIL]\n"

#define TEST_SUCCESS   "\t[PASS]\n"


Function Documentation

static void at45dbx_example_check_mem ( void   )  [static]

Performs a memory check on all DFs.

Definition at line 162 of file at45dbx_example.c.

References at45dbx_mem_check(), AT45DBX_MEM_CNT, AT45DBX_MEM_SIZE, TEST_FAIL, and TEST_SUCCESS.

Referenced by main().

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 }

static void at45dbx_example_test_byte_mem ( void   )  [static]

Tests single-byte access functions.

Definition at line 179 of file at45dbx_example.c.

References at45dbx_read_byte(), at45dbx_read_close(), at45dbx_read_open(), at45dbx_write_byte(), at45dbx_write_close(), at45dbx_write_open(), TEST_FAIL, and TEST_SUCCESS.

Referenced by main().

00180 {
00181   U8 Pattern = 0x55;
00182   U8 j = 0xA5;
00183   print_dbg("\tUsing Pattern 0x55");
00184   // Perform write access.
00185   if (at45dbx_write_open(Pattern) == OK)
00186   {
00187     at45dbx_write_byte(Pattern);
00188     at45dbx_write_close();
00189   }
00190   // Perform read access.
00191   if (at45dbx_read_open(Pattern) == OK)
00192   {
00193     j = at45dbx_read_byte();
00194     at45dbx_read_close();
00195   }
00196   // Check read and write operations.
00197   if (j == Pattern)
00198   {
00199     print_dbg(TEST_SUCCESS);
00200   }
00201   else
00202   {
00203     print_dbg(TEST_FAIL);
00204   }
00205 
00206   // Change the pattern used.
00207   Pattern = 0xAA;
00208   j = 0xA5;
00209   print_dbg("\tUsing Pattern 0xAA");
00210   // Perform write access.
00211   if (at45dbx_write_open(Pattern) == OK)
00212   {
00213     at45dbx_write_byte(Pattern);
00214     at45dbx_write_close();
00215   }
00216   // Perform read access.
00217   if (at45dbx_read_open(Pattern) == OK)
00218   {
00219     j = at45dbx_read_byte();
00220     at45dbx_read_close();
00221   }
00222   // Check read and write operations.
00223   if (j == Pattern)
00224   {
00225     print_dbg(TEST_SUCCESS);
00226   }
00227   else
00228   {
00229     print_dbg(TEST_FAIL);
00230   }
00231 }

static void at45dbx_example_test_multiple_sector ( void   )  [static]

Tests multiple-sector access functions.

Definition at line 298 of file at45dbx_example.c.

References at45dbx_example_error_cnt, at45dbx_read_close(), at45dbx_read_multiple_sector(), at45dbx_read_open(), at45dbx_write_close(), at45dbx_write_multiple_sector(), at45dbx_write_open(), TEST_FAIL, and TEST_SUCCESS.

Referenced by main().

00299 {
00300   U32 position = 252;
00301   U32 nb_sector = 4;
00302 
00303   // Initialize counters.
00304   at45dbx_example_error_cnt = 0;
00305 
00306   // Write sectors.
00307   print_dbg("\tWriting sectors\n");
00308   at45dbx_write_open(position);
00309   at45dbx_write_multiple_sector(nb_sector);
00310   at45dbx_write_close();
00311 
00312   // Read written sectors.
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 }

static void at45dbx_example_test_RAM_mem ( void   )  [static]

Tests single-sector access functions.

Definition at line 236 of file at45dbx_example.c.

References at45dbx_read_close(), at45dbx_read_open(), at45dbx_read_sector_2_ram(), AT45DBX_SECTOR_SIZE, at45dbx_write_close(), at45dbx_write_open(), at45dbx_write_sector_from_ram(), TEST_FAIL, and TEST_SUCCESS.

Referenced by main().

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   // Perform write access.
00246   if (at45dbx_write_open(Pattern) == OK)
00247   {
00248     at45dbx_write_sector_from_ram(PatternTable);
00249     at45dbx_write_close();
00250   }
00251   // Perform read access.
00252   if (at45dbx_read_open(Pattern) == OK)
00253   {
00254     at45dbx_read_sector_2_ram(ReceiveTable);
00255     at45dbx_read_close();
00256   }
00257   // Check read and write operations.
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   // Change the pattern used.
00268   Pattern = 0xAA;
00269   memset(PatternTable, Pattern, AT45DBX_SECTOR_SIZE);
00270   memset(ReceiveTable, 0xA5, AT45DBX_SECTOR_SIZE);
00271   print_dbg("\tUsing Pattern 0xAA");
00272   // Perform write access.
00273   if (at45dbx_write_open(Pattern) == OK)
00274   {
00275     at45dbx_write_sector_from_ram(PatternTable);
00276     at45dbx_write_close();
00277   }
00278   // Perform read access.
00279   if (at45dbx_read_open(Pattern) == OK)
00280   {
00281     at45dbx_read_sector_2_ram(ReceiveTable);
00282     at45dbx_read_close();
00283   }
00284   // Check read and write operations.
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 }

void at45dbx_read_multiple_sector_callback ( const void *  psector  ) 

Callback function invoked after each sector read during at45dbx_read_multiple_sector.

Parameters:
psector Pointer to read sector.

Definition at line 339 of file at45dbx_example.c.

References at45dbx_example_error_cnt, AT45DBX_SECTOR_SIZE, and PATTERN_MULTIPLE_SECTOR.

Referenced by at45dbx_read_multiple_sector().

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 }

static void at45dbx_resources_init ( void   )  [static]

Initializes AT45DBX resources: GPIO, SPI and AT45DBX.

Definition at line 359 of file at45dbx_example.c.

References AT45DBX_ENABLE_NPCS_PIN, at45dbx_init(), AT45DBX_MEM_CNT, AT45DBX_SPI_BITS, AT45DBX_SPI_FIRST_NPCS, AT45DBX_SPI_MASTER_SPEED, EXAMPLE_TARGET_PBACLK_FREQ_HZ, spi_enable(), spi_initMaster(), and spi_selectionMode().

Referenced by main().

00360 {
00361   static const gpio_map_t AT45DBX_SPI_GPIO_MAP =
00362   {
00363     {AT45DBX_SPI_SCK_PIN,          AT45DBX_SPI_SCK_FUNCTION         },  // SPI Clock.
00364     {AT45DBX_SPI_MISO_PIN,         AT45DBX_SPI_MISO_FUNCTION        },  // MISO.
00365     {AT45DBX_SPI_MOSI_PIN,         AT45DBX_SPI_MOSI_FUNCTION        },  // MOSI.
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   // SPI options.
00373   spi_options_t spiOptions =
00374   {
00375     .reg          = AT45DBX_SPI_FIRST_NPCS,   // Defined in conf_at45dbx.h.
00376     .baudrate     = AT45DBX_SPI_MASTER_SPEED, // Defined in conf_at45dbx.h.
00377     .bits         = AT45DBX_SPI_BITS,         // Defined in conf_at45dbx.h.
00378     .spck_delay   = 0,
00379     .trans_delay  = 0,
00380     .stay_act     = 1,
00381     .spi_mode     = 0,
00382     .modfdis      = 1
00383   };
00384 
00385   // Assign I/Os to SPI.
00386   gpio_enable_module(AT45DBX_SPI_GPIO_MAP,
00387                      sizeof(AT45DBX_SPI_GPIO_MAP) / sizeof(AT45DBX_SPI_GPIO_MAP[0]));
00388 
00389   // Initialize as master.
00390   spi_initMaster(AT45DBX_SPI, &spiOptions);
00391 
00392   // Set selection mode: variable_ps, pcs_decode, delay.
00393   spi_selectionMode(AT45DBX_SPI, 0, 0, 0);
00394 
00395   // Enable SPI.
00396   spi_enable(AT45DBX_SPI);
00397 
00398   // Initialize data flash with SPI clock Osc0.
00399   at45dbx_init(spiOptions, EXAMPLE_TARGET_PBACLK_FREQ_HZ);
00400 }

void at45dbx_write_multiple_sector_callback ( void *  psector  ) 

Callback function invoked before each sector write during at45dbx_write_multiple_sector.

Parameters:
psector Pointer to sector to write.

Definition at line 331 of file at45dbx_example.c.

References AT45DBX_SECTOR_SIZE, and PATTERN_MULTIPLE_SECTOR.

Referenced by at45dbx_write_multiple_sector().

00332 {
00333   U32 *pdata = psector;
00334   psector = (U8 *)psector + AT45DBX_SECTOR_SIZE;
00335   while ((void *)pdata < psector) *pdata++ = PATTERN_MULTIPLE_SECTOR.u32;
00336 }

int main ( void   ) 

Main function. Execution starts here.

Definition at line 422 of file at45dbx_example.c.

References at45dbx_example_check_mem(), at45dbx_example_test_byte_mem(), at45dbx_example_test_multiple_sector(), at45dbx_example_test_RAM_mem(), at45dbx_resources_init(), EXAMPLE_TARGET_PBACLK_FREQ_HZ, MSG_GOODBYE, MSG_WELCOME, and pcl_dfll_freq_param.

00423 {
00424 
00425 #if BOARD == UC3L_EK
00426   // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected
00427   // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the
00428   // main clock source to the DFLL.
00429   pcl_configure_clocks(&pcl_dfll_freq_param);
00430   // Note: since it is dynamically computing the appropriate field values of the
00431   // configuration registers from the parameters structure, this function is not
00432   // optimal in terms of code size. For a code size optimal solution, it is better
00433   // to create a new function from pcl_configure_clocks_dfll0() and modify it
00434   // to use preprocessor computation from pre-defined target frequencies.
00435 #else
00436   // Configure Osc0 in crystal mode (i.e. use of an external crystal source, with
00437   // frequency FOSC0) with an appropriate startup time then switch the main clock
00438   // source to Osc0.
00439   pcl_switch_to_osc(PCL_OSC0, FOSC0, OSC0_STARTUP);
00440 #endif
00441 
00442   // Initialize RS232 debug text output.
00443   init_dbg_rs232(EXAMPLE_TARGET_PBACLK_FREQ_HZ);
00444   print_dbg(MSG_WELCOME);
00445 
00446   // Initialize AT45DBX resources: GPIO, SPI and AT45DBX.
00447   at45dbx_resources_init();
00448 
00449   // Perform a memory check on all DFs.
00450   print_dbg("Entering Memory Check:\n");
00451   at45dbx_example_check_mem();
00452 
00453   // Test single-byte access functions.
00454   print_dbg("Entering Memory Test (Byte Access):\n");
00455   at45dbx_example_test_byte_mem();
00456 
00457   // Test single-sector access functions.
00458   print_dbg("Entering Memory Test (RAM Access):\n");
00459   at45dbx_example_test_RAM_mem();
00460 
00461   // Test multiple-sector access functions.
00462   print_dbg("Entering Multiple Sector Test:\n");
00463   at45dbx_example_test_multiple_sector();
00464 
00465   print_dbg(MSG_GOODBYE);
00466   while (TRUE);
00467 }


Variable Documentation

Number of erroneous bytes detected during the test of the AT45DBX multiple- sector access functions.

Definition at line 157 of file at45dbx_example.c.

Referenced by at45dbx_example_test_multiple_sector(), and at45dbx_read_multiple_sector_callback().

scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF} [static]

Definition at line 407 of file at45dbx_example.c.

const Union32 PATTERN_MULTIPLE_SECTOR = {0xDEADBEEF} [static]

Pattern to test the AT45DBX multiple-sector access functions with.

Definition at line 153 of file at45dbx_example.c.

Referenced by at45dbx_read_multiple_sector_callback(), and at45dbx_write_multiple_sector_callback().

pcl_freq_param_t pcl_dfll_freq_param [static]

Initial value:

{
  .main_clk_src = PCL_MC_DFLL0,
  .cpu_f        = EXAMPLE_TARGET_MCUCLK_FREQ_HZ,
  .pba_f        = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
  .pbb_f        = EXAMPLE_TARGET_PBACLK_FREQ_HZ,
  .dfll_f       = EXAMPLE_TARGET_DFLL_FREQ_HZ,
  .pextra_params = &gc_dfllif_ref_opt
}

Definition at line 408 of file at45dbx_example.c.

Referenced by main().


Generated on Thu Dec 17 19:57:40 2009 for AVR32 - AT45DBX Data Flash Driver by  doxygen 1.5.5