📄 flash.c
字号:
/******************************************************************************* * * Filename: flash.c * * Instantiation of flash control routines supporting AM29LV017/033 chips. * * Revision information: * * 20SEP2004 kb_admin initial creation * * BEGIN_KBDD_BLOCK * No warranty, expressed or implied, is included with this software. It is * provided "AS IS" and no warranty of any kind including statutory or aspects * relating to merchantability or fitness for any purpose is provided. All * intellectual property rights of others is maintained with the respective * owners. This software is not copyrighted and is intended for reference * only. * END_BLOCK ******************************************************************************/#include "AT91RM9200.h"#include "debug_io.h"#include "flash.h"/* ********************** PRIVATE FUNCTIONS/DATA ******************************//* * .KB_C_FN_DEFINITION_START * void int EraseSector(char *address) * Private function to erase the sector at address and return non-zero * on failure. * .KB_C_FN_DEFINITION_END */static int EraseSector(char *address) { char val1, val2, *end; int failure = 0; *address = 0xF0; *address = 0xAA; *address = 0x55; *address = 0x80; *address = 0xAA; *address = 0x55; *address = 0x30; val1 = *address; while (!failure) { val2 = *address; if (val2 == 0xFF) break; if (((val1 & val2) & 0x40)) break; if (val2 & 0x20) failure = 1; val1 = val2; } if (failure) { DebugPrint("Flash failed sector erase . . . verifying\n\r"); } else { DebugPrint("Verifying sector erase\n\r"); } failure = 0; address = (char*)(((unsigned)address) & ~(FLASH_SECTOR_SIZE - 1)); end += FLASH_SECTOR_SIZE; for ( ; (address < end) && !failure; ++address) { if (*address != 0xFF) failure = 1; } DebugPrint("Flash sector erase at: "); DebugPrintHex(8, (unsigned)address); if (failure) { DebugPrint(" FAIL\n\r"); } else { DebugPrint(" PASS\n\r"); } return (failure);}/* * .KB_C_FN_DEFINITION_START * int ProgramByte(char *address, char data) * Private function to program a byte at the specified address. * Returns non-zero on success. * .KB_C_FN_DEFINITION_END */static int ProgramByte(char *address, char data) { int failure = 0, count = 0; char read; if (*address != 0xFF) { return (1); } *address = 0xAA; *address = 0x55; *address = 0xA0; *address = data; while (!failure) { if ((read = *address) == data) break; if (read & 0x20) { if (*address == data) break; } if (++count > 0x10000) failure = 1; } return (failure);}/* ************************** GLOBAL FUNCTIONS ********************************//* * .KB_C_FN_DEFINITION_START * void EraseFlashSector(unsigned start,unsigned end) * Global function to erase the sectors from start to end. * .KB_C_FN_DEFINITION_END */void EraseFlashSector(unsigned start, unsigned end) { if ((start < FLASH_BASE) || (start >= FLASH_MAX) || (end < FLASH_BASE) || (end >= FLASH_MAX) || (start > end)) return ; while (start < end) { if (EraseSector((char*)start)) { return ; } start += FLASH_SECTOR_SIZE; }}/* * .KB_C_FN_DEFINITION_START * void EraseFlashChip(void) * Global function to erase the entire flash chip and verify results. * .KB_C_FN_DEFINITION_END */void EraseFlashChip(void) { char val1, val2, *address; int failure = 0; address = (char*)FLASH_BASE; *address = 0xF0; *address = 0xAA; *address = 0x55; *address = 0x80; *address = 0xAA; *address = 0x55; *address = 0x10; val1 = *address; while (!failure) { val2 = *address; if (((val1 & val2) & 0x40)) break; if (val2 & 0x20) failure = 1; val1 = val2; } if (failure) { DebugPrint("Flash failed algorithm. . verifying\n\r"); } else { DebugPrint("Verifying erase\n\r"); } failure = 0; for (address = (char*)FLASH_BASE; (address < (char*)FLASH_MAX) && !failure; ++address) { if (*address != 0xFF) failure = 1; } DebugPrint("Flash erase: "); if (failure) { DebugPrint("FAIL at "); DebugPrintHex(8, (unsigned)address); DebugPrint("\n\r"); } else { DebugPrint("PASS\n\r"); }}/* * .KB_C_FN_DEFINITION_START * void ProgramFlash(unsigned target, unsigned source, unsigned size) * Global function to program flash starting at target using data supplied * at source for size bytes. * .KB_C_FN_DEFINITION_END */void ProgramFlash(unsigned target, unsigned source, unsigned size) { char *src, *dest, failure = 0;; if ((target < FLASH_BASE) || (target > FLASH_MAX) || ((target + size) > FLASH_MAX)) return ; src = (char*)source; for (dest = (char*)target;(!failure) && (size--); ++src, ++dest) { if (!(size & 0xFFFF)) DebugPrint("."); failure = ProgramByte(dest, *src); } DebugPrint("\n\rFlash program status: "); if (failure) { DebugPrint("FAIL at: "); DebugPrintHex(8, (unsigned)dest); DebugPrint("\n\r"); } else { DebugPrint("PASS\n\r"); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -