1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 //#include <autoconf.h>
6 #include <osdep_service.h>
7 #include <stdio.h>
8 #include <alios/wrapper.h>
9 #include "aos/kernel.h"
10 #include <aos/errno.h>
11 #include <freertos_pmu.h>
12 #include "k_api.h"
13 /********************* os depended utilities ********************/
14 
15 #ifndef USE_MUTEX_FOR_SPINLOCK
16 #define USE_MUTEX_FOR_SPINLOCK 1
17 #endif
18 
19 static volatile uint32_t ulCriticalNesting = 0;
20 static u32 uxSavedInterruptStatus = 0;
21 //----- ------------------------------------------------------------------
22 // Misc Function
23 //----- ------------------------------------------------------------------
24 #if 0
25 
26 void save_and_cli()
27 {
28     RHINO_CRITICAL_ENTER();
29 }
30 
31 void restore_flags()
32 {
33     RHINO_CRITICAL_EXIT();
34 }
35 #endif
36 
aos_interrupt_enter()37 void aos_interrupt_enter()
38 {
39 	krhino_intrpt_enter();
40 }
41 
aos_interrupt_leave()42 void aos_interrupt_leave()
43 {
44 	krhino_intrpt_exit();
45 }
cli()46 void cli()
47 {
48     //RHINO_CPU_INTRPT_DISABLE();
49 }
50 
51 /* Not needed on 64bit architectures */
__div64_32(u64 * n,unsigned int base)52 static unsigned int __div64_32(u64 *n, unsigned int base)
53 {
54 	u64 rem = *n;
55 	u64 b = base;
56 	u64 res, d = 1;
57 	unsigned int high = rem >> 32;
58 
59 	/* Reduce the thing a bit first */
60 	res = 0;
61 	if (high >= base) {
62 		high /= base;
63 		res = (u64) high << 32;
64 		rem -= (u64) (high * base) << 32;
65 	}
66 
67 	while ((u64)b > 0 && b < rem) {
68 		b = b+b;
69 		d = d+d;
70 	}
71 
72 	do {
73 		if (rem >= b) {
74 			rem -= b;
75 			res += d;
76 		}
77 		b >>= 1;
78 		d >>= 1;
79 	} while (d);
80 
81 	*n = res;
82 	return rem;
83 }
84 
85 /********************* os depended service ********************/
86 
_aos_malloc(u32 sz)87 u8* _aos_malloc(u32 sz)
88 {
89     //DBG_8195A("  realsil malloc %d\r\n", sz);
90     return aos_malloc(sz);
91 }
92 
_aos_zmalloc(u32 sz)93 u8* _aos_zmalloc(u32 sz)
94 {
95     u8 *pbuf = _aos_malloc(sz);
96 
97     if (pbuf != NULL)
98         memset(pbuf, 0, sz);
99 
100     return pbuf;
101 }
102 
_aos_mfree(u8 * pbuf,u32 sz)103 void _aos_mfree(u8 *pbuf, u32 sz)
104 {
105     aos_free(pbuf);
106 }
107 
_aos_memcpy(void * dst,void * src,u32 sz)108 static void _aos_memcpy(void* dst, void* src, u32 sz)
109 {
110     _memcpy(dst, src, sz);
111 }
112 
_aos_memcmp(void * dst,void * src,u32 sz)113 static int _aos_memcmp(void *dst, void *src, u32 sz)
114 {
115 //under Linux/GNU/GLibc, the return value of memcmp for two same mem. chunk is 0
116     if (!(_memcmp(dst, src, sz)))
117         return 1;
118 
119     return 0;
120 }
121 
_aos_memset(void * pbuf,int c,u32 sz)122 static void _aos_memset(void *pbuf, int c, u32 sz)
123 {
124     _memset(pbuf, c, sz);
125 }
126 
_aos_init_sema(_sema * sema,int init_val)127 static void _aos_init_sema(_sema *sema, int init_val)
128 {
129     aos_sem_new(sema, init_val);	//Set max count 0xffffffff
130 }
131 
_aos_free_sema(_sema * sema)132 static void _aos_free_sema(_sema *sema)
133 {
134     if(sema != NULL)
135         aos_sem_free(sema);
136 }
137 
_aos_up_sema(_sema * sema)138 static void _aos_up_sema(_sema *sema)
139 {
140     aos_sem_signal(sema);
141 }
142 
_aos_up_sema_from_isr(_sema * sema)143 static void _aos_up_sema_from_isr(_sema *sema)
144 {
145     aos_sem_signal(sema);
146 }
147 
_aos_down_sema(_sema * sema,u32 timeout)148 static u32 _aos_down_sema(_sema *sema, u32 timeout)
149 {
150     //DBG_8195A("_aos_down_sema\r\n");
151     if(timeout == RTW_MAX_DELAY) {
152         timeout = AOS_WAIT_FOREVER;
153     } else {
154         timeout = rtw_ms_to_systime(timeout);
155     }
156     //DBG_8195A("_aos_down_sema, timeout %d\r\n", timeout);
157 	if(aos_sem_wait(sema, timeout) != 0) {
158                 printf("_aos_down_sema 0x%x, timeout %d\n", sema, timeout);
159 		return FALSE;
160 	}
161 
162 	return TRUE;
163     //return aos_sem_wait(sema, timeout);
164 }
165 
_aos_mutex_init(_mutex * pmutex)166 static void _aos_mutex_init(_mutex *pmutex)
167 {
168     aos_mutex_new(pmutex);
169     return;
170 }
171 
_aos_mutex_free(_mutex * pmutex)172 static void _aos_mutex_free(_mutex *pmutex)
173 {
174     if(pmutex != NULL)
175         aos_mutex_free(pmutex);
176 
177     //pmutex = NULL;
178 }
179 
_aos_mutex_get(_lock * plock)180 static void _aos_mutex_get(_lock *plock)
181 {
182     while(aos_mutex_lock(plock, 60 * 1000) != 0)
183         DBG_8195A("%s(%p) failed, retry\n",  __FUNCTION__, plock);
184 }
185 
_aos_mutex_get_timeout(_lock * plock,u32 timeout_ms)186 static int _aos_mutex_get_timeout(_lock *plock, u32 timeout_ms)
187 {
188     if(aos_mutex_lock(plock, timeout_ms ) != 0){
189         DBG_8195A(" %s(%p) failed, retry\n",  __FUNCTION__, plock);
190         return -1;
191     }
192     return 0;
193 }
194 
_aos_mutex_put(_lock * plock)195 static void _aos_mutex_put(_lock *plock)
196 {
197     aos_mutex_unlock(plock);
198 }
199 #if 1
_aos_enter_critical(_lock * plock,_irqL * pirqL)200 static void _aos_enter_critical(_lock *plock, _irqL *pirqL)
201 {
202 	__asm volatile( "cpsid i" );
203 	ulCriticalNesting++;
204 	__asm volatile( "dsb" ::: "memory" );
205 	__asm volatile( "isb" );
206 }
207 
_aos_exit_critical(_lock * plock,_irqL * pirqL)208 static void _aos_exit_critical(_lock *plock, _irqL *pirqL)
209 {
210 	if(ulCriticalNesting == 0)
211 		return;
212 
213 	ulCriticalNesting--;
214 
215 	if( ulCriticalNesting == 0 )
216 	{
217 		__asm volatile( "cpsie i" );
218 	}
219 }
220 
_aos_enter_critical_from_isr(_lock * plock,_irqL * pirqL)221 static void _aos_enter_critical_from_isr(_lock *plock, _irqL *pirqL)
222 {
223 	uxSavedInterruptStatus = cpu_intrpt_save( );
224 }
225 
_aos_exit_critical_from_isr(_lock * plock,_irqL * pirqL)226 static void _aos_exit_critical_from_isr(_lock *plock, _irqL *pirqL)
227 {
228 	cpu_intrpt_restore(uxSavedInterruptStatus);
229 }
230 #endif
_aos_enter_critical_mutex(_mutex * pmutex,_irqL * pirqL)231 static int _aos_enter_critical_mutex(_mutex *pmutex, _irqL *pirqL)
232 {
233     int ret = 0;
234 
235     while(aos_mutex_lock(pmutex, 60 * 1000) != 0)
236         DBG_8195A("%s(%p) failed, retry\n", __FUNCTION__, pmutex);
237 
238     return ret;
239 }
240 
_aos_exit_critical_mutex(_mutex * pmutex,_irqL * pirqL)241 static void _aos_exit_critical_mutex(_mutex *pmutex, _irqL *pirqL)
242 {
243     aos_mutex_unlock(pmutex);
244 }
245 
_aos_spinlock_init(_lock * plock)246 static void _aos_spinlock_init(_lock *plock)
247 {
248 #if USE_MUTEX_FOR_SPINLOCK
249     aos_mutex_new(plock);
250 #endif
251 }
252 
_aos_spinlock_free(_lock * plock)253 static void _aos_spinlock_free(_lock *plock)
254 {
255 #if USE_MUTEX_FOR_SPINLOCK
256     if(plock != NULL)
257         aos_mutex_free(plock);
258 
259     //*plock = NULL;
260 #endif
261 }
262 
_aos_spinlock(_lock * plock)263 static void _aos_spinlock(_lock *plock)
264 {
265 #if USE_MUTEX_FOR_SPINLOCK
266     while(aos_mutex_lock(plock, 60 * 1000) != 0)
267         DBG_8195A(" %s(%p) failed, retry\n", __FUNCTION__, plock);
268 #endif
269 }
270 
_aos_spinunlock(_lock * plock)271 static void _aos_spinunlock(_lock *plock)
272 {
273 #if USE_MUTEX_FOR_SPINLOCK
274     aos_mutex_unlock(plock);
275 #endif
276 }
277 #if 0
278 static void _aos_spinlock_irqsave(_lock *plock, _irqL *irqL)
279 {
280     RHINO_CRITICAL_ENTER();
281 #if USE_MUTEX_FOR_SPINLOCK
282     while(aos_mutex_lock(plock, 60 * 1000) != 0)
283         DBG_8195A("[%s] %s(%p) failed, retry\n", aos_task_name(), __FUNCTION__, plock);
284 #endif
285 }
286 
287 static void _aos_spinunlock_irqsave(_lock *plock, _irqL *irqL)
288 {
289 #if USE_MUTEX_FOR_SPINLOCK
290     aos_mutex_unlock(plock);
291 #endif
292     RHINO_CRITICAL_EXIT();
293 }
294 }
295 #endif
_aos_init_xqueue(_xqueue * queue,const char * name,u32 message_size,u32 number_of_messages)296 static int _aos_init_xqueue( _xqueue* queue, const char* name, u32 message_size, u32 number_of_messages )
297 {
298     int ret = 0;
299     ret = krhino_buf_queue_dyn_create(&queue, name, message_size, number_of_messages);
300     return ret;
301 }
302 
_aos_push_to_xqueue(_xqueue * queue,void * message,u32 timeout_ms)303 static int _aos_push_to_xqueue( _xqueue* queue, void* message, u32 timeout_ms )
304 {
305     krhino_buf_queue_send(queue, message, sizeof(void*));
306 
307     return 0;
308 }
309 
_aos_pop_from_xqueue(_xqueue * queue,void * message,u32 timeout_ms)310 static int _aos_pop_from_xqueue( _xqueue* queue, void* message, u32 timeout_ms )
311 {
312     int size = 0;
313     krhino_buf_queue_recv(queue, timeout_ms, message, &size);
314 
315     return 0;
316 }
317 
_aos_deinit_xqueue(_xqueue * queue)318 static int _aos_deinit_xqueue( _xqueue* queue )
319 {
320     krhino_buf_queue_dyn_del(queue);
321     return 0;
322 }
323 
_aos_get_current_time(void)324 static u32 _aos_get_current_time(void)
325 {
326     return aos_now_ms();	//The count of ticks since vTaskStartScheduler was called.
327 }
328 
_aos_systime_to_ms(u32 systime)329 static u32 _aos_systime_to_ms(u32 systime)
330 {
331     return  krhino_ticks_to_ms(systime);
332 }
333 
_aos_systime_to_sec(u32 systime)334 static u32 _aos_systime_to_sec(u32 systime)
335 {
336     return krhino_ticks_to_ms(systime)/1000;
337 }
338 
_aos_ms_to_systime(u32 ms)339 static u32 _aos_ms_to_systime(u32 ms)
340 {
341     return krhino_ms_to_ticks(ms);
342 }
343 
_aos_sec_to_systime(u32 sec)344 static u32 _aos_sec_to_systime(u32 sec)
345 {
346     return krhino_ms_to_ticks(sec) * 1000;
347 }
348 
_aos_msleep_os(int ms)349 static void _aos_msleep_os(int ms)
350 {
351 #if defined(CONFIG_PLATFORM_8195A)
352 	aos_msleep(ms);
353 #elif defined(CONFIG_PLATFORM_8711B)
354 	if (pmu_yield_os_check()) {
355 		aos_msleep(ms);
356 	} else {
357 		DelayMs(ms);
358 	}
359 #elif defined(CONFIG_PLATFORM_8721D)
360         if (pmu_yield_os_check()) {
361                 aos_msleep(ms);
362         } else {
363                 DelayMs(ms);
364         }
365 #endif
366 }
367 
_aos_usleep_os(int us)368 static void _aos_usleep_os(int us)
369 {
370 #if defined(STM32F2XX) || defined(STM32F4XX) || defined(STM32F10X_XL)
371 	// FreeRTOS does not provide us level delay. Use busy wait
372 	WLAN_BSP_UsLoop(us);
373 #elif defined(CONFIG_PLATFORM_8195A)
374 	//DBG_ERR("%s: Please Implement micro-second delay\n", __FUNCTION__);
375 #elif defined(CONFIG_PLATFORM_8711B)
376 	DelayUs(us);
377 #elif defined(CONFIG_PLATFORM_8721D)
378         DelayUs(us);
379 #else
380 	#error "Please implement hardware dependent micro second level sleep here"
381 #endif
382 }
383 
_aos_mdelay_os(int ms)384 static void _aos_mdelay_os(int ms)
385 {
386 #if defined(CONFIG_PLATFORM_8195A)
387 	aos_msleep(ms);
388 #elif defined(CONFIG_PLATFORM_8711B)
389 	if (pmu_yield_os_check()) {
390 		aos_msleep(ms);
391 	} else {
392 		DelayMs(ms);
393 	}
394 #elif defined(CONFIG_PLATFORM_8721D)
395         if (pmu_yield_os_check()) {
396                 aos_msleep(ms);
397         } else {
398                 DelayMs(ms);
399         }
400 #endif
401 }
402 
_aos_udelay_os(int us)403 static void _aos_udelay_os(int us)
404 {
405 #if defined(STM32F2XX)	|| defined(STM32F4XX) || defined(STM32F10X_XL)
406 	// FreeRTOS 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 #elif defined(CONFIG_PLATFORM_8721D)
413         DelayUs(us);
414 #else
415 	#error "Please implement hardware dependent micro second level sleep here"
416 #endif
417 }
418 
_aos_yield_os(void)419 static void _aos_yield_os(void)
420 {
421 #if defined(CONFIG_PLATFORM_8195A)
422 	taskYIELD();
423 #elif defined(CONFIG_PLATFORM_8711B)
424 	krhino_task_yield();
425 #elif defined(CONFIG_PLATFORM_8721D)
426         krhino_task_yield();
427 #endif
428 }
429 
_aos_ATOMIC_SET(ATOMIC_T * v,int i)430 static void _aos_ATOMIC_SET(ATOMIC_T *v, int i)
431 {
432 	atomic_set(v,i);
433 }
434 
_aos_ATOMIC_READ(ATOMIC_T * v)435 static int _aos_ATOMIC_READ(ATOMIC_T *v)
436 {
437 	return atomic_read(v);
438 }
439 
_aos_ATOMIC_ADD(ATOMIC_T * v,int i)440 static void _aos_ATOMIC_ADD(ATOMIC_T *v, int i)
441 {
442         CPSR_ALLOC();
443 	cpsr = cpu_intrpt_save();
444 	v->counter += i;
445 	cpu_intrpt_restore(cpsr);
446 }
447 
_aos_ATOMIC_SUB(ATOMIC_T * v,int i)448 static void _aos_ATOMIC_SUB(ATOMIC_T *v, int i)
449 {
450     size_t cpsr_sig;
451 
452 	CPSR_ALLOC();
453 	cpsr = cpu_intrpt_save();
454 	v->counter -= i;
455 	cpu_intrpt_restore(cpsr_sig);
456 }
457 
_aos_ATOMIC_INC(ATOMIC_T * v)458 static void _aos_ATOMIC_INC(ATOMIC_T *v)
459 {
460 	_aos_ATOMIC_ADD(v, 1);
461 }
462 
_aos_ATOMIC_DEC(ATOMIC_T * v)463 static void _aos_ATOMIC_DEC(ATOMIC_T *v)
464 {
465 	_aos_ATOMIC_SUB(v, 1);
466 }
467 
_aos_ATOMIC_ADD_RETURN(ATOMIC_T * v,int i)468 static int _aos_ATOMIC_ADD_RETURN(ATOMIC_T *v, int i)
469 {
470 	int temp;
471 	CPSR_ALLOC();
472 	cpsr = cpu_intrpt_save();
473 	temp = v->counter;
474 	temp += i;
475 	v->counter = temp;
476 	cpu_intrpt_restore(cpsr);
477 
478 	return temp;
479 }
480 
_aos_ATOMIC_SUB_RETURN(ATOMIC_T * v,int i)481 static int _aos_ATOMIC_SUB_RETURN(ATOMIC_T *v, int i)
482 {
483 	int temp;
484 	CPSR_ALLOC();
485 	cpsr = cpu_intrpt_save();
486 	temp = v->counter;
487 	temp -= i;
488 	v->counter = temp;
489 	cpu_intrpt_restore(cpsr);
490 
491 	return temp;
492 }
493 
_aos_ATOMIC_INC_RETURN(ATOMIC_T * v)494 static int _aos_ATOMIC_INC_RETURN(ATOMIC_T *v)
495 {
496 	return _aos_ATOMIC_ADD_RETURN(v, 1);
497 }
498 
_aos_ATOMIC_DEC_RETURN(ATOMIC_T * v)499 static int _aos_ATOMIC_DEC_RETURN(ATOMIC_T *v)
500 {
501 	return _aos_ATOMIC_SUB_RETURN(v, 1);
502 }
503 
_aos_modular64(u64 n,u64 base)504 static u64 _aos_modular64(u64 n, u64 base)
505 {
506 	unsigned int __base = (base);
507 	unsigned int __rem;
508 
509 	if (((n) >> 32) == 0) {
510 		__rem = (unsigned int)(n) % __base;
511 		(n) = (unsigned int)(n) / __base;
512 	}
513 	else
514 		__rem = __div64_32(&(n), __base);
515 
516 	return __rem;
517 }
518 
519 /* Refer to ecos bsd tcpip codes */
_aos_arc4random(void)520 static int _aos_arc4random(void)
521 {
522 	u32 res = aos_now_ms();
523 	static unsigned long seed = 0xDEADB00B;
524 
525 #if CONFIG_PLATFORM_8711B
526 	if(random_seed){
527 		seed = random_seed;
528 		random_seed = 0;
529 	}
530 #endif
531 
532 	seed = ((seed & 0x007F00FF) << 7) ^
533 	    ((seed & 0x0F80FF00) >> 8) ^ // be sure to stir those low bits
534 	    (res << 13) ^ (res >> 9);    // using the clock too!
535 	return (int)seed;
536 }
537 
_aos_get_random_bytes(void * buf,u32 len)538 static int _aos_get_random_bytes(void *buf, u32 len)
539 {
540 #if 1 //becuase of 4-byte align, we use the follow code style.
541 	unsigned int ranbuf;
542 	unsigned int *lp;
543 	int i, count;
544 	count = len / sizeof(unsigned int);
545 	lp = (unsigned int *) buf;
546 
547 	for(i = 0; i < count; i ++) {
548 		lp[i] = _aos_arc4random();
549 		len -= sizeof(unsigned int);
550 	}
551 
552 	if(len > 0) {
553 		ranbuf = _aos_arc4random();
554 		_aos_memcpy(&lp[i], &ranbuf, len);
555 	}
556 	return 0;
557 #else
558 	unsigned long ranbuf, *lp;
559 	lp = (unsigned long *)buf;
560 	while (len > 0) {
561 		ranbuf = _freertos_arc4random();
562 		*lp++ = ranbuf; //this op need the pointer is 4Byte-align!
563 		len -= sizeof(ranbuf);
564 	}
565 	return 0;
566 #endif
567 }
568 
_aos_GetFreeHeapSize(void)569 static u32 _aos_GetFreeHeapSize(void)
570 {
571 	//return (u32)xPortGetFreeHeapSize();
572 	extern k_mm_head *g_kmm_head;
573 	return g_kmm_head->free_size;
574 }
575 
_aos_create_task(struct task_struct * ptask,const char * name,u32 stack_size,u32 priority,thread_func_t func,void * thctx)576 static int _aos_create_task(struct task_struct *ptask, const char *name,
577 	u32  stack_size, u32 priority, thread_func_t func, void *thctx)
578 {
579 	thread_func_t task_func = NULL;
580 	void *task_ctx = NULL;
581 	int ret = 0;
582 
583         //DBG_8195A("_aos_create_task  start\r\n");
584 	ptask->task_name = name;
585 	ptask->blocked = 0;
586 	ptask->callback_running = 0;
587 
588 	_aos_init_sema(&ptask->wakeup_sema, 0);
589 	_aos_init_sema(&ptask->terminate_sema, 0);
590 	//rtw_init_queue(&wq->work_queue);
591        // DBG_8195A("_aos_create_task  init sema done\r\n");
592 
593 	if(func){
594 		task_func = func;
595 		task_ctx = thctx;
596 	}
597 	//else{
598 	//	task_func = freertos_wq_thread_handler;
599 	//	task_ctx = wq;
600 	//}
601 
602 	priority = 11-priority;
603 
604 	ret = aos_task_new_ext(&ptask->task,
605 				name,
606 				func,
607 				task_ctx,
608 				stack_size*sizeof(cpu_stack_t),
609 				priority);
610 
611 
612 	if(ret != 0){
613 		DBG_8195A("Create Task \"%s\" Failed! ret=%d\n", ptask->task_name, ret);
614 	}
615 
616 	//DBG_8195A("Create Task \"%s\"\n", ptask->task_name);
617 	return 1;
618 }
619 
_aos_delete_task(struct task_struct * ptask)620 static void _aos_delete_task(struct task_struct *ptask)
621 {
622 	if (!(ptask->task)){
623 		DBG_8195A("_aos_delete_task(): ptask is NULL!\n");
624 		return;
625 	}
626 
627 	ptask->blocked = 1;
628 
629 	_aos_up_sema(&ptask->wakeup_sema);
630 	_aos_down_sema(&ptask->terminate_sema, RTW_MAX_DELAY);
631 
632 	//rtw_deinit_queue(&wq->work_queue);
633 	_aos_free_sema(&ptask->wakeup_sema);
634 	_aos_free_sema(&ptask->terminate_sema);
635 
636 	//ptask->task = 0;
637 
638 	//DBG_8195A("Delete Task \"%s\"\n", ptask->task_name);
639 }
640 
_aos_wakeup_task(struct task_struct * ptask)641 void _aos_wakeup_task(struct task_struct *ptask)
642 {
643     _aos_up_sema(&ptask->wakeup_sema);
644 }
645 
_aos_set_priority_task(void * task,u32 NewPriority)646 void _aos_set_priority_task(void* task, u32 NewPriority)
647 {
648     uint8_t old_pri = 0;
649     uint8_t priority = (uint8_t)(11 - NewPriority);
650     aos_task_pri_change((aos_task_t *)&task, priority, &old_pri);
651 }
652 
_aos_get_priority_task(void * task)653 int _aos_get_priority_task(void* task)
654 {
655     uint8_t priority = 0;
656     aos_task_pri_get((aos_task_t *)&task, &priority);
657     return (int)(11 - priority);
658 }
659 
_aos_suspend_task(void * task)660 void _aos_suspend_task(void* task)
661 {
662     aos_task_suspend((aos_task_t *)&task);
663 }
664 
_aos_resume_task(void * task)665 void _aos_resume_task(void* task)
666 {
667     aos_task_resume((aos_task_t *)&task);
668 }
669 
_aos_thread_enter(char * name)670 static void _aos_thread_enter(char *name)
671 {
672     //DBG_8195A("\n\rRTKTHREAD %s\n", name);
673 }
674 
_aos_thread_exit(void)675 static void _aos_thread_exit(void)
676 {
677     //DBG_8195A("\n\rRTKTHREAD exit %s\n", __FUNCTION__);
678     aos_task_exit(0);
679 }
680 
_aos_timerCreate(const signed char * pcTimerName,osdepTickType xTimerPeriodInTicks,u32 uxAutoReload,void * pvTimerID,TIMER_FUN pxCallbackFunction)681 _timerHandle _aos_timerCreate( const signed char *pcTimerName,
682 							  osdepTickType xTimerPeriodInTicks,
683 							  u32 uxAutoReload,
684 							  void * pvTimerID,
685 							  TIMER_FUN pxCallbackFunction )
686 {
687 	int ret ;
688     _timerHandle t_handler= _aos_zmalloc(sizeof(struct timerHandle));
689 
690     if(xTimerPeriodInTicks>= 0xffffffff)
691         xTimerPeriodInTicks = 0x7ffffffe;
692 
693     ret = aos_timer_new_ext(&t_handler->timer, pxCallbackFunction,(void *)t_handler, krhino_ticks_to_ms(xTimerPeriodInTicks), 0, 0);
694 
695     return t_handler;
696 }
697 
_aos_timerDelete(_timerHandle xTimer,osdepTickType xBlockTime)698 u32 _aos_timerDelete( _timerHandle xTimer,
699 							   osdepTickType xBlockTime )
700 {
701     aos_timer_stop(&xTimer->timer);
702     aos_timer_free(&xTimer->timer);
703     _aos_mfree(xTimer, sizeof(xTimer));
704     return 0;
705 }
706 
_aos_timerIsTimerActive(_timerHandle xTimer)707 u32 _aos_timerIsTimerActive( _timerHandle xTimer )
708 {
709 	return 1;
710 }
711 
_aos_timerStop(_timerHandle xTimer,osdepTickType xBlockTime)712 u32  _aos_timerStop( _timerHandle xTimer,
713 							   osdepTickType xBlockTime )
714 {
715 	if(!aos_timer_stop(&xTimer->timer))
716 		return 1;
717 	return 0;
718 }
719 
720 #define MS2TICK(ms) krhino_ms_to_ticks(ms)
721 
_aos_timer_change_no_repeat(aos_timer_t * timer,int ms)722 int _aos_timer_change_no_repeat(aos_timer_t *timer, int ms)
723 {
724     int ret;
725 
726     ret = aos_timer_change_once(timer, ms);
727 
728 	ret = aos_timer_start(timer);
729 
730     return ret;
731 }
732 
_aos_timerChangePeriod(_timerHandle xTimer,osdepTickType xNewPeriod,osdepTickType xBlockTime)733 u32  _aos_timerChangePeriod( _timerHandle xTimer,
734 							   osdepTickType xNewPeriod,
735 							   osdepTickType xBlockTime )
736 {
737 	if(xNewPeriod == 0)
738 		xNewPeriod += 1;
739 	//if(!aos_timer_change(&xTimer->timer, xNewPeriod))
740 	if(!_aos_timer_change_no_repeat(&xTimer->timer, xNewPeriod))
741 		return 1;
742 	return 0;
743 }
_aos_timerGetID(_timerHandle xTimer)744 void *_aos_timerGetID( _timerHandle xTimer ){
745 
746 	return 0;
747 }
748 
_aos_timerStart(_timerHandle xTimer,osdepTickType xBlockTime)749 u32  _aos_timerStart( _timerHandle xTimer,
750 							   osdepTickType xBlockTime )
751 {
752 	int ret;
753 	ret = aos_timer_start(&xTimer->timer);
754 	return  !ret; //!aos_timer_start(&xTimer->timer);
755 }
756 
_aos_timerStartFromISR(_timerHandle xTimer,osdepBASE_TYPE * pxHigherPriorityTaskWoken)757 u32  _aos_timerStartFromISR( _timerHandle xTimer,
758 							   osdepBASE_TYPE *pxHigherPriorityTaskWoken )
759 {
760     return !aos_timer_start(&xTimer->timer);
761 }
762 
_aos_timerStopFromISR(_timerHandle xTimer,osdepBASE_TYPE * pxHigherPriorityTaskWoken)763 u32  _aos_timerStopFromISR( _timerHandle xTimer,
764 							   osdepBASE_TYPE *pxHigherPriorityTaskWoken )
765 {
766     return !aos_timer_stop(&xTimer->timer);
767 }
768 
_aos_timerResetFromISR(_timerHandle xTimer,osdepBASE_TYPE * pxHigherPriorityTaskWoken)769 u32  _aos_timerResetFromISR( _timerHandle xTimer,
770 							   osdepBASE_TYPE *pxHigherPriorityTaskWoken )
771 {
772     (u32)aos_timer_stop(&xTimer->timer);
773     return !aos_timer_start(&xTimer->timer);
774 }
775 
_aos_timerChangePeriodFromISR(_timerHandle xTimer,osdepTickType xNewPeriod,osdepBASE_TYPE * pxHigherPriorityTaskWoken)776 u32  _aos_timerChangePeriodFromISR( _timerHandle xTimer,
777 							   osdepTickType xNewPeriod,
778 							   osdepBASE_TYPE *pxHigherPriorityTaskWoken )
779 {
780     if(xNewPeriod == 0)
781         xNewPeriod += 1;
782 
783     (u32)aos_timer_stop(&xTimer->timer);
784 
785     return !aos_timer_change(&xTimer->timer, xNewPeriod);
786 }
787 
_aos_timerReset(_timerHandle xTimer,osdepTickType xBlockTime)788 u32  _aos_timerReset( _timerHandle xTimer,
789 							   osdepTickType xBlockTime )
790 {
791     (u32)aos_timer_stop(&xTimer->timer);
792     return !aos_timer_start(&xTimer->timer);
793 }
794 
_aos_acquire_wakelock()795 void _aos_acquire_wakelock()
796 {
797 #if defined(CONFIG_PLATFORM_8195A)
798 
799 #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
800    	pmu_acquire_wakelock(PMU_WLAN_DEVICE);
801 #endif
802 
803 #elif defined(CONFIG_PLATFORM_8711B)
804 
805 #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
806 	if (pmu_yield_os_check())
807    		 pmu_acquire_wakelock(PMU_WLAN_DEVICE);
808 #endif
809 #elif defined(CONFIG_PLATFORM_8721D)
810 
811 #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
812         if (pmu_yield_os_check())
813                  pmu_acquire_wakelock(PMU_WLAN_DEVICE);
814 #endif
815 
816 #endif
817 }
818 
_aos_release_wakelock()819 void _aos_release_wakelock()
820 {
821 
822 #if defined(CONFIG_PLATFORM_8195A)
823 
824 #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
825     pmu_release_wakelock(PMU_WLAN_DEVICE);
826 #endif
827 
828 #elif defined(CONFIG_PLATFORM_8711B)
829 
830 #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
831 	if (pmu_yield_os_check())
832 		pmu_release_wakelock(PMU_WLAN_DEVICE);
833 #endif
834 #elif defined(CONFIG_PLATFORM_8721D)
835 
836 #if defined(configUSE_WAKELOCK_PMU) && (configUSE_WAKELOCK_PMU == 1)
837         if (pmu_yield_os_check())
838                 pmu_release_wakelock(PMU_WLAN_DEVICE);
839 #endif
840 
841 #endif
842 }
843 
_aos_wakelock_timeout(uint32_t timeout)844 void _aos_wakelock_timeout(uint32_t timeout)
845 {
846 #if defined(CONFIG_PLATFORM_8195A)
847 
848 #elif defined(CONFIG_PLATFORM_8711B)
849 	if (pmu_yield_os_check())
850 		pmu_set_sysactive_time(timeout);
851 	else
852 		DBG_INFO("can't aquire wake during suspend flow!!\n");
853 #elif defined(CONFIG_PLATFORM_8721D)
854         if (pmu_yield_os_check())
855                 pmu_set_sysactive_time(timeout);
856         else
857                 DBG_INFO("can't aquire wake during suspend flow!!\n");
858 #endif
859 }
860 
_aos_get_scheduler_state(void)861 u8 _aos_get_scheduler_state(void)
862 {
863 	return 0;
864 }
865 
866 
867 const struct osdep_service_ops osdep_service = {
868 	_aos_malloc,			//rtw_vmalloc
869 	_aos_zmalloc,			//rtw_zvmalloc
870 	_aos_mfree,			//rtw_vmfree
871 	_aos_malloc,			//rtw_malloc
872 	_aos_zmalloc,			//rtw_zmalloc
873 	_aos_mfree,			//rtw_mfree
874 	_aos_memcpy,			//rtw_memcpy
875 	_aos_memcmp,			//rtw_memcmp
876 	_aos_memset,			//rtw_memset
877 	_aos_init_sema,		//rtw_init_sema
878 	_aos_free_sema,		//rtw_free_sema
879 	_aos_up_sema,			//rtw_up_sema
880 	_aos_up_sema_from_isr,	//rtw_up_sema_from_isr
881 	_aos_down_sema,		//rtw_down_sema
882 	_aos_mutex_init,		//rtw_mutex_init
883 	_aos_mutex_free,		//rtw_mutex_free
884 	_aos_mutex_get,		//rtw_mutex_get
885 	_aos_mutex_get_timeout,//rtw_mutex_get_timeout
886 	_aos_mutex_put,		//rtw_mutex_put
887 	_aos_enter_critical,	//rtw_enter_critical
888 	_aos_exit_critical,	//rtw_exit_critical
889 	_aos_enter_critical_from_isr,	//rtw_enter_critical_from_isr
890 	_aos_exit_critical_from_isr,	//rtw_exit_critical_from_isr
891 	NULL,		//rtw_enter_critical_bh
892 	NULL,		//rtw_exit_critical_bh
893 	_aos_enter_critical_mutex,	//rtw_enter_critical_mutex
894 	_aos_exit_critical_mutex,	//rtw_exit_critical_mutex
895 	_aos_spinlock_init,		//rtw_spinlock_init
896 	_aos_spinlock_free,		//rtw_spinlock_free
897 	_aos_spinlock,				//rtw_spin_lock
898 	_aos_spinunlock,			//rtw_spin_unlock
899 	NULL,		//rtw_spinlock_irqsave
900 	NULL,	//rtw_spinunlock_irqsave
901 	_aos_init_xqueue,			//rtw_init_xqueue
902 	_aos_push_to_xqueue,		//rtw_push_to_xqueue
903 	_aos_pop_from_xqueue,		//rtw_pop_from_xqueue
904 	_aos_deinit_xqueue,		//rtw_deinit_xqueue
905 	_aos_get_current_time,		//rtw_get_current_time
906 	_aos_systime_to_ms,		//rtw_systime_to_ms
907 	_aos_systime_to_sec,		//rtw_systime_to_sec
908 	_aos_ms_to_systime,		//rtw_ms_to_systime
909 	_aos_sec_to_systime,		//rtw_sec_to_systime
910 	_aos_msleep_os,	//rtw_msleep_os
911 	_aos_usleep_os,	//rtw_usleep_os
912 	_aos_mdelay_os,	//rtw_mdelay_os
913 	_aos_udelay_os,	//rtw_udelay_os
914 	_aos_yield_os,		//rtw_yield_os
915 
916 	_aos_ATOMIC_SET,	//ATOMIC_SET
917 	_aos_ATOMIC_READ,	//ATOMIC_READ
918 	_aos_ATOMIC_ADD,	//ATOMIC_ADD
919 	_aos_ATOMIC_SUB,	//ATOMIC_SUB
920 	_aos_ATOMIC_INC,	//ATOMIC_INC
921 	_aos_ATOMIC_DEC,	//ATOMIC_DEC
922 	_aos_ATOMIC_ADD_RETURN,	//ATOMIC_ADD_RETURN
923 	_aos_ATOMIC_SUB_RETURN,	//ATOMIC_SUB_RETURN
924 	_aos_ATOMIC_INC_RETURN,	//ATOMIC_INC_RETURN
925 	_aos_ATOMIC_DEC_RETURN,	//ATOMIC_DEC_RETURN
926 
927 	_aos_modular64,			//rtw_modular64
928 	_aos_get_random_bytes,		//rtw_get_random_bytes
929 	_aos_GetFreeHeapSize,		//rtw_getFreeHeapSize
930 
931 	_aos_create_task,			//rtw_create_task
932 	_aos_delete_task,			//rtw_delete_task
933 	_aos_wakeup_task,			//rtw_wakeup_task
934 
935 	_aos_set_priority_task,     //rtw_set_priority_task
936 	_aos_get_priority_task,     //rtw_get_priority_task
937 	_aos_suspend_task,          //rtw_suspend_task
938 	_aos_resume_task,           //rtw_resume_task
939 
940 	_aos_thread_enter,			//rtw_thread_enter
941 	_aos_thread_exit,			//rtw_thread_exit
942 
943 	_aos_timerCreate,			//rtw_timerCreate,
944 	_aos_timerDelete,			//rtw_timerDelete,
945 	_aos_timerIsTimerActive,	//rtw_timerIsTimerActive,
946 	_aos_timerStop,			//rtw_timerStop,
947 	_aos_timerChangePeriod,	//rtw_timerChangePeriod
948 	_aos_timerGetID,			//rtw_timerGetID
949 	_aos_timerStart,			//rtw_timerStart
950 	_aos_timerStartFromISR,	//rtw_timerStartFromISR
951 	_aos_timerStopFromISR,		//rtw_timerStopFromISR
952 	_aos_timerResetFromISR,	//rtw_timerResetFromISR
953 	_aos_timerChangePeriodFromISR,	//rtw_timerChangePeriodFromISR
954 	_aos_timerReset,			//rtw_timerReset
955 
956 	_aos_acquire_wakelock,		//rtw_acquire_wakelock
957 	_aos_release_wakelock,		//rtw_release_wakelock
958 	_aos_wakelock_timeout,		//rtw_wakelock_timeout
959 	_aos_get_scheduler_state	//rtw_get_scheduler_state
960 };
961 
962