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

📄 my_640j3.c

📁 640J3的Flash的驱动和测试程序
💻 C
📖 第 1 页 / 共 3 页
字号:
	    address = STD_READ_MODE;
	}
	address = address << 1;  /* shift so address is on A16..A1 */
	address += BASE_FLASH_ADDRESS; /* offset by base flash mem */

	/* write Read Configuration command */
	WriteFlash( BASE_FLASH_ADDRESS, CONFIG_SETUP );
	WriteFlash( address, SET_READ_CONFIG );
	stat.Result = StatCompleted;

	return( stat );
}

/****************************************************************************
 * Function:
 *    This procedure is called to program a block.
 *
 * Parameters: address ---- The program address
 *             item ---- The value to be programmed
 *
 * Returns: stat ---- The flash status
 ***************************************************************************/
/************************** Program Block Flash ***************************/
Flash_Status ProgramFlash( UINT32 address, UINT16 item )
{
    Flash_Status stat;
    UINT16 writedata;
    //UINT16 data2 = 0x8686;

    if ( (address - BASE_FLASH_ADDRESS ) >= TOTAL_SIZE )
    {
        stat.Result = StatBadAddress;    /* address over */

        return( stat );
    }

    WriteFlash( BASE_FLASH_ADDRESS, CLEAR_STATUS_REGISTER );  /* Clear status register 的目的是什么 */
    stat.SR = ReadStatus();
    writedata =  item;

    *(UINT16 *)address = 0x0040;      //Programming command data
    *(UINT16 *)address = writedata;   //AA55
    Delay(1000);
    
    //*(UINT16 *)address = 0x0040;
    //*(UINT16 *)address = data2;
    //Delay(1000);
//ManufactureCode = ReadFlash( BASE_FLASH_ADDRESS );
//    WriteFlash(address, PROGRAM_SETUP );    /* Byte/Word program command 0x0040 */

      /* Send Word Data into Flash */
///    GetIo32(address) = writedata;

    if ( !WaitUntilReady( PROGRAM_TIMEOUT ) )
    {
        stat.Result = StatTimeout;
    }
    else
    {
        stat.Result = StatCompleted;
    }

    stat.SR = ReadStatus();

    /* return device to read array mode */
    WriteFlash(BASE_FLASH_ADDRESS, READ_ARRAY );

    return( stat );
}

