⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 Nor flash application in at91sam9260
💻 C
字号:
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include <board.h>
#include <board_memories.h>
#include <utility/trace.h>
#include <utility/math.h>
#include <memories/norflash/NorFlashCFI.h>
#include <memories/norflash/NorFlashApi.h>

#include <string.h>

//------------------------------------------------------------------------------
//         Local variables
//------------------------------------------------------------------------------

/// Norflash device structure.
static struct NorFlash norFlash;

/// Pins to configure for the application
static const Pin pPins[] = {PINS_DBGU
#ifdef PINS_NORFLASH
    , PINS_NORFLASH
#endif
};

/// Temporary buffer for unaligned read/write operations.
static unsigned char pBuffer[1024];

//------------------------------------------------------------------------------
//         Global functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Tests the norflash connected to the board by performing several command
/// on each of its pages.
//------------------------------------------------------------------------------
int main(void)
{
    unsigned int blockNumber, block, blockSize, blockAddress, pageSize, packetSize, i;
    unsigned char testFailed;
    const unsigned char busWidth[3] = {FLASH_CHIP_WIDTH_8BITS, FLASH_CHIP_WIDTH_16BITS, FLASH_CHIP_WIDTH_32BITS};

    // Configure pins and DBGU
    PIO_Configure(pPins, PIO_LISTSIZE(pPins));
    DBGU_Configure(DBGU_STANDARD, 115200, BOARD_MCK);
    printf("-- Basic NorFlash Project 1.4 --\n\r");

    norFlash.norFlashInfo.baseAddress = BOARD_NORFLASH_ADDR;
    // Check device CFI and get Vendor setting from it.
    printf("-I- \t Common Flash Interface detecting...\n\r");
    for (i = 0; i < 3; i++) {
        // Configure SMC for Norflash accesses
        trace_LOG(trace_INFO, "-I- \t Try bus width %d bits\n\r", busWidth[i] * 8);
        BOARD_ConfigureNorFlash48MHz(busWidth[i] * 8);
        if(! NorFlash_CFI_Detect(&norFlash, busWidth[i])) break;
    }

    if (norFlash.norFlashInfo.cfiCompatible == 0) {
        printf("-E- \tDevice Unknown\n\r");
        testFailed = 1;
        goto exit;
    }

    printf("-I- \t CFI detected and driver initialized\n\r");
    printf("-I- \t manufactureID : 0x%08x, deviceID : 0x%08x\n\r",
    		NORFLASH_ReadManufactoryID(&norFlash),
            NORFLASH_ReadDeviceID(&norFlash));

    // Test all pages
  	testFailed = 0;
	block = 0;
	blockNumber = NorFlash_GetDeviceNumOfBlocks(&(norFlash.norFlashInfo));
	pageSize = min(NorFlash_GetDeviceMinBlockSize(&(norFlash.norFlashInfo)), 1024);

	while (!testFailed && (block < blockNumber)) {

		trace_LOG(trace_INFO, "-I- Test in progress on block: %6u\r", block);
        // Erase block
        NORFLASH_EraseSector(&norFlash, NorFlash_GetDeviceSectorAddress(&(norFlash.norFlashInfo), block));

		blockSize =  NorFlash_GetDeviceBlockSize(&(norFlash.norFlashInfo), block);
		blockAddress = NorFlash_GetDeviceSectorAddress(&(norFlash.norFlashInfo), block);
		packetSize = pageSize;
		
		while (blockSize) {
	        // Verify that page has been erased correctly
	        memset(pBuffer, 0, packetSize);
			NORFLASH_ReadData(&norFlash, blockAddress, pBuffer, packetSize);
			for (i=0; i < packetSize; i++) {
	
				if (pBuffer[i] != 0xFF) {
	                printf("\n-E- Could not erase block %u\n\r", block);
					testFailed = 1;
	                goto exit;
	            }
	        }
	
	        blockAddress += packetSize;
	        blockSize -= packetSize;
	        if (blockSize < pageSize) {
	        	 packetSize = blockSize;
	        }
	    }
	
        blockSize =  NorFlash_GetDeviceBlockSize(&(norFlash.norFlashInfo), block);
		blockAddress = NorFlash_GetDeviceSectorAddress(&(norFlash.norFlashInfo), block);
		packetSize = pageSize;
		
		while (blockSize) {
	    	// Write page
			for (i = 0; i < packetSize; i++) {
				pBuffer[i] = i & 0xFF;
	    	}
    		NORFLASH_WriteData(&norFlash, blockAddress, pBuffer, packetSize);
        	// Check that data has been written correctly                	
        	memset(pBuffer, 0, packetSize);
			NORFLASH_ReadData(&norFlash, blockAddress, pBuffer, packetSize);
		
			for (i = 0; i < packetSize; i++) {
				if (pBuffer[i] != (i & 0xFF)) {
	                printf("\n-E- Could not write block %u\n\r", block);
					testFailed = 1;
	                goto exit;
            	}
            }
            blockAddress += packetSize;
	        blockSize -= packetSize;
	        if (blockSize < pageSize) {
	        	 packetSize = blockSize;
	        }
    	}
    	block++;	
	}
	exit:
    // Display test result
	if (testFailed) {

		printf("-E- Test failed.\n\r");
    }
	else {

 		printf("\n\r-I- Test passed.\n\r");
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -