/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. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include <unistd.h>
  19. #include <sys/io.h>
  20. #include <stdint.h>
  21. #include <ctype.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <sys/types.h>
  25. #include <sys/time.h>
  26. #include <fcntl.h>
  27. #include <unistd.h>
  28. #include "../include/elekIO.h"
  29. #ifdef RUNONARM
  30. #define SERBUS_IOCSDEBUGON 0x40047301
  31. #define SERBUS_IOCSDEBUGOFF 0x40047302
  32. #define SERBUS_IOCTWRITEWORD 0x40047303
  33. #define SERBUS_IOCHREADWORD 0x40047304
  34. #endif
  35. #ifdef RUNONPC
  36. //=====================================================================================================
  37. // PC Version
  38. int elkInit (void) {
  39. int ret;
  40. ret=ioperm(ELK_BASE, 10, 1);
  41. return (ret);
  42. }
  43. //=====================================================================================================
  44. #endif
  45. #ifdef RUNONARM
  46. //=====================================================================================================
  47. // ARM9 version
  48. int fd = 0;
  49. int elkInit (void)
  50. {
  51. printf("Trying to open /dev/serbus...\n\r");
  52. fd = open("/dev/serbus",O_RDWR);
  53. if(fd < 0)
  54. {
  55. printf("Failed...\n\r");
  56. return (1);
  57. }
  58. else
  59. {
  60. printf("Success!\n\r");
  61. ioctl(fd, SERBUS_IOCSDEBUGOFF);
  62. return (0);
  63. }
  64. }
  65. //=====================================================================================================
  66. #endif
  67. #ifdef RUNONARM
  68. //=====================================================================================================
  69. // ARM9 Version
  70. int elkExit (void)
  71. {
  72. close(fd);
  73. return (0);
  74. }
  75. //=====================================================================================================
  76. #endif
  77. #ifdef RUNONPC
  78. int elkExit (void) {
  79. int ret;
  80. ret=ioperm(ELK_BASE, 10, 0); // we don't need it anymore
  81. return (ret);
  82. }
  83. //=====================================================================================================
  84. #endif
  85. #ifdef RUNONPC
  86. int elkWriteData(uint16_t Adress, uint16_t Data)
  87. {
  88. unsigned Counter=ELK_TIMEOUT;
  89. uint16_t ElkQToDo=1;
  90. uint16_t ElkQReady;
  91. uint16_t ElkQReadyNew;
  92. uint16_t ElkQStatus;
  93. uint16_t ElkReadData;
  94. int ret;
  95. #if (DEBUGLEVEL > 0)
  96. printf("Write adr %x to port %x and data %x to adr %x\n",Adress,ELK_ADR,Data,ELK_DATA);
  97. #endif
  98. outw(Adress, ELK_ADR);
  99. outw(Data, ELK_DATA);
  100. #if (DEBUGLEVEL > 5)
  101. printf("ElkTodo ");
  102. #endif
  103. while (ElkQToDo && Counter) {
  104. Counter--;
  105. ElkQStatus = inw(ELK_TODO);
  106. ElkQReady = ElkQStatus & 0x000f;
  107. ElkQToDo = ElkQStatus>>8;
  108. #if (DEBUGLEVEL > 5)
  109. printf(":%2x %2x",ElkQToDo,ElkQReady);
  110. #endif
  111. }
  112. #if (DEBUGLEVEL > 5)
  113. printf("\n");
  114. #endif
  115. if ((unsigned)0==Counter) { // did we run into a timeout ?
  116. ret=ELK_STAT_TIMEOUT;
  117. return(ret);
  118. }
  119. // in the case of a write command to the ELK we remove the command from the
  120. // ELK Queue as soon as it is processed
  121. ElkReadData = inw(ELK_DATA); // read on data increase Tailptr
  122. ElkQStatus = inw(ELK_TODO); // let see if it worked
  123. ElkQReadyNew = ElkQStatus & 0x000f;
  124. ElkQToDo = ElkQStatus>>8;
  125. #if (DEBUGLEVEL > 0)
  126. printf("QStat %04x QReady %x QReadyNew %x\n",ElkQStatus,ElkQReady,ElkQReadyNew);
  127. #endif
  128. if (((ElkQReady-1) % ELK_QSIZE)!=ElkQReadyNew ) { // we have a problem with the Q
  129. #if (DEBUGLEVEL > 0)
  130. printf("Problem in ELKQueue, QReady QReadyNew \n");
  131. #endif
  132. }
  133. } // end elkWriteData(uint16_t Adress, uint16_t Data)
  134. //=====================================================================================================
  135. #endif
  136. #ifdef RUNONPC
  137. int elkReadData(uint16_t Adress) {
  138. unsigned Counter=ELK_TIMEOUT;
  139. uint16_t ElkQToDo=1;
  140. uint16_t ElkQReady;
  141. uint16_t ElkQReadyNew;
  142. uint16_t ElkQStatus;
  143. uint16_t ElkReadData;
  144. uint16_t ElkReadAdress;
  145. int ret;
  146. Adress=Adress | (uint16_t) 0x0001; // LSB of Adress for ReadCommand has to be 1
  147. ElkReadData = (uint16_t) 0; // init ElkReadData to something
  148. #if (DEBUGLEVEL > 0)
  149. printf("Write adr %x to port %x and data %x to adr %x\n",Adress,ELK_ADR,ElkReadData,ELK_DATA);
  150. #endif
  151. outw(Adress, ELK_ADR); // Adress we want the data from
  152. outw(ElkReadData, ELK_DATA); // write to Dataport to submit request
  153. #if (DEBUGLEVEL > 5)
  154. printf("ElkTodo ");
  155. #endif
  156. while (ElkQToDo && Counter) {
  157. Counter--;
  158. ElkQStatus = inw(ELK_TODO);
  159. ElkQReady = ElkQStatus & 0x000f;
  160. ElkQToDo = ElkQStatus>>8;
  161. #if (DEBUGLEVEL > 5)
  162. printf(":TD%2x Rdy%2x",ElkQToDo,ElkQReady);
  163. #endif
  164. }
  165. #if (DEBUGLEVEL > 5)
  166. printf("\n");
  167. #endif
  168. if ((unsigned)0==Counter) { // did we run into a timeout ?
  169. ret=ELK_STAT_TIMEOUT;
  170. return(ret);
  171. }
  172. // in the case of a read command to the ELK we read back the adress for consistency check
  173. // and the Data of course ;)
  174. ElkReadAdress = inw(ELK_ADR); // get the address
  175. ElkReadData = inw(ELK_DATA); // read on data and increase Tailptr
  176. // printf("Problem in ELKQueue, expected Adress %x, Read Address %x \n",Adress,ElkReadAdress);
  177. if (ElkReadAdress!=Adress) { // did we get the same adress back ?
  178. #if (DEBUGLEVEL > 0)
  179. printf("Problem in ELKQueue, expected Adress %x, Read Address %x \n",Adress,ElkReadAdress);
  180. #endif
  181. }
  182. ElkQStatus = inw(ELK_TODO); // let see if it worked
  183. ElkQReadyNew = ElkQStatus & 0x000f;
  184. ElkQToDo = ElkQStatus>>8;
  185. #if (DEBUGLEVEL > 0)
  186. printf("QStat %04x QReady %x QReadyNew %x\n",ElkQStatus,ElkQReady,ElkQReadyNew);
  187. #endif
  188. if (((ElkQReady-1) % ELK_QSIZE)!=ElkQReadyNew ) { // we have a problem with the Q
  189. #if (DEBUGLEVEL > 0)
  190. printf("Problem in ELKQueue, QReady %x QReadyNew %x\n",ElkQReady,ElkQReadyNew);
  191. #endif
  192. }
  193. return(ElkReadData);
  194. } // end elkReadData(uint16_t Adress, uint16_t Data)
  195. //======================================================================================================
  196. #endif
  197. #ifdef RUNONARM
  198. int elkWriteData(uint16_t Adress, uint16_t Data)
  199. {
  200. return (ioctl(fd, SERBUS_IOCTWRITEWORD, (unsigned long)(Adress) + (unsigned long)(Data<<16)));
  201. }
  202. int elkReadData(uint16_t Adress)
  203. {
  204. return (ioctl(fd, SERBUS_IOCHREADWORD, (unsigned long)(Adress)));
  205. }
  206. #endif