SAMV71 Xplained Ultra Software Package 1.3

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /**
00031  * \page qspi_flash QSPI with Serialflash Example
00032  *
00033  * \section Purpose
00034  *
00035  * This example demonstrates how to setup the QSPI in order to initialize, read
00036  * and write a serial dataflash.
00037  *
00038  * \section Requirements
00039  *
00040  * This package can be used with SAM V71 Xplained Ultra board.
00041  *
00042  * \section Description
00043  *
00044  * The demonstration program tests the serial dataflash present on the
00045  * evaluation kit by erasing and writing each one of its pages. It also gives
00046  * read/write bandwidth by the test.
00047  *
00048  * \section Usage
00049  *
00050  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00051  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00052  * -# Optionally, on the computer, open and configure a terminal application
00053  *    (e.g. HyperTerminal on Microsoft Windows) with these settings:
00054  *   - 115200 bauds
00055  *   - 8 bits of data
00056  *   - No parity
00057  *   - 1 stop bit
00058  *   - No flow control
00059  * -# Start the application.
00060  * -# Upon startup, the application will output the following lines on the 
00061  * terminal window:
00062  *    \code
00063  *    -- QSPI Serialflash Example xxx --
00064  *    -- SAMxxxxx-xx
00065  *    -- Compiled: xxx xx xxxx xx:xx:xx --
00066  *    QSPI drivers initialized
00067  *    \endcode
00068  * -# The program will connect to the serial firmware dataflash through the QSPI
00069  *    and start sending commands to it. It will perform the following:
00070  *    - Read the JEDEC identifier of the device to auto detect it
00071  *      The next line should indicate if the serial dataflash has been
00072  *      correctly identified. 
00073  * \section References
00074  * - qspi_flash/main.c
00075  * - qspi.c
00076  * - s25fl1.c
00077  */
00078 
00079 /**
00080  * \file
00081  *
00082  * This file contains all the specific code for the spi_serialflash example.
00083  */
00084 
00085 /*----------------------------------------------------------------------------
00086  *        Headers
00087  *----------------------------------------------------------------------------*/
00088 
00089 #include <board.h>
00090  
00091 #include <stdio.h>
00092 #include <assert.h>
00093 #include <string.h>
00094 #include "stdlib.h"
00095 /*----------------------------------------------------------------------------
00096  *        Local definitions
00097  *----------------------------------------------------------------------------*/
00098 
00099 /** Maximum device page size in bytes. */
00100 #define MAXPAGESIZE         256
00101 
00102 #define BUFFER_SIZE         512
00103 /*----------------------------------------------------------------------------
00104  *        Local variables
00105  *----------------------------------------------------------------------------*/
00106 
00107 /** Global DMA driver instance for all DMA transfers in application. */
00108 static sXdmad xDmad;
00109 
00110 /** Pins to configure for the application. */
00111 static Pin Qspi_pins[] =  PINS_QSPI;
00112 
00113 /** buffer for test QSPI flash */
00114 static uint32_t Buffer[BUFFER_SIZE], TestBuffer[BUFFER_SIZE];
00115 static uint8_t PeripheralInit = 0;
00116 /*----------------------------------------------------------------------------
00117  *         Local functions
00118  *----------------------------------------------------------------------------*/
00119 
00120 
00121 static void _fillupbuffer(uint32_t *pBuff, uint32_t size)
00122 {
00123     uint32_t i;
00124     // one page
00125     for(i = 0; i < (size /4);) {
00126         pBuff[i++] = 0x9955030c;
00127         pBuff[i++] = 0x11232244;
00128         pBuff[i++] = 0xDEADBEAF;
00129         pBuff[i++] = 0xBEAFDEAD;
00130         pBuff[i++] = 0xFF770088;
00131         pBuff[i++] = 0xBAD69DAD;
00132     }
00133 }
00134 
00135 static uint8_t  _VerifyData( 
00136     uint32_t AddrBegin, uint32_t AddrEnd, uint32_t *TestBuff , uint8_t secure)
00137 {
00138     static uint8_t TestPassed = 0;
00139     uint32_t i,j, Fault = 0;
00140     uint8_t *pBuffRx, *pBuffTx;
00141     
00142     pBuffTx = (uint8_t *)TestBuff;
00143     TRACE_INFO_WP("Verifying data from  0x%x  to 0x%x \n\r", AddrBegin, AddrEnd);
00144     if(secure) {
00145         TRACE_INFO(" Reading data with scramble ON \n\r");
00146     } else {
00147         TRACE_INFO(" Reading data with scramble OFF \n\r");
00148     }
00149     for(i = AddrBegin; i<AddrEnd;) {
00150       if(PeripheralInit==2)
00151       {
00152         S25FL1D_Read(Buffer, BUFFER_SIZE, i);
00153         pBuffRx = (uint8_t *)Buffer;
00154         pBuffRx = &pBuffRx[6];
00155       }
00156       else if(PeripheralInit==1)
00157       {
00158         S25FL1D_ReadQuadIO(Buffer, BUFFER_SIZE, i, 0, secure);
00159         pBuffRx = (uint8_t *)Buffer;
00160       } 
00161 
00162         
00163         for( j=0; j< BUFFER_SIZE; j++) {
00164             if(pBuffRx[j] != pBuffTx[j]) {
00165                 TestPassed = 1;
00166                 if(Fault==0)
00167                     printf("\n\rData does not match @ 0x%x ", i);
00168                 Fault++;
00169             } else {
00170                 if(Fault > 1)
00171                     printf("upto 0x%x \r\n", i);
00172                 Fault = 0;
00173             }
00174         }
00175         i +=BUFFER_SIZE;
00176     }
00177     
00178     if(Fault > 1) {
00179         TRACE_INFO_WP("upto 0x%x \r\n", i);
00180         Fault = 0;
00181     }
00182     
00183     if(TestPassed) {
00184         TRACE_ERROR("Data Does not match \n\r");
00185     }
00186     return TestPassed;
00187   
00188 }
00189 
00190 static void QSPI_UserMenu(void)
00191 {
00192   printf("\n\r===============Choose the peripheral==================");
00193   printf("\n\r       1. Run example as QSPI");
00194   printf("\n\r       2. Run example as SPI master");
00195   printf("\n\r       3. Display Menu");
00196   printf("\n\r======================================================\n\r");
00197 }
00198 /*----------------------------------------------------------------------------
00199  *         Global functions
00200  *----------------------------------------------------------------------------*/
00201 /**
00202  * \brief Application entry point for SPI with Serialflash example.
00203  * Initializes the serial flash and performs several tests on it.
00204  *
00205  * \return Unused (ANSI-C compatibility).
00206  */
00207 
00208 int main(void)
00209 {
00210     uint32_t i;
00211     uint32_t deviceId;
00212     uint8_t ucKey;
00213     uint8_t TestPassed = 0;
00214 
00215     /* Disable watchdog */
00216     WDT_Disable( WDT ) ;
00217 
00218     /* Output example information */
00219     printf( "-- QSPI Serialflash Example %s --\n\r", SOFTPACK_VERSION ) ;
00220     printf( "-- %s\n\r", BOARD_NAME ) ;
00221     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00222     SCB_EnableICache(); 
00223     SCB_EnableDCache();
00224     TimeTick_Configure();
00225 
00226     PIO_Configure(Qspi_pins, PIO_LISTSIZE(Qspi_pins));
00227     ENABLE_PERIPHERAL(ID_QSPI);
00228     
00229     QSPI_UserMenu();
00230     
00231     while ( 1 ) {
00232         ucKey = DBG_GetChar() ;
00233         
00234         switch ( ucKey ) {
00235           
00236         case '1' :
00237             S25FL1D_InitFlashInterface(1);
00238             TRACE_INFO("QSPI drivers initialized ");
00239             /* enable quad mode */
00240             S25FL1D_QuadMode(ENABLE);
00241             PeripheralInit = 1;
00242             break ;
00243           
00244         case '2' :
00245           S25FL1D_InitFlashInterface(0);
00246           TRACE_INFO("QSPI Initialized in SPI mode");
00247           S25FL1D_QuadMode(DISABLE);
00248           PeripheralInit = 2;
00249           break ;
00250         
00251         case '3' :
00252           QSPI_UserMenu() ;
00253           PeripheralInit = 0;
00254             break ;
00255         default:
00256           break;
00257         
00258     }
00259     
00260 
00261     if(PeripheralInit)
00262     {
00263       while(1) {
00264           deviceId = S25FL1D_ReadJedecId();
00265           printf("ID read: Manufacture ID = 0x%x, Device Type = 0x%x, \
00266               Capacity = 0x%x\n\r",
00267               (uint8_t)(deviceId), (uint8_t)(deviceId>>8), (uint8_t)(deviceId>>16));
00268           break;
00269       }
00270       /* erase entire chip  */
00271       S25FL1D_EraseChip();
00272       
00273       /* fill up the buffer*/
00274       _fillupbuffer(TestBuffer, BUFFER_SIZE);
00275       printf("Writing buffer to Flash memory...... \n\r");
00276 
00277       /* write the buffer to flash memory */
00278       for( i = 0; i<0x200000;) {
00279           S25FL1D_Write(TestBuffer, BUFFER_SIZE, i, 0);
00280           i += BUFFER_SIZE;
00281       }
00282       TestPassed = _VerifyData(0, 0x200000, TestBuffer, 0);
00283       
00284       printf("Erasing a block(64 KB) @ Add 0x10000 \n\r");
00285       S25FL1D_Erase64KBlock(0x10000);
00286       
00287       memset(TestBuffer, 0xFFFFFF, BUFFER_SIZE);
00288       TestPassed = _VerifyData( 0x10000, (0x10000 + 64*1024), TestBuffer, 0);
00289       
00290       if(TestPassed) {
00291           printf(" \n\r**** Test Failed ***** \n\r");
00292       } else {
00293         printf(" \n\r### Test Passed ###\n\r");
00294       }
00295       }
00296       PeripheralInit = 0;
00297       QSPI_UserMenu();
00298     }
00299 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines