fm_transmitter/transmitter.cpp

516 wiersze
17 KiB
C++
Czysty Zwykły widok Historia

2015-12-16 20:30:58 +00:00
/*
fm_transmitter - use Raspberry Pi as FM transmitter
Copyright (c) 2019, Marcin Kondej
2015-12-16 20:30:58 +00:00
All rights reserved.
See https://github.com/markondej/fm_transmitter
Redistribution and use in source and binary forms, with or without modification, are
permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list
of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this
list of conditions and the following disclaimer in the documentation and/or other
materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be
used to endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
2019-09-19 22:53:36 +00:00
#include "transmitter.hpp"
#include "mailbox.h"
#include <bcm_host.h>
2019-09-19 22:53:36 +00:00
#include <thread>
2019-09-27 07:59:23 +00:00
#include <chrono>
2015-12-16 20:30:58 +00:00
#include <cmath>
2016-12-07 12:34:19 +00:00
#include <fcntl.h>
2015-12-16 20:30:58 +00:00
#include <sys/mman.h>
#define PERIPHERALS_PHYS_BASE 0x7E000000
2019-01-07 01:52:33 +00:00
#define BCM2835_PERI_VIRT_BASE 0x20000000
2019-09-12 06:49:15 +00:00
#define BCM2838_PERI_VIRT_BASE 0xFE000000
#define DMA0_BASE_OFFSET 0x00007000
#define DMA15_BASE_OFFSET 0x00E05000
#define CLK0_BASE_OFFSET 0x00101070
#define PWMCLK_BASE_OFFSET 0x001010A0
#define GPIO_BASE_OFFSET 0x00200000
#define PWM_BASE_OFFSET 0x0020C000
#define TIMER_BASE_OFFSET 0x00003000
2019-01-07 01:52:33 +00:00
#define BCM2835_MEM_FLAG 0x0C
2019-09-12 13:57:38 +00:00
#define BCM2838_MEM_FLAG 0x04
#define BCM2835_PLLD_FREQ 500
#define BCM2838_PLLD_FREQ 750
2019-01-07 01:52:33 +00:00
#define BUFFER_TIME 1000000
2019-01-07 01:52:33 +00:00
#define PWM_WRITES_PER_SAMPLE 10
#define PWM_CHANNEL_RANGE 32
2019-09-12 13:57:38 +00:00
#define PAGE_SIZE 4096
struct TimerRegisters {
2019-09-19 22:53:36 +00:00
uint32_t ctlStatus;
uint32_t low;
uint32_t high;
uint32_t c0;
uint32_t c1;
uint32_t c2;
uint32_t c3;
};
struct ClockRegisters {
2019-09-19 22:53:36 +00:00
uint32_t ctl;
uint32_t div;
};
struct PWMRegisters {
2019-09-19 22:53:36 +00:00
uint32_t ctl;
uint32_t status;
uint32_t dmaConf;
uint32_t reserved0;
uint32_t chn1Range;
uint32_t chn1Data;
uint32_t fifoIn;
uint32_t reserved1;
uint32_t chn2Range;
uint32_t chn2Data;
};
struct DMAControllBlock {
2019-09-19 22:53:36 +00:00
uint32_t transferInfo;
uint32_t srcAddress;
uint32_t dstAddress;
uint32_t transferLen;
uint32_t stride;
uint32_t nextCbAddress;
uint32_t reserved0;
uint32_t reserved1;
};
struct DMARegisters {
2019-09-19 22:53:36 +00:00
uint32_t ctlStatus;
uint32_t cbAddress;
uint32_t transferInfo;
uint32_t srcAddress;
uint32_t dstAddress;
uint32_t transferLen;
uint32_t stride;
uint32_t nextCbAddress;
uint32_t debug;
};
struct AllocatedMemory {
uint32_t handle, size, physicalBase, virtualBase;
int mBoxFd;
};
2015-12-16 20:30:58 +00:00
2018-12-30 08:34:27 +00:00
bool Transmitter::transmitting = false;
2019-09-19 22:53:36 +00:00
volatile ClockRegisters *Transmitter::output = nullptr;
2019-09-26 20:42:52 +00:00
uint32_t Transmitter::sampleOffset, Transmitter::clockDivisor, Transmitter::divisorRange, Transmitter::sampleRate;
2019-09-27 09:17:03 +00:00
std::vector<Sample> Transmitter::samples;
2019-09-27 09:21:00 +00:00
std::mutex Transmitter::samplesMutex;
2019-09-26 20:42:52 +00:00
void *Transmitter::peripherals;
2018-12-30 08:34:27 +00:00
Transmitter::Transmitter()
2015-12-16 20:30:58 +00:00
{
int memFd;
if ((memFd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
throw std::runtime_error("Cannot open /dev/mem (permission denied)");
2015-12-16 20:30:58 +00:00
}
2019-09-19 22:53:36 +00:00
peripherals = mmap(nullptr, getPeripheralsSize(), PROT_READ | PROT_WRITE, MAP_SHARED, memFd, getPeripheralsVirtBaseAddress());
2015-12-16 20:30:58 +00:00
close(memFd);
if (peripherals == MAP_FAILED) {
throw std::runtime_error("Cannot obtain access to peripherals (mmap error)");
2015-12-16 20:30:58 +00:00
}
}
Transmitter::~Transmitter()
{
2019-09-19 22:53:36 +00:00
munmap(peripherals, getPeripheralsSize());
2015-12-16 20:30:58 +00:00
}
Transmitter &Transmitter::getInstance()
2015-12-16 20:30:58 +00:00
{
static Transmitter instance;
return instance;
2015-12-16 20:30:58 +00:00
}
2019-10-29 01:04:55 +00:00
uint32_t Transmitter::getPeripheralsVirtBaseAddress() const
2019-01-07 13:56:06 +00:00
{
2019-09-19 22:53:36 +00:00
return (bcm_host_get_peripheral_size() == BCM2838_PERI_VIRT_BASE) ? BCM2838_PERI_VIRT_BASE : bcm_host_get_peripheral_address();
}
2019-10-29 01:04:55 +00:00
uint32_t Transmitter::getPeripheralsSize() const
2019-09-19 22:53:36 +00:00
{
uint32_t size = bcm_host_get_peripheral_size();
if (size == BCM2838_PERI_VIRT_BASE) {
size = 0x01000000;
2019-01-07 13:56:06 +00:00
}
2019-09-19 22:53:36 +00:00
return size;
2019-01-07 13:56:06 +00:00
}
2019-10-29 01:04:55 +00:00
float Transmitter::getSourceFreq() const
2019-01-07 13:56:06 +00:00
{
2019-09-19 22:53:36 +00:00
return (getPeripheralsVirtBaseAddress() == BCM2838_PERI_VIRT_BASE) ? BCM2838_PLLD_FREQ : BCM2835_PLLD_FREQ;
}
2019-10-29 01:04:55 +00:00
uint32_t Transmitter::getPeripheralPhysAddress(volatile void *object) const
{
2019-09-25 11:59:40 +00:00
return PERIPHERALS_PHYS_BASE + (reinterpret_cast<uint32_t>(object) - reinterpret_cast<uint32_t>(peripherals));
2019-09-19 22:53:36 +00:00
}
2019-01-07 13:56:06 +00:00
2019-10-29 01:04:55 +00:00
uint32_t Transmitter::getPeripheralVirtAddress(uint32_t offset) const
{
2019-09-25 11:59:40 +00:00
return reinterpret_cast<uint32_t>(peripherals) + offset;
2019-01-07 13:56:06 +00:00
}
2019-10-29 01:04:55 +00:00
uint32_t Transmitter::getMemoryPhysAddress(AllocatedMemory &memory, volatile void *object) const
2019-09-12 06:49:15 +00:00
{
2019-09-25 11:59:40 +00:00
return memory.physicalBase + (reinterpret_cast<uint32_t>(object) - memory.virtualBase);
2019-09-12 06:49:15 +00:00
}
2019-09-19 22:53:36 +00:00
AllocatedMemory Transmitter::allocateMemory(uint32_t size)
2019-09-12 06:49:15 +00:00
{
2019-09-19 22:53:36 +00:00
AllocatedMemory memory;
memory.size = 0x00000000;
memory.mBoxFd = mbox_open();
if (size % PAGE_SIZE) {
size = (size / PAGE_SIZE + 1) * PAGE_SIZE;
2019-09-12 06:49:15 +00:00
}
2019-09-19 22:53:36 +00:00
memory.handle = mem_alloc(memory.mBoxFd, size, PAGE_SIZE, (getPeripheralsVirtBaseAddress() == BCM2835_PERI_VIRT_BASE) ? BCM2835_MEM_FLAG : BCM2838_MEM_FLAG);
if (!memory.handle) {
mbox_close(memory.mBoxFd);
return memory;
}
memory.physicalBase = mem_lock(memory.mBoxFd, memory.handle);
2019-09-25 11:59:40 +00:00
memory.virtualBase = reinterpret_cast<uint32_t>(mapmem(memory.physicalBase & ~0xC0000000, size));
2019-09-19 22:53:36 +00:00
memory.size = size;
return memory;
}
void Transmitter::freeMemory(AllocatedMemory &memory)
{
2019-09-25 11:59:40 +00:00
unmapmem(reinterpret_cast<void *>(memory.virtualBase), memory.size);
2019-09-19 22:53:36 +00:00
mem_unlock(memory.mBoxFd, memory.handle);
mem_free(memory.mBoxFd, memory.handle);
mbox_close(memory.mBoxFd);
memory.size = 0x00000000;
}
volatile PWMRegisters *Transmitter::initPwmController()
{
2019-09-25 11:59:40 +00:00
volatile ClockRegisters *pwmClk = reinterpret_cast<ClockRegisters *>(getPeripheralVirtAddress(PWMCLK_BASE_OFFSET));
2019-09-26 15:04:57 +00:00
float pwmClkFreq = PWM_WRITES_PER_SAMPLE * PWM_CHANNEL_RANGE * sampleRate / 1000000;
2019-09-19 22:53:36 +00:00
pwmClk->ctl = (0x5A << 24) | 0x06;
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2019-09-25 11:59:40 +00:00
pwmClk->div = (0x5A << 24) | static_cast<uint32_t>(getSourceFreq() * (0x01 << 12) / pwmClkFreq);
2019-09-19 22:53:36 +00:00
pwmClk->ctl = (0x5A << 24) | (0x01 << 4) | 0x06;
2019-09-25 11:59:40 +00:00
volatile PWMRegisters *pwm = reinterpret_cast<PWMRegisters *>(getPeripheralVirtAddress(PWM_BASE_OFFSET));
2019-09-19 22:53:36 +00:00
pwm->ctl = 0x00000000;
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2019-09-19 22:53:36 +00:00
pwm->status = 0x01FC;
pwm->ctl = (0x01 << 6);
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2019-09-19 22:53:36 +00:00
pwm->chn1Range = PWM_CHANNEL_RANGE;
pwm->dmaConf = (0x01 << 31) | 0x0707;
pwm->ctl = (0x01 << 5) | (0x01 << 2) | 0x01;
return pwm;
}
void Transmitter::closePwmController(volatile PWMRegisters *pwm)
{
pwm->ctl = 0x00000000;
2019-09-12 06:49:15 +00:00
}
2019-09-19 22:53:36 +00:00
volatile DMARegisters *Transmitter::startDma(AllocatedMemory &memory, volatile DMAControllBlock *dmaCb, uint8_t dmaChannel)
2019-09-12 13:57:38 +00:00
{
2019-09-25 11:59:40 +00:00
volatile DMARegisters *dma = reinterpret_cast<DMARegisters *>(getPeripheralVirtAddress((dmaChannel < 15) ? DMA0_BASE_OFFSET + dmaChannel * 0x100 : DMA15_BASE_OFFSET));
2019-09-19 22:53:36 +00:00
dma->ctlStatus = (0x01 << 31);
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2019-09-19 22:53:36 +00:00
dma->ctlStatus = (0x01 << 2) | (0x01 << 1);
dma->cbAddress = getMemoryPhysAddress(memory, dmaCb);
dma->ctlStatus = (0xFF << 16) | 0x01;
return dma;
2019-09-12 13:57:38 +00:00
}
2019-09-19 22:53:36 +00:00
void Transmitter::closeDma(volatile DMARegisters *dma)
2019-01-07 13:56:06 +00:00
{
2019-09-19 22:53:36 +00:00
dma->ctlStatus = (0x01 << 31);
2019-01-07 13:56:06 +00:00
}
2019-09-19 22:53:36 +00:00
volatile ClockRegisters *Transmitter::initClockOutput()
{
2019-09-25 11:59:40 +00:00
volatile ClockRegisters *clock = reinterpret_cast<ClockRegisters *>(getPeripheralVirtAddress(CLK0_BASE_OFFSET));
volatile uint32_t *gpio = reinterpret_cast<uint32_t *>(getPeripheralVirtAddress(GPIO_BASE_OFFSET));
2019-09-19 22:53:36 +00:00
clock->ctl = (0x5A << 24) | 0x06;
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2019-09-19 22:53:36 +00:00
clock->div = (0x5A << 24) | clockDivisor;
clock->ctl = (0x5A << 24) | (0x01 << 9) | (0x01 << 4) | 0x06;
*gpio = (*gpio & 0xFFFF8FFF) | (0x01 << 14);
return clock;
2019-01-10 10:19:16 +00:00
}
2019-09-19 22:53:36 +00:00
void Transmitter::closeClockOutput(volatile ClockRegisters *clock)
{
clock->ctl = (0x5A << 24) | 0x06;
2019-01-10 10:19:16 +00:00
}
2019-09-26 15:04:57 +00:00
void Transmitter::transmit(WaveReader &reader, float frequency, float bandwidth, uint8_t dmaChannel, bool preserveCarrierOnExit)
2015-12-16 20:30:58 +00:00
{
2016-12-14 23:55:21 +00:00
if (transmitting) {
throw std::runtime_error("Cannot play, transmitter already in use");
2015-12-16 20:30:58 +00:00
}
2016-12-14 23:55:21 +00:00
transmitting = true;
2016-12-07 12:17:26 +00:00
PCMWaveHeader header = reader.getHeader();
2019-09-25 11:59:40 +00:00
uint32_t bufferSize = static_cast<uint32_t>(static_cast<uint64_t>(header.sampleRate) * BUFFER_TIME / 1000000);
2015-12-16 20:30:58 +00:00
2019-09-19 22:53:36 +00:00
preserveCarrier = preserveCarrierOnExit;
2019-09-25 11:59:40 +00:00
clockDivisor = static_cast<uint32_t>(round(getSourceFreq() * (0x01 << 12) / frequency));
2019-09-26 15:04:57 +00:00
divisorRange = clockDivisor - static_cast<uint32_t>(round(getSourceFreq() * (0x01 << 12) / (frequency + 0.0005f * bandwidth)));
2019-09-19 22:53:36 +00:00
sampleRate = header.sampleRate;
2019-09-26 19:19:47 +00:00
if (output == nullptr) {
output = initClockOutput();
}
2019-09-26 23:02:17 +00:00
auto finally = [&]() {
if (!preserveCarrier) {
closeClockOutput(output);
output = nullptr;
}
};
2019-09-19 22:53:36 +00:00
try {
if (dmaChannel != 0xFF) {
transmitViaDma(reader, bufferSize, dmaChannel);
} else {
transmitViaCpu(reader, bufferSize);
}
2019-10-07 15:06:29 +00:00
} catch (...) {
2019-09-26 20:42:52 +00:00
preserveCarrier = false;
2019-09-26 23:02:17 +00:00
finally();
2019-10-07 15:06:29 +00:00
throw;
2019-09-26 11:51:56 +00:00
}
2019-09-26 23:02:17 +00:00
finally();
2019-09-19 22:53:36 +00:00
}
2019-09-19 22:53:36 +00:00
void Transmitter::transmitViaCpu(WaveReader &reader, uint32_t bufferSize)
{
2019-09-27 09:17:03 +00:00
samples = reader.getSamples(bufferSize, transmitting);
2019-09-26 10:01:59 +00:00
if (!samples.size()) {
2019-09-19 22:53:36 +00:00
return;
}
sampleOffset = 0;
2019-09-26 23:02:17 +00:00
bool eof = samples.size() < bufferSize;
2019-09-19 22:53:36 +00:00
std::thread txThread(Transmitter::transmitThread);
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(BUFFER_TIME / 2));
2019-09-27 09:21:00 +00:00
bool wait = false;
2019-09-26 23:02:17 +00:00
auto finally = [&]() {
transmitting = false;
txThread.join();
2019-09-27 09:21:00 +00:00
samples.clear();
2019-09-26 23:02:17 +00:00
};
2019-09-19 22:53:36 +00:00
try {
while (!eof && transmitting) {
2019-09-27 09:21:00 +00:00
if (wait) {
std::this_thread::sleep_for(std::chrono::microseconds(BUFFER_TIME / 2));
}
std::lock_guard<std::mutex> locked(samplesMutex);
if (!samples.size()) {
2019-09-19 22:53:36 +00:00
if (!reader.setSampleOffset(sampleOffset + bufferSize)) {
break;
}
2019-01-07 01:52:33 +00:00
samples = reader.getSamples(bufferSize, transmitting);
2019-09-26 10:01:59 +00:00
if (!samples.size()) {
break;
}
2019-09-26 10:01:59 +00:00
eof = samples.size() < bufferSize;
}
2019-09-27 09:21:00 +00:00
wait = true;
2019-01-07 01:52:33 +00:00
}
2019-10-07 15:06:29 +00:00
} catch (...) {
2019-09-26 23:02:17 +00:00
finally();
2019-10-07 15:06:29 +00:00
throw;
2019-09-19 22:53:36 +00:00
}
2019-09-26 23:02:17 +00:00
finally();
2019-09-19 22:53:36 +00:00
}
2019-01-07 01:52:33 +00:00
2019-09-19 22:53:36 +00:00
void Transmitter::transmitViaDma(WaveReader &reader, uint32_t bufferSize, uint8_t dmaChannel)
{
if (dmaChannel > 15) {
throw std::runtime_error("DMA channel number out of range (0 - 15)");
2019-09-19 22:53:36 +00:00
}
2019-09-27 09:17:03 +00:00
samples = reader.getSamples(bufferSize, transmitting);
2019-09-26 10:01:59 +00:00
if (!samples.size()) {
2019-09-19 22:53:36 +00:00
return;
}
bool eof = false;
2019-09-26 10:01:59 +00:00
if (samples.size() < bufferSize) {
bufferSize = samples.size();
2019-09-19 22:53:36 +00:00
eof = true;
}
2019-01-07 01:52:33 +00:00
2019-09-19 22:53:36 +00:00
AllocatedMemory dmaMemory = allocateMemory(sizeof(uint32_t) * (bufferSize + 1) + sizeof(DMAControllBlock) * (2 * bufferSize));
if (!dmaMemory.size) {
throw std::runtime_error("Cannot allocate memory");
2019-09-19 22:53:36 +00:00
}
2019-09-19 22:53:36 +00:00
volatile PWMRegisters *pwm = initPwmController();
2019-09-19 22:53:36 +00:00
uint32_t i, cbOffset = 0;
#ifndef NO_PREEMP
PreEmphasis preEmphasis(sampleRate);
#endif
2019-09-25 11:59:40 +00:00
volatile DMAControllBlock *dmaCb = reinterpret_cast<DMAControllBlock *>(dmaMemory.virtualBase);
volatile uint32_t *clkDiv = reinterpret_cast<uint32_t *>(reinterpret_cast<uint32_t>(dmaCb) + 2 * sizeof(DMAControllBlock) * bufferSize);
volatile uint32_t *pwmFifoData = reinterpret_cast<uint32_t *>(reinterpret_cast<uint32_t>(clkDiv) + sizeof(uint32_t) * bufferSize);
2019-09-19 22:53:36 +00:00
for (i = 0; i < bufferSize; i++) {
2019-09-26 15:04:57 +00:00
float value = samples[i].getMonoValue();
2019-09-19 22:53:36 +00:00
#ifndef NO_PREEMP
value = preEmphasis.filter(value);
#endif
2019-09-25 11:59:40 +00:00
clkDiv[i] = (0x5A << 24) | (clockDivisor - static_cast<int32_t>(round(value * divisorRange)));
2019-09-19 22:53:36 +00:00
dmaCb[cbOffset].transferInfo = (0x01 << 26) | (0x01 << 3);
dmaCb[cbOffset].srcAddress = getMemoryPhysAddress(dmaMemory, &clkDiv[i]);
dmaCb[cbOffset].dstAddress = getPeripheralPhysAddress(&output->div);
dmaCb[cbOffset].transferLen = sizeof(uint32_t);
dmaCb[cbOffset].stride = 0;
dmaCb[cbOffset].nextCbAddress = getMemoryPhysAddress(dmaMemory, &dmaCb[cbOffset + 1]);
cbOffset++;
dmaCb[cbOffset].transferInfo = (0x01 << 26) | (0x05 << 16) | (0x01 << 6) | (0x01 << 3);
dmaCb[cbOffset].srcAddress = getMemoryPhysAddress(dmaMemory, pwmFifoData);
dmaCb[cbOffset].dstAddress = getPeripheralPhysAddress(&pwm->fifoIn);
dmaCb[cbOffset].transferLen = sizeof(uint32_t) * PWM_WRITES_PER_SAMPLE;
dmaCb[cbOffset].stride = 0;
dmaCb[cbOffset].nextCbAddress = getMemoryPhysAddress(dmaMemory, (i < bufferSize - 1) ? &dmaCb[cbOffset + 1] : dmaCb);
cbOffset++;
}
*pwmFifoData = 0x00000000;
volatile DMARegisters *dma = startDma(dmaMemory, dmaCb, dmaChannel);
2019-09-26 19:19:47 +00:00
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(BUFFER_TIME / 4));
2019-09-19 22:53:36 +00:00
2019-09-26 23:02:17 +00:00
auto finally = [&]() {
dmaCb[(cbOffset < 2 * bufferSize) ? cbOffset : 0].nextCbAddress = 0x00000000;
while (dma->cbAddress != 0x00000000) {
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2019-09-26 23:02:17 +00:00
}
closeDma(dma);
closePwmController(pwm);
freeMemory(dmaMemory);
transmitting = false;
2019-09-27 09:21:00 +00:00
samples.clear();
2019-09-26 23:02:17 +00:00
};
2019-09-19 22:53:36 +00:00
try {
while (!eof && transmitting) {
samples = reader.getSamples(bufferSize, transmitting);
2019-09-26 10:01:59 +00:00
if (!samples.size()) {
2019-09-19 22:53:36 +00:00
break;
}
cbOffset = 0;
2019-09-26 10:01:59 +00:00
eof = samples.size() < bufferSize;
for (i = 0; i < samples.size(); i++) {
2019-09-26 15:04:57 +00:00
float value = samples[i].getMonoValue();
2019-09-19 22:53:36 +00:00
#ifndef NO_PREEMP
value = preEmphasis.filter(value);
#endif
while (i == ((dma->cbAddress - getMemoryPhysAddress(dmaMemory, dmaCb)) / (2 * sizeof(DMAControllBlock)))) {
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1000));
2016-12-14 23:55:21 +00:00
}
2019-09-25 11:59:40 +00:00
clkDiv[i] = (0x5A << 24) | (clockDivisor - static_cast<int32_t>(round(value * divisorRange)));
2019-09-19 22:53:36 +00:00
cbOffset += 2;
2016-12-13 01:43:28 +00:00
}
2015-12-16 20:30:58 +00:00
}
2019-10-07 15:06:29 +00:00
} catch (...) {
2019-09-26 23:02:17 +00:00
finally();
2019-10-07 15:06:29 +00:00
throw;
2019-01-07 13:56:06 +00:00
}
2019-09-26 23:02:17 +00:00
finally();
}
2015-12-16 20:30:58 +00:00
2019-09-19 22:53:36 +00:00
void Transmitter::transmitThread()
2015-12-16 20:30:58 +00:00
{
#ifndef NO_PREEMP
2019-09-19 22:53:36 +00:00
PreEmphasis preEmphasis(sampleRate);
2015-12-16 20:30:58 +00:00
#endif
2019-09-25 11:59:40 +00:00
volatile TimerRegisters *timer = reinterpret_cast<TimerRegisters *>(getPeripheralVirtAddress(TIMER_BASE_OFFSET));
uint64_t current = *(reinterpret_cast<volatile uint64_t *>(&timer->low));
2019-09-19 22:53:36 +00:00
uint64_t playbackStart = current;
2015-12-16 20:30:58 +00:00
2018-12-30 08:34:27 +00:00
while (transmitting) {
2019-09-26 10:01:59 +00:00
uint64_t start = current;
2019-09-27 09:21:00 +00:00
bool locked = samplesMutex.try_lock();
auto unlock = [&]() {
if (locked) {
samplesMutex.unlock();
}
};
while ((!locked || !samples.size()) && transmitting) {
unlock();
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1));
2019-09-25 11:59:40 +00:00
current = *(reinterpret_cast<volatile uint64_t *>(&timer->low));
2019-09-27 09:21:00 +00:00
locked = samplesMutex.try_lock();
2015-12-16 20:30:58 +00:00
}
2018-12-30 08:34:27 +00:00
if (!transmitting) {
unlock();
2015-12-16 20:30:58 +00:00
break;
}
2019-09-27 09:17:03 +00:00
std::vector<Sample> loaded(std::move(samples));
unlock();
2015-12-16 20:30:58 +00:00
2019-09-19 22:53:36 +00:00
sampleOffset = (current - playbackStart) * sampleRate / 1000000;
2019-09-26 10:01:59 +00:00
uint32_t offset = (current - start) * sampleRate / 1000000;
2015-12-16 20:30:58 +00:00
while (true) {
2019-09-27 09:17:03 +00:00
if (offset >= loaded.size()) {
2015-12-16 20:30:58 +00:00
break;
}
2019-09-26 10:01:59 +00:00
uint32_t prevOffset = offset;
2019-09-27 09:17:03 +00:00
float value = loaded[offset].getMonoValue();
2015-12-16 20:30:58 +00:00
#ifndef NO_PREEMP
2019-09-19 22:53:36 +00:00
value = preEmphasis.filter(value);
2015-12-16 20:30:58 +00:00
#endif
2019-09-25 11:59:40 +00:00
output->div = (0x5A << 24) | (clockDivisor - static_cast<int32_t>(round(value * divisorRange)));
while (offset == prevOffset) {
2019-09-27 07:59:23 +00:00
std::this_thread::sleep_for(std::chrono::microseconds(1)); // asm("nop");
2019-09-25 11:59:40 +00:00
current = *(reinterpret_cast<volatile uint64_t *>(&timer->low));;
2019-09-19 22:53:36 +00:00
offset = (current - start) * sampleRate / 1000000;
2015-12-16 20:30:58 +00:00
}
}
}
}
void Transmitter::stop()
{
preserveCarrier = false;
transmitting = false;
2015-12-16 20:30:58 +00:00
}