We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi friends, I am using STM32CubeMX and trying to start software timer but I couldn't start.These are my codes.Do you have any simple example or How can I manage it?
#include "main.h"#include "stm32f1xx_hal.h"#include "cmsis_os.h"
UART_HandleTypeDef huart2;
osThreadId defaultTaskHandle;osThreadId myTask02Handle;osTimerId myTimer01Handle;osTimerId myTimer02Handle;osTimerId myTimer03Handle;osTimerId myTimer04Handle;osMutexId myMutex01Handle;osSemaphoreId myBinarySem01Handle;
/* USER CODE BEGIN PV *//* Private variables ---------------------------------------------------------*/
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/void SystemClock_Config(void);static void MX_GPIO_Init(void);static void MX_USART2_UART_Init(void);void StartDefaultTask(void const * argument);void StartTask02(void const * argument);void Callback01(void const * argument);void Callback02(void const * argument);extern void Callback03(void const * argument);extern void Callback04(void const * argument);
int main(void){
HAL_Init(); SystemClock_Config();
MX_GPIO_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Create the mutex(es) */ /* definition and creation of myMutex01 */ osMutexDef(myMutex01); myMutex01Handle = osMutexCreate(osMutex(myMutex01));
/* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */
/* Create the semaphores(s) */ /* definition and creation of myBinarySem01 */ osSemaphoreDef(myBinarySem01); myBinarySem01Handle = osSemaphoreCreate(osSemaphore(myBinarySem01), 1);
/* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */
/* Create the timer(s) */ /* definition and creation of myTimer01 */ osTimerDef(myTimer01, Callback01); myTimer01Handle = osTimerCreate(osTimer(myTimer01), osTimerOnce, NULL);
/* definition and creation of myTimer02 */ osTimerDef(myTimer02, Callback02); myTimer02Handle = osTimerCreate(osTimer(myTimer02), osTimerPeriodic, NULL);
/* definition and creation of myTimer03 */ osTimerDef(myTimer03, Callback03); myTimer03Handle = osTimerCreate(osTimer(myTimer03), osTimerOnce, NULL);
/* definition and creation of myTimer04 */ osTimerDef(myTimer04, Callback04); myTimer04Handle = osTimerCreate(osTimer(myTimer04), osTimerPeriodic, NULL);
/* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */
/* Create the thread(s) */ /* definition and creation of defaultTask */ osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128); defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
/* definition and creation of myTask02 */ osThreadDef(myTask02, StartTask02, osPriorityIdle, 0, 128); myTask02Handle = osThreadCreate(osThread(myTask02), NULL);
/* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */
/* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */
/* Start scheduler */ osKernelStart(); xTimerStart(myTimer02Handle,1000); /* We should never get here as control is now taken by the scheduler */
/* Infinite loop */ /* USER CODE BEGIN WHILE */
while (1) {
}
Hi,
Please check the FreeRTOSConfig.h. You need to enable the software timer attribute in FreeRTOS, which remains disabled by default in STM32CubeMX.
Something like the following..
/* Software timer related definitions. */ #define configUSE_TIMERS 1 #define configTIMER_TASK_PRIORITY 3 #define configTIMER_QUEUE_LENGTH 10 #define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZEThanks
As much as I learn now,first ,If we use RTOS ,system doesnt enter the whie loop in main,so it means RTOS is handling the system and I managed to start ,HAL Library has own functions about that is very simple,this is why I would want to see any example code,I realised it by mistake.Now my example codes are below.
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private variables ---------------------------------------------------------*/UART_HandleTypeDef huart2;
uint8_t temp=1;/* USER CODE BEGIN PV *//* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PFP *//* Private function prototypes -----------------------------------------------*/
/* USER CODE END PFP */
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/** * @brief The application entry point. * * @retval None */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();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */ SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */ MX_GPIO_Init(); MX_USART2_UART_Init(); /* USER CODE BEGIN 2 */
/* USER CODE BEGIN RTOS_QUEUES */ /* add queues, ... */ /* USER CODE END RTOS_QUEUES */ //osTimerStart(myTimer01Handle,500); /* 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 System Clock Configuration * @retval None */void SystemClock_Config(void){
RCC_OscInitTypeDef RCC_OscInitStruct; RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE; RCC_OscInitStruct.HSEState = RCC_HSE_ON; RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE; RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }
/**Initializes the CPU, AHB and APB busses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }
/**Configure the Systick interrupt time */ HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick */ HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */ HAL_NVIC_SetPriority(SysTick_IRQn, 15, 0);}
/* USART2 init function */static void MX_USART2_UART_Init(void){
huart2.Instance = USART2; huart2.Init.BaudRate = 115200; huart2.Init.WordLength = UART_WORDLENGTH_8B; huart2.Init.StopBits = UART_STOPBITS_1; huart2.Init.Parity = UART_PARITY_NONE; huart2.Init.Mode = UART_MODE_TX_RX; huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; huart2.Init.OverSampling = UART_OVERSAMPLING_16; if (HAL_UART_Init(&huart2) != HAL_OK) { _Error_Handler(__FILE__, __LINE__); }
/** Configure pins as * Analog * Input * Output * EVENT_OUT * EXTI*/static void MX_GPIO_Init(void){
GPIO_InitTypeDef GPIO_InitStruct;
/* GPIO Ports Clock Enable */ __HAL_RCC_GPIOC_CLK_ENABLE(); __HAL_RCC_GPIOD_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_RESET);
/*Configure GPIO pin : PC13 */ GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/* StartDefaultTask function */void StartDefaultTask(void const * argument){
osStatus osSt; /* USER CODE BEGIN 5 */ /* Infinite loop */ for(;;) { if(temp) { osSt = osTimerStart(myTimer01Handle,1000); temp=0; }// else if(temp == 0 && osSt == osEventTimeout)// osTimerStop(); osDelay(1); } /* USER CODE END 5 */ }
/* StartTask02 function */void StartTask02(void const * argument){ /* USER CODE BEGIN StartTask02 */ /* Infinite loop */ for(;;) { osDelay(1); } /* USER CODE END StartTask02 */}
/* Callback01 function */void Callback01(void const * argument){ /* USER CODE BEGIN Callback01 */ HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13); /* USER CODE END Callback01 */}
/* Callback02 function */void Callback02(void const * argument){ /* USER CODE BEGIN Callback02 */ HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13); /* USER CODE END Callback02 */}void Callback03(void const * argument){ /* USER CODE BEGIN Callback02 */ /* USER CODE END Callback02 */}void Callback04(void const * argument){ /* USER CODE BEGIN Callback02 */ /* USER CODE END Callback02 */}
/** * @brief Period elapsed callback in non blocking mode * @note This function is called when TIM1 interrupt took place, inside * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment * a global variable "uwTick" used as application time base. * @param htim : TIM handle * @retval None */void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){ /* USER CODE BEGIN Callback 0 */
/* USER CODE END Callback 0 */ if (htim->Instance == TIM1) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */
/* USER CODE END Callback 1 */}
/** * @brief This function is executed in case of error occurrence. * @param file: The file name as string. * @param line: The line in file as a number. * @retval None */void _Error_Handler(char *file, int line){ /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ while(1) { } /* USER CODE END Error_Handler_Debug */}
#ifdef USE_FULL_ASSERT/** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */void assert_failed(uint8_t* file, uint32_t line){ /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */}#endif /* USE_FULL_ASSERT */
/** * @} */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/