Author Topic: TIMER->PWM GENERATION NO OUTPUT->DMA->GPIO  (Read 232 times)

0 Members and 1 Guest are viewing this topic.

Offline alizone

  • Newbie
  • Posts: 3
  • Country: tr
TIMER->PWM GENERATION NO OUTPUT->DMA->GPIO
« on: December 05, 2017, 11:25:14 PM »
Hello everyone,

I am using stm32f4 and HAL library.
I am working a project which is about t?mer to GPIO with DMA. I can't see any output. how i can i do?
I couldn't see any output.

Code: [Select]
//----------------------------------------- TIM4 init function---------------------------------//
TIM_MasterConfigTypeDef sMasterConfig;
TIM_OC_InitTypeDef sConfigOC1;
TIM_OC_InitTypeDef sConfigOC2;

uint8_t tim_period;
uint8_t timer_reset_pulse_period;
static void MX_TIM4_Init(void)
{
__HAL_RCC_TIM4_CLK_ENABLE();

tim_period =  SystemCoreClock / 800000; // 0,125us period (10 times lower the 1,25us period to have fixed math below)
timer_reset_pulse_period = (SystemCoreClock / (320 * 60)); // 60us just to be sure

uint32_t cc1 = (10 * tim_period) / 36;
uint32_t cc2 = (10 * tim_period) / 15;

  htim4.Instance = TIM4;

  htim4.Init.Prescaler = 0;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = tim_period;
  htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim4.Init.RepetitionCounter =0;
HAL_TIM_PWM_Init(&htim4);

//----------- TIM4 interrupt Init---------------------/
  HAL_NVIC_SetPriority(TIM4_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(TIM4_IRQn);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sConfigOC1.OCMode = TIM_OCMODE_PWM1;
  sConfigOC1.Pulse = cc1;
  sConfigOC1.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC1.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC1.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC1, TIM_CHANNEL_1);
 
sConfigOC2.OCMode = TIM_OCMODE_PWM1;
  sConfigOC2.Pulse = cc2;
  sConfigOC2.OCPolarity   = TIM_OCPOLARITY_HIGH;
  sConfigOC2.OCFastMode   = TIM_OCFAST_DISABLE;
sConfigOC2.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
sConfigOC2.OCIdleState  = TIM_OCIDLESTATE_RESET;
sConfigOC2.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC2, TIM_CHANNEL_2);
 
  HAL_TIM_Base_Start(&htim4);
HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
__HAL_TIM_DISABLE(&htim4);
}

