[STM32F0] 【NUCLEO-64试用】F091RC开箱及基于Keil MDK的RT-Thread Nano移植

[复制链接]
751|0
 楼主| hbzjt2011 发表于 2020-7-29 22:40 | 显示全部楼层 |阅读模式
一、开箱及简介:
       今天收到了论坛寄来的NUCLEO-F091RC开发板,晚上便小试了一下,板载的STMF091RCT6是以Cortex-M0为内核的高性能MCU,该芯片具有256KB的Flash和32KB的SRAM,主频可以到48MHz。此外,该芯片的一大特色就是外围通信接口异常丰富,具有两路I2C接口,两路SPI接口,八路UART,以及一路CAN接口,因此非常适合外设和通信模块比较多的应用场景。板子的靓照如下:
733855f21796f2e93a.png 106465f217984bcd95.png
二、RT-Thread Nano移植:
       RT-Thread Nano 是RT-Thread操作系统的极简版,其内存资源占用极小,功能包括任务处理、软件定时器、信号量、邮箱和实时调度等相对完整的实时操作系统特性,是一款可裁剪的、抢占式实时多任务的 RTOS。RT-Thread Nano可以使用多种方式进行移植,这里介绍通过使用Keil-MDK进行移植的过程。
1、首先使用STM32CubeMX软件选择NUCLEO-F091RC开发板生成裸机工程,在生成代码的过程中,需要注意将异常处理函数HardFault_Handler()和悬挂处理函数 PendSV_Handler()以及系统嘀嗒定时器函数SysTick_Handler()屏蔽掉,因为这几个函数已由 RT-Thread 实现。具有设置如下图所示。
294395f217cac01fe3.png
2、从官网下载RT-Thread Nano pack安装文件,RT-Thread Nano 离线安装包下载,下载结束后双击文件进行安装:
196975f218513e09fa.png
安装完毕以后,打开Keil工程文件,在 Manage Rum-Time Environment 里 "Software Component" 栏找到 RTOS,Variant 栏选择 RT-Thread,然后勾选 kernel,点击 "OK" 就添加 RT-Thread 内核到工程了。
621405f218597f2ee0.png
3、系统时钟配置,需要将主函数中的HAL_Init()函数和SystemClock_Config()函数注释掉,并在RT-Thread代码中的board.c里的调用和添加外部函数声明,如下图所示。
822885f2188285e3ac.png
714835f218893510fd.png
10915f2188a763ade.png
4、经过以上的步骤,便已经移植好了RT-Thread Nano。接下来就可以在主函数中引用“rtthread.h”并编写应用程序了。本例程通过静态创建线程的方法编写了LED灯闪烁和串口输出打印两个线程,代码如下:
  1. /* USER CODE BEGIN Header */
  2. /**
  3.   ******************************************************************************
  4.   * [url=home.php?mod=space&uid=288409]@file[/url]           : main.c
  5.   * [url=home.php?mod=space&uid=247401]@brief[/url]          : Main program body
  6.   ******************************************************************************
  7.   * @attention
  8.   *
  9.   * <h2><center>© Copyright (c) 2020 STMicroelectronics.
  10.   * All rights reserved.</center></h2>
  11.   *
  12.   * This software component is licensed by ST under BSD 3-Clause license,
  13.   * the "License"; You may not use this file except in compliance with the
  14.   * License. You may obtain a copy of the License at:
  15.   *                        opensource.org/licenses/BSD-3-Clause
  16.   *
  17.   ******************************************************************************
  18.   */
  19. /* USER CODE END Header */

  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. #include "usart.h"
  23. #include "gpio.h"

  24. /* Private includes ----------------------------------------------------------*/
  25. /* USER CODE BEGIN Includes */
  26. #include "rtthread.h"
  27. /* USER CODE END Includes */

  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN PTD */

  30. /* USER CODE END PTD */

  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN PD */
  33. /* USER CODE END PD */

  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN PM */

  36. /* USER CODE END PM */

  37. /* Private variables ---------------------------------------------------------*/

  38. /* USER CODE BEGIN PV */
  39. static struct rt_thread led_thread;
  40. static char led_thread_stack[256];

  41. static struct rt_thread serial_thread;
  42. static char serial_thread_stack[256];
  43. /* USER CODE END PV */

  44. /* Private function prototypes -----------------------------------------------*/
  45. void SystemClock_Config(void);
  46. /* USER CODE BEGIN PFP */

  47. /* USER CODE END PFP */

  48. /* Private user code ---------------------------------------------------------*/
  49. /* USER CODE BEGIN 0 */
  50. static void led_thread_entry(void *parameter)
  51. {
  52.         while(1)
  53.         {
  54.                 HAL_GPIO_TogglePin(LD2_GPIO_Port, LD2_Pin);
  55.                 rt_thread_mdelay(500);
  56.         }
  57. }

  58. static void serial_thread_entry(void *parameter)
  59. {
  60.         while(1)
  61.         {
  62.                 HAL_UART_Transmit(&huart2, "rt-thread nano running...", sizeof("rt-thread nano running..."), 0xFFFF);
  63.                 rt_thread_mdelay(1000);
  64.         }
  65. }
  66. /* USER CODE END 0 */

  67. /**
  68.   * @brief  The application entry point.
  69.   * @retval int
  70.   */
  71. int main(void)
  72. {
  73.   /* USER CODE BEGIN 1 */

  74.   /* USER CODE END 1 */

  75.   /* MCU Configuration--------------------------------------------------------*/

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

  78.   /* USER CODE BEGIN Init */

  79.   /* USER CODE END Init */

  80.   /* Configure the system clock */
  81.   //SystemClock_Config();

  82.   /* USER CODE BEGIN SysInit */

  83.   /* USER CODE END SysInit */

  84.   /* Initialize all configured peripherals */
  85.   MX_GPIO_Init();
  86.   MX_USART2_UART_Init();
  87.   /* USER CODE BEGIN 2 */
  88.    rt_err_t ret;
  89.    ret = rt_thread_init(&led_thread,
  90.                                                 "ledflash",
  91.                                                 led_thread_entry,
  92.                                                 RT_NULL,
  93.                                                 &led_thread_stack[0],
  94.                                                 sizeof(led_thread_stack),
  95.                                                 RT_THREAD_PRIORITY_MAX - 2,
  96.                                                 20);
  97.         if(ret == RT_EOK)
  98.         {
  99.                 rt_thread_startup(&led_thread);
  100.         }
  101.        
  102.    ret = rt_thread_init(&serial_thread,
  103.                                                 "serialtest",
  104.                                                 serial_thread_entry,
  105.                                                 RT_NULL,
  106.                                                 &serial_thread_stack[0],
  107.                                                 sizeof(serial_thread_stack),
  108.                                                 RT_THREAD_PRIORITY_MAX - 2,
  109.                                                 20);
  110.         if(ret == RT_EOK)
  111.         {
  112.                 rt_thread_startup(&serial_thread);
  113.         }
  114.    
  115.   /* USER CODE END 2 */

  116.   /* Infinite loop */
  117.   /* USER CODE BEGIN WHILE */
  118.   while (1)
  119.   {
  120.     /* USER CODE END WHILE */

  121.     /* USER CODE BEGIN 3 */
  122.           rt_thread_mdelay(200);
  123.   }
  124.   /* USER CODE END 3 */
  125. }

  126. /**
  127.   * @brief System Clock Configuration
  128.   * @retval None
  129.   */
  130. void SystemClock_Config(void)
  131. {
  132.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  133.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  134.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  135.   /** Initializes the CPU, AHB and APB busses clocks
  136.   */
  137.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  138.   RCC_OscInitStruct.HSEState = RCC_HSE_BYPASS;
  139.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  140.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  141.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL12;
  142.   RCC_OscInitStruct.PLL.PREDIV = RCC_PREDIV_DIV2;
  143.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  144.   {
  145.     Error_Handler();
  146.   }
  147.   /** Initializes the CPU, AHB and APB busses clocks
  148.   */
  149.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  150.                               |RCC_CLOCKTYPE_PCLK1;
  151.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  152.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  153.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;

  154.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  155.   {
  156.     Error_Handler();
  157.   }
  158.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  159.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  160.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  161.   {
  162.     Error_Handler();
  163.   }
  164. }

  165. /* USER CODE BEGIN 4 */

  166. /* USER CODE END 4 */

  167. /**
  168.   * @brief  This function is executed in case of error occurrence.
  169.   * @retval None
  170.   */
  171. void Error_Handler(void)
  172. {
  173.   /* USER CODE BEGIN Error_Handler_Debug */
  174.   /* User can add his own implementation to report the HAL error return state */

  175.   /* USER CODE END Error_Handler_Debug */
  176. }

  177. #ifdef  USE_FULL_ASSERT
  178. /**
  179.   * @brief  Reports the name of the source file and the source line number
  180.   *         where the assert_param error has occurred.
  181.   * @param  file: pointer to the source file name
  182.   * @param  line: assert_param error line source number
  183.   * @retval None
  184.   */
  185. void assert_failed(uint8_t *file, uint32_t line)
  186. {
  187.   /* USER CODE BEGIN 6 */
  188.   /* User can add his own implementation to report the file name and line number,
  189.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  190.   /* USER CODE END 6 */
  191. }
  192. #endif /* USE_FULL_ASSERT */

  193. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
编译程序,并通过板载的ST-Link下载程序至开发板即可看到LED每一秒亮灭一次,同时串口一秒打印输出一次。
969095f218a2c5f534.png
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎参与LabVIEW版块的讨论学习! 点我一键即达

323

主题

2908

帖子

45

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