SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002 *         SAM Software Package License
00003 * ----------------------------------------------------------------------------
00004 * Copyright (c) 2015, 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 /** Pins to configure for the application. */
00108 static Pin Qspi_pins[] =  PINS_QSPI;
00109 
00110 /** buffer for test QSPI flash */
00111 COMPILER_ALIGNED(32) static uint32_t Buffer[BUFFER_SIZE],
00112                 TestBuffer[BUFFER_SIZE];
00113 
00114 static uint8_t PeripheralInit = 0;
00115 /*----------------------------------------------------------------------------
00116  *         Local functions
00117  *----------------------------------------------------------------------------*/
00118 
00119 
00120 static void _fillupbuffer(uint32_t *pBuff, uint32_t size)
00121 {
00122     uint32_t i;
00123 
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",
00144         (unsigned int)AddrBegin, (unsigned int)AddrEnd);
00145 
00146     if (secure) {
00147         TRACE_INFO(" Reading data with scramble ON \n\r");
00148     } else {
00149         TRACE_INFO(" Reading data with scramble OFF \n\r");
00150     }
00151 
00152     for (i = AddrBegin; i < AddrEnd; ) {
00153         if (PeripheralInit == 2) {
00154             S25FL1D_Read(Buffer, BUFFER_SIZE, i);
00155             pBuffRx = (uint8_t *)Buffer;
00156             pBuffRx = &pBuffRx[6];
00157         } else if (PeripheralInit == 1) {
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 
00167                 if (Fault == 0)
00168                     printf("\n\rData does not match @ 0x%x ", (unsigned int)i);
00169 
00170                 Fault++;
00171             } else {
00172                 if (Fault > 1)
00173                     printf("upto 0x%x \r\n", (unsigned int)i);
00174 
00175                 Fault = 0;
00176             }
00177         }
00178 
00179         i += BUFFER_SIZE;
00180     }
00181 
00182     if (Fault > 1) {
00183         TRACE_INFO_WP("upto 0x%x \r\n", (unsigned int)i);
00184         Fault = 0;
00185     }
00186 
00187     if (TestPassed)
00188         TRACE_ERROR("Data Does not match \n\r");
00189 
00190     return TestPassed;
00191 
00192 }
00193 
00194 static void QSPI_UserMenu(void)
00195 {
00196     printf("\n\r===============Choose the peripheral==================");
00197     printf("\n\r       1. Run example as QSPI");
00198     printf("\n\r       2. Run example as SPI master");
00199     printf("\n\r       3. Display Menu");
00200     printf("\n\r======================================================\n\r");
00201 }
00202 /*----------------------------------------------------------------------------
00203  *         Global functions
00204  *----------------------------------------------------------------------------*/
00205 /**
00206  * \brief Application entry point for SPI with Serialflash example.
00207  * Initializes the serial flash and performs several tests on it.
00208  *
00209  * \return Unused (ANSI-C compatibility).
00210  */
00211 
00212 int main(void)
00213 {
00214     uint32_t i;
00215     uint32_t deviceId;
00216     uint8_t ucKey;
00217     uint8_t TestPassed = 0;
00218 
00219     /* Disable watchdog */
00220     WDT_Disable(WDT);
00221 
00222     /* Output example information */
00223     printf("-- QSPI Serialflash Example %s --\n\r", SOFTPACK_VERSION);
00224     printf("-- %s\n\r", BOARD_NAME);
00225     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME);
00226     SCB_EnableICache();
00227     SCB_EnableDCache();
00228     TimeTick_Configure();
00229 
00230     PIO_Configure(Qspi_pins, PIO_LISTSIZE(Qspi_pins));
00231     ENABLE_PERIPHERAL(ID_QSPI);
00232 
00233     QSPI_UserMenu();
00234 
00235     while (1) {
00236         ucKey = DBG_GetChar();
00237 
00238         switch (ucKey) {
00239         case '1' :
00240             S25FL1D_InitFlashInterface(1);
00241             TRACE_INFO("QSPI drivers initialized ");
00242             /* enable quad mode */
00243             S25FL1D_QuadMode(ENABLE);
00244             PeripheralInit = 1;
00245             break;
00246 
00247         case '2' :
00248             S25FL1D_InitFlashInterface(0);
00249             TRACE_INFO("QSPI Initialized in SPI mode");
00250             S25FL1D_QuadMode(DISABLE);
00251             PeripheralInit = 2;
00252             break;
00253 
00254         case '3' :
00255             QSPI_UserMenu();
00256             PeripheralInit = 0;
00257             break;
00258 
00259         default:
00260             break;
00261 
00262         }
00263 
00264 
00265         if (PeripheralInit) {
00266             while (1) {
00267                 deviceId = S25FL1D_ReadJedecId();
00268                 printf("ID read: Manufacture ID = 0x%x, Device Type = 0x%x, \
00269                 Capacity = 0x%x\n\r",
00270                        (uint8_t)(deviceId), (uint8_t)(deviceId >> 8), (uint8_t)(deviceId >> 16));
00271                 break;
00272             }
00273 
00274             /* erase entire chip  */
00275             S25FL1D_EraseChip();
00276 
00277             /* fill up the buffer*/
00278             _fillupbuffer(TestBuffer, BUFFER_SIZE);
00279             printf("Writing buffer to Flash memory...... \n\r");
00280 
00281             /* write the buffer to flash memory */
00282             for (i = 0; i < 0x200000; ) {
00283                 S25FL1D_Write(TestBuffer, BUFFER_SIZE, i, 0);
00284                 i += BUFFER_SIZE;
00285             }
00286 
00287             TestPassed = _VerifyData(0, 0x200000, TestBuffer, 0);
00288 
00289             printf("Erasing a block(64 KB) @ Add 0x10000 \n\r");
00290             S25FL1D_Erase64KBlock(0x10000);
00291 
00292             memset(TestBuffer, 0xFFFFFF, BUFFER_SIZE);
00293             SCB_CleanDCache_by_Addr((uint32_t *)TestBuffer, sizeof(TestBuffer));
00294             TestPassed = _VerifyData(0x10000, (0x10000 + 64 * 1024), TestBuffer, 0);
00295 
00296             if (TestPassed)
00297                 printf(" \n\r**** Test Failed ***** \n\r");
00298             else
00299                 printf(" \n\r### Test Passed ###\n\r");
00300         }
00301 
00302         PeripheralInit = 0;
00303         QSPI_UserMenu();
00304     }
00305 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines