Advertisement
Guest User

Untitled

a guest
May 22nd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.89 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. * This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * Copyright (c) 2019 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* USER CODE END Header */
  50.  
  51. /* Includes ------------------------------------------------------------------*/
  52. #include "main.h"
  53. #include "cmsis_os.h"
  54.  
  55. /* Private includes ----------------------------------------------------------*/
  56. /* USER CODE BEGIN Includes */
  57. // Храним указатель на задачу, которую должны нотифицировать из прерывания
  58. TaskHandle_t xTaskToNotify = NULL;
  59. /* USER CODE END Includes */
  60.  
  61. /* Private typedef -----------------------------------------------------------*/
  62. /* USER CODE BEGIN PTD */
  63.  
  64. /* Define the data type that will be queued. */
  65.  
  66.  
  67. typedef struct A_Message {
  68.  
  69. char ucMessageID;
  70. char ucMessageData;
  71.  
  72. } AMessage;
  73.  
  74.  
  75. void HAL_GPIO_EXTI_Callback( uint16_t _gpioPin )
  76. {
  77. BaseType_t xHigherPriorityTaskWoken;
  78.  
  79. /* 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. */
  80.  
  81. xHigherPriorityTaskWoken = pdFALSE;
  82.  
  83. /* Send a notification directly to the task to which interrupt processing is being deferred. */
  84.  
  85. 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 );
  86.  
  87. /* 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. */
  88.  
  89. portYIELD_FROM_ISR( xHigherPriorityTaskWoken );
  90. }
  91.  
  92. /* USER CODE END PTD */
  93.  
  94. /* Private define ------------------------------------------------------------*/
  95. /* USER CODE BEGIN PD */
  96.  
  97. /* USER CODE END PD */
  98.  
  99. /* Private macro -------------------------------------------------------------*/
  100. /* USER CODE BEGIN PM */
  101.  
  102. /* USER CODE END PM */
  103.  
  104. /* Private variables ---------------------------------------------------------*/
  105. osThreadId defaultTaskHandle;
  106. osThreadId Led12BlinkerHandle;
  107. osThreadId Led13BlinkerHandle;
  108. osThreadId QueueSenderHandle;
  109. osThreadId QueueAwaiterHandle;
  110. osMessageQId adcDataHandle;
  111. /* USER CODE BEGIN PV */
  112.  
  113. /* USER CODE END PV */
  114.  
  115. /* Private function prototypes -----------------------------------------------*/
  116. void SystemClock_Config(void);
  117. static void MX_GPIO_Init(void);
  118. void StartDefaultTask(void const * argument);
  119. void StartTask02(void const * argument);
  120. void StartTask03(void const * argument);
  121. void QueueSenderEntry(void const * argument);
  122. void QueueAwaiterEntry(void const * argument);
  123.  
  124. /* USER CODE BEGIN PFP */
  125.  
  126. /* USER CODE END PFP */
  127.  
  128. /* Private user code ---------------------------------------------------------*/
  129. /* USER CODE BEGIN 0 */
  130.  
  131. /* USER CODE END 0 */
  132.  
  133. /**
  134. * @brief The application entry point.
  135. * @retval int
  136. */
  137. int main(void)
  138. {
  139. /* USER CODE BEGIN 1 */
  140.  
  141. /* USER CODE END 1 */
  142.  
  143. /* MCU Configuration--------------------------------------------------------*/
  144.  
  145. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  146. HAL_Init();
  147.  
  148. /* USER CODE BEGIN Init */
  149.  
  150. /* USER CODE END Init */
  151.  
  152. /* Configure the system clock */
  153. SystemClock_Config();
  154.  
  155. /* USER CODE BEGIN SysInit */
  156.  
  157. /* USER CODE END SysInit */
  158.  
  159. /* Initialize all configured peripherals */
  160. MX_GPIO_Init();
  161. /* USER CODE BEGIN 2 */
  162.  
  163. /* USER CODE END 2 */
  164.  
  165. /* USER CODE BEGIN RTOS_MUTEX */
  166. /* add mutexes, ... */
  167. /* USER CODE END RTOS_MUTEX */
  168.  
  169. /* USER CODE BEGIN RTOS_SEMAPHORES */
  170. /* add semaphores, ... */
  171. /* USER CODE END RTOS_SEMAPHORES */
  172.  
  173. /* USER CODE BEGIN RTOS_TIMERS */
  174. /* start timers, add new ones, ... */
  175. /* USER CODE END RTOS_TIMERS */
  176.  
  177. /* Create the thread(s) */
  178. /* definition and creation of defaultTask */
  179. osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  180. defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);
  181.  
  182. /* definition and creation of Led12Blinker */
  183. osThreadDef(Led12Blinker, StartTask02, osPriorityNormal, 0, 128);
  184. Led12BlinkerHandle = osThreadCreate(osThread(Led12Blinker), NULL);
  185.  
  186. /* definition and creation of Led13Blinker */
  187. osThreadDef(Led13Blinker, StartTask03, osPriorityIdle, 0, 128);
  188. Led13BlinkerHandle = osThreadCreate(osThread(Led13Blinker), NULL);
  189.  
  190. /* definition and creation of QueueSender */
  191. osThreadDef(QueueSender, QueueSenderEntry, osPriorityNormal, 0, 128);
  192. QueueSenderHandle = osThreadCreate(osThread(QueueSender), NULL);
  193.  
  194. /* definition and creation of QueueAwaiter */
  195. osThreadDef(QueueAwaiter, QueueAwaiterEntry, osPriorityNormal, 0, 128);
  196. QueueAwaiterHandle = osThreadCreate(osThread(QueueAwaiter), NULL);
  197.  
  198. /* USER CODE BEGIN RTOS_THREADS */
  199. /* add threads, ... */
  200. /* USER CODE END RTOS_THREADS */
  201.  
  202. /* Create the queue(s) */
  203. /* definition and creation of adcData */
  204. osMessageQDef(adcData, 16, sizeof( AMessage ) );
  205. adcDataHandle = osMessageCreate(osMessageQ(adcData), NULL);
  206.  
  207. /* USER CODE BEGIN RTOS_QUEUES */
  208. /* add queues, ... */
  209. /* USER CODE END RTOS_QUEUES */
  210.  
  211.  
  212. /* Start scheduler */
  213. osKernelStart();
  214.  
  215. /* We should never get here as control is now taken by the scheduler */
  216.  
  217. /* Infinite loop */
  218. /* USER CODE BEGIN WHILE */
  219. while (1)
  220. {
  221. /* USER CODE END WHILE */
  222.  
  223. /* USER CODE BEGIN 3 */
  224. }
  225. /* USER CODE END 3 */
  226. }
  227.  
  228. /**
  229. * @brief System Clock Configuration
  230. * @retval None
  231. */
  232. void SystemClock_Config(void)
  233. {
  234. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  235. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  236.  
  237. /**Configure the main internal regulator output voltage
  238. */
  239. __HAL_RCC_PWR_CLK_ENABLE();
  240. __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  241. /**Initializes the CPU, AHB and APB busses clocks
  242. */
  243. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  244. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  245. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  246. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  247. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  248. RCC_OscInitStruct.PLL.PLLM = 8;
  249. RCC_OscInitStruct.PLL.PLLN = 168;
  250. RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  251. RCC_OscInitStruct.PLL.PLLQ = 4;
  252. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  253. {
  254. Error_Handler();
  255. }
  256. /**Initializes the CPU, AHB and APB busses clocks
  257. */
  258. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  259. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  260. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  261. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  262. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  263. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  264.  
  265. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5) != HAL_OK)
  266. {
  267. Error_Handler();
  268. }
  269. }
  270.  
  271. /**
  272. * @brief GPIO Initialization Function
  273. * @param None
  274. * @retval None
  275. */
  276. static void MX_GPIO_Init(void)
  277. {
  278. GPIO_InitTypeDef GPIO_InitStruct = {0};
  279.  
  280. /* GPIO Ports Clock Enable */
  281. __HAL_RCC_GPIOH_CLK_ENABLE();
  282. __HAL_RCC_GPIOA_CLK_ENABLE();
  283. __HAL_RCC_GPIOD_CLK_ENABLE();
  284.  
  285. /*Configure GPIO pin Output Level */
  286. HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15, GPIO_PIN_RESET);
  287.  
  288. /*Configure GPIO pin : PA0 */
  289. GPIO_InitStruct.Pin = GPIO_PIN_0;
  290. GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING;
  291. GPIO_InitStruct.Pull = GPIO_PULLDOWN;
  292. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  293.  
  294. /*Configure GPIO pins : PD12 PD13 PD14 PD15 */
  295. GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
  296. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  297. GPIO_InitStruct.Pull = GPIO_NOPULL;
  298. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  299. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  300.  
  301. /* EXTI interrupt init*/
  302. HAL_NVIC_SetPriority(EXTI0_IRQn, 5, 0);
  303. HAL_NVIC_EnableIRQ(EXTI0_IRQn);
  304.  
  305. }
  306.  
  307. /* USER CODE BEGIN 4 */
  308.  
  309. /* USER CODE END 4 */
  310.  
  311. /* USER CODE BEGIN Header_StartDefaultTask */
  312. /**
  313. * @brief Function implementing the defaultTask thread.
  314. * @param argument: Not used
  315. * @retval None
  316. */
  317. /* USER CODE END Header_StartDefaultTask */
  318. void StartDefaultTask(void const * argument)
  319. {
  320.  
  321. /* USER CODE BEGIN 5 */
  322. /* Infinite loop */
  323. for(;;)
  324. {
  325. osDelay(1);
  326. }
  327. /* USER CODE END 5 */
  328. }
  329.  
  330. /* USER CODE BEGIN Header_StartTask02 */
  331. /**
  332. * @brief Function implementing the testTaskBlinker thread.
  333. * @param argument: Not used
  334. * @retval None
  335. */
  336. /* USER CODE END Header_StartTask02 */
  337. void StartTask02(void const * argument)
  338. {
  339. /* USER CODE BEGIN StartTask02 */
  340. /* Infinite loop */
  341. for(;;)
  342. {
  343. HAL_GPIO_WritePin( GPIOD, GPIO_PIN_12, GPIO_PIN_SET );
  344. osDelay(300);
  345. HAL_GPIO_WritePin( GPIOD, GPIO_PIN_12, GPIO_PIN_RESET );
  346. osDelay(300);
  347. }
  348. /* USER CODE END StartTask02 */
  349. }
  350.  
  351. /* USER CODE BEGIN Header_StartTask03 */
  352. /**
  353. * @brief Function implementing the Led13Blinker thread.
  354. * @param argument: Not used
  355. * @retval None
  356. */
  357. /* USER CODE END Header_StartTask03 */
  358. void StartTask03(void const * argument)
  359. {
  360. /* USER CODE BEGIN StartTask03 */
  361. /* Infinite loop */
  362. //
  363. xTaskToNotify = xTaskGetCurrentTaskHandle();
  364. uint32_t ulEventsToProcess;
  365. for(;;)
  366. {
  367. ulEventsToProcess = ulTaskNotifyTake( pdTRUE, portMAX_DELAY );
  368. if( ulEventsToProcess != 0 )
  369. {
  370. while( ulEventsToProcess > 0 )
  371. {
  372. HAL_GPIO_WritePin( GPIOD, GPIO_PIN_13, GPIO_PIN_SET );
  373. osDelay(100);
  374. HAL_GPIO_WritePin( GPIOD, GPIO_PIN_13, GPIO_PIN_RESET );
  375. osDelay(100);
  376.  
  377. --ulEventsToProcess;
  378. }
  379. }
  380. }
  381. /* USER CODE END StartTask03 */
  382. }
  383.  
  384. /* USER CODE BEGIN Header_QueueSenderEntry */
  385. /**
  386. * @brief Function implementing the QueueSender thread.
  387. * @param argument: Not used
  388. * @retval None
  389. */
  390. /* USER CODE END Header_QueueSenderEntry */
  391. void QueueSenderEntry(void const * argument)
  392. {
  393. /* USER CODE BEGIN QueueSenderEntry */
  394. AMessage xMessage;
  395. /* Infinite loop */
  396. for(;;)
  397. {
  398. xMessage.ucMessageID++;
  399. xMessage.ucMessageData = 10;
  400.  
  401. xQueueSendToBack( adcDataHandle,&xMessage ,10 );
  402. osDelay(10);
  403. }
  404. /* USER CODE END QueueSenderEntry */
  405. }
  406.  
  407. /* USER CODE BEGIN Header_QueueAwaiterEntry */
  408. /**
  409. * @brief Function implementing the QueueAwaiter thread.
  410. * @param argument: Not used
  411. * @retval None
  412. */
  413. /* USER CODE END Header_QueueAwaiterEntry */
  414. void QueueAwaiterEntry(void const * argument)
  415. {
  416. /* USER CODE BEGIN QueueAwaiterEntry */
  417. /* Infinite loop */
  418. #define MessagesSize 8 // use std::array from c++11 instead such trics with macro
  419. AMessage messages[MessagesSize];
  420. for(;;)
  421. {
  422. if( uxQueueMessagesWaiting( adcDataHandle ) == MessagesSize )
  423. {
  424. for(int i = 0; i < MessagesSize; ++i )
  425. xQueueReceive( adcDataHandle,(messages + i), portMAX_DELAY );
  426. }
  427. osDelay(1);
  428. }
  429. /* USER CODE END QueueAwaiterEntry */
  430. }
  431.  
  432. /**
  433. * @brief This function is executed in case of error occurrence.
  434. * @retval None
  435. */
  436. void Error_Handler(void)
  437. {
  438. /* USER CODE BEGIN Error_Handler_Debug */
  439. /* User can add his own implementation to report the HAL error return state */
  440.  
  441. /* USER CODE END Error_Handler_Debug */
  442. }
  443.  
  444. #ifdef USE_FULL_ASSERT
  445. /**
  446. * @brief Reports the name of the source file and the source line number
  447. * where the assert_param error has occurred.
  448. * @param file: pointer to the source file name
  449. * @param line: assert_param error line source number
  450. * @retval None
  451. */
  452. void assert_failed(uint8_t *file, uint32_t line)
  453. {
  454. /* USER CODE BEGIN 6 */
  455. /* User can add his own implementation to report the file name and line number,
  456. tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  457. /* USER CODE END 6 */
  458. }
  459. #endif /* USE_FULL_ASSERT */
  460.  
  461. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement