/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file FreeRTOS\FreeRTOS_StopMode\Src\main.c * @author MCD Application Team * @brief Main program body ****************************************************************************** * @attention * * Copyright (c) 2020 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" #include "cmsis_os.h" /* Private includes ----------------------------------------------------------*/ /* USER CODE BEGIN Includes */ /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ /* USER CODE BEGIN PTD */ /* USER CODE END PTD */ /* Private define ------------------------------------------------------------*/ /* USER CODE BEGIN PD */ /* USER CODE END PD */ /* Private macro -------------------------------------------------------------*/ /* USER CODE BEGIN PM */ /* USER CODE END PM */ /* Private variables ---------------------------------------------------------*/ LPTIM_HandleTypeDef hlptim1; /* Definitions for TxThread */ osThreadId_t TxThreadHandle; const osThreadAttr_t TxThread_attributes = { .name = "TxThread", .priority = (osPriority_t) osPriorityBelowNormal, .stack_size = 128 * 4 }; /* Definitions for RxThread */ osThreadId_t RxThreadHandle; const osThreadAttr_t RxThread_attributes = { .name = "RxThread", .priority = (osPriority_t) osPriorityNormal, .stack_size = 128 * 4 }; /* Definitions for osqueue */ osMessageQueueId_t osqueueHandle; const osMessageQueueAttr_t osqueue_attributes = { .name = "osqueue" }; /* Definitions for osSemaphore */ osSemaphoreId_t osSemaphoreHandle; const osSemaphoreAttr_t osSemaphore_attributes = { .name = "osSemaphore" }; /* USER CODE BEGIN PV */ uint32_t osQueueMsg; uint32_t Queue_value = 100; __IO uint32_t OsStatus = 0; /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_LPTIM1_Init(void); void QueueSendThread(void *argument); void QueueReceiveThread(void *argument); /* USER CODE BEGIN PFP */ /* USER CODE END PFP */ /* Private user code ---------------------------------------------------------*/ /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ /** * @brief The application entry point. * @retval int */ 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_LPTIM1_Init(); /* USER CODE BEGIN 2 */ /* Initialize LED */ BSP_LED_Init(LED2); /* ### Start counting in interrupt mode ############################# */ /* * Period = 1 second */ if (HAL_LPTIM_Counter_Start_IT(&hlptim1, LSI_VALUE) != HAL_OK) { Error_Handler(); } /* Disable autoreload write complete interrupt */ __HAL_LPTIM_DISABLE_IT(&hlptim1, LPTIM_IT_ARROK); /* USER CODE END 2 */ /* Init scheduler */ osKernelInitialize(); /* USER CODE BEGIN RTOS_MUTEX */ /* add mutexes, ... */ /* USER CODE END RTOS_MUTEX */ /* Create the semaphores(s) */ /* creation of osSemaphore */ osSemaphoreHandle = osSemaphoreNew(1, 1, &osSemaphore_attributes); /* USER CODE BEGIN RTOS_SEMAPHORES */ /* add semaphores, ... */ /* USER CODE END RTOS_SEMAPHORES */ /* USER CODE BEGIN RTOS_TIMERS */ /* start timers, add new ones, ... */ /* USER CODE END RTOS_TIMERS */ /* Create the queue(s) */ /* creation of osqueue */ osqueueHandle = osMessageQueueNew (1, sizeof(uint16_t), &osqueue_attributes); /* USER CODE BEGIN RTOS_QUEUES */ /* USER CODE END RTOS_QUEUES */ /* Create the thread(s) */ /* creation of TxThread */ TxThreadHandle = osThreadNew(QueueSendThread, NULL, &TxThread_attributes); /* creation of RxThread */ RxThreadHandle = osThreadNew(QueueReceiveThread, NULL, &RxThread_attributes); /* USER CODE BEGIN RTOS_THREADS */ /* add threads, ... */ /* USER CODE END RTOS_THREADS */ /* USER CODE BEGIN RTOS_EVENTS */ /* add events, ... */ /* USER CODE END RTOS_EVENTS */ /* 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 = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Configure the main internal regulator output voltage */ __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1); /** Initializes the CPU, AHB and APB buses clocks */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_MSI; RCC_OscInitStruct.MSIState = RCC_MSI_ON; RCC_OscInitStruct.MSICalibrationValue = RCC_MSICALIBRATION_DEFAULT; RCC_OscInitStruct.MSIClockRange = RCC_MSIRANGE_8; RCC_OscInitStruct.LSIDiv = RCC_LSI_DIV1; RCC_OscInitStruct.LSIState = RCC_LSI_ON; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON; RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_MSI; RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV4; RCC_OscInitStruct.PLL.PLLN = 24; RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2; RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2; RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Configure the SYSCLKSource, HCLK, PCLK1 and PCLK2 clocks dividers */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK3|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_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.AHBCLK3Divider = RCC_SYSCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK) { Error_Handler(); } } /** * @brief LPTIM1 Initialization Function * @param None * @retval None */ static void MX_LPTIM1_Init(void) { /* USER CODE BEGIN LPTIM1_Init 0 */ /* USER CODE END LPTIM1_Init 0 */ /* USER CODE BEGIN LPTIM1_Init 1 */ /* USER CODE END LPTIM1_Init 1 */ hlptim1.Instance = LPTIM1; hlptim1.Init.Clock.Source = LPTIM_CLOCKSOURCE_APBCLOCK_LPOSC; hlptim1.Init.Clock.Prescaler = LPTIM_PRESCALER_DIV1; hlptim1.Init.Trigger.Source = LPTIM_TRIGSOURCE_SOFTWARE; hlptim1.Init.OutputPolarity = LPTIM_OUTPUTPOLARITY_HIGH; hlptim1.Init.UpdateMode = LPTIM_UPDATE_IMMEDIATE; hlptim1.Init.CounterSource = LPTIM_COUNTERSOURCE_INTERNAL; hlptim1.Init.Input1Source = LPTIM_INPUT1SOURCE_GPIO; hlptim1.Init.Input2Source = LPTIM_INPUT2SOURCE_GPIO; if (HAL_LPTIM_Init(&hlptim1) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN LPTIM1_Init 2 */ /* USER CODE END LPTIM1_Init 2 */ } /* USER CODE BEGIN 4 */ /** * @brief Autoreload match callback in non blocking mode * @param hlptim : LPTIM handle * @retval None */ void HAL_LPTIM_AutoReloadMatchCallback(LPTIM_HandleTypeDef *hlptim) { osSemaphoreRelease(osSemaphoreHandle); } /* USER CODE END 4 */ /* USER CODE BEGIN Header_QueueSendThread */ /** * @brief Function implementing the QueueSendThread thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_QueueSendThread */ void QueueSendThread(void *argument) { /* USER CODE BEGIN 5 */ for (;;) { if (osSemaphoreHandle != NULL) { /* Try to obtain the semaphore */ OsStatus = osSemaphoreAcquire(osSemaphoreHandle, osWaitForever); if (OsStatus == osOK) { osMessageQueuePut(osqueueHandle, &Queue_value, 100, 0U); } } } /* USER CODE END 5 */ } /* USER CODE BEGIN Header_QueueReceiveThread */ /** * @brief Function implementing the QueueReceiveThread thread. * @param argument: Not used * @retval None */ /* USER CODE END Header_QueueReceiveThread */ void QueueReceiveThread(void *argument) { /* USER CODE BEGIN QueueReceiveThread */ for (;;) { OsStatus = osMessageQueueGet(osqueueHandle, &osQueueMsg, NULL, 100); if (OsStatus == osOK) { if (osQueueMsg == Queue_value) { BSP_LED_Toggle(LED2); } } } /* USER CODE END QueueReceiveThread */ } /** * @brief Period elapsed callback in non blocking mode * @note This function is called when TIM17 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 == TIM17) { HAL_IncTick(); } /* USER CODE BEGIN Callback 1 */ /* USER CODE END Callback 1 */ } /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ BSP_LED_On(LED3); 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) */ /* Infinite loop */ while (1) {} /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */