diff --git a/examples/DAC/main.c b/examples/DAC/main.c --- a/examples/DAC/main.c +++ b/examples/DAC/main.c @@ -29,20 +29,22 @@ #include #include #include -int16_t __attribute__ ((aligned (4))) data[512]; +int16_t __attribute__ ((aligned (4))) data[4096]; int libuc_main() { /** Depending on the board the printf can be directed on one uart or LCD screen or any other stream device. */ - printf("DAC example\n\r"); - for(int i=0;i<512;i++) + printf("DAC example \n\r"); + for(int i=0;i<4096;i++) { - data[i]=(int16_t)(2000.0*cos(i/10.0))+2000.0; + data[i]=(2048+i)%4096; } + data[0]=4000; dacopen(PA4); - dacsetconfig(timer8, 512, DAC1,DAC1_12bit_right_align , 100000,(void*)data); + dacsetconfig(timer8, 4096, DAC1,DAC1_12bit_right_align , 100000,(void*)&data); + while (1); return 0; } diff --git a/mkspecs/features/stm32f4/crt0.s b/mkspecs/features/stm32f4/crt0.s --- a/mkspecs/features/stm32f4/crt0.s +++ b/mkspecs/features/stm32f4/crt0.s @@ -38,11 +38,11 @@ .fpu fpv4-sp-d16 .section .stack - .align 3 + .align 4 #ifdef __STACK_SIZE .equ Stack_Size, __STACK_SIZE #else - .equ Stack_Size, 0x2000 + .equ Stack_Size, 0x7F00 #endif .globl __StackTop .globl __StackLimit @@ -57,7 +57,7 @@ #ifdef __HEAP_SIZE .equ Heap_Size, __HEAP_SIZE #else - .equ Heap_Size, 0x2000 + .equ Heap_Size, 0x7F00 #endif .globl __HeapBase .globl __HeapLimit diff --git a/mkspecs/features/stm32f4/syscalls.c b/mkspecs/features/stm32f4/syscalls.c --- a/mkspecs/features/stm32f4/syscalls.c +++ b/mkspecs/features/stm32f4/syscalls.c @@ -11,7 +11,6 @@ #include #include #include -#include #undef errno diff --git a/src/stm32f4/CORE/core.c b/src/stm32f4/CORE/core.c --- a/src/stm32f4/CORE/core.c +++ b/src/stm32f4/CORE/core.c @@ -90,30 +90,30 @@ uint32_t getAPB2Freq() uint32_t getCpuFreq() { - uint32_t cpufreq = OSC0; + uint32_t _freq_ = OSC0; uint32_t PLLN,PLLM,PLLP; if((RCC->CFGR & 0xC) == 8) //PLL used as sys clk { - uint32_t pllinput=INTOSC; + uint32_t _pllin_=INTOSC; if((RCC->PLLCFGR & (1<<22)) == (1<<22)) { - pllinput=OSC0; + _pllin_=OSC0; } PLLN = (RCC->PLLCFGR>>6) & 0x1FF; PLLM = RCC->PLLCFGR & 0x3F; PLLP = 1<<(((RCC->PLLCFGR>>16) & 3 )+1); - cpufreq = (pllinput * PLLN )/(PLLM*PLLP); + _freq_ = (_pllin_ * PLLN )/(PLLM*PLLP); } else if((RCC->CFGR & 0xC) == 0) //HSI used as sys clk { - cpufreq=INTOSC; + _freq_=INTOSC; } - if((RCC->CFGR & (1<<7))==1<<7) + if((RCC->CFGR & (1<<7))==(1<<7)) { - return cpufreq>>((RCC->CFGR & (7<<4))>>4); + return _freq_>>((RCC->CFGR & (7<<4))>>4); } - return cpufreq; + return _freq_; } void reset_AHB1() diff --git a/src/stm32f4/CPU/crt0.s b/src/stm32f4/CPU/crt0.s --- a/src/stm32f4/CPU/crt0.s +++ b/src/stm32f4/CPU/crt0.s @@ -42,7 +42,7 @@ #ifdef __STACK_SIZE .equ Stack_Size, __STACK_SIZE #else - .equ Stack_Size, 0x2000 + .equ Stack_Size, 0x7F00 #endif .globl __StackTop .globl __StackLimit @@ -57,7 +57,7 @@ #ifdef __HEAP_SIZE .equ Heap_Size, __HEAP_SIZE #else - .equ Heap_Size, 0x2000 + .equ Heap_Size, 0x7F00 #endif .globl __HeapBase .globl __HeapLimit diff --git a/src/stm32f4/DAC/dac.c b/src/stm32f4/DAC/dac.c --- a/src/stm32f4/DAC/dac.c +++ b/src/stm32f4/DAC/dac.c @@ -11,9 +11,6 @@ #include -#define DAC_DHR12R2_ADDRESS 0x40007414 -#define DAC_DHR8R1_ADDRESS 0x40007410 - const TIM_TypeDef* _timer_dev_table[14]={TIM1,TIM2,TIM3,TIM4,TIM5,TIM6,TIM7, TIM8,TIM9,TIM10,TIM11,TIM12,TIM13,TIM14}; @@ -35,58 +32,72 @@ int dacopen(uint32_t pin) int dacsetconfig(int timer, int buffer_size, int dac, uint32_t dac_type, uint32_t samplingFreq, void *data) { -#include +//#include DAC_InitTypeDef DAC_InitStructure; - TIM_TypeDef* tim = (TIM_TypeDef*)_timer_dev_table[timer]; + // 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); - uint32_t timfreq = getCpuFreq()/2; - uint32_t period = timfreq/samplingFreq - 1; - uint16_t PrescalerValue=0; - while (period>=0x0FFFF) - { - PrescalerValue++; - timfreq = getCpuFreq()/(2*(PrescalerValue+1)); - period = (timfreq/samplingFreq) - 1; - } - printf("period = %d \n\r prescaler = %d \n\r",period,PrescalerValue); + 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 = 10; - TIM_TimeBaseStructure.TIM_Prescaler = 0; + 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(tim, &TIM_TimeBaseStructure); - TIM_SelectOutputTrigger(tim, TIM_TRGOSource_Update); - TIM_ARRPreloadConfig(tim, ENABLE); - TIM_Cmd(tim, ENABLE); + 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; + // 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)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_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; @@ -96,21 +107,32 @@ int dacsetconfig(int timer, int buffer_s 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); - if (dac == DAC1){ - DMA_Init(DMA1_Stream5, &DMA_InitStructure); - DMA_Cmd(DMA1_Stream5, ENABLE); - DAC_DMACmd(DAC_Channel_1, ENABLE); - DAC_Cmd(DAC_Channel_1, ENABLE); + /* Enable DAC Channel2 */ + DAC_Cmd(DAC_Channel_1, ENABLE); + + /* Enable DMA for DAC Channel2 */ + DAC_DMACmd(DAC_Channel_1, ENABLE); - } - if (dac == DAC2){ - DMA_Init(DMA1_Stream6, &DMA_InitStructure); - DMA_Cmd(DMA1_Stream6, ENABLE); - DAC_DMACmd(DAC_Channel_2, ENABLE); - DAC_Cmd(DAC_Channel_2, 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; }