AnsweredAssumed Answered

generating an interrupt for the loaded values in ARR

Question asked by das.sanjib on Mar 1, 2014
Latest reply on Jun 7, 2014 by Clive One
Hi,
I want to generate a timer interrupt for 6.25 us, 9.375 us, 0.1 milisec, 0.4 millisecond and so on . I think that it can be done by using DMA taking the array SRC_Buffer_DEC[4] ={200,300,3200,12800} as DMA memory base address and ADC1->ARR as peripheral address therefore the Auto reload value should change everytime and should generate an interrupt for each value. but I am not able to do that . I didn't get any example for this but I think that it can be done. Can anybody help me and say me whether my thinking is right or wrong. whether it is achieveable, if no how it can be done? I have paste my code below please see
#include "unistd.h"
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx.h"
#include <misc.h>
#include "stm32f4xx_adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_dac.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_dma.h"
#include "stm32f4xx_usart.h"
#include "time.h"
uint16_t SRC_Buffer_DEC[4] ={200,300,3200,12800};

int main(void)
{
    STM_EVAL_LEDInit(LED3);
    rcc();
    INTTIM_Config();
    NVIC_Configuration();
    EnableTimerInterrupt();
    while(1);

DMA();
#ifdef DEBUG
  debug();
#endif
}

void rcc()
{
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE);
    /**
     * Enable clock for DMA2 (ADC DMA)
     **/
    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
     /**
      * Enable clock for TIM2 (used for ADC Trigger)
      **/
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

}
#if 1
void DMA()
{
    DMA_InitTypeDef DMA_InitStructure;
DMA_InitStructure.DMA_Channel = DMA_Channel_6;
    DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&TIM2->ARR;
    DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&SRC_Buffer_DEC;
    DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral;
    DMA_InitStructure.DMA_BufferSize = 4;
    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_PeripheralDataSize_HalfWord;
    DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
    //DMA_InitStructure.DMA_Mode = DMA_Mode_Normal;
    DMA_InitStructure.DMA_Priority = DMA_Priority_High;
    DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
    DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full;
    DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
    DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;

    DMA_Init(DMA2_Stream4, &DMA_InitStructure);


//    DMA_ITConfig(DMA2_Stream4, DMA_IT_TC | DMA_IT_HT, ENABLE);
    DMA_Cmd(DMA2_Stream4, ENABLE);

    /* TIM8 Update DMA Request enable */
    TIM_DMACmd(TIM2, TIM_DMA_Trigger , ENABLE);
    TIM_DMACmd(TIM2, TIM_DMA_Update, ENABLE);
}
#endif
void INTTIM_Config(void)
{
    TIM_TimeBaseInitTypeDef    TIM_InitStruct;
    TIM_OCInitTypeDef       TIM_OCInitStructure;
  /* TIM2 clock enable */

  /* Time base configuration */
  TIM_InitStruct.TIM_Prescaler = 2 - 1;                // This will configure the clock to 2 kHz
  TIM_InitStruct.TIM_CounterMode = TIM_CounterMode_Up;     // Count-up timer mode
// TIM_InitStruct.TIM_Period = 200 - 1;                    // 2 kHz down to 1 Hz = 1 second
  TIM_InitStruct.TIM_ClockDivision = TIM_CKD_DIV1;        // Divide clock by 1
  TIM_InitStruct.TIM_RepetitionCounter = 0;                // Set to 0, not used
  TIM_TimeBaseInit(TIM2, &TIM_InitStruct);

      TIM_ARRPreloadConfig(TIM2, ENABLE);


      /* TIM8 enable counter */
      TIM_Cmd(TIM2, ENABLE);


  /* TIM2 enable counter */

  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
}

void EnableTimerInterrupt()
{
    NVIC_InitTypeDef nvicStructure;
    nvicStructure.NVIC_IRQChannel = TIM2_IRQn;
    nvicStructure.NVIC_IRQChannelPreemptionPriority = 0;
    nvicStructure.NVIC_IRQChannelSubPriority = 0;
    nvicStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&nvicStructure);
}
void TIM2_IRQHandler()

{
        if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
        {
            TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
             STM_EVAL_LEDToggle(LED3);
    }
}

Outcomes