added support for stm32 hardware + some c++ cosmetic

This commit is contained in:
2018-11-03 11:50:23 +01:00
parent 4e4d558910
commit 1f60eb5d12
2 changed files with 220 additions and 116 deletions
+121 -20
View File
@@ -1,5 +1,7 @@
#include "hardwaresetup.h" #include "hardwaresetup.h"
#include "sys/io.h" #include "sys/io.h"
// filecontrol
#include <fcntl.h>
#include <iostream> #include <iostream>
#include <stdint.h> #include <stdint.h>
#include <stdio.h> #include <stdio.h>
@@ -11,6 +13,13 @@
#define USB_LED_OFF 0 #define USB_LED_OFF 0
#define USB_DATA_OUT 2 #define USB_DATA_OUT 2
#define HARDWARE_VERSION 3
// V1 Parallelport
// V2 USB Atmega8
// V3 STM32F07
#define PORT_PATH "/dev/ttyACM0"
using namespace std; using namespace std;
// not needed anymore -> usb // not needed anymore -> usb
// #define BASEPORT 0xe050 /* lp1 */ // #define BASEPORT 0xe050 /* lp1 */
@@ -22,7 +31,8 @@ HardwareSetup::HardwareSetup() {
// //perror("ioperm"); // //perror("ioperm");
// } // }
this->stop = 0; this->stop = 0;
this->handle = NULL; this->handle = nullptr;
this->fd = 0;
} }
void HardwareSetup::setStop() { void HardwareSetup::setStop() {
this->stop = 1; this->stop = 1;
@@ -32,13 +42,26 @@ HardwareSetup::~HardwareSetup() {
/*if (ioperm(BASEPORT, 3, 0)) { /*if (ioperm(BASEPORT, 3, 0)) {
//perror("ioperm"); //perror("ioperm");
}*/ }*/
std::cout << "Hardware beendet" << std::endl; std::cout << "Hardware beendet" << std::endl;
this->stop = 1; this->stop = 1;
if (HARDWARE_VERSION == 2) {
if (this->handle) { if (this->handle) {
usb_close(this->handle); usb_close(this->handle);
} }
this->handle = NULL; this->handle = NULL;
} }
else if (HARDWARE_VERSION == 3) {
// close filedescriptor
if (this->fd < 0) {
// already closed
}
else {
close(this->fd);
}
}
}
/* Used to get descriptor strings for device identification */ /* Used to get descriptor strings for device identification */
int HardwareSetup::usbGetDescriptorString(usb_dev_handle * dev, int index, int HardwareSetup::usbGetDescriptorString(usb_dev_handle * dev, int index,
@@ -137,11 +160,14 @@ usb_dev_handle * HardwareSetup::usbOpenDevice(int vendor, char * vendorName,
void HardwareSetup::run() { void HardwareSetup::run() {
struct TransStruct buffer[6];
if (HARDWARE_VERSION == 2) {
int testmode = 0; int testmode = 0;
int nBytes = 0; int nBytes = 0;
// char buffer[256]; // char buffer[256];
struct TransStruct buffer[6];
// struct TransStruct *buffer = buffer1; // struct TransStruct *buffer = buffer1;
// cout << "thread started" << endl; // cout << "thread started" << endl;
@@ -212,51 +238,60 @@ void HardwareSetup::run() {
nBytes = usb_control_msg( nBytes = usb_control_msg(
this->handle, this->handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000); USB_DATA_OUT, 0, 0,
static_cast<char *>(static_cast<void *>(buffer)),
sizeof(buffer), 5000);
for (int i = 0; i < 6; i++) { for (int i = 0; i < 6; i++) {
if (buffer[i].update != 0) { if (buffer[i].update != 0) {
switch (buffer[i].id) { switch (buffer[i].id) {
case 0: case 0:
cout << "Shell Zeit 1: " << (int)buffer[i].time << endl; cout << "Shell Zeit 1: "
emit Shell((int)buffer[i].time, 1); << static_cast<int>(buffer[i].time) << endl;
emit Shell(static_cast<int>(buffer[i].time), 1);
break; break;
case 1: case 1:
cout << "Dea Zeit 1: " << (int)buffer[i].time << endl; cout << "Dea Zeit 1: "
emit Dea((int)buffer[i].time, 1); << static_cast<int>(buffer[i].time) << endl;
emit Dea(static_cast<int>(buffer[i].time), 1);
break; break;
case 2: case 2:
cout << "Shell Zeit 2: " << (int)buffer[i].time << endl; cout << "Shell Zeit 2: "
emit Shell((int)buffer[i].time, 2); << static_cast<int>(buffer[i].time) << endl;
emit Shell(static_cast<int>(buffer[i].time), 2);
break; break;
case 3: case 3:
cout << "Dea Zeit 2: " << (int)buffer[i].time << endl; cout << "Dea Zeit 2: "
emit Dea((int)buffer[i].time, 2); << static_cast<int>(buffer[i].time) << endl;
emit Dea(static_cast<int>(buffer[i].time), 2);
break; break;
case 4: case 4:
cout << "Shell Zeit 2: " << (int)buffer[i].time << endl; cout << "Shell Zeit 2: "
emit Shell((int)buffer[i].time, 3); << static_cast<int>(buffer[i].time) << endl;
emit Shell(static_cast<int>(buffer[i].time), 3);
break; break;
case 5: case 5:
cout << "Dea Zeit 3: " << (int)buffer[i].time << endl; cout << "Dea Zeit 3: "
emit Dea((int)buffer[i].time, 3); << static_cast<int>(buffer[i].time) << endl;
emit Dea(static_cast<int>(buffer[i].time), 3);
break; break;
} }
} }
} }
if (buffer[0].update != 0) { if (buffer[0].update != 0) {
cout << "Got " << nBytes << " bytes: " << (int)buffer[2].time cout << "Got " << nBytes
<< ", " << (int)buffer[2].id << endl; << " bytes: " << static_cast<int>(buffer[2].time)
<< ", " << static_cast<int>(buffer[2].id) << endl;
} }
//} //}
if (nBytes < 0) { if (nBytes < 0) {
fprintf(stderr, "USB error: %sn", usb_strerror()); fprintf(stderr, "USB error: %sn", usb_strerror());
while (!this->handle) { while (!this->handle) {
this->handle = this->handle = usbOpenDevice(0x16C0, "test01", 0x05DC,
usbOpenDevice(0x16C0, "test01", 0x05DC, "USBExample"); "USBExample");
if (this->handle == NULL) { if (this->handle == nullptr) {
fprintf(stderr, "Could not find USB device!\n"); fprintf(stderr, "Could not find USB device!\n");
} }
sleep(1); sleep(1);
@@ -264,6 +299,72 @@ void HardwareSetup::run() {
} }
} }
} }
} // end HARDWARE_VERSION 2
else if (HARDWARE_VERSION == 3) {
fd = open(PORT_PATH, O_RDONLY);
while (this->fd < 0) {
// wait 1 second
sleep(1);
cout << "Port can't be opened" << endl;
fd = open(PORT_PATH, O_RDONLY);
}
cout << "Port opened" << endl;
// port opened
// prepare buffer
struct TransStruct * received;
uint8_t uintBuf[4 * 6];
void * voidBuf;
while (!this->stop) {
// read data
read(this->fd, uintBuf, 4 * 6);
voidBuf = static_cast<void *>(uintBuf);
received = static_cast<struct TransStruct *>(voidBuf);
usleep(150000); // 150ms
for (int i = 0; i < 6; i++) {
if (received[i].update != 0) {
switch (received[i].id) {
case 0:
cout << "Shell Zeit 1: "
<< static_cast<int>(received[i].time) << endl;
emit Shell(static_cast<int>(received[i].time), 1);
break;
case 1:
cout << "Dea Zeit 1: "
<< static_cast<int>(received[i].time) << endl;
emit Dea(static_cast<int>(received[i].time), 1);
break;
case 2:
cout << "Shell Zeit 2: "
<< static_cast<int>(received[i].time) << endl;
emit Shell(static_cast<int>(received[i].time), 2);
break;
case 3:
cout << "Dea Zeit 2: "
<< static_cast<int>(received[i].time) << endl;
emit Dea(static_cast<int>(received[i].time), 2);
break;
case 4:
cout << "Shell Zeit 2: "
<< static_cast<int>(received[i].time) << endl;
emit Shell(static_cast<int>(received[i].time), 3);
break;
case 5:
cout << "Dea Zeit 3: "
<< static_cast<int>(received[i].time) << endl;
emit Dea(static_cast<int>(received[i].time), 3);
break;
}
}
}
}
}
else {
cout << "unknown hardware version" << endl;
}
} }
bool HardwareSetup::getShell() { bool HardwareSetup::getShell() {
+9 -6
View File
@@ -3,9 +3,8 @@
#include <QtCore> #include <QtCore>
#include <stdint.h> #include <stdint.h>
#include <string.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <string.h>
#include <usb.h> #include <usb.h>
struct TransStruct { struct TransStruct {
@@ -14,22 +13,26 @@ struct TransStruct{
uint8_t update; uint8_t update;
}; };
class HardwareSetup : public QThread class HardwareSetup : public QThread {
{
Q_OBJECT Q_OBJECT
protected: protected:
void run(); void run();
private: private:
bool shellBefore; bool shellBefore;
bool deaBefore; bool deaBefore;
usb_dev_handle * handle; usb_dev_handle * handle;
int fd;
bool getShell(); bool getShell();
bool getDea(); bool getDea();
int * findBit(int * array, int zahl); int * findBit(int * array, int zahl);
bool stop; bool stop;
int usbGetDescriptorString(usb_dev_handle *dev, int index, int langid, char *buf, int buflen); int usbGetDescriptorString(usb_dev_handle * dev, int index, int langid,
usb_dev_handle *usbOpenDevice(int vendor, char *vendorName, int product, char *productName); char * buf, int buflen);
usb_dev_handle * usbOpenDevice(int vendor, char * vendorName, int product,
char * productName);
public: public:
void setStop(); void setStop();
~HardwareSetup(); ~HardwareSetup();