12下一页
返回列表 发新帖我要提问本帖赏金: 3.00元(功能说明)

[STM32F4] 【F446RE开发板试用】STM32 CubeMX生成freeRTOS

[复制链接]
 楼主| sasmike 发表于 2015-10-18 18:30 | 显示全部楼层 |阅读模式
本帖最后由 sasmike 于 2015-10-18 18:33 编辑

拿到论坛送的stm32f446板子了,最近学习freeRTOS。
发现cube可以自动配置,很是方便,给大家分享一下。比较简单,高手可以忽略下文。:lol

----------------------------------------------------------------------
实现功能:
创建2个任务,
一个串口任务,每一秒发送字符串,表示任务正在运行
另一个是LED反转任务,每1秒反转LED灯一次,然后串口发送字符串提示。先上效果:
0049.png
----------------------------------------------------------------------
1、打开cube,新建工程,选择STM32F446
0035.png
2、选择串口,GPIOA5,freeRTOS
0036.png
2、这个是配置界面
0045.png
3、配置LED引脚
0037.png
4、配置串口,这个用默认的就行了
0038.png
5、在freeRTOS中配置,首先是配置参数,这里用默认的,具体什么意思,可以看下面提示
0039.png
6、配置实用的函数,也是默认的
0040.png
7、在任务配置中添加串口2任务
0041.png
8、添加LED任务
0042.png
9、如下图所示
0043.png
9、生成MDK工程
0044.png
10、可以看到生成的工程,含有了很多代码了。
0046.png
11、添加自己的任务代码。
整个main.c代码如下:
  1. /**
  2.   ******************************************************************************
  3.   * File Name          : main.c
  4.   * Description        : Main program body
  5.   ******************************************************************************
  6.   *
  7.   * COPYRIGHT(c) 2015 STMicroelectronics
  8.   *
  9.   * Redistribution and use in source and binary forms, with or without modification,
  10.   * are permitted provided that the following conditions are met:
  11.   *   1. Redistributions of source code must retain the above copyright notice,
  12.   *      this list of conditions and the following disclaimer.
  13.   *   2. Redistributions in binary form must reproduce the above copyright notice,
  14.   *      this list of conditions and the following disclaimer in the documentation
  15.   *      and/or other materials provided with the distribution.
  16.   *   3. Neither the name of STMicroelectronics nor the names of its contributors
  17.   *      may be used to endorse or promote products derived from this software
  18.   *      without specific prior written permission.
  19.   *
  20.   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  23.   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  24.   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25.   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  26.   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  27.   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28.   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29.   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30.   *
  31.   ******************************************************************************
  32.   */
  33. /* Includes ------------------------------------------------------------------*/
  34. #include "stm32f4xx_hal.h"
  35. #include "cmsis_os.h"

  36. /* USER CODE BEGIN Includes */

  37. /* USER CODE END Includes */

  38. /* Private variables ---------------------------------------------------------*/
  39. UART_HandleTypeDef huart2;

  40. osThreadId defaultTaskHandle;
  41. osThreadId Uart2TaskHandle;
  42. osThreadId LedTaskHandle;

  43. /* USER CODE BEGIN PV */
  44. /* Private variables ---------------------------------------------------------*/

  45. /* USER CODE END PV */

  46. /* Private function prototypes -----------------------------------------------*/
  47. void SystemClock_Config(void);
  48. static void MX_GPIO_Init(void);
  49. static void MX_USART2_UART_Init(void);
  50. void StartDefaultTask(void const * argument);
  51. void Uart2StartTask(void const * argument);
  52. void LedStartTask(void const * argument);

  53. /* USER CODE BEGIN PFP */
  54. /* Private function prototypes -----------------------------------------------*/

  55. <font color="#ff0000">#ifdef __GNUC__
  56.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  57.      set to 'Yes') calls __io_putchar() */
  58.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  59. #else
  60.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  61. #endif /* __GNUC__ */</font>

  62. /* USER CODE END PFP */

  63. /* USER CODE BEGIN 0 */

  64. /* USER CODE END 0 */

  65. int main(void)
  66. {

  67.   /* USER CODE BEGIN 1 */

  68.   /* USER CODE END 1 */

  69.   /* MCU Configuration----------------------------------------------------------*/

  70.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  71.   HAL_Init();

  72.   /* Configure the system clock */
  73.   SystemClock_Config();

  74.   /* Initialize all configured peripherals */
  75.   MX_GPIO_Init();
  76.   MX_USART2_UART_Init();

  77.   /* USER CODE BEGIN 2 */
  78. <font color="#ff0000">        printf("\r\n------------- This is a freeRTOS test -------------\r\n\r\n");</font>
  79.         
  80.   /* USER CODE END 2 */

  81.   /* USER CODE BEGIN RTOS_MUTEX */
  82.   /* add mutexes, ... */
  83.   /* USER CODE END RTOS_MUTEX */

  84.   /* USER CODE BEGIN RTOS_SEMAPHORES */
  85.   /* add semaphores, ... */
  86.   /* USER CODE END RTOS_SEMAPHORES */

  87.   /* USER CODE BEGIN RTOS_TIMERS */
  88.   /* start timers, add new ones, ... */
  89.   /* USER CODE END RTOS_TIMERS */

  90.   /* Create the thread(s) */
  91.   /* definition and creation of defaultTask */
  92.   osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  93.   defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  94.   /* definition and creation of Uart2Task */
  95.   osThreadDef(Uart2Task, Uart2StartTask, osPriorityNormal, 0, 128);
  96.   Uart2TaskHandle = osThreadCreate(osThread(Uart2Task), NULL);

  97.   /* definition and creation of LedTask */
  98.   osThreadDef(LedTask, LedStartTask, osPriorityNormal, 0, 128);
  99.   LedTaskHandle = osThreadCreate(osThread(LedTask), NULL);

  100.   /* USER CODE BEGIN RTOS_THREADS */
  101.   /* add threads, ... */
  102.   /* USER CODE END RTOS_THREADS */

  103.   /* USER CODE BEGIN RTOS_QUEUES */
  104.   /* add queues, ... */
  105.   /* USER CODE END RTOS_QUEUES */


  106.   /* Start scheduler */
  107.   osKernelStart();
  108.   
  109.   /* We should never get here as control is now taken by the scheduler */

  110.   /* Infinite loop */
  111.   /* USER CODE BEGIN WHILE */
  112.   while (1)
  113.   {
  114.   /* USER CODE END WHILE */

  115.   /* USER CODE BEGIN 3 */

  116.   }
  117.   /* USER CODE END 3 */

  118. }

  119. /** System Clock Configuration
  120. */
  121. void SystemClock_Config(void)
  122. {

  123.   RCC_OscInitTypeDef RCC_OscInitStruct;
  124.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  125.   __PWR_CLK_ENABLE();

  126.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

  127.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  128.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  129.   RCC_OscInitStruct.HSICalibrationValue = 16;
  130.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  131.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;
  132.   RCC_OscInitStruct.PLL.PLLM = 8;
  133.   RCC_OscInitStruct.PLL.PLLN = 180;
  134.   RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  135.   RCC_OscInitStruct.PLL.PLLQ = 2;
  136.   RCC_OscInitStruct.PLL.PLLR = 2;
  137.   HAL_RCC_OscConfig(&RCC_OscInitStruct);

  138.   HAL_PWREx_ActivateOverDrive();

  139.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK|RCC_CLOCKTYPE_PCLK1
  140.                               |RCC_CLOCKTYPE_PCLK2;
  141.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  142.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  143.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  144.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
  145.   HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);

  146.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  147.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  148.   /* SysTick_IRQn interrupt configuration */
  149.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  150. }

  151. /* USART2 init function */
  152. void MX_USART2_UART_Init(void)
  153. {

  154.   huart2.Instance = USART2;
  155.   huart2.Init.BaudRate = 115200;
  156.   huart2.Init.WordLength = UART_WORDLENGTH_8B;
  157.   huart2.Init.StopBits = UART_STOPBITS_1;
  158.   huart2.Init.Parity = UART_PARITY_NONE;
  159.   huart2.Init.Mode = UART_MODE_TX_RX;
  160.   huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  161.   huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  162.   HAL_UART_Init(&huart2);

  163. }

  164. /** Configure pins as
  165.         * Analog
  166.         * Input
  167.         * Output
  168.         * EVENT_OUT
  169.         * EXTI
  170. */
  171. void MX_GPIO_Init(void)
  172. {

  173.   GPIO_InitTypeDef GPIO_InitStruct;

  174.   /* GPIO Ports Clock Enable */
  175.   __GPIOA_CLK_ENABLE();

  176.   /*Configure GPIO pin : PA5 */
  177.   GPIO_InitStruct.Pin = GPIO_PIN_5;
  178.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  179.   GPIO_InitStruct.Pull = GPIO_PULLUP;
  180.   GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  181.   HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

  182. }

  183. /* USER CODE BEGIN 4 */

  184. <font color="#ff0000">/**
  185.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Retargets the C library printf function to the USART.
  186.   * @param  None
  187.   * @retval None
  188.   */
  189. PUTCHAR_PROTOTYPE
  190. {
  191.   /* Place your implementation of fputc here */
  192.   /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  193.   HAL_UART_Transmit(&huart2, (uint8_t *)&ch, 1, 0xFFFF);

  194.   return ch;
  195. }</font>

  196. /* USER CODE END 4 */

  197. /* StartDefaultTask function */
  198. void StartDefaultTask(void const * argument)
  199. {

  200.   /* USER CODE BEGIN 5 */
  201.   /* Infinite loop */
  202.   for(;;)
  203.   {
  204.     osDelay(1);
  205.   }
  206.   /* USER CODE END 5 */
  207. }

  208. /* Uart2StartTask function */
  209. void Uart2StartTask(void const * argument)
  210. {
  211.   /* USER CODE BEGIN Uart2StartTask */
  212.   /* Infinite loop */
  213.   for(;;)
  214.   {
  215. <font color="#ff0000">                printf("Uart2 task is running.\r\n");
  216.     osDelay(1000);</font>
  217.   }
  218.   /* USER CODE END Uart2StartTask */
  219. }

  220. /* LedStartTask function */
  221. void LedStartTask(void const * argument)
  222. {
  223.   /* USER CODE BEGIN LedStartTask */
  224.   /* Infinite loop */
  225.   for(;;)
  226.   {
  227. <font color="#ff0000">                HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
  228.                 printf("LED task is running.\r\n");
  229.     osDelay(1000);</font>
  230.   }
  231.   /* USER CODE END LedStartTask */
  232. }

  233. #ifdef USE_FULL_ASSERT

  234. /**
  235.    * [url=home.php?mod=space&uid=247401]@brief[/url] Reports the name of the source file and the source line number
  236.    * where the assert_param error has occurred.
  237.    * @param file: pointer to the source file name
  238.    * @param line: assert_param error line source number
  239.    * @retval None
  240.    */
  241. void assert_failed(uint8_t* file, uint32_t line)
  242. {
  243.   /* USER CODE BEGIN 6 */
  244.   /* User can add his own implementation to report the file name and line number,
  245.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  246.   /* USER CODE END 6 */

  247. }

  248. #endif

  249. /**
  250.   * @}
  251.   */

  252. /**
  253.   * @}
  254. */

  255. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/



