aboutsummaryrefslogtreecommitdiff
path: root/stm32/unittest/src/tst_ofdm_mod.c
blob: 77b2e3702a0bd97f0bafb48d08c8cd2f03c9692c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*---------------------------------------------------------------------------*\

  FILE........: tst_ofdm_mod.c
  AUTHOR......: David Rowe, Don Reid
  DATE CREATED: 25 June 2018

  Test and profile OFDM modulation on the STM32F4.

\*---------------------------------------------------------------------------*/

/*
  Copyright (C) 2014 David Rowe

  All rights reserved.

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Lesser General Public License version 2.1, as
  published by the Free Software Foundation.  This program is
  distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or
  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
  License for more details.

  You should have received a copy of the GNU Lesser General Public License
  along with this program; if not, see <http://www.gnu.org/licenses/>.
*/

/* This is a unit test implementation of the OFDM Mod function.
 *
 * Typical run:

    ofdm_gen_test_bits stm_in.raw 6 --rand 

    ofdm_mod stm_in.raw ref_mod_out.raw

    echo "00000000" > stm_cfg.txt

    <Load stm32 and run>

    compare_ints -s -b2 ref_mod_out.raw mod.raw

    ofdm_demod ref_mod_out.raw ref_ofdm_demod.raw --testframes
    ofdm_demod mod.raw stm_demod.raw --testframes

 * For LDPC use:

    ofdm_gen_test_bits stm_in.raw 6 --rand --ldpc

    ofdm_mod stm_in.raw ref_mod_out.raw --ldpc

    echo "00100000" > stm_cfg.txt

    <Load stm32 and run>

    ofdm_demod ref_mod_out.raw ref_ofdm_demod.raw --ldpc --testframes
    ofdm_demod mod.raw stm_demod.raw --ldpc --testframes

 */

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>

#include "semihosting.h"
#include "codec2_ofdm.h"
#include "ofdm_internal.h"
#include "ldpc_codes.h"
#include "interldpc.h"
#include "gp_interleaver.h"

#include "stm32f4xx_conf.h"
#include "stm32f4xx.h"
#include "machdep.h"

#include "debug_alloc.h"

