Add SetAmSamples method

This commit is contained in:
F5OEO 2018-11-05 13:10:23 +00:00
parent f448ed0be7
commit 754322f642
2 changed files with 51 additions and 3 deletions

View file

@ -17,13 +17,17 @@ This program is free software: you can redistribute it and/or modify
#include "stdio.h" #include "stdio.h"
#include "amdmasync.h" #include "amdmasync.h"
#include "gpio.h"
#include <unistd.h>
#include <math.h> #include <math.h>
#include <time.h>
#include <sched.h>
amdmasync::amdmasync(uint64_t TuneFrequency,uint32_t SampleRate,int Channel,uint32_t FifoSize):bufferdma(Channel,FifoSize,3,2) amdmasync::amdmasync(uint64_t TuneFrequency,uint32_t SR,int Channel,uint32_t FifoSize):bufferdma(Channel,FifoSize,3,2)
{ {
SampleRate=SR;
tunefreq=TuneFrequency; tunefreq=TuneFrequency;
clkgpio::SetAdvancedPllMode(true); clkgpio::SetAdvancedPllMode(true);
clkgpio::SetCenterFrequency(TuneFrequency,SampleRate); clkgpio::SetCenterFrequency(TuneFrequency,SampleRate);
@ -136,4 +140,45 @@ void amdmasync::SetAmSample(uint32_t Index,float Amplitude) //-1;1
PushSample(Index); PushSample(Index);
} }
void amdmasync::SetAmSamples(float *sample,size_t Size)
{
size_t NbWritten=0;
int OSGranularity=100;
long int start_time;
long time_difference=0;
struct timespec gettime_now;
while(NbWritten<Size)
{
clock_gettime(CLOCK_REALTIME, &gettime_now);
start_time = gettime_now.tv_nsec;
int Available=GetBufferAvailable();
int TimeToSleep=1e6*((int)buffersize*3/4-Available)/SampleRate-OSGranularity; // Sleep for theorically fill 3/4 of Fifo
if(TimeToSleep>0)
{
//fprintf(stderr,"buffer size %d Available %d SampleRate %d Sleep %d\n",buffersize,Available,SampleRate,TimeToSleep);
usleep(TimeToSleep);
}
else
{
//fprintf(stderr,"No Sleep %d\n",TimeToSleep);
sched_yield();
}
clock_gettime(CLOCK_REALTIME, &gettime_now);
time_difference = gettime_now.tv_nsec - start_time;
if(time_difference<0) time_difference+=1E9;
//fprintf(stderr,"Measure samplerate=%d\n",(int)((GetBufferAvailable()-Available)*1e9/time_difference));
Available=GetBufferAvailable();
int Index=GetUserMemIndex();
int ToWrite=((int)Size-(int)NbWritten)<Available?Size-NbWritten:Available;
for(int i=0;i<ToWrite;i++)
{
SetAmSample(Index+i,sample[NbWritten++]);
}
}
}

View file

@ -2,6 +2,7 @@
#define DEF_AMDMASYNC #define DEF_AMDMASYNC
#include "stdint.h" #include "stdint.h"
#include "stdio.h"
#include "dma.h" #include "dma.h"
#include "gpio.h" #include "gpio.h"
@ -11,12 +12,14 @@ class amdmasync:public bufferdma,public clkgpio,public pwmgpio,public pcmgpio
uint64_t tunefreq; uint64_t tunefreq;
bool syncwithpwm; bool syncwithpwm;
uint32_t Originfsel; uint32_t Originfsel;
uint32_t SampleRate;
public: public:
amdmasync(uint64_t TuneFrequency,uint32_t SampleRate,int Channel,uint32_t FifoSize); amdmasync(uint64_t TuneFrequency,uint32_t SR,int Channel,uint32_t FifoSize);
~amdmasync(); ~amdmasync();
void SetDmaAlgo(); void SetDmaAlgo();
void SetAmSample(uint32_t Index,float Amplitude); void SetAmSample(uint32_t Index,float Amplitude);
void SetAmSamples(float *sample,size_t Size);
}; };
#endif #endif