void main (void) { char ch; // Used to store character from UART unsigned char c; // Used to store character from UART FLADDR bla; PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer // enable) PORT_Init(); // Initialize Port I/O SYSCLK_Init (); // Initialize Oscillator UART0_Init(); P3_0 = 1; P3_2 = 1; EA = 0; bla = 0x0000; while (1) { ch = FLASH_ByteRead(bla); c = FLASH_ByteRead(bla); printf ("\n0x%x: 0x%x %d", bla , c, c+1); bla ++; if (bla == 0x3e00) break; } P3_0 = 1; P3_2 = 0; while (1); }
void FLASH_Copy (FLADDR dest, FLADDR src, unsigned int numbytes) { FLADDR i; for (i = 0; i < numbytes; i++) { FLASH_ByteWrite ((FLADDR) dest+i, FLASH_ByteRead((FLADDR) src+i)); } }
//----------------------------------------------------------------------------- // FLASH_Read //----------------------------------------------------------------------------- // // This routine copies <numbytes> from the linear FLASH address <src> to // <dest>. // char * FLASH_Read (char *dest, FLADDR src, unsigned numbytes) { FLADDR i; for (i = 0; i < numbytes; i++) { *dest++ = FLASH_ByteRead (src+i); } return dest; }
char * FLASH_Read (char *dest, FLADDR src, unsigned int numbytes) { FLADDR i; // Pause_Measure() ; for (i = 0; i < numbytes; i++) { *dest++ = FLASH_ByteRead (src+i); } // Restart_Measure() ; return dest; }
/*功能码03读*/ void readRegisters(void) { uint16 addr; uint16 tempAddr; // uint16 result; uint16 crcData; uint8 readCount; uint8 byteCount; // uint8 finsh; // uint8 i; uint16 tempData = 0; tempData=mod_buf[2]; addr = (tempData<<8) + mod_buf[3]; tempAddr = addr ;//& 0xfff; //addr = mod_buf[3]; //tempAddr = addr; //readCount = (receBuf[4]<<8) + receBuf[5]; readCount = mod_buf[5]; if(readCount<MAXREG) { byteCount = readCount* 2 ;//; for(i=0;i<byteCount;i++,tempAddr++) { tempData=FLASH_ByteRead(tempAddr); //getRegisterVal(tempAddr,&tempData); sendBuf[i+3] = tempData;// & 0xff; } sendBuf[0] = localAddr; sendBuf[1] = 3; sendBuf[2] = byteCount; byteCount += 3; crcData = crc_chk(sendBuf,byteCount); sendBuf[byteCount] = crcData & 0xff; byteCount++; sendBuf[byteCount] = crcData >> 8; sendCount = byteCount + 1; beginSend(); }
/* begin_addr,被写数据Flash开始地址;counter,连续写多少个字节; array[],数据来源 */ UCHAR sequential_write_flash_in_one_sector(UINT begin_addr, UINT counter, UCHAR array[]) { UINT i = 0; UINT in_sector_begin_addr = 0; UINT sector_addr = 0; FLASH_PageErase(begin_addr); for(i=0; i<counter; i++) { /* 写一个字节 */ FLASH_ByteWrite(begin_addr, array[i]); /* 比较对错 */ if (FLASH_ByteRead(begin_addr) != array[i]) { IAP_Disable(); return 0; } begin_addr++; } IAP_Disable(); return 1; }
void main (void) { unsigned char temp_byte = 0x00; FLADDR start_address = 0x5FFE; char test_write_buff[8] = "ABCDEFG"; char test_write_buff2[3] = "HIJ"; char test_read_buff[8] = {0}; char test_compare_buff[8] = "ABCDEFG"; unsigned char i; bit error_flag = 0; PCA0MD &= ~0x40; // WDTE = 0 (clear watchdog timer // enable) if ((RSTSRC & 0x02) != 0x02) { if ((RSTSRC & 0x40) == 0x40) { LED = 0; while(1); // Last reset was caused by a Flash // Error Device Reset // LED is off and loop forever to // indicate error } } Oscillator_Init(); // Initialize the internal oscillator // to 24.5 MHz VDDMon_Init(); Port_Init(); LED = 1; SFRPAGE = LEGACY_PAGE; // Initially erase the test page of Flash FLASH_PageErase(start_address); //BEGIN TEST================================================================ // Check if able to Write and Read the Flash-------------------------------- FLASH_ByteWrite(start_address, 0xA5); temp_byte = FLASH_ByteRead(start_address); if (temp_byte != 0xA5) { error_flag = 1; } //-------------------------------------------------------------------------- // Check if able to Erase a page of the Flash------------------------------- FLASH_PageErase(start_address); temp_byte = FLASH_ByteRead(start_address); if (temp_byte != 0xFF) { error_flag = 1; } //-------------------------------------------------------------------------- // Check if able to write and read a series of bytes------------------------ FLASH_Write(start_address, test_write_buff, sizeof(test_write_buff)); FLASH_Read(test_read_buff, start_address, sizeof(test_write_buff)); for (i = 0; i < sizeof(test_write_buff); i++) { if (test_read_buff[i] != test_write_buff[i]) { error_flag = 1; } } //-------------------------------------------------------------------------- // Check if able to Erase a few bytes--------------------------------------- FLASH_Clear(start_address, 2); FLASH_Read(test_read_buff, start_address, sizeof(test_write_buff)); // Simulate the same changes to a data array for comparison test_compare_buff[0] = 0xFF; test_compare_buff[1] = 0xFF; for (i = 0; i < sizeof(test_compare_buff); i++) { if (test_read_buff[i] != test_compare_buff[i]) { error_flag = 1; } } //-------------------------------------------------------------------------- // Check if able to "update" (erase then re-write) a few bytes-------------- FLASH_Update (start_address, test_write_buff2, 3); FLASH_Read(test_read_buff, start_address, sizeof(test_write_buff)); // Simulate the same changes to a data array for comparison test_compare_buff[0] = test_write_buff2[0]; test_compare_buff[1] = test_write_buff2[1]; test_compare_buff[2] = test_write_buff2[2]; for (i = 0; i < sizeof(test_compare_buff); i++) { if (test_read_buff[i] != test_compare_buff[i]) { error_flag = 1; } } //-------------------------------------------------------------------------- // Check if able to copy data in the Flash---------------------------------- FLASH_Copy (start_address+sizeof(test_write_buff), start_address, sizeof(test_write_buff)); FLASH_Read(test_read_buff, start_address+sizeof(test_write_buff), sizeof(test_read_buff)); for (i = 0; i < sizeof(test_write_buff); i++) { if (test_read_buff[i] != test_compare_buff[i]) { error_flag = 1; } } //-------------------------------------------------------------------------- // FLASH test routines------------------------------------------------------ FLASH_Fill (start_address+sizeof(test_write_buff)*2, sizeof(test_write_buff), 0x5A); FLASH_Read(test_read_buff, start_address+sizeof(test_write_buff)*2, sizeof(test_write_buff)); for (i = 0; i < sizeof(test_write_buff); i++) { if (test_read_buff[i] != 0x5A) { error_flag = 1; } } //-------------------------------------------------------------------------- //END OF TEST=============================================================== while (1) // Loop forever { // Blink LED to indicate success if (error_flag == 0) { LED = ~LED; Timer0_Delay_ms (100); } else { LED = 0; } } }