rtthread_service.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147
  1. /* rtthread includes */
  2. #include <osdep_service.h>
  3. #include <rtthread_service.h>
  4. #ifdef SECTION
  5. #undef SECTION
  6. #endif
  7. #include <rthw.h>
  8. #include <rtthread.h>
  9. #include <stdio.h>
  10. // #define RTTHREAD_SERVICE_DEBUG
  11. #define RTTHREAD_SERVICE_DEBUG_LEVEL 2
  12. /********************* os depended utilities ********************/
  13. #ifdef RTTHREAD_SERVICE_DEBUG
  14. #define DEBUG_LOG(_level, fmt, args...) if ((_level) >= RTTHREAD_SERVICE_DEBUG_LEVEL) rt_kprintf(fmt, args)
  15. #else
  16. #define DEBUG_LOG(level, fmt, args...)
  17. #endif
  18. #ifndef USE_MUTEX_FOR_SPINLOCK
  19. #define USE_MUTEX_FOR_SPINLOCK 1
  20. #endif
  21. extern uint32_t pmu_yield_os_check(void);
  22. extern _LONG_CALL_ void DelayUs(u32 us);
  23. extern _LONG_CALL_ void DelayMs(u32 ms);
  24. extern uint32_t pmu_set_sysactive_time(uint32_t timeout_ms);
  25. void save_and_cli(void)
  26. {
  27. DEBUG_LOG(1, "L:%d fun:%s runing...\n", __LINE__, __FUNCTION__);
  28. rt_enter_critical();
  29. }
  30. void restore_flags(void)
  31. {
  32. DEBUG_LOG(1, "L:%d fun:%s runing...\n", __LINE__, __FUNCTION__);
  33. rt_exit_critical();
  34. }
  35. void cli(void)
  36. {
  37. DEBUG_LOG(1, "L:%d fun:%s runing...\n", __LINE__, __FUNCTION__);
  38. rt_hw_interrupt_disable();
  39. }
  40. /* Not needed on 64bit architectures */
  41. static unsigned int __div64_32(u64 *n, unsigned int base)
  42. {
  43. u64 rem = *n;
  44. u64 b = base;
  45. u64 res, d = 1;
  46. unsigned int high = rem >> 32;
  47. /* Reduce the thing a bit first */
  48. res = 0;
  49. if (high >= base) {
  50. high /= base;
  51. res = (u64) high << 32;
  52. rem -= (u64) (high * base) << 32;
  53. }
  54. while ((u64)b > 0 && b < rem) {
  55. b = b+b;
  56. d = d+d;
  57. }
  58. do {
  59. if (rem >= b) {
  60. rem -= b;
  61. res += d;
  62. }
  63. b >>= 1;
  64. d >>= 1;
  65. } while (d);
  66. *n = res;
  67. return rem;
  68. }
  69. /********************* os depended service ********************/
  70. u8* _rtthread_malloc(u32 sz)
  71. {
  72. void *pbuf;
  73. DEBUG_LOG(2, "L:%d fun:%s sz:%d\n", __LINE__, __FUNCTION__, sz);
  74. pbuf = rt_malloc(sz);
  75. return pbuf;
  76. }
  77. u8* _rtthread_zmalloc(u32 sz)
  78. {
  79. void *pbuf = rt_malloc(sz);
  80. DEBUG_LOG(2, "L:%d fun:%s sz:%d\n", __LINE__, __FUNCTION__, sz);
  81. if (pbuf != RT_NULL)
  82. memset(pbuf, 0, sz);
  83. return pbuf;
  84. }
  85. void _rtthread_mfree(u8 *pbuf, u32 sz)
  86. {
  87. DEBUG_LOG(2, "L:%d fun:%s\n", __LINE__, __FUNCTION__);
  88. rt_free(pbuf);
  89. }
  90. static void _rtthread_memcpy(void* dst, void* src, u32 sz)
  91. {
  92. DEBUG_LOG(1, "L:%d fun:%s dst:0x%08x src:0x%08x sz:%d\n", __LINE__, __FUNCTION__, dst, src, sz);
  93. memcpy(dst, src, sz);
  94. }
  95. static int _rtthread_memcmp(void *dst, void *src, u32 sz)
  96. {
  97. //under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0
  98. DEBUG_LOG(1, "L:%d fun:%s dst:0x%08x src:0x%08x sz:%d\n", __LINE__, __FUNCTION__, dst, src, sz);
  99. if (!(memcmp(dst, src, sz)))
  100. return 1;
  101. return 0;
  102. }
  103. static void _rtthread_memset(void *pbuf, int c, u32 sz)
  104. {
  105. DEBUG_LOG(1, "L:%d fun:%s buf:0x%08x c:%c sz:%d\n", __LINE__, __FUNCTION__, pbuf, c, sz);
  106. memset(pbuf, c, sz);
  107. }
  108. static void _rtthread_init_sema(_sema *sema, int init_val)
  109. {
  110. char name[RT_NAME_MAX];
  111. static int ameba_sem = 0;
  112. DEBUG_LOG(3, "L:%d fun:%s begin val:%d\n", __LINE__, __FUNCTION__, init_val);
  113. memset(name, 0, RT_NAME_MAX);
  114. snprintf(name, RT_NAME_MAX, "sem-%03d", ameba_sem);
  115. *sema = rt_sem_create(name, init_val, RT_IPC_FLAG_FIFO);
  116. if (*sema != RT_NULL)
  117. ameba_sem ++;
  118. DEBUG_LOG(3, "L:%d fun:%s end sema:0x%08x num:%d\n", __LINE__, __FUNCTION__, *sema, ameba_sem);
  119. }
  120. static void _rtthread_free_sema(_sema *sema)
  121. {
  122. RT_ASSERT(*sema != RT_NULL);
  123. DEBUG_LOG(2, "L:%d fun:%s sema:0x%08x\n", __LINE__, __FUNCTION__, *sema);
  124. rt_sem_delete(*sema);
  125. *sema = RT_NULL;
  126. }
  127. static void _rtthread_up_sema(_sema *sema)
  128. {
  129. if (*sema == RT_NULL)
  130. {
  131. rt_kprintf("err!! up sema is NULL\n");
  132. return;
  133. }
  134. DEBUG_LOG(2, "L:%d fun:%s sema:0x%08x\n", __LINE__, __FUNCTION__, *sema);
  135. rt_sem_release(*sema);
  136. }
  137. static void _rtthread_up_sema_from_isr(_sema *sema)
  138. {
  139. if (*sema == RT_NULL)
  140. {
  141. rt_kprintf("err!! up sema from isr is NULL\n");
  142. return;
  143. }
  144. DEBUG_LOG(2, "L:%d fun:%s sema:0x%08x\n", __LINE__, __FUNCTION__, *sema);
  145. rt_sem_release(*sema);
  146. }
  147. static u32 _rtthread_down_sema(_sema *sema, u32 timeout)
  148. {
  149. rt_int32_t tick;
  150. RT_ASSERT(*sema != RT_NULL);
  151. DEBUG_LOG(2, "L:%d fun:%s sema:0x%08x timeout:%d\n", __LINE__, __FUNCTION__, *sema, timeout);
  152. if(timeout >= RT_TICK_MAX / 2)
  153. tick = RT_WAITING_FOREVER;
  154. else
  155. tick = rtw_ms_to_systime(timeout);
  156. if(rt_sem_take(*sema, tick) != RT_EOK)
  157. return RT_FALSE;
  158. return RT_TRUE;
  159. }
  160. static void _rtthread_mutex_init(_mutex *pmutex)
  161. {
  162. char name[RT_NAME_MAX];
  163. static int ameba_mutex = 0;
  164. DEBUG_LOG(3, "L:%d fun:%s begin\n", __LINE__, __FUNCTION__);
  165. memset(name, 0, RT_NAME_MAX);
  166. snprintf(name, RT_NAME_MAX, "mux-%03d", ameba_mutex);
  167. *pmutex = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  168. if (*pmutex != RT_NULL)
  169. ameba_mutex ++;
  170. DEBUG_LOG(3, "L:%d fun:%s end pmutex:0x%08x\n", __LINE__, __FUNCTION__, *pmutex);
  171. }
  172. static void _rtthread_mutex_free(_mutex *pmutex)
  173. {
  174. RT_ASSERT(*pmutex != RT_NULL);
  175. DEBUG_LOG(2, "L:%d fun:%s pmutex:0x%08x\n", __LINE__, __FUNCTION__, *pmutex);
  176. rt_mutex_delete(*pmutex);
  177. *pmutex = RT_NULL;
  178. }
  179. static void _rtthread_mutex_get(_lock *plock)
  180. {
  181. RT_ASSERT(*plock != RT_NULL);
  182. DEBUG_LOG(2, "L:%d fun:%s pmutex:0x%08x\n", __LINE__, __FUNCTION__, *plock);
  183. rt_mutex_take(*plock, RT_WAITING_FOREVER);
  184. }
  185. static int _rtthread_mutex_get_timeout(_lock *plock, u32 timeout_ms)
  186. {
  187. rt_int32_t tick;
  188. RT_ASSERT(*plock != RT_NULL);
  189. DEBUG_LOG(2, "L:%d fun:%s plock:0x%08x timeout_ms:%d\n", __LINE__, __FUNCTION__, *plock, timeout_ms);
  190. if(timeout_ms >= RT_TICK_MAX / 2)
  191. tick = RT_WAITING_FOREVER;
  192. else
  193. tick = rtw_ms_to_systime(timeout_ms);
  194. return rt_mutex_take(*plock, tick);
  195. }
  196. static void _rtthread_mutex_put(_lock *plock)
  197. {
  198. if (*plock == RT_NULL)
  199. {
  200. rt_kprintf("err!! mutex put is null\n");
  201. return;
  202. }
  203. DEBUG_LOG(2, "L:%d fun:%s pmutex:0x%08x\n", __LINE__, __FUNCTION__, *plock);
  204. rt_mutex_release(*plock);
  205. }
  206. static void _rtthread_enter_critical(_lock *plock, _irqL *pirqL)
  207. {
  208. DEBUG_LOG(1, "L:%d fun:%s *pirqL:0x%08x\n", __LINE__, __FUNCTION__, *pirqL);
  209. rt_enter_critical();
  210. }
  211. static void _rtthread_exit_critical(_lock *plock, _irqL *pirqL)
  212. {
  213. DEBUG_LOG(1, "L:%d fun:%s *pirqL:0x%08x\n", __LINE__, __FUNCTION__, *pirqL);
  214. rt_exit_critical();
  215. }
  216. // static rt_base_t level;
  217. static void _rtthread_enter_critical_from_isr(_lock *plock, _irqL *pirqL)
  218. {
  219. DEBUG_LOG(1, "L:%d fun:%s *pirqL:0x%08x\n", __LINE__, __FUNCTION__, *pirqL);
  220. *pirqL = rt_hw_interrupt_disable();
  221. }
  222. static void _rtthread_exit_critical_from_isr(_lock *plock, _irqL *pirqL)
  223. {
  224. DEBUG_LOG(1, "L:%d fun:%s *pirqL:0x%08x\n", __LINE__, __FUNCTION__, *pirqL);
  225. rt_hw_interrupt_enable(*pirqL);
  226. }
  227. static int _rtthread_enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
  228. {
  229. RT_ASSERT(*pmutex != RT_NULL);
  230. DEBUG_LOG(1, "L:%d fun:%s *pirqL:0x%08x\n", __LINE__, __FUNCTION__, *pirqL);
  231. if(rt_mutex_take(*pmutex, RT_WAITING_FOREVER) != RT_EOK)
  232. return RT_FALSE;
  233. return RT_TRUE;
  234. }
  235. static void _rtthread_exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
  236. {
  237. if (*pmutex == RT_NULL)
  238. {
  239. rt_kprintf("err!! critical mutex is null\n");
  240. return;
  241. }
  242. DEBUG_LOG(1, "L:%d fun:%s *pirqL:0x%08x\n", __LINE__, __FUNCTION__, *pirqL);
  243. rt_mutex_release(*pmutex);
  244. }
  245. static void _rtthread_spinlock_init(_lock *plock)
  246. {
  247. #if USE_MUTEX_FOR_SPINLOCK
  248. char name[RT_NAME_MAX];
  249. static int ameba_spin = 0;
  250. DEBUG_LOG(3, "L:%d fun:%s begin\n", __LINE__, __FUNCTION__);
  251. memset(name, 0, RT_NAME_MAX);
  252. snprintf(name, RT_NAME_MAX, "spn-03d", ameba_spin);
  253. *plock = rt_mutex_create(name, RT_IPC_FLAG_FIFO);
  254. if (*plock != RT_NULL)
  255. ameba_spin ++;
  256. DEBUG_LOG(3, "L:%d fun:%s end plock:0x%08x ameba_spin:%d\n", __LINE__, __FUNCTION__, *plock, ameba_spin);
  257. #endif
  258. }
  259. static void _rtthread_spinlock_free(_lock *plock)
  260. {
  261. #if USE_MUTEX_FOR_SPINLOCK
  262. // RT_ASSERT(*plock != RT_NULL);
  263. if (*plock == RT_NULL)
  264. return;
  265. DEBUG_LOG(2, "L:%d fun:%s plock:0x%08x\n", __LINE__, __FUNCTION__, *plock);
  266. rt_mutex_delete(*plock);
  267. *plock = NULL;
  268. #endif
  269. }
  270. static void _rtthread_spinlock(_lock *plock)
  271. {
  272. #if USE_MUTEX_FOR_SPINLOCK
  273. RT_ASSERT(*plock != RT_NULL);
  274. DEBUG_LOG(2, "L:%d fun:%s plock:0x%08x\n", __LINE__, __FUNCTION__, *plock);
  275. rt_mutex_take(*plock, RT_WAITING_FOREVER);
  276. #endif
  277. }
  278. static void _rtthread_spinunlock(_lock *plock)
  279. {
  280. #if USE_MUTEX_FOR_SPINLOCK
  281. if (*plock == RT_NULL)
  282. {
  283. rt_kprintf("err!! spinunlock is null\n");
  284. return;
  285. }
  286. DEBUG_LOG(2, "L:%d fun:%s plock:0x%08x\n", __LINE__, __FUNCTION__, *plock);
  287. rt_mutex_release(*plock);
  288. #endif
  289. }
  290. static void _rtthread_spinlock_irqsave(_lock *plock, _irqL *irqL)
  291. {
  292. #if USE_MUTEX_FOR_SPINLOCK
  293. if (*plock == RT_NULL)
  294. rt_kprintf("err!! spinlock irqsave null\n");
  295. DEBUG_LOG(2, "L:%d fun:%s plock:0x%08x irqL:0x%08x\n", __LINE__, __FUNCTION__, *plock, *irqL);
  296. *irqL = 0xdeadbeff;
  297. while (rt_mutex_take(*plock, 0) != RT_EOK)
  298. {
  299. rt_kprintf("spinlock_irqsave failed!\n");
  300. }
  301. #endif
  302. }
  303. static void _rtthread_spinunlock_irqsave(_lock *plock, _irqL *irqL)
  304. {
  305. #if USE_MUTEX_FOR_SPINLOCK
  306. if (*plock == RT_NULL)
  307. rt_kprintf("err!! spinunlock irqsave null\n");
  308. DEBUG_LOG(2, "L:%d fun:%s plock:0x%08x irqL:0x%08x\n", __LINE__, __FUNCTION__, *plock, *irqL);
  309. rt_mutex_release(*plock);
  310. #endif
  311. }
  312. static int _rtthread_init_xqueue( _xqueue* queue, const char* name, u32 message_size, u32 number_of_messages )
  313. {
  314. DEBUG_LOG(3, "L:%d fun:%s begin name:%s size:%d msgs:%d\n", __LINE__, __FUNCTION__, name, message_size, number_of_messages);
  315. *queue = rt_mq_create(name, message_size, number_of_messages, RT_IPC_FLAG_FIFO);
  316. if (*queue == RT_NULL)
  317. {
  318. rt_kprintf("err!! create xqueue fail\n");
  319. return -1;
  320. }
  321. DEBUG_LOG(3, "L:%d fun:%s end queue:0x%08x\n", __LINE__, __FUNCTION__, *queue);
  322. return 0;
  323. }
  324. static int _rtthread_push_to_xqueue( _xqueue* queue, void* message, u32 timeout_ms )
  325. {
  326. RT_ASSERT(*queue != RT_NULL);
  327. DEBUG_LOG(2, "L:%d fun:%s queue:0x%08x timeout_ms:%d\n", __LINE__, __FUNCTION__, *queue, timeout_ms);
  328. if (rt_mq_send(*queue, message, ((rt_mq_t)*queue)->msg_size) != RT_EOK)
  329. {
  330. return -1;
  331. }
  332. return 0;
  333. }
  334. static int _rtthread_pop_from_xqueue( _xqueue* queue, void* message, u32 timeout_ms )
  335. {
  336. rt_uint32_t tick;
  337. RT_ASSERT(*queue != RT_NULL);
  338. DEBUG_LOG(2, "L:%d fun:%s queue:0x%08x msg:0x%08x timeout_ms:%d\n", __LINE__, __FUNCTION__, *queue, message, timeout_ms);
  339. if(timeout_ms >= RT_TICK_MAX / 2)
  340. tick = RT_WAITING_FOREVER;
  341. else
  342. tick = rtw_ms_to_systime(timeout_ms);
  343. if (rt_mq_recv(*queue, message, ((rt_mq_t)*queue)->msg_size, tick) != RT_EOK)
  344. {
  345. return -1;
  346. }
  347. return 0;
  348. }
  349. static int _rtthread_deinit_xqueue( _xqueue* queue )
  350. {
  351. RT_ASSERT(*queue != RT_NULL);
  352. DEBUG_LOG(2, "L:%d fun:%s queue:0x%08x\n", __LINE__, __FUNCTION__, *queue);
  353. rt_mq_delete(*queue);
  354. return 0;
  355. }
  356. static u32 _rtthread_get_current_time(void)
  357. {
  358. return (u32)rt_tick_get();
  359. }
  360. static u32 _rtthread_systime_to_ms(u32 systime)
  361. {
  362. return systime * 1000 / RT_TICK_PER_SECOND;
  363. }
  364. static u32 _rtthread_systime_to_sec(u32 systime)
  365. {
  366. return systime / RT_TICK_PER_SECOND;
  367. }
  368. static u32 _rtthread_ms_to_systime(u32 ms)
  369. {
  370. return rt_tick_from_millisecond(ms);
  371. }
  372. static u32 _rtthread_sec_to_systime(u32 sec)
  373. {
  374. return sec * RT_TICK_PER_SECOND;
  375. }
  376. static void _rtthread_msleep_os(int ms)
  377. {
  378. DEBUG_LOG(2, "L:%d fun:%s ms:%d\n", __LINE__, __FUNCTION__, ms);
  379. #if defined(CONFIG_PLATFORM_8195A)
  380. rt_thread_delay(rt_tick_from_millisecond(ms));
  381. #elif defined(CONFIG_PLATFORM_8711B)
  382. if (pmu_yield_os_check())
  383. rt_thread_delay(rt_tick_from_millisecond(ms));
  384. else
  385. DelayMs(ms);
  386. #endif
  387. }
  388. static void _rtthread_usleep_os(int us)
  389. {
  390. #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F10X_XL)
  391. // rtthread does not provide us level delay. Use busy wait
  392. WLAN_BSP_UsLoop(us);
  393. #elif defined(CONFIG_PLATFORM_8195A)
  394. //DBG_ERR("%s: Please Implement micro-second delay\n", __FUNCTION__);
  395. #elif defined(CONFIG_PLATFORM_8711B)
  396. DelayUs(us);
  397. #endif
  398. }
  399. static void _rtthread_mdelay_os(int ms)
  400. {
  401. rt_thread_delay(rt_tick_from_millisecond(ms));
  402. }
  403. static void _rtthread_udelay_os(int us)
  404. {
  405. #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F10X_XL)
  406. // rtthread does not provide us level delay. Use busy wait
  407. WLAN_BSP_UsLoop(us);
  408. #elif defined(CONFIG_PLATFORM_8195A)
  409. HalDelayUs(us);
  410. #elif defined(CONFIG_PLATFORM_8711B)
  411. DelayUs(us);
  412. #else
  413. #error "Please implement hardware dependent micro second level sleep here"
  414. #endif
  415. }
  416. static void _rtthread_yield_os(void)
  417. {
  418. #if defined(CONFIG_PLATFORM_8195A)
  419. rt_thread_yield();
  420. #elif defined(CONFIG_PLATFORM_8711B)
  421. if (pmu_yield_os_check())
  422. rt_thread_yield();
  423. else
  424. DelayMs(1);
  425. #endif
  426. }
  427. static void _rtthread_ATOMIC_SET(ATOMIC_T *v, int i)
  428. {
  429. atomic_set(v,i);
  430. }
  431. static int _rtthread_ATOMIC_READ(ATOMIC_T *v)
  432. {
  433. return atomic_read(v);
  434. }
  435. static void _rtthread_ATOMIC_ADD(ATOMIC_T *v, int i)
  436. {
  437. save_and_cli();
  438. v->counter += i;
  439. restore_flags();
  440. }
  441. static void _rtthread_ATOMIC_SUB(ATOMIC_T *v, int i)
  442. {
  443. save_and_cli();
  444. v->counter -= i;
  445. restore_flags();
  446. }
  447. static void _rtthread_ATOMIC_INC(ATOMIC_T *v)
  448. {
  449. _rtthread_ATOMIC_ADD(v, 1);
  450. }
  451. static void _rtthread_ATOMIC_DEC(ATOMIC_T *v)
  452. {
  453. _rtthread_ATOMIC_SUB(v, 1);
  454. }
  455. static int _rtthread_ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
  456. {
  457. int temp;
  458. save_and_cli();
  459. temp = v->counter;
  460. temp += i;
  461. v->counter = temp;
  462. restore_flags();
  463. return temp;
  464. }
  465. static int _rtthread_ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
  466. {
  467. int temp;
  468. save_and_cli();
  469. temp = v->counter;
  470. temp -= i;
  471. v->counter = temp;
  472. restore_flags();
  473. return temp;
  474. }
  475. static int _rtthread_ATOMIC_INC_RETURN(ATOMIC_T *v)
  476. {
  477. return _rtthread_ATOMIC_ADD_RETURN(v, 1);
  478. }
  479. static int _rtthread_ATOMIC_DEC_RETURN(ATOMIC_T *v)
  480. {
  481. return _rtthread_ATOMIC_SUB_RETURN(v, 1);
  482. }
  483. static u64 _rtthread_modular64(u64 n, u64 base)
  484. {
  485. unsigned int __base = (base);
  486. unsigned int __rem;
  487. if (((n) >> 32) == 0) {
  488. __rem = (unsigned int)(n) % __base;
  489. (n) = (unsigned int)(n) / __base;
  490. }
  491. else
  492. __rem = __div64_32(&(n), __base);
  493. return __rem;
  494. }
  495. /* Refer to ecos bsd tcpip codes */
  496. static int _rtthread_arc4random(void)
  497. {
  498. u32 res = _rtthread_get_current_time();
  499. static unsigned long seed = 0xDEADB00B;
  500. #if CONFIG_PLATFORM_8711B
  501. if(random_seed){
  502. seed = random_seed;
  503. random_seed = 0;
  504. }
  505. #endif
  506. seed = ((seed & 0x007F00FF) << 7) ^
  507. ((seed & 0x0F80FF00) >> 8) ^ // be sure to stir those low bits
  508. (res << 13) ^ (res >> 9); // using the clock too!
  509. return (int)seed;
  510. }
  511. static int _rtthread_get_random_bytes(void *buf, u32 len)
  512. {
  513. #if 1 //becuase of 4-byte align, we use the follow code style.
  514. unsigned int ranbuf;
  515. unsigned int *lp;
  516. int i, count;
  517. count = len / sizeof(unsigned int);
  518. lp = (unsigned int *) buf;
  519. for(i = 0; i < count; i ++) {
  520. lp[i] = _rtthread_arc4random();
  521. len -= sizeof(unsigned int);
  522. }
  523. if(len > 0) {
  524. ranbuf = _rtthread_arc4random();
  525. _rtthread_memcpy(&lp[i], &ranbuf, len);
  526. }
  527. return 0;
  528. #else
  529. unsigned long ranbuf, *lp;
  530. lp = (unsigned long *)buf;
  531. while (len > 0) {
  532. ranbuf = _rtthread_arc4random();
  533. *lp++ = ranbuf; //this op need the pointer is 4Byte-align!
  534. len -= sizeof(ranbuf);
  535. }
  536. return 0;
  537. #endif
  538. }
  539. static u32 _rtthread_GetFreeHeapSize(void)
  540. {
  541. return 16 * 1024;
  542. }
  543. #ifndef RT_USING_LWIP
  544. #define RT_LWIP_TCPTHREAD_PRIORITY 10
  545. #endif
  546. static int _rtthread_create_task(struct task_struct *ptask, const char *name,
  547. u32 stack_size, u32 priority, thread_func_t func, void *thctx)
  548. {
  549. rt_thread_t tid;
  550. DEBUG_LOG(3, "L:%d fun:%s begin name:%s stack_size:%d priority:%d func:0x%08x thctx:0x%08x\n", \
  551. __LINE__, __FUNCTION__, name, stack_size, priority, func, thctx);
  552. rt_memset(ptask, 0, sizeof(struct task_struct));
  553. ptask->task_name = name;
  554. ptask->blocked = 0;
  555. ptask->callback_running = 0;
  556. _rtthread_init_sema(&ptask->wakeup_sema, 0);
  557. if (ptask->wakeup_sema == RT_NULL)
  558. {
  559. rt_kprintf("L:%d create wakeup sem fail\n");
  560. goto _thread_err;
  561. }
  562. _rtthread_init_sema(&ptask->terminate_sema, 0);
  563. if (ptask->terminate_sema == RT_NULL)
  564. {
  565. rt_kprintf("L:%d terminate sem fail\n");
  566. goto _thread_err;
  567. }
  568. stack_size = (stack_size * 3);
  569. priority = RT_LWIP_TCPTHREAD_PRIORITY + priority;
  570. if(priority >= RT_THREAD_PRIORITY_MAX)
  571. priority = RT_THREAD_PRIORITY_MAX - 1;
  572. tid = rt_thread_create(name, func, thctx, stack_size, priority, 10);
  573. if (tid == RT_NULL)
  574. {
  575. rt_kprintf("L:%d thread create fail\n");
  576. goto _thread_err;
  577. }
  578. ptask->task = tid;
  579. rt_thread_startup(tid);
  580. DEBUG_LOG(3, "L:%d fun:%s end stack_size:%d priority:%d wakeup:0x%08x terminate:0x%08x\n", \
  581. __LINE__, __FUNCTION__, stack_size, priority, ptask->wakeup_sema, ptask->terminate_sema);
  582. return RT_TRUE;
  583. _thread_err:
  584. if (ptask->wakeup_sema)
  585. _rtthread_free_sema(&ptask->wakeup_sema);
  586. if (ptask->terminate_sema)
  587. _rtthread_free_sema(&ptask->terminate_sema);
  588. rt_memset(ptask, 0, sizeof(struct task_struct));
  589. return RT_FALSE;
  590. }
  591. static void _rtthread_delete_task(struct task_struct *ptask)
  592. {
  593. if (!ptask->task)
  594. {
  595. rt_kprintf("_rtthread_delete_task(): ptask is NULL!\n");
  596. return;
  597. }
  598. DEBUG_LOG(2, "L:%d fun:%s name:%s\n", __LINE__, __FUNCTION__, ptask->task_name);
  599. ptask->blocked = 1;
  600. _rtthread_up_sema(&ptask->wakeup_sema);
  601. _rtthread_down_sema(&ptask->terminate_sema, TIMER_MAX_DELAY);
  602. _rtthread_free_sema(&ptask->wakeup_sema);
  603. _rtthread_free_sema(&ptask->terminate_sema);
  604. ptask->task = 0;
  605. }
  606. void _rtthread_wakeup_task(struct task_struct *ptask)
  607. {
  608. DEBUG_LOG(2, "L:%d fun:%s name:%s\n", __LINE__, __FUNCTION__, ptask->task_name);
  609. _rtthread_up_sema(&ptask->wakeup_sema);
  610. }
  611. static void _rtthread_thread_enter(char *name)
  612. {
  613. DEBUG_LOG(3, "L:%d fun:%s name:%s\n", __LINE__, __FUNCTION__, name);
  614. }
  615. static void _rtthread_thread_exit(void)
  616. {
  617. DEBUG_LOG(3, "L:%d fun:%s\n", __LINE__, __FUNCTION__);
  618. }
  619. _timerHandle _rtthread_timerCreate( const signed char *pcTimerName,
  620. osdepTickType xTimerPeriodInTicks,
  621. u32 uxAutoReload,
  622. void * pvTimerID,
  623. TIMER_FUN pxCallbackFunction )
  624. {
  625. rt_timer_t timer;
  626. rt_tick_t time;
  627. rt_uint8_t flag;
  628. DEBUG_LOG(3, "L:%d fun:%s begin name:%s reload:%d tick:%d fun:0x%08x ID:0x%08x\n",
  629. __LINE__, __FUNCTION__, pcTimerName, uxAutoReload, xTimerPeriodInTicks, pxCallbackFunction, pvTimerID);
  630. if(uxAutoReload == 1)
  631. flag = (RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_PERIODIC);
  632. else
  633. flag = (RT_TIMER_FLAG_SOFT_TIMER | RT_TIMER_FLAG_ONE_SHOT);
  634. if( xTimerPeriodInTicks >= (RT_TICK_MAX / 2) )
  635. time = (RT_TICK_MAX / 2) - 1;
  636. else
  637. time = xTimerPeriodInTicks;
  638. timer = rt_timer_create(pcTimerName, pxCallbackFunction, RT_NULL, time, flag);
  639. if(timer == RT_NULL)
  640. {
  641. rt_kprintf("timer create fail\n");
  642. return RT_NULL;
  643. }
  644. timer->parameter = timer;
  645. DEBUG_LOG(3, "L:%d fun:%s end timer:0x%08x\n", __LINE__, __FUNCTION__, timer);
  646. return (_timerHandle)timer;
  647. }
  648. u32 _rtthread_timerDelete(_timerHandle xTimer, osdepTickType xBlockTime)
  649. {
  650. DEBUG_LOG(3, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  651. RT_ASSERT(xTimer != RT_NULL);
  652. rt_timer_delete(xTimer);
  653. return RT_TRUE;
  654. }
  655. u32 _rtthread_timerIsTimerActive( _timerHandle xTimer )
  656. {
  657. rt_timer_t pxTimer = (rt_timer_t)xTimer;
  658. DEBUG_LOG(3, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  659. if(!xTimer)
  660. {
  661. rt_kprintf("err!! timer is active null\n");
  662. return 0;
  663. }
  664. return (pxTimer->parent.flag & RT_TIMER_FLAG_ACTIVATED) ? 1 : 0;
  665. }
  666. u32 _rtthread_timerStop(_timerHandle xTimer, osdepTickType xBlockTime)
  667. {
  668. RT_ASSERT(xTimer != RT_NULL);
  669. DEBUG_LOG(3, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  670. if (rt_timer_stop(xTimer) != RT_EOK)
  671. {
  672. // rt_kprintf("timer stop fail\n");
  673. return RT_FALSE;
  674. }
  675. return RT_TRUE;
  676. }
  677. u32 _rtthread_timerChangePeriod( _timerHandle xTimer, osdepTickType xNewPeriod, osdepTickType xBlockTime )
  678. {
  679. int time;
  680. RT_ASSERT(xTimer != RT_NULL);
  681. DEBUG_LOG(3, "L:%d fun:%s timer:0x%08x new_tick:%d\n", __LINE__, __FUNCTION__, xTimer, xNewPeriod);
  682. if(xNewPeriod == 0)
  683. time = 1;
  684. else if(xNewPeriod >= (RT_TICK_MAX / 2))
  685. time = (RT_TICK_MAX / 2) - 1;
  686. else
  687. time = xNewPeriod;
  688. rt_timer_stop(xTimer);
  689. rt_timer_control(xTimer, RT_TIMER_CTRL_SET_TIME, (void *)&time);
  690. if(rt_timer_start(xTimer) != RT_EOK)
  691. {
  692. rt_kprintf("change time and timer start fail\n");
  693. return RT_FALSE;
  694. }
  695. return RT_TRUE;
  696. }
  697. void *_rtthread_timerGetID( _timerHandle xTimer )
  698. {
  699. return xTimer;
  700. }
  701. u32 _rtthread_timerStart( _timerHandle xTimer, osdepTickType xBlockTime )
  702. {
  703. RT_ASSERT(xTimer != RT_NULL);
  704. DEBUG_LOG(3, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  705. if(rt_timer_start(xTimer) != RT_EOK)
  706. {
  707. rt_kprintf("change time and timer start fail\n");
  708. return RT_FALSE;
  709. }
  710. return RT_TRUE;
  711. }
  712. u32 _rtthread_timerStartFromISR( _timerHandle xTimer, osdepBASE_TYPE *pxHigherPriorityTaskWoken )
  713. {
  714. DEBUG_LOG(1, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  715. if (xTimer == RT_NULL)
  716. {
  717. rt_kprintf("timer start from isr null\n");
  718. return RT_FALSE;
  719. }
  720. if(rt_timer_start(xTimer) != RT_EOK)
  721. {
  722. rt_kprintf("timer start from isr fail\n");
  723. return RT_FALSE;
  724. }
  725. return RT_TRUE;
  726. }
  727. u32 _rtthread_timerStopFromISR( _timerHandle xTimer, osdepBASE_TYPE *pxHigherPriorityTaskWoken )
  728. {
  729. DEBUG_LOG(1, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  730. if (xTimer == RT_NULL)
  731. {
  732. rt_kprintf("timer start from isr null\n");
  733. return RT_FALSE;
  734. }
  735. if(rt_timer_stop(xTimer) != RT_EOK)
  736. {
  737. rt_kprintf("timer stop from isr fail\n");
  738. return RT_FALSE;
  739. }
  740. return RT_TRUE;
  741. }
  742. u32 _rtthread_timerResetFromISR( _timerHandle xTimer, osdepBASE_TYPE *pxHigherPriorityTaskWoken )
  743. {
  744. DEBUG_LOG(1, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  745. if (xTimer == RT_NULL)
  746. {
  747. rt_kprintf("timer Reset from isr null\n");
  748. return RT_FALSE;
  749. }
  750. if(rt_timer_stop(xTimer) != RT_EOK)
  751. {
  752. rt_kprintf("timer Reset from isr stop fail\n");
  753. return RT_FALSE;
  754. }
  755. if(rt_timer_start(xTimer) != RT_EOK)
  756. {
  757. rt_kprintf("timer Reset from isr start fail\n");
  758. return RT_FALSE;
  759. }
  760. return RT_TRUE;
  761. }
  762. u32 _rtthread_timerChangePeriodFromISR( _timerHandle xTimer,
  763. osdepTickType xNewPeriod,
  764. osdepBASE_TYPE *pxHigherPriorityTaskWoken )
  765. {
  766. int time;
  767. DEBUG_LOG(1, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  768. if (xTimer == RT_NULL)
  769. {
  770. rt_kprintf("timer Change Period from isr null\n");
  771. return RT_FALSE;
  772. }
  773. if(xNewPeriod == 0)
  774. time = 1;
  775. else if(xNewPeriod >= (RT_TICK_MAX / 2))
  776. time = (RT_TICK_MAX / 2) - 1;
  777. else
  778. time = xNewPeriod;
  779. rt_timer_stop(xTimer);
  780. rt_timer_control(xTimer, RT_TIMER_CTRL_SET_TIME, (void *)&time);
  781. if(rt_timer_start(xTimer) != RT_EOK)
  782. {
  783. rt_kprintf("timer Change Period from isr start fail\n");
  784. return RT_FALSE;
  785. }
  786. return RT_TRUE;
  787. }
  788. u32 _rtthread_timerReset( _timerHandle xTimer,
  789. osdepTickType xBlockTime )
  790. {
  791. RT_ASSERT(xTimer != RT_NULL);
  792. DEBUG_LOG(2, "L:%d fun:%s timer:0x%08x\n", __LINE__, __FUNCTION__, xTimer);
  793. rt_timer_stop(xTimer);
  794. if(rt_timer_start(xTimer) != RT_EOK)
  795. {
  796. rt_kprintf("timer reset fail\n");
  797. return RT_FALSE;
  798. }
  799. return RT_TRUE;
  800. }
  801. void _rtthread_acquire_wakelock(void)
  802. {
  803. #if defined(CONFIG_PLATFORM_8195A)
  804. #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
  805. pmu_acquire_wakelock(PMU_WLAN_DEVICE);
  806. #endif
  807. #elif defined(CONFIG_PLATFORM_8711B)
  808. #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
  809. if (pmu_yield_os_check())
  810. pmu_acquire_wakelock(PMU_WLAN_DEVICE);
  811. #endif
  812. #endif
  813. }
  814. void _rtthread_release_wakelock(void)
  815. {
  816. #if defined(CONFIG_PLATFORM_8195A)
  817. #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
  818. pmu_release_wakelock(PMU_WLAN_DEVICE);
  819. #endif
  820. #elif defined(CONFIG_PLATFORM_8711B)
  821. #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
  822. if (pmu_yield_os_check())
  823. pmu_release_wakelock(PMU_WLAN_DEVICE);
  824. #endif
  825. #endif
  826. }
  827. void _rtthread_wakelock_timeout(uint32_t timeout)
  828. {
  829. #if defined(CONFIG_PLATFORM_8195A)
  830. #elif defined(CONFIG_PLATFORM_8711B)
  831. if (pmu_yield_os_check())
  832. pmu_set_sysactive_time(/*PMU_WLAN_DEVICE, */timeout);
  833. else
  834. printf("can't aquire wake during suspend flow!!\n");
  835. #endif
  836. }
  837. u8 _rtthread_get_scheduler_state(void)
  838. {
  839. rt_thread_t thread = rt_thread_self();
  840. u8 state = thread->stat;
  841. switch(state){
  842. case RT_THREAD_INIT: state = OS_SCHEDULER_NOT_STARTED; break;
  843. case RT_THREAD_RUNNING: state = OS_SCHEDULER_RUNNING; break;
  844. case RT_THREAD_SUSPEND: state = OS_SCHEDULER_SUSPENDED; break;
  845. }
  846. rt_kprintf("[func]:%s [line]:%d state:%d\n", __FUNCTION__, __LINE__, state);
  847. return state;
  848. }
  849. const struct osdep_service_ops osdep_service = {
  850. _rtthread_malloc, //rtw_vmalloc
  851. _rtthread_zmalloc, //rtw_zvmalloc
  852. _rtthread_mfree, //rtw_vmfree
  853. _rtthread_malloc, //rtw_malloc
  854. _rtthread_zmalloc, //rtw_zmalloc
  855. _rtthread_mfree, //rtw_mfree
  856. _rtthread_memcpy, //rtw_memcpy
  857. _rtthread_memcmp, //rtw_memcmp
  858. _rtthread_memset, //rtw_memset
  859. _rtthread_init_sema, //rtw_init_sema
  860. _rtthread_free_sema, //rtw_free_sema
  861. _rtthread_up_sema, //rtw_up_sema
  862. _rtthread_up_sema_from_isr, //rtw_up_sema_from_isr
  863. _rtthread_down_sema, //rtw_down_sema
  864. _rtthread_mutex_init, //rtw_mutex_init
  865. _rtthread_mutex_free, //rtw_mutex_free
  866. _rtthread_mutex_get, //rtw_mutex_get
  867. _rtthread_mutex_get_timeout,//rtw_mutex_get_timeout
  868. _rtthread_mutex_put, //rtw_mutex_put
  869. _rtthread_enter_critical, //rtw_enter_critical
  870. _rtthread_exit_critical, //rtw_exit_critical
  871. _rtthread_enter_critical_from_isr, //rtw_enter_critical_from_isr
  872. _rtthread_exit_critical_from_isr, //rtw_exit_critical_from_isr
  873. NULL, //rtw_enter_critical_bh
  874. NULL, //rtw_exit_critical_bh
  875. _rtthread_enter_critical_mutex, //rtw_enter_critical_mutex
  876. _rtthread_exit_critical_mutex, //rtw_exit_critical_mutex
  877. _rtthread_spinlock_init, //rtw_spinlock_init
  878. _rtthread_spinlock_free, //rtw_spinlock_free
  879. _rtthread_spinlock, //rtw_spin_lock
  880. _rtthread_spinunlock, //rtw_spin_unlock
  881. _rtthread_spinlock_irqsave, //rtw_spinlock_irqsave
  882. _rtthread_spinunlock_irqsave, //rtw_spinunlock_irqsave
  883. _rtthread_init_xqueue, //rtw_init_xqueue
  884. _rtthread_push_to_xqueue, //rtw_push_to_xqueue
  885. _rtthread_pop_from_xqueue, //rtw_pop_from_xqueue
  886. _rtthread_deinit_xqueue, //rtw_deinit_xqueue
  887. _rtthread_get_current_time, //rtw_get_current_time
  888. _rtthread_systime_to_ms, //rtw_systime_to_ms
  889. _rtthread_systime_to_sec, //rtw_systime_to_sec
  890. _rtthread_ms_to_systime, //rtw_ms_to_systime
  891. _rtthread_sec_to_systime, //rtw_sec_to_systime
  892. _rtthread_msleep_os, //rtw_msleep_os
  893. _rtthread_usleep_os, //rtw_usleep_os
  894. _rtthread_mdelay_os, //rtw_mdelay_os
  895. _rtthread_udelay_os, //rtw_udelay_os
  896. _rtthread_yield_os, //rtw_yield_os
  897. _rtthread_ATOMIC_SET, //ATOMIC_SET
  898. _rtthread_ATOMIC_READ, //ATOMIC_READ
  899. _rtthread_ATOMIC_ADD, //ATOMIC_ADD
  900. _rtthread_ATOMIC_SUB, //ATOMIC_SUB
  901. _rtthread_ATOMIC_INC, //ATOMIC_INC
  902. _rtthread_ATOMIC_DEC, //ATOMIC_DEC
  903. _rtthread_ATOMIC_ADD_RETURN, //ATOMIC_ADD_RETURN
  904. _rtthread_ATOMIC_SUB_RETURN, //ATOMIC_SUB_RETURN
  905. _rtthread_ATOMIC_INC_RETURN, //ATOMIC_INC_RETURN
  906. _rtthread_ATOMIC_DEC_RETURN, //ATOMIC_DEC_RETURN
  907. _rtthread_modular64, //rtw_modular64
  908. _rtthread_get_random_bytes, //rtw_get_random_bytes
  909. _rtthread_GetFreeHeapSize, //rtw_getFreeHeapSize
  910. _rtthread_create_task, //rtw_create_task
  911. _rtthread_delete_task, //rtw_delete_task
  912. _rtthread_wakeup_task, //rtw_wakeup_task
  913. _rtthread_thread_enter, //rtw_thread_enter
  914. _rtthread_thread_exit, //rtw_thread_exit
  915. _rtthread_timerCreate, //rtw_timerCreate,
  916. _rtthread_timerDelete, //rtw_timerDelete,
  917. _rtthread_timerIsTimerActive, //rtw_timerIsTimerActive,
  918. _rtthread_timerStop, //rtw_timerStop,
  919. _rtthread_timerChangePeriod, //rtw_timerChangePeriod
  920. _rtthread_timerGetID, //rtw_timerGetID
  921. _rtthread_timerStart, //rtw_timerStart
  922. _rtthread_timerStartFromISR, //rtw_timerStartFromISR
  923. _rtthread_timerStopFromISR, //rtw_timerStopFromISR
  924. _rtthread_timerResetFromISR, //rtw_timerResetFromISR
  925. _rtthread_timerChangePeriodFromISR, //rtw_timerChangePeriodFromISR
  926. _rtthread_timerReset, //rtw_timerReset
  927. _rtthread_acquire_wakelock, //rtw_acquire_wakelock
  928. _rtthread_release_wakelock, //rtw_release_wakelock
  929. _rtthread_wakelock_timeout, //rtw_wakelock_timeout
  930. _rtthread_get_scheduler_state //rtw_get_scheduler_state
  931. };
  932. void vTaskDelay( const rt_uint32_t xTicksToDelay )
  933. {
  934. rt_thread_delay(xTicksToDelay);
  935. }
  936. void *pvPortMalloc( size_t xWantedSize )
  937. {
  938. return rt_malloc(xWantedSize);
  939. }
  940. void vPortFree( void *pv )
  941. {
  942. rt_free(pv);
  943. }
  944. uint32_t xTaskGetTickCount( void )
  945. {
  946. return rt_tick_get();
  947. }