2015-03-30 23:20:45 +00:00
|
|
|
#include "transmitter.h"
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <exception>
|
|
|
|
#include <iostream>
|
|
|
|
|
2015-04-01 00:31:19 +00:00
|
|
|
#define ACCESS(base, offset) *(volatile int*)((int)base + offset)
|
2015-03-30 23:20:45 +00:00
|
|
|
|
2015-04-01 00:31:19 +00:00
|
|
|
Transmitter::Transmitter(double frequency)
|
2015-03-30 23:20:45 +00:00
|
|
|
{
|
|
|
|
int memFd;
|
|
|
|
if ((memFd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
|
2015-04-01 13:00:15 +00:00
|
|
|
std::cout << "Error: sudo privileges are required" << std::endl;
|
2015-03-30 23:20:45 +00:00
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:31:19 +00:00
|
|
|
void *peripheralsMap = mmap(NULL, 0x002FFFFF, PROT_READ | PROT_WRITE, MAP_SHARED, memFd, 0x20000000);
|
2015-03-30 23:20:45 +00:00
|
|
|
close(memFd);
|
2015-04-01 00:31:19 +00:00
|
|
|
if (peripheralsMap == MAP_FAILED) {
|
2015-04-01 13:00:15 +00:00
|
|
|
std::cout << "Error: cannot obtain access to peripherals (mmap error)" << std::endl;
|
2015-03-30 23:20:45 +00:00
|
|
|
throw std::exception();
|
|
|
|
}
|
|
|
|
|
2015-04-01 00:31:19 +00:00
|
|
|
peripherals = (volatile unsigned*)peripheralsMap;
|
|
|
|
|
|
|
|
ACCESS(peripherals, 0x00200000) = (ACCESS(peripherals, 0x00200000) & 0xFFFF8FFF) | (0x01 << 14);
|
2015-04-01 15:55:36 +00:00
|
|
|
ACCESS(peripherals, 0x00101070) = (0x5A << 24) | (0x01 << 9) | (0x01 << 4) | 0x06;
|
2015-03-30 23:20:45 +00:00
|
|
|
}
|
2015-04-01 00:31:19 +00:00
|
|
|
|
|
|
|
void Transmitter::transmit(std::vector<unsigned int> *freqDivs, unsigned int sampleRate) {
|
2015-04-01 15:55:36 +00:00
|
|
|
unsigned int offset = 0, length = freqDivs->size();
|
2015-04-01 00:52:27 +00:00
|
|
|
unsigned int *data = &(*freqDivs)[0];
|
2015-04-01 00:31:19 +00:00
|
|
|
|
2015-04-01 15:55:36 +00:00
|
|
|
unsigned long long current = 0;
|
|
|
|
unsigned long long start = ((unsigned long long)ACCESS(peripherals, 0x00003008) << 32) | ACCESS(peripherals, 0x00003004);
|
|
|
|
|
2015-04-01 00:31:19 +00:00
|
|
|
while (true) {
|
2015-04-01 15:55:36 +00:00
|
|
|
if (offset >= length) break;
|
|
|
|
ACCESS(peripherals, 0x00101074) = (0x5A << 24) | data[offset];
|
2015-04-01 00:31:19 +00:00
|
|
|
|
2015-04-01 16:37:05 +00:00
|
|
|
usleep(10);
|
2015-04-01 00:31:19 +00:00
|
|
|
|
2015-04-01 15:55:36 +00:00
|
|
|
current = ((unsigned long long)ACCESS(peripherals, 0x00003008) << 32) | ACCESS(peripherals, 0x00003004);
|
|
|
|
offset = (unsigned int)((current - start) * (unsigned long long)sampleRate / 1000000);
|
2015-04-01 00:31:19 +00:00
|
|
|
}
|
|
|
|
}
|
2015-03-30 23:20:45 +00:00
|
|
|
|
|
|
|
Transmitter::~Transmitter()
|
2015-04-01 00:31:19 +00:00
|
|
|
{
|
|
|
|
ACCESS(peripherals, 0x00101070) = (0x5A << 24);
|
2015-03-30 23:20:45 +00:00
|
|
|
}
|