board.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright (c) 2020-2021, Bluetrum Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-11-18 greedyhao first version
  9. */
  10. #include <rthw.h>
  11. #include "board.h"
  12. int rt_hw_usart_init(void);
  13. void my_printf(const char *format, ...);
  14. void my_print_r(const void *buf, uint16_t cnt);
  15. void timer0_cfg(uint32_t ticks);
  16. void rt_soft_isr(int vector, void *param);
  17. void cpu_irq_comm(void);
  18. void set_cpu_irq_comm(void (*irq_hook)(void));
  19. void load_cache();
  20. void os_cache_init(void);
  21. void sys_error_hook(uint8_t err_no);
  22. void huart_timer_isr(void);
  23. typedef void (*spiflash_init_func)(uint8_t sf_read, uint8_t dummy);
  24. static struct rt_mutex mutex_spiflash = {0};
  25. static struct rt_mutex mutex_cache = {0};
  26. extern volatile rt_uint8_t rt_interrupt_nest;
  27. extern uint32_t __heap_start, __heap_end;
  28. #ifdef RT_USING_CONSOLE
  29. void hal_printf(const char *fmt, ...)
  30. {
  31. rt_device_t console = rt_console_get_device();
  32. va_list args;
  33. rt_size_t length;
  34. static char rt_log_buf[RT_CONSOLEBUF_SIZE];
  35. va_start(args, fmt);
  36. /* the return value of vsnprintf is the number of bytes that would be
  37. * written to buffer had if the size of the buffer been sufficiently
  38. * large excluding the terminating null byte. If the output string
  39. * would be larger than the rt_log_buf, we have to adjust the output
  40. * length. */
  41. length = rt_vsnprintf(rt_log_buf, sizeof(rt_log_buf) - 1, fmt, args);
  42. if (length > RT_CONSOLEBUF_SIZE - 1)
  43. length = RT_CONSOLEBUF_SIZE - 1;
  44. #ifdef RT_USING_DEVICE
  45. if (console == RT_NULL)
  46. {
  47. rt_hw_console_output(rt_log_buf);
  48. }
  49. else
  50. {
  51. rt_uint16_t old_flag = console->open_flag;
  52. console->open_flag |= RT_DEVICE_FLAG_STREAM;
  53. rt_device_write(console, 0, rt_log_buf, length);
  54. console->open_flag = old_flag;
  55. }
  56. #else
  57. rt_hw_console_output(rt_log_buf);
  58. #endif
  59. va_end(args);
  60. }
  61. #endif
  62. rt_section(".irq")
  63. void os_interrupt_enter(void)
  64. {
  65. rt_interrupt_enter();
  66. }
  67. rt_section(".irq")
  68. void os_interrupt_leave(void)
  69. {
  70. rt_interrupt_leave();
  71. }
  72. typedef void (*isr_t)(void);
  73. rt_section(".irq")
  74. isr_t register_isr(int vector, isr_t isr)
  75. {
  76. char buf[8] = {0};
  77. rt_snprintf(buf, sizeof(buf), "sys%d", vector);
  78. rt_isr_handler_t handle = (rt_isr_handler_t)isr;
  79. rt_hw_interrupt_install(vector, handle, RT_NULL, buf);
  80. }
  81. rt_section(".irq.timer")
  82. void timer0_isr(int vector, void *param)
  83. {
  84. rt_interrupt_enter();
  85. TMR0CPND = BIT(9);
  86. rt_tick_increase();
  87. #ifdef RT_USING_SERIAL
  88. huart_timer_isr();
  89. #endif
  90. rt_interrupt_leave();
  91. }
  92. void timer0_init(void)
  93. {
  94. TMR0CON = BIT(7); //TIE
  95. TMR0CNT = 0;
  96. rt_hw_interrupt_install(IRQ_TMR0_VECTOR, timer0_isr, RT_NULL, "tick");
  97. }
  98. void timer0_cfg(uint32_t ticks)
  99. {
  100. TMR0PR = (uint32_t)(ticks - 1UL); //1ms interrupt
  101. TMR0CON |= BIT(0); // EN
  102. }
  103. uint32_t hal_get_ticks(void)
  104. {
  105. return rt_tick_get();
  106. }
  107. void hal_mdelay(uint32_t nms)
  108. {
  109. rt_thread_mdelay(nms);
  110. }
  111. void hal_udelay(uint32_t nus)
  112. {
  113. rt_hw_us_delay(nus);
  114. }
  115. /**
  116. * The time delay function.
  117. *
  118. * @param us microseconds.
  119. */
  120. rt_section(".com_text")
  121. void rt_hw_us_delay(rt_uint32_t us)
  122. {
  123. rt_uint32_t ticks;
  124. rt_uint32_t told, tnow, tcnt = 0;
  125. rt_uint32_t reload = TMR0PR;
  126. ticks = us * reload / (1000 / RT_TICK_PER_SECOND);
  127. told = TMR0CNT;
  128. while (1)
  129. {
  130. tnow = TMR0CNT;
  131. if (tnow != told)
  132. {
  133. if (tnow < told)
  134. {
  135. tcnt += told - tnow;
  136. }
  137. else
  138. {
  139. tcnt += reload - tnow + told;
  140. }
  141. told = tnow;
  142. if (tcnt >= ticks)
  143. {
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. void rt_hw_systick_init(void)
  150. {
  151. CLKCON2 &= 0x00ffffff;
  152. CLKCON2 |= (25 << 24); //配置x26m_div_clk = 1M (timer, ir, fmam ...用到)
  153. CLKCON0 &= ~(7 << 23);
  154. CLKCON0 |= BIT(24); //tmr_inc select x26m_div_clk = 1M
  155. set_sysclk(SYSCLK_48M);
  156. /* Setting software interrupt */
  157. set_cpu_irq_comm(cpu_irq_comm);
  158. rt_hw_interrupt_install(IRQ_SW_VECTOR, rt_soft_isr, RT_NULL, "sw_irq");
  159. timer0_init();
  160. hal_set_tick_hook(timer0_cfg);
  161. hal_set_ticks(get_sysclk_nhz() / RT_TICK_PER_SECOND);
  162. PICCON |= 0x10002;
  163. }
  164. void rt_hw_board_init(void)
  165. {
  166. WDT_DIS();
  167. rt_hw_systick_init();
  168. #ifdef RT_USING_HEAP
  169. rt_system_heap_init(&__heap_start, &__heap_end);
  170. #endif
  171. #ifdef RT_USING_PIN
  172. rt_hw_pin_init();
  173. #endif // RT_USING_PIN
  174. #ifdef RT_USING_SERIAL
  175. rt_hw_usart_init();
  176. #endif // RT_USING_SERIAL
  177. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  178. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  179. #endif // RT_USING_CONSOLE
  180. #ifdef RT_USING_COMPONENTS_INIT
  181. rt_components_board_init();
  182. #endif
  183. }
  184. rt_section(".irq.cache")
  185. void cache_init(void)
  186. {
  187. os_cache_init();
  188. rt_mutex_init(&mutex_spiflash, "flash_mutex", RT_IPC_FLAG_PRIO);
  189. rt_mutex_init(&mutex_cache, "cache_mutex", RT_IPC_FLAG_PRIO);
  190. }
  191. rt_section(".irq.cache")
  192. void os_spiflash_lock(void)
  193. {
  194. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
  195. {
  196. rt_mutex_take(&mutex_spiflash, RT_WAITING_FOREVER);
  197. }
  198. }
  199. rt_section(".irq.cache")
  200. void os_spiflash_unlock(void)
  201. {
  202. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
  203. {
  204. rt_mutex_release(&mutex_spiflash);
  205. }
  206. }
  207. rt_section(".irq.cache")
  208. void os_cache_lock(void)
  209. {
  210. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
  211. {
  212. rt_mutex_take(&mutex_cache, RT_WAITING_FOREVER);
  213. }
  214. }
  215. rt_section(".irq.cache")
  216. void os_cache_unlock(void)
  217. {
  218. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0))
  219. {
  220. rt_mutex_release(&mutex_cache);
  221. }
  222. }
  223. rt_section(".irq.err.str")
  224. static const char stack_info[] = "thread sp=0x%x name=%s";
  225. void rt_hw_console_output(const char *str)
  226. {
  227. my_printf(str);
  228. }
  229. /**
  230. * @brief print exception error
  231. * @note Every message needed to print, must put in .comm exction.
  232. * @note (IRQ in Flash: %x %x - %x %x\n, -, rt_interrupt_nest, PC, miss_addr)
  233. * miss_addr: The address in map file minus 0x10000000
  234. */
  235. rt_section(".irq.err")
  236. void exception_isr(void)
  237. {
  238. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  239. extern long list_thread(void);
  240. #endif
  241. sys_error_hook(1);
  242. #if defined(RT_USING_CONSOLE) && defined(RT_USING_DEVICE)
  243. rt_console_set_device(RT_NULL);
  244. rt_kprintf(stack_info, rt_thread_self()->sp, rt_thread_self()->name);
  245. #endif
  246. while (1);
  247. }