如何在STM32快速创建 FREERTOS和RTX工程呢?

2019-07-14 17:52发布

如何在STM32快速创建 FREERTOS和RTX工程


友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
6条回答
bobnice
1楼-- · 2019-07-15 00:27
使用MDK 打开工程
从上到下 的组依次为  OS 的C文件
.s 启动文件
用户文件
HAL库文件
CMSIS中间件文件
其中 在 第一组中的 cmsis_os.c 中实现了  cmsis_os  到FREERTOS 的中间层转换   稍后会讨论其中一处代码
4.png
接下来  添加自己的代码  首先添加 072 上面LED吧  板子不在身边  记得是PA5 控制
先看看  main.c 的 64 到 105行

  1. int main(void)
  2. {

  3.   /* USER CODE BEGIN 1 */

  4.   /* USER CODE END 1 */

  5.   /* MCU Configuration----------------------------------------------------------*/

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

  8.   /* Configure the system clock */
  9.   SystemClock_Config();

  10.   /* Initialize all configured peripherals */
  11.   MX_GPIO_Init();
  12.   MX_USART2_UART_Init();

  13.   /* USER CODE BEGIN 2 */

  14.   /* USER CODE END 2 */

  15.   /* Init code generated for FreeRTOS */
  16.   /* Create Start thread */
  17.   osThreadDef(USER_Thread, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  18.   osThreadCreate (osThread(USER_Thread), NULL);

  19.   /* Start scheduler */
  20.   osKernelStart(NULL, NULL);

  21.   /* We should never get here as control is now taken by the scheduler */

  22.   /* USER CODE BEGIN 3 */
  23.   /* Infinite loop */
  24.   while (1)
  25.   {

  26.   }
  27.   /* USER CODE END 3 */

  28. }
复制代码
bobnice
2楼-- · 2019-07-15 01:10
mian函数  C代码的入口  初始化一些硬件后
  osThreadDef(USER_Thread, StartThread, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  osThreadCreate (osThread(USER_Thread), NULL);
  /* Start scheduler */
  osKernelStart(NULL, NULL);
定义了一个 线程 USER_Thread 然后启动OS  
注意 osThreadDef  是一个宏  定义一个用于描述  线程的结构体  并不是执行函数
宏的第二项参数 StartThread 为线程 入口函数地址。

  1. /* USER CODE BEGIN 4 */
  2. void Nucleo_072_Led(const void *par);
  3. /* USER CODE END 4 */

  4. static void StartThread(void const * argument)
  5. {

  6.   /* USER CODE BEGIN 5 */
  7.   osThreadDef(LED_Thread, Nucleo_072_Led, osPriorityNormal, 0, configMINIMAL_STACK_SIZE);
  8.   osThreadCreate (osThread(LED_Thread), NULL);
  9.   /* Infinite loop */
  10.   for(;;)
  11.   {
  12.     osDelay(1);
  13.   }

  14.   /* USER CODE END 5 */

  15. }
复制代码

至此mian函数的工作结束了 OS将转向 就绪线程并永不返回  也就是执行StartThread  


修改  StartThrea函数 如下
bobnice
3楼-- · 2019-07-15 05:22
 精彩回答 2  元偷偷看……
bobnice
4楼-- · 2019-07-15 06:47
  1.   {
  2.   osThreadDef( StartThread, osPriorityNormal, 0, 0);
  3.   osThreadCreate (osThread(StartThread), NULL);
  4.         }
  5. osThreadDef( Nucleo_072_Led, osPriorityNormal, 0, 0);
  6.   osThreadCreate (osThread(Nucleo_072_Led), NULL);
复制代码
不知为何  ST写的 中间件和 MDK的 接口有一点差距  所以 创建线程的地方需要如上修改
修改 stm32f0xx_hal_conf.h
添加 图示内容  不出意外  下面 将可以直接编译了!
8.png
写的有些多了  本来想 继续写 RTOS 基于串口的  应用  太长了 下次发帖写了
下面提出一个讨论 MX 创建的  工程 FREERTOS 中 cmsis_os.c 中 创建一个信号量  
osSemaphoreCreate参数 count 直接
传递给 xSemaphoreCreateCounting的两个形参  
9.png
bobnice
5楼-- · 2019-07-15 10:23
  1. #define xSemaphoreCreateCounting( uxMaxCount, uxInitialCount ) xQueueCreateCountingSemaphore( ( uxMaxCount ), ( uxInitialCount ) )
复制代码
该宏创建一个 数值型信号量 第一个参数是 信号量最大数值  第二个则为  初始化值
基于串口使用信号量  那么需要如下要求
假设 usart_sem 为串口使用的信号量
每收到一个数据 usart_sem ++  缓冲 1024字节
需要数据的线程 osSemaphoreWait(usart_sem ); 当有数据时 线程被激活  获取数据
如此我们知道 这个信号量的 最大值应为1024
可是使用 ST 的cmsis_os osSemaphoreCreate 创建一个信号亮 osSemaphoreCreate(0,1024);
会出现这样的问题 !  此信号量 被赋予初值1024  意味着 这个信号量将可以被osSemaphoreWait 1024次
显然这不是我们想要
通常  我们需要的数值型信号量  最大值可以很大  但是初值 基本为0,或1
zhuzb0754
6楼-- · 2019-07-15 13:54
cube创建 MDK编辑是最快的

一周热门 更多>