hal_fmc_interface.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. #include "driver.h"
  2. #include <linux/types.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include "com_gpio.h"
  6. #include "hal_interface_api.h"
  7. #include "linux/fcntl.h"
  8. #define DEV_NAME "/dev/fmc_cpld"
  9. int fmc_write_8bit(uint32_t address, uint8_t *buf, uint32_t len)
  10. {
  11. int fd;
  12. fmc_cpld_t fmc_arg;
  13. uint32_t remLen = len, writeCnt = 0;;
  14. fd = open(DEV_NAME, O_RDWR);
  15. if(-1 == fd)
  16. {
  17. printf("Open %s failed!\n", DEV_NAME);
  18. return -1;
  19. }
  20. while(remLen > 1024)
  21. {
  22. fmc_arg.address = address + writeCnt;
  23. fmc_arg.length = 1024;
  24. memcpy(fmc_arg.data, buf+writeCnt, 1024);
  25. ioctl(fd, WRITE_BYTE, &fmc_arg);
  26. writeCnt += 1024;
  27. remLen -= 1024;
  28. }
  29. if(remLen > 0)
  30. {
  31. fmc_arg.address = address + writeCnt;
  32. fmc_arg.length = remLen;
  33. memcpy(fmc_arg.data, buf+writeCnt, remLen);
  34. ioctl(fd, WRITE_BYTE, &fmc_arg);
  35. writeCnt += remLen;
  36. remLen = 0;
  37. }
  38. close(fd);
  39. return 0;
  40. }
  41. int fmc_read_8bit(uint32_t address, uint8_t *buf, uint32_t len)
  42. {
  43. int fd;
  44. fmc_cpld_t fmc_arg;
  45. uint32_t remLen = len, readCnt = 0;
  46. fd = open(DEV_NAME, O_RDWR);
  47. if(-1 == fd)
  48. {
  49. printf("Open %s failed!\n", DEV_NAME);
  50. return -1;
  51. }
  52. while(remLen > 1024)
  53. {
  54. fmc_arg.address = address + readCnt;
  55. fmc_arg.length = 1024;
  56. ioctl(fd, READ_BYTE, &fmc_arg);
  57. memcpy(buf+readCnt, fmc_arg.data, 1024);
  58. readCnt += 1024;
  59. remLen -= 1024;
  60. }
  61. if(remLen > 0)
  62. {
  63. fmc_arg.address = address + readCnt;
  64. fmc_arg.length = remLen;
  65. ioctl(fd, READ_BYTE, &fmc_arg);
  66. memcpy( buf+readCnt, fmc_arg.data, remLen);
  67. readCnt += remLen;
  68. remLen = 0;
  69. }
  70. close(fd);
  71. return 0;
  72. }
  73. int fmc_write_16bit(uint32_t address, uint16_t *buf, uint32_t len)
  74. {
  75. int fd;
  76. fmc_cpld_t fmc_arg;
  77. uint32_t remLen = len*sizeof(uint16_t), writeCnt = 0;;
  78. fd = open(DEV_NAME, O_RDWR);
  79. if(-1 == fd)
  80. {
  81. printf("Open %s failed!\n", DEV_NAME);
  82. return -1;
  83. }
  84. while(remLen > 1024)
  85. {
  86. fmc_arg.address = address + writeCnt;
  87. fmc_arg.length = 1024/sizeof(uint16_t);
  88. memcpy(fmc_arg.data, buf+writeCnt, 1024);
  89. ioctl(fd, WRITE_SHORT, &fmc_arg);
  90. writeCnt += 1024;
  91. remLen -= 1024;
  92. }
  93. if(remLen > 0)
  94. {
  95. fmc_arg.address = address + writeCnt;
  96. fmc_arg.length = remLen/sizeof(uint16_t);
  97. memcpy(fmc_arg.data, buf+writeCnt, remLen);
  98. ioctl(fd, WRITE_SHORT, &fmc_arg);
  99. writeCnt += remLen;
  100. remLen = 0;
  101. }
  102. close(fd);
  103. return 0;
  104. }
  105. int fmc_read_16bit(uint32_t address, uint16_t *buf, uint32_t len)
  106. {
  107. int fd;
  108. fmc_cpld_t fmc_arg;
  109. uint32_t remLen = len*sizeof(uint16_t), readCnt = 0;
  110. fd = open(DEV_NAME, O_RDWR);
  111. if(-1 == fd)
  112. {
  113. printf("Open %s failed!\n", DEV_NAME);
  114. return -1;
  115. }
  116. while(remLen > 1024)
  117. {
  118. fmc_arg.address = address + readCnt;
  119. fmc_arg.length = 1024/sizeof(uint16_t);
  120. ioctl(fd, READ_SHORT, &fmc_arg);
  121. memcpy(buf+readCnt, fmc_arg.data, 1024);
  122. readCnt += 1024;
  123. remLen -= 1024;
  124. }
  125. if(remLen > 0)
  126. {
  127. fmc_arg.address = address + readCnt;
  128. fmc_arg.length = remLen/sizeof(uint16_t);
  129. ioctl(fd, READ_SHORT, &fmc_arg);
  130. memcpy( buf+readCnt, fmc_arg.data, remLen);
  131. readCnt += remLen;
  132. remLen = 0;
  133. }
  134. close(fd);
  135. return 0;
  136. }
  137. int fmc_write_32bit(uint32_t address, uint32_t *buf, uint32_t len)
  138. {
  139. int fd;
  140. fmc_cpld_t fmc_arg;
  141. uint32_t remLen = len*sizeof(uint32_t), writeCnt = 0;;
  142. fd = open(DEV_NAME, O_RDWR);
  143. if(-1 == fd)
  144. {
  145. printf("Open %s failed!\n", DEV_NAME);
  146. return -1;
  147. }
  148. while(remLen > 1024)
  149. {
  150. fmc_arg.address = address + writeCnt;
  151. fmc_arg.length = 1024/sizeof(uint32_t);
  152. memcpy(fmc_arg.data, buf+writeCnt, 1024);
  153. ioctl(fd, WRITE_WORD, &fmc_arg);
  154. writeCnt += 1024;
  155. remLen -= 1024;
  156. }
  157. if(remLen > 0)
  158. {
  159. fmc_arg.address = address + writeCnt;
  160. fmc_arg.length = remLen/sizeof(uint32_t);
  161. memcpy(fmc_arg.data, buf+writeCnt, remLen);
  162. ioctl(fd, WRITE_WORD, &fmc_arg);
  163. writeCnt += remLen;
  164. remLen = 0;
  165. }
  166. close(fd);
  167. return 0;
  168. }
  169. int fmc_read_32bit(uint32_t address, uint32_t *buf, uint32_t len)
  170. {
  171. int fd;
  172. fmc_cpld_t fmc_arg;
  173. uint32_t remLen = len*sizeof(uint32_t), readCnt = 0;
  174. fd = open(DEV_NAME, O_RDWR);
  175. if(-1 == fd)
  176. {
  177. printf("Open %s failed!\n", DEV_NAME);
  178. return -1;
  179. }
  180. while(remLen > 1024)
  181. {
  182. fmc_arg.address = address + readCnt;
  183. fmc_arg.length = 1024/sizeof(uint32_t);
  184. ioctl(fd, READ_WORD, &fmc_arg);
  185. memcpy(buf+readCnt, fmc_arg.data, 1024);
  186. readCnt += 1024;
  187. remLen -= 1024;
  188. }
  189. if(remLen > 0)
  190. {
  191. fmc_arg.address = address + readCnt;
  192. fmc_arg.length = remLen/sizeof(uint32_t);
  193. ioctl(fd, READ_WORD, &fmc_arg);
  194. memcpy( buf+readCnt, fmc_arg.data, remLen);
  195. readCnt += remLen;
  196. remLen = 0;
  197. }
  198. close(fd);
  199. return 0;
  200. }
  201. int fmc_set_bus_width(uint8_t bus_is16bit)
  202. {
  203. int fd;
  204. fd = open(DEV_NAME, O_RDWR);
  205. if(-1 == fd)
  206. {
  207. printf("Open %s failed!\n", DEV_NAME);
  208. return -1;
  209. }
  210. if(bus_is16bit)
  211. ioctl(fd, BUS_16BIT, NULL);
  212. else
  213. ioctl(fd, BUS_8BIT, NULL);
  214. close(fd);
  215. return 0;
  216. }