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

📄 at25f512_drv.c~

📁 CodeVision AT25F512 + ATmega168 Driver
💻 C~
📖 第 1 页 / 共 2 页
字号:
/*F**************************************************************
/ Function: put_char_array
/----------------------------------------------------------------
/ Description: Write Status Register (WRSR)  
/---------------------------------------------------------------- 
/ Parameter:    address             : get data address
                nb_of_byte          : get number of byte
                destination         : the result area                                                                                                            
/ Return:       TRANSFER_COMPLETED  : the read access is completed without error
                BUSY                : the SPI peripheral is busy                                                     
                OUT_OF_RANGE        : address out of range (0x00FFFF)
/----------------------------------------------------------------
/ Note:
*****************************************************************/ 
unsigned char put_char_array(unsigned long start_add, unsigned char nb_of_byte, unsigned char* source)
{
    unsigned char sr;                                                /* SPI memory status register */
 
    if (start_add <= TOP_ADDR)
    {
        if (read_status_register(&sr) == TRANSFER_COMPLETED)        /* Is the SPI interface currently not busy */
        {
            if (!(sr & (1<<RDY)))                                   /* is the SPI memory ready ? */
            {
                if (test_write_protect(start_add, sr) == WRITE_ALLOWED)
                {               
                    while(write_command(WREN)!= TRANSFER_COMPLETED);/* always perform a WREN command before any write operation */                   
                    spi_state = INSTRUCTION;                        /* Serial SPI memory write access WRITE start */
                    nb_byte = nb_of_byte;
                    byte_cnt = 0;
                    address = start_add; 
                    data_ptr = source;
                    SPCR |= (1<<SPE);                               /* Enable SPI Interface */                                   
                    SPCR |= (1<< SPIE);                                                       
                    AT25F512A_CS = 0;                               /* Enable AT25F512A Chip Select (L) */      
                    SPDR = PROGRAM;                    
                    return  TRANSFER_STARTED;
                }
                else
                {
                    return DATA_WR_PROTECTED;
                }         
            }
            else
            {
                return BUSY;
            }
        }  
        else
        {
            return BUSY;
        }
    }
    else
    {
        return OUT_OF_RANGE;
    }
}

/*F**************************************************************
/ Function: erase_sector
/----------------------------------------------------------------
/ Description: Erase Sector (0x000000 ~ 0x007FFF), (0x008000 ~ 0x00FFFF)
/---------------------------------------------------------------- 
/ Parameter:    address             : erase sector (0x007FFF / 0x00FFFF)                                                                                                           
/ Return:       TRANSFER_COMPLETED  : the read access is completed without error
                BUSY                : the SPI peripheral is busy                                                     
                DATA_WR_PROTECTED   : the selected sector is write protected.
/----------------------------------------------------------------
/ Note:
*****************************************************************/
unsigned char erase_sector(unsigned long long_add)
{
    unsigned char sr;                                               /* read status register */
    if (read_status_register(&sr) == TRANSFER_COMPLETED)            /* Is the SPI interface currently not busy */
    {  
        if (!(sr & (1<<RDY)))                                       /* is the serial memory ready ? */
        {
            if (test_write_protect(long_add, sr) == WRITE_ALLOWED)
            {               
                while(write_command(WREN)!=TRANSFER_COMPLETED);                                /* always perform a WREN command before any write operation */
                SPCR |= (1<<SPE);                                   /* Enable SPI Interface */
                AT25F512A_CS = 0;                                   /* Enable AT25F512A Chip Select (L) */                
                spi_transfer(SECTOR_ERASE);                         /* transmit the SECTOR_ERASE op_code */
                spi_transfer((unsigned char)(long_add>>16));        /* transmit the most significant address byte */
                spi_transfer((unsigned char)(long_add>>8));         /* transmit the middle address byte */
                spi_transfer((unsigned char)(long_add));            /* transmit the less significant address byte */
                AT25F512A_CS = 1;                                   /* Disable AT25F512A Chip Select (H) */
                SPCR &= ~(1<<SPE);
                return TRANSFER_COMPLETED;
            }
            else
            {
                return DATA_WR_PROTECTED;
            }
        }
        else
        {
            return BUSY;
        }
    }
    else
    {
        return BUSY;
    }       
}   

