void pwmWriteDigital(uint8_t index, uint16_t value)
{

    if (!pwmMotorsEnabled) {
        return;
    }

    motorDmaOutput_t * const motor = &dmaMotors[index];

    uint16_t packet = (value << 1) | 0;                            // Here goes telemetry bit (false for now)
    // compute checksum
    int csum = 0;
    int csum_data = packet;
    for (int i = 0; i < 3; i++) {
        csum ^=  csum_data;   // xor data by nibbles
        csum_data >>= 4;
    }
    csum &= 0xf;
    // append checksum
    packet = (packet << 4) | csum;
    // generate pulses for whole packet
    for (int i = 0; i < 16; i++) {
        motor->dmaBuffer[i] = (packet & 0x8000) ? MOTOR_BIT_1 : MOTOR_BIT_0;  // MSB first
        packet <<= 1;
    }

    if(  HAL_TIM_PWM_Start_DMA(&motor->TimHandle, motor->timerHardware->channel, motor->dmaBuffer, MOTOR_DMA_BUFFER_SIZE) != HAL_OK)
    {
      /* Starting PWM generation Error */
        return;
    }
}
void ws2811LedStripDMAEnable(void)
{
    if (!ws2811Initialised)
    {
        ws2811LedDataTransferInProgress = 0;
        return;
    }

    if(  HAL_TIM_PWM_Start_DMA(&TimHandle, WS2811_TIMER_CHANNEL, ledStripDMABuffer, WS2811_DMA_BUFFER_SIZE) != HAL_OK)
    {
      /* Starting PWM generation Error */
        ws2811LedDataTransferInProgress = 0;
        return;
    }

}
Example #3
0
int main(void) {
  uint16_t IV[200];
  float angle;

  HAL_Init();

  Nucleo_BSP_Init();
  MX_TIM3_Init();

  for (uint8_t i = 0; i < 200; i++) {
    angle = ASR*(float)i;
    IV[i] = (uint16_t) rint(100 + 99*sinf(angle*(PI/180)));
  }

  HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_1, (uint32_t *)IV, 200);

  while (1);
}
Example #4
0
/* pwm_sine_Start
 * Description: Inject a sinusoidal signal using PWM
 * Inputs: htimx - timer handle of PWM pin (htim1, etc.)
 * 		   tim_channel - channel of PWM pin (TIM_CHANNEL_1, etc.)
 * 		   u32_duty_cycle - the DC duty cycle on which the sine wave will be added. [0 - htimx.Init.Period]
 * 		   NOTE: the duty cycle scale for this function is different from pwm_Start.
 * 		   u16_ampl - the amplitude of the sine wave [0 - htimx.Init.Period]
 * 		   u8_conv_id - offset for the DMA memory for sine wave
 */
void pwm_sine_Start(TIM_HandleTypeDef htimx, uint32_t tim_channel, uint32_t u32_dc_duty_cycle, uint16_t u16_ampl, uint8_t u8_conv_id)
{
	int32_t i32_pwm_ampl[SINE_RES_500HZ] = {0}; // amplitude of the sine wave expressed as a % of the pwm period
	int32_t dc_check[SINE_RES_500HZ] = {0};
	int32_t i32_max_period = (int32_t)htimx.Init.Period;
	uint32_t *p_dma; // pointer to the location of DMA memory

	for(uint8_t i=0; i<(uint8_t)SINE_RES_500HZ; i++)
	{
		i32_pwm_ampl[i] = ((int32_t)i16_sine500hz_lookup[i] * (int32_t)u16_ampl * i32_max_period) / (1000 * 1000);
		dc_check[i] = (int32_t)u32_dc_duty_cycle + i32_pwm_ampl[i];
		u32_sine_duty_cycle[i+u8_conv_id*SINE_RES_500HZ] = (uint32_t)max(min(i32_max_period, dc_check[i]), 0);
	}
	p_dma = &u32_sine_duty_cycle[u8_conv_id*SINE_RES_500HZ];

	HAL_TIM_PWM_Start_DMA(&htimx, tim_channel, p_dma, (uint16_t)SINE_RES_500HZ);
	HAL_Delay(10);

}
Example #5
0
/* pwm_sine_Start
 * Description: Inject a sinusoidal signal using PWM
 * Inputs: htimx - timer handle of PWM pin (htim1, etc.)
 * 		   tim_channel - channel of PWM pin (TIM_CHANNEL_1, etc.)
 * 		   u32_duty_cycle - the DC duty cycle on which the sine wave will be added. [0 - htimx.Init.Period]
 * 		   NOTE: the duty cycle scale for this function is different from pwm_Start.
 */
