標(biāo)題: RT-Thread工程移植到STM32f103 RTT-RTC做TICK 串口DMA接收源程序 [打印本頁(yè)]

作者: 312859829    時(shí)間: 2019-8-1 21:56
標(biāo)題: RT-Thread工程移植到STM32f103 RTT-RTC做TICK 串口DMA接收源程序
       初學(xué)RT-Thread,官網(wǎng)給的歷程大多是HAL庫(kù)版本的,原來做的項(xiàng)目中都是用的固件庫(kù),底層驅(qū)動(dòng)移植起來相當(dāng)麻煩,于是網(wǎng)上找資料動(dòng)手移植起來,實(shí)現(xiàn)功能:①線程中LED閃爍;②軟件定時(shí)器回調(diào)函數(shù)打印“timer1 is timeout”;③串口DMA接收數(shù)據(jù),郵箱實(shí)現(xiàn)線程同步(信號(hào)量本已實(shí)現(xiàn),為了驗(yàn)證郵箱實(shí)現(xiàn)同樣的功能,屏蔽)并打印接收到的數(shù)據(jù)。
      平臺(tái):正點(diǎn)原子精英開發(fā)板
全部資料51hei下載地址:
RTT-RTC做TICK.7z (2.03 MB, 下載次數(shù): 29)
RT-Thread-串口DMA接收.7z (846.48 KB, 下載次數(shù): 60)

