aboutsummaryrefslogtreecommitdiff
path: root/stm32/src/adc_rec_usb.c
blob: e5ab084dcc74e83876b18cfd54b72f051b02d3ba (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
/*---------------------------------------------------------------------------*\

  FILE........: adc_rec_usb.c
  AUTHOR......: David Rowe
  DATE CREATED: Nov 2015

  Records a 16 kHz sample rate raw file from one of the ADC channels,
  which are connected to pins PA1 (ADC1) and PA2 (ADC2).  Uploads to the
  host PC via the STM32F4 USB port, which appears as /dev/ttyACM0.

  On the SM1000:
    ADC1 -> PA1 -> "from radio"
    ADC2 -> PA2 -> "mic amp"

  I used this to record:
    $ sudo dd if=/dev/ttyACM0 of=test.raw count=100

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

/*
  Copyright (C) 2015 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/>.
*/

#include <stdlib.h>
#include <math.h>
#include "stm32f4_adc.h"
#include "stm32f4_usb_vcp.h"
#include "sm1000_leds_switches.h"

#define  N  (ADC_BUF_SZ*6)

/* test tone parameters */

#define  FREQ 999.0    /* make sure no alignment with frame boundaries */
#define  FS   16000.0
#define  AMP  10000.0

extern int adc_overflow1;
extern int adc_overflow2;

int main(void){
    short  buf[N];
    #ifdef TEST_TONE
    float  phase = 0.0;
    float  sam;
    int    i;
    #endif

    usb_vcp_init();
    adc_open(ADC_FS_96KHZ, 4*N, NULL, NULL);
    sm1000_leds_switches_init();

    /* set up test buffer, lets us test USB comms indep of ADC, record to a file
       then play back/examine waveform to make sure no clicks */

    while(1) {
        while(adc1_read(buf, N) == -1);

        #ifdef TEST_TONE
        for(i=0; i<N; i++) {
            phase += 2.0*M_PI*FREQ/FS;
            phase -= 2.0*M_PI*floor(phase/(2.0*M_PI));
            sam = AMP*cos(phase);
            buf[i] = (short)sam;
        }
        #endif

        led_pwr(1);
        VCP_send_buffer((uint8_t*)buf, sizeof(buf));
        led_pwr(0);
    }
}