Subversion Repositories shark

Rev

Rev 281 | Rev 283 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
281 giacomo 1
/*
2
 * Project: S.Ha.R.K.
3
 *
4
 * Coordinators:
5
 *   Giorgio Buttazzo    <giorgio@sssup.it>
6
 *   Paolo Gai           <pj@gandalf.sssup.it>
7
 *
8
 * Authors     :
9
 *   Giacomo Guidi       <giacomo@gandalf.sssup.it>
10
 *   (see the web pages for full authors list)
11
 *
12
 * ReTiS Lab (Scuola Superiore S.Anna - Pisa - Italy)
13
 *
14
 * http://www.sssup.it
15
 * http://retis.sssup.it
16
 * http://shark.sssup.it
17
 */
18
 
19
/*
20
 * Copyright (C) 2002 Paolo Gai
21
 *
22
 * This program is free software; you can redistribute it and/or modify
23
 * it under the terms of the GNU General Public License as published by
24
 * the Free Software Foundation; either version 2 of the License, or
25
 * (at your option) any later version.
26
 *
27
 * This program is distributed in the hope that it will be useful,
28
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30
 * GNU General Public License for more details.
31
 *
32
 * You should have received a copy of the GNU General Public License
33
 * along with this program; if not, write to the Free Software
34
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
35
 *
36
 */
37
 
38
#include <ll/sys/ll/ll-instr.h>
282 giacomo 39
#include "kernel/kern.h"
281 giacomo 40
#include "servo.h"
41
 
42
#define THR 0
43
#define RBR 0
44
#define IER 1
45
#define FCR 2
46
#define IIR 2
47
#define LCR 3
48
#define MCR 4
49
#define LSR 5
50
#define MSR 6
51
#define SPad 7
52
 
53
#define barrier() __asm__ __volatile__("" ::: "memory");
54
 
282 giacomo 55
#define SERVO_TIMEOUT 100000 /* us */
281 giacomo 56
 
282 giacomo 57
#define SERVO_PORT 1
58
 
281 giacomo 59
int timer_expired = 0;
60
unsigned com_base[] = {0x03F8,0x02F8,0x03E8,0x02E8};
61
 
282 giacomo 62
void set_timer_expired(void *arg)
281 giacomo 63
{
64
 
65
  timer_expired = 1;
66
 
67
}
68
 
69
unsigned com_read(unsigned port,unsigned reg)
70
{
71
    unsigned b;
72
    if (port > 3 || reg > 7) return(0);
73
    b = ll_in(com_base[port]+reg);
74
    return(b);
75
}
76
 
77
void com_write(unsigned port,unsigned reg,unsigned value)
78
{
79
    if (port > 3 || reg > 7) return;
80
    ll_out(com_base[port]+reg,value);
81
}
82
 
83
int com_send(unsigned port,BYTE b)
84
{
85
    while ((com_read(port,LSR) & 32) == 0 && !timer_expired)
86
      barrier();
87
    if (!timer_expired) {
88
      com_write(port,THR,b);
89
      return 0;
90
    } else {
91
      return -1;
92
    }
93
}
94
 
95
int com_receive(unsigned port)
96
{
97
    while ((com_read(port,LSR) & 1) == 0 && !timer_expired)
98
      barrier();
99
    if (!timer_expired) {
100
      return((int)(com_read(port,RBR)));
101
    } else {
102
      return -1;
103
    }
104
}
105
 
106
int servo_set_RS232_baudrate(int baud)
107
{
108
 
109
 
110
  return 0;
111
 
112
}
113
 
114
int servo_get_RS232_baudrate(void)
115
{
116
 
117
  return 0;
118
 
119
}
120
 
121
int servo_store_RS232_buadrate(void)
122
{
123
 
124
  return 0;
125
 
126
}
127
 
128
int servo_set_period(int period)
129
{
130
 
131
  return 0;
132
 
133
}
134
 
135
int servo_get_period(void)
136
{
137
 
138
  return 0;
139
 
140
}
141
 
