Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- * This notice applies to any and all portions of this file
- * that are not between comment pairs USER CODE BEGIN and
- * USER CODE END. Other portions of this file, whether
- * inserted by the user or by software development tools
- * are owned by their respective copyright owners.
- *
- * Copyright (c) 2019 STMicroelectronics International N.V.
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted, provided that the following conditions are met:
- *
- * 1. Redistribution of source code must retain the above copyright notice,
- * this list of conditions and the following disclaimer.
- * 2. Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * 3. Neither the name of STMicroelectronics nor the names of other
- * contributors to this software may be used to endorse or promote products
- * derived from this software without specific written permission.
- * 4. This software, including modifications and/or derivative works of this
- * software, must execute solely and exclusively on microcontroller or
- * microprocessor devices manufactured by or for STMicroelectronics.
- * 5. Redistribution and use of this software other than as permitted under
- * this license is void and will automatically terminate your rights under
- * this license.
- *
- * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
- * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
- * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
- * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
- * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
- * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
- * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
- * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
- * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "cmsis_os.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- // Храним указатель на задачу, которую должны нотифицировать из прерывания
- TaskHandle_t xTaskToNotify = NULL;
- /* USER CODE END Includes */
- /* Private typedef -----------------------------------------------------------*/
- /* USER CODE BEGIN PTD */
- /* Define the data type that will be queued. */
- typedef struct A_Message {
- char ucMessageID;
- char ucMessageData;
- } AMessage;
- void HAL_GPIO_EXTI_Callback( uint16_t _gpioPin )
- {
- BaseType_t xHigherPriorityTaskWoken;
- /* The xHigherPriorityTaskWoken parameter must be initialized to pdFALSE because it will get set to pdTRUE inside the interrupt-safe API function if a context switch is required. */
- xHigherPriorityTaskWoken = pdFALSE;
- /* Send a notification directly to the task to which interrupt processing is being deferred. */
- vTaskNotifyGiveFromISR( /* The handle of the task to which the notification is being sent. The handle was saved when the task was created. */ xTaskToNotify, /* xHigherPriorityTaskWoken is used in the usual way. */ &xHigherPriorityTaskWoken );
- /* Pass the xHigherPriorityTaskWoken value into portYIELD_FROM_ISR(). If xHigherPriorityTaskWoken was set to pdTRUE inside vTaskNotifyGiveFromISR(), then calling portYIELD_FROM_ISR() will request a context switch. If xHigherPriorityTaskWoken is still pdFALSE, then calling portYIELD_FROM_ISR() will have no effect. The implementation of portYIELD_FROM_ISR() used by the Windows port includes a return statement, which is why this function does not explicitly return a value. */
- portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
- }
- /* 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 ---------------------------------------------------------*/
- osThreadId defaultTaskHandle;
- osThreadId Led12BlinkerHandle;
- osThreadId Led13BlinkerHandle;
- osThreadId QueueSenderHandle;
- osThreadId QueueAwaiterHandle;
- osMessageQId adcDataHandle;
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- void StartDefaultTask(void const * argument);
- void StartTask02(void const * argument);
- void StartTask03(void const * argument);
- void QueueSenderEntry(void const * argument);
- void QueueAwaiterEntry(void const * 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_GPIO_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* USER CODE BEGIN RTOS_MUTEX */
- /* add mutexes, ... */
- /* USER CODE END RTOS_MUTEX */
- /* 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 thread(s) */
- /* definition and creation of defaultTask */
- osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
- defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
- /* definition and creation of Led12Blinker */
- osThreadDef(Led12Blinker, StartTask02, osPriorityNormal, 0, 128);
- Led12BlinkerHandle = osThreadCreate(osThread(Led12Blinker), NULL);
- /* definition and creation of Led13Blinker */
- osThreadDef(Led13Blinker, StartTask03, osPriorityIdle, 0, 128);
- Led13BlinkerHandle = osThreadCreate(osThread(Led13Blinker), NULL);
- /* definition and creation of QueueSender */
- osThreadDef(QueueSender, QueueSenderEntry, osPriorityNormal, 0, 128);
- QueueSenderHandle = osThreadCreate(osThread(QueueSender), NULL);
- /* definition and creation of QueueAwaiter */
- osThreadDef(QueueAwaiter, QueueAwaiterEntry, osPriorityNormal, 0, 128);
- QueueAwaiterHandle = osThreadCreate(osThread(QueueAwaiter), NULL);
- /* USER CODE BEGIN RTOS_THREADS */
- /* add threads, ... */
- /* USER CODE END RTOS_THREADS */
- /* Create the queue(s) */
- /* definition and creation of adcData */
- osMessageQDef(adcData, 16, sizeof( AMessage ) );
- adcDataHandle = osMessageCreate(osMessageQ(adcData), NULL);
- /* USER CODE BEGIN RTOS_QUEUES */
- /* add queues, ... */
- /* USER CODE END RTOS_QUEUES */
- /* 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_RCC_PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- /**Initializes the CPU, AHB and APB busses clocks
- */
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
- RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
- RCC_OscInitStruct.PLL.PLLM = 8;
- RCC_OscInitStruct.PLL.PLLN = 168;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 4;
- if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /**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_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief GPIO Initialization Function
- * @param None
- * @retval None
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- /* GPIO Ports Clock Enable */
- __HAL_RCC_GPIOH_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- /*Configure GPIO pin Output Level */
- HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
- /*Configure GPIO pin : PA0 */
- GPIO_InitStruct.Pin = GPIO_PIN_0;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
- GPIO_InitStruct.Pull = GPIO_PULLDOWN;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
- GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
- /* EXTI interrupt init*/
- HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0);
- HAL_NVIC_EnableIRQ(EXTI0_IRQn);
- }
- /* USER CODE BEGIN 4 */
- /* USER CODE END 4 */
- /* USER CODE BEGIN Header_StartDefaultTask */
- /**
- * @brief Function implementing the defaultTask thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_StartDefaultTask */
- void StartDefaultTask(void const * argument)
- {
- /* USER CODE BEGIN 5 */
- /* Infinite loop */
- for(;;)
- {
- osDelay(1);
- }
- /* USER CODE END 5 */
- }
- /* USER CODE BEGIN Header_StartTask02 */
- /**
- * @brief Function implementing the testTaskBlinker thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_StartTask02 */
- void StartTask02(void const * argument)
- {
- /* USER CODE BEGIN StartTask02 */
- /* Infinite loop */
- for(;;)
- {
- HAL_GPIO_WritePin( GPIOD, GPIO_PIN_12, GPIO_PIN_SET );
- osDelay(300);
- HAL_GPIO_WritePin( GPIOD, GPIO_PIN_12, GPIO_PIN_RESET );
- osDelay(300);
- }
- /* USER CODE END StartTask02 */
- }
- /* USER CODE BEGIN Header_StartTask03 */
- /**
- * @brief Function implementing the Led13Blinker thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_StartTask03 */
- void StartTask03(void const * argument)
- {
- /* USER CODE BEGIN StartTask03 */
- /* Infinite loop */
- //
- xTaskToNotify = xTaskGetCurrentTaskHandle();
- uint32_t ulEventsToProcess;
- for(;;)
- {
- ulEventsToProcess = ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
- if( ulEventsToProcess != 0 )
- {
- while( ulEventsToProcess > 0 )
- {
- HAL_GPIO_WritePin( GPIOD, GPIO_PIN_13, GPIO_PIN_SET );
- osDelay(100);
- HAL_GPIO_WritePin( GPIOD, GPIO_PIN_13, GPIO_PIN_RESET );
- osDelay(100);
- --ulEventsToProcess;
- }
- }
- }
- /* USER CODE END StartTask03 */
- }
- /* USER CODE BEGIN Header_QueueSenderEntry */
- /**
- * @brief Function implementing the QueueSender thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_QueueSenderEntry */
- void QueueSenderEntry(void const * argument)
- {
- /* USER CODE BEGIN QueueSenderEntry */
- AMessage xMessage;
- /* Infinite loop */
- for(;;)
- {
- xMessage.ucMessageID++;
- xMessage.ucMessageData = 10;
- xQueueSendToBack( adcDataHandle,&xMessage ,10 );
- osDelay(10);
- }
- /* USER CODE END QueueSenderEntry */
- }
- /* USER CODE BEGIN Header_QueueAwaiterEntry */
- /**
- * @brief Function implementing the QueueAwaiter thread.
- * @param argument: Not used
- * @retval None
- */
- /* USER CODE END Header_QueueAwaiterEntry */
- void QueueAwaiterEntry(void const * argument)
- {
- /* USER CODE BEGIN QueueAwaiterEntry */
- /* Infinite loop */
- #define MessagesSize 8 // use std::array from c++11 instead such trics with macro
- AMessage messages[MessagesSize];
- for(;;)
- {
- if( uxQueueMessagesWaiting( adcDataHandle ) == MessagesSize )
- {
- for(int i = 0; i < MessagesSize; ++i )
- xQueueReceive( adcDataHandle,(messages + i), portMAX_DELAY );
- }
- osDelay(1);
- }
- /* USER CODE END QueueAwaiterEntry */
- }
- /**
- * @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 */
- /* 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****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement