Browse Source

Add sanity check

Add short and long operations
Add extra files and folders to git ignore
Change bar size schema for test purposes
v1.0
h-4nd-h 5 years ago
parent
commit
504116f016
  1. 112
      api/api.cpp
  2. 32
      api/api.h
  3. 9
      usdpci.c

112
api/api.cpp

@ -11,57 +11,127 @@
#include <pthread.h>
/*************************************************************************************************/
UltraSoundDevice::UltraSoundDevice()
void UltraSoundDevice::sanityCheck(uint32_t address, uint32_t bar)
{
files[0] = open("/proc/usd_reg_0", O_RDWR, 0);
bars[0]= mmap(NULL, 64 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, files[0], 0);
if(bar >= BAR_NUM)
{
throw(myexception("Wrong bar number"));
}
files[1] = open("/proc/usd_reg_1", O_RDWR, 0);
bars[1]= mmap(NULL, 128 * 1024 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, files[1], 0);
if(address >= bars[bar].size)
{
throw(myexception("Bar overflow"));
}
}
files[2] = open("/proc/usd_reg_2", O_RDWR, 0);
bars[2]= mmap(NULL, 8 * 1024, PROT_READ | PROT_WRITE, MAP_SHARED, files[2], 0);
/*************************************************************************************************/
UltraSoundDevice::UltraSoundDevice()
{
#if 0
bars[0].size = 64 * 1024;
bars[1].size = 128 * 1024 * 1024;
bars[2].size = 8 * 1024;
#else
bars[0].size = 32 * 1024 * 1024;
bars[1].size = 128 * 1024 * 1024;
bars[2].size = 16 * 1024;
#endif
for(int i = 0; i < BAR_NUM; i++)
{
char str[64];
sprintf(str, "%s_%d", "/proc/usd_reg", i);
bars[i].file = open(str, O_RDWR, 0);
bars[i].ptr = mmap(NULL, bars[i].size, PROT_READ | PROT_WRITE, MAP_SHARED, bars[i].file , 0);
}
}
/*************************************************************************************************/
UltraSoundDevice::~UltraSoundDevice()
{
munmap(bars[0], 64 * 1024);
munmap(bars[1], 128 * 1024 * 1024);
munmap(bars[2], 8 * 1024);
for(int i = 0; i < BAR_NUM; i++)
{
close(files[i]);
}
for(int i = 0; i < BAR_NUM; i++)
{
munmap(bars[i].ptr, bars[i].size);
close(bars[i].file);
}
}
/*************************************************************************************************/
uint8_t UltraSoundDevice::readByte(uint32_t address, uint32_t bar)
{
uint8_t* ptr = (uint8_t*)bars[bar];
sanityCheck(address, bar);
auto ptr = static_cast<uint8_t*>(bars[bar].ptr);
return ptr[address];
}
/*************************************************************************************************/
uint16_t UltraSoundDevice::readShort(uint32_t address, uint32_t bar)
{
sanityCheck(address, bar);
return ptr[address];
auto ptr = static_cast<uint16_t*>(bars[bar].ptr);
return ptr[address];
}
/*************************************************************************************************/
uint32_t UltraSoundDevice::readWord(uint32_t address, uint32_t bar)
{
uint32_t* ptr = (uint32_t*)bars[bar];
sanityCheck(address, bar);
auto ptr = static_cast<uint32_t*>(bars[bar].ptr);
return ptr[address / 4];
}
/*************************************************************************************************/
uint64_t UltraSoundDevice::readLong(uint32_t address, uint32_t bar)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint64_t*>(bars[bar].ptr);
return ptr[address / 4];
return ptr[address];
}
/*************************************************************************************************/
void UltraSoundDevice::writeByte(uint32_t address, uint32_t bar, uint8_t data)
{
uint8_t* ptr = (uint8_t*)bars[bar];
sanityCheck(address, bar);
auto ptr = static_cast<uint8_t*>(bars[bar].ptr);
ptr[address] = data;
}
/*************************************************************************************************/
void UltraSoundDevice::writeShort(uint32_t address, uint32_t bar, uint16_t data)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint16_t*>(bars[bar].ptr);
ptr[address / 2] = data;
}
/*************************************************************************************************/
void UltraSoundDevice::writeWord(uint32_t address, uint32_t bar, uint32_t data)
{
uint32_t* ptr = (uint32_t*)bars[bar];
sanityCheck(address, bar);
auto ptr = static_cast<uint32_t*>(bars[bar].ptr);
ptr[address / 4] = data;
}
/*************************************************************************************************/
void UltraSoundDevice::writeLong(uint32_t address, uint32_t bar, uint64_t data)
{
sanityCheck(address, bar);
auto ptr = static_cast<uint64_t*>(bars[bar].ptr);
ptr[address / 8] = data;
}

