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

📄 ecc.c

📁 mx21的NAND Flash Bootloader源代码
💻 C
字号:
/*************************************************************************Title:   	Filename:   $Header:$Hardware:   MX21Summay:License:    The contents of this file are subject to the Mozilla Public            License Version 1.1 (the "License"); you may not use this file            except in compliance with the License. You may obtain a copy of            the License at http://www.mozilla.org/MPL/Author:   ====================Change Log========================$Log:$********************************************************************************////@ingroup    NAND_BOOTLOADER///@file 	ecc.c///@brief 	ECC interface for data read/write.//////@remark/////////@bug///@version	$Version$ //<<<<<Include#include "uart.h"#include "nand_ecc.c"//>>>>>Include//<<<<<< Private Macro//>>>>>> Private Macro//<<<<<< Private Structurestruct nand_oob_config {    int	ecc_pos[6];    int	badblock_pos;    int	eccvalid_pos;};//>>>>>> Private Structure//<<<<<< Global Variablestatic  struct nand_oob_config oob_config;static	int oobblock = 512;//>>>>>> Global Variable//<<<<<Private Function Declearation//>>>>>Private Function Declearation//<<<<<Body/* ecc init function */void ecc_init(){	oob_config.ecc_pos[0] = 0;	oob_config.ecc_pos[1] = 1;	oob_config.ecc_pos[2] = 2;	oob_config.ecc_pos[3] = 3;	oob_config.ecc_pos[4] = 6;	oob_config.ecc_pos[5] = 7;	oob_config.badblock_pos = 5;	oob_config.eccvalid_pos = 4;}/* ecc handle interface after read a page */void mx2_read_ecc( char* buff, char* para , int page){	char	main_buf[512];	char	oob_buf[16];	char	ecc_code[6];	int	i;	char*	p1 = buff;	char*	p2 = para;	unsigned char	ecc_calc[6];	/* Write the ecc_pos for 8 bit nand flash with 16 byte oob size */	ecc_init();	/* Copy the main buffer and para buff into array */	for( i = 0; i < 512; i++ )	    main_buf[i] = *p1++;		for( i = 0; i < 16; i++ )	    oob_buf[i] = *p2++;		/* Pick the ECC bytes out of the oob data */	for( i = 0; i < 6; i++ )	    ecc_code[i] = oob_buf[oob_config.ecc_pos[i]];	/* Calculate the ECC and verify it */	/* If block was not written with ECC, skip ECC */	if (oob_config.eccvalid_pos != -1 && 		(ecc_code[oob_config.eccvalid_pos] & 0x0f) != 0x0f)	{	    nand_calculate_ecc (&main_buf[0], &ecc_calc[0]);	    switch (nand_correct_data (&main_buf[0],&ecc_code[0], &ecc_calc[0]))	    {		case -1:			break;		case 1:		case 2:	/* transfer ECC corrected data to cache */			for(i=0,p1=buff;i<256;i++)			    *p1++ = main_buf[i];			break;	    }	}	/* Calculate the ECC for upper 256 bytes of data in this page */	/* Has some problem need resolved. So, just ignore the output */	if (oob_config.eccvalid_pos != -1 && 		oobblock == 512 && (ecc_code[oob_config.eccvalid_pos] & 0xf0) != 0xf0)	{	    nand_calculate_ecc (&main_buf[256],&ecc_calc[3]);	    switch (nand_correct_data (&main_buf[256],&ecc_code[3], &ecc_calc[3]))	    {		case -1:			break;		case 1:		case 2:	/* transfer ECC corrected data to cache */			if (buff)			    for(i=0,p1=buff+256;i<256;i++)				*p1++ = main_buf[256+i];			break;	    }	}}/* ecc handle interface before write a page */void mx2_write_ecc( char* buff, char* para, int last ){	char	main_buf[512];	char	oob_buf[16];	char	ecc_code[6];	int	i;	char*	p1 = buff;	char*	p2 = para;	/* Write the ecc_pos for 8 bit nand flash with 16 byte oob size */	ecc_init();		/* Zero out the ECC array */	for(i=0; i<6; i++)		ecc_code[i] = 0x00;		/* pad oob area */	for(i=0;i < 16; i++)		oob_buf[i] = 0xff;			/* Copy the main buffer into array */	for( i = 0; i < 512; i++ )	    main_buf[i] = *p1++;	/* Calculate and write the ECC if we have enough data */	if ((last >= 256)) {		nand_calculate_ecc (&main_buf[0], &(ecc_code[0]));		for (i=0 ; i<3 ; i++)			oob_buf[oob_config.ecc_pos[i]] = ecc_code[i];		if (oob_config.eccvalid_pos != -1)			oob_buf[oob_config.eccvalid_pos] = 0xf0;	}	/* Calculate and write the second ECC if we have enough data */	if ((oobblock == 512) && (last == oobblock)) {		nand_calculate_ecc (&main_buf[256], &(ecc_code[3]));		for (i=3 ; i<6 ; i++)			oob_buf[oob_config.ecc_pos[i]] = ecc_code[i];		if (oob_config.eccvalid_pos != -1)			oob_buf[oob_config.eccvalid_pos] &= 0x0f;	}	for(i=0;i<16;i++)		*p2++ = oob_buf[i];}//>>>>>Body

⌨️ 快捷键说明

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