forked from INQNET/klimalogger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
linux3600.c
381 lines (323 loc) · 9.64 KB
/
linux3600.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/* open3600 - linux3600 library functions
* This file contains the common functions that are unique to
* Linux. The entire file is ignored in case of Windows
*
* Version 0.01
*
* Control WS3600 weather station
*
* Copyright 2003-2005, Kenneth Lavrsen, Grzegorz Wisniewski, Sander Eerkes
* This program is published under the GNU General Public license
*/
#define DEBUG 0
#include "eeprom.h"
#include "mcdelay.h"
#include "mcdelay.c"
#define PAGE_SIZE (4*1024)
#define BLOCK_SIZE (4*1024)
int mem_fd;
char *gpio_mem, *gpio_map;
char *spi0_mem, *spi0_map;
// I/O access
volatile unsigned *gpio;
#define BCM2708_PERI_BASE 0x20000000
#define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
// GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or SET_GPIO_ALT(x,y)
#define INP_GPIO(g) *(gpio+((g)/10)) &= ~(7<<(((g)%10)*3))
#define OUT_GPIO(g) *(gpio+((g)/10)) |= (1<<(((g)%10)*3))
#define SET_GPIO_ALT(g,a) *(gpio+(((g)/10))) |= (((a)<=3?(a)+4:(a)==4?3:2)<<(((g)%10)*3))
#define GPIO_SET *(gpio+7) // sets bits which are 1 ignores bits which are 0
#define GPIO_CLR *(gpio+10) // clears bits which are 1 ignores bits which are 0
//
// Set up a memory regions to access GPIO
//
void setup_io()
{
/* open /dev/mem */
if ((mem_fd = open("/dev/mem", O_RDWR|O_SYNC) ) < 0) {
printf("can't open /dev/mem \n");
exit (-1);
}
/* mmap GPIO */
// Allocate MAP block
if ((gpio_mem = malloc(BLOCK_SIZE + (PAGE_SIZE-1))) == NULL) {
printf("allocation error \n");
exit (-1);
}
// Make sure pointer is on 4K boundary
if ((unsigned long)gpio_mem % PAGE_SIZE)
gpio_mem += PAGE_SIZE - ((unsigned long)gpio_mem % PAGE_SIZE);
// Now map it
gpio_map = (unsigned char *)mmap(
(caddr_t)gpio_mem,
BLOCK_SIZE,
PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_FIXED,
mem_fd,
GPIO_BASE
);
if ((long)gpio_map < 0) {
printf("mmap error %d\n", (int)gpio_map);
exit (-1);
}
// Always use volatile pointer!
gpio = (volatile unsigned *)gpio_map;
}
/********************************************************************
* open_weatherstation, Windows version
*
* Input: devicename (COM1, COM2 etc)
*
* Returns: Handle to the weatherstation (type WEATHERSTATION)
*
********************************************************************/
WEATHERSTATION open_weatherstation (char *device) {
WEATHERSTATION ws;
struct termios adtio;
unsigned char buffer[BUFFER_SIZE];
long i;
print_log(1,"open_weatherstation");
//calibrate nanodelay function
microdelay_init(1);
// Set up gpi pointer for direct register access
setup_io();
// Switch GPIO 7..11 to output mode
/************************************************************************\
* You are about to change the GPIO settings of your computer. *
* Mess this up and it will stop working! *
* It might be a good idea to 'sync' before running this program *
* so at least you still have your code changes written to the SD-card! *
\************************************************************************/
// Set GPIO pins 7-11 to output
for (g=7; g<=11; g++)
{
INP_GPIO(g); // must use INP_GPIO before we can use OUT_GPIO
OUT_GPIO(g);
}
//Setup serial port
if ((ws = open(device, O_RDWR | O_NOCTTY)) < 0)
{
printf("\nUnable to open serial device %s\n", device);
exit(EXIT_FAILURE);
}
if ( flock(ws, LOCK_EX) < 0 ) {
perror("\nSerial device is locked by other program\n");
exit(EXIT_FAILURE);
}
//We want full control of what is set and simply reset the entire adtio struct
memset(&adtio, 0, sizeof(adtio));
// Serial control options
adtio.c_cflag &= ~PARENB; // No parity
adtio.c_cflag &= ~CSTOPB; // One stop bit
adtio.c_cflag &= ~CSIZE; // Character size mask
adtio.c_cflag |= CS8; // Character size 8 bits
adtio.c_cflag |= CREAD; // Enable Receiver
//adtio.c_cflag &= ~CREAD; // Disable Receiver
adtio.c_cflag &= ~HUPCL; // No "hangup"
adtio.c_cflag &= ~CRTSCTS; // No flowcontrol
adtio.c_cflag |= CLOCAL; // Ignore modem control lines
// Baudrate, for newer systems
cfsetispeed(&adtio, BAUDRATE);
cfsetospeed(&adtio, BAUDRATE);
// Serial local options: adtio.c_lflag
// Raw input = clear ICANON, ECHO, ECHOE, and ISIG
// Disable misc other local features = clear FLUSHO, NOFLSH, TOSTOP, PENDIN, and IEXTEN
// So we actually clear all flags in adtio.c_lflag
adtio.c_lflag = 0;
// Serial input options: adtio.c_iflag
// Disable parity check = clear INPCK, PARMRK, and ISTRIP
// Disable software flow control = clear IXON, IXOFF, and IXANY
// Disable any translation of CR and LF = clear INLCR, IGNCR, and ICRNL
// Ignore break condition on input = set IGNBRK
// Ignore parity errors just in case = set IGNPAR;
// So we can clear all flags except IGNBRK and IGNPAR
adtio.c_iflag = IGNBRK|IGNPAR;
// Serial output options
// Raw output should disable all other output options
adtio.c_oflag &= ~OPOST;
adtio.c_cc[VTIME] = 10; // timer 1s
adtio.c_cc[VMIN] = 0; // blocking read until 1 char
if (tcsetattr(ws, TCSANOW, &adtio) < 0)
{
printf("Unable to initialize serial device");
exit(0);
}
tcflush(ws, TCIOFLUSH);
for (i = 0; i < 448; i++) {
buffer[i] = 'U';
}
write(ws, buffer, 448);
set_DTR(ws,0);
set_RTS(ws,0);
i = 0;
do {
sleep_short(10);
i++;
} while (i < INIT_WAIT && !get_DSR(ws));
if (i == INIT_WAIT)
{
print_log(2,"Connection timeout 1");
printf ("Connection timeout\n");
close_weatherstation(ws);
exit(0);
}
i = 0;
do {
sleep_short(10);
i++;
} while (i < INIT_WAIT && get_DSR(ws));
if (i != INIT_WAIT) {
set_RTS(ws,1);
set_DTR(ws,1);
} else {
print_log(2,"Connection timeout 2");
printf ("Connection timeout\n");
close_weatherstation(ws);
exit(0);
}
write(ws, buffer, 448);
return ws;
}
/********************************************************************
* close_weatherstation, Linux version
*
* Input: Handle to the weatherstation (type WEATHERSTATION)
*
* Returns nothing
*
********************************************************************/
void close_weatherstation(WEATHERSTATION ws)
{
tcflush(ws,TCIOFLUSH);
close(ws);
return;
}
/********************************************************************
* set_DTR
* Sets or resets DTR signal
*
* Inputs: serdevice - opened file handle
* val - value to set
*
* Returns nothing
*
********************************************************************/
void set_DTR(WEATHERSTATION ws, int val)
{
//TODO: use TIOCMBIC and TIOCMBIS instead of TIOCMGET and TIOCMSET
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (val)
{
print_log(5,"Set DTR");
portstatus |= TIOCM_DTR;
}
else
{
print_log(5,"Clear DTR");
portstatus &= ~TIOCM_DTR;
}
ioctl(ws, TIOCMSET, &portstatus); // set current port status
/*if (val)
ioctl(ws, TIOCMBIS, TIOCM_DTR);
else
ioctl(ws, TIOCMBIC, TIOCM_DTR);*/
}
/********************************************************************
* set_RTS
* Sets or resets RTS signal
*
* Inputs: serdevice - opened file handle,
* val - value to set
*
* Returns nothing
*
********************************************************************/
void set_RTS(WEATHERSTATION ws, int val)
{
//TODO: use TIOCMBIC and TIOCMBIS instead of TIOCMGET and TIOCMSET
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (val)
{
print_log(5,"Set RTS");
portstatus |= TIOCM_RTS;
}
else
{
print_log(5,"Clear RTS");
portstatus &= ~TIOCM_RTS;
}
ioctl(ws, TIOCMSET, &portstatus); // set current port status
/*if (val)
ioctl(ws, TIOCMBIS, TIOCM_RTS);
else
ioctl(ws, TIOCMBIC, TIOCM_RTS);
*/
}
/********************************************************************
* get_DSR
* Checks status of DSR signal
*
* Inputs: ws - opened file handle
*
*
* Returns: status of DSR signal
*
********************************************************************/
int get_DSR(WEATHERSTATION ws)
{
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (portstatus & TIOCM_DSR)
{
print_log(5,"Got DSR = 1");
return 1;
}
else
{
print_log(5,"Got DSR = 0");
return 0;
}
}
/********************************************************************
* get_CTS
* Checks status of CTS signal
*
* Inputs: ws - opened file handle
*
*
* Returns: status of CTS signal
*
********************************************************************/
int get_CTS(WEATHERSTATION ws)
{
int portstatus;
ioctl(ws, TIOCMGET, &portstatus); // get current port status
if (portstatus & TIOCM_CTS)
{
print_log(5,"Got CTS = 1");
return 1;
}
else
{
print_log(5,"Got CTS = 0");
return 0;
}
}
/********************************************************************
* sleep_short - Linux version
*
* Inputs: Time in milliseconds (integer)
*
* Returns: nothing
*
********************************************************************/
void sleep_short(int milliseconds)
{
usleep(milliseconds * 1000);
}
/* Note: if you see timing issues, maybe you need to adjust this ... */
void nanodelay() {
microdelay(10);
}