aboutsummaryrefslogtreecommitdiffstats
path: root/src/fsm/psk31.c
blob: 6eafa53e914ef97db7f4f21d9165c6211562e39c (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2015 Matthias P. Braendli
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
*/

#include "psk31.h"
#include "common.h"
#include "audio.h"
#include <string.h>
#include "arm_math.h"

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "semphr.h"

/* PSK31 generator
 *
 * Concept:
 *
 * +----------------------+                       +--------------+
 * | psk31_push_message() | -> psk31_msg_queue -> | psk31_task() |
 * +----------------------+                       +--------------+
 *                                                       |
 *      _________________________________________________/
 *     /
 *     |
 *    \|/
 *     V
 *
 * psk31_audio_queue
 *
 * The psk31_fill_buffer() function can be called to fetch audio from the audio_queue
 */

#define PSK31_MAX_MESSAGE_LEN 10240
#define PHASE_BUFFER_SIZE (20 + PSK31_MAX_MESSAGE_LEN + 20)

struct psk31_out_message_s {
    // Contains a sequence of ones and zeros corresponding to
    // the BPSK31 phase
    char    phase_buffer[PHASE_BUFFER_SIZE];
    size_t  phase_buffer_end;

    int     freq; // Audio frequency for signal center
};

// The queue contains above structs
QueueHandle_t psk31_msg_queue;

// Queue that contains audio data
QueueHandle_t psk31_audio_queue;
static int    psk31_samplerate;

static int    psk31_transmit_ongoing;

static void psk31_task(void *pvParameters);
static void psk31_str_to_bits(const char* instr, char* outbits);


void psk31_init(unsigned int samplerate)
{
    psk31_samplerate = samplerate;
    psk31_transmit_ongoing = 0;

    psk31_msg_queue = xQueueCreate(15, sizeof(struct psk31_out_message_s));
    if (psk31_msg_queue == 0) {
        while(1); /* fatal error */
    }

    psk31_audio_queue = xQueueCreate(2, AUDIO_BUF_LEN * sizeof(int16_t));
    if (psk31_audio_queue == 0) {
        while(1); /* fatal error */
    }

    xTaskCreate(
            psk31_task,
            "TaskPSK31",
            8*configMINIMAL_STACK_SIZE,
            (void*) NULL,
            tskIDLE_PRIORITY + 2UL,
            NULL);
}

int psk31_push_message(const char* text, int frequency)
{
    if (strlen(text) > PSK31_MAX_MESSAGE_LEN) {
        return 0;
    }

    struct psk31_out_message_s msg;
    msg.phase_buffer_end = 0;
    msg.freq = frequency;

    psk31_str_to_bits(text, msg.phase_buffer);

    xQueueSendToBack(psk31_msg_queue, &msg, portMAX_DELAY);

    return 1;
}

// Write the waveform into the buffer (stereo)
size_t psk31_fill_buffer(int16_t *buf, size_t bufsize);

// Return 1 if the psk31 generator has completed transmission
int psk31_busy(void);

static int16_t psk31_audio_buf[AUDIO_BUF_LEN];
static void psk31_task(void *pvParameters)
{
    struct psk31_out_message_s psk31_fill_msg_current;

    float nco_phase = 0.0f;
    float ampl = 0.0f;

    int   buf_pos = 0;

    while (1) {
        int status = xQueueReceive(psk31_msg_queue, &psk31_fill_msg_current, portMAX_DELAY);
        if (status == pdTRUE) {

            psk31_transmit_ongoing = 1;

            /* BPSK31 is at 31.25 symbols per second. */
            const int samples_per_symbol = psk31_samplerate * 100 / 3125;

            // Angular frequency of NCO
            const float omega = 2.0f * FLOAT_PI * psk31_fill_msg_current.freq /
                (float)psk31_samplerate;

            int current_psk_phase = 0;

            for (int i = 0; i < psk31_fill_msg_current.phase_buffer_end; i++) {
                for (int t = 0; t < samples_per_symbol; t++) {
                    int16_t s = 0;

                    if (psk31_fill_msg_current.phase_buffer[i]) {
                        ampl = 32000.0f;
                    }
                    else {
                        ampl =
                            (float)current_psk_phase *
                            32000.0f *
                            arm_cos_f32(
                                    FLOAT_PI*(float)t/(float)samples_per_symbol);
                    }

                    nco_phase += omega;
                    if (nco_phase > FLOAT_PI) {
                        nco_phase -= 2.0f * FLOAT_PI;
                    }

                    s = ampl * arm_sin_f32(nco_phase);

                    if (buf_pos == AUDIO_BUF_LEN) {
                        xQueueSendToBack(psk31_audio_queue, &psk31_audio_buf, portMAX_DELAY);
                        buf_pos = 0;
                    }
                    psk31_audio_buf[buf_pos++] = s;

                    // Stereo
                    if (buf_pos == AUDIO_BUF_LEN) {
                        xQueueSendToBack(psk31_audio_queue, &psk31_audio_buf, portMAX_DELAY);
                        buf_pos = 0;
                    }
                    psk31_audio_buf[buf_pos++] = s;
                }


                if (! psk31_fill_msg_current.phase_buffer[i]) {
                    current_psk_phase *= -1;
                }
            }

            // We have completed this message

            psk31_transmit_ongoing = 0;
        }
    }
}

/*
 * Turn a null terminated ASCII string into a null terminated
 * string of '0's and '1's representing the PSK31 varicode for the input.
 *
 * outstr must be at least size 20 + strlen(instr)*12 + 20 to accomodate
 * the header and tail
 */
static void psk31_str_to_bits(const char* instr, char* outbits)
{
    int i=0, j, k;

    /* Header of 0s */
    for (j=0; j < 20; j++) {
        outbits[i++] = '0';
    }

    /* Encode the message, with 00 between letters */
    for (j=0; j < strlen(instr); j++) {
        const char* varicode_bits = psk31_varicode[(int)instr[j]];
        for(k=0; k < strlen(varicode_bits); k++) {
            outbits[i++] = varicode_bits[k];
        }
        outbits[i++] = '0';
        outbits[i++] = '0';
    }

    /* Tail of 0s */
    for (j=0; j < 20; j++) {
        outbits[i++] = '0';
    }

    /* NULL terminate */
    outbits[i] = 0;
}

#if 0
/*
 * Turn a null terminated string `bits` containing '0's and '1's
 * into `outlen` IQ samples for BPSK, `outbuf`.
 * Note that `outlen` is set to the number of IQ samples, i.e. half the
 * number of bytes in `outbuf`.
 * Allocates memory (possibly lots of memory) for the IQ samples, which
 * should be freed elsewhere.
 * Modulation:
 * '0': swap phase, smoothed by a cosine
 * '1': maintain phase
 * Output: I carries data, Q constantly 0
 */
void bits_to_iq(char* bits, uint8_t** outbuf, int* outlen)
{
    *outlen = strlen(bits) * 256000 * 2;
    *outbuf = malloc(*outlen);
    int8_t *buf = (int8_t*)(*outbuf);
    if(*outbuf == NULL) {
        fprintf(stderr, "Could not allocate memory for IQ buffer\n");
        exit(EXIT_FAILURE);
    }

    int i, j, phase = 1;
    for(i=0; i<strlen(bits); i++) {
        if(bits[i] == '1') {
            for(j=0; j<256000; j++) {
                buf[i*256000*2 + 2*j] = phase*50;
                buf[i*256000*2 + 2*j + 1] = 0;
            }
        } else {
            for(j=0; j<256000; j++) {
                buf[i*256000*2 + 2*j] = phase *
                    (int8_t)(50.0f * cosf(M_PI*(float)j/256000.0f));
                buf[i*256000*2 + 2*j + 1] = 0;
            }
            phase *= -1;
        }
    }
}
#endif