-
Notifications
You must be signed in to change notification settings - Fork 2
/
adsensors.c
222 lines (195 loc) · 5.72 KB
/
adsensors.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
/* Emulator for LEGO RCX Brick, Copyright (C) 2003 Jochen Hoenicke.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; see the file COPYING.LESSER. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
* $Id: adsensors.c 152 2005-08-13 15:24:05Z hoenicke $
*/
#include <unistd.h>
#include <fcntl.h>
#include <netinet/in.h> /* for htonx/ntohx */
#include "h8300.h"
#include "memory.h"
#include "peripherals.h"
#define ADCSR_ADF 0x80
#define ADCSR_ADIE 0x40
#define ADCSR_ADST 0x20
#define ADCSR_SCAN 0x10
#define ADCSR_CKS 0x08
#define ADCSR_CH 0x07
static uint16 values[8], polled[4];
static uint8 tmp;
static uint8 adcsr, read_adcsr, adcr, adchannel;
static cycle_count_t ad_start_cycle;
typedef struct {
cycle_count_t ad_start_cycle;
uint16 polled[4];
uint8 tmp;
uint8 adcsr, read_adcsr, adcr, adchannel;
} ad_save_type;
static int ad_save(void *buffer, int maxlen) {
int i;
ad_save_type *data = buffer;
for (i = 0; i< 4; i++)
data->polled[i] = htons(polled[i]);
data->tmp = tmp;
data->adcsr = adcsr;
data->read_adcsr = read_adcsr;
data->adcr = adcr;
data->adchannel = adchannel;
data->ad_start_cycle = htonl(ad_start_cycle);
return sizeof(ad_save_type);
}
static void ad_load(void *buffer, int len) {
int i;
ad_save_type *data = buffer;
for (i = 0; i< 4; i++)
polled[i] = ntohs(data->polled[i]);
tmp = data->tmp;
adcsr = data->adcsr;
read_adcsr = data->read_adcsr;
adcr = data->adcr;
adchannel = data->adchannel;
ad_start_cycle = ntohl(data->ad_start_cycle);
}
static void ad_reset() {
adcr = 0x7f;
adcsr = 0;
adchannel = 0;
}
static void ad_read_fd(int fd) {
char buf[5];
int len = 0;
/* read in 5 bytes: sensorid 3xVal newline */
do {
len += read(fd, buf + len, 5 - len);
} while (len < 5);
values[buf[0]-'0'] =
((buf[1] > '9' ? buf[1] - 'a' + 10 : buf[1]-'0') << 14)
| ((buf[2] > '9' ? buf[2] - 'a' + 10 : buf[2]-'0') << 10)
| ((buf[3] > '9' ? buf[3] - 'a' + 10 : buf[3]-'0') << 6);
}
static void ad_check_next_cycle() {
if ((adcsr & 0xc0) == 0xc0) {
next_timer_cycle = cycles;
} else if ((adcsr & (ADCSR_ADIE|ADCSR_ADST)) == (ADCSR_ADIE|ADCSR_ADST)) {
cycle_count_t next_conv_time = add_to_cycle('S', ad_start_cycle, (adcsr & ADCSR_CKS ? 134 : 266));
if (next_conv_time < next_timer_cycle) {
next_timer_cycle = next_conv_time;
}
}
}
static void ad_update_time() {
int conv_time = (adcsr & ADCSR_CKS ? 134 : 266);
while ((adcsr & ADCSR_ADST)) {
int32 polling_time = cycles - ad_start_cycle;
if (polling_time < conv_time)
break;
polled[adchannel & 3] = values[adchannel];
if ((adcsr & ADCSR_SCAN)) {
ad_start_cycle += conv_time;
if (adchannel < (adcsr & ADCSR_CH)) {
adchannel++;
} else {
adchannel &= 4;
adcsr = (adcsr & ~ADCSR_ADST) | ADCSR_ADF;
}
} else {
adcsr = (adcsr & ~ADCSR_ADST) | ADCSR_ADF;
}
}
ad_check_next_cycle();
}
static int ad_check_irq() {
if ((adcsr & 0xc0) == 0xc0)
return 35;
return 255;
}
static void set_adcsr(uint8 value) {
value &= 0x7f | read_adcsr; /* high bit can only be cleared */
#ifdef VERBOSE_ADSENSORS
printf("adsensors.c: set_adcsr(%02x)\n", value);
#endif
if ((~adcsr & value & ADCSR_ADST)) {
adchannel = (value & ADCSR_SCAN) ? value & 4 : value & ADCSR_CH;
ad_start_cycle = cycles;
}
adcsr = value;
ad_check_next_cycle();
}
static uint8 get_adcsr(void) {
return read_adcsr = adcsr;
}
static void set_adcr(uint8 value) {
adcr = value | 0x7f;
#ifdef VERBOSE_ADSENSORS
printf("adsensors.c: set_adcr(%02x)\n", value);
#endif
ad_check_next_cycle();
}
static uint8 get_adcr(void) {
return adcr;
}
static uint8 get_addrah(void) {
tmp = (polled[0] & 0xc0);
return (polled[0] >> 8);
}
static uint8 get_addral(void) {
return tmp;
}
static uint8 get_addrbh(void) {
tmp = (polled[1] & 0xc0);
return (polled[1] >> 8);
}
static uint8 get_addrbl(void) {
return tmp;
}
static uint8 get_addrch(void) {
tmp = (polled[2] & 0xc0);
return (polled[2] >> 8);
}
static uint8 get_addrcl(void) {
return tmp;
}
static uint8 get_addrdh(void) {
tmp = (polled[3] & 0xc0);
return (polled[3] >> 8);
}
static uint8 get_addrdl(void) {
return tmp;
}
static peripheral_ops adsensor = {
id: 'A',
reset: ad_reset,
update_time: ad_update_time,
read_fd: ad_read_fd,
check_irq: ad_check_irq,
load_data: ad_load,
save_data: ad_save
};
void ad_init() {
port[0xe0 - 0x88].get = get_addrah;
port[0xe1 - 0x88].get = get_addral;
port[0xe2 - 0x88].get = get_addrbh;
port[0xe3 - 0x88].get = get_addrbl;
port[0xe4 - 0x88].get = get_addrch;
port[0xe5 - 0x88].get = get_addrcl;
port[0xe6 - 0x88].get = get_addrdh;
port[0xe7 - 0x88].get = get_addrdl;
port[0xe8 - 0x88].get = get_adcsr;
port[0xe8 - 0x88].set = set_adcsr;
port[0xe9 - 0x88].get = get_adcr;
port[0xe9 - 0x88].set = set_adcr;
register_peripheral(adsensor);
}