int main(int argc, char *argv[]) {
    struct OFDM *ofdm;
    FILE        *fcfg;
    struct LDPC  ldpc;

    // Test configuration, read from stm_cfg.txt
    int          config_verbose;
//    int          config_testframes;
    int          config_ldpc_en;
//    int          config_log_payload_syms;
    int          config_profile;

    int          Nbitsperframe, Nsamperframe;
    int          frame = 0;
    int          i;

    semihosting_init();

    printf("OFDM_mod test and profile\n");

    // Read configuration - a file of '0' or '1' characters
    char config[8];
    fcfg = fopen("stm_cfg.txt", "r");
    if (fcfg == NULL) {
        fprintf(stderr, "Error opening config file\n");
        exit(1);
    }
    if (fread(&config[0], 1, 8, fcfg) != 8) {
        fprintf(stderr, "Error reading config file\n");
        exit(1);
    }
    config_verbose = config[0] - '0';
//    config_testframes = config[1] - '0';
    config_ldpc_en = config[2] - '0';
//    config_log_payload_syms = config[3] - '0';
    config_profile = config[4] - '0';
    fclose(fcfg);

    PROFILE_VAR(ofdm_mod_start);
    if (config_profile) machdep_profile_init();

    struct OFDM_CONFIG *ofdm_config;

    ofdm = ofdm_create(NULL);
    assert(ofdm != NULL);

    /* Get a copy of the actual modem config */
    ofdm_config = ofdm_get_config_param(ofdm);

    ldpc_codes_setup(&ldpc, "HRA_112_112");

    Nbitsperframe = ofdm_get_bits_per_frame(ofdm);
    int Ndatabitsperframe;
    if (config_ldpc_en) {
        Ndatabitsperframe = ldpc.data_bits_per_frame;
    } else {
        Ndatabitsperframe = ofdm_get_bits_per_frame(ofdm) - ofdm->nuwbits - ofdm->ntxtbits;
    }

    Nsamperframe = ofdm_get_samples_per_frame(ofdm);
//    int ofdm_nuwbits = (ofdm_config->ns - 1) * ofdm_config->bps - ofdm_config->txtbits;

    if (config_verbose) {
        ofdm_set_verbose(ofdm, config_verbose);
        fprintf(stderr, "Nsamperframe: %d, Nbitsperframe: %d \n", Nsamperframe, Nbitsperframe);
    }

    int ofdm_ntxtbits =  ofdm_config->txtbits;

    uint8_t tx_bits_char[Ndatabitsperframe];
    int16_t tx_scaled[Nsamperframe];
    uint8_t txt_bits_char[ofdm_ntxtbits];

    for(i=0; i< ofdm_ntxtbits; i++) {
        txt_bits_char[i] = 0;
    }    
    
    if (config_verbose) {
	    ofdm_print_info(ofdm);
    }

    int sin = open("stm_in.raw", O_RDONLY);
    if (sin < 0) {
        printf("Error opening input file\n");
        exit(1);
    }

    int sout = open("mod.raw", O_WRONLY|O_TRUNC|O_CREAT, 0666);
    if (sout < 0) {
        printf("Error opening output file\n");
        exit(1);
    }

    while (read(sin, tx_bits_char, sizeof(char) * Ndatabitsperframe) == Ndatabitsperframe) {
        fprintf(stderr, "Frame %d\n", frame);

        if (config_profile) { PROFILE_SAMPLE(ofdm_mod_start); }

            if (config_ldpc_en) {

                complex float tx_sams[Nsamperframe];
                ofdm_ldpc_interleave_tx(ofdm, &ldpc, tx_sams, tx_bits_char, txt_bits_char);

                for(i=0; i<Nsamperframe; i++) {
                    tx_scaled[i] = crealf(tx_sams[i]);
                }

             } else { // !config_ldpc_en

                uint8_t tx_frame[Nbitsperframe];
                ofdm_assemble_qpsk_modem_packet(ofdm, tx_frame, tx_bits_char, txt_bits_char);

                int tx_bits[Nbitsperframe];
                for(i=0; i<Nbitsperframe; i++) {
                    tx_bits[i] = tx_frame[i];
                }

	        if (config_verbose >=3) {
                    fprintf(stderr, "\ntx_bits:\n");
                    for (i = 0; i < Nbitsperframe; i++) {
                        fprintf(stderr, "  %3d %8d\n", i, tx_bits[i]);
                    }
                }

                COMP tx_sams[Nsamperframe];
                ofdm_mod(ofdm, tx_sams, tx_bits);

	        if (config_verbose >=3) {
                    fprintf(stderr, "\ntx_sams:\n");
                    for (i = 0; i < Nsamperframe; i++) {
                        fprintf(stderr, "  %3d % f\n", i, (double)tx_sams[i].real);
                    }
                }

                for(i=0; i<Nsamperframe; i++) {
                    tx_scaled[i] = tx_sams[i].real;
                }
            }

        if (config_profile) PROFILE_SAMPLE_AND_LOG2(ofdm_mod_start, "  ofdm_mod");

        write(sout, tx_scaled, sizeof(int16_t) * Nsamperframe);

        frame ++;

    }  // while (fread(...

    close(sin);
    close(sout);

    if (config_verbose)
        printf("%d frames processed\n", frame);

    if (config_profile) {
        printf("\nStart Profile Data\n");
        machdep_profile_print_logged_samples();
        printf("End Profile Data\n");
        }

    printf("\nEnd of Test\n");
    fclose(stdout);
    fclose(stderr);

    return 0;
}

/* vi:set ts=4 et sts=4: */