/****************************************************************************
 * Function:
 *    This procedure is called to program the flash device at the specified
 *    starting address contiguously with the specified buffer data.
 *
 * Parameters: address ---- The flash address to be programmed
 *             buffer ---- The buffer containing data to be programmed.
 *             numbytes ---- The number of data items contained in the buffer.
 *
 * Returns: stat ---- The flash status
***************************************************************************/
/******************** Program Flash Buffered ******************************/
Flash_Status ProgramFlashBuffered( UINT32 address, BYTE_PTR buffer, UINT32 numbytes )
{
Flash_Status   stat;
//UINT32_PTR     fptr;
UINT16         writedata;
UINT32         numitems;
UINT32         cmndaddress;
UINT32         numwritebytes;
UINT32         byteswritten;

cmndaddress = address;

if ( ( address + numbytes - BASE_FLASH_ADDRESS ) > TOTAL_SIZE )
{
    stat.Result = StatBadAddress;

    return( stat );
}

if (( cmndaddress & 0x01 ))
{
    cmndaddress --;
}

if ( !WaitUntilReady( PROGRAM_TIMEOUT ) )
{
    stat.Result = StatTimeout;
    stat.SR = ReadStatus();

    return( stat );
}
else
{
    WriteFlash( BASE_FLASH_ADDRESS, CLEAR_STATUS_REGISTER );  /* Clear status register */

    /* if (start address is not BUFFER_SIZE-byte aligned ) */
    if ( ( address % BUFFER_SIZE ) != 0 )
    {
        /* if ( buffer size is > BUFFER_SIZE ) or
          ( buffer crosses block boundary ) */
        if ( ( numbytes > BUFFER_SIZE ) || ( ( address & 0x1 ) && ( numbytes >= BUFFER_SIZE ) ) ||
           ( ( address + numbytes -1 ) > ( address | BLOCK_MASK) ) )
        {
	           /* write partial buffer */
            numwritebytes = ( BUFFER_SIZE - ( address % BUFFER_SIZE ) );
        }
        else
        {
	    /* write all remaining bytes */
            numwritebytes = numbytes;
        }

        byteswritten = numwritebytes;
        WriteFlash( cmndaddress, WRITE_TO_BUFFER );
        numitems = numwritebytes / sizeof(UINT16);

        if ( ( ( numwritebytes % sizeof(UINT16) ) != 0 ) || ( ( numwritebytes > 0x01 ) && ( address & 0x01 ) ) )
        {
            numitems++;
        }

        WriteFlash( cmndaddress, numitems-1 );

        if ( numwritebytes > 0 ) /* while more data to write */
        {
            while ( numwritebytes > 0 ) /* if more bytes still to write */
	    {
	        if ( ( address & 0x1 ) != 0 ) /* if destination address is odd */
	        {
                    address--;
                    #if (BIG_ENDIAN_ARCHITECTURE)
                    {
                        writedata = (UINT16) *buffer;
	 	        writedata |= 0xff00;
	            }
                    #else /* little endian */
                    {
	                writedata = *((UINT32_PTR)buffer);
	                writedata = ( writedata << 8 ) | 0x00ff;
	            }
                    #endif
	            numwritebytes--;
	            buffer++;
                }
                else /* destination address is even */
                {
                    #if BIG_ENDIAN_ARCHITECTURE
                    {
	                /* grab first byte */
	                writedata = (UINT16)( *buffer );
	                writedata = ( ( writedata << 8 ) & 0xff00 );
	                /* grab second byte */
	                writedata = writedata | ( (UINT16) *(buffer+1) );
                    }
                    #else /* little endian architecture */
                    {
	                /* grab 2 bytes */
                        writedata = *( (UINT32_PTR)buffer );
	            }
                    #endif /* BIG_ENDIAN_ARCHITECTURE */
 	            if ( numwritebytes == 1 )
 	            {
                        #if BIG_ENDIAN_ARCHITECTURE
                        {
 	                   writedata |= 0x00ff;
 	                }
                        #else
                        {
 	                   writedata |= 0xff00;
 	                }
                        #endif
 	                numwritebytes--;
                    }
 	            else
 	            {
 	                numwritebytes -= sizeof(UINT16);
 	            }

                    buffer += sizeof(UINT16);
                }
 	     /*   fptr = GetFptr(address);
                *fptr = writedata;*/
                GetIo32(address) = writedata;
                address += sizeof(UINT16);
            }
        }

        WriteFlash( cmndaddress, CONFIRM );

        if ( !WaitUntilReady( PROGRAM_TIMEOUT ) )
        {
            stat.Result = StatTimeout;
            stat.SR = ReadStatus();

            return( stat );
        }

        numbytes -= byteswritten;

    } /* end if  ( ( address % BUFFER_SIZE )  != 0 ) ) */

      /* while bytes remain */
    while ( numbytes != 0 )
    {
        /* if BUFFER_SIZE bytes remain */
        if ( numbytes > BUFFER_SIZE )
        {
            /* write full BUFFER_SIZE-byte buffer */
            numwritebytes = BUFFER_SIZE;
        }
        else
	        {
 	           /* write partial buffer */
 	            numwritebytes = numbytes;
            }

            byteswritten = numwritebytes;
            cmndaddress = address;
 	        WriteFlash( cmndaddress, WRITE_TO_BUFFER );
 	        numitems = numwritebytes / sizeof(UINT16);

 	        if ( ( numwritebytes % sizeof(UINT16) ) != 0 )
 	        {
 	            numitems++;
 	        }

 	        WriteFlash( cmndaddress, numitems - 1 );

 	        if ( numwritebytes > 0 ) /* while more data to write */
 	        {
  	            while ( numwritebytes > 0 ) /* if more bytes still to write */
  	            {   /* address is known even at this point */
                    #if BIG_ENDIAN_ARCHITECTURE
  	                {   /* grab first byte */
 		            writedata = (UINT16)( *buffer );
 		            writedata = ( ( writedata << 8 ) & 0xff00 );
 		            /* grab second byte */
 		            writedata = writedata | ( (UINT16) *(buffer + 1) );
 		        }
                    #else /* little endian architecture */
 		        {   /* grab 2 bytes */
 		            writedata = *( (UINT32_PTR)buffer );
 		        }
                    #endif /* BIG_ENDIAN_ARCHITECTURE */

 		        if ( numwritebytes == 1 )
 		        {
                        #if BIG_ENDIAN_ARCHITECTURE
 	 	            {
 	 	     	        writedata |= 0x00ff;
 	 	            }
                        #else
 	 	            {
 	 	                writedata |= 0xff00;
 	 	            }
                        #endif
 	 	            numwritebytes--;
 	 	        }
 	 	        else
 	 	        {
 	 	            numwritebytes -= sizeof(UINT16);
 	 	        }

 	 	        buffer += sizeof(UINT16);
 	 	     /*   fptr = GetFptr(address);
	 	        *fptr = writedata;*/
	 	        GetIo32(address) = writedata;
	                 address += sizeof(UINT16);
	             }
	         }

 	         WriteFlash( cmndaddress, CONFIRM );

             if ( !WaitUntilReady( PROGRAM_TIMEOUT ) )
             {
                 stat.Result = StatTimeout;
                 stat.SR = ReadStatus();

                 return( stat );
             }

             numbytes -= byteswritten;

         } /* end while numbytes != 0 ) */

     }     /* end if ( !WaitUntilReady( PROGRAM_TIMEOUT ) ) */

     stat.SR = ReadStatus();

     /* return device to read array mode */
     WriteFlash( BASE_FLASH_ADDRESS, READ_ARRAY );

     return( stat );
}

