Points&Forces (core)
Software tools facilitating the task of surveying architecture
parallel_write.cxx
Go to the documentation of this file.
1 /*
2  Copyright 2008-2009 Pierre SMARS (smars@yuntech.edu.tw)
3  This program is free software: you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation, either version 2 of the License, or
6  (at your option) any later version.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License
14  along with this program. If not, see <http://www.gnu.org/licenses/>.
15  */
16 #include <dev/ppbus/ppi.h>
17 #include <dev/ppbus/ppbconf.h>
18 #include <fcntl.h>
19 #include <iostream>
20 #include <string>
21 #include <sstream>
22 #include <unistd.h>
23 #include "SimpleOpt.h"
24 
26 
27 CSimpleOpt::SOption g_rgOptions[] = {
28  { OPT_HELP,_T("-?"), SO_NONE },
29  { OPT_HELP,_T("-h"), SO_NONE },
30  { OPT_HELP,_T("--help"), SO_NONE },
31  { OPT_INIT,_T("-i"), SO_NONE },
32  { OPT_INIT,_T("--init"), SO_NONE },
33  { OPT_FREQ,_T("-f"), SO_REQ_SEP },
34  { OPT_FREQ,_T("--frequency"), SO_REQ_SEP },
36 };
37 
38 //---------------------------------------------------------------------------
39 int error(int val)
40 {
41  std::cerr << "parallel_write:" << std::endl;
42  std::cerr << " set the data on the parallel port" << std::endl;
43  std::cerr << "syntax: parallel_write [-i|--init] [-f val|--frequency val] [-?|-h|--help] [bits|seq] " << std::endl;
44  std::cerr << " bits: data bits of the parallel port" << std::endl;
45  std::cerr << " examples: parallel_write 1101" << std::endl;
46  std::cerr << " sets data1, data3 and data4 to '1'" << std::endl;
47  std::cerr << " parallel_write 5 0 10" << std::endl;
48  std::cerr << " sets data1 (5 times) and data3 (3 times) high then low" << std::endl;
49  std::cerr << " keeps other bits unchanged" << std::endl;
50  std::cerr << " --init: put the INIT bit 'low' then 'high'" << std::endl;
51  std::cerr << " --frequency val: sets the frequency of the most frequent bit change" << std::endl;
52  std::cerr << " default (and max value): 500Hz" << std::endl;
53  std::cerr << "author: P.Smars, 2008-9" << std::endl;
54  std::cerr << "version: 2009-05-12" << std::endl;
55  return val;
56 }
57 //---------------------------------------------------------------------------
58 int fd;
59 int val;
60 //---------------------------------------------------------------------------
61 void send_init()
62 {
63  ioctl(fd, PPIGCTRL, &val);
64  val &= ~nINIT;
65  ioctl(fd, PPISCTRL, &val);
66  val |= nINIT;
67  ioctl(fd, PPISCTRL, &val);
68 }
69 //---------------------------------------------------------------------------
71 {
72  int stat;
73  ioctl(fd, PPIGCTRL, &stat);
74  stat |= STROBE;
75  ioctl(fd, PPISCTRL, &stat);
76  stat &= ~STROBE;
77  ioctl(fd, PPISCTRL, &stat);
78 }
79 //---------------------------------------------------------------------------
80 int main( int argc, char *argv[] )
81 {
82  CSimpleOpt args(argc, argv, g_rgOptions);
83  bool do_init = false;
84  int wtime = 1000;
85  while (args.Next())
86  {
87  if (args.LastError() == SO_SUCCESS)
88  {
89  if (args.OptionId() == OPT_HELP)
90  return error(0);
91  else if (args.OptionId() == OPT_INIT)
92  do_init = true;
93  else if (args.OptionId() == OPT_FREQ)
94  {
95  std::ostringstream o;
96  o << args.OptionArg();
97  std::istringstream in(o.str().c_str());
98  double freq;
99  in >> freq;
100  if ((freq >= 0.00001)&&(freq <= 5000))
101  wtime = (1/freq)*1000000/2;
102  }
103 
104  else
105  return error(-1);
106  } else {
107  std::cerr << "Invalid argument: " << args.OptionText() << std::endl;
108  return error(args.LastError());
109  }
110  }
111  if ((args.FileCount() < 1)&&(!do_init)) return error(-2);
112  fd = open ("/dev/ppi0", O_RDWR);
113  if (do_init)
114  send_init();
115  if (args.FileCount() == 1)
116  {
117  val = 0;
118  int ex = 1;
119  std::string binary = args.File(0);
120  for (int i=binary.size()-1; i>-1; i--)
121  {
122  char digit = binary[i];
123  if (digit == '1')
124  val += ex;
125  else if (digit != '0')
126  return error(-3);
127  ex *= 2;
128  if (ex==512)
129  return error(-4);
130  }
131  ioctl (fd, PPISDATA, &val);
132  ioctl(fd, PPIGCTRL, &val);
133  val |= STROBE;
134  ioctl(fd, PPISCTRL, &val);
135  val &= ~STROBE;
136  ioctl(fd, PPISCTRL, &val);
137  }
138  else if (args.FileCount() > 8)
139  return error(-3);
140  else if (args.FileCount() > 1)
141  {
142  int val0 = 0;
143  unsigned int num[] = {0,0,0,0,0,0,0,0};
144  int num0[] = {-1,-1,-1,-1,-1,-1,-1,-1};
145  int nmax = 0;
146  for (int i=0; i<args.FileCount(); i++)
147  {
148  std::string ns = args.File(i);
149  std::istringstream in(ns.c_str());
150  in >> num[i];
151  if (num[i]>nmax)
152  nmax = num[i];
153  }
154  ioctl (fd, PPIGDATA, &val0);
155  for (int i=1; i<=nmax; i++)
156  {
157  val = 0;
158  int ex = 1;
159  for (int j=0; j<8; j++)
160  {
161  int bit;
162  if (num[j] != 0)
163  {
164  int t = (int)(i*num[j]/(double)(nmax)+.5);
165  if ((t>0)&&(t != num0[j]))
166  {
167  num0[j] = t;
168  val += ex;
169  }
170  }
171  ex *= 2;
172  }
173  val0 |= val;
174  ioctl (fd, PPISDATA, &val0);
175  send_strobe();
176  usleep(wtime);
177  val0 &= ~val;
178  ioctl (fd, PPISDATA, &val0);
179  send_strobe();
180  usleep(wtime);
181  }
182  }
183  close(fd);
184 }
A cross-platform command line library which can parse almost any of the standard command line formats...
#define SO_END_OF_OPTIONS
this option definition must be the last entry in the table
Definition: SimpleOpt.h:307
#define CSimpleOpt
TCHAR version dependent on if _UNICODE is defined.
Definition: SimpleOpt.h:1048
#define _T(s)
Definition: SimpleOpt.h:197
@ SO_SUCCESS
No error.
Definition: SimpleOpt.h:204
@ SO_REQ_SEP
Definition: SimpleOpt.h:290
@ SO_NONE
Definition: SimpleOpt.h:286
void binary(int h)
int main(int argc, char *argv[])
int error(int val)
CSimpleOpt::SOption g_rgOptions[]
void send_init()
void send_strobe()
int fd
@ OPT_INIT
@ OPT_HELP
@ OPT_FREQ
int val