打赏榜单

21ic小喇叭 打赏了 3.00 元 2015-10-30

598330983 发表于 2015-10-18 19:27 来自手机 | 显示全部楼层
断言的代码删掉也不影响运行吧?
 楼主| sasmike 发表于 2015-10-18 20:00 | 显示全部楼层
598330983 发表于 2015-10-18 19:27
断言的代码删掉也不影响运行吧?

是的,断言删掉应该不影响的
alex_li0824 发表于 2015-10-18 21:46 | 显示全部楼层
学习了,哈哈
598330983 发表于 2015-10-19 10:03 | 显示全部楼层
sasmike 发表于 2015-10-18 20:00
是的,断言删掉应该不影响的

我一般都是删的只剩下自己想要的代码,这样看起来简洁,要不乱乱的不爽。
逍遥李 发表于 2015-10-19 16:35 | 显示全部楼层
楼主为什么我的编译过程中报错呀?
“..\Middlewares\Third_Party\FreeRTOS\Source\portable\RVDS\ARM_CM4F\port.c(79): error:  #35: #error directive: This port can only be used when the project options are configured to enable hardware floating point support.
        #error This port can only be used when the project options are configured to enable hardware floating point support.
..\Middlewares\Third_Party\FreeRTOS\Source\portable\RVDS\ARM_CM4F\port.c: 0 warnings, 1 error

