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

📄 at25f512_drv.i

📁 CodeVision AT25F512 + ATmega168 Driver
💻 I
📖 第 1 页 / 共 2 页
字号:
/*C**************************************************************
/ Function: at25f512_drv
/----------------------------------------------------------------
/ Description: ATMEL FLASH (AT25F512A) Driver 
/----------------------------------------------------------------                                                       
/ Note:
/----------------------------------------------------------------
/ Author:   Sam Lu
/ Version:  V1.00
/ Date:     2006/02/05
*****************************************************************/
/*_____________I N C L U D E____________________________________*/
// CodeVisionAVR C Compiler
// (C) 1998-2004 Pavel Haiduc, HP InfoTech S.R.L.

// I/O registers definitions for the ATmega168(V)


#pragma used+
sfrb PINB=3;
sfrb DDRB=4;
sfrb PORTB=5;
sfrb PINC=6;
sfrb DDRC=7;
sfrb PORTC=8;
sfrb PIND=9;
sfrb DDRD=0xa;
sfrb PORTD=0xb;
sfrb TIFR0=0x15;
sfrb TIFR1=0x16;
sfrb TIFR2=0x17;
sfrb PCIFR=0x1b;
sfrb EIFR=0x1c;
sfrb EIMSK=0x1d;
sfrb GPIOR0=0x1e;
sfrb EECR=0x1f;
sfrb EEDR=0x20;
sfrb EEARL=0x21;
sfrb EEARH=0x22;
sfrw EEAR=0x21;   // 16 bit access
sfrb GTCCR=0x23;
sfrb TCCR0A=0x24;
sfrb TCCR0B=0x25;
sfrb TCNT0=0x26;
sfrb OCR0A=0x27;
sfrb OCR0B=0x28;
sfrb GPIOR1=0x2a;
sfrb GPIOR2=0x2b;
sfrb SPCR=0x2c;
sfrb SPSR=0x2d;
sfrb SPDR=0x2e;
sfrb ACSR=0x30;
sfrb MONDR=0x31;
sfrb SMCR=0x33;
sfrb MCUSR=0x34;
sfrb MCUCR=0x35;
sfrb SPMCSR=0x37;
sfrb SPL=0x3d;
sfrb SPH=0x3e;
sfrb SREG=0x3f;
#pragma used-


// Interrupt vectors definitions



/*H**************************************************************
/ Function: spi_drv.h
/----------------------------------------------------------------
/ Description: spi driver header
/----------------------------------------------------------------                                                       
/ Note:
/----------------------------------------------------------------
/ Author:
/ Version:
/ Date:
*****************************************************************/
/*_____________M A R C O________________________________________*/
/*_____________D E F I N A T I O N______________________________*/
/* SPCR - SPI Control Register */

/* SPSR - SPI Status Register */
/*_____________D E F - F U N C T I O N__________________________*/
void spi_initial(void);
unsigned char spi_transfer(volatile unsigned char data);

/*_____________E N D____________________________________________*/

/*H**************************************************************
/ Function: at25f512_drv.h
/----------------------------------------------------------------
/ Description: ATMEL AT25F512A Flash driver header
/----------------------------------------------------------------                                                       
/ Note:
/----------------------------------------------------------------
/ Author:
/ Version:
/ Date:
*****************************************************************/
/*_____________M A R C O________________________________________*/
/*_____________D E F I N A T I O N______________________________*/

/* Block write Protect bit */
                        
/* Instruction Codes */

/* Address Space */

/* Address 3-byte mode */

/* Status Register Bit */

/* Interrupt Service Status */                                                                          

/*Access Status */

/* Error code */
/*_____________D E F - F U N C T I O N__________________________*/

void at25f512_initial(void);
unsigned char write_command(unsigned char op_code);
unsigned char read_status_register(unsigned char *status);
unsigned char write_status_register(unsigned char status_register);
unsigned char get_char_array(unsigned long address, unsigned long nb_of_byte, unsigned char* destination);
unsigned char put_char_array(unsigned long start_add, unsigned char nb_of_byte, unsigned char* source);
unsigned char erase_sector(unsigned long long_add);
unsigned char erase_chip(void);
unsigned char get_mem_id(unsigned char *id);
unsigned char test_write_protect(unsigned long address, unsigned int status_register);
unsigned char set_write_protected_area(unsigned char area);










/*_____________E N D____________________________________________*/
/*_____________V A R I A B L E__________________________________*/
/*_____________E N D____________________________________________*/ 
                              

/*F**************************************************************
/ Function: at25f512_initial
/----------------------------------------------------------------
/ Description: atmel at25f512a initial
/---------------------------------------------------------------- 
/ Parameter:                                                                                                                
/ Return:                                                      
/----------------------------------------------------------------
/ Note:
*****************************************************************/
void at25f512_initial(void)
{
    spi_state = 0x00         ;
}

/*F**************************************************************
/ Function: spi_stc
/----------------------------------------------------------------
/ Description: spi stc (interrupt service routine)
/---------------------------------------------------------------- 
/ Parameter:                                                                                                                
/ Return:                                                      
/----------------------------------------------------------------
/ Note:
*****************************************************************/
interrupt [18] void spi_stc(void)
{
    switch (spi_state)
    {
        case 0x01          :                                          /* INSTRUCTION STATE */        
            spi_state = 0x02         ;        
            byte_cnt = 1;
            SPDR = (unsigned char)(address>>16);                    /* Address phase is 3 byte long for SPI flashes */
            break;        

        case 0x02          :                                              /* ADDRESS STATE */        
            if (byte_cnt == 3)                           /* is the last address byte reached? */
            {
                spi_state = 0x04         ;                                   /* go to the DATA state */
                byte_cnt = 0;
                SPDR = *data_ptr;                                   /* send the first byte */
            }
            else if (byte_cnt == 1)                                 /* must the middle address byte be sent? */
                {
                    byte_cnt ++;
                    SPDR = (unsigned char)(address>>8);
                }
                else
                {
                    byte_cnt ++;
                    SPDR = (unsigned char)(address);
                }
             break;         

        case 0x04          :                                                 /* DATA STATE */        
            data_ptr++;                                             /* point to the next byte (even if it was the last) */
            if (byte_cnt == nb_byte - 1 )                               /* is the last byte sent? */
            {   
                PORTB.2 = 1;                                   /* Disable AT25F512 Chip Select (H) */
                7;
                //SPCR &= ~(1<<SPIE);
                SPCR &= ~((1<<6)|(1<<7));
                spi_state = 0x00         ;                          /* return to the idle state */                                
            }
            else
            {      
                byte_cnt ++;
                SPDR = *data_ptr;                
            }
            break;    
    
        default :
        {
            spi_state = 0x00         ;
        }
    }  
}


/*F**************************************************************
/ Function: write_command
/----------------------------------------------------------------
/ Description: write command for at25f512 (WREN / WRDI)  
/---------------------------------------------------------------- 
/ Parameter:    op_code             : WREN/WRDI instruction code                                                                                                                
/ Return:       TRANSFER_COMPLETED  : the write access was completed without error
                BUSY                : the SPI peripheral or the serial memory is busy                                                     
/----------------------------------------------------------------
/ Note:
*****************************************************************/ 
unsigned char write_command(unsigned char op_code)
{
    unsigned char sr;                                               /* status register byte */  
    if (read_status_register(&sr) == 0x01    )            /* Is the SPI interface not currently busy */
    {      
        if (!(sr & (1<<0           )))                                       /* is the serial memory ready ? */
        {   
            SPCR |= (1<<6);                                       /* Enable SPI Interface */
            PORTB.2 = 0;                                       /* Enable AT25F512A Chip Select (L) */                                                                                                       
            spi_transfer(op_code);                                  /* transmitt the op_code */            
            PORTB.2 = 1;                                       /* Enable AT25F512A Chip Select (H) */                        
            SPCR &= ~(1<<6);                                      /* Disable SPI Interface */           
            return 0x01    ;
        }
        else
        {               
            return 0xFF    ;
        }
    }
    else
    {          
        return 0xFF    ; 
    }
}

/*F**************************************************************
/ Function: read_status_register
/----------------------------------------------------------------
/ Description: Read Status Register (RDSR)  
/---------------------------------------------------------------- 
/ Parameter:    op_code             : status variable address                                                                                                                
/ Return:       TRANSFER_COMPLETED  : the read access is completed without error
                BUSY                : the SPI peripheral is busy                                                     
/----------------------------------------------------------------
/ Note:
*****************************************************************/ 
unsigned char read_status_register(unsigned char *status)
{
    if (spi_state == 0x00         )
    {
        SPCR |= (1<<6);                                           /* Enable SPI Interface */
        PORTB.2 = 0;                                           /* Enable AT25F512A Chip Select (L) */
        spi_transfer(0x05);                                         /* transmitt the op_code RDSR */
        *status = spi_transfer(0xFF);                               /* get the status register value, send the value 0xFF to avoid toggle on the MOSI line */
        PORTB.2 = 1;                                           /* Disable AT25F512A Chip Select (H) */
        SPCR &= ~(1<<6);                                          /* Disable SPI Interface */    
        return 0x01    ;
    }
    else
    {
        *status = 1;
        return 0xFF    ; 
    }
}


/*F**************************************************************
/ Function: write_status_register
/----------------------------------------------------------------
/ Description: Write Status Register (WRSR)  
/---------------------------------------------------------------- 
/ Parameter:    op_code             : status variable address                                                                                                                
/ Return:       TRANSFER_COMPLETED  : the read access is completed without error
                BUSY                : the SPI peripheral is busy                                                     
/----------------------------------------------------------------
/ Note:
*****************************************************************/ 
unsigned char write_status_register(unsigned char status_register)
{
    unsigned char sr;                                               /* read status register */
    if (read_status_register(&sr) == 0x01    )            /* Is the SPI interface currently not busy */
    {                  
        if (!(sr & (1<<0           )))                                       /* is the serial memory ready ? */
        {                                                          
            while(write_command(0x06) != 0x01    );       /* write commande - write enable */                                                                        
            SPCR |= (1<<6);
            PORTB.2 = 0;            
            spi_transfer(0x01);                                     /* transmit the WRSR op_code */
            spi_transfer(status_register);                          /* transmit the status register value */
            PORTB.2 = 1;                                       /* Disable AT25F512A Chip Select (H) */
            SPCR &= ~(1<<6);                                      /* Disable SPI Interface */
            return 0x01    ;
        }
        else
        {
            return 0xFF    ;
        }
    }
    else
    {
        return 0xFF    ;
    }       
}

/*F**************************************************************
/ Function: get_char_array
/----------------------------------------------------------------
/ Description: get data by block mode and put on buffer
/---------------------------------------------------------------- 
/ 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:

⌨️ 快捷键说明

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