/***************** Program Protection *******************/
Flash_Status ProgramProtection( UINT32 location, UINT16 value )
{
	UINT32      baseaddr;
	UINT32      address;
	Flash_Status stat;

	if ( location > OTP_NUMWORDS )
	{
	    stat.Result = StatBadOtp;
	    return( stat );
	}

	baseaddr = BASE_FLASH_ADDRESS;
	address = OTP_BASE + BASE_FLASH_ADDRESS;
	address += ( location * sizeof(UINT16) );

    WriteFlash( BASE_FLASH_ADDRESS, CLEAR_STATUS_REGISTER );  /* Clear status register */

	WriteFlash( baseaddr, OTP_PROGRAM );
	WriteFlash( address, value );

	if ( !WaitUntilReady( PROGRAM_TIMEOUT ) )
	{
	    stat.Result = StatTimeout;
	}
	else
	{
	    stat.Result = StatCompleted;
	}

    stat.SR = ReadStatus();

	/* return device to read array mode */
	WriteFlash( BASE_FLASH_ADDRESS, READ_ARRAY );

	return( stat );
}

/*********************** Program Suspend ****************************/
Flash_Status ProgramSuspend( UINT16 blocknum )
{
	Flash_Status stat;
	UINT32      blockaddr;

	stat = GetBlockAddress( blocknum, &blockaddr );

	if ( stat.Result != StatCompleted )
	{
	    return( stat );
	}

	WriteFlash( blockaddr, BLOCK_SUSPEND);

	if ( !WaitUntilReady( PROGRAM_TIMEOUT ) )
	{
	    stat.Result = StatTimeout;
	}
	else
	{
	    stat.Result = StatCompleted;
	}

    stat.SR = ReadStatus();

	/* return device to read array mode */
	WriteFlash( BASE_FLASH_ADDRESS, READ_ARRAY );

	return( stat );
}

