|
|
#include <stdio.h>
|
|
|
#include <timer.h>
|
|
|
#include <stm32f4xx_rcc.h>
|
|
|
#include <stm32f4xx_tim.h>
|
|
|
#include <stm32f4xx_dac.h>
|
|
|
#include <stm32f4xx_gpio.h>
|
|
|
#include <stm32f4xx_dma.h>
|
|
|
#include <stm32f4xx.h>
|
|
|
#include <gpio.h>
|
|
|
#include <stm32f4xx.h>
|
|
|
#include <dac.h>
|
|
|
|
|
|
|
|
|
|
|
|
const TIM_TypeDef* _timer_dev_table[14]={TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7,
|
|
|
TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14};
|
|
|
|
|
|
|
|
|
|
|
|
int dacopen(uint32_t pin)
|
|
|
{
|
|
|
#define GPIOGETPORT(gpio) ((GPIO_TypeDef*)(((((uint32_t)gpio) & (uint32_t)0x0000FF00)*(uint32_t)4) + (uint32_t)GPIOA))
|
|
|
#define GPIOPORTNUM(gpio) (((uint32_t)(gpio) & (uint32_t)0x0000FF00)>>(uint32_t)8)
|
|
|
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1 , ENABLE);
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
|
|
|
gpio_t DACOUT;
|
|
|
DACOUT = gpioopen(pin);
|
|
|
gpiosetconfig(DACOUT, gpioan, gpiolowspeed, gpiopushpulltype, gpionopulltype);
|
|
|
return 0;
|
|
|
}
|
|
|
|
|
|
int dacsetconfig(int timer, int buffer_size, int dac, uint32_t dac_type, uint32_t samplingFreq, void *data)
|
|
|
{
|
|
|
//#include <core.h>
|
|
|
DAC_InitTypeDef DAC_InitStructure;
|
|
|
// TIM_TypeDef* tim = (TIM_TypeDef*)_timer_dev_table[timer];
|
|
|
TIM_TypeDef* tim = TIM6;
|
|
|
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
|
|
|
DMA_InitTypeDef DMA_InitStructure;
|
|
|
if(timer>=timer2 && timer <= timer7)RCC_APB1PeriphClockCmd((1<<(timer-1)), ENABLE);
|
|
|
if(timer==timer8)RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);
|
|
|
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
|
|
|
unsigned int __inputFreq__ = 80000000;//getCpuFreq()/2;
|
|
|
unsigned int period = __inputFreq__/samplingFreq - 1;
|
|
|
unsigned int PrescalerValue=0;
|
|
|
// while (period>=0x0FFFF)
|
|
|
// {
|
|
|
// PrescalerValue++;
|
|
|
// __inputFreq__ = getCpuFreq()/(2*(PrescalerValue+1));
|
|
|
// period = (__inputFreq__/samplingFreq) - 1;
|
|
|
// }
|
|
|
|
|
|
printf("period = %d \n\r prescaler = 0x%x 0x%x\n\r",period,data,__get_MSP());
|
|
|
TIM_TimeBaseStructInit(&TIM_TimeBaseStructure);
|
|
|
TIM_TimeBaseStructure.TIM_Period = 0xff;
|
|
|
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)0;
|
|
|
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
|
|
|
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
|
|
|
TIM_TimeBaseInit(TIM6, &TIM_TimeBaseStructure);
|
|
|
TIM_SelectOutputTrigger(TIM6, TIM_TRGOSource_Update);
|
|
|
// TIM_ARRPreloadConfig(tim, ENABLE);
|
|
|
TIM_Cmd(TIM6, ENABLE);
|
|
|
|
|
|
// if (timer == timer2)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T2_TRGO;
|
|
|
// if (timer == timer4)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T4_TRGO;
|
|
|
// if (timer == timer5)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T5_TRGO;
|
|
|
// if (timer == timer6)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
|
|
|
// if (timer == timer7)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T7_TRGO;
|
|
|
// if (timer == timer8)DAC_InitStructure.DAC_Trigger = DAC_Trigger_T8_TRGO;
|
|
|
DAC_InitStructure.DAC_Trigger = DAC_Trigger_T6_TRGO;
|
|
|
|
|
|
DAC_InitStructure.DAC_WaveGeneration = DAC_WaveGeneration_None;
|
|
|
DAC_InitStructure.DAC_OutputBuffer = DAC_OutputBuffer_Enable;
|
|
|
if (dac == DAC1){
|
|
|
printf("DAC1 Init ");
|
|
|
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
|
|
|
DMA_DeInit(DMA1_Stream5);
|
|
|
}
|
|
|
|
|
|
if (dac == DAC2){
|
|
|
printf("DAC2 Init ");
|
|
|
DAC_Init(DAC_Channel_2, &DAC_InitStructure);
|
|
|
DMA_DeInit(DMA1_Stream6);
|
|
|
}
|
|
|
if (dac == DAC1){
|
|
|
DMA_DeInit(DMA1_Stream5);
|
|
|
DMA_Cmd(DMA1_Stream5, DISABLE);
|
|
|
}
|
|
|
if (dac == DAC2){
|
|
|
DMA_DeInit(DMA1_Stream6);
|
|
|
DMA_Cmd(DMA1_Stream6, DISABLE);
|
|
|
}
|
|
|
DMA_InitStructure.DMA_Channel = DMA_Channel_7;
|
|
|
// DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)dac_type;
|
|
|
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)DAC1_12bit_right_align;
|
|
|
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)data;
|
|
|
DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
|
|
|
DMA_InitStructure.DMA_BufferSize = buffer_size;
|
|
|
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_Stream5, &DMA_InitStructure);
|
|
|
/* Enable DMA1_Stream6 */
|
|
|
DMA_Cmd(DMA1_Stream5, ENABLE);
|
|
|
|
|
|
/* Enable DAC Channel2 */
|
|
|
DAC_Cmd(DAC_Channel_1, ENABLE);
|
|
|
|
|
|
/* Enable DMA for DAC Channel2 */
|
|
|
DAC_DMACmd(DAC_Channel_1, ENABLE);
|
|
|
|
|
|
// if (dac == DAC1){
|
|
|
// printf("DAC1 DMA Init");
|
|
|
// DMA_Init(DMA1_Stream5, &DMA_InitStructure);
|
|
|
// DMA_Cmd(DMA1_Stream5, ENABLE);
|
|
|
// DAC_Cmd(DAC_Channel_1, ENABLE);
|
|
|
// DAC_DMACmd(DAC_Channel_1, ENABLE);
|
|
|
|
|
|
// }
|
|
|
// if (dac == DAC2){
|
|
|
// printf("DAC2 DMA Init");
|
|
|
// DMA_Init(DMA1_Stream6, &DMA_InitStructure);
|
|
|
// DMA_Cmd(DMA1_Stream6, ENABLE);
|
|
|
// DAC_Cmd(DAC_Channel_2, ENABLE);
|
|
|
// DAC_DMACmd(DAC_Channel_2, ENABLE);
|
|
|
|
|
|
// }
|
|
|
return 0;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|