From ff8fa74517efc578558fa3baadfbedfb29fa55aa Mon Sep 17 00:00:00 2001 From: F5OEO Date: Thu, 20 Dec 2018 15:40:16 +0000 Subject: [PATCH] Add OOK example to simulate Ventus/alecto wind station, reduce granularity to 100Khz --- app/testrpitx.cpp | 584 ++++++++++++++++++++++++++++++---------------- src/ookburst.cpp | 35 ++- src/ookburst.h | 17 ++ 3 files changed, 437 insertions(+), 199 deletions(-) diff --git a/app/testrpitx.cpp b/app/testrpitx.cpp index b6a406a..94c0b48 100644 --- a/app/testrpitx.cpp +++ b/app/testrpitx.cpp @@ -5,7 +5,7 @@ #include #include -bool running=true; +bool running = true; /*int gcd ( int a, int b ) { @@ -16,7 +16,7 @@ gcd ( int a, int b ) return b; }*/ -uint64_t gcd(uint64_t x,uint64_t y) +uint64_t gcd(uint64_t x, uint64_t y) { return y == 0 ? x : gcd(y, x % y); } @@ -29,72 +29,72 @@ uint64_t lcm(uint64_t x, uint64_t y) void SimpleTest(uint64_t Freq) { generalgpio genpio; - fprintf(stderr,"GPIOPULL =%x\n",genpio.gpioreg[GPPUDCLK0]); - - #define PULL_OFF 0 - #define PULL_DOWN 1 - #define PULL_UP 2 - genpio.gpioreg[GPPUD]=1;//PULL_DOWN; - usleep(150); - genpio.gpioreg[GPPUDCLK0]=(1<<4); //GPIO CLK is GPIO 4 - usleep(150); - genpio.gpioreg[GPPUDCLK0]=(0); //GPIO CLK is GPIO 4 - + fprintf(stderr, "GPIOPULL =%x\n", genpio.gpioreg[GPPUDCLK0]); + +#define PULL_OFF 0 +#define PULL_DOWN 1 +#define PULL_UP 2 + genpio.gpioreg[GPPUD] = 1; //PULL_DOWN; + usleep(150); + genpio.gpioreg[GPPUDCLK0] = (1 << 4); //GPIO CLK is GPIO 4 + usleep(150); + genpio.gpioreg[GPPUDCLK0] = (0); //GPIO CLK is GPIO 4 + //genpio.setpulloff(4); padgpio pad; pad.setlevel(7); - clkgpio clk; clk.print_clock_tree(); - clk.SetPllNumber(clk_pllc,0); - + clk.SetPllNumber(clk_pllc, 0); + //clk.SetAdvancedPllMode(true); //clk.SetPLLMasterLoop(0,4,0); //clk.Setppm(+7.7); - clk.SetCenterFrequency(Freq,1000); - double freqresolution=clk.GetFrequencyResolution(); - double RealFreq=clk.GetRealFrequency(0); - fprintf(stderr,"Frequency resolution=%f Error freq=%f\n",freqresolution,RealFreq); - int Deviation=0; + clk.SetCenterFrequency(Freq, 1000); + double freqresolution = clk.GetFrequencyResolution(); + double RealFreq = clk.GetRealFrequency(0); + fprintf(stderr, "Frequency resolution=%f Error freq=%f\n", freqresolution, RealFreq); + int Deviation = 0; clk.SetFrequency(000); - + clk.enableclk(4); usleep(100); //clk.SetClkDivFrac(100,0); // If mash!=0 update doesnt seem to work - int count=0; - while(running) + int count = 0; + while (running) { //clk.SetMasterMultFrac(44,(1<15) - { Kp=0; - Ki=Ki+1; + int a = getc(stdin); + static int Ki = 4, Kp = 0, Ka = 0; + Kp = Kp + 1; + if (Kp > 15) + { + Kp = 0; + Ki = Ki + 1; } //Ki=Ki+1; - if(Ki>11) + if (Ki > 11) { - Ki=4; - Ka++; + Ki = 4; + Ka++; } - Ki=Kp; - - clk.SetClkDivFrac(count,count); - + Ki = Kp; + + clk.SetClkDivFrac(count, count); + //clk.SetPLLMasterLoop(Ki,4,Ka); //clk.SetPLLMasterLoop(2,4,0); //clk.SetPLLMasterLoop(3,4,0); //best one - + //printf("Ki=%d :Kp %d Ka %d\n ",Ki,Kp,Ka); /*clk.SetFrequency(000); sleep(5); @@ -108,75 +108,66 @@ void SimpleTest(uint64_t Freq) usleep(1000); }*/ clk.disableclk(4); - } void SimpleTestDMA(uint64_t Freq) { - - int SR=200000; - int FifoSize=4096; - ngfmdmasync ngfmtest(Freq,SR,14,FifoSize); - for(int i=0;running;) + int SR = 200000; + int FifoSize = 4096; + ngfmdmasync ngfmtest(Freq, SR, 14, FifoSize); + for (int i = 0; running;) { //usleep(10); - usleep(FifoSize*1000000.0*3.0/(4.0*SR)); - int Available=ngfmtest.GetBufferAvailable(); - if(Available>FifoSize/2) - { - int Index=ngfmtest.GetUserMemIndex(); + usleep(FifoSize * 1000000.0 * 3.0 / (4.0 * SR)); + int Available = ngfmtest.GetBufferAvailable(); + if (Available > FifoSize / 2) + { + int Index = ngfmtest.GetUserMemIndex(); //printf("GetIndex=%d\n",Index); - for(int j=0;j5000)?1000:0); - ngfmtest.SetFrequencySample(Index+j,(i%SR)/10.0); + ngfmtest.SetFrequencySample(Index + j, (i % SR) / 10.0); i++; - } } - - } - fprintf(stderr,"End\n"); - - ngfmtest.stop(); - -} + fprintf(stderr, "End\n"); + ngfmtest.stop(); +} void SimpleTestFileIQ(uint64_t Freq) { - FILE *iqfile=NULL; - iqfile=fopen("../ssbtest.iq","rb"); - if (iqfile==NULL) printf("input file issue\n"); + FILE *iqfile = NULL; + iqfile = fopen("../ssbtest.iq", "rb"); + if (iqfile == NULL) + printf("input file issue\n"); - #define IQBURST 1280 - bool stereo=true; - int SR=48000; - int FifoSize=512; - iqdmasync iqtest(Freq,SR,14,FifoSize,MODE_IQ); - short IQBuffer[IQBURST*2]; - std::complex CIQBuffer[IQBURST]; - while(running) +#define IQBURST 1280 + bool stereo = true; + int SR = 48000; + int FifoSize = 512; + iqdmasync iqtest(Freq, SR, 14, FifoSize, MODE_IQ); + short IQBuffer[IQBURST * 2]; + std::complex CIQBuffer[IQBURST]; + while (running) { - int nbread=fread(IQBuffer,sizeof(short),IQBURST*2,iqfile); - if(nbread>0) + int nbread = fread(IQBuffer, sizeof(short), IQBURST * 2, iqfile); + if (nbread > 0) { - for(int i=0;i(IQBuffer[i*2]/32768.0,IQBuffer[i*2+1]/32768.0); - + + CIQBuffer[i] = std::complex(IQBuffer[i * 2] / 32768.0, IQBuffer[i * 2 + 1] / 32768.0); } - iqtest.SetIQSamples(CIQBuffer,nbread/2,1); + iqtest.SetIQSamples(CIQBuffer, nbread / 2, 1); } - else + else { printf("End of file\n"); - fseek ( iqfile , 0 , SEEK_SET ); - - + fseek(iqfile, 0, SEEK_SET); } } @@ -185,62 +176,54 @@ void SimpleTestFileIQ(uint64_t Freq) void SimpleTestbpsk(uint64_t Freq) { - - + clkgpio clk; clk.print_clock_tree(); - - int SR=250000; - int FifoSize=10000; - int NumberofPhase=2; - phasedmasync biphase(Freq,SR,NumberofPhase,14,FifoSize); + int SR = 250000; + int FifoSize = 10000; + int NumberofPhase = 2; + phasedmasync biphase(Freq, SR, NumberofPhase, 14, FifoSize); padgpio pad; - pad.setlevel(7); - int lastphase=0; - #define BURST_SIZE 100 + pad.setlevel(7); + int lastphase = 0; +#define BURST_SIZE 100 int PhaseBuffer[BURST_SIZE]; - while(running) + while (running) { - for(int i=0;i256) - { - int Index=testserial.GetUserMemIndex(); - - - for(int i=0;i 256) + { + int Index = testserial.GetUserMemIndex(); + + for (int i = 0; i < Available; i++) + { + + testserial.SetSample(Index + i, i); + } } } testserial.stop(); @@ -248,50 +231,48 @@ void SimpleTestSerial() void SimpleTestAm(uint64_t Freq) { - FILE *audiofile=NULL; - audiofile=fopen("../ssbaudio48.wav","rb"); - if (audiofile==NULL) printf("input file issue\n"); + FILE *audiofile = NULL; + audiofile = fopen("../ssbaudio48.wav", "rb"); + if (audiofile == NULL) + printf("input file issue\n"); + bool Stereo = true; + int SR = 48000; + int FifoSize = 512; + amdmasync amtest(Freq, SR, 14, FifoSize); - bool Stereo=true; - int SR=48000; - int FifoSize=512; - amdmasync amtest(Freq,SR,14,FifoSize); - - short AudioBuffer[128*2]; + short AudioBuffer[128 * 2]; - while(running) + while (running) { //usleep(FifoSize*1000000.0*1.0/(8.0*SR)); usleep(100); - int Available=amtest.GetBufferAvailable(); - if(Available>256) - { - int Index=amtest.GetUserMemIndex(); - int nbread=fread(AudioBuffer,sizeof(short),128*2,audiofile); - if(nbread>0) + int Available = amtest.GetBufferAvailable(); + if (Available > 256) + { + int Index = amtest.GetUserMemIndex(); + int nbread = fread(AudioBuffer, sizeof(short), 128 * 2, audiofile); + if (nbread > 0) { - - for(int i=0;i> (i)) & 1; + } + fprintf(stderr, "CRC=%x", checksumcalc); + return checksumcalc; +} + +void AlectoOOK(uint64_t Freq) +{ + + // 30ms max message + ookbursttiming ooksender(Freq, 1200000); + ookbursttiming::SampleOOKTiming Message[50 * 6 * 2]; // 46 exactly * 6 times + + ookbursttiming::SampleOOKTiming pulse; + pulse.value = 1; + pulse.duration = 450;//468 + + ookbursttiming::SampleOOKTiming Sync; + Sync.value = 0; + Sync.duration = 9000;//8956 + + ookbursttiming::SampleOOKTiming Zero; + Zero.value = 0; + Zero.duration = 2000;//2016 + + ookbursttiming::SampleOOKTiming One; + One.value = 0; + One.duration = 4000; + + size_t n = 0; + + //Widn average + unsigned char AlectoProtocol[] = {0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0}; + // Wind gust direction + unsigned char AlectoProtocol1[] = {0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + + // Temperature + unsigned char AlectoProtocolT[] = {0, 0, 0, 1, 0, 1, 0, 1, 0, 0, 0, 0, 1, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0}; + //ID + + /*AlectoProtocol[0]=1; + unsigned char LowBattery=0; + AlectoProtocol[8]=LowBattery; + AlectoProtocol[9]=1;//Wind + AlectoProtocol[10]=1;//Wind + */ + //AlectoProtocol[11] = 1; //Button generated + //AlectoProtocol[28] = 1; //Wind + //AlectoProtocol1[28] = 1; //Wind + + + //CheckSum n8 = ( 0xf - n0 - n1 - n2 - n3 - n4 - n5 - n6 - n7 ) & 0xf + + + for (size_t h = 0; h < 6; h++) + { + + + int direction = h*45; + for (size_t i = 0; i < 8; i++) + { + AlectoProtocol1[15 + i] = (direction >> (i)) & 0x1; + } + int Speed = h*10; + Speed=Speed/0.2; + for (size_t i = 0; i < 8; i++) + { + AlectoProtocol[24 + i] = (direction >> (i)) & 0x1; + AlectoProtocol1[24 + i] = (direction >> (i)) & 0x1; + } + SetCRC8(AlectoProtocol1); + SetCRC8(AlectoProtocol); + n=0; + for (size_t i = 0; i < 8; i++) + { + Message[n++] = pulse; + Message[n++] = One; + } + + Message[n++] = pulse; + Message[n++] = Sync; + for (size_t k = 0; k < 6; k++) + { + for (size_t i = 0; i < 36; i++) + { + Message[n++] = pulse; + if (k % 2 == 0) + { + if (AlectoProtocol[i] == 0) + Message[n++] = Zero; + else + Message[n++] = One; + } + else + { + if (AlectoProtocol1[i] == 0) + Message[n++] = Zero; + else + Message[n++] = One; + } + //Message[n++]=(AlectoProtocol[i]==0)?Zero:One; + } + + if (k < 5) // Not last one + { + for (size_t i = 0; i < 4; i++) + { + Message[n++] = pulse; + Message[n++] = Sync; + } + } + } + Message[n++] = pulse; + Message[n++] = Sync; + Message[n++] = pulse; + + fprintf(stderr, "N=%d\n", n); + ooksender.SendMessage(Message, n); + sleep(10); + + int Temperature = h * 100+200; + for (size_t i = 0; i < 12; i++) + { + AlectoProtocolT[12 + i] = (Temperature >> (i)) & 0x1; + } + //AlectoProtocolT[11] = 1; + //AlectoProtocolT[8] = 1; + SetCRC8(AlectoProtocolT); + fprintf(stderr, "Temperature sending\n"); + n = 0; + for (size_t i = 0; i < 8; i++) + { + Message[n++] = pulse; + Message[n++] = One; + } + + Message[n++] = pulse; + Message[n++] = Sync; + for (size_t k = 0; k < 6; k++) + { + for (size_t i = 0; i < 36; i++) + { + Message[n++] = pulse; + + if (AlectoProtocolT[i] == 0) + Message[n++] = Zero; + else + Message[n++] = One; + } + if (k < 5) // Not last one + { + //for (size_t i = 0; i < 4; i++) + { + Message[n++] = pulse; + Message[n++] = Sync; + } + } + } + Message[n++] = pulse; + Message[n++] = Sync; + Message[n++] = pulse; + + ooksender.SendMessage(Message, n); + //sleep(5); + sleep(20); + } } void SimpleTestBurstFsk(uint64_t Freq) { - + //int SR=40625; - int SR=1000; - float Deviation=2000; - int FifoSize=21; - fskburst fsktest(Freq,SR,Deviation,14,FifoSize); - - unsigned char TabSymbol[FifoSize]={0,1,0,1,0,1,0,1,0,0,1,1,0,0,1,1,0,1,0,1,0}; - - + int SR = 1000; + float Deviation = 2000; + int FifoSize = 21; + fskburst fsktest(Freq, SR, Deviation, 14, FifoSize); + + unsigned char TabSymbol[FifoSize] = {0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 0, 1, 0}; + //while(running) { - - fsktest.SetSymbols(TabSymbol,FifoSize); - - + + fsktest.SetSymbols(TabSymbol, FifoSize); } fsktest.stop(); } static void terminate(int num) { - running=false; - fprintf(stderr,"Caught signal - Terminating\n"); - + running = false; + fprintf(stderr, "Caught signal - Terminating\n"); } - -int main(int argc, char* argv[]) +int main(int argc, char *argv[]) { - - uint64_t Freq=144200000; - if(argc>1) - Freq=atof(argv[1]); - for (int i = 0; i < 64; i++) { - struct sigaction sa; + uint64_t Freq = 144200000; + if (argc > 1) + Freq = atof(argv[1]); - std::memset(&sa, 0, sizeof(sa)); - sa.sa_handler = terminate; - sigaction(i, &sa, NULL); - } + for (int i = 0; i < 64; i++) + { + struct sigaction sa; + + std::memset(&sa, 0, sizeof(sa)); + sa.sa_handler = terminate; + sigaction(i, &sa, NULL); + } //SimpleTest(Freq); //SimpleTestbpsk(Freq); @@ -372,7 +562,7 @@ int main(int argc, char* argv[]) //SimpleTestDMA(Freq); //SimpleTestAm(Freq); //SimpleTestOOK(Freq); - SimpleTestBurstFsk(Freq); - -} - + //SimpleTestBurstFsk(Freq); + //SimpleTestOOKTiming(Freq); + AlectoOOK(Freq); +} diff --git a/src/ookburst.cpp b/src/ookburst.cpp index 3cfce82..bbd8e7d 100644 --- a/src/ookburst.cpp +++ b/src/ookburst.cpp @@ -132,8 +132,8 @@ This program is free software: you can redistribute it and/or modify dma_cb_t *cbp=cbarray; cbp++; // Skip the first which is the Fiiling of Fifo - unsigned i=0; - for(i=0;i=m_MaxMessage) + { + fprintf(stderr,"OOK Message too long abort time(%d/%d)\n",n,m_MaxMessage); + return; + } + } + } + SetSymbols(ookrenderbuffer,n); + } diff --git a/src/ookburst.h b/src/ookburst.h index 438f3c2..92fa9bc 100644 --- a/src/ookburst.h +++ b/src/ookburst.h @@ -20,4 +20,21 @@ class ookburst:public bufferdma,public clkgpio,public pwmgpio,public pcmgpio void SetSymbols(unsigned char *Symbols,uint32_t Size); }; +class ookbursttiming:public ookburst +{ + protected: + unsigned char *ookrenderbuffer=nullptr; + size_t m_MaxMessage=0; + public: + typedef struct SampleOOKTiming + { + unsigned char value; + size_t duration; + } SampleOOKTiming; + + ookbursttiming(uint64_t TuneFrequency,size_t MaxMessageDuration); + ~ookbursttiming(); + void SendMessage(SampleOOKTiming *TabSymbols,size_t Size); +}; + #endif