142
int servo_store_period(void)
143
{
144
 
145
  return 0;
146
 
147
}
148
 
149
int servo_get_setup_switch(void)
150
{
151
 
152
  return 0;
153
 
154
}
155
 
156
int servo_set_RC5_switch(int data)
157
{
158
 
159
  return 0;
160
 
161
}
162
 
282 giacomo 163
/* 0000.0ppp:0000.vvvv:vvvv.vvvv */
281 giacomo 164
int servo_set_angle(int servo, int angle)
165
{
166
 
282 giacomo 167
  struct timespec current_time;
168
  unsigned char b;
169
  int err,timeout_event;
170
 
171
  if (servo > 7) return -1;
172
 
173
  timer_expired = 0;
174
  kern_gettime(&current_time);
175
  ADDUSEC2TIMESPEC(SERVO_TIMEOUT,&current_time);
176
  timeout_event = kern_event_post(&current_time, set_timer_expired, NULL);
177
 
178
  b = 0x00 | (servo & 0x07);
179
  err = com_send(SERVO_PORT, b);
180
  err = com_receive(SERVO_PORT);
181
  if (err != (int)(b)) return -1;
182
 
183
  b = 0x00 | ((angle >> 8) & 0x0F);
184
  err = com_send(SERVO_PORT, b);
185
  err = com_receive(SERVO_PORT);
186
  if (err != (int)(b)) return -1;
187
 
188
  b = 0x00 | (angle & 0xFF);
189
  err = com_send(SERVO_PORT, b);
190
  err = com_receive(SERVO_PORT);
191
  if (err != (int)(b)) return -1;  
192
 
193
  kern_event_delete(timeout_event);
194
 
281 giacomo 195
  return 0;
196
 
197
}
198
 
282 giacomo 199
/* 0000.1ppp */
281 giacomo 200
int servo_get_angle(int servo)
201
{
202
 
282 giacomo 203
  struct timespec current_time;
204
  unsigned char b;
205
  int err,res,timeout_event;
206
 
207
  if (servo > 7) return -1;
208
 
209
  timer_expired = 0;
210
  kern_gettime(&current_time);
211
  ADDUSEC2TIMESPEC(SERVO_TIMEOUT,&current_time);
212
  timeout_event = kern_event_post(&current_time, set_timer_expired, NULL);
281 giacomo 213
 
282 giacomo 214
  b = 0x08 | (servo & 0x07);
215
  err = com_send(SERVO_PORT, b);
216
  err = com_receive(SERVO_PORT);
217
  if (err != (int)(b)) return -1;
218
  res = com_receive(SERVO_PORT);
219
 
220
  kern_event_delete(timeout_event);
221
 
222
  if (!timer_expired)
223
    return res;
224
  else
225
    return -1;
226
 
281 giacomo 227
}
228
 
282 giacomo 229
/* 0100:0aaa */
281 giacomo 230
int servo_get_analog(int port)
231
{
232
 
282 giacomo 233
  struct timespec current_time;
234
  unsigned char b;
235
  int err,res,timeout_event;
236
 
237
  if (port > 4) return -1;
238
 
239
  timer_expired = 0;
240
  kern_gettime(&current_time);
241
  ADDUSEC2TIMESPEC(SERVO_TIMEOUT,&current_time);
242
  timeout_event = kern_event_post(&current_time, set_timer_expired, NULL);
281 giacomo 243
 
282 giacomo 244
  b = 0x40 | (port & 7);
245
  err = com_send(SERVO_PORT, b);
246
  err = com_receive(SERVO_PORT);
247
  if (err != (int)(b)) return -1;
248
  res = com_receive(SERVO_PORT) << 8;
249
  res |= com_receive(SERVO_PORT);
250
 
251
  kern_event_delete(timeout_event);
252
 
253
  if (!timer_expired)
254
    return res;
255
  else
256
    return -1;
257
 
281 giacomo 258
}
259