Nife version Beta
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

341 lines
8.5 KiB

  1. /* Copyright (C) 2011-2022 Patrick H. E. Foubet - S.E.R.I.A.N.E.
  2. This program is free software: you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License as published by
  4. the Free Software Foundation, either version 3 of the License, or any
  5. later version.
  6. This program is distributed in the hope that it will be useful,
  7. but WITHOUT ANY WARRANTY; without even the implied warranty of
  8. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  9. GNU General Public License for more details.
  10. You should have received a copy of the GNU General Public License
  11. along with this program. If not, see <http://www.gnu.org/licenses/>
  12. *******************************************************************/
  13. /* dev.c */
  14. #ifdef HAVE_CONFIG_H
  15. #include "../config.h"
  16. #endif
  17. #ifdef HAVE_COMEDILIB_H
  18. #define _WITH_COMEDILIB
  19. #endif
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include "debug.h"
  23. static int DFL_did=0, DFL_sid=0, DFL_cid=0;
  24. #ifdef _WITH_COMEDILIB
  25. #include <comedilib.h>
  26. static char *subdev_types[]={
  27. "unused",
  28. "analog input",
  29. "analog output",
  30. "digital input",
  31. "digital output",
  32. "digital I/O",
  33. "counter",
  34. "timer",
  35. "memory",
  36. "calibration",
  37. "processor",
  38. "serial digital I/O"
  39. };
  40. static comedi_t *it;
  41. static long n_it = -1;
  42. static char *cmd_src(int src,char *buf)
  43. {
  44. buf[0]=0;
  45. if(src&TRIG_NONE)strcat(buf,"none|");
  46. if(src&TRIG_NOW)strcat(buf,"now|");
  47. if(src&TRIG_FOLLOW)strcat(buf, "follow|");
  48. if(src&TRIG_TIME)strcat(buf, "time|");
  49. if(src&TRIG_TIMER)strcat(buf, "timer|");
  50. if(src&TRIG_COUNT)strcat(buf, "count|");
  51. if(src&TRIG_EXT)strcat(buf, "ext|");
  52. if(src&TRIG_INT)strcat(buf, "int|");
  53. #ifdef TRIG_OTHER
  54. if(src&TRIG_OTHER)strcat(buf, "other|");
  55. #endif
  56. if(strlen(buf)==0){
  57. sprintf(buf,"unknown(0x%08x)",src);
  58. }else{
  59. buf[strlen(buf)-1]=0;
  60. }
  61. return buf;
  62. }
  63. static void probe_max_1chan(comedi_t *it,int s)
  64. {
  65. comedi_cmd cmd;
  66. char buf[100];
  67. printf(" command fast 1chan:\n");
  68. if(comedi_get_cmd_generic_timed(it, s, &cmd, 1, 1)<0){
  69. printf(" not supported\n");
  70. }else{
  71. printf(" start: %s %d\n",
  72. cmd_src(cmd.start_src,buf),cmd.start_arg);
  73. printf(" scan_begin: %s %d\n",
  74. cmd_src(cmd.scan_begin_src,buf),cmd.scan_begin_arg);
  75. printf(" convert: %s %d\n",
  76. cmd_src(cmd.convert_src,buf),cmd.convert_arg);
  77. printf(" scan_end: %s %d\n",
  78. cmd_src(cmd.scan_end_src,buf),cmd.scan_end_arg);
  79. printf(" stop: %s %d\n",
  80. cmd_src(cmd.stop_src,buf),cmd.stop_arg);
  81. }
  82. }
  83. static void get_command_stuff(comedi_t *it,int s)
  84. {
  85. comedi_cmd cmd;
  86. char buf[100];
  87. if(comedi_get_cmd_src_mask(it,s,&cmd)<0){
  88. printf("not supported\n");
  89. }else{
  90. printf("\n");
  91. printf(" start: %s\n",cmd_src(cmd.start_src,buf));
  92. printf(" scan_begin: %s\n",cmd_src(cmd.scan_begin_src,buf));
  93. printf(" convert: %s\n",cmd_src(cmd.convert_src,buf));
  94. printf(" scan_end: %s\n",cmd_src(cmd.scan_end_src,buf));
  95. printf(" stop: %s\n",cmd_src(cmd.stop_src,buf));
  96. probe_max_1chan(it,s);
  97. }
  98. }
  99. static int devOpen(long n)
  100. {
  101. char filename[24];
  102. if (n == n_it) return 1; /* deja fait */
  103. n_it=-1;
  104. sprintf(filename,"/dev/comedi%ld",n);
  105. if (! (it = comedi_open(filename))) {
  106. printf("%s : Error access !\n",filename);
  107. return 0;
  108. }
  109. n_it = n;
  110. return 1;
  111. }
  112. #endif /* for _WITH_COMEDILIB */
  113. /* *********************************************************************/
  114. #include "stackN.h"
  115. #include "dev.h"
  116. #include "err.h"
  117. #define LBUFD 200
  118. static char buf[LBUFD];
  119. void IF_listDev (void)
  120. {
  121. FILE * fd;
  122. int id,ns;
  123. char driver[24], nomdev[24];
  124. if ((fd = fopen("/proc/comedi","r")) == NULL)
  125. printf("No devices !\n");
  126. else {
  127. while (fgets(buf,LBUFD,fd) != NULL) {
  128. ns = -1;
  129. sscanf(buf,"%d: %s %s %d\n",&id,driver,nomdev,&ns);
  130. if (ns != -1)
  131. printf(" %2d : %-20s driver %-20s %4d subdevices\n",id,nomdev,driver,ns);
  132. }
  133. }
  134. }
  135. void IFD_listDev (void)
  136. {
  137. _IFD_BEGIN_
  138. IF_listDev();
  139. _IFD_END_
  140. }
  141. #ifdef _WITH_COMEDILIB
  142. static void show_subdev(int i, int V)
  143. {
  144. int j, type, chan,n_chans, n_ranges, subdev_flags;
  145. comedi_range *rng;
  146. printf("subdevice %d :",i);
  147. type = comedi_get_subdevice_type(it, i);
  148. printf(" type %d (%s) ",type,subdev_types[type]);
  149. if (type==COMEDI_SUBD_UNUSED) return;
  150. subdev_flags = comedi_get_subdevice_flags(it, i);
  151. printf("- flags 0x%08x\n",subdev_flags);
  152. n_chans=comedi_get_n_channels(it,i);
  153. printf(" nb of channels : %d",n_chans);
  154. if(!comedi_maxdata_is_chan_specific(it,i)){
  155. printf(" - max data value = %lu\n",(unsigned long)comedi_get_maxdata(it,i,0));
  156. } else {
  157. printf("\n max data value: (channel specific)\n");
  158. for(chan=0;chan<n_chans;chan++){
  159. printf(" chan%d: %lu\n",chan,
  160. (unsigned long)comedi_get_maxdata(it,i,chan));
  161. }
  162. }
  163. if (V >= n_chans) {
  164. printf("Error : CId must be < %d !!\n", n_chans);
  165. return;
  166. }
  167. printf(" ranges (Volts) :");
  168. if(!comedi_range_is_chan_specific(it,i)){
  169. n_ranges=comedi_get_n_ranges(it,i,0);
  170. printf(" all chans:");
  171. for(j=0;j<n_ranges;j++){
  172. rng=comedi_get_range(it,i,0,j);
  173. printf(" [%g,%g]",rng->min,rng->max);
  174. }
  175. printf("\n");
  176. } else {
  177. printf("\n");
  178. for (chan=0;chan<n_chans;chan++){
  179. n_ranges=comedi_get_n_ranges(it,i,chan);
  180. printf(" chan%d:",chan);
  181. for (j=0;j<n_ranges;j++){
  182. rng=comedi_get_range(it,i,chan,j);
  183. printf(" [%g,%g]",rng->min,rng->max);
  184. }
  185. printf("\n");
  186. }
  187. }
  188. printf(" command : ");
  189. get_command_stuff(it,i);
  190. }
  191. #endif
  192. void IF_showDev (void)
  193. {
  194. long n;
  195. if (!getParLong(&n)) return;
  196. #ifdef _WITH_COMEDILIB
  197. int i, n_subdev;
  198. if (! devOpen(n)) return;
  199. printf("overall info:\n");
  200. printf(" version code : 0x%06x\n", comedi_get_version_code(it));
  201. printf(" driver name : %s\n", comedi_get_driver_name(it));
  202. printf(" board name : %s\n", comedi_get_board_name(it));
  203. printf(" nb subdevices : %d\n", n_subdev = comedi_get_n_subdevices(it));
  204. for (i = 0; i < n_subdev; i++) {
  205. show_subdev(i,0);
  206. }
  207. #else
  208. messErr(17);
  209. #endif
  210. }
  211. void IFD_showDev (void)
  212. {
  213. _IFD_BEGIN_
  214. IF_showDev();
  215. _IFD_END_
  216. }
  217. void IF_devShowDflt (void)
  218. {
  219. #ifdef _WITH_COMEDILIB
  220. int n_subdev;
  221. printf("Default : DId=%d, SId=%d CId=%d\n", DFL_did, DFL_sid, DFL_cid);
  222. if (! devOpen(DFL_did)) return;
  223. printf("overall info:\n");
  224. printf(" version code : 0x%06x\n", comedi_get_version_code(it));
  225. printf(" driver name : %s\n", comedi_get_driver_name(it));
  226. printf(" board name : %s\n", comedi_get_board_name(it));
  227. printf(" nb subdevices : %d\n", n_subdev = comedi_get_n_subdevices(it));
  228. if (DFL_sid < n_subdev)
  229. show_subdev(DFL_sid,DFL_cid);
  230. else printf("Error : SId must be < %d !!\n", n_subdev);
  231. #else
  232. messErr(17);
  233. #endif
  234. }
  235. void IFD_devShowDflt (void)
  236. {
  237. _IFD_BEGIN_
  238. IF_devShowDflt();
  239. _IFD_END_
  240. }
  241. static void dev_read(int did, int sid, int cid)
  242. {
  243. #ifdef _WITH_COMEDILIB
  244. lsampl_t data;
  245. if (! devOpen(did)) return;
  246. if (comedi_data_read(it,sid,cid,0,0,&data) < 0)
  247. messErr(18);
  248. else putLong((long)data);
  249. #else
  250. messErr(17);
  251. #endif
  252. }
  253. void IF_devDflR (void)
  254. {
  255. dev_read(DFL_did, DFL_sid, DFL_cid);
  256. }
  257. void IF_devRead (void)
  258. {
  259. long n, sd, ch;
  260. if (!getParLong(&ch)) return;
  261. if (!getParLong(&sd)) return;
  262. if (!getParLong(&n)) return;
  263. dev_read(n, sd, ch);
  264. }
  265. void IF_devDflt (void)
  266. {
  267. long n, sd, ch;
  268. if (!getParLong(&ch)) return;
  269. if (!getParLong(&sd)) return;
  270. if (!getParLong(&n)) return;
  271. DFL_did=n;
  272. DFL_sid=sd;
  273. DFL_cid=ch;
  274. }
  275. static void dev_write(int did, int sid, int cid, long d)
  276. {
  277. #ifdef _WITH_COMEDILIB
  278. lsampl_t data;
  279. if (! devOpen(did)) return;
  280. data = (lsampl_t)d;
  281. if (comedi_data_write(it,sid,cid,0,0,data) < 0)
  282. messErr(18);
  283. #else
  284. messErr(17);
  285. #endif
  286. }
  287. void IF_devWrite (void)
  288. {
  289. long d, n, sd, ch;
  290. if (!getParLong(&ch)) return;
  291. if (!getParLong(&sd)) return;
  292. if (!getParLong(&n)) return;
  293. if (!getParLong(&d)) return;
  294. dev_write(n, sd, ch, d);
  295. return;
  296. }
  297. void IF_devDflW (void)
  298. {
  299. long d;
  300. if (!getParLong(&d)) return;
  301. dev_write(DFL_did, DFL_sid, DFL_cid, d);
  302. return;
  303. }