/** * @brief Configures a Timer to emulate an encoder sensor outputs in Backward * direction * @param htim : TIM handle * @retval None */ static void Emulate_Backward_Direction(TIM_HandleTypeDef* htim) { /*## -1- Re-Configure the Pulse ########################################## */ sConfig.Pulse = (EMU_PERIOD * 3 )/4; if(HAL_TIM_OC_ConfigChannel(htim, &sConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } sConfig.Pulse = (EMU_PERIOD * 1 )/4; if(HAL_TIM_OC_ConfigChannel(htim, &sConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*## -2- Start signals generation ######################################### */ if(HAL_TIM_OC_Start(htim, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } if(HAL_TIM_OC_Start(htim, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } }
// Reconfigure the HAL tick using a standard timer instead of systick. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { // Enable timer clock TIM_MST_RCC; // Reset timer TIM_MST_RESET_ON; TIM_MST_RESET_OFF; // Update the SystemCoreClock variable SystemCoreClockUpdate(); // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFF; TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimMasterHandle); // Configure output compare channel 1 for mbed timeout (enabled later when used) HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); // Configure output compare channel 2 for HAL tick HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); // Configure interrupts // Update interrupt used for 32-bit counter // Output compare channel 1 interrupt for mbed timeout // Output compare channel 2 interrupt for HAL tick NVIC_SetVector(TIM_MST_UP_IRQ, (uint32_t)timer_update_irq_handler); NVIC_EnableIRQ(TIM_MST_UP_IRQ); NVIC_SetPriority(TIM_MST_UP_IRQ, 0); NVIC_SetVector(TIM_MST_OC_IRQ, (uint32_t)timer_oc_irq_handler); NVIC_EnableIRQ(TIM_MST_OC_IRQ); NVIC_SetPriority(TIM_MST_OC_IRQ, 1); // Enable interrupts __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick // Enable timer HAL_TIM_Base_Start(&TimMasterHandle); #if 0 // For DEBUG only __GPIOB_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); #endif return HAL_OK; }
// Reconfigure the HAL tick using a standard timer instead of systick. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { RCC_ClkInitTypeDef RCC_ClkInitStruct; uint32_t PclkFreq; // Get clock configuration // Note: PclkFreq contains here the Latency (not used after) HAL_RCC_GetClockConfig(&RCC_ClkInitStruct, &PclkFreq); // Get TIM5 clock value PclkFreq = HAL_RCC_GetPCLK1Freq(); // Enable timer clock TIM_MST_RCC; // Reset timer TIM_MST_RESET_ON; TIM_MST_RESET_OFF; // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFFFFFF; // TIMxCLK = PCLKx when the APB prescaler = 1 else TIMxCLK = 2 * PCLKx if (RCC_ClkInitStruct.APB1CLKDivider == RCC_HCLK_DIV1) TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq) / 1000000) - 1; // 1 us tick else TimMasterHandle.Init.Prescaler = (uint16_t)((PclkFreq * 2) / 1000000) - 1; // 1 us tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; HAL_TIM_OC_Init(&TimMasterHandle); NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); NVIC_EnableIRQ(TIM_MST_IRQ); // Channel 1 for mbed timeout HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); // Channel 2 for HAL tick HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); #if 0 // For DEBUG only __GPIOB_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); #endif return HAL_OK; }
void lp_ticker_init(void) { if (!lp_ticker_inited) { lp_ticker_inited = 1; __TIM2_CLK_ENABLE(); __TIM2_FORCE_RESET(); __TIM2_RELEASE_RESET(); // Update the SystemCoreClock variable SystemCoreClockUpdate(); // Configure time base TimMasterHandle.Instance = TIM2; TimMasterHandle.Init.Period = 0xFFFFFFFF; TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000000) - 1; // 1 ms tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; HAL_TIM_OC_Init(&TimMasterHandle); vIRQ_SetVector(TIM2_IRQn, (uint32_t)lp_handler); vIRQ_EnableIRQ(TIM2_IRQn); HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); } }
// Reconfigure the HAL tick using a standard timer instead of systick. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { // Enable timer clock TIM_MST_RCC; // Reset timer TIM_MST_RESET_ON; TIM_MST_RESET_OFF; // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFFFFFF; if ( SystemCoreClock == 16000000 ) { TimMasterHandle.Init.Prescaler = (uint32_t)( SystemCoreClock / 1000000) - 1; // 1 µs tick } else { TimMasterHandle.Init.Prescaler = (uint32_t)( SystemCoreClock / 2 / 1000000) - 1; // 1 µs tick } TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; HAL_TIM_OC_Init(&TimMasterHandle); NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); NVIC_EnableIRQ(TIM_MST_IRQ); // Channel 1 for mbed timeout HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); // Channel 2 for HAL tick HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); #if 0 // For DEBUG only __GPIOB_CLK_ENABLE(); GPIO_InitTypeDef GPIO_InitStruct; GPIO_InitStruct.Pin = GPIO_PIN_6; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FAST; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); #endif return HAL_OK; }
// Reconfigure the HAL tick using a standard timer instead of systick. HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority) { // Enable timer clock TIM_MST_RCC; // Reset timer TIM_MST_RESET_ON; TIM_MST_RESET_OFF; // Update the SystemCoreClock variable SystemCoreClockUpdate(); // Configure time base TimMasterHandle.Instance = TIM_MST; TimMasterHandle.Init.Period = 0xFFFFFFFF; TimMasterHandle.Init.Prescaler = (uint32_t)(SystemCoreClock / 1000000) - 1; // 1 us tick TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; HAL_TIM_Base_Init(&TimMasterHandle); // Configure output compare channel 1 for mbed timeout (enabled later when used) HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1); // Configure output compare channel 2 for HAL tick HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_2); PreviousVal = __HAL_TIM_GetCounter(&TimMasterHandle); __HAL_TIM_SetCompare(&TimMasterHandle, TIM_CHANNEL_2, PreviousVal + HAL_TICK_DELAY); // Configure interrupts // Update interrupt used for 32-bit counter // Output compare channel 1 interrupt for mbed timeout // Output compare channel 2 interrupt for HAL tick NVIC_SetVector(TIM_MST_IRQ, (uint32_t)timer_irq_handler); NVIC_EnableIRQ(TIM_MST_IRQ); // Enable interrupts __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_UPDATE); // For 32-bit counter __HAL_TIM_ENABLE_IT(&TimMasterHandle, TIM_IT_CC2); // For HAL tick // Enable timer HAL_TIM_Base_Start(&TimMasterHandle); return HAL_OK; }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* 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 LED1, LED2 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED2); BSP_LED_Init(LED3); /* Turn off LED1 */ BSP_LED_Off(LED1); /* De-assert trig ouput pin */ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7, GPIO_PIN_RESET); /* TIM3 input clock is set to APB1 clock (PCLK1), * if (APB1 prescaler = 1) x1 else x2 * prescaler is 4. * TIM3CLK = (HCLK/4) x2 = (HCLK/2) * Compute the prescaler value to have TIMx counter clock equal to 10 kHz */ uwPrescalerValue = ((SystemCoreClock/2) / 10000) - 1; /*##-1- Configure the TIM peripheral #######################################*/ /* Initialize TIMx peripheral as follow: + Prescaler = (SystemCoreClock)/10000 - 1; + Period = 65535 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIM3; TimHandle.Init.Period = 65535; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Output Compare channels #########################################*/ /* Common configuration for all channels */ sConfig.OCMode = TIM_OCMODE_ACTIVE; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; /* Set the pulse (delay1) value for channel 1 */ sConfig.Pulse = PULSE1_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay2) value for channel 2 */ sConfig.Pulse = PULSE2_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay3) value for channel 3 */ sConfig.Pulse = PULSE3_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay4) value for channel 4 */ sConfig.Pulse = PULSE4_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Turn On LED2: use PB.07 edge as reference ####################*/ /* Turn on LED2 */ BSP_LED_On(LED2); /* Assert trig ouput pin */ HAL_GPIO_WritePin(GPIOB,GPIO_PIN_7,GPIO_PIN_SET); /*##-4- Start signals generation #######################################*/ /* Start channel 1 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 2 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 3 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 4 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK) { /* Starting Error */ Error_Handler(); } while (1) { } }
/** * @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 LED1 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /* Compute the prescaler value to have TIMx counter clock equal to 2 KHz */ uwPrescalerValue = ((SystemCoreClock /2) / 2000) - 1; /*##-1- Configure the TIM peripheral #######################################*/ /* --------------------------------------------------------------------------- TIM3 Configuration: Output Compare Active Mode: In this example TIM3 input clock (TIM3CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK = HCLK / 2 = SystemCoreClock /2 To get TIM3 counter clock at 2 KHz, the prescaler is computed as follows: Prescaler = (TIM3CLK / TIM3 counter clock) - 1 Prescaler = ((SystemCoreClock /2) /1 KHz) - 1 Generate 4 signals with 4 different delays: TIM3_CH1 delay = uhCCR1_Val/TIM3 counter clock = 500 ms TIM3_CH2 delay = uhCCR2_Val/TIM3 counter clock = 250 ms TIM3_CH3 delay = uhCCR3_Val/TIM3 counter clock = 125 ms TIM3_CH4 delay = uhCCR4_Val/TIM3 counter clock = 62.5 ms 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 TIMx peripheral as follow: + Prescaler = (SystemCoreClock/2)/2000 + Period = 65535 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIMx; TimHandle.Init.Period = 65535; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Output Compare channels ##############################*/ /* Common configuration for all channels */ sConfig.OCMode = TIM_OCMODE_ACTIVE; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; /* Set the pulse (delay1) value for channel 1 */ sConfig.Pulse = PULSE1_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay2) value for channel 2 */ sConfig.Pulse = PULSE2_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay3) value for channel 3 */ sConfig.Pulse = PULSE3_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay4) value for channel 4 */ sConfig.Pulse = PULSE4_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Turn On LED1: use PG6 falling edge as reference ####################*/ /* Turn on LED1 */ BSP_LED_On(LED1); /*##-4- Start signals generation ###########################################*/ /* Start channel 1 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 2 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 3 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 4 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @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 LED1 and LED3 */ BSP_LED_Init(LED1); BSP_LED_Init(LED3); /* Compute the prescaler value to have TIMx counter clock equal to 2 KHz */ uwPrescalerValue = ((SystemCoreClock /2) / 2000) - 1; /*##-1- Configure the TIM peripheral #######################################*/ /* Initialize TIMx peripheral as follow: + Prescaler = (SystemCoreClock/2)/2000 + Period = 65535 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIMx; TimHandle.Init.Period = 65535; TimHandle.Init.Prescaler = uwPrescalerValue; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the Output Compare channels ##############################*/ /* Common configuration for all channels */ sConfig.OCMode = TIM_OCMODE_ACTIVE; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; /* Set the pulse (delay1) value for channel 1 */ sConfig.Pulse = PULSE1_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay2) value for channel 2 */ sConfig.Pulse = PULSE2_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay3) value for channel 3 */ sConfig.Pulse = PULSE3_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse (delay4) value for channel 4 */ sConfig.Pulse = PULSE4_VALUE; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_4) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Turn On LED1: use PG6 falling edge as reference ####################*/ /* Turn on LED1 */ BSP_LED_On(LED1); /*##-4- Start signals generation ###########################################*/ /* Start channel 1 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 2 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 3 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 4 in Output compare mode */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_4) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* STM32F2xx 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 120 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /* Timers synchronisation in cascade mode with an external trigger ----- 1/TIM1 is configured as Master Timer: - Toggle Mode is used - The TIM1 Enable event is used as Trigger Output 2/TIM1 is configured as Slave Timer for an external Trigger connected to TIM1 TI2 pin (TIM1 CH2 configured as input pin): - The TIM1 TI2FP2 is used as Trigger Input - Rising edge is used to start and stop the TIM1: Gated Mode. 3/TIM3 is slave for TIM1 and Master for TIM4, - Toggle Mode is used - The ITR1(TIM1) is used as input trigger - Gated mode is used, so start and stop of slave counter are controlled by the Master trigger output signal(TIM1 enable event). - The TIM3 enable event is used as Trigger Output. 4/TIM4 is slave for TIM3, - Toggle Mode is used - The ITR2(TIM3) is used as input trigger - Gated mode is used, so start and stop of slave counter are controlled by the Master trigger output signal(TIM3 enable event). 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 TIM3/TIM4 input clock (TIM3CLK/TIM4CLK) is set to 2 * APB1 clock (PCLK1), since APB1 prescaler is different from 1. TIM3CLK/TIM4CLK = 2 * PCLK1 PCLK1 = HCLK / 4 => TIM3CLK/TIM4CLK = HCLK / 2 = SystemCoreClock /2 The TIM1CLK is fixed to 120 MHZ, the Prescaler is equal to 5 so the TIMx clock counter is equal to 28 MHz. The TIM3CLK and TIM4CLK are fixed to 84 MHZ, the Prescaler is equal to 5 so the TIMx clock counter is equal to 14 MHz. The Three Timers are running at: TIMx frequency = TIMx clock counter/ 2*(TIMx_Period + 1) = 93,3 KHz. The starts and stops of the TIM1 counters are controlled by the external trigger. The TIM3 starts and stops are controlled by the TIM1, and the TIM4 starts and stops are controlled by the TIM3. Note: SystemCoreClock variable holds HCLK frequency and is defined in system_stm32f2xx.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 -------------------------------------------------------------------- */ /* Set Timers instance */ TimMasterHandle.Instance = TIM1; TimSlaveMasterHandle.Instance = TIM3; TimSlaveHandle.Instance = TIM4; /*======= Master1/Slave for an external trigger configuration : TIM1 =======*/ /* Initialize TIM1 peripheral in Output Compare mode*/ TimMasterHandle.Init.Period = 149; TimMasterHandle.Init.Prescaler = 5; TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimMasterHandle.Init.RepetitionCounter = 0; if(HAL_TIM_OC_Init(&TimMasterHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the output: Channel_1 */ sOCConfig.OCMode = TIM_OCMODE_TOGGLE; sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; if(HAL_TIM_OC_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure the Input: channel_2 */ sICConfig.ICPolarity = TIM_ICPOLARITY_RISING; sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; sICConfig.ICPrescaler = TIM_ICPSC_DIV1; sICConfig.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&TimMasterHandle, &sICConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM1 in Gated Slave mode for the external trigger (Filtered Timer Input 2) */ sSlaveConfig.InputTrigger = TIM_TS_TI2FP2; sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED; if( HAL_TIM_SlaveConfigSynchronization(&TimMasterHandle, &sSlaveConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM1 in Master Enable mode & use the update event as Trigger Output (TRGO) */ sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; if( HAL_TIMEx_MasterConfigSynchronization(&TimMasterHandle, &sMasterConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*=== End of Master1/Slave for an external trigger configuration : TIM1 ====*/ /*=================== Slave/Master configuration : TIM3 ====================*/ /* Initialize TIM3 peripheral in Output Compare mode*/ TimSlaveMasterHandle.Init.Period = 74; TimSlaveMasterHandle.Init.Prescaler = 5; TimSlaveMasterHandle.Init.ClockDivision = 0; TimSlaveMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimSlaveMasterHandle.Init.RepetitionCounter = 0; if(HAL_TIM_OC_Init(&TimSlaveMasterHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the Output Compare channel_1 */ sOCConfig.OCMode = TIM_OCMODE_TOGGLE; sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; if(HAL_TIM_OC_ConfigChannel(&TimSlaveMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM3 in Gated Slave mode for the internal trigger 0(ITR0) */ sSlaveConfig.InputTrigger = TIM_TS_ITR0; sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED; if( HAL_TIM_SlaveConfigSynchronization(&TimSlaveMasterHandle, &sSlaveConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM3 in Master Enable mode & use the update event as Trigger Output (TRGO) */ sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; if( HAL_TIMEx_MasterConfigSynchronization(&TimSlaveMasterHandle, &sMasterConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*=============== End of Slave/Master configuration : TIM3 =================*/ /*====================== Slave configuration : TIM4 ========================*/ /* Initialize TIM4 peripheral in Output Compare mode*/ TimSlaveHandle.Init.Period = 74; TimSlaveHandle.Init.Prescaler = 5; TimSlaveHandle.Init.ClockDivision = 0; TimSlaveHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimSlaveHandle.Init.RepetitionCounter = 0; if(HAL_TIM_OC_Init(&TimSlaveHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the Output Compare channel_1 */ sOCConfig.OCMode = TIM_OCMODE_TOGGLE; sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; if(HAL_TIM_OC_ConfigChannel(&TimSlaveHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM4 in Gated Slave mode for the internal trigger 2(ITR2) */ sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED; sSlaveConfig.InputTrigger = TIM_TS_ITR2; if(HAL_TIM_SlaveConfigSynchronization(&TimSlaveHandle, &sSlaveConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*================== End of Slave configuration : TIM4 =====================*/ /* 1- Start Timer1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ /* Start Channel2 in Input Capture */ if(HAL_TIM_IC_Start(&TimMasterHandle, TIM_CHANNEL_2) != HAL_OK) { /* Start Error */ Error_Handler(); } /* Start the Output Compare */ if(HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1) != HAL_OK) { /* Start Error */ Error_Handler(); } /* 2- Start Timer3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ /* Start the Output Compare */ if(HAL_TIM_OC_Start(&TimSlaveMasterHandle, TIM_CHANNEL_1) != HAL_OK) { /* Start Error */ Error_Handler(); } /* 3- Start Timer3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ /* Start the Output Compare */ if(HAL_TIM_OC_Start(&TimSlaveHandle, TIM_CHANNEL_1) != HAL_OK) { /* Start Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @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); /*##-1- Configure the TIM peripheral #######################################*/ /*---------------------------------------------------------------------------- The STM32F4xx TIM1 peripheral offers the possibility to program in advance the configuration for the next TIM1 outputs behaviour (step) and change the configuration of all the channels at the same time. This operation is possible when the COM (commutation) event is used. The COM event can be generated by software by setting the COM bit in the TIM1_EGR register or by hardware (on TRC rising edge). In this example, a software COM event is generated each 1 ms: using the SysTick interrupt. The TIM1 is configured in Timing Mode, each time a COM event occurs, a new TIM1 configuration will be set in advance. ----------------------------------------------------------------------------*/ /* Initialize TIMx peripheral as follow: + Prescaler = 0 + Period = 4095 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIM1; TimHandle.Init.Period = 4095; TimHandle.Init.Prescaler = 0; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the output channels ######################################*/ /* Common configuration for all channels */ sConfig.OCMode = TIM_OCMODE_TIMING; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCIdleState = TIM_OCIDLESTATE_SET; sConfig.OCNIdleState = TIM_OCNIDLESTATE_SET; sConfig.OCFastMode = TIM_OCFAST_DISABLE; /* Set the pulse value for channel 1 */ sConfig.Pulse = 2047; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse value for channel 2 */ sConfig.Pulse = 1023; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse value for channel 3 */ sConfig.Pulse = 511; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Configure the Break stage ##########################################*/ sConfigBK.OffStateRunMode = TIM_OSSR_ENABLE; sConfigBK.OffStateIDLEMode = TIM_OSSI_ENABLE; sConfigBK.LockLevel = TIM_LOCKLEVEL_OFF; sConfigBK.BreakState = TIM_BREAK_ENABLE; sConfigBK.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sConfigBK.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE; sConfigBK.DeadTime = 1; if(HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sConfigBK) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-4- Configure the commutation event: software event ####################*/ HAL_TIMEx_ConfigCommutationEvent_IT(&TimHandle, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE); /*##-5- Start signals generation ###########################################*/ /*--------------------------------------------------------------------------*/ /* Start channel 1 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 1N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* Start channel 2 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 2N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* Start channel 3 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 3N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /* Infinite loop */ while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /* Enable the CPU Cache */ CPU_CACHE_Enable(); /* STM32F7xx HAL library initialization: - Configure the Flash ART accelerator on ITCM interface - 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 216 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /*##-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 PCLK1 = HCLK / 2 => TIM1CLK = HCLK = SystemCoreClock --------------------------------------------------------------------------- */ /* Initialize TIMx peripheral as follow: + Prescaler = 0 + Period = 4095 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIM1; TimHandle.Init.Period = 4095; TimHandle.Init.Prescaler = 0; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the output channels ######################################*/ /* Common configuration for all channels */ sPWMConfig1.OCMode = TIM_OCMODE_TIMING; sPWMConfig1.OCPolarity = TIM_OCPOLARITY_HIGH; sPWMConfig1.OCNPolarity = TIM_OCNPOLARITY_HIGH; sPWMConfig1.OCIdleState = TIM_OCIDLESTATE_SET; sPWMConfig1.OCNIdleState = TIM_OCNIDLESTATE_SET; sPWMConfig1.OCFastMode = TIM_OCFAST_DISABLE; /* Set the pulse value for channel 1 */ sPWMConfig1.Pulse = 2047; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sPWMConfig1, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse value for channel 2 */ sPWMConfig2 = sPWMConfig1; sPWMConfig2.Pulse = 1023; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sPWMConfig2, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse value for channel 3 */ sPWMConfig3 = sPWMConfig1; sPWMConfig3.Pulse = 511; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sPWMConfig3, TIM_CHANNEL_3) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Configure the Break stage ##########################################*/ sBreakConfig.OffStateRunMode = TIM_OSSR_ENABLE; sBreakConfig.OffStateIDLEMode = TIM_OSSI_ENABLE; sBreakConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakConfig.BreakState = TIM_BREAK_ENABLE; sBreakConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sBreakConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE; sBreakConfig.DeadTime = 1; if(HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sBreakConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-4- Configure the commutation event: software event ####################*/ HAL_TIMEx_ConfigCommutationEvent_IT(&TimHandle, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE); /*##-5- Start signals generation ###########################################*/ /*--------------------------------------------------------------------------*/ /* Start channel 1 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 1N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* Start channel 2 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 2N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* Start channel 3 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 3N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Authorize TIM COM event generation */ uwAuthorizeTimComEvent = 1; while (1) { } }
int main(void) { HAL_Init(); Nucleo_BSP_Init(); MX_TIM1_Init(); MX_TIM3_Init(); HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1); HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_2); cnt1 = __HAL_TIM_GET_COUNTER(&htim3); tick = HAL_GetTick(); while (1) { if (HAL_GetTick() - tick > 1000L) { cnt2 = __HAL_TIM_GET_COUNTER(&htim3); if (__HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3)) { if (cnt2 < cnt1) /* Check for counter underflow */ diff = cnt1 - cnt2; else diff = (65535 - cnt2) + cnt1; } else { if (cnt2 > cnt1) /* Check for counter overflow */ diff = cnt2 - cnt1; else diff = (65535 - cnt1) + cnt2; } sprintf(msg, "Difference: %d\r\n", diff); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); speed = ((diff / PULSES_PER_REVOLUTION) / 60); /* If the first three bits of SMCR register are set to 0x3 * then the timer is set in X4 mode (TIM_ENCODERMODE_TI12) * and we need to divide the pulses counter by two, because * they include the pulses for both the channels */ if ((TIM3->SMCR & 0x3) == 0x3) speed /= 2; sprintf(msg, "Speed: %d RPM\r\n", speed); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); dir = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3); sprintf(msg, "Direction: %d\r\n", dir); HAL_UART_Transmit(&huart2, (uint8_t*) msg, strlen(msg), HAL_MAX_DELAY); tick = HAL_GetTick(); cnt1 = __HAL_TIM_GET_COUNTER(&htim3); } if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13) == GPIO_PIN_RESET) { /* Invert rotation by swapping CH1 and CH2 CCR value */ tim1_ch1_pulse = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_1); tim1_ch2_pulse = __HAL_TIM_GET_COMPARE(&htim1, TIM_CHANNEL_2); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_1, tim1_ch2_pulse); __HAL_TIM_SET_COMPARE(&htim1, TIM_CHANNEL_2, tim1_ch1_pulse); } } }
int main(void) { /* USER CODE BEGIN 1 */ /* 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_ADC1_Init(); MX_ADC3_Init(); MX_I2C1_Init(); MX_SPI1_Init(); MX_SPI2_Init(); MX_TIM1_Init(); MX_TIM2_Init(); MX_USB_OTG_FS_PCD_Init(); /* USER CODE BEGIN 2 */ TIM1->CCR1 = 0x300; TIM1->CCR2 = 0x300; TIM2->CCR2 = 0x60; // Настройка deathTime pwm.init(); /* bugfix */ pwmDeathTime.setValue(10); htim1.Instance->BDTR &= ~TIM_BDTR_DTG; htim1.Instance->BDTR |= pwm.computeDeathTime(pwmDeathTime.getValueFlt()); HAL_TIM_Base_Start(&htim1); // Выходной сигнал для ацп HAL_TIM_OC_Start(&htim2, TIM_CHANNEL_2); // Выходной сигнал для ацп HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_3); // HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_1); // HAL_TIM_PWM_Start(&htim1,TIM_CHANNEL_2); // pwm.start(); // HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_1); // HAL_TIMEx_PWMN_Start(&htim1,TIM_CHANNEL_2); __HAL_RCC_DMA2_CLK_ENABLE(); HAL_ADC_Start_DMA(&hadc1, (uint32_t*)adc1.getBufer(), adc1.getBuferSize()); HAL_ADC_Start_DMA(&hadc3, (uint32_t*)adc3.getBufer(), adc3.getBuferSize()); //------------------------------------------------------------------------------ // Низкоуровневая инициализация //------------------------------------------------------------------------------ // Разрешение выходов буферов GPIOC->BSRR = BIT_14; // Инициализация SPI портов //------------------------------------------------------------------------------ // Агрегация объектов // Определяется только при инициализации программы //------------------------------------------------------------------------------ mainMenu.addObserver( &menuEngine ); // Объект menuEngine подписался на рассылку событий, объявленных в IControlCommands //------------------------------------------------------------------------------ // Начальные условия //------------------------------------------------------------------------------ menuEngine.setMenuValue(""); // Затычка на время отсутствия FRAM. Инициализация float данных vICalibrating.setValue(vICalibrating.getValue()); vUDcBusCodeUCal.setValue(vUDcBusCodeUCal.getValue()); vUDcBusCodeZero.setValue(vUDcBusCodeZero.getValue()); vIChargeCodeICal.setValue(vIChargeCodeICal.getValue()); vUChargeCodeUCal.setValue(vUChargeCodeUCal.getValue()); vIChargeCodeZero.setValue(vIChargeCodeZero.getValue()); vUChargeCodeZero.setValue(vUChargeCodeZero.getValue()); vDcBusLoadVoltageDifferent.setValue(vDcBusLoadVoltageDifferent.getValue()); /* USER CODE END 2 */ /* Call init function for freertos objects (in freertos.c) */ MX_FREERTOS_Init(); /* Start scheduler */ osKernelStart(); /* We should never get here as control is now taken by the scheduler */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ } /* USER CODE END 3 */ }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /* Enable the CPU Cache */ CPU_CACHE_Enable(); /* STM32F7xx HAL library initialization: - Configure the Flash ART accelerator on ITCM interface - 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 216 MHz */ SystemClock_Config(); /* Configure LED3 */ BSP_LED_Init(LED3); /* Set Timers instance */ TimMasterHandle.Instance = TIM1; TimSlaveMasterHandle.Instance = TIM4; TimSlaveHandle.Instance = TIM5; /*======= Master1/Slave for an external trigger configuration : TIM1 =======*/ /* Initialize TIM1 peripheral in Output Compare mode*/ TimMasterHandle.Init.Period = 79; TimMasterHandle.Init.Prescaler = (SystemCoreClock / 16000000) - 1;; TimMasterHandle.Init.ClockDivision = 0; TimMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OC_Init(&TimMasterHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the output: Channel_1 */ sOCConfig.OCMode = TIM_OCMODE_TOGGLE; sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; if(HAL_TIM_OC_ConfigChannel(&TimMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure the Input: channel_2 */ sICConfig.ICPolarity = TIM_ICPOLARITY_RISING; sICConfig.ICSelection = TIM_ICSELECTION_DIRECTTI; sICConfig.ICPrescaler = TIM_ICPSC_DIV1; sICConfig.ICFilter = 0; if(HAL_TIM_IC_ConfigChannel(&TimMasterHandle, &sICConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM1 in Gated Slave mode for the external trigger (Filtered Timer Input 2) */ sSlaveConfig.InputTrigger = TIM_TS_TI2FP2; sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED; if( HAL_TIM_SlaveConfigSynchronization(&TimMasterHandle, &sSlaveConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM1 in Master Enable mode & use the update event as Trigger Output (TRGO) */ sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; if( HAL_TIMEx_MasterConfigSynchronization(&TimMasterHandle, &sMasterConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*=== End of Master1/Slave for an external trigger configuration : TIM1 ====*/ /*=================== Slave/Master configuration : TIM4 ====================*/ /* Initialize TIM4 peripheral in Output Compare mode*/ TimSlaveMasterHandle.Init.Period = 79; TimSlaveMasterHandle.Init.Prescaler = ((SystemCoreClock/2) / 16000000) - 1;; TimSlaveMasterHandle.Init.ClockDivision = 0; TimSlaveMasterHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OC_Init(&TimSlaveMasterHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the Output Compare channel_1 */ sOCConfig.OCMode = TIM_OCMODE_TOGGLE; sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; if(HAL_TIM_OC_ConfigChannel(&TimSlaveMasterHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM4 in Gated Slave mode for the internal trigger 0(ITR0) */ sSlaveConfig.InputTrigger = TIM_TS_ITR0; sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED; if( HAL_TIM_SlaveConfigSynchronization(&TimSlaveMasterHandle, &sSlaveConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM4 in Master Enable mode & use the update event as Trigger Output (TRGO) */ sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE; sMasterConfig.MasterOutputTrigger = TIM_TRGO_ENABLE; if( HAL_TIMEx_MasterConfigSynchronization(&TimSlaveMasterHandle, &sMasterConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*=============== End of Slave/Master configuration : TIM4 =================*/ /*====================== Slave configuration : TIM5 ========================*/ /* Initialize TIM5 peripheral in Output Compare mode*/ TimSlaveHandle.Init.Period = 79; TimSlaveHandle.Init.Prescaler = ((SystemCoreClock/2) / 16000000) - 1;; TimSlaveHandle.Init.ClockDivision = 0; TimSlaveHandle.Init.CounterMode = TIM_COUNTERMODE_UP; if(HAL_TIM_OC_Init(&TimSlaveHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /* Configure the Output Compare channel_1 */ sOCConfig.OCMode = TIM_OCMODE_TOGGLE; sOCConfig.OCPolarity = TIM_OCPOLARITY_HIGH; if(HAL_TIM_OC_ConfigChannel(&TimSlaveHandle, &sOCConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Configure TIM5 in Gated Slave mode for the internal trigger 2(ITR2) */ sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED; sSlaveConfig.InputTrigger = TIM_TS_ITR2; if(HAL_TIM_SlaveConfigSynchronization(&TimSlaveHandle, &sSlaveConfig) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*================== End of Slave configuration : TIM5 =====================*/ /* 1- Start Timer1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ /* Start Channel2 in Input Capture */ if(HAL_TIM_IC_Start(&TimMasterHandle, TIM_CHANNEL_2) != HAL_OK) { /* Start Error */ Error_Handler(); } /* Start the Output Compare */ if(HAL_TIM_OC_Start(&TimMasterHandle, TIM_CHANNEL_1) != HAL_OK) { /* Start Error */ Error_Handler(); } /* 2- Start Timer3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ /* Start the Output Compare */ if(HAL_TIM_OC_Start(&TimSlaveMasterHandle, TIM_CHANNEL_1) != HAL_OK) { /* Start Error */ Error_Handler(); } /* 3- Start Timer3 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx */ /* Start the Output Compare */ if(HAL_TIM_OC_Start(&TimSlaveHandle, TIM_CHANNEL_1) != HAL_OK) { /* Start Error */ Error_Handler(); } /* Infinite loop */ while (1) { } }
/** * @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 #######################################*/ /* Initialize TIMx peripheral as follow: + Prescaler = 0 + Period = 4095 + ClockDivision = 0 + Counter direction = Up */ TimHandle.Instance = TIM1; TimHandle.Init.Period = 4095; TimHandle.Init.Prescaler = 0; TimHandle.Init.ClockDivision = 0; TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP; TimHandle.Init.RepetitionCounter = 0; if(HAL_TIM_OC_Init(&TimHandle) != HAL_OK) { /* Initialization Error */ Error_Handler(); } /*##-2- Configure the output channels ######################################*/ /* Common configuration for all channels */ sConfig.OCMode = TIM_OCMODE_TIMING; sConfig.OCPolarity = TIM_OCPOLARITY_HIGH; sConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfig.OCIdleState = TIM_OCIDLESTATE_SET; sConfig.OCNIdleState = TIM_OCNIDLESTATE_SET; sConfig.OCFastMode = TIM_OCFAST_DISABLE; /* Set the pulse value for channel 1 */ sConfig.Pulse = 2047; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_1) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse value for channel 2 */ sConfig.Pulse = 1023; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_2) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /* Set the pulse value for channel 3 */ sConfig.Pulse = 511; if(HAL_TIM_OC_ConfigChannel(&TimHandle, &sConfig, TIM_CHANNEL_3) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-3- Configure the Break stage ##########################################*/ sConfigBK.OffStateRunMode = TIM_OSSR_ENABLE; sConfigBK.OffStateIDLEMode = TIM_OSSI_ENABLE; sConfigBK.LockLevel = TIM_LOCKLEVEL_OFF; sConfigBK.BreakState = TIM_BREAK_ENABLE; sConfigBK.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sConfigBK.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE; sConfigBK.DeadTime = 1; if(HAL_TIMEx_ConfigBreakDeadTime(&TimHandle, &sConfigBK) != HAL_OK) { /* Configuration Error */ Error_Handler(); } /*##-4- Configure the commutation event: software event ####################*/ HAL_TIMEx_ConfigCommutationEvent_IT(&TimHandle, TIM_TS_NONE, TIM_COMMUTATION_SOFTWARE); /*##-5- Start signals generation ###########################################*/ /*--------------------------------------------------------------------------*/ /* Start channel 1 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 1N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* Start channel 2 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 2N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_2) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /*--------------------------------------------------------------------------*/ /* Start channel 3 */ if(HAL_TIM_OC_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /* Start channel 3N */ if(HAL_TIMEx_OCN_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK) { /* Starting Error */ Error_Handler(); } /*--------------------------------------------------------------------------*/ /* Infinite loop */ while (1) { } }