void pwm_sine_Start(TIM_HandleTypeDef htimx, uint32_t tim_channel, uint32_t u32_dc_duty_cycle, uint16_t u16_ampl)
{
	//u32_sine_duty_cycle = {0};
	int32_t i32_pwm_ampl[SINE_RES_500HZ] = {0}; // amplitude of the sine wave expressed as a % of the pwm period
	//uint32_t u32_pwm_duty_cycle[SINE_RESOLUTION] = {0}; // duty cycle expressed as [0 - htimx.Init.Period]
	int32_t dc_check[SINE_RES_500HZ] = {0};
	int32_t i32_max_period = (int32_t)htimx.Init.Period;

	for(uint8_t i=0; i<(uint8_t)SINE_RES_500HZ; i++)
	{
		i32_pwm_ampl[i] = ((int32_t)i16_sine500hz_lookup[i] * (int32_t)u16_ampl * i32_max_period) / (1000 * 1000);
		dc_check[i] = (int32_t)u32_dc_duty_cycle + i32_pwm_ampl[i];
		u32_sine_duty_cycle[i] = (uint32_t)max(min(i32_max_period, dc_check[i]), 0);
	}

	HAL_TIM_PWM_Start_DMA(&htimx, tim_channel, u32_sine_duty_cycle, (uint16_t)SINE_RES_500HZ);
	HAL_Delay(10);

}
void pwmWriteDshotInt(uint8_t index, uint16_t value)
{
    motorDmaOutput_t *const motor = &dmaMotors[index];

    if (!motor->timerHardware || !motor->timerHardware->dmaRef) {
        return;
    }

    uint16_t packet = prepareDshotPacket(motor, value);

    uint8_t bufferSize = loadDmaBuffer(motor, packet);

    if (motor->timerHardware->output & TIMER_OUTPUT_N_CHANNEL) {
        if (HAL_TIMEx_PWMN_Start_DMA(&motor->TimHandle, motor->timerHardware->channel, motor->dmaBuffer, bufferSize) != HAL_OK) {
            /* Starting PWM generation Error */
            return;
        }
    } else {
        if (HAL_TIM_PWM_Start_DMA(&motor->TimHandle, motor->timerHardware->channel, motor->dmaBuffer, bufferSize) != HAL_OK) {
            /* Starting PWM generation Error */
            return;
        }
    }
}
Example #7
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{

  /* STM32F3xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure LED3 */
  BSP_LED_Init(LED3);

  /* Configure the system clock to have a system clock = 72 Mhz */
  SystemClock_Config();

  /* Compute the value of ARR regiter to generate signal frequency at 17.57 Khz */
  uhTimerPeriod = (uint32_t) ((SystemCoreClock / 17570 ) - 1);
  /* Compute CCR1 value to generate a duty cycle at 75% */
  aCCValue_Buffer[0] = (uint32_t)(((uint32_t) 75 * (uhTimerPeriod - 1)) / 100);
  /* Compute CCR2 value to generate a duty cycle at 50% */
  aCCValue_Buffer[1] = (uint32_t)(((uint32_t) 50 * (uhTimerPeriod - 1)) / 100);
  /* Compute CCR3 value to generate a duty cycle at 25% */
  aCCValue_Buffer[2] = (uint32_t)(((uint32_t) 25 * (uhTimerPeriod - 1)) / 100);
  
  
  /*##-1- Configure the TIM peripheral #######################################*/ 
  /* --------------------------------------------------------------------------- 
    TIM1 input clock (TIM1CLK) is set to APB2 clock (PCLK2), since APB2 
    prescaler is 1.   
    TIM1CLK = PCLK2  
    PCLK2 = HCLK
    => TIM1CLK = HCLK = SystemCoreClock
  
  TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
  SystemCoreClock is set to 72 MHz for STM32F3xx devices.

  The objective is to configure TIM1 channel 2 to generate complementary PWM
  signal with a frequency equal to 17.57 KHz:
     - TIM1_Period = (SystemCoreClock / 17570) - 1
  and a variable duty cycle that is changed by the DMA after a specific number of
  Update DMA request.

  The number of this repetitive requests is defined by the TIM1 Repetion counter,
  each 4 Update Requests, the TIM1 Channel 2 Duty Cycle changes to the next new 
  value defined by the aCCValue_Buffer.
  
    Note: 
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f3xx.c file.
     Each time the core clock (HCLK) changes, user had to update SystemCoreClock 
     variable value. Otherwise, any configuration based on this variable will be incorrect.
     This variable is updated in three ways:
      1) by calling CMSIS function SystemCoreClockUpdate()
      2) by calling HAL API function HAL_RCC_GetSysClockFreq()
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency     
  -----------------------------------------------------------------------------*/
  /* Initialize TIM1 peripheral as follows:
      + Period = TimerPeriod (To have an output frequency equal to 17.570 KHz)
      + Repetition Counter = 3
      + Prescaler = 0
      + ClockDivision = 0
      + Counter direction = Up
  */
  TimHandle.Instance = TIMx;
  
  TimHandle.Init.Period            = uhTimerPeriod;
  TimHandle.Init.RepetitionCounter = 3;
  TimHandle.Init.Prescaler         = 0;
  TimHandle.Init.ClockDivision     = 0;
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /*##-2- Configure the PWM channel 2 ########################################*/ 
  sConfig.OCMode     = TIM_OCMODE_PWM1;
  sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfig.Pulse      = aCCValue_Buffer[0];
  if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK)
  {
    /* Configuration Error */
    Error_Handler();
  }

  /*##-3- Start PWM signal generation in DMA mode ############################*/ 
  if(HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_2, aCCValue_Buffer, 3) != HAL_OK)
  {
    /* Starting PWM generation Error */
    Error_Handler();
  }

  while (1)
  {
  }

}
Example #8
0
/**
  * @brief  TIM3, TIM4, TIM8 configuration
  * @param  None
  * @retval None
  */
