From 997cbc6bf554627f81f66136b1ea6c20aa9d5022 Mon Sep 17 00:00:00 2001 From: h-4nd-h Date: Mon, 21 Dec 2020 11:09:27 +0330 Subject: [PATCH] api --- api/Buffer.h | 12 +++++ api/SonoException.h | 22 +++++++++ api/SonoPcieDevice.cpp | 106 ++++++++++++++++++++++++++++++++++++++++ api/SonoPcieDevice.h | 42 ++++++++++++++++ app/Test/.gitignore | 73 +++++++++++++++++++++++++++ app/Test/MainWindow.cpp | 15 ++++++ app/Test/MainWindow.h | 21 ++++++++ app/Test/MainWindow.ui | 22 +++++++++ app/Test/Test.pro | 31 ++++++++++++ app/Test/main.cpp | 11 +++++ 10 files changed, 355 insertions(+) create mode 100644 api/Buffer.h create mode 100644 api/SonoException.h create mode 100644 api/SonoPcieDevice.cpp create mode 100644 api/SonoPcieDevice.h create mode 100644 app/Test/.gitignore create mode 100644 app/Test/MainWindow.cpp create mode 100644 app/Test/MainWindow.h create mode 100644 app/Test/MainWindow.ui create mode 100644 app/Test/Test.pro create mode 100644 app/Test/main.cpp diff --git a/api/Buffer.h b/api/Buffer.h new file mode 100644 index 0000000..cb62f44 --- /dev/null +++ b/api/Buffer.h @@ -0,0 +1,12 @@ +#ifndef BUFFER_H +#define BUFFER_H + +#include + +struct Buffer_t +{ + uint32_t size; + void* ptr; +}; + +#endif \ No newline at end of file diff --git a/api/SonoException.h b/api/SonoException.h new file mode 100644 index 0000000..cdf98ae --- /dev/null +++ b/api/SonoException.h @@ -0,0 +1,22 @@ +#ifndef SONO_EXCEPTION_H +#define SONO_EXCEPTION_H + +#include + +class SonoException : public std::exception +{ +private: + std::string _message; +public: + explicit SonoException(const std::string& message) + { + _message = message; + } + + const char* what() const noexcept override + { + return _message.c_str(); + } +}; + +#endif \ No newline at end of file diff --git a/api/SonoPcieDevice.cpp b/api/SonoPcieDevice.cpp new file mode 100644 index 0000000..bb3168a --- /dev/null +++ b/api/SonoPcieDevice.cpp @@ -0,0 +1,106 @@ +#include "SonoPcieDevice.h" + +#include +#include + +SonoPcieDevice::SonoPcieDevice() +{ + bars[0].size = 32 MB; + bars[1].size = 16 MB; + bars[2].size = 16 KB; + + for(auto i = 0; i < TOTAL_DMA_BUFFER_NUM; i++) + { + dmaBuffers[i].size = DMA_SIZE; + } +} + +void SonoPcieDevice::init() +{ + initBars(); + + initDmaBuffers(); +} + +void SonoPcieDevice::initBars() +{ + int fd = open("/proc/sono_bars", O_RDWR | O_SYNC); + if(fd == -1) + { + SonoException exp("Could not open bar proc file"); + throw exp; + } + + for(auto i = 0; i < TOTAL_BAR_NUM; i++) + { + bars[i].ptr = mmap(0, bars[i].size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, i << 12); + } +} + +void SonoPcieDevice::initDmaBuffers() +{ + int fd = open("/proc/sono_buffers", O_RDWR | O_SYNC); + if(fd == -1) + { + SonoException exp("Could not open buffers proc file"); + throw exp; + } + + for(auto i = 0; i < TOTAL_DMA_BUFFER_NUM; i++) + { + dmaBuffers[i].ptr = mmap(0, dmaBuffers[i].size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, i << 12); + } +} + + void SonoPcieDevice::writeByte(uint32_t bar, uint8_t data, uint32_t offset) + { + uint8_t* temp = (uint8_t*)bars[bar].ptr; + temp[offset] = data; + } + + void SonoPcieDevice::writeShort(uint32_t bar, uint16_t data, uint32_t offset) + { + uint16_t* temp = (uint16_t*)bars[bar].ptr; + temp[offset / 2] = data; + } + + void SonoPcieDevice::writeWord(uint32_t bar, uint32_t data, uint32_t offset) + { + uint32_t* temp = (uint32_t*)bars[bar].ptr; + temp[offset / 4] = data; + } + + void SonoPcieDevice::writeLong(uint32_t bar, uint64_t data, uint32_t offset) + { + uint64_t* temp = (uint64_t*)bars[bar].ptr; + temp[offset / 8] = data; + } + + uint8_t SonoPcieDevice::readByte(uint32_t bar, uint32_t offset) + { + uint8_t* temp = (uint8_t*)bars[bar].ptr; + return temp[offset]; + } + + uint16_t SonoPcieDevice::readShort(uint32_t bar, uint32_t offset) + { + uint16_t* temp = (uint16_t*)bars[bar].ptr; + return temp[offset / 2]; + } + + uint32_t SonoPcieDevice::readWord(uint32_t bar, uint32_t offset) + { + uint32_t* temp = (uint32_t*)bars[bar].ptr; + return temp[offset / 4]; + } + + uint64_t SonoPcieDevice::readLong(uint32_t bar, uint32_t offset) + { + uint64_t* temp = (uint64_t*)bars[bar].ptr; + return temp[offset / 8]; + } + + uint8_t* SonoPcieDevice::getBufferPtr(uint32_t buffer) + { + return (uint8_t*)dmaBuffers[buffer].ptr; + } \ No newline at end of file diff --git a/api/SonoPcieDevice.h b/api/SonoPcieDevice.h new file mode 100644 index 0000000..f1e24a6 --- /dev/null +++ b/api/SonoPcieDevice.h @@ -0,0 +1,42 @@ +#ifndef SONOPCIEDEVICE_H +#define SONOPCIEDEVICE_H + +#include + +#include "SonoException.h" +#include "Buffer.h" + +#define KB * 1024 +#define MB KB KB +#define TOTAL_BAR_NUM 3 +#define TOTAL_DMA_BUFFER_NUM 16 +#define DMA_SIZE 4 MB + +class SonoPcieDevice +{ +private: + Buffer_t bars[TOTAL_BAR_NUM]; + Buffer_t dmaBuffers[TOTAL_DMA_BUFFER_NUM]; + + void initBars(); + void initDmaBuffers(); + +public: + SonoPcieDevice(); + + void init(); + + void writeByte(uint32_t bar, uint8_t data, uint32_t offset); + void writeShort(uint32_t bar, uint16_t data, uint32_t offset); + void writeWord(uint32_t bar, uint32_t data, uint32_t offset); + void writeLong(uint32_t bar, uint64_t data, uint32_t offset); + + uint8_t readByte(uint32_t bar, uint32_t offset); + uint16_t readShort(uint32_t bar, uint32_t offset); + uint32_t readWord(uint32_t bar, uint32_t offset); + uint64_t readLong(uint32_t bar, uint32_t offset); + + uint8_t* getBufferPtr(uint32_t buffer); +}; + +#endif \ No newline at end of file diff --git a/app/Test/.gitignore b/app/Test/.gitignore new file mode 100644 index 0000000..fab7372 --- /dev/null +++ b/app/Test/.gitignore @@ -0,0 +1,73 @@ +# This file is used to ignore files which are generated +# ---------------------------------------------------------------------------- + +*~ +*.autosave +*.a +*.core +*.moc +*.o +*.obj +*.orig +*.rej +*.so +*.so.* +*_pch.h.cpp +*_resource.rc +*.qm +.#* +*.*# +core +!core/ +tags +.DS_Store +.directory +*.debug +Makefile* +*.prl +*.app +moc_*.cpp +ui_*.h +qrc_*.cpp +Thumbs.db +*.res +*.rc +/.qmake.cache +/.qmake.stash + +# qtcreator generated files +*.pro.user* + +# xemacs temporary files +*.flc + +# Vim temporary files +.*.swp + +# Visual Studio generated files +*.ib_pdb_index +*.idb +*.ilk +*.pdb +*.sln +*.suo +*.vcproj +*vcproj.*.*.user +*.ncb +*.sdf +*.opensdf +*.vcxproj +*vcxproj.* + +# MinGW generated files +*.Debug +*.Release + +# Python byte code +*.pyc + +# Binaries +# -------- +*.dll +*.exe + diff --git a/app/Test/MainWindow.cpp b/app/Test/MainWindow.cpp new file mode 100644 index 0000000..ec12972 --- /dev/null +++ b/app/Test/MainWindow.cpp @@ -0,0 +1,15 @@ +#include "MainWindow.h" +#include "ui_MainWindow.h" + +MainWindow::MainWindow(QWidget *parent) + : QMainWindow(parent) + , ui(new Ui::MainWindow) +{ + ui->setupUi(this); +} + +MainWindow::~MainWindow() +{ + delete ui; +} + diff --git a/app/Test/MainWindow.h b/app/Test/MainWindow.h new file mode 100644 index 0000000..274dd1b --- /dev/null +++ b/app/Test/MainWindow.h @@ -0,0 +1,21 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include + +QT_BEGIN_NAMESPACE +namespace Ui { class MainWindow; } +QT_END_NAMESPACE + +class MainWindow : public QMainWindow +{ + Q_OBJECT + +public: + MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + +private: + Ui::MainWindow *ui; +}; +#endif // MAINWINDOW_H diff --git a/app/Test/MainWindow.ui b/app/Test/MainWindow.ui new file mode 100644 index 0000000..b232854 --- /dev/null +++ b/app/Test/MainWindow.ui @@ -0,0 +1,22 @@ + + + MainWindow + + + + 0 + 0 + 800 + 600 + + + + MainWindow + + + + + + + + diff --git a/app/Test/Test.pro b/app/Test/Test.pro new file mode 100644 index 0000000..b6fdfca --- /dev/null +++ b/app/Test/Test.pro @@ -0,0 +1,31 @@ +QT += core gui + +greaterThan(QT_MAJOR_VERSION, 4): QT += widgets + +CONFIG += c++11 + +# The following define makes your compiler emit warnings if you use +# any Qt feature that has been marked deprecated (the exact warnings +# depend on your compiler). Please consult the documentation of the +# deprecated API in order to know how to port your code away from it. +DEFINES += QT_DEPRECATED_WARNINGS + +# You can also make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +# You can also select to disable deprecated APIs only up to a certain version of Qt. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + main.cpp \ + MainWindow.cpp + +HEADERS += \ + MainWindow.h + +FORMS += \ + MainWindow.ui + +# Default rules for deployment. +qnx: target.path = /tmp/$${TARGET}/bin +else: unix:!android: target.path = /opt/$${TARGET}/bin +!isEmpty(target.path): INSTALLS += target diff --git a/app/Test/main.cpp b/app/Test/main.cpp new file mode 100644 index 0000000..723a9ab --- /dev/null +++ b/app/Test/main.cpp @@ -0,0 +1,11 @@ +#include "MainWindow.h" + +#include + +int main(int argc, char *argv[]) +{ + QApplication a(argc, argv); + MainWindow w; + w.show(); + return a.exec(); +}