rthw.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-18 Bernard the first version
  9. * 2006-04-25 Bernard add rt_hw_context_switch_interrupt declaration
  10. * 2006-09-24 Bernard add rt_hw_context_switch_to declaration
  11. * 2012-12-29 Bernard add rt_hw_exception_install declaration
  12. * 2017-10-17 Hichard add some micros
  13. * 2018-11-17 Jesven add rt_hw_spinlock_t
  14. * add smp support
  15. * 2019-05-18 Bernard add empty definition for not enable cache case
  16. */
  17. #ifndef __RT_HW_H__
  18. #define __RT_HW_H__
  19. #include <rtthread.h>
  20. #ifdef __cplusplus
  21. extern "C" {
  22. #endif
  23. /*
  24. * Some macros define
  25. */
  26. #ifndef HWREG32
  27. #define HWREG32(x) (*((volatile rt_uint32_t *)(x)))
  28. #endif
  29. #ifndef HWREG16
  30. #define HWREG16(x) (*((volatile rt_uint16_t *)(x)))
  31. #endif
  32. #ifndef HWREG8
  33. #define HWREG8(x) (*((volatile rt_uint8_t *)(x)))
  34. #endif
  35. #ifndef RT_CPU_CACHE_LINE_SZ
  36. #define RT_CPU_CACHE_LINE_SZ 32
  37. #endif
  38. enum RT_HW_CACHE_OPS
  39. {
  40. RT_HW_CACHE_FLUSH = 0x01,
  41. RT_HW_CACHE_INVALIDATE = 0x02,
  42. };
  43. /*
  44. * CPU interfaces
  45. */
  46. #ifdef RT_USING_CACHE
  47. void rt_hw_cpu_icache_enable(void);
  48. void rt_hw_cpu_icache_disable(void);
  49. rt_base_t rt_hw_cpu_icache_status(void);
  50. void rt_hw_cpu_icache_ops(int ops, void* addr, int size);
  51. void rt_hw_cpu_dcache_enable(void);
  52. void rt_hw_cpu_dcache_disable(void);
  53. rt_base_t rt_hw_cpu_dcache_status(void);
  54. void rt_hw_cpu_dcache_ops(int ops, void* addr, int size);
  55. #else
  56. /* define cache ops as empty */
  57. #define rt_hw_cpu_icache_enable(...)
  58. #define rt_hw_cpu_icache_disable(...)
  59. #define rt_hw_cpu_icache_ops(...)
  60. #define rt_hw_cpu_dcache_enable(...)
  61. #define rt_hw_cpu_dcache_disable(...)
  62. #define rt_hw_cpu_dcache_ops(...)
  63. #define rt_hw_cpu_icache_status(...) 0
  64. #define rt_hw_cpu_dcache_status(...) 0
  65. #endif
  66. void rt_hw_cpu_reset(void);
  67. void rt_hw_cpu_shutdown(void);
  68. rt_uint8_t *rt_hw_stack_init(void *entry,
  69. void *parameter,
  70. rt_uint8_t *stack_addr,
  71. void *exit);
  72. /*
  73. * Interrupt handler definition
  74. */
  75. typedef void (*rt_isr_handler_t)(int vector, void *param);
  76. struct rt_irq_desc
  77. {
  78. rt_isr_handler_t handler;
  79. void *param;
  80. #ifdef RT_USING_INTERRUPT_INFO
  81. char name[RT_NAME_MAX];
  82. rt_uint32_t counter;
  83. #endif
  84. };
  85. /*
  86. * Interrupt interfaces
  87. */
  88. void rt_hw_interrupt_init(void);
  89. void rt_hw_interrupt_mask(int vector);
  90. void rt_hw_interrupt_umask(int vector);
  91. rt_isr_handler_t rt_hw_interrupt_install(int vector,
  92. rt_isr_handler_t handler,
  93. void *param,
  94. const char *name);
  95. #ifdef RT_USING_SMP
  96. rt_base_t rt_hw_local_irq_disable();
  97. void rt_hw_local_irq_enable(rt_base_t level);
  98. #define rt_hw_interrupt_disable rt_cpus_lock
  99. #define rt_hw_interrupt_enable rt_cpus_unlock
  100. #else
  101. rt_base_t rt_hw_interrupt_disable(void);
  102. void rt_hw_interrupt_enable(rt_base_t level);
  103. #endif /*RT_USING_SMP*/
  104. /*
  105. * Context interfaces
  106. */
  107. #ifdef RT_USING_SMP
  108. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  109. void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread);
  110. void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread);
  111. #else
  112. void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to);
  113. void rt_hw_context_switch_to(rt_ubase_t to);
  114. void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to, rt_thread_t from_thread, rt_thread_t to_thread);
  115. #endif /*RT_USING_SMP*/
  116. void rt_hw_console_output(const char *str);
  117. void rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry);
  118. void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size);
  119. /*
  120. * Exception interfaces
  121. */
  122. void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context));
  123. /*
  124. * delay interfaces
  125. */
  126. void rt_hw_us_delay(rt_uint32_t us);
  127. #ifdef RT_USING_SMP
  128. #include <cpuport.h> /* for spinlock from arch */
  129. struct rt_spinlock
  130. {
  131. rt_hw_spinlock_t lock;
  132. };
  133. void rt_hw_spin_lock_init(rt_hw_spinlock_t *lock);
  134. void rt_hw_spin_lock(rt_hw_spinlock_t *lock);
  135. void rt_hw_spin_unlock(rt_hw_spinlock_t *lock);
  136. int rt_hw_cpu_id(void);
  137. extern rt_hw_spinlock_t _cpus_lock;
  138. extern rt_hw_spinlock_t _rt_critical_lock;
  139. #define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0}
  140. #define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \
  141. (rt_hw_spinlock_t) __RT_HW_SPIN_LOCK_INITIALIZER(lockname)
  142. #define RT_DEFINE_SPINLOCK(x) rt_hw_spinlock_t x = __RT_HW_SPIN_LOCK_UNLOCKED(x)
  143. #define RT_DECLARE_SPINLOCK(x)
  144. /**
  145. * ipi function
  146. */
  147. void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask);
  148. /**
  149. * boot secondary cpu
  150. */
  151. void rt_hw_secondary_cpu_up(void);
  152. /**
  153. * secondary cpu idle function
  154. */
  155. void rt_hw_secondary_cpu_idle_exec(void);
  156. #else
  157. #define RT_DEFINE_SPINLOCK(x)
  158. #define RT_DECLARE_SPINLOCK(x) rt_ubase_t x
  159. #define rt_hw_spin_lock(lock) *(lock) = rt_hw_interrupt_disable()
  160. #define rt_hw_spin_unlock(lock) rt_hw_interrupt_enable(*(lock))
  161. typedef int rt_spinlock_t;
  162. #endif
  163. #ifdef RT_USING_CACHE
  164. #include <cpuport.h>
  165. #else
  166. #define rt_hw_isb()
  167. #define rt_hw_dmb()
  168. #define rt_hw_dsb()
  169. #endif
  170. #ifdef __cplusplus
  171. }
  172. #endif
  173. #endif