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 */ 16 17 #ifndef __RT_HW_H__ 18 #define __RT_HW_H__ 19 20 #include <rtthread.h> 21 22 #ifdef __cplusplus 23 extern "C" { 24 #endif 25 26 /* 27 * Some macros define 28 */ 29 #ifndef HWREG32 30 #define HWREG32(x) (*((volatile rt_uint32_t *)(x))) 31 #endif 32 #ifndef HWREG16 33 #define HWREG16(x) (*((volatile rt_uint16_t *)(x))) 34 #endif 35 #ifndef HWREG8 36 #define HWREG8(x) (*((volatile rt_uint8_t *)(x))) 37 #endif 38 39 #ifndef RT_CPU_CACHE_LINE_SZ 40 #define RT_CPU_CACHE_LINE_SZ 32 41 #endif 42 43 enum RT_HW_CACHE_OPS 44 { 45 RT_HW_CACHE_FLUSH = 0x01, 46 RT_HW_CACHE_INVALIDATE = 0x02, 47 }; 48 49 /* 50 * CPU interfaces 51 */ 52 void rt_hw_cpu_icache_enable(void); 53 void rt_hw_cpu_icache_disable(void); 54 rt_base_t rt_hw_cpu_icache_status(void); 55 void rt_hw_cpu_icache_ops(int ops, void* addr, int size); 56 57 void rt_hw_cpu_dcache_enable(void); 58 void rt_hw_cpu_dcache_disable(void); 59 rt_base_t rt_hw_cpu_dcache_status(void); 60 void rt_hw_cpu_dcache_ops(int ops, void* addr, int size); 61 62 void rt_hw_cpu_reset(void); 63 void rt_hw_cpu_shutdown(void); 64 65 rt_uint8_t *rt_hw_stack_init(void *entry, 66 void *parameter, 67 rt_uint8_t *stack_addr, 68 void *exit); 69 70 /* 71 * Interrupt handler definition 72 */ 73 typedef void (*rt_isr_handler_t)(int vector, void *param); 74 75 struct rt_irq_desc 76 { 77 rt_isr_handler_t handler; 78 void *param; 79 80 #ifdef RT_USING_INTERRUPT_INFO 81 char name[RT_NAME_MAX]; 82 rt_uint32_t counter; 83 #endif 84 }; 85 86 /* 87 * Interrupt interfaces 88 */ 89 void rt_hw_interrupt_init(void); 90 void rt_hw_interrupt_mask(int vector); 91 void rt_hw_interrupt_umask(int vector); 92 rt_isr_handler_t rt_hw_interrupt_install(int vector, 93 rt_isr_handler_t handler, 94 void *param, 95 const char *name); 96 97 #ifdef RT_USING_SMP 98 rt_base_t rt_hw_local_irq_disable(); 99 void rt_hw_local_irq_enable(rt_base_t level); 100 101 #define rt_hw_interrupt_disable rt_cpus_lock 102 #define rt_hw_interrupt_enable rt_cpus_unlock 103 104 #else 105 rt_base_t rt_hw_interrupt_disable(void); 106 void rt_hw_interrupt_enable(rt_base_t level); 107 #endif /*RT_USING_SMP*/ 108 109 /* 110 * Context interfaces 111 */ 112 #ifdef RT_USING_SMP 113 void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread); 114 void rt_hw_context_switch_to(rt_ubase_t to, struct rt_thread *to_thread); 115 void rt_hw_context_switch_interrupt(void *context, rt_ubase_t from, rt_ubase_t to, struct rt_thread *to_thread); 116 #else 117 void rt_hw_context_switch(rt_ubase_t from, rt_ubase_t to); 118 void rt_hw_context_switch_to(rt_ubase_t to); 119 void rt_hw_context_switch_interrupt(rt_ubase_t from, rt_ubase_t to); 120 #endif /*RT_USING_SMP*/ 121 122 void rt_hw_console_output(const char *str); 123 124 void rt_hw_backtrace(rt_uint32_t *fp, rt_ubase_t thread_entry); 125 void rt_hw_show_memory(rt_uint32_t addr, rt_size_t size); 126 127 /* 128 * Exception interfaces 129 */ 130 void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context)); 131 132 /* 133 * delay interfaces 134 */ 135 void rt_hw_us_delay(rt_uint32_t us); 136 137 #ifdef RT_USING_SMP 138 typedef union { 139 unsigned long slock; 140 struct __arch_tickets { 141 unsigned short owner; 142 unsigned short next; 143 } tickets; 144 } rt_hw_spinlock_t; 145 146 void rt_hw_spin_lock(rt_hw_spinlock_t *lock); 147 void rt_hw_spin_unlock(rt_hw_spinlock_t *lock); 148 149 int rt_hw_cpu_id(void); 150 151 extern rt_hw_spinlock_t _cpus_lock; 152 extern rt_hw_spinlock_t _rt_critical_lock; 153 154 #define __RT_HW_SPIN_LOCK_INITIALIZER(lockname) {0} 155 156 #define __RT_HW_SPIN_LOCK_UNLOCKED(lockname) \ 157 (struct rt_hw_spinlock ) __RT_HW_SPIN_LOCK_INITIALIZER(lockname) 158 159 #define RT_DEFINE_SPINLOCK(x) struct rt_hw_spinlock x = __RT_HW_SPIN_LOCK_UNLOCKED(x) 160 161 /** 162 * ipi function 163 */ 164 void rt_hw_ipi_send(int ipi_vector, unsigned int cpu_mask); 165 166 /** 167 * boot secondary cpu 168 */ 169 void rt_hw_secondary_cpu_up(void); 170 171 /** 172 * secondary cpu idle function 173 */ 174 void rt_hw_secondary_cpu_idle_exec(void); 175 176 #endif 177 178 #ifdef __cplusplus 179 } 180 #endif 181 182 #endif 183