static void TIM_Config(void) {
	TIM_MasterConfigTypeDef sMasterConfig;
	
	/*########## TIM3 peripheral - PING (1 Hz) ##########*/
	TimHandle3.Instance = TIM3;
	TimHandle3.Init.Period = 10000;
	TimHandle3.Init.Prescaler = (uint32_t)(((SystemCoreClock / 2) / 10000) - 1); //10kHz
	// T = 1/f = 1/10k = 0,0001 ; time = Period * T = 1s
	TimHandle3.Init.ClockDivision = 0;
	TimHandle3.Init.CounterMode = TIM_COUNTERMODE_UP;
	if(HAL_TIM_OC_Init(&TimHandle3) != HAL_OK) {
		Error_Handler();
	}
	// Configure the Output Compare channel:
	sConfig.OCMode = TIM_OCMODE_TOGGLE;
	sConfig.Pulse = 100;
	sConfig.OCPolarity = TIM_OCPOLARITY_LOW;
	if(HAL_TIM_OC_ConfigChannel(&TimHandle3, &sConfig, TIM_CHANNEL_1) != HAL_OK) {
		Error_Handler();
	}
	if(HAL_TIM_OC_Start_IT(&TimHandle3, TIM_CHANNEL_1) != HAL_OK) {
		Error_Handler();
	}
	
	/*########## TIM4 peripheral - UDP (10 Hz) ##########*/
	TimHandle4.Instance = TIM4;
	TimHandle4.Init.Period = 10000;
	TimHandle4.Init.Prescaler = (uint32_t)(((SystemCoreClock / 2) / 100000) - 1); //100kHz
	// T = 1/f = 1/100k = 0,00001 ; time = Period * T = 0,1s
	TimHandle4.Init.ClockDivision = 0;
	TimHandle4.Init.CounterMode = TIM_COUNTERMODE_UP;
	if(HAL_TIM_OC_Init(&TimHandle4) != HAL_OK) {
		Error_Handler();
	}
	if(HAL_TIM_OC_ConfigChannel(&TimHandle4, &sConfig, TIM_CHANNEL_2) != HAL_OK) {
		Error_Handler();
	}
	if(HAL_TIM_OC_Start_IT(&TimHandle4, TIM_CHANNEL_2) != HAL_OK) {
		Error_Handler();
	}
	
	/*########## TIM8 peripheral - ADC ##########*/
	TimHandle8.Instance = TIM8;
	TimHandle8.Init.Period = 0x3C;
	TimHandle8.Init.Prescaler = 0;
	TimHandle8.Init.ClockDivision = 0;
	TimHandle8.Init.CounterMode = TIM_COUNTERMODE_UP;
	TimHandle8.Init.RepetitionCounter = 0x0;
	if(HAL_TIM_Base_Init(&TimHandle8) != HAL_OK) {
		Error_Handler();
	}
	// TIM8 TRGO selection:
	sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
	sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
	if(HAL_TIMEx_MasterConfigSynchronization(&TimHandle8, &sMasterConfig) != HAL_OK) {
		Error_Handler(); //TIM8 TRGO selection Error
	}
	if(HAL_TIM_Base_Start(&TimHandle8) != HAL_OK) {
		Error_Handler(); //Counter Enable Error
	}
	
	/*########## TIM1 peripheral - PWM ##########*/
	TimHandle1.Instance = TIM1;
	TimHandle1.Init.Period = uhTimerPeriod;
	TimHandle1.Init.RepetitionCounter = 3;
	TimHandle1.Init.Prescaler = 0;
	TimHandle1.Init.ClockDivision = 0;
	TimHandle1.Init.CounterMode = TIM_COUNTERMODE_UP;
	if(HAL_TIM_PWM_Init(&TimHandle1) != HAL_OK) {
		Error_Handler();
	}
	// Configure the PWM channel 3:
	sConfig.OCMode = TIM_OCMODE_PWM1;
	sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfig.Pulse = aCCValue_Buffer;
	if(HAL_TIM_PWM_ConfigChannel(&TimHandle1, &sConfig, TIM_CHANNEL_3) != HAL_OK) {
		Error_Handler();
	}
	// Start PWM signal generation in DMA mode:
	if(HAL_TIM_PWM_Start_DMA(&TimHandle1, TIM_CHANNEL_3, &aCCValue_Buffer, 1) != HAL_OK) {
		Error_Handler();
	}
}
Example #9
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 168 Mhz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);  

  
  /* Compute the value of ARR regiter to generate signal frequency at 17.57 Khz */
  uhTimerPeriod = (uint32_t) ((SystemCoreClock / 17570 ) - 1);
  /* Compute CCR1 value to generate a duty cycle at 75% */
  aCCValue_Buffer[0] = (uint32_t)(((uint32_t) 75 * (uhTimerPeriod - 1)) / 100);
  /* Compute CCR2 value to generate a duty cycle at 50% */
  aCCValue_Buffer[1] = (uint32_t)(((uint32_t) 50 * (uhTimerPeriod - 1)) / 100);
  /* Compute CCR3 value to generate a duty cycle at 25% */
  aCCValue_Buffer[2] = (uint32_t)(((uint32_t) 25 * (uhTimerPeriod - 1)) / 100);
  
  
  /*##-1- Configure the TIM peripheral #######################################*/ 
  /* ---------------------------------------------------------------------------
  TIM1 input clock (TIM1CLK) is set to 2 * APB2 clock (PCLK2), since APB2 
  prescaler is different from 1.   
    TIM1CLK = 2 * PCLK2  
    PCLK2 = HCLK / 2 
    => TIM1CLK = 2 * (HCLK / 2) = HCLK = SystemCoreClock
  
  TIM1CLK = SystemCoreClock, Prescaler = 0, TIM1 counter clock = SystemCoreClock
  SystemCoreClock is set to 168 MHz for STM32F4xx devices.

  The objective is to configure TIM1 channel 3 to generate complementary PWM
  signal with a frequency equal to 17.57 KHz:
     - TIM1_Period = (SystemCoreClock / 17570) - 1
  and a variable duty cycle that is changed by the DMA after a specific number of
  Update DMA request.

  The number of this repetitive requests is defined by the TIM1 Repetion counter,
  each 3 Update Requests, the TIM1 Channel 3 Duty Cycle changes to the next new 
  value defined by the aSRC_Buffer.
  
  Note: 
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
     Each time the core clock (HCLK) changes, user had to update SystemCoreClock 
     variable value. Otherwise, any configuration based on this variable will be incorrect.
     This variable is updated in three ways:
      1) by calling CMSIS function SystemCoreClockUpdate()
      2) by calling HAL API function HAL_RCC_GetSysClockFreq()
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency  
  -----------------------------------------------------------------------------*/
  /* Initialize TIM3 peripheral as follow:
      + Period = TimerPeriod (To have an output frequency equal to 17.570 KHz)
      + Repetition Counter = 3
      + Prescaler = 0
      + ClockDivision = 0
      + Counter direction = Up
  */
  TimHandle.Instance = TIMx;
  
  TimHandle.Init.Period            = uhTimerPeriod;
  TimHandle.Init.RepetitionCounter = 3;
  TimHandle.Init.Prescaler         = 0;
  TimHandle.Init.ClockDivision     = 0;
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /*##-2- Configure the PWM channel 3 ########################################*/ 
  sConfig.OCMode     = TIM_OCMODE_PWM1;
  sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfig.Pulse      = aCCValue_Buffer[0];
  if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
  {
    /* Configuration Error */
    Error_Handler();
  }
  
  /*##-3- Start PWM signal generation in DMA mode ############################*/ 
  if(  HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_3, aCCValue_Buffer, 3) != HAL_OK)
  {
    /* Starting PWM generation Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while (1)
  {
  }

}
Example #10
0
/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */
int main(void)
{
 /* This sample code shows how to use DMA with TIM3 Update request to transfer
  Data from memory to TIM3 Capture Compare Register 3 (CCR3), through the
  STM32F4xx HAL API. To proceed, 3 steps are required */

  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch
       - Systick timer is configured by default as source of time base, but user 
         can eventually implement his proper time base source (a general purpose 
         timer for example or other time source), keeping in mind that Time base 
         duration should be kept 1ms since PPP_TIMEOUT_VALUEs are defined and 
         handled in milliseconds basis.
       - Set NVIC Group Priority to 4
       - Low Level Initialization
     */
  HAL_Init();

  /* Configure the system clock to 180 MHz */
  SystemClock_Config();

  /* Configure LED2 */
  BSP_LED_Init(LED2);

  /* Compute the value of ARR regiter to generate signal frequency at 17.57 Khz */
  uwTimerPeriod = (uint32_t)(((SystemCoreClock/2) / 17570) - 1);
  /* Compute CCR1 value to generate a duty cycle at 75% */
  aCCValue_Buffer[0] = (uint32_t)(((uint32_t) 75 * (uwTimerPeriod - 1)) / 100);
  /* Compute CCR2 value to generate a duty cycle at 50% */
  aCCValue_Buffer[1] = (uint32_t)(((uint32_t) 50 * (uwTimerPeriod - 1)) / 100);
  /* Compute CCR3 value to generate a duty cycle at 25% */
  aCCValue_Buffer[2] = (uint32_t)(((uint32_t) 25 * (uwTimerPeriod - 1)) / 100);

  /*##-1- Configure the TIM peripheral #######################################*/
  /* ---------------------------------------------------------------------------
  * TIM3 input clock is set to APB1 clock (PCLK1), 
   * if (APB1 prescaler = 1) x1 else x2
   * prescaler is 4.
   * TIM1CLK = (HCLK/4) x2 = (HCLK/2)

  TIM3CLK = SystemCoreClock, Prescaler = 0, TIM3 counter clock = SystemCoreClock/2
  SystemCoreClock is set to 180 MHz for STM32F4xx devices.

  The objective is to configure TIM3 channel 3 to generate a PWM
  signal with a frequency equal to 17.57 KHz:
     - TIM3_Period = ((SystemCoreClock/2) / 17570) - 1
  and a variable duty cycle that is changed by the DMA after a specific number of
  Update DMA request.

  The number of this repetitive requests is defined by the TIM3 Repetition counter,
  each 4 Update Requests, the TIM3 Channel 3 Duty Cycle changes to the next new
  value defined by the aCCValue_Buffer.

    Note:
     SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f4xx.c file.
     Each time the core clock (HCLK) changes, user had to update SystemCoreClock
     variable value. Otherwise, any configuration based on this variable will be incorrect.
     This variable is updated in three ways:
      1) by calling CMSIS function SystemCoreClockUpdate()
      2) by calling HAL API function HAL_RCC_GetSysClockFreq()
      3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency
  -----------------------------------------------------------------------------*/
  /* Initialize TIM3 peripheral as follows:
      + Period = TimerPeriod (To have an output frequency equal to 17.570 KHz)
      + Repetition Counter = 3
      + Prescaler = 0
      + ClockDivision = 0
      + Counter direction = Up
  */
  TimHandle.Instance               = TIMx;
  TimHandle.Init.Period            = uwTimerPeriod;
  TimHandle.Init.RepetitionCounter = 3;
  TimHandle.Init.Prescaler         = 0;
  TimHandle.Init.ClockDivision     = 0;
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  if (HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }

  /*##-2- Configure the PWM channel 3 ########################################*/
  sConfig.OCMode       = TIM_OCMODE_PWM1;
  sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
  sConfig.Pulse        = aCCValue_Buffer[0];
  sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
  sConfig.OCFastMode   = TIM_OCFAST_DISABLE;
  sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
  sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
  {
    /* Configuration Error */
    Error_Handler();
  }

  /*##-3- Start PWM signal generation in DMA mode ############################*/
  if (HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_3, aCCValue_Buffer, 3) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }

  while (1)
  {
  }

}
Example #11
0
/**
  * @brief  Main program
  * @param  None
  * @retval None
  */
