📄 dsk_app.c
字号:
/* ====== RecieverEDMA ======
*
* ======== dsk_app.c ========
*
* Version 1.0 - Erik Bergenudd - Black Team
*
*
* Data transfer - Using OFDM
*
* Program flow
*
* From main() all system functionality of the board is initialized.
* After that the interupt routine takes over. The system Starts by recieving
* frames and search for synchronization sine signal. When it is recieved,
* it starts by process the following training data. From the training a bitallocation
* is calculated. The bitallocation is then send back via feedback channel.
*
* The edmaHwi() interrupt service routine is called when a buffer has been
* filled. It contains a state variable named pingOrPong that indicates
* whether the buffer is a PING or PONG buffer. dmaHwi switches the buffer
* state to the opposite buffer and calls the SWI thread processBuffer to
* process the audio data.
*
* Other Functions
*
* Please see the 6713 DSK help file under Software/Examples for more
* detailed information.
*/
#include "dsk_appcfg.h"
/*
* These are include files that support interfaces to BIOS and CSL modules
* used by the program.
*/
#include <std.h>
#include <swi.h>
#include <log.h>
#include <c6x.h>
#include <csl.h>
#include <csl_edma.h>
#include <csl_irq.h>
#include <csl_mcbsp.h>
//#include "sync.h"
/*
* The 6713 DSK Board Support Library is divided into several modules, each
* of which has its own include file. The file dsk6713.h must be included
* in every program that uses the BSL. This example also uses the
* DIP, LED and AIC23 modules.
*/
#include "dsk6713.h"
#include "dsk6713_led.h"
#include "dsk6713_dip.h"
#include "dsk6713_aic23.h"
#include <rtdx.h> /* defines RTDX target API calls */
#include "target.h" //./shared/target.h" /* defines TARGET_INITIALIZE() */
#include "./subfuntions/chan_estimation/chan_estimation.h"
#include "./subfuntions/dft_DSP/dft_DSP.h"
#include "./subfuntions/waterfilling/waterfill.h"
#include <math.h>
#include <stdio.h>
#include <C:/ti/c6700/dsplib/include/dspf_sp_convol.h>
/* Function prototypes */
void initIrq(void);
void initMcbsp(void);
void initEdma(void);
void copyData(const short *inbuf, short *outbuf, const short length);
void copy2Data(const short *inbuf, short *outbuf, const short length);
void copyData2x2(const float *inbuf, short *outbuf, const short length, const short offset);
void processBuffer(void);
void edmaHwi(void);
void processData(short *databuf, short length);
void framesync(short *databuf, short *startindex, short length);
void correlation(const short *reference, const short *input, const short len_ref, const short len_inp, int *res);
void trainingFrame(short *framebuffer);
void extractFrame(short *framebuffer, short length);
void demodulation_shart(const float *input, unsigned int *output);
void rtdx_send_result(void);
void trainingRun(short *framebuffer);
int sync(float *in , const float *training_seq);
void createTrig(short *databuf, const short length);
void qpsk(float *output, short *input);
short loopIndex;
#define UPFACTOR 4
#define FILTER_LENGTH 41
#define TRAININGSIZE 254
#define INTS_IN_FILE 16654
/* These are the values we are going to send to the host */
#define BUF_SIZE 200
// Frame control variables
#define FRAMELEN 1192
#define SUBCARSIZE 124
#define RECIEVELEN 1192
#define TOTFRAMES 50
/* declare a global output channel */
RTDX_CreateOutputChannel( ochan );
short b2; // Demodulation index variables for insertion to final data.
short b3;
short state_r;
short state_temp;
short nextFrame;
short train_count;
short frameNumber = 0;
short framebuffer[RECIEVELEN];
/* Variables used in channel estimation*/
float pilots_prod[2*SUBCARSIZE]; /* contains the product of the pilot symbols */
float alpha[2*SUBCARSIZE]; /* Re-part of saved value from previous calculations of chan. coeffs */
float beta[2*SUBCARSIZE]; /* Im-part of saved value from previous calculations of chan. coeffs */
float h_hat[2*SUBCARSIZE]; /* Vector that will contain the inverse of the channel estimation for the pilotes */
float gn[SUBCARSIZE]; // SNR
float lambda;
#pragma DATA_ALIGN(temp, 8);
#pragma DATA_ALIGN(workbuffer, 8);
float rx[RECIEVELEN+(FILTER_LENGTH-1)]; //+(FILTER_LENGTH-1)]; 2484+80 = 2564
float temp[RECIEVELEN+2*(FILTER_LENGTH-1)];
float workbuffer[256];
/* Constants for the buffered ping-pong transfer */
#define BUFFSIZE 2384 //4768 //4800 //4256
#define procBUFFSIZE (BUFFSIZE/2) //2048 //2128
#define PING 0
#define PONG 1
/*
* Data buffer declarations - the program uses four logical buffers of size
* BUFFSIZE, one ping and one pong buffer on both receive and transmit sides.
*/
short gBufferXmtPing[BUFFSIZE]; // Transmit PING buffer
short gBufferXmtPong[BUFFSIZE]; // Transmit PONG buffer
short gBufferRcvPing[BUFFSIZE]; // Receive PING buffer
short gBufferRcvPong[BUFFSIZE]; // Receive PONG buffer
#pragma DATA_SECTION(finalData,"mydata");
unsigned int finalData[INTS_IN_FILE];
unsigned int arraydata[BUF_SIZE];
short error_report[256];
short ProcessBuffer[procBUFFSIZE]; // Receive PONG buffer
short *transmit_Handle;
short RefIndex;
int treshold;
float noise_h;
EDMA_Handle hEdmaXmt; // EDMA channel handles
EDMA_Handle hEdmaReloadXmtPing;
EDMA_Handle hEdmaReloadXmtPong;
EDMA_Handle hEdmaRcv;
EDMA_Handle hEdmaReloadRcvPing;
EDMA_Handle hEdmaReloadRcvPong;
MCBSP_Handle hMcbsp1; // McBSP1 (codec data) handle
short gXmtChan; // TCC codes (see initEDMA())
short gRcvChan;
#pragma DATA_ALIGN(cos_filter,8);
float cos_filter[FILTER_LENGTH] = {
-0.0067, -0.0052, 0.0027, 0.0114, 0.0127, 0.0023, -0.0149,
-0.0262, -0.0191, 0.0074, 0.0386, 0.0502, 0.0248, -0.0326,
-0.0896, -0.0999, -0.0287, 0.1233, 0.3126, 0.4693, 0.5301,
0.4693, 0.3126, 0.1233, -0.0287, -0.0999, -0.0896, -0.0326,
0.0248, 0.0502, 0.0386, 0.0074, -0.0191, -0.0262, -0.0149,
0.0023, 0.0127, 0.0114, 0.0027, -0.0052, -0.0067 };
float training[TRAININGSIZE+34] = {
-3.048098, -0.7424511, -1.74085, -3.316733, -0.3423774, -1.807032, 0.7805056, 0.7707505,
2.499502, 2.878856, 0.3309775, 2.211342, 1.044806, -0.08486244, -0.8214678, -2.455833,
1.920416, 2.173422, -1.388459, -3.034301, 0.9010915, 0.3448012, 0.4353514, 0.2389492,
-1.776044, 0.3396091, -1.801926, 1.098975, -0.2901754, -1.326706, 0.379903, -2.059355,
0.5, -1.33557, -0.0442239, -0.6809366, 2.33654, -0.07288218, -0.08385682, 1.208217,
2.240007, -0.3266363, 1.029195, 0.2153398, 0.1446444, -0.62167, 2.915631, 1.061586,
-0.1982233, 1.85369, -1.68073, -0.8131676, -2.321988, -0.8608431, 0.05544913, 1.40112,
0.3220083, 0.3659526, -0.3339389, -0.9945482, -1.961081, 1.663205, -0.8007339, -0.6204649,
-1.883884, -1.033748, 0.559741, 2.847859, 0.3337603, 0.3512732, -2.039865, 0.06774627,
-0.01859483, 0.2938014, -0.1947467, 0.1604841, 0.2330566, -0.6857147, -0.05045086, 1.276375,
-0.4431081, 1.99545, 0.3399946, 1.869978, 0.130788, -1.348277, 0.104191, -2.136519,
-0.6283257, 1.042256, -1.540504, -1.196987, 0.6398896, -0.9071258, 2.187697, -3.416859,
0.5, 0.2274055, -0.2649262, 0.2094747, 1.8269, -0.2632722, -0.3294926, 0.7401766, -3.984663,
0.5856035, 2.679184, 0.6378283, 0.1782027, 0.1604342, 1.464542, 0.4525083, -0.5517768,
-1.378811, 1.354302, -1.963592, 0.9050447, -0.5149552, -2.33127, 2.021015, 0.03194213,
4.541542, 2.434346, 0.2732195, -1.291831, -0.1280512, 0.5815768, -0.7216167, 1.548098,
-0.8152939, 0.3539852, -0.05512881, -1.386539, -2.649823, -0.2685779, -1.816575, -0.8002441,
0.7144734, 1.355674, -0.1430551, -3.209305, 0.7113149, -0.3675202, 0.2996904, -0.9633094,
2.821777, -1.250847, -1.394303, -3.41579, 2.252138, 0.2425862, -0.3587947, -0.3396985,
0.005598843, 0.2947105, 0.5254463, -2.440575, 0.2963155, 1.859067, 0.3392568, -0.5,
0.0336557, 3.625386, -0.429329, 2.042737, 1.664062, -2.482112, 0.4634131, 1.054448,
-0.6122965, -0.2632658, 1.683899, -1.464424, -0.3428666, 1.474366, 1.443759, -0.1982233,
0.2322761, -0.2119309, 2.328827, 0.08739829, -2.002696, -1.60464, -1.518275, -0.2401377,
1.867274, 0.4840611, -0.03007737, 0.06779385, -1.155034, -2.401174, -0.5677059, -0.1161162,
-0.5775269, 0.21259, -0.5087352, -0.444409, 0.6759435, 0.9548871, -0.4884402, 0.2335506,
-1.350429, -0.2547555, 0.4089598, 1.138549, 1.223451, 0.2121537, -1.897528, -0.01399896,
0.6784465, -2.111959, -0.4023154, 0.4455613, -0.3440315, -0.4487179, -0.2591534, 3.244068,
0.3554666, -0.9550362, 0.5159285, -0.1162459, 1.042913, -0.003141522, 2.782112, -2.0,
-2.180369, 1.67679, -0.5684594, 0.87603, -0.771169, 0.8747162, 1.098674, -0.2240064,
0.8546914, -0.752109, -1.89873, 0.3486834, 0.3282532, 0.8489341, -1.872766, -0.5517768,
0.4782507, 2.571137, -1.315393, 0.02525237, -0.4748803, 2.48399, -1.267521, 0.3861873,
1.327345, -0.5118661, -0.2417687, 0.9780115, -1.665924, 0.1774734, 1.792059, -3.048098,
-0.7424511, -1.74085, -3.316733, -0.3423774, -1.807032, 0.7805056, 0.7707505, 2.499502,
2.878856, 0.3309775, 2.211342, 1.044806, -0.08486244, -0.8214678, -2.455833, 1.920416,
2.173422, -1.388459, -3.034301, 0.9010915, 0.3448012, 0.4353514, 0.2389492, -1.776044,
0.3396091, -1.801926, 1.098975, -0.2901754, -1.326706, 0.379903, -2.059355 };
/* Pre-generated sine wave data, 16-bit signed samples */
const short sinetable[ ] = {
0x0000, 0x10b4, 0x2120, 0x30fb, 0x3fff, 0x4dea, 0x5a81, 0x658b,
0x6ed8, 0x763f, 0x7ba1, 0x7ee5, 0x7ffd, 0x7ee5, 0x7ba1, 0x76ef,
0x6ed8, 0x658b, 0x5a81, 0x4dea, 0x3fff, 0x30fb, 0x2120, 0x10b4,
0x0000, 0xef4c, 0xdee0, 0xcf06, 0xc002, 0xb216, 0xa57f, 0x9a75,
0x9128, 0x89c1, 0x845f, 0x811b, 0x8002, 0x811b, 0x845f, 0x89c1,
0x9128, 0x9a76, 0xa57f, 0xb216, 0xc002, 0xcf06, 0xdee0, 0xef4c };
const float pilots[2*SUBCARSIZE]={ 1, -1, -1, 1, 1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, 1, -1,
1, -1, -1, 1, 1, 1, -1, 1,
-1, 1, -1, -1, -1, -1, -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 2, 2, 2, 2, 2, 2, 2, 2,
2, 2, 2 };
/*
* EDMA Config data structure
*/
/* Transmit side EDMA configuration */
EDMA_Config gEdmaConfigXmt = {
EDMA_FMKS(OPT, PRI, HIGH) | // Priority
EDMA_FMKS(OPT, ESIZE, 16BIT) | // Element size
EDMA_FMKS(OPT, 2DS, NO) | // 2 dimensional source?
EDMA_FMKS(OPT, SUM, INC) | // Src update mode
EDMA_FMKS(OPT, 2DD, NO) | // 2 dimensional dest
EDMA_FMKS(OPT, DUM, NONE) | // Dest update mode
EDMA_FMKS(OPT, TCINT, YES) | // Cause EDMA interrupt?
EDMA_FMKS(OPT, TCC, OF(0)) | // Transfer complete code
EDMA_FMKS(OPT, LINK, YES) | // Enable link parameters?
EDMA_FMKS(OPT, FS, NO), // Use frame sync?
(Uint32)&gBufferXmtPing, // Src address
EDMA_FMK (CNT, FRMCNT, NULL) | // Frame count
EDMA_FMK (CNT, ELECNT, BUFFSIZE), // Element count
EDMA_FMKS(DST, DST, OF(0)), // Dest address
EDMA_FMKS(IDX, FRMIDX, DEFAULT) | // Frame index value
EDMA_FMKS(IDX, ELEIDX, DEFAULT), // Element index value
EDMA_FMK (RLD, ELERLD, NULL) | // Reload element
EDMA_FMK (RLD, LINK, NULL) // Reload link
};
/* Receive side EDMA configuration */
EDMA_Config gEdmaConfigRcv = {
EDMA_FMKS(OPT, PRI, HIGH) | // Priority
EDMA_FMKS(OPT, ESIZE, 16BIT) | // Element size
EDMA_FMKS(OPT, 2DS, NO) | // 2 dimensional source?
EDMA_FMKS(OPT, SUM, NONE) | // Src update mode
EDMA_FMKS(OPT, 2DD, NO) | // 2 dimensional dest
EDMA_FMKS(OPT, DUM, INC) | // Dest update mode
EDMA_FMKS(OPT, TCINT, YES) | // Cause EDMA interrupt?
EDMA_FMKS(OPT, TCC, OF(0)) | // Transfer complete code
EDMA_FMKS(OPT, LINK, YES) | // Enable link parameters?
EDMA_FMKS(OPT, FS, NO), // Use frame sync?
EDMA_FMKS(SRC, SRC, OF(0)), // Src address
EDMA_FMK (CNT, FRMCNT, NULL) | // Frame count
EDMA_FMK (CNT, ELECNT, BUFFSIZE), // Element count
(Uint32)&gBufferRcvPing, // Dest address
EDMA_FMKS(IDX, FRMIDX, DEFAULT) | // Frame index value
EDMA_FMKS(IDX, ELEIDX, DEFAULT), // Element index value
EDMA_FMK (RLD, ELERLD, NULL) | // Reload element
EDMA_FMK (RLD, LINK, NULL) // Reload link
};
/* McBSP codec data channel configuration */
static MCBSP_Config mcbspCfg1 = {
MCBSP_FMKS(SPCR, FREE, NO) |
MCBSP_FMKS(SPCR, SOFT, NO) |
MCBSP_FMKS(SPCR, FRST, YES) |
MCBSP_FMKS(SPCR, GRST, YES) |
MCBSP_FMKS(SPCR, XINTM, XRDY) |
MCBSP_FMKS(SPCR, XSYNCERR, NO) |
MCBSP_FMKS(SPCR, XRST, YES) |
MCBSP_FMKS(SPCR, DLB, OFF) |
MCBSP_FMKS(SPCR, RJUST, RZF) |
MCBSP_FMKS(SPCR, CLKSTP, DISABLE) |
MCBSP_FMKS(SPCR, DXENA, OFF) |
MCBSP_FMKS(SPCR, RINTM, RRDY) |
MCBSP_FMKS(SPCR, RSYNCERR, NO) |
MCBSP_FMKS(SPCR, RRST, YES),
MCBSP_FMKS(RCR, RPHASE, SINGLE) |
MCBSP_FMKS(RCR, RFRLEN2, DEFAULT) |
MCBSP_FMKS(RCR, RWDLEN2, DEFAULT) |
MCBSP_FMKS(RCR, RCOMPAND, MSB) |
MCBSP_FMKS(RCR, RFIG, NO) |
MCBSP_FMKS(RCR, RDATDLY, 0BIT) |
MCBSP_FMKS(RCR, RFRLEN1, OF(1)) |
MCBSP_FMKS(RCR, RWDLEN1, 16BIT) |
MCBSP_FMKS(RCR, RWDREVRS, DISABLE),
MCBSP_FMKS(XCR, XPHASE, SINGLE) |
MCBSP_FMKS(XCR, XFRLEN2, DEFAULT) |
MCBSP_FMKS(XCR, XWDLEN2, DEFAULT) |
MCBSP_FMKS(XCR, XCOMPAND, MSB) |
MCBSP_FMKS(XCR, XFIG, NO) |
MCBSP_FMKS(XCR, XDATDLY, 0BIT) |
MCBSP_FMKS(XCR, XFRLEN1, OF(1)) |
MCBSP_FMKS(XCR, XWDLEN1, 16BIT) |
MCBSP_FMKS(XCR, XWDREVRS, DISABLE),
MCBSP_FMKS(SRGR, GSYNC, DEFAULT) |
MCBSP_FMKS(SRGR, CLKSP, DEFAULT) |
MCBSP_FMKS(SRGR, CLKSM, DEFAULT) |
MCBSP_FMKS(SRGR, FSGM, DEFAULT) |
MCBSP_FMKS(SRGR, FPER, DEFAULT) |
MCBSP_FMKS(SRGR, FWID, DEFAULT) |
MCBSP_FMKS(SRGR, CLKGDV, DEFAULT),
MCBSP_MCR_DEFAULT,
MCBSP_RCER_DEFAULT,
MCBSP_XCER_DEFAULT,
MCBSP_FMKS(PCR, XIOEN, SP) |
MCBSP_FMKS(PCR, RIOEN, SP) |
MCBSP_FMKS(PCR, FSXM, EXTERNAL) |
MCBSP_FMKS(PCR, FSRM, EXTERNAL) |
MCBSP_FMKS(PCR, CLKXM, INPUT) |
MCBSP_FMKS(PCR, CLKRM, INPUT) |
MCBSP_FMKS(PCR, CLKSSTAT, DEFAULT) |
MCBSP_FMKS(PCR, DXSTAT, DEFAULT) |
MCBSP_FMKS(PCR, FSXP, ACTIVEHIGH) |
MCBSP_FMKS(PCR, FSRP, ACTIVEHIGH) |
MCBSP_FMKS(PCR, CLKXP, FALLING) |
MCBSP_FMKS(PCR, CLKRP, RISING)
};
/* Codec configuration settings */
DSK6713_AIC23_Config config = { \
0x0017, /* 0 DSK6713_AIC23_LEFTINVOL Left line input channel volume */ \
0x0017, /* 1 DSK6713_AIC23_RIGHTINVOL Right line input channel volume */\
0x01f9, /* 2 DSK6713_AIC23_LEFTHPVOL Left channel headphone volume */ \
0x01f9, /* 3 DSK6713_AIC23_RIGHTHPVOL Right channel headphone volume */ \
0x0011, /* 4 DSK6713_AIC23_ANAPATH Analog audio path control */ \
0x0000, /* 5 DSK6713_AIC23_DIGPATH Digital audio path control */ \
0x0000, /* 6 DSK6713_AIC23_POWERDOWN Power down control */ \
0x0043, /* 7 DSK6713_AIC23_DIGIF Digital audio interface format */ \
0x001d, /* 8 DSK6713_AIC23_SAMPLERATE Sample rate control */ \
0x0001 /* 9 DSK6713_AIC23_DIGACT Digital interface activation */ \
};
/* --------------------------- main() function -------------------------- */
/*
* main() - The main user task. Performs application initialization and
* starts the data transfer.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -