2020-09-03 10:08 PM
TIM_HandleTypeDef htim3;
DMA_HandleTypeDef hdma_tim3_ch2;
/* TIM3 init function */
void MX_TIM3_Init(void)
{
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
TIM_OC_InitTypeDef sConfigOC = {0};
htim3.Instance = TIM3;
htim3.Init.Prescaler = 0;
htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
htim3.Init.Period = 125-1;
htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
{
Error_Handler();
}
HAL_TIM_MspPostInit(&htim3);
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspInit 0 */
/* USER CODE END TIM3_MspInit 0 */
/* TIM3 clock enable */
__HAL_RCC_TIM3_CLK_ENABLE();
/* TIM3 DMA Init */
/* TIM3_CH2 Init */
hdma_tim3_ch2.Instance = DMA1_Stream5;
hdma_tim3_ch2.Init.Channel = DMA_CHANNEL_5;
hdma_tim3_ch2.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_tim3_ch2.Init.PeriphInc = DMA_PINC_ENABLE;
hdma_tim3_ch2.Init.MemInc = DMA_MINC_DISABLE;
hdma_tim3_ch2.Init.PeriphDataAlignment = DMA_PDATAALIGN_WORD;
hdma_tim3_ch2.Init.MemDataAlignment = DMA_MDATAALIGN_WORD;
hdma_tim3_ch2.Init.Mode = DMA_CIRCULAR;
hdma_tim3_ch2.Init.Priority = DMA_PRIORITY_HIGH;
hdma_tim3_ch2.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
if (HAL_DMA_Init(&hdma_tim3_ch2) != HAL_OK)
{
Error_Handler();
}
__HAL_LINKDMA(tim_baseHandle,hdma[TIM_DMA_ID_CC2],hdma_tim3_ch2);
/* USER CODE BEGIN TIM3_MspInit 1 */
/* USER CODE END TIM3_MspInit 1 */
}
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(timHandle->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspPostInit 0 */
/* USER CODE END TIM3_MspPostInit 0 */
__HAL_RCC_GPIOA_CLK_ENABLE();
/**TIM3 GPIO Configuration
PA7 ------> TIM3_CH2
*/
GPIO_InitStruct.Pin = GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
/* USER CODE BEGIN TIM3_MspPostInit 1 */
/* USER CODE END TIM3_MspPostInit 1 */
}
}
/* Main Code */
if (process_complete)
{
HAL_TIM_PWM_Start_DMA(&htim2, TIM_CHANNEL_3,(uint32_t*)LED_buffer,LED_BUFF_LEN);
process_complete=0;
}
Solved! Go to Solution.
2020-09-09 05:35 AM
@Community member
I got the solution. After changing the below setting it start working :
hdma_tim3_ch2.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim3_ch2.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim3_ch2.Init.MemInc = DMA_MINC_ENABLE;
2020-09-03 10:15 PM
@Community member @Community member I am Interfacing ws2812b RGB LED .This i have interfaced with STM32F1 using TIMER 2 CHANNEL 3 it is working fine. but now i am trying to interface it with F4 via same method but it is not working.
Please Help it is urgent !!!
2020-09-03 11:40 PM
Watch DMA width, TIM3 16-bit as I recall
Later code references TIM2 CH3 not TIM3 CH2
2020-09-04 12:03 AM
Sorry Clive !!! That i have posted mistakenly.Here is the actual code given below :
if (process_complete)
{
HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_2,(uint32_t*)LED_buffer,LED_BUFF_LEN);
process_complete=0;
}
What changes i have to do for 16 bit timer ?
2020-09-09 05:35 AM
@Community member
I got the solution. After changing the below setting it start working :
hdma_tim3_ch2.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim3_ch2.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim3_ch2.Init.MemInc = DMA_MINC_ENABLE;
2020-09-09 05:40 AM
@Community member @Community member
For F1 it works with the following DMA Settings :
hdma_tim2_ch3.Init.Direction = DMA_PERIPH_TO_MEMORY;
hdma_tim2_ch3.Init.PeriphInc = DMA_PINC_ENABLE;
hdma_tim2_ch3.Init.MemInc = DMA_MINC_DISABLE;
Whereas for F4 it works with the follwing settings :
hdma_tim3_ch2.Init.Direction = DMA_MEMORY_TO_PERIPH;
hdma_tim3_ch2.Init.PeriphInc = DMA_PINC_DISABLE;
hdma_tim3_ch2.Init.MemInc = DMA_MINC_ENABLE;
Is it means that TIMxCCR3 is Memory side in F1 and TIMxCCR2 is peripheral side ? Can you explain more please !!!
2020-09-09 08:55 AM