/*F**************************************************************
/ Function: erase_chip
/----------------------------------------------------------------
/ Description: Erase ALL (0x000000 ~ 0x00FFFF)
/---------------------------------------------------------------- 
/ Parameter:                                                                                                      
/ Return:       TRANSFER_COMPLETED  : the read access is completed without error
                BUSY                : the SPI peripheral is busy                                                     
                DATA_WR_PROTECTED   : the selected sector is write protected.
/----------------------------------------------------------------
/ Note:
*****************************************************************/
unsigned char erase_chip(void)
{
    unsigned char sr;                                               /* read status register */
    if (read_status_register(&sr) == TRANSFER_COMPLETED)            /* Is the SPI interface currently not busy */
    {           
        if (!(sr & (1<<RDY)))                                       /* is the serial memory ready ? */
        {        
            if (test_write_protect(0, sr) == WRITE_ALLOWED)         /* test if the first byte is write protected to detect a full array write protection */
            {   
                while(write_command(WREN) != TRANSFER_COMPLETED);   /* always perform a WREN command before any write operation */                
                SPCR |= (1<<SPE);                                   /* Enable SPI Interface */                
                AT25F512A_CS = 0;                                   /* Enable AT25F512A Chip Select (L) */                 
                spi_transfer(CHIP_ERASE);                           /* transmit the CHIP_ERASE op_code */                        
                AT25F512A_CS = 1;                                   /* Disable AT25F512A Chip Select (H) */
                SPCR &= ~(1<<SPE);                                  /* Disable SPI Interface */
                return TRANSFER_COMPLETED;
            }
            else
            {                
                return DATA_WR_PROTECTED;
            }
        }
        else
        {
            return BUSY;
        }
    }
    else
    {
        return BUSY;
    }       
}

/*F**************************************************************
/ Function: Read_mem_id
/----------------------------------------------------------------
/ Description: Read Mem ID (ATMEL manufacurer: 0x1F, Device Code: 0x65) 
/---------------------------------------------------------------- 
/ Parameter:                                                                                                      
/ Return:       TRANSFER_COMPLETED  : the read access is completed without error
                BUSY                : the SPI peripheral is busy                                                     
/----------------------------------------------------------------
/ Note:
*****************************************************************/
unsigned char get_mem_id(unsigned char *id)
{
    unsigned char sr;                                               /* read status register */
    if (read_status_register(&sr) == TRANSFER_COMPLETED)            /* Is the SPI interface not currently busy */
    {  
        if (!(sr & (1<<RDY)))                                       /* is the serial memory ready ? */
        {
            SPCR |= (1<<SPE);                                       /* Enable SPI Interface */        
            AT25F512A_CS = 0;                                       /* Enable AT25F512 Chip Select (L) */
            spi_transfer(RDID);                                     /* transmit the SECTOR_ERASE op_code */
            spi_transfer(0xFF);                                     /* receive the manufacturing code */
            *id = spi_transfer(0xFF);                               /* receive the ID code */
            AT25F512A_CS = 1;                                       /* Disable AT25F512A Chip Select (H) */
            SPCR &= ~(1<<SPE);                                      /* Disable SPI Interface */
            return TRANSFER_COMPLETED;
        }
        else
        {
            return BUSY;
        }
    }
    else
    {
        return BUSY;
    }       
}

/*F**************************************************************
/ Function: test_write_protect
/----------------------------------------------------------------
/ Description: Check write protect (Y/N)
/---------------------------------------------------------------- 
/ Parameter:    address             : test write protect address                                                                                                  
/               status_register     : check status            
/ Return:       WRITE_ALLOWED       : allowed write data 
                WRITE_PROTECTED     : protected write data                                                   
/----------------------------------------------------------------
/ Note:
*****************************************************************/
unsigned char test_write_protect(unsigned long address, unsigned int status_register)
{        
    if ((status_register & (1 << BP0)) == NONE)          /* if no write protection area is configured */
    {    
        return WRITE_ALLOWED;
    }
    else
    {  
//        return WRITE_ALLOWED;    
        return WRITE_PROTECTED;
    }
}


/*F**************************************************************
/ Function: write_protected_area
/----------------------------------------------------------------
/ Description: Set Write Protected Area
/---------------------------------------------------------------- 
/ Parameter:    area                : the region of the serial memory to be protected                                                                                    
/               status_register     : check status            
/ Return:       TRANSFER_COMPLETED  : the write access was completed without error
                BUSY                : the SPI peripheral or the serial memory is busy.
                HW_PROTECTED        : the serial memory is hardware write protected.                                            
/----------------------------------------------------------------
/ Note:
*****************************************************************/
unsigned char set_write_protected_area(unsigned char area)
{
    unsigned char sr;                                                               /* status register byte */
  
    if (read_status_register(&sr) == TRANSFER_COMPLETED)                            /* Is the SPI interface not currently busy */
    {           
        if (!(sr & (1<<RDY)))                                                       /* is the serial memory ready ? */
        {                        
            while ( write_status_register(0<<WPEN) != TRANSFER_COMPLETED){};        /* Disable software write protection */                        
            while ( write_status_register((1<<WPEN)|area) != TRANSFER_COMPLETED){}; /* set software write portection to the selecte area */
            do
            {
                read_status_register(&sr);
            }while (sr & (1<<RDY));                                                 /* read backs status register */
            if (sr != ((1<<WPEN)|area))                                             /* detect hardware protection */
            {
                return HW_PROTECTED;
            }
            else
            {                  
                return TRANSFER_COMPLETED;
            }    
        }
        else
        {
            return BUSY;
        }
    }
    else
    {
        return BUSY; 
    }
}


⌨️ 快捷键说明

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