cancel
Showing results for 
Search instead for 
Did you mean: 

OC toggle and DMA problem

dangraf
Associate II
Posted on June 04, 2015 at 16:40

I'm trying to connect the output Compare together with the DMA controller for my STM32F3 discovery board.

But I have a strange problem, it seems like the DMA controller is only copying one byte from the memory to the compare register

DMA disabled

CCR2 register

100tick => 5.6us

200tick =>11.3us

300tick=>17.2us

400tick=>22.3us

but when using the DMA it seems to only copy one byte from the memory to the CCR2 register

100tick => 5.6us

200tick => 11.3us

300tick => 2.5us = 0x12C, 0x2C => 44 * 5.6/100 = 2.5us

400tick => 8.1us  = 0x190, 0x90 = >144 * 5.6/100 = 8.1us

I've tried to setup the DMA in both half_word and word but still get the same results.

Any idea what the problem could be?

[code]

#define PULSE_LENGTH 0xFFFF

uint16_t OC_1_arr[4];

void initDebugPort_PE12(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOE, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOE, &GPIO_InitStructure);

}

void initICport(void)

{

TIM_ICInitTypeDef  TIM_ICInitStructure;

TIM_ICStructInit(&TIM_ICInitStructure);

TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;

TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

TIM_ICInitStructure.TIM_ICFilter = 0;

TIM_ICInit(TIM8, &TIM_ICInitStructure);

}

void initOCports_PC7_PC8_PC9(void)

{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6| GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_4);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_4);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_4);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_4);

}

void initTimer8(void)

{

TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);

uint16_t PrescalerValue = (uint16_t) ((SystemCoreClock ) / CLOCK_FREQYENCY) - 1;

/* Time base configuration */

TIM_TimeBaseStructInit(& TIM_TimeBaseStructure);

TIM_TimeBaseStructure.TIM_Period = 0xFFFF;

TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;

TIM_TimeBaseStructure.TIM_ClockDivision = 0;

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;

TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

TIM_SelectOnePulseMode(TIM8, TIM_OPMode_Single);

TIM_SelectInputTrigger(TIM8, TIM_TS_TI1FP1); //connect timer8 to trigger on channel 1

TIM_SelectSlaveMode(TIM8, TIM_SlaveMode_Combined_ResetTrigger);

TIM_CtrlPWMOutputs(TIM8,ENABLE);

TIM_Cmd(TIM8, ENABLE);

}

void initPA0_and_PA1_As_PWM(void)

{

TIM_OCInitTypeDef  TIM_OCInitStructure;

TIM_OCStructInit(&TIM_OCInitStructure);

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Toggle;

TIM_OCInitStructure.TIM_Pulse = 100;

TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

TIM_OC2Init(TIM8,&TIM_OCInitStructure );

TIM_OCInitStructure.TIM_Pulse = 300;

TIM_OC3Init(TIM8,&TIM_OCInitStructure );

TIM_OCInitStructure.TIM_Pulse = 0x30;

TIM_OC4Init(TIM8,&TIM_OCInitStructure );

TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Disable);

TIM_DMACmd(TIM8, TIM_DMA_CC2, ENABLE);

// connect timer8 to AF pin

}

void setupDMA(void)

{

DMA_InitTypeDef  DMA_InitStructure;

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE);

DMA_DeInit(DMA1_Channel1);

DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST;

DMA_InitStructure.DMA_BufferSize = 2;

DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)OC_1_arr;

DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;

DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;

DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;

DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&(TIM8->CCR2); //ch2

DMA_InitStructure.DMA_MemoryDataSize = DMA_PeripheralDataSize_HalfWord; //16bit

DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;

DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;

DMA_InitStructure.DMA_Priority = DMA_Priority_High;

DMA_Init(DMA2_Channel5,&DMA_InitStructure);

DMA_Cmd(DMA2_Channel5, ENABLE);

}

void TIM8_Configuration(void)

{

OC_1_arr[0] = 400;

OC_1_arr[1] = 54;

// OC_1_arr[2] = 0x120;

// OC_1_arr[3] = 0x400;

initDebugPort_PE12();

initOCports_PC7_PC8_PC9();

initICport();

initTimer8();

setupDMA();

initPA0_and_PA1_As_PWM();

}

[\code]
1 REPLY 1
Posted on June 04, 2015 at 16:56

Might want it to advance

DMA_InitStructure.DMA_MemoryInc 

= DMA_MemoryInc_Disable;

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..