This repository was archived by the owner on Mar 30, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory.c
201 lines (167 loc) · 3.75 KB
/
memory.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "includes/memory.h"
#include "includes/registers.h"
#define WRITE 0xA0
#define READ 0xA1
/* Wait for I2C bus to become idle
* Author: h4xxel
*/
void i2c_idle() {
while (I2C1CON & 0x1F || I2C1STAT & (1 << 14))
; // TRSTAT
}
/* Send one byte on I2C bus, return ack/nack status of transaction
* Author: h4xxel
*/
int i2c_send(byte data) {
i2c_idle();
I2C1TRN = data & 0xFF;
i2c_idle();
return !(I2C1STAT & (1 << 15)); // ACKSTAT
}
/* Receive one byte from I2C bus
* Author: h4xxel
*/
byte i2c_recv() {
i2c_idle();
I2C1CONSET = 1 << 3; // RCEN = 1
i2c_idle();
I2C1STATCLR = 1 << 6; // I2COV = 0
return I2C1RCV;
}
/* Send acknowledge conditon on the bus
* Author: h4xxel
*/
void i2c_ack() {
i2c_idle();
I2C1CONCLR = 1 << 5; // ACKDT = 0
I2C1CONSET = 1 << 4; // ACKEN = 1
}
/* Send not-acknowledge conditon on the bus
* Author: h4xxel
*/
void i2c_nack() {
i2c_idle();
I2C1CONSET = 1 << 5; // ACKDT = 1
I2C1CONSET = 1 << 4; // ACKEN = 1
}
/* Send start conditon on the bus
* Author: h4xxel
*/
void i2c_start() {
i2c_idle();
I2C1CONSET = 1; // SEN
i2c_idle();
}
/* Send restart conditon on the bus
* Author: h4xxel
*/
void i2c_restart() {
i2c_idle();
I2C1CONSET = 1 << 1; // RSEN
i2c_idle();
}
/* Send stop conditon on the bus
* Author: h4xxel
*/
void i2c_stop() {
i2c_idle();
I2C1CONSET = 1 << 2; // PEN
i2c_idle();
}
/**
* Writes an integer to the EEPROM.
* @param address The 16-bit address to send to the EEPROM.
* @param data The integer to send.
* @author Alex Diaz
*/
void write_int(short address, int data) {
do {
i2c_start();
} while (!i2c_send(WRITE));
byte seg[] = {(data & 0xFF), ((data >> 8) & 0xFF), ((data >> 16) & 0xFF),
((data >> 24) & 0xFF)};
i2c_send(address >> 8);
i2c_send(address);
int i;
for (i = 0; i < sizeof(int); i++)
i2c_send(seg[i]);
i2c_stop();
}
/**
* Reads an integer from the EEPROM.
* @param address The 16-bit address to read from the EEPROM.
* @return The integer.
* @author Alex Diaz
*/
int read_int(short address) {
int recv = 0;
do {
i2c_start();
} while (!i2c_send(WRITE));
i2c_send(address >> 8);
i2c_send(address);
i2c_start();
i2c_send(READ);
int i;
for (i = 0; i < sizeof(int); i++) {
recv |= i2c_recv();
i2c_ack();
}
i2c_nack();
i2c_stop();
return recv;
}
/**
* Writes a character to the EEPROM.
* @param address The 16-bit address to send to the EEPROM.
* @param data The character to send.
* @author Alex Diaz
*/
void write_char(short address, char* data, int len) {
do {
i2c_start();
} while (!i2c_send(WRITE));
i2c_send(address >> 8);
i2c_send(address);
while (len >= 0) {
i2c_send(*data);
data++;
len--;
}
i2c_stop();
}
/**
* Reads a character from the EEPROM.
* @param address The 16-bit address to read from the EEPROM.
* @return The character.
* @author Alex Diaz
*/
char* read_char(short address, int len) {
char* recv;
do {
i2c_start();
} while (!i2c_send(WRITE));
i2c_send(address >> 8);
i2c_send(address);
i2c_start();
i2c_send(READ);
while (len >= 0) {
*recv = i2c_recv();
i2c_ack();
recv++;
len--;
}
i2c_nack();
i2c_stop();
return recv;
}
/* Set up i2c */
void i2c_init() {
I2C1CON = 0x0;
/* I2C Baud rate should be less than 400 kHz, is generated by dividing
the 40 MHz peripheral bus clock down */
I2C1BRG = 0x0C2;
I2C1STAT = 0x0;
I2C1CONSET = 1 << 13; // SIDL = 1
I2C1CONSET = 1 << 15; // ON = 1
}