From ae83ff29dd0786ce0e3bfa435e5e559fbc6f724e Mon Sep 17 00:00:00 2001 From: drowe67 Date: Tue, 12 Aug 2014 04:12:43 +0000 Subject: [PATCH] various tweaks and unit tests as SM1000 is brought up. ADC and DAC noise not too bad on loopbakc tests. Currently bombs when freedv_api called git-svn-id: https://svn.code.sf.net/p/freetel/code@1793 01035d8c-6547-0410-b346-abe4f91aad63 --- codec2-dev/stm32/Makefile | 34 +++++++++++++--- codec2-dev/stm32/inc/debugblinky.h | 33 ++++++++++++++++ codec2-dev/stm32/inc/stm32f4_adc.h | 4 +- codec2-dev/stm32/src/adc_rec.c | 6 +-- codec2-dev/stm32/src/adcdac_ut.c | 61 +++++++++++++++++++++++++++++ codec2-dev/stm32/src/dac_play.c | 3 +- codec2-dev/stm32/src/dac_ut.c | 1 + codec2-dev/stm32/src/debugblinky.c | 45 +++++++++++++++++++++ codec2-dev/stm32/src/power_ut.c | 2 +- codec2-dev/stm32/src/sm1000_main.c | 63 ++++++++++++++++++++++++------ codec2-dev/stm32/src/stm32f4_adc.c | 9 ++++- codec2-dev/stm32/src/stm32f4_dac.c | 13 +++++- 12 files changed, 247 insertions(+), 27 deletions(-) create mode 100644 codec2-dev/stm32/inc/debugblinky.h create mode 100644 codec2-dev/stm32/src/adcdac_ut.c create mode 100644 codec2-dev/stm32/src/debugblinky.c diff --git a/codec2-dev/stm32/Makefile b/codec2-dev/stm32/Makefile index edc5427f..dcf055a2 100644 --- a/codec2-dev/stm32/Makefile +++ b/codec2-dev/stm32/Makefile @@ -13,7 +13,7 @@ SIZE=$(BINPATH)/arm-none-eabi-size ################################################### -CFLAGS = -std=gnu99 -O3 --param max-unroll-times=200 -g -Wall -Tstm32_flash.ld -DSTM32F4XX -DCORTEX_M4 +CFLAGS = -std=gnu99 -g -Wall -Tstm32_flash.ld -DSTM32F4XX -DCORTEX_M4 CFLAGS += -mlittle-endian -mthumb -mthumb-interwork -nostartfiles -mcpu=cortex-m4 ifeq ($(FLOAT_TYPE), hard) @@ -109,7 +109,7 @@ OBJS = $(SRCS:.c=.o) ################################################### -all: libstm32f4.a codec2_profile.elf fft_test.elf dac_ut.elf dac_play.elf adc_rec.elf pwm_ut.elf power_ut.elf fdmdv_profile.elf sm1000_leds_switches_ut.elf sm1000.elf +all: libstm32f4.a codec2_profile.elf fft_test.elf dac_ut.elf dac_play.elf adc_rec.elf pwm_ut.elf fdmdv_profile.elf sm1000_leds_switches_ut.elf sm1000.elf adcdac_ut.elf dl/$(PERIPHLIBZIP): mkdir -p dl @@ -146,6 +146,7 @@ DAC_UT_SRCS=\ src/dac_ut.c \ ../src/fifo.c \ src/stm32f4_dac.c \ +src/debugblinky.c \ src/system_stm32f4xx.c \ src/startup_stm32f4xx.s \ src/init.c @@ -153,11 +154,25 @@ src/init.c dac_ut.elf: $(DAC_UT_SRCS) $(CC) $(CFLAGS) -O0 $^ -o $@ $(LIBPATHS) $(LIBS) +ADCDAC_UT_SRCS=\ +src/adcdac_ut.c \ +../src/fifo.c \ +src/stm32f4_dac.c \ +src/stm32f4_adc.c \ +src/debugblinky.c \ +src/system_stm32f4xx.c \ +src/startup_stm32f4xx.s \ +src/init.c + +adcdac_ut.elf: $(ADCDAC_UT_SRCS) + $(CC) $(CFLAGS) -O0 $^ -o $@ $(LIBPATHS) $(LIBS) + DAC_PLAY_SRCS=\ src/dac_play.c \ ../src/fifo.c \ gdb_stdio.c \ src/stm32f4_dac.c \ +src/debugblinky.c \ src/system_stm32f4xx.c \ src/startup_stm32f4xx.s \ src/init.c @@ -170,6 +185,7 @@ src/adc_rec.c \ ../src/fifo.c \ gdb_stdio.c \ src/stm32f4_adc.c \ +src/debugblinky.c \ src/system_stm32f4xx.c \ src/startup_stm32f4xx.s \ src/init.c @@ -193,6 +209,7 @@ gdb_stdio.c \ ../src/fifo.c \ src/stm32f4_adc.c \ src/stm32f4_dac.c \ +src/debugblinky.c \ src/system_stm32f4xx.c \ src/startup_stm32f4xx.s \ src/init.c \ @@ -230,16 +247,21 @@ SM1000_SRCS=\ src/sm1000_main.c \ src/sm1000_leds_switches.c \ ../src/fifo.c \ -src/stm32f4_adc.c \ -src/stm32f4_dac.c \ +src/debugblinky.c \ src/system_stm32f4xx.c \ src/startup_stm32f4xx.s \ src/init.c SM1000_SRCS += $(CODEC2_SRCS) -sm1000.elf: $(SM1000_SRCS) - $(CC) $(CFLAGS) $^ -o $@ $(LIBPATHS) $(LIBS) +src/stm32f4_dac.o: src/stm32f4_dac.c + $(CC) $(CFLAGS) $^ -c -o $@ + +src/stm32f4_adc.o: src/stm32f4_adc.c + $(CC) $(CFLAGS) $^ -c -o $@ + +sm1000.elf: $(SM1000_SRCS) src/stm32f4_dac.o src/stm32f4_adc.o + $(CC) $(CFLAGS) -O3 $^ -o $@ $(LIBPATHS) $(LIBS) clean: rm -f *.o diff --git a/codec2-dev/stm32/inc/debugblinky.h b/codec2-dev/stm32/inc/debugblinky.h new file mode 100644 index 00000000..0561272c --- /dev/null +++ b/codec2-dev/stm32/inc/debugblinky.h @@ -0,0 +1,33 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: debugblinky.h + AUTHOR......: David Rowe + DATE CREATED: 12 August 2014 + + Configures GPIO pins used for debug blinkies + +\*---------------------------------------------------------------------------*/ + +/* + 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 . +*/ + +#ifndef __DEBUGBLINKY__ +#define __DEBUGBLINKY__ + +void init_debug_blinky(void); + +#endif diff --git a/codec2-dev/stm32/inc/stm32f4_adc.h b/codec2-dev/stm32/inc/stm32f4_adc.h index 17c318e0..d819fa72 100644 --- a/codec2-dev/stm32/inc/stm32f4_adc.h +++ b/codec2-dev/stm32/inc/stm32f4_adc.h @@ -28,7 +28,9 @@ #ifndef __STM32F4_ADC__ #define __STM32F4_ADC__ -void adc_open(void); +#define ADC_BUF_SZ 320 + +void adc_open(int fifo_sz); int adc1_read(short buf[], int n); /* ADC1 Pin PA1 */ int adc2_read(short buf[], int n); /* ADC2 Pin PA2 */ diff --git a/codec2-dev/stm32/src/adc_rec.c b/codec2-dev/stm32/src/adc_rec.c index c20516d9..f1a17cbf 100644 --- a/codec2-dev/stm32/src/adc_rec.c +++ b/codec2-dev/stm32/src/adc_rec.c @@ -30,7 +30,7 @@ #include "gdb_stdio.h" #define REC_TIME_SECS 10 -#define N 2000 +#define N (ADC_BUF_SZ*6) #define FS 16000 int main(void){ @@ -38,7 +38,7 @@ int main(void){ FILE *frec; int i, bufs; - adc_open(); + adc_open(2*N); frec = fopen("stm_out.raw", "wb"); if (frec == NULL) { @@ -51,7 +51,7 @@ int main(void){ for(i=0; i. +*/ + +#include +#include "stm32f4_dac.h" +#include "stm32f4_adc.h" + +#define SINE_SAMPLES 32 + + +/* 32 sample sine wave which at Fs=16kHz will be 500Hz. Note samples + are 16 bit 2's complement, the DAC driver convertsto 12 bit + unsigned. */ + +short aSine[] = { + -16, 6384, 12528, 18192, 23200, 27232, 30256, 32128, + 32752, 32128, 30256, 27232, 23152, 18192, 12528, 6384, + -16, -6416, -12560, -18224, -23184, -27264, -30288, -32160, + -32768, -32160, -30288, -27264, -23184, -18224, -12560, -6416 +}; + +int main(void) { + short buf[SINE_SAMPLES]; + + dac_open(4*DAC_BUF_SZ); + adc_open(4*ADC_BUF_SZ); + + while (1) { + + /* keep DAC FIFOs topped up */ + + while(adc1_read(buf, SINE_SAMPLES) == -1); + dac2_write(buf, SINE_SAMPLES); + } + +} + diff --git a/codec2-dev/stm32/src/dac_play.c b/codec2-dev/stm32/src/dac_play.c index a8a1565f..6a6c63e9 100644 --- a/codec2-dev/stm32/src/dac_play.c +++ b/codec2-dev/stm32/src/dac_play.c @@ -4,7 +4,7 @@ AUTHOR......: David Rowe DATE CREATED: 1 June 2013 - Plays a 16 kHz sample rate raw file to the STM32F4 pin PA5. + Plays a 16 kHz sample rate raw file to the STM32F4 DACs. \*---------------------------------------------------------------------------*/ @@ -47,6 +47,7 @@ int main(void) { printf("Starting!\n"); while(fread(buf, sizeof(short), N, fplay) == N) { + while(dac1_write(buf, N) == -1); while(dac2_write(buf, N) == -1); } diff --git a/codec2-dev/stm32/src/dac_ut.c b/codec2-dev/stm32/src/dac_ut.c index 40b13a44..bfe9def1 100644 --- a/codec2-dev/stm32/src/dac_ut.c +++ b/codec2-dev/stm32/src/dac_ut.c @@ -44,6 +44,7 @@ short aSine[] = { }; int main(void) { + dac_open(4*DAC_BUF_SZ); while (1) { diff --git a/codec2-dev/stm32/src/debugblinky.c b/codec2-dev/stm32/src/debugblinky.c new file mode 100644 index 00000000..7e38fd17 --- /dev/null +++ b/codec2-dev/stm32/src/debugblinky.c @@ -0,0 +1,45 @@ +/*---------------------------------------------------------------------------*\ + + FILE........: debugblinky.c + AUTHOR......: David Rowe + DATE CREATED: 12 August 2014 + + Configures GPIO pins used for debug blinkies + +\*---------------------------------------------------------------------------*/ + +/* + 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 . +*/ + +#include "stm32f4xx.h" + +void init_debug_blinky(void) { + GPIO_InitTypeDef GPIO_InitStruct; + + /* PE0-3 used to indicate activity */ + + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); + + GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3; + GPIO_InitStruct.GPIO_Mode = GPIO_Mode_OUT; + GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; + GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; + GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(GPIOE, &GPIO_InitStruct); + +} + diff --git a/codec2-dev/stm32/src/power_ut.c b/codec2-dev/stm32/src/power_ut.c index 416fc956..bc701d54 100644 --- a/codec2-dev/stm32/src/power_ut.c +++ b/codec2-dev/stm32/src/power_ut.c @@ -121,7 +121,7 @@ int main(int argc, char *argv[]) { SystemInit(); gpio_init(); machdep_timer_init (); - adc_open(); + adc_open(4*DAC_BUF_SZ); dac_open(4*DAC_BUF_SZ); printf("Starting power_ut\n"); diff --git a/codec2-dev/stm32/src/sm1000_main.c b/codec2-dev/stm32/src/sm1000_main.c index ed930fdd..138d84ff 100644 --- a/codec2-dev/stm32/src/sm1000_main.c +++ b/codec2-dev/stm32/src/sm1000_main.c @@ -28,48 +28,85 @@ #include "stm32f4_adc.h" #include "stm32f4_dac.h" #include "freedv_api.h" +#include "codec2_fdmdv.h" #include "sm1000_leds_switches.h" +#include + +#define FREEDV_NSAMPLES_16K (2*FREEDV_NSAMPLES) int main(void) { struct freedv *f; - short buf[FREEDV_NSAMPLES]; - int nin, nout; + float adc16k[FDMDV_OS_TAPS_16K+FREEDV_NSAMPLES_16K]; + float adc8k[FREEDV_NSAMPLES]; + float dac8k[FDMDV_OS_TAPS_8K+FREEDV_NSAMPLES]; + float dac16k[FREEDV_NSAMPLES_16K]; + short buf[FREEDV_NSAMPLES_16K]; + + int nin, nout, i; /* init all the drivers for various peripherals */ - sm1000_leds_switches_init(); + //sm1000_leds_switches_init(); dac_open(4*DAC_BUF_SZ); - adc_open(); - f = freedv_open(FREEDV_MODE_1600); + adc_open(4*ADC_BUF_SZ); + //f = freedv_open(FREEDV_MODE_1600); /* LEDs into a known state */ - led_pwr(1); led_ptt(0); led_rt(0); led_err(0); + //led_pwr(1); led_ptt(0); led_rt(0); led_err(0); /* TODO: [ ] UT analog interfaces from file IO [ ] UTs for simultaneous tx & rx on analog interfaces [ ] measure CPU load of various parts with a blinky + [ ] ADC and DAC drivers + [ ] rate conversion [ ] detect program assert type errors with a blinky [ ] timer tick function to measure 10ms-ish type times [ ] switch debouncing? [ ] light led with bit errors + [ ] 16 to 8 kHz rate conversion */ - while(1) { + /* clear filter memories */ - if(switch_ptt()) { + for(i=0; iODR = (1 << 3); + for(i=0; iODR &= ~(1 << 3); + + //led_ptt(1); led_rt(0); led_err(0); } + } else { @@ -80,7 +117,7 @@ int main(void) { nin = freedv_nin(f); f->total_bit_errors = 0; - if (adc1_read(buf, nin) == nin) { + if (adc1_read(buf, nin) == 0) { nout = freedv_rx(f, buf, buf); dac2_write(buf, nout); led_ptt(0); led_rt(f->fdmdv_stats.sync); led_err(f->total_bit_errors); diff --git a/codec2-dev/stm32/src/stm32f4_adc.c b/codec2-dev/stm32/src/stm32f4_adc.c index 07cffa68..7790a4c2 100644 --- a/codec2-dev/stm32/src/stm32f4_adc.c +++ b/codec2-dev/stm32/src/stm32f4_adc.c @@ -35,6 +35,7 @@ #include "codec2_fifo.h" #include "stm32f4_adc.h" +#include "debugblinky.h" struct FIFO *adc1_fifo; unsigned short adc_buf[ADC_BUF_SZ]; @@ -56,6 +57,7 @@ void adc_open(int fifo_sz) { tim2_config(); adc_configure(); + init_debug_blinky(); } /* n signed 16 bit samples in buf[] if return != -1 */ @@ -135,7 +137,8 @@ void adc_configure(){ // Select the channel to be read from - ADC_RegularChannelConfig(ADCx,ADC_Channel_3,1,ADC_SampleTime_144Cycles); + ADC_RegularChannelConfig(ADCx,ADC_Channel_2,1,ADC_SampleTime_144Cycles); + //ADC_VBATCmd(ENABLE); /* DMA configuration **************************************/ @@ -195,6 +198,8 @@ void DMA2_Stream0_IRQHandler(void) { int i, sam; short signed_buf[ADC_BUF_SZ/2]; + GPIOE->ODR = (1 << 0); + /* Half transfer interrupt */ if(DMA_GetITStatus(DMA2_Stream0, DMA_IT_HTIF0) != RESET) { @@ -240,5 +245,7 @@ void DMA2_Stream0_IRQHandler(void) { DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0); } + + GPIOE->ODR &= ~(1 << 0); } diff --git a/codec2-dev/stm32/src/stm32f4_dac.c b/codec2-dev/stm32/src/stm32f4_dac.c index 3f0d520c..28dc258e 100644 --- a/codec2-dev/stm32/src/stm32f4_dac.c +++ b/codec2-dev/stm32/src/stm32f4_dac.c @@ -31,6 +31,7 @@ #include "stm32f4xx.h" #include "codec2_fifo.h" #include "stm32f4_dac.h" +#include "debugblinky.h" /* write to these registers for 12 bit left aligned data, as per data sheet make sure 4 least sig bits set to 0 */ @@ -94,6 +95,8 @@ void dac_open(int fifo_size) { tim6_config(); dac1_config(); dac2_config(); + + init_debug_blinky(); } /* Call these puppies to send samples to the DACs. For your @@ -116,7 +119,7 @@ static void tim6_config(void) /* -------------------------------------------------------- - TIM3 input clock (TIM6CLK) is set to 2 * APB1 clock (PCLK1), since + TIM6 input clock (TIM6CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1 (see system_stm32f4xx.c and Fig 13 clock tree figure in DM0031020.pdf). @@ -275,6 +278,8 @@ void DMA1_Stream5_IRQHandler(void) { int i, j, sam; short signed_buf[DAC_BUF_SZ/2]; + GPIOE->ODR = (1 << 1); + /* Transfer half empty interrupt - refill first half */ if(DMA_GetITStatus(DMA1_Stream5, DMA_IT_HTIF5) != RESET) { @@ -318,6 +323,8 @@ void DMA1_Stream5_IRQHandler(void) { DMA_ClearITPendingBit(DMA1_Stream5, DMA_IT_TCIF5); } + + GPIOE->ODR &= ~(1 << 1); } /* @@ -328,6 +335,8 @@ void DMA1_Stream6_IRQHandler(void) { int i, j, sam; short signed_buf[DAC_BUF_SZ/2]; + GPIOE->ODR = (1 << 2); + /* Transfer half empty interrupt - refill first half */ if(DMA_GetITStatus(DMA1_Stream6, DMA_IT_HTIF6) != RESET) { @@ -371,5 +380,7 @@ void DMA1_Stream6_IRQHandler(void) { DMA_ClearITPendingBit(DMA1_Stream6, DMA_IT_TCIF6); } + + GPIOE->ODR &= ~(1 << 2); } -- 2.25.1