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

📄 upsd_flash.c

📁 upsd_flash.c These functions are provided to help you develop your initial code. They are optim
💻 C
📖 第 1 页 / 共 2 页
字号:
/* upsd_flash.c

These functions are provided to help you develop your initial code. 
They are optimized for speed rather that size. As a result, you will 
see very few nested function calls. If speed is not critical, you 
can use function calls for common tasks (like dat polling after 
writing a byte to Flash) The penalty is the extra processor 
time to make the nested calls.

These files have been compiled using a C cross compiler from COSMIC 
Software Inc. You may have to implement some syntax changes to be 
compatible with other compilers. The intent of this generated C code 
is to provide you with a core of useful broadbased functions that are 
adaptable to many vendor's compilers and microcontrollers.


NOTES:

1.Some of the routines provided may not have been thoroughly tested.  
  Please check them in your system. If you find a bug, or a place 
  where the code could be improved, PLEASE forward your comments by 
  emailing to apps.psd@st.com. Any comments and feedback are 
  appreciated. Please tell us what you like and or what you think 
  can be improved.

2.The Software is provided "AS IS."
  LIMITATION OF LIABILITY:    NEITHER WSI NOR ITS VENDORS OR AGENTS
  SHALL BE LIABLE FOR ANY LOSS OF PROFITS, LOSS OF USE, LOSS OF DATA,
  INTERRUPTION OF BUSINESS, NOR FOR INDIRECT, SPECIAL, INCIDENTAL OR
  CONSEQUENTIAL DAMAGES OF ANY KIND WHETHER UNDER THIS AGREEMENT OR
  OTHERWISE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.

You are free to use/modify any of the provided code at your own risk
in your applications with the expressed limitation of liability above.
*/


#include "upsd3200.h"			// special function register declarations for UPSD
#include "upsd_flash.h"          // include the definition of your system memory map


/* Functions
   Group: Main Flash Memory
   Coverage: Program, Erase, Reset, Read ID, Read Protection */


/*  Module: flash_write_with_poll
    Programs a single byte, checks status using polling method.
    You'll need to include the header files generated by PSDsoft
    Express. Important: if memory paging is used, the correct 
    page value must be set in the PSD page register prior to 
    calling this function. */


unsigned char flash_write_with_poll(volatile unsigned char xdata* addr, unsigned char dat)
{
	unsigned char done;
	unsigned char error;
	unsigned char err;
	unsigned char poll;

	done = FALSE;
   	err = FALSE;

	//  Note:  the following constants (FLASH_COMMON_XXXX)
	//     are declared type volatile in the header file 
	//	   so they are not optimized away by the compiler

	
	*(FLASH_COMMON_X555) = 0xAA;     // unlock main flash, write 0xAA to addess 0xX555
	*(FLASH_COMMON_XAAA) = 0x55;     // unlock main flash, write 0x55 to addess 0xXAAA
	*(FLASH_COMMON_X555) = 0xA0;     // write 0xA0 command to program

	*(addr) = dat;                  // write byte to flash   
	
	dat = dat & NVM_DATA_POLL;     // get bit DQ7 of original dat   
	
	do                               // now use dat polling method to verify successful write
    {  

   		poll = *(addr);          // Read the location that was just programmed

		error = poll & NVM_ERROR;   // save timeout error bit at DQ5

		poll = poll & NVM_DATA_POLL;  // get DQ7 of poll byte read from flash  

		if (dat == poll)        // compare DQ7 

			done = TRUE;     // dat byte programmed into flash OK,
                                         // indicate successful exit criteria

		else if (error ==  NVM_ERROR )	 // check for timeout error   
			err = TRUE;      // indicate timeout error occurred

    } while((done == FALSE) && (err == FALSE)); 


	if (err == TRUE)                 // make sure timeout error and dat poll didn't 
                                         // occur simultaneously
	{
		poll = *(addr);          // Read location in flash again

		poll = poll & NVM_DATA_POLL;   // get DQ7 of poll byte read from flash  

		if (dat == poll)        // compare DQ7 

			done = TRUE;     // dat byte programmed into flash OK at the same
                                         // time timout error occured, indicate successful 
		                        // exit criteria

		*(FLASH_COMMON_X555) = 0xF0;  // reset the flash array (short reset instruction) 
		        // now delay 3 msec per dat sheet
    }

   	return(done);         // a successful flash write returns 1, timeout error returns 0
}

