PageRenderTime 20ms CodeModel.GetById 10ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

/elekIO/elekIO.c

http://github.com/Yniold/liftsrc
C | 266 lines | 174 code | 60 blank | 32 comment | 10 complexity | f1f2a3ed34b4ec6cd075cf789cbd7c89 MD5 | raw file
  1/*
  2* $RCSfile: elekIO.c,v $ last changed on $Date: 2006/09/04 11:40:36 $ by $Author: rudolf $
  3*
  4* $Log: elekIO.c,v $
  5* Revision 1.4  2006/09/04 11:40:36  rudolf
  6* Fixed warnings for GCC 4.03
  7*
  8* Revision 1.3  2005/04/21 15:01:58  rudolf
  9* made elekIO work with ARM controller. Added routines to open device driver for read and write to the backplane
 10*
 11* Revision 1.2  2005/04/21 14:11:42  rudolf
 12* added revision history field
 13*
 14*
 15*/
 16
 17#include <stdio.h>
 18#include <stdlib.h>
 19#include <unistd.h>
 20#include <sys/io.h>
 21#include <stdint.h>
 22
 23#include <ctype.h>
 24#include <string.h>
 25#include <errno.h>
 26#include <sys/types.h>
 27#include <sys/time.h>
 28
 29#include <fcntl.h>
 30#include <unistd.h>
 31
 32#include "../include/elekIO.h"
 33
 34#ifdef RUNONARM
 35	#define SERBUS_IOCSDEBUGON    0x40047301
 36	#define SERBUS_IOCSDEBUGOFF   0x40047302
 37	#define SERBUS_IOCTWRITEWORD  0x40047303
 38	#define SERBUS_IOCHREADWORD   0x40047304
 39#endif
 40
 41#ifdef RUNONPC
 42//=====================================================================================================
 43// PC Version
 44
 45int elkInit (void) {
 46
 47     int ret;
 48
 49    ret=ioperm(ELK_BASE, 10, 1);
 50    return (ret);
 51}
 52//=====================================================================================================
 53#endif
 54
 55#ifdef RUNONARM
 56//=====================================================================================================
 57// ARM9 version
 58int fd = 0;
 59
 60int elkInit (void)
 61{
 62	printf("Trying to open /dev/serbus...\n\r");
 63    fd = open("/dev/serbus",O_RDWR);
 64
 65	if(fd < 0)
 66	{
 67		printf("Failed...\n\r");
 68    	return (1);
 69	}
 70	else
 71	{
 72		printf("Success!\n\r");
 73		ioctl(fd, SERBUS_IOCSDEBUGOFF);
 74		return (0);
 75	}
 76}
 77//=====================================================================================================
 78#endif
 79
 80#ifdef RUNONARM
 81//=====================================================================================================
 82// ARM9 Version
 83int elkExit (void)
 84{
 85	close(fd);
 86	return (0);
 87}
 88//=====================================================================================================
 89#endif
 90
 91#ifdef RUNONPC
 92int elkExit (void) {
 93
 94    int ret;
 95
 96    ret=ioperm(ELK_BASE, 10, 0);                // we don't need it anymore
 97    return (ret);
 98}
 99//=====================================================================================================
100#endif
101
102#ifdef RUNONPC
103int elkWriteData(uint16_t Adress, uint16_t Data)
104{
105
106    unsigned Counter=ELK_TIMEOUT;
107    uint16_t ElkQToDo=1;
108    uint16_t ElkQReady;
109    uint16_t ElkQReadyNew;
110    uint16_t ElkQStatus;
111    uint16_t ElkReadData;
112    int ret;
113
114#if (DEBUGLEVEL > 0)
115    printf("Write adr %x to port %x and data %x to adr %x\n",Adress,ELK_ADR,Data,ELK_DATA);
116#endif
117
118    outw(Adress, ELK_ADR);
119    outw(Data, ELK_DATA);
120
121#if (DEBUGLEVEL > 5)
122    printf("ElkTodo ");
123#endif
124
125    while (ElkQToDo && Counter) {
126	Counter--;
127	ElkQStatus = inw(ELK_TODO);
128	ElkQReady  = ElkQStatus & 0x000f;
129	ElkQToDo   = ElkQStatus>>8;
130#if (DEBUGLEVEL > 5)
131	printf(":%2x %2x",ElkQToDo,ElkQReady);
132#endif
133    }
134#if (DEBUGLEVEL > 5)
135    printf("\n");
136#endif
137
138    if ((unsigned)0==Counter) { // did we run into a timeout ?
139	ret=ELK_STAT_TIMEOUT;
140	return(ret);
141    }
142
143    // in the case of a write command to the ELK we remove the command from the
144    // ELK Queue as soon as it is processed
145
146    ElkReadData  = inw(ELK_DATA);                        // read on data increase Tailptr
147    ElkQStatus   = inw(ELK_TODO);                        // let see if it worked
148    ElkQReadyNew = ElkQStatus & 0x000f;
149    ElkQToDo     = ElkQStatus>>8;
150
151#if (DEBUGLEVEL > 0)
152	printf("QStat %04x QReady %x QReadyNew %x\n",ElkQStatus,ElkQReady,ElkQReadyNew);
153#endif
154
155    if (((ElkQReady-1) % ELK_QSIZE)!=ElkQReadyNew ) { // we have a problem with the Q
156
157#if (DEBUGLEVEL > 0)
158	printf("Problem in ELKQueue, QReady QReadyNew \n");
159#endif
160
161    }
162} // end elkWriteData(uint16_t Adress, uint16_t Data)
163
164//=====================================================================================================
165#endif
166
167#ifdef RUNONPC
168int elkReadData(uint16_t Adress) {
169
170    unsigned Counter=ELK_TIMEOUT;
171    uint16_t ElkQToDo=1;
172    uint16_t ElkQReady;
173    uint16_t ElkQReadyNew;
174    uint16_t ElkQStatus;
175    uint16_t ElkReadData;
176    uint16_t ElkReadAdress;
177
178    int ret;
179    
180    Adress=Adress | (uint16_t) 0x0001;            // LSB of Adress for ReadCommand has to be 1 
181
182    ElkReadData = (uint16_t) 0;                  // init ElkReadData to something
183
184#if (DEBUGLEVEL > 0)
185    printf("Write adr %x to port %x and data %x to adr %x\n",Adress,ELK_ADR,ElkReadData,ELK_DATA);
186#endif
187
188    outw(Adress, ELK_ADR);                      // Adress we want the data from
189    outw(ElkReadData, ELK_DATA);                       // write to Dataport to submit request
190
191#if (DEBUGLEVEL > 5)
192    printf("ElkTodo ");
193#endif
194
195    while (ElkQToDo && Counter) {
196	Counter--;
197	ElkQStatus = inw(ELK_TODO);
198	ElkQReady  = ElkQStatus & 0x000f;
199	ElkQToDo   = ElkQStatus>>8;
200#if (DEBUGLEVEL > 5)
201	printf(":TD%2x Rdy%2x",ElkQToDo,ElkQReady);
202#endif
203    }
204#if (DEBUGLEVEL > 5)
205    printf("\n");
206#endif
207
208    if ((unsigned)0==Counter) { // did we run into a timeout ?
209	ret=ELK_STAT_TIMEOUT;
210	return(ret);
211    } 
212    
213    // in the case of a read command to the ELK we read back the adress for consistency check 
214    // and the Data of course ;)
215
216    ElkReadAdress = inw(ELK_ADR);                         // get the address
217    ElkReadData   = inw(ELK_DATA);                        // read on data and increase Tailptr
218
219//	printf("Problem in ELKQueue, expected Adress %x, Read Address %x \n",Adress,ElkReadAdress);
220
221    if (ElkReadAdress!=Adress) {                          // did we get the same adress back ?
222#if (DEBUGLEVEL > 0)
223	printf("Problem in ELKQueue, expected Adress %x, Read Address %x \n",Adress,ElkReadAdress);
224#endif
225    }
226
227    ElkQStatus    = inw(ELK_TODO);                        // let see if it worked
228    ElkQReadyNew  = ElkQStatus & 0x000f;
229    ElkQToDo      = ElkQStatus>>8;
230    
231#if (DEBUGLEVEL > 0)
232	printf("QStat %04x QReady %x QReadyNew %x\n",ElkQStatus,ElkQReady,ElkQReadyNew);
233#endif
234
235
236    if (((ElkQReady-1) % ELK_QSIZE)!=ElkQReadyNew ) { // we have a problem with the Q
237
238#if (DEBUGLEVEL > 0)
239	printf("Problem in ELKQueue, QReady %x QReadyNew %x\n",ElkQReady,ElkQReadyNew);
240#endif
241	
242    }
243
244    return(ElkReadData);
245
246
247} // end elkReadData(uint16_t Adress, uint16_t Data)
248
249//======================================================================================================
250#endif
251
252#ifdef RUNONARM
253int elkWriteData(uint16_t Adress, uint16_t Data)
254{
255	return (ioctl(fd, SERBUS_IOCTWRITEWORD, (unsigned long)(Adress) + (unsigned long)(Data<<16)));
256}
257
258int elkReadData(uint16_t Adress)
259{
260	return (ioctl(fd, SERBUS_IOCHREADWORD, (unsigned long)(Adress)));
261}
262
263#endif
264
265    
266