是我的软件问题吗?

 楼主| sasmike 发表于 2015-10-19 21:58 | 显示全部楼层
598330983 发表于 2015-10-19 10:03
我一般都是删的只剩下自己想要的代码,这样看起来简洁,要不乱乱的不爽。 ...

多谢指导,程序写好了不修改的话这样做比较好。
不过要是需要重新从cube中生成的话,可能有问题了
 楼主| sasmike 发表于 2015-10-19 22:01 | 显示全部楼层
逍遥李 发表于 2015-10-19 16:35
楼主为什么我的编译过程中报错呀?
“..\Middlewares\Third_Party\FreeRTOS\Source\portable\RVDS\ARM_CM4F ...

感觉是的问题好奇怪,我直接配置的没有问题呀,你再查查软件安装
逍遥李 发表于 2015-10-20 09:41 | 显示全部楼层
sasmike 发表于 2015-10-19 22:01
感觉是的问题好奇怪,我直接配置的没有问题呀,你再查查软件安装

你软件版本?
仙人球W 发表于 2015-10-20 09:51 | 显示全部楼层
不错 准入门槛越来越低了呀
moyanming2013 发表于 2015-10-20 11:10 | 显示全部楼层
逍遥李 发表于 2015-10-19 16:35
楼主为什么我的编译过程中报错呀?
“..\Middlewares\Third_Party\FreeRTOS\Source\portable\RVDS\ARM_CM4F ...

