-
Notifications
You must be signed in to change notification settings - Fork 1
/
eeprom.c
246 lines (215 loc) · 6.05 KB
/
eeprom.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* open3600 - rw3600.c library functions
* This is a library of functions common to Linux and Windows
*
* Version 0.10
*
* Control WS3600 weather station
*
* Copyright 2003-2005, Kenneth Lavrsen, Grzegorz Wisniewski, Sander Eerkes
* This program is published under the GNU General Public license
*/
#include "eeprom.h"
/********************************************************************
* read_data reads data from the WS2300 based on a given address,
* number of data read, and a an already open serial port
*
* Inputs: serdevice - device number of the already open serial port
* number - number of bytes to read, max value 15
*
* Output: readdata - pointer to an array of chars containing
* the just read data, not zero terminated
*
* Returns: number of bytes read, -1 if failed
*
********************************************************************/
int eeprom_read(WEATHERSTATION ws, unsigned char *buf, size_t count) {
unsigned char command = 0xa1;
int i;
if (!write_byte(ws,command))
return -1;
for (i = 0; i < count; i++) {
buf[i] = read_byte(ws);
if (i + 1 < count)
read_next_byte_seq(ws);
//printf("%i\n",readdata[i]);
}
read_last_byte_seq(ws);
return i;
}
int eeprom_seek(WEATHERSTATION ws, off_t pos) {
return write_data(ws, pos, 0, NULL);
}
/********************************************************************
* write_data writes data to the WS2300.
* It can both write nibbles and set/unset bits
*
* Inputs: ws2300 - device number of the already open serial port
* address (interger - 16 bit)
* number - number of nibbles to be written/changed
* must 1 for bit modes (SETBIT and UNSETBIT)
* max 80 for nibble mode (WRITENIB)
* writedata - pointer to an array of chars containing
* data to write, not zero terminated
* data must be in hex - one digit per byte
* If bit mode value must be 0-3 and only
* the first byte can be used.
*
* Output: commanddata - pointer to an array of chars containing
* the commands that were sent to the station
*
* Returns: number of bytes written, -1 if failed
*
********************************************************************/
int write_data(WEATHERSTATION ws, int address, int number, unsigned char *writedata) {
unsigned char command = 0xa0;
int i = -1;
write_byte(ws,command);
write_byte(ws,address/256);
write_byte(ws,address%256);
if (writedata!=NULL) {
for (i = 0; i < number; i++)
{
write_byte(ws,writedata[i]);
}
}
set_DTR(ws,0);
nanodelay();
set_RTS(ws,0);
nanodelay();
set_RTS(ws,1);
nanodelay();
set_DTR(ws,1);
nanodelay();
set_RTS(ws,0);
nanodelay();
//return -1 for errors
return i;
}
void read_next_byte_seq(WEATHERSTATION ws) {
print_log(3,"read_next_byte_seq");
write_bit(ws,0);
set_RTS(ws,0);
nanodelay();
}
void read_last_byte_seq(WEATHERSTATION ws) {
print_log(3,"read_last_byte_seq");
set_RTS(ws,1);
nanodelay();
set_DTR(ws,0);
nanodelay();
set_RTS(ws,0);
nanodelay();
set_RTS(ws,1);
nanodelay();
set_DTR(ws,1);
nanodelay();
set_RTS(ws,0);
nanodelay();
}
/********************************************************************
* read_bit
* Reads one bit from the COM
*
* Inputs: serdevice - opened file handle
*
* Returns: bit read from the COM
*
********************************************************************/
int read_bit(WEATHERSTATION ws) {
int bit_value;
char str[20];
set_DTR(ws,0);
nanodelay();
bit_value = get_CTS(ws);
nanodelay();
set_DTR(ws,1);
nanodelay();
sprintf(str,"Read bit %i",!bit_value);
print_log(4,str);
return !bit_value;
}
/********************************************************************
* write_bit
* Writes one bit to the COM
*
* Inputs: serdevice - opened file handle
* bit - bit to write
*
* Returns: nothing
*
********************************************************************/
void write_bit(WEATHERSTATION ws,int bit) {
char str[20];
set_RTS(ws,!bit);
nanodelay();
set_DTR(ws,0);
nanodelay();
set_DTR(ws,1);
sprintf(str,"Write bit %i",bit);
print_log(4,str);
}
/********************************************************************
* read_byte
* Reads one byte from the COM
*
* Inputs: serdevice - opened file handle
*
* Returns: byte read from the COM
*
********************************************************************/
int read_byte(WEATHERSTATION ws) {
int byte = 0;
int i;
char str[20];
for (i = 0; i < 8; i++)
{
byte *= 2;
byte += read_bit(ws);
}
sprintf(str,"Read byte %i",byte);
print_log(3,str);
return byte;
}
/********************************************************************
* write_byte
* Writes one byte to the COM
*
* Inputs: serdevice - opened file handle
* byte - byte to write
*
* Returns: nothing
*
********************************************************************/
int write_byte(WEATHERSTATION ws, int byte) {
int status;
int i;
char str[20];
sprintf(str,"Writing byte %i",byte);
print_log(3,str);
for (i = 0; i < 8; i++)
{
write_bit(ws, byte & 0x80);
byte <<= 1;
byte &= 0xff;
}
set_RTS(ws,0);
nanodelay();
status = get_CTS(ws);
//TODO: checking value of status, error routine
nanodelay();
set_DTR(ws,0);
nanodelay();
set_DTR(ws,1);
nanodelay();
if (status)
return 1;
else
return 0;
}
/********************************************************************
* log
********************************************************************/
void print_log(int log_level, char* str) {
if (log_level < 0)
fprintf(stderr,"%s\n",str);
}