forked from hexagon5un/AVR-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path25LC256.c
105 lines (93 loc) · 3.1 KB
/
25LC256.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "25LC256.h"
void initSPI(void) {
SPI_SS_DDR |= (1 << SPI_SS); /* set SS output */
SPI_SS_PORT |= (1 << SPI_SS); /* start off not selected (high) */
SPI_MOSI_DDR |= (1 << SPI_MOSI); /* output on MOSI */
SPI_MISO_PORT |= (1 << SPI_MISO); /* pullup on MISO */
SPI_SCK_DDR |= (1 << SPI_SCK); /* output on SCK */
/* Don't have to set phase, polarity b/c
* default works with 25LCxxx chips */
SPCR |= (1 << SPR1); /* div 16, safer for breadboards */
SPCR |= (1 << MSTR); /* clockmaster */
SPCR |= (1 << SPE); /* enable */
}
void SPI_tradeByte(uint8_t byte) {
SPDR = byte; /* SPI starts sending immediately */
loop_until_bit_is_set(SPSR, SPIF); /* wait until done */
/* SPDR now contains the received byte */
}
void EEPROM_send16BitAddress(uint16_t address) {
SPI_tradeByte((uint8_t) (address >> 8)); /* most significant byte */
SPI_tradeByte((uint8_t) address); /* least significant byte */
}
uint8_t EEPROM_readStatus(void) {
SLAVE_SELECT;
SPI_tradeByte(EEPROM_RDSR);
SPI_tradeByte(0); /* clock out eight bits */
SLAVE_DESELECT;
return (SPDR); /* return the result */
}
void EEPROM_writeEnable(void) {
SLAVE_SELECT;
SPI_tradeByte(EEPROM_WREN);
SLAVE_DESELECT;
}
uint8_t EEPROM_readByte(uint16_t address) {
SLAVE_SELECT;
SPI_tradeByte(EEPROM_READ);
EEPROM_send16BitAddress(address);
SPI_tradeByte(0);
SLAVE_DESELECT;
return (SPDR);
}
uint16_t EEPROM_readWord(uint16_t address) {
uint16_t eepromWord;
SLAVE_SELECT;
SPI_tradeByte(EEPROM_READ);
EEPROM_send16BitAddress(address);
SPI_tradeByte(0);
eepromWord = SPDR;
eepromWord = (eepromWord << 8); /* most-sig bit */
SPI_tradeByte(0);
eepromWord += SPDR; /* least-sig bit */
SLAVE_DESELECT;
return (eepromWord);
}
void EEPROM_writeByte(uint16_t address, uint8_t byte) {
EEPROM_writeEnable();
SLAVE_SELECT;
SPI_tradeByte(EEPROM_WRITE);
EEPROM_send16BitAddress(address);
SPI_tradeByte(byte);
SLAVE_DESELECT;
while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {;
}
}
void EEPROM_writeWord(uint16_t address, uint16_t word) {
EEPROM_writeEnable();
SLAVE_SELECT;
SPI_tradeByte(EEPROM_WRITE);
EEPROM_send16BitAddress(address);
SPI_tradeByte((uint8_t) (word >> 8));
SPI_tradeByte((uint8_t) word);
SLAVE_DESELECT;
while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {;
}
}
void EEPROM_clearAll(void) {
uint8_t i;
uint16_t pageAddress = 0;
while (pageAddress < EEPROM_BYTES_MAX) {
EEPROM_writeEnable();
SLAVE_SELECT;
SPI_tradeByte(EEPROM_WRITE);
EEPROM_send16BitAddress(pageAddress);
for (i = 0; i < EEPROM_BYTES_PER_PAGE; i++) {
SPI_tradeByte(0);
}
SLAVE_DESELECT;
pageAddress += EEPROM_BYTES_PER_PAGE;
while (EEPROM_readStatus() & _BV(EEPROM_WRITE_IN_PROGRESS)) {;
}
}
}