1 #include "mainwindow.h"
2 #include "ui_mainwindow.h"
3 #include <fcntl.h>
4 #include <sys/ioctl.h>
5 #include <linux/types.h>
6 #include <linux/spi/spidev.h>
7 #include <QMessageBox>
8 #include <unistd.h>
9
MainWindow(QWidget * parent)10 MainWindow::MainWindow(QWidget *parent) :
11 QMainWindow(parent),
12 ui(new Ui::MainWindow)
13 {
14
15 ui->setupUi(this);
16
17 }
18
update_params()19 void MainWindow::update_params()
20 {
21 unsigned char mode = 0,bits;
22 unsigned int speed;
23 unsigned short delay;
24 int ret;
25 speed = ui->speed->text().toInt();
26 delay = ui->delay->text().toInt();
27 bits = ui->bits->text().toInt();
28 if(ui->loop->isChecked())
29 mode |= SPI_LOOP;
30
31 if(ui->clockphase->isChecked())
32 mode |= SPI_CPHA;
33
34 if(ui->clockpolarity->isChecked())
35 mode |= SPI_CPOL;
36
37 if(ui->lsb->isChecked())
38 mode |= SPI_LSB_FIRST;
39
40 if(ui->cshigh->isChecked())
41 mode |= SPI_CS_HIGH;
42
43 if(ui->wire->isChecked())
44 mode |= SPI_3WIRE;
45
46 if(ui->nocs->isChecked())
47 mode |= SPI_NO_CS;
48
49 if(ui->ready->isChecked())
50 mode |= SPI_READY;
51
52 ret = ::ioctl(fd,SPI_IOC_WR_MODE,&mode);
53 if(ret < 0)
54 {
55 QMessageBox::about(this,"error","SPI_IOC_WR_MODE failure");
56 exit(0);
57 }
58
59 ret = ::ioctl(fd,SPI_IOC_RD_MODE,&mode);
60 if(ret < 0)
61 {
62 QMessageBox::about(this,"error","SPI_IOC_RD_MODE failure");
63 exit(0);
64 }
65
66
67 ret = ::ioctl(fd,SPI_IOC_WR_BITS_PER_WORD,&bits);
68 if(ret < 0)
69 {
70 QMessageBox::about(this,"error","SPI_IOC_WR_BITS_PER_WORD failure");
71 exit(0);
72 }
73
74 ret = ::ioctl(fd,SPI_IOC_RD_BITS_PER_WORD,&bits);
75 if(ret < 0)
76 {
77 QMessageBox::about(this,"error","SPI_IOC_RD_BITS_PER_WORD failure");
78 exit(0);
79 }
80
81 ret = ::ioctl(fd,SPI_IOC_WR_MAX_SPEED_HZ,&speed);
82 if(ret < 0)
83 {
84 QMessageBox::about(this,"error","SPI_IOC_RD_MAX_SPEED_HZ failure");
85 exit(0);
86 }
87 ret = ::ioctl(fd,SPI_IOC_RD_MAX_SPEED_HZ,&speed);
88 if(ret < 0)
89 {
90 QMessageBox::about(this,"error","SPI_IOC_RD_MAX_SPEED_HZ failure");
91 exit(0);
92 }
93 fprintf(stderr,"mode %d\n", mode);
94 fprintf(stderr, "speed %d delay %d bits %d\n",speed, delay, bits);
95 }
96
~MainWindow()97 MainWindow::~MainWindow()
98 {
99 delete ui;
100 ::close(fd);
101 }
102
103 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
on_send_clicked()104 void MainWindow::on_send_clicked()
105 {
106 struct spi_ioc_transfer tr;
107 memset(&tr, 0, sizeof(tr));
108 std::string str = ui->editsend->text().toStdString();
109 char buf[str.length()+1];
110 memset(buf,0,str.length()+1);
111
112 tr.tx_buf = (__u64)str.c_str();
113 tr.rx_buf = (__u64)buf;
114 tr.len = str.length();
115 tr.delay_usecs = ui->delay->text().toInt();
116 tr.speed_hz = ui->speed->text().toInt();
117 tr.bits_per_word = ui->bits->text().toInt();
118
119 ::ioctl(fd,SPI_IOC_MESSAGE(1),&tr);
120
121 ui->editrecv->append(QString(buf));
122 }
123
on_loop_clicked(bool checked)124 void MainWindow::on_loop_clicked(bool checked)
125 {
126 Q_UNUSED(checked)
127 update_params();
128 }
129
on_clockphase_clicked(bool checked)130 void MainWindow::on_clockphase_clicked(bool checked)
131 {
132 Q_UNUSED(checked)
133 update_params();
134 }
135
on_clockpolarity_clicked(bool checked)136 void MainWindow::on_clockpolarity_clicked(bool checked)
137 {
138 Q_UNUSED(checked)
139 update_params();
140 }
141
on_lsb_clicked(bool checked)142 void MainWindow::on_lsb_clicked(bool checked)
143 {
144 Q_UNUSED(checked)
145 update_params();
146 }
147
on_cshigh_clicked(bool checked)148 void MainWindow::on_cshigh_clicked(bool checked)
149 {
150 Q_UNUSED(checked)
151 update_params();
152 }
153
on_wire_clicked(bool checked)154 void MainWindow::on_wire_clicked(bool checked)
155 {
156 Q_UNUSED(checked)
157 update_params();
158 }
159
on_nocs_clicked(bool checked)160 void MainWindow::on_nocs_clicked(bool checked)
161 {
162 Q_UNUSED(checked)
163 update_params();
164 }
165
on_ready_clicked(bool checked)166 void MainWindow::on_ready_clicked(bool checked)
167 {
168 Q_UNUSED(checked)
169 update_params();
170 }
171
on_speed_textChanged(const QString & arg1)172 void MainWindow::on_speed_textChanged(const QString &arg1)
173 {
174 Q_UNUSED(arg1)
175 update_params();
176 }
177
on_delay_textChanged(const QString & arg1)178 void MainWindow::on_delay_textChanged(const QString &arg1)
179 {
180 Q_UNUSED(arg1)
181 update_params();
182 }
183
on_bits_textChanged(const QString & arg1)184 void MainWindow::on_bits_textChanged(const QString &arg1)
185 {
186 Q_UNUSED(arg1)
187 update_params();
188 }
189
moveEvent(QMoveEvent *)190 void MainWindow::moveEvent(QMoveEvent *)
191 {
192 this->move(QPoint(0,0));
193 }
194
resizeEvent(QResizeEvent *)195 void MainWindow::resizeEvent(QResizeEvent *)
196 {
197 this->showMaximized();
198 }
199
closeEvent(QCloseEvent *)200 void MainWindow::closeEvent(QCloseEvent *)
201 {
202 exit(0);
203 }
204
205
on_openBtn_clicked()206 void MainWindow::on_openBtn_clicked()
207 {
208 if(fd >=0)
209 {
210 QMessageBox::about(this,"info","open spidev already open");
211 return;
212 }
213 fd = ::open("/dev/spidev1.0",O_RDWR);
214 if(fd < 0){
215 QMessageBox::about(this,"error","open spidev failure");
216 return;
217 }
218 update_params();
219 }
220