STM32單片機(jī)源程序如下:
  1. /*
  2. *************************************************************************
  3. *                             包含的頭文件
  4. *************************************************************************
  5. */
  6. #include "board.h"
  7. #include "rtthread.h"
  8. #include <string.h>

  9. /*
  10. ******************************************************************
  11. *                               變量
  12. ******************************************************************
  13. */
  14. /* 定義線程控制塊 */
  15. static rt_thread_t usart_thread = RT_NULL;

  16. /* 定義線程控制塊 */
  17. static rt_thread_t led1_thread = RT_NULL;

  18. /* 定義信號(hào)量控制塊 */
  19. rt_sem_t test_sem = RT_NULL;


  20. /* 郵 箱 控 制 塊 */
  21. struct rt_mailbox mb;
  22. /* 用 于 放 郵 件 的 內(nèi) 存 池 */
  23. static char mb_pool[128];

  24. /* 定 時(shí) 器 的 控 制 塊 */
  25. static rt_timer_t timer1;

  26. /************************* 全局變量聲明 ****************************/
  27. /*
  28. * 當(dāng)我們?cè)趯憫?yīng)用程序的時(shí)候,可能需要用到一些全局變量。
  29. */

  30. /* 相關(guān)宏定義 */
  31. extern char Usart_Rx_Buf[USART_RBUFF_SIZE];

  32. /*
  33. *************************************************************************
  34. *                             函數(shù)聲明
  35. *************************************************************************
  36. */
  37. static void usart_thread_entry(void* parameter);

  38. static void led1_thread_entry(void* parameter);
  39. static void timeout1(void *parameter);

  40. /*
  41. *************************************************************************
  42. *                             main 函數(shù)
  43. *************************************************************************
  44. */
  45. /**
  46.   * @brief  主函數(shù)
  47.   * @param  無(wú)
  48.   * @retval 無(wú)
  49.   */
  50. int main(void)
  51. {       
  52.         rt_err_t result;
  53.         /*
  54.          * 開發(fā)板硬件初始化,RTT系統(tǒng)初始化已經(jīng)在main函數(shù)之前完成,
  55.          * 即在component.c文件中的rtthread_startup()函數(shù)中完成了。
  56.          * 所以在main函數(shù)中,只需要?jiǎng)?chuàng)建線程和啟動(dòng)線程即可。
  57.          */

  58.         /* 創(chuàng)建一個(gè)信號(hào)量 */
  59.         //test_sem = rt_sem_create("test_sem",/* 消息隊(duì)列名字 */
  60.           //               0,     /* 信號(hào)量初始值,默認(rèn)有一個(gè)信號(hào)量 */
  61.          //                RT_IPC_FLAG_FIFO); /* 信號(hào)量模式 FIFO(0x00)*/
  62.         //if (test_sem != RT_NULL)
  63.         //rt_kprintf("信號(hào)量創(chuàng)建成功!\n\n");

  64.         /* 初 始 化 一 個(gè) mailbox */
  65.         result = rt_mb_init(&mb,
  66.                                         "mbt", /* 名 稱 是 mbt */
  67.                                         &mb_pool[0], /* 郵 箱 用 到 的 內(nèi) 存 池 是 mb_pool */
  68.                                         sizeof(mb_pool) / 4, /* 郵 箱 中 的 郵 件 數(shù) 目 , 因 為 一 封 郵
  69.                                         件 占 4 字 節(jié) */
  70.                                         RT_IPC_FLAG_FIFO); /* 采 用 FIFO 方 式 進(jìn) 行 線 程 等 待 */
  71.         if (result != RT_EOK)
  72.         {
  73.                 rt_kprintf("init mailbox failed.\n");
  74.                 return -1;
  75.         }

  76.         usart_thread =                          /* 線程控制塊指針 */
  77.         rt_thread_create( "usart",              /* 線程名字 */
  78.                           usart_thread_entry,   /* 線程入口函數(shù) */
  79.                           RT_NULL,             /* 線程入口函數(shù)參數(shù) */
  80.                           512,                 /* 線程棧大小 */
  81.                           2,                   /* 線程的優(yōu)先級(jí) */
  82.                           20);                 /* 線程時(shí)間片 */
  83.                       
  84.         /* 啟動(dòng)線程,開啟調(diào)度 */
  85.         if (usart_thread != RT_NULL)
  86.             rt_thread_startup(usart_thread);
  87.         else
  88.             return -1;

  89.        
  90.     led1_thread =                          /* 線程控制塊指針 */
  91.     rt_thread_create( "led1",              /* 線程名字 */
  92.                       led1_thread_entry,   /* 線程入口函數(shù) */
  93.                       RT_NULL,             /* 線程入口函數(shù)參數(shù) */
  94.                       512,                 /* 線程棧大小 */
  95.                       3,                   /* 線程的優(yōu)先級(jí) */
  96.                       20);                 /* 線程時(shí)間片 */
  97.                   
  98.     /* 啟動(dòng)線程,開啟調(diào)度 */
  99.    if (led1_thread != RT_NULL)
  100.         rt_thread_startup(led1_thread);
  101.     else
  102.         return -1;

  103.         /* 創(chuàng) 建 定 時(shí) 器 1 周 期 定 時(shí) 器 */
  104.         timer1 = rt_timer_create("timer1", timeout1,
  105.                                                         RT_NULL, 2500,
  106.                                                         RT_TIMER_FLAG_PERIODIC);
  107.         /* 啟 動(dòng) 定 時(shí) 器 1 */
  108.         if (timer1 != RT_NULL)
  109.                 rt_timer_start(timer1);

  110. }

  111. /*
  112. *************************************************************************
  113. *                             線程定義
  114. *************************************************************************
  115. */
  116. static void usart_thread_entry(void* parameter)
  117. {
  118.   rt_err_t uwRet = RT_EOK;       
  119.   char *str;
  120.     /* 任務(wù)都是一個(gè)無(wú)限循環(huán),不能返回 */
  121.   while (1)
  122.   {
  123.                 //uwRet = rt_sem_take(test_sem,        /* 獲取串口中斷的信號(hào)量 */
  124.                         //-1);           /* 等待時(shí)間:0 */
  125.     uwRet=rt_mb_recv(&mb, (rt_uint32_t *)&str, RT_WAITING_FOREVER);
  126.                     
  127.     if(RT_EOK == uwRet)
  128.     {
  129.       //rt_kprintf("收到數(shù)據(jù):%s\n",Usart_Rx_Buf);
  130.       rt_kprintf("收到數(shù)據(jù):%s\n",str);
  131.       memset(Usart_Rx_Buf,0,USART_RBUFF_SIZE);/* 清零 */
  132.     }


  133.   }
  134. }

  135. static void led1_thread_entry(void* parameter)
  136. {       
  137.     while (1)
  138.     {
  139.         LED1_ON;
  140.          rt_kprintf("LED1_ON\r\n");       
  141.         rt_thread_delay(5000);   /* 延時(shí)500個(gè)tick */
  142.         
  143.         LED1_OFF;  
  144.          rt_kprintf("LED1_OFF\r\n");       
  145.         rt_thread_delay(5000);   /* 延時(shí)500個(gè)tick */                                
  146.     }
  147. }

  148. /* 定 時(shí) 器 2 超 時(shí) 函 數(shù) */
  149. static void timeout1(void *parameter)
  150. {
  151.         rt_kprintf("timer1 is timeout\n");
  152. }


  153. /********************************END OF FILE****************************/
復(fù)制代碼








歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1