打开浮点运算预定义
逍遥李 发表于 2015-10-20 11:10 | 显示全部楼层
moyanming2013 发表于 2015-10-20 11:10
打开浮点运算预定义

在哪里打开啊,请指点……非常感谢!
moyanming2013 发表于 2015-10-20 11:19 | 显示全部楼层
逍遥李 发表于 2015-10-20 11:10
在哪里打开啊,请指点……非常感谢!

在工程选项(Project->Options for target "XXXX")中的C/C++选项卡的Define中加入如下的语句:__FPU_PRESENT=1,__FPU_USED =1。
你也可以搜索更多相关的配置
逍遥李 发表于 2015-10-20 16:45 | 显示全部楼层
moyanming2013 发表于 2015-10-20 11:19
在工程选项(Project->Options for target "XXXX")中的C/C++选项卡的Define中加入如下的语句:__FPU_PRE ...

谢谢了!
598330983 发表于 2015-10-20 22:15 | 显示全部楼层
(Project->Options for target "XXXX")中的C/C++选项卡的Define中加入如下的语句:__FPU_PRESENT=1,__FPU_USED =1
这个以前我也是不知道的
 楼主| sasmike 发表于 2015-10-22 23:48 | 显示全部楼层
 楼主| sasmike 发表于 2015-10-22 23:49 | 显示全部楼层
仙人球W 发表于 2015-10-20 09:51
不错 准入门槛越来越低了呀

是呀,快傻瓜式了;P
 楼主| sasmike 发表于 2015-10-22 23:50 | 显示全部楼层
598330983 发表于 2015-10-20 22:15
(Project->Options for target "XXXX")中的C/C++选项卡的Define中加入如下的语句:__FPU_PRESENT=1,__FPU ...

共同学习!
逍遥李 发表于 2015-10-23 08:49 | 显示全部楼层
moyanming2013 发表于 2015-10-20 11:19
在工程选项(Project->Options for target "XXXX")中的C/C++选项卡的Define中加入如下的语句:__FPU_PRE ...

经测试,这方法不行……
逍遥李 发表于 2015-10-23 10:18 | 显示全部楼层

我的是4.10.1,莫非是这个原因?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

4

主题

56

帖子

2

粉丝
快速回复 在线客服 返回列表 返回顶部