Ejemplo n.º 1
0
void start_adc_pwm(){

    //Enable ADC and interrupts
    __HAL_ADC_ENABLE(&hadc2);
    //Warp field stabilize.
    osDelay(2);
    __HAL_ADC_ENABLE_IT(&hadc2, ADC_IT_JEOC);

    //Init PWM
    int half_load = htim1.Instance->ARR/2;
    htim1.Instance->CCR1 = half_load;
    htim1.Instance->CCR2 = half_load;
    htim1.Instance->CCR3 = half_load;

    //This hardware obfustication layer really is getting on my nerves
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_2);
    HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3);
    HAL_TIMEx_PWMN_Start(&htim1, TIM_CHANNEL_3);

    htim1.Instance->CCR4 = 1;
    HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_4);

    //Turn off output
    //__HAL_TIM_MOE_DISABLE(&htim1);

}
/******************************************************//**
 * @brief  Start the step clock by using the given frequency
 * @param[in] newFreq in Hz of the step clock
 * @retval None
 * @note The frequency is directly the current speed of the device
 **********************************************************/
void BSP_MotorControlBoard_StartStepClock(uint16_t newFreq)
{
  uint32_t sysFreq = HAL_RCC_GetSysClockFreq();
  uint32_t period = (sysFreq/ (TIMER_PRESCALER * newFreq)) - 1;
  
  __HAL_TIM_SetAutoreload(&hTimStepClock, period);
  __HAL_TIM_SetCompare(&hTimStepClock, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_STEP_CLOCK, period >> 1);
  HAL_TIM_PWM_Start_IT(&hTimStepClock, BSP_MOTOR_CONTROL_BOARD_CHAN_TIMER_STEP_CLOCK);  
}
Ejemplo n.º 3
0
void PWMStart()
{
  HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_1);//known bug:cannot use TIM_CHANNEL_ALL!!!
  HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_2);
  HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_3);
  HAL_TIM_PWM_Start_IT(&htim2,TIM_CHANNEL_4);
  
  HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_1);
  HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_2);
  HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_3);
  HAL_TIM_PWM_Start_IT(&htim3,TIM_CHANNEL_4);
}
Ejemplo n.º 4
0
void test_adc_trigger() {
    //Set trigger to mid phase to check for trigger polarity
    htim1.Instance->CCR4 = 2048;

    HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_4);
    __HAL_ADC_ENABLE(&hadc2);

    //Warp field stabilize.
    osDelay(2);
    __HAL_ADC_ENABLE_IT(&hadc2, ADC_IT_JEOC);
}
Ejemplo n.º 5
0
void CAM_init()
{
	/*
	 * Initialize hardware
	 */
	CAM_GPIO_init();
	CAM_TIM_init(SLOW);
	CAM_ADC_init();

	/*
	 * Initial GPIO pins status
	 */
	RESET_high();
	START_low();
	LOAD_low();
	SIN_low();

	/*
	 * Configure NVIC (interrupts)
	 */
	HAL_NVIC_SetPriority(ADC_IRQn, 0, 0);
	HAL_NVIC_EnableIRQ(ADC_IRQn);

	HAL_NVIC_SetPriority(TIM2_IRQn, 0, 1);
	HAL_NVIC_EnableIRQ(TIM2_IRQn);

	HAL_NVIC_SetPriority(EXTI1_IRQn, 3, 0);
	HAL_NVIC_EnableIRQ(EXTI1_IRQn);

	/*
	 * Initialize library global variables
	 */
	imageCursor = 0;

	flag_captureGoing = 0;
	flag_captureEnd = 0;

	clockPhase = 0;

	acquiring = 0;

	skip = PIXSKIP;

	/*
	 * Start ADC
	 */
	HAL_ADC_Start_IT(&AdcHandle);					// Providing ConvCplt Callback

	/*
	 * Start TIM Base and TIM PWM
	 */
	HAL_TIM_Base_Start_IT(&htim);					// Providing PeriodElapsed Callback
	HAL_TIM_PWM_Start_IT(&htim, TIM_CHANNEL_2);	// Providing PulseFinished Callback
}
Ejemplo n.º 6
0
void CAM_changeClockSpeed(CLOCKSPEED speed)
{
	/* Wait for clock falling edge */
	CAM_waitClockFalling();

	/* Stop the timer */
	HAL_TIM_PWM_Stop_IT(&htim, TIM_CHANNEL_2);
	HAL_TIM_Base_Stop_IT(&htim);

	/* De-initialize the timer */
	HAL_TIM_PWM_DeInit(&htim);
	HAL_TIM_Base_DeInit(&htim);

	/* Re-initialize the timer */
	CAM_TIM_init(speed);

	/* And finally restart it along with PWM */
	HAL_TIM_Base_Start_IT(&htim);					// Providing PeriodElapsed Callback
	HAL_TIM_PWM_Start_IT(&htim, TIM_CHANNEL_2);	// Providing PulseFinished Callback
}
Ejemplo n.º 7
0
/**
 * Callback function implementation.
 * @param htim timer handle which trigger the callback
 */
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim) {
	static uint8_t steerRise = 0;

	if (htim->Instance == TIM4) {

		if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_1) {

			/* TIM4 Channel 1 (Motor input) */
			uwIC1Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);
		} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_3) {

			/* TIM4 Channel 3 (Steer input) */
			uwIC2Value = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_3) - uwIC1Value;
			steerRise = 1;

			if (_signalDetected == RESET) {
				_signalDetected = SET;
				if (_servo == ENABLE)
					HAL_TIM_PWM_Start_IT(&htim4, TIM_CHANNEL_4);

			}

			/* Connect to servo output */
			if (_connected == ENABLE) {
				BSP_Radio_SetSteer(uwIC2Value - 1500);
			}
		} else if (htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2) {

			/* TIM4 Channel 2 (Motor input, fall edge) */
			if (!steerRise && _signalDetected == SET) {
				_signalDetected = RESET;
				HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);
			} else if (steerRise) {
				steerRise = 0;
			}
		}
	}
}
Ejemplo n.º 8
0
Archivo: main.c Proyecto: z80/stm32f429
/**
  * @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);

  /*##-1- Configure the TIM peripheral #######################################*/
  /* -----------------------------------------------------------------------
  In this example TIM3 input clock (TIM3CLK) is set to 4 * APB1 clock (PCLK1), 
  since TIMPRE bit from RCC_DCKCFGR register is set.   
      TIM3CLK = 4 * PCLK1  
      PCLK1 = HCLK / 4 
      => TIM3CLK = HCLK = SystemCoreClock
          
  For TIM3CLK equal to SystemCoreClock, TIM3 counter clock is computed as follows:
       TIM3 counter clock = TIM3CLK / (Prescaler + 1)
                          = SystemCoreClock / (Prescaler + 1)
                          = 36MHz

  For ARR equal to (1800 - 1), the TIM3 output clock is computed as follows:
       TIM3 output clock = TIM3 counter clock / (ARR + 1)
                         = 20KHZ
    
  The TIM3 CCR1 register value is equal to 900, so the TIM3 Channel 1 generates a 
  PWM signal with a frequency equal to 20 KHz and a duty cycle equal to 50%:
       TIM3 Channel1 duty cycle = (TIM3_CCR1/ TIM3_ARR + 1)* 100 = 50%
  
  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     
  ----------------------------------------------------------------------- */ 
  
  /* Timer clock prescalers selection activation */ 
  __HAL_RCC_TIMCLKPRESCALER(RCC_TIMPRES_ACTIVATED);
  
  /* Initialize TIMx peripheral as follow:
       + Prescaler = (5 - 1)
       + Period = (1800 - 1)
       + ClockDivision = 0
       + Counter direction = Up
  */
  TimHandle.Instance = TIMx;
  
  TimHandle.Init.Prescaler     = PRESCALER_VALUE;
  TimHandle.Init.Period        = PERIOD_VALUE;
  TimHandle.Init.ClockDivision = 0;
  TimHandle.Init.CounterMode   = TIM_COUNTERMODE_UP;
  TimHandle.Init.RepetitionCounter = 0x0;
  if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  {
    /* Initialization Error */
    Error_Handler();
  }
  
  /*##-2- Configure the PWM channels #########################################*/ 
  /* Common configuration for all channels */
  sConfig.OCMode     = TIM_OCMODE_PWM1;
  sConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfig.OCFastMode = TIM_OCFAST_DISABLE;

  /* Set the pulse value for channel 1 */
  sConfig.Pulse = PULSE1_VALUE;  
  if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK)
  {
    /* Configuration Error */
    Error_Handler();
  }
  
  /*##-3- Start PWM signals generation #######################################*/ 
  /* Start channel 1 */
  if(HAL_TIM_PWM_Start_IT(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
  {
    /* Starting Error */
    Error_Handler();
  }
  
  /* Infinite loop */
  while (1)
  {
  }
}