Main Page | Modules | Data Structures | Directories | File List | Data Fields | Globals | Related Pages

spieeprom.c

Go to the documentation of this file.
00001 /*! \file spieeprom.c \brief Interface for standard SPI EEPROM memories. */
00002 //*****************************************************************************
00003 //
00004 // File Name    : 'spieeprom.c'
00005 // Title        : Interface for standard SPI EEPROM memories
00006 // Author       : Pascal Stang - Copyright (C) 2004
00007 // Created      : 2004.10.07
00008 // Revised      : 2004.10.07
00009 // Version      : 0.1
00010 // Target MCU   : Atmel AVR series
00011 // Editor Tabs  : 4
00012 //
00013 // This code is distributed under the GNU Public License
00014 //      which can be found at http://www.gnu.org/licenses/gpl.txt
00015 //
00016 //*****************************************************************************
00017 
00018 #include <avr/io.h>
00019 #include <avr/signal.h>
00020 #include <avr/interrupt.h>
00021 
00022 #include "spi.h"
00023 #include "spieeprom.h"
00024 
00025 // functions
00026 void spieepromInit(void)
00027 {
00028     // although there is no code here
00029     // don't forget to initialize the SPI interface itself
00030 //  sbi(DDRB, 0);
00031 }
00032 
00033 u08 spieepromReadByte(u32 memAddr)
00034 {
00035     u08 data;
00036 //  cbi(PORTB,0);
00037     // send command
00038     spiTransferByte(SPIEEPROM_CMD_READ);
00039     // send address
00040     spiTransferByte(memAddr>>8);
00041     spiTransferByte(memAddr&0x00FF);
00042     // read contents of memory address
00043     data = spiTransferByte(0xFF);
00044     // return data
00045     return data;
00046 //  sbi(PORTB,0);
00047 }
00048 
00049 void spieepromWriteByte(u32 memAddr, u08 data)
00050 {
00051     // wait for any previous write to complete
00052     while(spieepromReadStatus() & SPIEEPROM_STATUS_WIP);
00053 
00054 //  cbi(PORTB,0);
00055     // send command
00056     spiTransferByte(SPIEEPROM_CMD_WRITE);
00057     // send address
00058     spiTransferByte(memAddr>>8);
00059     spiTransferByte(memAddr&0x00FF);
00060     // send data to be written
00061     spiTransferByte(data);
00062 //  sbi(PORTB,0);
00063 }
00064 
00065 void spieepromWriteEnable(void)
00066 {
00067 //  cbi(PORTB,0);
00068     // send command
00069     spiTransferByte(SPIEEPROM_CMD_WREN);
00070 //  sbi(PORTB,0);
00071 }
00072 
00073 void spieepromWriteDisable(void)
00074 {
00075 //  cbi(PORTB,0);
00076     // send command
00077     spiTransferByte(SPIEEPROM_CMD_WRDI);
00078 //  sbi(PORTB,0);
00079 }
00080 
00081 u08 spieepromReadStatus(void)
00082 {
00083     u08 status;
00084 //  cbi(PORTB,0);
00085     // send command
00086     spiTransferByte(SPIEEPROM_CMD_RDSR);
00087     // get status register value
00088     status = spiTransferByte(0xFF);
00089 //  sbi(PORTB,0);
00090     return status;
00091 }

Generated on Tue Sep 20 03:11:43 2005 for Procyon AVRlib by  doxygen 1.4.2