📄 dataflash.c
字号:
/******************************************************************
* dataflash.c: Low level routines for accessing the external dataflash
*
* Copyright (c) 2001 Atmel Corporation.
* All Rights Reserved.
*
* You are autorized to use, copy and distribute this software only at
* a single site (the term "site" meaning a single company location).
* This copyright notice must be included in any copy, modification
* or portion of this software merged into another program.
*
* This software is licenced solely for use with Atmel AVR micro
* controller family. The software may not be modified to execute on
* any other microcontroller architectures
*
* This software is provided "as is"; Without warranties either express
* or implied, including any warranty regarding merchantability,
* fitness for a particular purpose or noninfringement.
*
* In no event shall Atmel or its suppliers be liable for any special,
* indirect,incidential or concequential damages resulting from the
* use or inability to use this software.
*
* Revision history:
*
* January 17, 2001: Version 1.0 Created by JB
* July 13, 2001 Version 1.2 JB
* - Changed to IAR compiler V2.25
* - Renamed flash file functions to avoid conflict with
* standard file I/O names
* - Bug fixes in HTTP
* - Speed optimization in TCP
*
*******************************************************************/
#define ENABLE_BIT_DEFINITIONS
#include "comp_a90.h"
#include <iom103.h>
#include "dataflash.h"
#include <ina90.h>
/*macros*/
#define SPI_ENABLE() (PORTB &= ~(1<<FLASH_SS))
#define SPI_DISABLE() (PORTB |= (1<<FLASH_SS))
/*write one byte to SPI.*/
void write_SPI(unsigned char data)
{
unsigned char temp;
SPDR = data;
while (!(SPSR & (1<<SPIF)))
{
temp = SPDR; // Dummy reading, gives compiler warning
}
}
/* Check if dataflash is ready.*/
char read_status(void)
{
unsigned char status;
SPI_ENABLE();
write_SPI (0x57);
SPCR = 0x00;
PORTB |= (1<<FLASH_SCK); //Must add one extra clock cycle
PORTB &= ~(1<<FLASH_SCK); //to shift in the whole byte
SPCR = 0x50;
write_SPI (0x00);
status = SPDR;
SPI_DISABLE();
return (status);
}
/*Write one page in dataflash*/
void write_page(int pageNr, char *buffer)
{
unsigned int counter;
while (!(read_status() & 0x80) )
{
}
SPI_ENABLE();
write_SPI(FLASH_MM_PROG_THROUGH_B1); // Main memory program through Buffer 1
write_SPI( (unsigned char) (pageNr>>7)&0x0f ); // Page Number to program
write_SPI( (unsigned char) (pageNr<<1)&0xfe );
write_SPI( 0 ); // Byte Address..
for ( counter=0;counter<FLASH_PAGE_SIZE;counter++ ) /*Write all bytes in page*/
{
write_SPI(buffer[counter]);
}
SPI_DISABLE();
}
/*Read 'length' bytes from page in dataflash*/
void read_page(int pageNr, char *buffer, int length)
{
unsigned int counter;
while (!(read_status() & 0x80) )
{
}
SPI_ENABLE();
write_SPI(FLASH_MM_READ);
write_SPI( (unsigned char) (pageNr>>7)&0x0f ); //Page Number to program
write_SPI( (unsigned char) (pageNr<<1)&0xfe );
write_SPI( 0 );
write_SPI( 0 );
write_SPI( 0 );
write_SPI( 0 );
write_SPI( 0 );
SPCR = 0x00;
PORTB |= (1<<FLASH_SCK); //Must add one extra clock cycle
PORTB &= ~(1<<FLASH_SCK); //so the whole byte is shifted in.
SPCR = 0x50;
for (counter=0;counter<length;counter++)
{
write_SPI(0);
buffer[counter]=SPDR;
}
}
/*Copy one page to another page*/
void copy_page(int frmPage, int toPage)
{
while (!(read_status() & 0x80) )
{
}
SPI_ENABLE();
write_SPI(FLASH_MM_PAGE_TO_B1);
write_SPI( (unsigned char) (frmPage>>7)&0x0f ); // Page Number to program
write_SPI( (unsigned char) (frmPage<<1)&0xfe );
write_SPI( 0 );
SPI_DISABLE();
while(!(read_status() & 0x80) )
{
}
SPI_ENABLE();
write_SPI(FLASH_B1_TO_MM_PAGE);
write_SPI( (unsigned char) (toPage>>7)&0x0f ); // Page Number to program
write_SPI( (unsigned char) (toPage<<1)&0xfe );
write_SPI( 0 );
SPI_DISABLE();
}
/*Secure write to EEPROM*/
void EEput(int uiAddress, char cValue)
{
unsigned char c;
while(EECR & 0x02)
{
}
c = SREG;
_CLI();
EEAR = uiAddress;
EEDR = cValue;
EECR |= 4;
EECR |= 2;
SREG = c;
}
/*Secure integer write to EEPROM*/
void EEputInt(int uiAddress, unsigned int cValue)
{
unsigned char c = SREG;
_CLI();
EEput(uiAddress,cValue); // write low byte
EEput(++uiAddress,cValue >> 8); // write high byte
SREG = c;
}
/*Secure read from EEPROM*/
char EEget(int uiAddress)
{
unsigned char cValue;
unsigned char c;
while (EECR & 0x02)
{
}
c = SREG;
_CLI();
EEAR = uiAddress;
EECR |= 0x01;
cValue = EEDR;
SREG = c;
return(cValue);
}
/*Secure integer read from EEPROM*/
unsigned int EEgetInt(int uiAddress)
{
unsigned int cValue;
unsigned char c = SREG;
_CLI();
cValue = EEget(uiAddress);
cValue |= EEget(++uiAddress)<<8;
SREG = c;
return (cValue);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -