硬汉嵌入式论坛

 找回密码
 立即注册
查看: 2783|回复: 1
收起左侧

[以太网] 新版RL-TCPnet V7.X网络协议栈中比较重要的RTOS接口文件net_rots2.h

[复制链接]

1万

主题

7万

回帖

11万

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
111540
QQ
发表于 2018-4-20 02:41:00 | 显示全部楼层 |阅读模式
内容备份下,方便直接查看:
通过这个接口文件,其它RTOS也可以使用。
  1. /*------------------------------------------------------------------------------
  2. * MDK Middleware - Component ::Network
  3. * Copyright (c) 2004-2018 ARM Germany GmbH. All rights reserved.
  4. *------------------------------------------------------------------------------
  5. * Name:    net_rtos2.h
  6. * Purpose: Network CMSIS-RTOS2 abstraction layer
  7. * Rev.:    V7.7.1
  8. *----------------------------------------------------------------------------*/

  9. #include "cmsis_os2.h"
  10. #include "rl_net_lib.h"

  11. /* Avoid syntax-checker errors in editor */
  12. #ifndef NET_THREAD_STACK_SIZE
  13.   #define NET_THREAD_STACK_SIZE 1024
  14.   #define NET_THREAD_PRIORITY   osPriorityNormal
  15. #endif

  16. /* Network core resources */
  17. #ifdef RTE_CMSIS_RTOS2_RTX5
  18.   #include "rtx_os.h"
  19.   static osRtxMutex_t  net_lock_cb    __attribute__((section(".bss.os.mutex.cb")));
  20.   static osRtxMutex_t  mem_lock_cb    __attribute__((section(".bss.os.mutex.cb")));
  21.   static osRtxThread_t net_thread_cb  __attribute__((section(".bss.os.thread.cb")));
  22.   static uint64_t      net_stack[NET_THREAD_STACK_SIZE/8]
  23.                                       __attribute__((section(".bss.os.thread.stack")));
  24.   #define __NET_LOCK   &net_lock_cb,   sizeof(net_lock_cb)
  25.   #define __MEM_LOCK   &mem_lock_cb,   sizeof(mem_lock_cb)
  26.   #define __NET_TCB    &net_thread_cb, sizeof(net_thread_cb)
  27.   #define __NET_STACK  &net_stack,     sizeof(net_stack)
  28. #else
  29.   #define __NET_LOCK   NULL, 0
  30.   #define __MEM_LOCK   NULL, 0
  31.   #define __NET_TCB    NULL, 0
  32.   #define __NET_STACK  NULL, NET_THREAD_STACK_SIZE
  33. #endif

  34. static const osMutexAttr_t net_lock = {
  35.   "netCoreMutex",
  36.   osMutexPrioInherit | osMutexRecursive | osMutexRobust,
  37.   __NET_LOCK
  38. };

  39. static const osMutexAttr_t mem_lock = {
  40.   "netMemoryMutex",
  41.   osMutexPrioInherit | osMutexRobust,
  42.   __MEM_LOCK
  43. };

  44. static const osThreadAttr_t net_thread = {
  45.   "netCoreThread",
  46.   osThreadDetached,
  47.   __NET_TCB,
  48.   __NET_STACK,
  49.   NET_THREAD_PRIORITY,
  50.   0,
  51.   0
  52. };

  53. /* Ethernet interface resources */
  54. #if (ETH0_ENABLE)
  55. #ifdef RTE_CMSIS_RTOS2_RTX5
  56.   static osRtxSemaphore_t eth0_lock_cb   __attribute__((section(".bss.os.semaphore.cb")));
  57.   static osRtxThread_t    eth0_thread_cb __attribute__((section(".bss.os.thread.cb")));
  58.   static uint64_t         eth0_stack[ETH0_THREAD_STACK_SIZE/8]
  59.                                          __attribute__((section(".bss.os.thread.stack")));
  60.   #define __ETH0_LOCK     e0_lock_cb,   sizeof(eth0_lock_cb)
  61.   #define __ETH0_TCB      e0_thread_cb, sizeof(eth0_thread_cb)
  62.   #define __ETH0_STACK    e0_stack,     sizeof(eth0_stack)
  63. #else
  64.   #define __ETH0_LOCK     NULL, 0
  65.   #define __ETH0_TCB      NULL, 0
  66.   #define __ETH0_STACK    NULL, ETH0_THREAD_STACK_SIZE
  67. #endif

  68. static const osSemaphoreAttr_t eth0_lock = {
  69.   "netEth0_Semaphore",
  70.   0,
  71.   __ETH0_LOCK
  72. };

  73. static const osThreadAttr_t eth0_thread = {
  74.   "netEth0_Thread",
  75.   osThreadDetached,
  76.   __ETH0_TCB,
  77.   __ETH0_STACK,
  78.   ETH0_THREAD_PRIORITY,
  79.   0,
  80.   0
  81. };
  82. #endif

  83. /* PPP interface resources */
  84. #if (PPP_ENABLE)
  85. #ifdef RTE_CMSIS_RTOS2_RTX5
  86.   static osRtxSemaphore_t ppp_lock_cb    __attribute__((section(".bss.os.semaphore.cb")));
  87.   static osRtxThread_t    ppp_thread_cb  __attribute__((section(".bss.os.thread.cb")));
  88.   static uint64_t         ppp_stack[PPP_THREAD_STACK_SIZE/8]
  89.                                          __attribute__((section(".bss.os.thread.stack")));
  90.   #define __PPP_LOCK      &ppp_lock_cb,   sizeof(ppp_lock_cb)
  91.   #define __PPP_TCB       &ppp_thread_cb, sizeof(ppp_thread_cb)
  92.   #define __PPP_STACK     &ppp_stack,     sizeof(ppp_stack)
  93. #else
  94.   #define __PPP_LOCK      NULL, 0
  95.   #define __PPP_TCB       NULL, 0
  96.   #define __PPP_STACK     NULL, PPP_THREAD_STACK_SIZE
  97. #endif

  98. static const osSemaphoreAttr_t ppp_lock = {
  99.   "netPPP_Semaphore",
  100.   0,
  101.   __PPP_LOCK
  102. };

  103. static const osThreadAttr_t ppp_thread = {
  104.   "netPPP_Thread",
  105.   osThreadDetached,
  106.   __PPP_TCB,
  107.   __PPP_STACK,
  108.   PPP_THREAD_PRIORITY,
  109.   0,
  110.   0
  111. };
  112. #endif

  113. /* SLIP interface resources */
  114. #if (SLIP_ENABLE)
  115. #ifdef RTE_CMSIS_RTOS2_RTX5
  116.   static osRtxSemaphore_t slip_lock_cb   __attribute__((section(".bss.os.semaphore.cb")));
  117.   static osRtxThread_t    slip_thread_cb __attribute__((section(".bss.os.thread.cb")));
  118.   static uint64_t         slip_stack[SLIP_THREAD_STACK_SIZE/8]
  119.                                          __attribute__((section(".bss.os.thread.stack")));
  120.   #define __SLIP_LOCK     &slip_lock_cb,   sizeof(slip_lock_cb)
  121.   #define __SLIP_TCB      &slip_thread_cb, sizeof(slip_thread_cb)
  122.   #define __SLIP_STACK    &slip_stack,     sizeof(slip_stack)
  123. #else
  124.   #define __SLIP_LOCK     NULL, 0
  125.   #define __SLIP_TCB      NULL, 0
  126.   #define __SLIP_STACK    NULL, SLIP_THREAD_STACK_SIZE
  127. #endif

  128. static const osSemaphoreAttr_t slip_lock = {
  129.   "netSLIP_Semaphore",
  130.   0,
  131.   __SLIP_LOCK
  132. };

  133. static const osThreadAttr_t slip_thread = {
  134.   "netSLIP_Thread",
  135.   osThreadDetached,
  136.   __SLIP_TCB,
  137.   __SLIP_STACK,
  138.   SLIP_THREAD_PRIORITY,
  139.   0,
  140.   0
  141. };
  142. #endif


  143. /* Create network core thread */
  144. NETOS_ID netos_thread_create (void) {
  145.   return (osThreadNew ((osThreadFunc_t)&netCore_Thread, NULL, &net_thread));
  146. }

  147. /* Delete network thread */
  148. void netos_thread_delete (NETOS_ID thread) {
  149.   osThreadTerminate (thread);
  150. }

  151. /* Get running thread identifier */
  152. NETOS_ID netos_thread_id (void) {
  153.   return (osThreadGetId ());
  154. }

  155. /* Pass control to next ready thread */
  156. void netos_thread_pass (void) {
  157.   osThreadYield ();
  158. }

  159. /* Create periodic tick timer */
  160. NETOS_ID netos_timer_create (void) {
  161.   return (osTimerNew ((osTimerFunc_t)&net_sys_tick, osTimerPeriodic, NULL, NULL));
  162. }

  163. /* Delete periodic tick timer */
  164. void netos_timer_delete (NETOS_ID timer) {
  165.   osTimerDelete (timer);
  166. }

  167. /* Start periodic tick timer */
  168. void netos_timer_start (NETOS_ID timer, uint32_t interval_ms) {
  169.   osTimerStart (timer, interval_ms);
  170. }

  171. /* Create network protection mutex */
  172. NETOS_ID netos_mutex_create (uint8_t sys_id) {
  173.   switch (sys_id) {
  174.     case 0:  return (osMutexNew (&net_lock));
  175.     default: return (osMutexNew (&mem_lock));
  176.   }
  177. }

  178. /* Delete network protection mutex */
  179. void netos_mutex_delete (NETOS_ID mutex) {
  180.   osMutexDelete (mutex);
  181. }

  182. /* Lock network protection mutex */
  183. void netos_lock (NETOS_ID mutex) {
  184.   osMutexAcquire (mutex, osWaitForever);
  185. }

  186. /* Unlock network protection mutex */
  187. void netos_unlock (NETOS_ID mutex) {
  188.   osMutexRelease (mutex);
  189. }

  190. /* Wait for thread signal/event flag */
  191. void netos_flag_wait (uint32_t flag, uint32_t ms) {
  192.   osThreadFlagsWait (flag, osFlagsWaitAny, ms);
  193. }

  194. /* Set thread signal/event flag */
  195. void netos_flag_set (NETOS_ID thread, uint32_t flag) {
  196.   osThreadFlagsSet (thread, flag);
  197. }

  198. /* Clear thread signal/event flag */
  199. void netos_flag_clear (NETOS_ID thread, uint32_t flag) {
  200.   (void)thread;
  201.   osThreadFlagsClear (flag);
  202. }

  203. /* Delay thread execution */
  204. void netos_delay (uint32_t ms) {
  205.   osDelay (ms);
  206. }

  207. /* Create network interface thread and semaphore */
  208. NETOS_ID netif_create (uint8_t netif, NETOS_ID *semaphore) {
  209.   switch (netif) {
  210. #if (ETH0_ENABLE)
  211.     case  NETIF_ETH:
  212.       *semaphore = osSemaphoreNew (1, 1, e0_lock);
  213.       return (osThreadNew ((osThreadFunc_t)&netETH_Thread, NULL, e0_thread));
  214. #endif
  215. #if (PPP_ENABLE)
  216.     case NETIF_PPP:
  217.       *semaphore = osSemaphoreNew (1, 1, &ppp_lock);
  218.       return (osThreadNew ((osThreadFunc_t)&netPPP_Thread, NULL, &ppp_thread));
  219. #endif
  220. #if (SLIP_ENABLE)
  221.     case NETIF_SLIP:
  222.       *semaphore = osSemaphoreNew (1, 1, &slip_lock);
  223.       return (osThreadNew ((osThreadFunc_t)&netSLIP_Thread, NULL, &slip_thread));
  224. #endif
  225.   }
  226.   return (NULL);
  227. }

  228. /* Delete network interface thread and semaphore */
  229. void netif_delete (NETOS_ID thread, NETOS_ID semaphore) {
  230.   osSemaphoreDelete (semaphore);
  231.   osThreadTerminate (thread);
  232. }

  233. /* Lock interface protection semaphore */
  234. void netif_lock (NETOS_ID semaphore) {
  235.   osSemaphoreAcquire (semaphore, osWaitForever);
  236. }

  237. /* Unlock interface protection semaphore */
  238. void netif_unlock (NETOS_ID semaphore) {
  239.   osSemaphoreRelease (semaphore);
  240. }
复制代码




回复

使用道具 举报

655

主题

3299

回帖

5289

积分

论坛元老

积分
5289
发表于 2018-4-20 14:03:12 | 显示全部楼层
貌似太复杂,没有看懂喔
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋|Archiver|手机版|硬汉嵌入式论坛

GMT+8, 2024-11-1 07:16 , Processed in 0.257671 second(s), 26 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2023, Tencent Cloud.

快速回复 返回顶部 返回列表