/********************** Query command *************************/
Flash_Status Flash_Queryc ( struct Flash_QueryData *query )
{
    Flash_Status stat;
    UINT32 add;
    UINT32 offset;
    UINT32 item;
    UINT32 longitem;
    UINT32 i;

    WriteFlash(BASE_FLASH_ADDRESS, READ_QUERY );
    offset = QUERY_START_OFFSET;

    /* read query string */
    for ( i=0; i < 3; i++ )
    {
        stat = GetQueryAddress(offset, &add);

        if ( stat.Result != StatCompleted )
        {
            return( stat );
        }

//        ReadFlash( add, &item );
        item &= 0xff;
        query->QueryStr[i] = (char)item;
	    offset++;
    }
    query->QueryStr[3] = '\0'; /* null terminate string */

    /* read vendor id */
    query->VendorId = 0;
    for ( i=0; i < 2; i++ )
    {
        stat = GetQueryAddress(offset, &add);

        if ( stat.Result != StatCompleted )
        {
            return( stat );
        }

//        ReadFlash( add, &item );

        #if BIG_ENDIAN_ARCHITECTURE
        {
            item &= 0x00ff;
	        item = item << (8*(1-i) );
	    }
        #else
        {
            item &= 0xff;
	        item = item << (8*i);
	    }
        #endif

        query->VendorId = (UINT16)( query->VendorId | item );
        offset++;
    }

    /* read extended table ptr */
    query->ExtTablePtr = 0;
    for ( i=0; i < 2; i++ )
    {
        stat = GetQueryAddress(offset, &add);

        if ( stat.Result != StatCompleted )
        {
            return( stat );
        }

//        ReadFlash( add, &item );

    #if BIG_ENDIAN_ARCHITECTURE
    {
        item &= 0x00ff;
        item = item << (8*(1-i) );
    }
    #else
    {
        item &= 0xff;
        item = item << (8*i);
    }
    #endif

    query->ExtTablePtr = (UINT16)( query->ExtTablePtr | item );
    offset++;
    }

    /* read alternate vendor id */
    query->AltVendorId = 0;
    for ( i=0; i < 2; i++ )
    {
        stat = GetQueryAddress(offset, &add);

        if ( stat.Result != StatCompleted )
        {
            return( stat );
        }

//        ReadFlash( add, &item );

    #if BIG_ENDIAN_ARCHITECTURE
    {
        item &= 0x00ff;
        item = item << (8*(1-i) );
    }
    #else
    {
        item &= 0xff;
        item = item << (8*i);
    }
    #endif
    query->AltVendorId = (UINT16)( query->AltVendorId | item );
    offset++;
    }

    /* read secondary extended table ptr */
    query->SecExtTablePtr = 0;
    for ( i=0; i < 2; i++ )
    {
        stat = GetQueryAddress(offset, &add);

        if ( stat.Result != StatCompleted )
        {
            return( stat );
        }

//        ReadFlash( add, &item );

    #if BIG_ENDIAN_ARCHITECTURE
    {
        item &= 0x00ff;
        item = item << (8*(1-i) );
    }
    #else
    {
        item &= 0xff;
        item = item << (8*i);
    }
    #endif
    query->SecExtTablePtr = (UINT16)( query->SecExtTablePtr | item );
    offset++;
    }

    /* read minimum voltage */
    stat = GetQueryAddress(offset, &add);

    if ( stat.Result != StatCompleted )
    {
        return( stat );
    }

//    ReadFlash( add, &item );

    #if BIG_ENDIAN_ARCHITECTURE
    {
        item &= 0x00ff;
    }
    #else
    {
        item &= 0xff;
    }
    #endif

    query->VccMin = (BYTE)item;
    offset++;

⌨️ 快捷键说明

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