File: dialogsr.cpp

package info (click to toggle)
imsprog 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,828 kB
  • sloc: cpp: 6,525; ansic: 5,899; xml: 552; sh: 231; makefile: 5
file content (174 lines) | stat: -rw-r--r-- 6,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
 * Copyright (C) 2023 - 2024 Mikhail Medvedev <e-ink-reader@yandex.ru>
 *
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */
#include "dialogsr.h"
#include "ui_dialogsr.h"
#include <QValidator>
#include <QRegExp>
#include "unistd.h"
#include <QDebug>

DialogSR::DialogSR(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::DialogSR)
{
    ui->setupUi(this);
    setLineEditFilter();
    regReaded = false;
}

DialogSR::~DialogSR()
{
    delete ui;
}

void DialogSR::on_pushButton_read_clicked()
{
    //READING STATUS REGISTER
    uint8_t *buf;
    int retval;
    int stCH341 = 0;
    buf = (uint8_t *)malloc(2);
    stCH341 = ch341a_spi_init();
    if (stCH341 == 0)
        {
           SPI_CONTROLLER_Chip_Select_Low();
           if (currentChipType != 5) SPI_CONTROLLER_Write_One_Byte(0x05);
           else SPI_CONTROLLER_Write_One_Byte(0xd7);
           retval = SPI_CONTROLLER_Read_NByte(buf,1,SPI_CONTROLLER_SPEED_SINGLE);
           qDebug() << "retval=" << retval;
           SPI_CONTROLLER_Chip_Select_High();
           usleep(1);
           if (retval)
              {
                 QMessageBox::about(this, tr("Error"), tr("Error reading register!"));
                 return;
              }
            ui->lineEdit_sr07->setText(QString::number(((buf[0] & 128) >> 7)));
            ui->lineEdit_sr06->setText(QString::number(((buf[0] & 64) >> 6)));
            ui->lineEdit_sr05->setText(QString::number(((buf[0] & 32) >> 5)));
            ui->lineEdit_sr04->setText(QString::number(((buf[0] & 16) >> 4)));
            ui->lineEdit_sr03->setText(QString::number(((buf[0] & 8) >> 3)));
            ui->lineEdit_sr02->setText(QString::number(((buf[0] & 4) >> 2)));
            ui->lineEdit_sr01->setText(QString::number(((buf[0] & 2) >> 1)));
            ui->lineEdit_sr00->setText(QString::number((buf[0] & 1)));
            ch341a_spi_shutdown();
            regReaded = true;
       }
    else QMessageBox::about(this, tr("Error"), tr("Programmer CH341a is not connected!"));
}

void DialogSR::on_pushButton_write_clicked()
{
    //WRITING STATUS REGISTER
    uint8_t r0 = 0;
    int stCH341 = 0;
    stCH341 = ch341a_spi_init();
    if (stCH341 == 0)
        {
           if (regReaded)
           {
               if (QString::compare(ui->lineEdit_sr07->text(), "0", Qt::CaseInsensitive)) r0 = r0 + 128;
               if (QString::compare(ui->lineEdit_sr06->text(), "0", Qt::CaseInsensitive)) r0 = r0 +  64;
               if (QString::compare(ui->lineEdit_sr05->text(), "0", Qt::CaseInsensitive)) r0 = r0 +  32;
               if (QString::compare(ui->lineEdit_sr04->text(), "0", Qt::CaseInsensitive)) r0 = r0 +  16;
               if (QString::compare(ui->lineEdit_sr03->text(), "0", Qt::CaseInsensitive)) r0 = r0 +   8;
               if (QString::compare(ui->lineEdit_sr02->text(), "0", Qt::CaseInsensitive)) r0 = r0 +   4;
               if (QString::compare(ui->lineEdit_sr01->text(), "0", Qt::CaseInsensitive)) r0 = r0 +   2;
               if (QString::compare(ui->lineEdit_sr00->text(), "0", Qt::CaseInsensitive)) r0 = r0 +   1;
               //Writing status registers
               SPI_CONTROLLER_Chip_Select_Low();
               SPI_CONTROLLER_Write_One_Byte(0x06);
               SPI_CONTROLLER_Chip_Select_High();
               usleep(1);

               SPI_CONTROLLER_Chip_Select_Low();
               SPI_CONTROLLER_Write_One_Byte(0x01);
               SPI_CONTROLLER_Write_One_Byte(r0);
               SPI_CONTROLLER_Chip_Select_High();
               usleep(1);

               SPI_CONTROLLER_Chip_Select_Low();
               SPI_CONTROLLER_Write_One_Byte(0x04);
               SPI_CONTROLLER_Chip_Select_High();
               usleep(1);

           }
           else QMessageBox::about(this, tr("Error"), tr("Before writing the register, please press the `Read` button!"));
         }

    else QMessageBox::about(this, tr("Error"), tr("Programmer CH341a is not connected!"));
    ch341a_spi_shutdown();
}

void DialogSR::setLineEditFilter()
{
    QRegExp reHex( "[0-1]{1}" );
    QRegExpValidator *validator = new QRegExpValidator(reHex, this);
    ui->lineEdit_sr00->setValidator(validator);
    ui->lineEdit_sr01->setValidator(validator);
    ui->lineEdit_sr02->setValidator(validator);
    ui->lineEdit_sr03->setValidator(validator);
    ui->lineEdit_sr04->setValidator(validator);
    ui->lineEdit_sr05->setValidator(validator);
    ui->lineEdit_sr06->setValidator(validator);
    ui->lineEdit_sr07->setValidator(validator);
}

void DialogSR::setChipType(const uint chipType)
{
   switch (chipType)
   {

     case 3:
       ui->label_20->setText("/RDY");
       ui->label_19->setText("WEN");
       ui->label_18->setText("BP0");
       ui->label_17->setText("BP1");
       ui->label_13->setText("WPEN");
       ui->label_14->setText("X");
       ui->pushButton_write->setEnabled(true);
     break;

     case 4:
       ui->label_20->setText("WIP");
       ui->label_19->setText("WEL");
       ui->label_18->setText("BP0");
       ui->label_17->setText("BP1");
       ui->label_13->setText("SRWD");
       ui->label_14->setText("X");
       ui->pushButton_write->setEnabled(true);
     break;

     case 5:
       ui->label_20->setText("PAGE");
       ui->label_19->setText("WP");
       ui->label_18->setText("X");
       ui->label_17->setText("X");
       ui->label_13->setText("RDY");
       ui->label_14->setText("COMP");
       ui->pushButton_write->setEnabled(false);
     break;

     default:
     break;
   }
   currentChipType = chipType;
}

void DialogSR::closeEvent(QCloseEvent* event)
{
    emit closeRequestHasArrived();
    QWidget::closeEvent(event);
}