board.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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.timer")
  63. void timer0_isr(int vector, void *param)
  64. {
  65. rt_interrupt_enter();
  66. TMR0CPND = BIT(9);
  67. rt_tick_increase();
  68. #ifdef RT_USING_SERIAL
  69. huart_timer_isr();
  70. #endif
  71. rt_interrupt_leave();
  72. }
  73. void timer0_init(void)
  74. {
  75. TMR0CON = BIT(7); //TIE
  76. TMR0CNT = 0;
  77. rt_hw_interrupt_install(IRQ_TMR0_VECTOR, timer0_isr, RT_NULL, "tick");
  78. }
  79. void timer0_cfg(uint32_t ticks)
  80. {
  81. TMR0PR = (uint32_t)(ticks - 1UL); //1ms interrupt
  82. TMR0CON |= BIT(0); // EN
  83. }
  84. void hal_mdelay(uint32_t ms)
  85. {
  86. rt_thread_mdelay(ms);
  87. }
  88. void rt_hw_systick_init(void)
  89. {
  90. CLKCON2 &= 0x00ffffff;
  91. CLKCON2 |= (25 << 24); //配置x26m_div_clk = 1M (timer, ir, fmam ...用到)
  92. CLKCON0 &= ~(7 << 23);
  93. CLKCON0 |= BIT(24); //tmr_inc select x26m_div_clk = 1M
  94. set_sysclk(SYSCLK_48M);
  95. /* Setting software interrupt */
  96. set_cpu_irq_comm(cpu_irq_comm);
  97. rt_hw_interrupt_install(IRQ_SW_VECTOR, rt_soft_isr, RT_NULL, "sw_irq");
  98. timer0_init();
  99. hal_set_tick_hook(timer0_cfg);
  100. hal_set_ticks(get_sysclk_nhz()/RT_TICK_PER_SECOND);
  101. PICCON |= 0x10002;
  102. }
  103. void rt_hw_board_init(void)
  104. {
  105. WDT_DIS();
  106. rt_hw_systick_init();
  107. #ifdef RT_USING_HEAP
  108. rt_system_heap_init(&__heap_start, &__heap_end);
  109. #endif
  110. #ifdef RT_USING_PIN
  111. rt_hw_pin_init();
  112. #endif // RT_USING_PIN
  113. #ifdef RT_USING_SERIAL
  114. rt_hw_usart_init();
  115. #endif // RT_USING_SERIAL
  116. #ifdef RT_USING_CONSOLE
  117. rt_console_set_device(RT_CONSOLE_DEVICE_NAME);
  118. #endif // RT_USING_CONSOLE
  119. #ifdef RT_USING_COMPONENTS_INIT
  120. rt_components_board_init();
  121. #endif
  122. }
  123. void rt_hw_us_delay(rt_uint32_t us)
  124. {
  125. }
  126. RT_SECTION(".irq.cache")
  127. void cache_init(void)
  128. {
  129. os_cache_init();
  130. rt_mutex_init(&mutex_spiflash, "flash_mutex", RT_IPC_FLAG_FIFO);
  131. rt_mutex_init(&mutex_cache, "cache_mutex", RT_IPC_FLAG_FIFO);
  132. }
  133. RT_SECTION(".irq.cache")
  134. void os_spiflash_lock(void)
  135. {
  136. // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
  137. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
  138. rt_mutex_take(&mutex_spiflash, RT_WAITING_FOREVER);
  139. }
  140. }
  141. RT_SECTION(".irq.cache")
  142. void os_spiflash_unlock(void)
  143. {
  144. // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
  145. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
  146. rt_mutex_release(&mutex_spiflash);
  147. }
  148. }
  149. RT_SECTION(".irq.cache")
  150. void os_cache_lock(void)
  151. {
  152. // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
  153. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
  154. rt_mutex_take(&mutex_cache, RT_WAITING_FOREVER);
  155. }
  156. }
  157. RT_SECTION(".irq.cache")
  158. void os_cache_unlock(void)
  159. {
  160. // if (rt_thread_self()->stat == RT_THREAD_RUNNING) {
  161. if ((rt_thread_self() != RT_NULL) && (rt_interrupt_nest == 0)) {
  162. rt_mutex_release(&mutex_cache);
  163. }
  164. }
  165. RT_SECTION(".irq.err.str")
  166. static const char stack_info[] = "thread sp=0x%x name=%s";
  167. void rt_hw_console_output(const char *str)
  168. {
  169. my_printf(str);
  170. }
  171. /**
  172. * @brief print exception error
  173. * @note Every message needed to print, must put in .comm exction.
  174. */
  175. RT_SECTION(".irq.err")
  176. void exception_isr(void)
  177. {
  178. #if defined(RT_USING_FINSH) && defined(MSH_USING_BUILT_IN_COMMANDS)
  179. extern long list_thread(void);
  180. #endif
  181. sys_error_hook(1);
  182. #ifdef RT_USING_CONSOLE
  183. rt_console_set_device(RT_NULL);
  184. rt_kprintf(stack_info, rt_thread_self()->sp, rt_thread_self()->name);
  185. #endif
  186. while(1);
  187. }