| Line No. | Rev | Author | Line |
|---|---|---|---|
| 1 | 6 | kaklik | /*! \file spieeprom.c \brief Interface for standard SPI EEPROM memories. */ |
| 2 | //***************************************************************************** |
||
| 3 | // |
||
| 4 | // File Name : 'spieeprom.c' |
||
| 5 | // Title : Interface for standard SPI EEPROM memories |
||
| 6 | // Author : Pascal Stang - Copyright (C) 2004 |
||
| 7 | // Created : 2004.10.07 |
||
| 8 | // Revised : 2004.10.07 |
||
| 9 | // Version : 0.1 |
||
| 10 | // Target MCU : Atmel AVR series |
||
| 11 | // Editor Tabs : 4 |
||
| 12 | // |
||
| 13 | // This code is distributed under the GNU Public License |
||
| 14 | // which can be found at http://www.gnu.org/licenses/gpl.txt |
||
| 15 | // |
||
| 16 | //***************************************************************************** |
||
| 17 | |||
| 18 | #include <avr/io.h> |
||
| 19 | #include <avr/interrupt.h> |
||
| 20 | |||
| 21 | #include "spi.h" |
||
| 22 | #include "spieeprom.h" |
||
| 23 | |||
| 24 | // functions |
||
| 25 | void spieepromInit(void) |
||
| 26 | { |
||
| 27 | // although there is no code here |
||
| 28 | // don't forget to initialize the SPI interface itself |
||
| 29 | // sbi(DDRB, 0); |
||
| 30 | } |
||
| 31 | |||
| 32 | u08 spieepromReadByte(u32 memAddr) |
||
| 33 | { |
||
| 34 | u08 data; |
||
| 35 | // cbi(PORTB,0); |
||
| 36 | // send command |
||
| 37 | spiTransferByte(SPIEEPROM_CMD_READ); |
||
| 38 | // send address |
||
| 39 | spiTransferByte(memAddr>>8); |
||
| 40 | spiTransferByte(memAddr&0x00FF); |
||
| 41 | // read contents of memory address |
||
| 42 | data = spiTransferByte(0xFF); |
||
| 43 | // return data |
||
| 44 | return data; |
||
| 45 | // sbi(PORTB,0); |
||
| 46 | } |
||
| 47 | |||
| 48 | void spieepromWriteByte(u32 memAddr, u08 data) |
||
| 49 | { |
||
| 50 | // wait for any previous write to complete |
||
| 51 | while(spieepromReadStatus() & SPIEEPROM_STATUS_WIP); |
||
| 52 | |||
| 53 | // cbi(PORTB,0); |
||
| 54 | // send command |
||
| 55 | spiTransferByte(SPIEEPROM_CMD_WRITE); |
||
| 56 | // send address |
||
| 57 | spiTransferByte(memAddr>>8); |
||
| 58 | spiTransferByte(memAddr&0x00FF); |
||
| 59 | // send data to be written |
||
| 60 | spiTransferByte(data); |
||
| 61 | // sbi(PORTB,0); |
||
| 62 | } |
||
| 63 | |||
| 64 | void spieepromWriteEnable(void) |
||
| 65 | { |
||
| 66 | // cbi(PORTB,0); |
||
| 67 | // send command |
||
| 68 | spiTransferByte(SPIEEPROM_CMD_WREN); |
||
| 69 | // sbi(PORTB,0); |
||
| 70 | } |
||
| 71 | |||
| 72 | void spieepromWriteDisable(void) |
||
| 73 | { |
||
| 74 | // cbi(PORTB,0); |
||
| 75 | // send command |
||
| 76 | spiTransferByte(SPIEEPROM_CMD_WRDI); |
||
| 77 | // sbi(PORTB,0); |
||
| 78 | } |
||
| 79 | |||
| 80 | u08 spieepromReadStatus(void) |
||
| 81 | { |
||
| 82 | u08 status; |
||
| 83 | // cbi(PORTB,0); |
||
| 84 | // send command |
||
| 85 | spiTransferByte(SPIEEPROM_CMD_RDSR); |
||
| 86 | // get status register value |
||
| 87 | status = spiTransferByte(0xFF); |
||
| 88 | // sbi(PORTB,0); |
||
| 89 | return status; |
||
| 90 | } |
Powered by WebSVN v2.8.3