Code: [Select]
static void MX_DMA_Init(void)
{
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

HAL_TIM_PWM_MspInit(&htim4);
  /* DMA interrupt init */
  /* DMA1_Stream0_IRQn interrupt configuration */
 // HAL_NVIC_SetPriority(DMA1_Stream0_IRQn, 0, 0);
  //HAL_NVIC_EnableIRQ(DMA1_Stream0_IRQn);
  /* DMA1_Stream3_IRQn interrupt configuration */
  //HAL_NVIC_SetPriority(DMA1_Stream3_IRQn, 0, 0);
  //HAL_NVIC_EnableIRQ(DMA1_Stream3_IRQn);
  /* DMA1_Stream6_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Stream6_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream6_IRQn);

}

//----------------------------------------GPIO INIT--------------------------------//

static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  // GPIO Ports Clock Enable //
  __HAL_RCC_GPIOD_CLK_ENABLE();



  GPIO_InitStruct.Pin = GPIO_PIN_12;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 

GPIO_InitStruct.Pin = GPIO_PIN_13 | GPIO_PIN_14 | GPIO_PIN_15 ;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);

}

//----------------------------------DMA INIT-----------------------------------------//
uint16_t ws2812bDmaBitBuffer[24 * 2];
uint32_t WS2812_IO_Low[] = {GPIO_PIN_12<< 16};
void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
{

  if(htim_pwm->Instance==TIM4)
  {
  /* USER CODE BEGIN TIM4_MspInit 0 */

  /* USER CODE END TIM4_MspInit 0 */
    /* Peripheral clock enable */
    __HAL_RCC_TIM4_CLK_ENABLE();
 
    /* TIM4 DMA Init */
    /* TIM4_UP Init */
    hdma_tim4_up.Instance = DMA1_Stream6;
    hdma_tim4_up.Init.Channel = DMA_CHANNEL_2;
    hdma_tim4_up.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_tim4_up.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_tim4_up.Init.MemInc = DMA_MINC_DISABLE;
    hdma_tim4_up.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
    hdma_tim4_up.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_tim4_up.Init.Mode = DMA_CIRCULAR;
    hdma_tim4_up.Init.Priority = DMA_PRIORITY_VERY_HIGH;
    hdma_tim4_up.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tim4_up.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tim4_up.Init.MemBurst = DMA_MBURST_SINGLE;
hdma_tim4_up.Init.PeriphBurst = DMA_PBURST_SINGLE;
   
HAL_DMA_DeInit(&hdma_tim4_up);
HAL_DMA_Init(&hdma_tim4_up);

HAL_DMA_Start(&hdma_tim4_up, (uint32_t)GPIO_PIN_12, (uint32_t)(&GPIOD->BSRR), 48);
    //__HAL_LINKDMA(htim_pwm,hdma[TIM_DMA_ID_UPDATE],hdma_tim4_up);

    /* TIM4_CH1 Init */
    hdma_tim4_ch1.Instance = DMA1_Stream0;
    hdma_tim4_ch1.Init.Channel = DMA_CHANNEL_2;
    hdma_tim4_ch1.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_tim4_ch1.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_tim4_ch1.Init.MemInc = DMA_MINC_ENABLE;
    hdma_tim4_ch1.Init.PeriphDataAlignment = DMA_PDATAALIGN_HALFWORD;
    hdma_tim4_ch1.Init.MemDataAlignment = DMA_MDATAALIGN_HALFWORD;
    hdma_tim4_ch1.Init.Mode = DMA_CIRCULAR;
    hdma_tim4_ch1.Init.Priority = DMA_PRIORITY_VERY_HIGH;
    hdma_tim4_ch1.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tim4_ch1.Init.PeriphBurst = DMA_PBURST_SINGLE;
    hdma_tim4_ch1.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tim4_ch1.Init.MemBurst = DMA_MBURST_SINGLE;

HAL_DMA_DeInit(&hdma_tim4_ch1);
HAL_DMA_Init(&hdma_tim4_ch1);
HAL_DMA_Start(&hdma_tim4_ch1, (uint32_t)ws2812bDmaBitBuffer, (uint32_t)(&GPIOD->BSRR), 48);
   
//__HAL_LINKDMA(htim_pwm,hdma[TIM_DMA_ID_CC1],hdma_tim4_ch1);

    /* TIM4_CH2 Init */
    hdma_tim4_ch2.Instance = DMA1_Stream3;
    hdma_tim4_ch2.Init.Channel = DMA_CHANNEL_2;
    hdma_tim4_ch2.Init.Direction = DMA_MEMORY_TO_PERIPH;
    hdma_tim4_ch2.Init.PeriphInc = DMA_PINC_DISABLE;
    hdma_tim4_ch2.Init.MemInc = DMA_MINC_DISABLE;
    hdma_tim4_ch2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
    hdma_tim4_ch2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
    hdma_tim4_ch2.Init.Mode = DMA_CIRCULAR;
    hdma_tim4_ch2.Init.Priority = DMA_PRIORITY_VERY_HIGH;

    hdma_tim4_ch2.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
hdma_tim4_ch2.Init.PeriphBurst = DMA_PBURST_SINGLE;
    hdma_tim4_ch2.Init.FIFOThreshold = DMA_FIFO_THRESHOLD_FULL;
hdma_tim4_ch2.Init.MemBurst = DMA_MBURST_SINGLE;
   
HAL_DMA_DeInit(&hdma_tim4_ch2);
HAL_DMA_Init(&hdma_tim4_ch2);
    HAL_DMA_Start_IT(&hdma_tim4_ch2, (uint32_t)WS2812_IO_Low,(uint32_t)&GPIOD->BSRR, 48);
    //__HAL_LINKDMA(htim_pwm,hdma[TIM_DMA_ID_CC2],hdma_tim4_ch2);
    __HAL_DMA_ENABLE_IT(&hdma_tim4_ch2,DMA_IT_TC);
 }

}


Code: [Select]
  int main(void)
{
    HAL_Init();
  SystemClock_Config();

  MX_GPIO_Init();
  MX_DMA_Init();
  MX_TIM4_Init();


while (1)
  {
  }
}
 
« Last Edit: December 05, 2017, 11:35:22 PM by alizone »
 

Offline sainter

  • Regular Contributor
  • *
  • Posts: 106
  • Country: lt
Re: TIMER->PWM GENERATION NO OUTPUT->DMA->GPIO
« Reply #1 on: December 06, 2017, 01:19:40 AM »
This helped me a lot:


 

Offline alizone

  • Newbie
  • Posts: 3
  • Country: tr
Re: TIMER->PWM GENERATION NO OUTPUT->DMA->GPIO
« Reply #2 on: December 06, 2017, 01:33:56 AM »
My problem is about DMA. I can't transfer data with DMA to GPIO.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf