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/interrupt.h>
    00020
    00021 #include "spi.h"
    00022 #include "spieeprom.h"
    00023
    00024 // functions
    00025 void spieepromInit(void)
    00026 {
    00027     // although there is no code here
    00028     // don't forget to initialize the SPI interface itself
    00029 //  sbi(DDRB, 0);
    00030 }
    00031
    00032 u08 spieepromReadByte(u32 memAddr)
    00033 {
    00034     u08 data;
    00035 //  cbi(PORTB,0);
    00036     // send command
    00037     spiTransferByte(SPIEEPROM_CMD_READ);
    00038     // send address
    00039     spiTransferByte(memAddr>>8);
    00040     spiTransferByte(memAddr&0x00FF);
    00041     // read contents of memory address
    00042     data = spiTransferByte(0xFF);
    00043     // return data
    00044     return data;
    00045 //  sbi(PORTB,0);
    00046 }
    00047
    00048 void spieepromWriteByte(u32 memAddr, u08 data)
    00049 {
    00050     // wait for any previous write to complete
    00051     while(spieepromReadStatus() & SPIEEPROM_STATUS_WIP);
    00052
    00053 //  cbi(PORTB,0);
    00054     // send command
    00055     spiTransferByte(SPIEEPROM_CMD_WRITE);
    00056     // send address
    00057     spiTransferByte(memAddr>>8);
    00058     spiTransferByte(memAddr&0x00FF);
    00059     // send data to be written
    00060     spiTransferByte(data);
    00061 //  sbi(PORTB,0);
    00062 }
    00063
    00064 void spieepromWriteEnable(void)
    00065 {
    00066 //  cbi(PORTB,0);
    00067     // send command
    00068     spiTransferByte(SPIEEPROM_CMD_WREN);
    00069 //  sbi(PORTB,0);
    00070 }
    00071
    00072 void spieepromWriteDisable(void)
    00073 {
    00074 //  cbi(PORTB,0);
    00075     // send command
    00076     spiTransferByte(SPIEEPROM_CMD_WRDI);
    00077 //  sbi(PORTB,0);
    00078 }
    00079
    00080 u08 spieepromReadStatus(void)
    00081 {
    00082     u08 status;
    00083 //  cbi(PORTB,0);
    00084     // send command
    00085     spiTransferByte(SPIEEPROM_CMD_RDSR);
    00086     // get status register value
    00087     status = spiTransferByte(0xFF);
    00088 //  sbi(PORTB,0);
    00089     return status;
    00090 }