RTCSD_第二次作业


一、开发环境搭建

  • 完成开发环境的升级安装

  • 从老师的github上clone项目到本地电脑的Ubuntu虚拟机中

# sudo apt-get update
# sudo apt-get upgrade
# sudo apt-get install build-essential git 
# sudo apt-get install gcc-arm-none-eabi gdb-arm-none-eabi
# mkdir work
# cd work
# git clone https://github.com/cbhust/STM32F429_Discovery_FreeRTOS_9.git

二、QEMU安装

#cd ~/work
#tar xvf gnuarmeclipse-qemu-debian64-2.8.0-201612271623-dev.tgz
#chmod -R -w ./qemu
  • 把路径添加到$PATH变量中

为使新路径永久生效,采取了将新路径添加到profile文件中的方法

echo 'PATH="~/work/qemu/2.8.0-201612271623-dev/bin/:$PATH"'>>/etc/profile 

三、编译例程

  • 编译Demo1例程并通过Qemu运行例程
#cd  ~/work/STM32F429_Discovery_FreeRTOS_9/Projects/Demo1
#make
#./qemu.sh

可以在弹出的GUI中看到仿真板子红绿灯闪烁的场景

四、编程作业

作业要求

  • 创建三个任务:Sender_Task,Receiver_Task, Monitor_Task
  • Sender_Task的任务执行周期为2ms,Receiver_Task的任务执行周期为1000ms, Monitor_Task的任务执行周期为10000ms。
  • Sender_Task在每个执行周期向Receiver_Task发送一个32位无符号整数,第一次发送1,然后依次发送2,3,4......,发送完10000后再从1开始发送。同时对发送的数据进行计算累加计算并保存当前累加结果。
  • Receiver_Task对接收到的数据进行和Sender_Task同样的累加计算并保存当前累加结果。
  • Monitor_Task在每个执行周期检查Sender_Task发送的每个数据是否都被Receiver_Task正确的接收和处理,请自行设计一种检查机制并实现。
  • 可利用STM32F429I Discovery开发板的相关硬件(LED/LCD/串口)来输出相关状态信息。
  • 使用FreeRTOS的任务间通信和同步API完成上述功能。

实现思路

  • 用队列(FIFO)的方式实现FreeRTOS的任务间通信
  • Sender_Task每隔2ms向队列中发一个数,Receiver_Task每隔1000ms将队列中数全部接受
  • 通过比较Sender和Receiver的累加值来实现Monitor_Task的检查功能,但两个函数的周期不同步是一个难点,前者是逐步上升,后者是骤升,后者最多可能落后前者1000/2=500个数,所以两者累加值大部分时间都是不相等的。故这边设置了一个检测范围,即两者的误差不能大于前者当前发送值再往前数500个数的总和,即(SendNum+SendNum-500)500/2=(2SendNum-500)*250
  • 若检测结果正常,则绿灯闪烁,若不正常,则红灯闪烁

实现代码

  • 函数外

创建队列句柄,创建全局变量,另外还需要添加"queue.h"头文件

#include "queue.h"  

xQueueHandle MsgQueue; 
unsigned long SendNum;
unsigned long SendSumup = 0;
unsigned long ReceiveSumup = 0;
  • main

完成初始化,队列创建,任务创建
队列的单位是32位无符号整数,保险起见,深度设为10000

int main(void)
{
       Hardware_Init();

       /* Init and start tracing*/
       vTraceEnable(TRC_INIT);
       vTraceEnable(TRC_START);

       /* Creat Queue */
       MsgQueue=xQueueCreate(10000,sizeof(unsigned long));

       /* Create tasks */
       xTaskCreate(

                  Sender_Task,
                  "Sender_Task",
                  configMINIMAL_STACK_SIZE,	
                  (void*) NULL,
                  tskIDLE_PRIORITY+4UL,
                  NULL 
       );

       xTaskCreate(
                  Receiver_Task,
                  "Receiver_Task",
                  configMINIMAL_STACK_SIZE,
                  (void*) NULL,
                  tskIDLE_PRIORITY+3UL,
                  NULL
       );

       xTaskCreate(
                  Monitor_Task,
                  "Monitor_Task",
                  configMINIMAL_STACK_SIZE,
                  (void*) NULL,	
                  tskIDLE_PRIORITY+2UL, 
                  NULL
       );

	/* Start the scheduler. */
	vTaskStartScheduler();

	/* If all is well, the scheduler will now be running, and the following line will never be reached.  If the following line does execute, then there was insufficient FreeRTOS heap memory available for the idle and/or timer tasks to be created.  */

	for( ;; );
}
  • Sender_Task
void Sender_Task(void *pvParameters)
{
    SendNum=0;
    
    while (1) 
    {
        SendNum++;
        if(SendNum > 10000)
        {
            SendNum=1;
        }
        xQueueSend(MsgQueue,(void*)&SendNum,0);
        SendSumup += SendNum;
        vTaskDelay(2);
    }
}
  • Receiver_Task
void Receiver_Task(void *pvParameters)
{
    unsigned long ReceiveNum;
    
    while(1)
    {
        while(xQueueReceive(MsgQueue,&ReceiveNum,0/portTICK_RATE_MS ) == pdTRUE )
	{
	    ReceiveSumup += ReceiveNum;
	}
	vTaskDelay(1000);
    }
}
  • Monitor_Task
void Monitor_Task(void *pvParameters)
{
    while(1)
    {
    	if(SendSumup - ReceiveSumup <((2 * SendNum -500) * 250))
    	{
    	    Green_LED_On();
    	    vTaskDelay(1000);
    	    Green_LED_Off();
        }
    	else
    	{
    	    Red_LED_On();
    	    vTaskDelay(1000);
    	    Red_LED_Off();
        }
    	vTaskDelay(9000);
    }
}

运行结果

绿灯闪烁,程序运行正常

折腾许久终于在ubuntu上git push 成功了

git地址:https://github.com/JimiHe/STM32F429_Discovery_FreeRTOS_9

posted on 2017-09-30 17:28  Hust_Jamie  阅读(282)  评论(5编辑  收藏  举报