32
api/api.h

@ -1,21 +1,47 @@
#include "stdint.h"
#include <exception>
#define BAR_NUM 3
#define DEV_NAME "usd_reg"
typedef struct bar_t{
void* ptr;
int file;
uint32_t size;
}bar_t;
class myexception: public std::exception
{
private:
char* _message;
public:
myexception(char* message) { _message = message; }
virtual const char* what() const noexcept
{
return _message;
}
};
class UltraSoundDevice
{
private:
void* bars[BAR_NUM];
int files[BAR_NUM];
bar_t bars[BAR_NUM];
void sanityCheck(uint32_t address, uint32_t bar);
public:
UltraSoundDevice();
~UltraSoundDevice();
uint8_t readByte(uint32_t address, uint32_t bar);
uint16_t readShort(uint32_t address, uint32_t bar);
uint32_t readWord(uint32_t address, uint32_t bar);
uint64_t readLong(uint32_t address, uint32_t bar);
void writeByte(uint32_t address, uint32_t bar, uint8_t data);
void writeShort(uint32_t address, uint32_t bar, uint16_t data);
void writeWord(uint32_t address, uint32_t bar, uint32_t data);
};
void writeLong(uint32_t address, uint32_t bar, uint64_t data);
};

9
usdpci.c

@ -114,7 +114,7 @@ static int __init pcie_init(void)
bars[0].chrdev_fops.open = usd_open_0;
bars[0].chrdev_fops.mmap = usd_mmap_0;
bars[0].chrdev_fops.release = usd_close_0;
bars[0].bar_size = 64 * 1024;
bars[0].bar_size = 32 * 1024 * 1024;
bars[1].chrdev_fops.owner = THIS_MODULE;
bars[1].chrdev_fops.open = usd_open_1;
@ -126,7 +126,7 @@ static int __init pcie_init(void)
bars[2].chrdev_fops.open = usd_open_2;
bars[2].chrdev_fops.mmap = usd_mmap_2;
bars[2].chrdev_fops.release = usd_close_2;
bars[2].bar_size = 8 * 1024;
bars[2].bar_size = 16 * 1024;
PRINT_ALERT("register driver success" );
@ -406,12 +406,13 @@ static int __init pcie_probe (struct pci_dev *dev, const struct pci_device_id *i
pci_set_master(dev);
printk(KERN_ALERT "-----------------------------------\n");
printk(KERN_ALERT "BAR0 reg 0 = 0x%x\n", ioread32(bar0_data));
printk(KERN_ALERT "BAR2 reg 0 = 0x%x\n", ioread32(bar2_data));
// printk(KERN_ALERT "BAR0 reg 0 = 0x%x\n", ioread32(bar0_data));
*bar1_data = 0x12345678;
printk(KERN_ALERT "BAR1 reg 0 = 0x%x\n", ioread32(bar1_data));
printk(KERN_ALERT "-----------------------------------\n");
create_dma_buffer(2);
// create_dma_buffer(2);
return 0;

Loading…
Cancel
Save