int main(void)
{
  /* STM32F4xx HAL library initialization:
       - Configure the Flash prefetch, instruction and Data caches
       - Configure the Systick to generate an interrupt each 1 msec
       - Set NVIC Group Priority to 4
       - Global MSP (MCU Support Package) initialization
     */
  HAL_Init();
  
  /* Configure the system clock to 180 MHz */
  SystemClock_Config();
  
  /* Configure LED3 */
  BSP_LED_Init(LED3);

  /* Compute the value of ARR regiter to generate signal frequency at 17.57 Khz */
  uwTimerPeriod =      (uint32_t)(SystemCoreClock / 17570 ) - 1;
  /* Compute CCR1 value to generate a duty cycle at 75% */
  aCCValue_Buffer[0] = (uint32_t) ((75 * (uwTimerPeriod - 1)) / 100);
  /* Compute CCR2 value to generate a duty cycle at 50% */
  aCCValue_Buffer[1] = (uint32_t) ((50 * (uwTimerPeriod - 1)) / 100);
  /* Compute CCR3 value to generate a duty cycle at 25% */
  aCCValue_Buffer[2] = (uint32_t) ((25 * (uwTimerPeriod - 1)) / 100);
  
  
  /*##-1- Configure the TIM peripheral #######################################*/ 
  /* Initialize TIM3 peripheral as follow:
      + Period = TimerPeriod (To have an output frequency equal to 17.570 KHz)
      + Repetition Counter = 3
      + Prescaler = 0
      + ClockDivision = 0
      + Counter direction = Up
  */
  TimHandle.Instance = TIMx;
  
  TimHandle.Init.Period            = uwTimerPeriod;
  TimHandle.Init.RepetitionCounter = 3;
  TimHandle.Init.Prescaler         = 0;
  TimHandle.Init.ClockDivision     = 0;
  TimHandle.Init.CounterMode       = TIM_COUNTERMODE_UP;
  if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /*##-2- Configure the PWM channel 3 ########################################*/ 
  sConfig.OCMode     = TIM_OCMODE_PWM1;
  sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfig.Pulse      = aCCValue_Buffer[0];
  if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK)
  {
    /* Configuration Error */
    Error_Handler();
  }
  
  /*##-3- Start PWM signal generation in DMA mode ############################*/ 
  if(  HAL_TIM_PWM_Start_DMA(&TimHandle, TIM_CHANNEL_3, aCCValue_Buffer, 3) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }
  
  /* Wait till the channel3 start process finish */
  while(TimHandle.State != HAL_TIM_STATE_READY) 
  {}
  
  /*##-4- Start PWM Complementary signal generation in DMA mode ##############*/ 
  if(  HAL_TIMEx_PWMN_Start_DMA(&TimHandle, TIM_CHANNEL_3, aCCValue_Buffer, 3) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while (1)
  {
  }
}
Example #12
0
int main(void)
{

    /* USER CODE BEGIN 1 */
    init_buffers();
    /* USER CODE END 1 */

    /* MCU Configuration----------------------------------------------------------*/

    /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    /* Configure the system clock */
    SystemClock_Config();

    /* Initialize all configured peripherals */
    MX_GPIO_Init();
    MX_DMA_Init();
    MX_TIM3_Init();
    MX_USART1_UART_Init();
    MX_USART2_UART_Init();

    /* USER CODE BEGIN 2 */
    HAL_TIM_PWM_Start_DMA(&htim3, TIM_CHANNEL_4, (uint32_t *)&PWM_Buffer, PWM_BUFFER_SIZE);
    /* USER CODE END 2 */

    /* Infinite loop */
    /* USER CODE BEGIN WHILE */

    //extern UART_HandleTypeDef huart2;
    extern UART_HandleTypeDef huart1;

    //__HAL_UART_ENABLE_IT(&huart2, UART_IT_RXNE);
    __HAL_UART_ENABLE_IT(&huart1, UART_IT_RXNE);


    while (1)
    {
        /* USER CODE END WHILE */

        /* USER CODE BEGIN 3 */

        switch(g_CurMode)
        {
        case BREATHING:
            HAL_Delay(10);
            pattern4();
            break;
        case WAVE:
            HAL_Delay(40);
            pattern3();
            break;
        case SHINING:
            HAL_Delay(10);
            pattern2();
            break;
        case STACK:
            HAL_Delay(10);
            pattern1();
            break;
        case CUSTOM:
            for(int i = 0; i < FRAMEBUFFER_SIZE; i++)
            {
                ws2812_framebuffer[i].red = g_Red;
                ws2812_framebuffer[i].green = g_Green;
                ws2812_framebuffer[i].blue = g_Blue;
            }
            break;
        }
    }
    /* USER CODE END 3 */

}