From: drowe67 Date: Fri, 31 May 2013 07:38:36 +0000 (+0000) Subject: ut_dac producing a 500hz sine wave at Fs=16kHz X-Git-Url: http://git.whiteaudio.com/gitweb/?a=commitdiff_plain;h=eb9211badb61e4907d3e549be5952e708f2dd987;p=freetel-svn-tracking.git ut_dac producing a 500hz sine wave at Fs=16kHz git-svn-id: https://svn.code.sf.net/p/freetel/code@1298 01035d8c-6547-0410-b346-abe4f91aad63 --- diff --git a/codec2-dev/stm32/Makefile b/codec2-dev/stm32/Makefile index 62dd061e..0d1a8677 100644 --- a/codec2-dev/stm32/Makefile +++ b/codec2-dev/stm32/Makefile @@ -138,6 +138,7 @@ src/ut_dac.c \ $(DAC_TEST)/stm32f4xx_it.c \ $(DAC_TEST)/system_stm32f4xx.c \ $(PERIPHLIBDIR)/Utilities/STM32_EVAL/STM3240_41_G_EVAL/stm324xg_eval.c \ +gdb_stdio.c \ src/startup_stm32f4xx.s \ src/init.c diff --git a/codec2-dev/stm32/README.txt b/codec2-dev/stm32/README.txt index dd310d6f..dec57cf3 100644 --- a/codec2-dev/stm32/README.txt +++ b/codec2-dev/stm32/README.txt @@ -53,7 +53,7 @@ Getting Started . Place a copy of hts1a.raw in the stlink directory and start st-util: - ~/stlink$ cp ~/codec2-dev/raw/hts1a.raw . + ~/stlink$ cp ~/codec2-dev/raw/hts1a.raw stm_in.raw ~/stlink$ sudo ./st-util -f /home/david/codec2-dev/stm32/stm32f4_codec2.elf . In _another_ console start gdb: @@ -90,7 +90,7 @@ Getting Started . gdb will prints various debug messages, and the codec output file will be written to ~/stlink. - ~/stlink$ play -r 8000 -s -2 hts1a_out.raw + ~/stlink$ play -r 8000 -s -2 stm_out.raw Process ------- diff --git a/codec2-dev/stm32/src/main.c b/codec2-dev/stm32/src/main.c index c1d2356f..28d8ba75 100644 --- a/codec2-dev/stm32/src/main.c +++ b/codec2-dev/stm32/src/main.c @@ -38,13 +38,13 @@ static void c2demo(int mode, char inputfile[], char outputfile[]) fin = fopen(inputfile, "rb"); if (fin == NULL) { - printf("Error opening input file: %s\n",inputfile); + printf("Error opening input file: %s\n\nTerminating....\n",inputfile); exit(1); } fout = fopen(outputfile, "wb"); if (fout == NULL) { - printf("Error opening output file: %s\n",outputfile); + printf("Error opening output file: %s\n\nTerminating....\n",outputfile); exit(1); } @@ -98,7 +98,7 @@ static void c2speedtest(int mode, char inputfile[]) fin = fopen(inputfile, "rb"); if (fin == NULL) { - printf("Error opening input file: %s\n",inputfile); + printf("Error opening input file: %s\nTerminating....\n",inputfile); exit(1); } @@ -130,7 +130,7 @@ void gpio_init() { // purpose output } -int main(void) { +int main(int argc, char *argv[]) { SystemInit(); gpio_init(); machdep_timer_init (); @@ -140,14 +140,14 @@ int main(void) { /* File I/O test for profiling or (with #define DUMP) dumping states for optimisation and tiuning */ - c2demo(CODEC2_MODE_1600, "hts1a.raw", "hts1a_out.raw"); + c2demo(CODEC2_MODE_1600, "stm_in.raw", "stm_out.raw"); printf("Starting c2 speed test\n"); /* Another test of execution speed. Look at PD13 with a oscilliscope. On time is enc, off is dec */ - c2speedtest(CODEC2_MODE_1600, "hts1a.raw"); + c2speedtest(CODEC2_MODE_1600, "stm_in.raw"); printf("Finished\n"); diff --git a/codec2-dev/stm32/src/ut_dac.c b/codec2-dev/stm32/src/ut_dac.c new file mode 100644 index 00000000..6cfaeb56 --- /dev/null +++ b/codec2-dev/stm32/src/ut_dac.c @@ -0,0 +1,198 @@ +/** + ****************************************************************************** + * @file DAC/DAC_SignalsGeneration/main.c + * @author MCD Application Team + * @version V1.1.0 + * @date 18-January-2013 + * @brief Main program body. + ****************************************************************************** + * @attention + * + *

© COPYRIGHT 2013 STMicroelectronics

+ * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "ut_dac.h" +#include "gdb_stdio.h" + +#ifdef __EMBEDDED__ +#define printf gdb_stdio_printf +#define fopen gdb_stdio_fopen +#define fclose gdb_stdio_fclose +#define fread gdb_stdio_fread +#define fwrite gdb_stdio_fwrite +#endif + +/** @addtogroup STM32F4xx_StdPeriph_Examples + * @{ + */ + +/** @addtogroup DAC_SignalsGeneration + * @{ + */ + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +DAC_InitTypeDef DAC_InitStructure; + + +const uint16_t aSine12bit[32] = { + 2047, 2447, 2831, 3185, 3498, 3750, 3939, 4056, 4095, 4056, + 3939, 3750, 3495, 3185, 2831, 2447, 2047, 1647, 1263, 909, + 599, 344, 155, 38, 0, 38, 155, 344, 599, 909, 1263, 1647}; + +const uint8_t aEscalator8bit[6] = {0x0, 0x33, 0x66, 0x99, 0xCC, 0xFF}; + +__IO uint8_t ubSelectedWavesForm = 1; +__IO uint8_t ubKeyPressed = SET; + +/* Private function prototypes -----------------------------------------------*/ +static void TIM6_Config(void); + +static void DAC_Ch2_SineWaveConfig(void); + +/* Private functions ---------------------------------------------------------*/ + +/** + * @brief Main program + * @param None + * @retval None + */ +int main(void) +{ + /*!< At this stage the microcontroller clock setting is already configured, + this is done through SystemInit() function which is called from startup + files (startup_stm32f40xx.s/startup_stm32f427x.s) before to branch to + application main. + To reconfigure the default setting of SystemInit() function, refer to + system_stm32f4xx.c file + */ + + /* Preconfiguration before using DAC----------------------------------------*/ + GPIO_InitTypeDef GPIO_InitStructure; + + /* DMA1 clock enable */ + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); + /* GPIOA clock enable (to be used with DAC) */ + RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); + /* DAC Periph clock enable */ + RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE); + + /* DAC channel 1 & 2 (DAC_OUT1 = PA.4)(DAC_OUT2 = PA.5) configuration */ + GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5; + GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; + GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; + GPIO_Init(GPIOA, &GPIO_InitStructure); + + /* TIM6 Configuration ------------------------------------------------------*/ + + TIM6_Config(); + + DAC_Ch2_SineWaveConfig(); + + while (1); +} + +/** + * @brief TIM6 Configuration + * @note TIM6 configuration is based on APB1 frequency + * @note TIM6 Update event occurs each TIM6CLK/256 + * @param None + * @retval None + */ +static void TIM6_Config(void) +{ + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; + /* TIM6 Periph clock enable */ + RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE); + + /* -------------------------------------------------------- + TIM3 input clock (TIM6CLK) is set to 2 * APB1 clock (PCLK1), + since APB1 prescaler is different from 1. + TIM6CLK = 2 * PCLK1 + TIM6CLK = HCLK / 2 = SystemCoreClock /2 + + TIM6 Update event occurs each TIM6CLK/256 + + Note: + SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file. + Each time the core clock (HCLK) changes, user had to call SystemCoreClockUpdate() + function to update SystemCoreClock variable value. Otherwise, any configuration + based on this variable will be incorrect. + + ----------------------------------------------------------- */ + /* Time base configuration */ + TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); + TIM_TimeBaseStructure.TIM_Period = 1679; + TIM_TimeBaseStructure.TIM_Prescaler = 0; + TIM_TimeBaseStructure.TIM_ClockDivision = 0; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure); + + /* TIM6 TRGO selection */ + TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update); + + /* TIM6 enable counter */ + TIM_Cmd(TIM6, ENABLE); +} + +/** + * @brief DAC Channel2 SineWave Configuration + * @param None + * @retval None + */ +static void DAC_Ch2_SineWaveConfig(void) +{ + DMA_InitTypeDef DMA_InitStructure; + + /* DAC channel2 Configuration */ + DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO; + DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None; + DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable; + DAC_Init(DAC_Channel_2, &DAC_InitStructure); + + /* DMA1_Stream6 channel7 configuration **************************************/ + DMA_DeInit(DMA1_Stream6); + DMA_InitStructure.DMA_Channel = DMA_Channel_7; + DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC_DHR12R2_ADDRESS; + DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&aSine12bit; + DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; + DMA_InitStructure.DMA_BufferSize = 32; + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; + DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; + DMA_InitStructure.DMA_Priority = DMA_Priority_High; + DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; + DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; + DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; + DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; + DMA_Init(DMA1_Stream6, &DMA_InitStructure); + + /* Enable DMA1_Stream6 */ + DMA_Cmd(DMA1_Stream6, ENABLE); + + /* Enable DAC Channel2 */ + DAC_Cmd(DAC_Channel_2, ENABLE); + + /* Enable DMA for DAC Channel2 */ + DAC_DMACmd(DAC_Channel_2, ENABLE); +} + diff --git a/codec2-dev/stm32/src/ut_dac.h b/codec2-dev/stm32/src/ut_dac.h new file mode 100644 index 00000000..b235e56c --- /dev/null +++ b/codec2-dev/stm32/src/ut_dac.h @@ -0,0 +1,63 @@ +/** + ****************************************************************************** + * @file DAC/DAC_SignalsGeneration/main.h + * @author MCD Application Team + * @version V1.1.0 + * @date 18-January-2013 + * @brief Header for main.c module + ****************************************************************************** + * @attention + * + *

© COPYRIGHT 2013 STMicroelectronics

+ * + * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); + * You may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.st.com/software_license_agreement_liberty_v2 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __MAIN_H +#define __MAIN_H + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f4xx.h" + +#if defined (USE_STM324xG_EVAL) + #include "stm324xg_eval.h" + +#elif defined (USE_STM324x7I_EVAL) + #include "stm324x7i_eval.h" + +#else + #error "Please select first the Evaluation board used in your application (in Project Options)" +#endif + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +#if defined (USE_STM324xG_EVAL) + + #define DAC_DHR12R2_ADDRESS 0x40007414 + #define DAC_DHR8R1_ADDRESS 0x40007410 + +#else /* defined (USE_STM324x7I_EVAL)*/ + + #define DAC_DHR12R2_ADDRESS 0x40007414 + #define DAC_DHR8R1_ADDRESS 0x40007410 + +#endif +/* Exported functions ------------------------------------------------------- */ + +#endif /* __MAIN_H */ + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/