fm_transmitter/transmitter.cpp

60 wiersze
1.9 KiB
C++
Czysty Zwykły widok Historia

2015-03-30 23:20:45 +00:00
#include "transmitter.h"
2015-04-01 00:31:19 +00:00
#include "pcm_wave_reader.h"
2015-03-30 23:20:45 +00:00
#include <sys/mman.h>
2015-04-01 00:31:19 +00:00
#include <sys/time.h>
2015-03-30 23:20:45 +00:00
#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 00:31:19 +00:00
std::cout << "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) {
std::cout << "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);
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) {
struct timeval time;
gettimeofday(&time, NULL);
unsigned int start_sec = (unsigned int)time.tv_sec;
unsigned int start_usec = (unsigned int)time.tv_usec;
unsigned int current_sec = 0, current_usec = 0;
unsigned int offset = 0, dataOffset;
while (true) {
dataOffset = (unsigned int)((unsigned long long)offset * (unsigned long long)sampleRate / 1000000);
if (dataOffset >= freqDivs->size()) break;
ACCESS(peripherals, 0x00101074) = (0x5A << 24) | (unsigned int)freqDivs->at(dataOffset);
usleep(10);
gettimeofday(&time, NULL);
current_sec = (unsigned int)time.tv_sec;
current_usec = (unsigned int)time.tv_usec;
offset = (current_sec - start_sec) * 1000000 + current_usec - start_usec;
}
}
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
}