unsigned char flash_boot_write_with_poll(volatile unsigned char xdata* addr, unsigned char dat)
{
	unsigned char done;
	unsigned char error;
	unsigned char err;
	unsigned char poll;

	done = FALSE;
   	err = FALSE;

	//  Note:  the following constants (FLASH_BOOT_XXXX)
	//     are declared type volatile in the header file 
	//	   so they are not optimized away by the compiler

	
	*(FLASH_BOOT_X555) = 0xAA;     // unlock main flash, write 0xAA to addess 0xX555
	*(FLASH_BOOT_XAAA) = 0x55;     // unlock main flash, write 0x55 to addess 0xXAAA
	*(FLASH_BOOT_X555) = 0xA0;     // write 0xA0 command to program

	*(addr) = dat;                  // write byte to flash   
	
	dat = dat & NVM_DATA_POLL;     // get bit DQ7 of original dat   
	
	do                               // now use dat polling method to verify successful write
         {  

   		poll = *(addr);          // Read the location that was just programmed

		error = poll & NVM_ERROR;   // save timeout error bit at DQ5

		poll = poll & NVM_DATA_POLL;  // get DQ7 of poll byte read from flash  

		if (dat == poll)        // compare DQ7 

			done = TRUE;     // dat byte programmed into flash OK,
                                         // indicate successful exit criteria

		else if (error ==  NVM_ERROR )	 // check for timeout error   
			err = TRUE;      // indicate timeout error occurred

          } while((done == FALSE) && (err == FALSE)); 


	if (err == TRUE)                 // make sure timeout error and dat poll didn't 
                                         // occur simultaneously
	{
		poll = *(addr);          // Read location in flash again

		poll = poll & NVM_DATA_POLL;   // get DQ7 of poll byte read from flash  

		if (dat == poll)        // compare DQ7 

			done = TRUE;     // dat byte programmed into flash OK at the same
                                         // time timout error occured, indicate successful 
		                        // exit criteria

		*(FLASH_BOOT_X555) = 0xF0;  // reset the flash array (short reset instruction) 
		        // now delay 3 msec per dat sheet
        }

   	return(done);         // a successful flash write returns 1, timeout error returns 0
}



/* Module: flash_write_with_toggle
   Programs a single byte, checks status using toggle method.
   You'll need to include the header files generated by PSDsoft
   Express. Important: if memory paging is used, the correct page 
   value must be set in the PSD page register prior to calling this 
   function. */

unsigned char flash_write_with_toggle(addr,dat)	
volatile unsigned char *addr;
unsigned char dat;

	{
	unsigned char done;
	unsigned char error;
	unsigned char err;
	volatile unsigned char toggle_A;
	volatile unsigned char toggle_B;

	done = FALSE;
   	err = FALSE;

	//  Note:  the following constants (FLASH_BOOT_XXXX)
	//     are declared type volatile in the header file 
	//	   so they are not optimized away by the compiler

	*(FLASH_BOOT_X555) = 0xAA;		// unlock main flash, write 0xAA to addess 0xX555
	*(FLASH_BOOT_XAAA) = 0x55;		// unlock main flash, write 0x55 to addess 0xXAAA
	*(FLASH_BOOT_X555) = 0xA0;		// write 0xA0 command to program

	*(addr) = dat;		// write byte to flash   

		// now use toggling method to verify successful write

	toggle_A = *(addr);			// Read the location that was just programmed

	toggle_A = toggle_A & NVM_DATA_TOGGLE;	// mask toggle bit at DQ6
   		  								
	do
		{  
   		toggle_B = *(addr);		// Again read the location that was just programmed

		error = toggle_B & NVM_ERROR;	// save timeout error flag at DQ5

		toggle_B = toggle_B & NVM_DATA_TOGGLE;	// mask toggle bit at DQ6
   		  								
		if (toggle_A == toggle_B)	// compare toggle bit DQ6

			done = TRUE;		// bit did not toggle, dat byte programmed into 
						// flash OK, indicate successful exit criteria
		else
			{
	 		 if (error ==  NVM_ERROR )	// check for timeout error   
				err = TRUE; 	// indicate timeout error occurred

			toggle_A = toggle_B;  	// save most recent sample of toggle bit 
						// to compare with next sample
			}

		} while((done == FALSE) && (err == FALSE)); 


	if (err == TRUE)			// make sure timeout error and dat toggle didn't 
						// occur simultaneously
		{
		toggle_B = *(addr);		// Read location in flash again

		toggle_B = toggle_B & NVM_DATA_TOGGLE;	// mask toggle bit at DQ6

		if (toggle_A == toggle_B)		// compare toggle bit DQ6

			done = TRUE;			// dat byte programmed into flash OK at the same
							// time timout error occured, indicate successful 
							// exit criteria

		*(FLASH_BOOT_X555) = 0xF0;  // reset the flash array (short reset instruction) 
		        // now delay 3 msec per dat sheet
		}

   	return(done);		// a successful flash write returns 1, timeout error returns 0
	}


/* Module: flash_erase_bulk
   Erases the entire main Flash memory (all sectors).
   You'll need to include the header files generated by PSDsoft
   Express. Important: The address passed to this function should 
   be independent of memory paging or else the PSD page 
   register value should be set to the correct page prior to calling this 

⌨️ 快捷键说明

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