hal_os.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2017 ALLWINNERTECH TECHNOLOGY CO., LTD. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. * 1. Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * 2. Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the
  12. * distribution.
  13. * 3. Neither the name of ALLWINNERTECH TECHNOLOGY CO., LTD. nor the names of
  14. * its contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  20. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  21. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  22. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  23. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  24. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  25. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  27. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. */
  29. #ifndef _DRIVER_CHIP_HAL_OS_H_
  30. #define _DRIVER_CHIP_HAL_OS_H_
  31. #include "os.h"
  32. #include <string.h>
  33. #include <stdlib.h>
  34. #include <hal_atomic.h>
  35. #ifndef CONFIG_KERNEL_FREERTOS
  36. #include <ktimer.h>
  37. #include <hal_cfg.h>
  38. #else
  39. #include <hal_cache.h>
  40. #endif
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. /* IRQ disable/enable */
  45. #define HAL_DisableIRQ() arch_irq_disable()
  46. #define HAL_EnableIRQ() arch_irq_enable()
  47. /* Check if IRQ is disabled */
  48. #define HAL_IsIRQDisabled() __get_PRIMASK()
  49. /* Check if in ISR context or not */
  50. #define HAL_IsISRContext() __get_IPSR()
  51. extern hal_spinlock_t sdmmc_lock;
  52. /* Critical Sections */
  53. //#define HAL_EnterCriticalSection() arch_irq_save()
  54. //#define HAL_ExitCriticalSection(flags) arch_irq_restore(flags)
  55. #define HAL_EnterCriticalSection() ({hal_spin_lock_irqsave(&sdmmc_lock);})
  56. #define HAL_ExitCriticalSection(f) ({hal_spin_unlock_irqrestore(&sdmmc_lock, f);})
  57. #define HAL_ATMOTIC_SET(a,v) ({int flags = HAL_EnterCriticalSection();\
  58. a = v;HAL_ExitCriticalSection(flags);})
  59. #define HAL_ATMOTIC_READ(a) ({int flags=0;int v=0; flags = HAL_EnterCriticalSection();v=a;HAL_ExitCriticalSection(flags);v;})
  60. #define HAL_FlushDcacheRegion(s,len) (hal_dcache_clean_invalidate(s,len))
  61. #define HAL_InvalidDcacheRegion(s,len) (hal_dcache_invalidate(s, len))
  62. #if 0
  63. #define HAL_GetTimeMs() (aos_now_ms())
  64. #define HAL_GetTimeUs() (aos_now()/1000)
  65. //#define HAL_GetTimeUs() (OS_GetTime()*1000)
  66. #define HAL_GetTimeNs() (aos_now())
  67. #else
  68. #if 0
  69. #define HAL_GetTimeMs() ((rt_tick_get()*1000)/CONFIG_HZ)
  70. #define HAL_GetTimeUs() ((rt_tick_get()*1000*1000)/CONFIG_HZ)
  71. #define HAL_GetTimeNs() ((rt_tick_get()*1000*1000)/CONFIG_HZ)
  72. #endif
  73. #define HAL_GetTimeMs() ({\
  74. struct timeval tv;\
  75. gettimeofday(&tv, NULL);\
  76. (tv.tv_usec + tv.tv_sec * 1000000ll)/1000;\
  77. })
  78. #define HAL_GetTimeUs() ({\
  79. struct timeval tv;\
  80. gettimeofday(&tv, NULL);\
  81. (tv.tv_usec + tv.tv_sec * 1000000ll);\
  82. })
  83. #define HAL_GetTimeNs() ({\
  84. struct timespec64 sdmmc_timeval;\
  85. do_gettimeofday(&sdmmc_timeval);\
  86. (sdmmc_timeval.tv_sec*1000ll*1000*1000ll + sdmmc_timeval.tv_nsec);\
  87. })
  88. #endif
  89. /* Semaphore */
  90. typedef OS_Semaphore_t HAL_Semaphore;
  91. #define HAL_SemaphoreInit(sem, initCount, maxCount) \
  92. (OS_SemaphoreCreate(sem, initCount, maxCount) == OS_OK ? HAL_OK : HAL_ERROR)
  93. #define HAL_SemaphoreInitBinary(sem) \
  94. (OS_SemaphoreCreateBinary(sem) == OS_OK ? HAL_OK : HAL_ERROR)
  95. #define HAL_SemaphoreDeinit(sem) \
  96. (OS_SemaphoreDelete(sem) == OS_OK ? HAL_OK : HAL_ERROR)
  97. #define HAL_SemaphoreWait(sem, msec) \
  98. (OS_SemaphoreWait(sem, msec) == OS_OK ? HAL_OK : HAL_ERROR)
  99. #define HAL_SemaphoreRelease(sem) \
  100. (OS_SemaphoreRelease(sem) == OS_OK ? HAL_OK : HAL_ERROR)
  101. #define HAL_SemaphoreIsValid(sem) \
  102. OS_SemaphoreIsValid(sem)
  103. #define HAL_SemaphoreSetInvalid(sem) \
  104. OS_SemaphoreSetInvalid(sem)
  105. /* Mutex */
  106. typedef OS_Mutex_t HAL_Mutex;
  107. #define HAL_MutexInit(mtx) \
  108. (OS_RecursiveMutexCreate(mtx) == OS_OK ? HAL_OK : HAL_ERROR)
  109. #define HAL_MutexDeinit(mtx) \
  110. (OS_RecursiveMutexDelete(mtx) == OS_OK ? HAL_OK : HAL_ERROR)
  111. #define HAL_MutexLock(mtx, msec) \
  112. (OS_RecursiveMutexLock(mtx, msec) == OS_OK ? HAL_OK : HAL_ERROR)
  113. #define HAL_MutexUnlock(mtx) \
  114. (OS_RecursiveMutexUnlock(mtx) == OS_OK ? HAL_OK : HAL_ERROR)
  115. /* Thread */
  116. #define HAL_ThreadSuspendScheduler() OS_ThreadSuspendScheduler()
  117. #define HAL_ThreadResumeScheduler() OS_ThreadResumeScheduler()
  118. #define HAL_ThreadIsSchedulerRunning() OS_ThreadIsSchedulerRunning()
  119. #define HAL_ThreadEnd(s) (HAL_ATMOTIC_SET(s,0))
  120. #define HAL_ThreadStop(s) (HAL_ATMOTIC_SET(s,1))
  121. #define HAL_Thread_Should_Stop(s) (HAL_ATMOTIC_READ(s))
  122. #define HAL_ThreadDelete(w) (OS_ThreadDelete(NULL))
  123. /* Keep system alive, eg. feed watchdog */
  124. #define HAL_Alive() HAL_WDG_Feed()
  125. /* Time */
  126. #define HAL_Ticks() OS_GetTicks()
  127. #define HAL_MSleep(msec) OS_MSleep(msec)
  128. #define HAL_UDelay(us) OS_Udelay(us)
  129. #define HAL_SecsToTicks(sec) OS_SecsToTicks(sec)
  130. #define HAL_MSecsToTicks(msec) OS_MSecsToTicks(msec)
  131. #define HAL_TicksToMSecs(t) OS_TicksToMSecs(t)
  132. #define HAL_TicksToSecs(t) OS_TicksToSecs(t)
  133. #define HAL_TimeAfter(a, b) OS_TimeAfter(a, b)
  134. #define HAL_TimeBefore(a, b) OS_TimeBefore(a, b)
  135. #define HAL_TimeAfterEqual(a, b) OS_TimeAfterEqual(a, b)
  136. #define HAL_TimeBeforeEqual(a, b) OS_TimeBeforeEqual(a, b)
  137. #define HAL_ALIGN(x, a) __ALIGN_KERNEL((x), (a))
  138. #define ALIGN_DOWN(x, a) __ALIGN_KERNEL((x) - ((a)-1), (a))
  139. #define __ALIGN_KERNEL(x, a) __ALIGN_KERNEL_MASK(x, (typeof(x))(a)-1)
  140. #define __ALIGN_KERNEL_MASK(x, mask) (((x) + (mask)) & ~(mask))
  141. #define OS_CACHE_ALIGN_BYTES (64)
  142. static inline void *malloc_align_buf(size_t size)
  143. {
  144. void *fake_ptr = NULL;
  145. void *malloc_ptr = NULL;
  146. /*malloc_ptr = krhino_mm_alloc(size + OS_CACHE_ALIGN_BYTES);*/
  147. malloc_ptr = hal_malloc(size + OS_CACHE_ALIGN_BYTES);
  148. if (HAL_PT_TO_U(malloc_ptr) & 0x3) {
  149. printf("error: krhino_mm_alloc not align to 4 byte\r\n");
  150. }
  151. fake_ptr = (void *)(HAL_PT_TO_U(malloc_ptr + OS_CACHE_ALIGN_BYTES) & (~(OS_CACHE_ALIGN_BYTES -1)));
  152. *(uint32_t *)((uint32_t *)fake_ptr - 1) = HAL_PT_TO_U(malloc_ptr);
  153. return fake_ptr;
  154. }
  155. static inline void free_align_buf(void *addr)
  156. {
  157. void *malloc_ptr = NULL;
  158. if (!addr)
  159. return;
  160. malloc_ptr = (void *)HAL_PT_TO_U(*(uint32_t *)((uint32_t *)addr - 1));
  161. /*krhino_mm_free(malloc_ptr);*/
  162. hal_free(malloc_ptr);
  163. }
  164. /* Memory */
  165. #define HAL_Malloc(l) malloc(l)
  166. #define HAL_Free(p) free(p)
  167. #define HAL_Memcpy(d, s, l) memcpy(d, s, l)
  168. #define HAL_Memset(d, c, l) memset(d, c, l)
  169. #define HAL_Memcmp(a, b, l) memcmp(a, b, l)
  170. #define HAL_Memmove(d, s, n) memmove(d, s, n)
  171. #define HAL_MallocAlign(l) (malloc_align_buf(l))
  172. #define HAL_FreeAlign(p) (free_align_buf(p))
  173. #ifndef CONFIG_KERNEL_FREERTOS
  174. #define HAL_SetPin
  175. #endif
  176. #ifdef __cplusplus
  177. }
  178. #endif
  179. #endif /* _DRIVER_CHIP_HAL_OS_H_ */