rtt_port.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. * Copyright (c) 2021 - 2024 HPMicro
  3. * SPDX-License-Identifier: BSD-3-Clause
  4. *
  5. */
  6. #include "rtt_port.h"
  7. #include "board.h"
  8. #include "hpm_uart_drv.h"
  9. #include "hpm_mchtmr_drv.h"
  10. #include "hpm_ppor_drv.h"
  11. #include "rtconfig.h"
  12. #include <rthw.h>
  13. #include <rtthread.h>
  14. void os_tick_config(void);
  15. void rt_console_config(void);
  16. #ifdef RT_USING_HEAP
  17. #ifndef RT_HEAP_SIZE
  18. #define RT_HEAP_SIZE (16 * 1024)
  19. #endif
  20. static uint8_t rt_heap[RT_HEAP_SIZE];
  21. #endif
  22. __attribute__((weak)) void rt_hw_board_init(void)
  23. {
  24. board_init();
  25. rtt_base_init();
  26. }
  27. void rtt_base_init(void)
  28. {
  29. /* initialize memory system */
  30. #ifdef RT_USING_HEAP
  31. rt_system_heap_init(rt_heap, rt_heap + RT_HEAP_SIZE);
  32. #endif
  33. /* Configure the OS Tick */
  34. os_tick_config();
  35. #ifdef RT_USING_CONSOLE
  36. rt_console_config();
  37. #endif
  38. #ifdef RT_USING_COMPONENTS_INIT
  39. rt_components_board_init();
  40. #endif
  41. }
  42. void os_tick_config(void)
  43. {
  44. mchtmr_delay(HPM_MCHTMR, RT_MCHTMR_FREQ_IN_HZ / RT_TICK_PER_SECOND);
  45. enable_mchtmr_irq();
  46. }
  47. void rt_hw_console_output(const char *str)
  48. {
  49. while (*str != '\0')
  50. {
  51. if (*str == '\n') {
  52. while (status_success != uart_send_byte(BOARD_RT_CONSOLE_BASE, '\r')) {
  53. }
  54. }
  55. uart_send_byte(BOARD_RT_CONSOLE_BASE, *str++);
  56. }
  57. }
  58. #ifdef RT_USING_FINSH
  59. struct rt_finsh_rb {
  60. uint32_t wr;
  61. uint32_t rd;
  62. uint8_t buffer[FINSH_RX_BUF_SIZE];
  63. };
  64. static struct rt_finsh_rb finsh_rx_rb;
  65. static struct rt_semaphore finsh_rx_sem;
  66. void rt_finsh_rx_write(struct rt_finsh_rb *rb, char *data)
  67. {
  68. rb->buffer[rb->wr] = *data;
  69. rb->wr = (rb->wr + 1) % FINSH_RX_BUF_SIZE;
  70. if (rb->wr == rb->rd) {
  71. rb->rd = (rb->rd + 1) % FINSH_RX_BUF_SIZE;
  72. }
  73. }
  74. int rt_finsh_rx_read(struct rt_finsh_rb *rb, char *data)
  75. {
  76. if (rb->wr == rb->rd)
  77. return -1;
  78. *data = rb->buffer[rb->rd];
  79. rb->rd = (rb->rd + 1) % FINSH_RX_BUF_SIZE;
  80. return 0;
  81. }
  82. char rt_hw_console_getchar(void)
  83. {
  84. char ch = -1;
  85. while (rt_finsh_rx_read(&finsh_rx_rb, &ch) != 0) {
  86. rt_sem_take(&finsh_rx_sem, RT_WAITING_FOREVER);
  87. }
  88. return ch;
  89. }
  90. SDK_DECLARE_EXT_ISR_M(BOARD_RT_CONSOLE_IRQ, rt_finsh_uart_isr);
  91. void rt_finsh_uart_isr(void)
  92. {
  93. char ch = -1;
  94. uint8_t irq_id = uart_get_irq_id(BOARD_RT_CONSOLE_BASE);
  95. rt_interrupt_enter();
  96. if (irq_id == uart_intr_id_rx_data_avail) {
  97. uart_receive_byte(BOARD_RT_CONSOLE_BASE, (uint8_t *)&ch);
  98. rt_finsh_rx_write(&finsh_rx_rb, &ch);
  99. rt_sem_release(&finsh_rx_sem);
  100. }
  101. rt_interrupt_leave();
  102. }
  103. #endif /* RT_USING_FINSH */
  104. #ifdef RT_USING_CONSOLE
  105. void rt_console_config(void)
  106. {
  107. hpm_stat_t stat;
  108. uart_config_t config = {0};
  109. init_uart_pins((UART_Type *) BOARD_RT_CONSOLE_BASE);
  110. clock_add_to_group(BOARD_RT_CONSOLE_CLK_NAME, 0);
  111. uart_default_config(BOARD_RT_CONSOLE_BASE, &config);
  112. config.baudrate = RT_CONSOLE_BAUDRATE;
  113. config.fifo_enable = false;
  114. config.src_freq_in_hz = clock_get_frequency(BOARD_RT_CONSOLE_CLK_NAME);
  115. stat = uart_init(BOARD_RT_CONSOLE_BASE, &config);
  116. if (stat != status_success) {
  117. rt_kprintf("failed to initialize uart\n");
  118. while (1) {
  119. }
  120. }
  121. #ifdef RT_USING_FINSH
  122. rt_sem_init(&finsh_rx_sem, "finsh_rx", 0, RT_IPC_FLAG_FIFO);
  123. uart_enable_irq(BOARD_RT_CONSOLE_BASE, uart_intr_rx_data_avail_or_timeout);
  124. uart_disable_irq(BOARD_RT_CONSOLE_BASE, uart_intr_tx_slot_avail);
  125. intc_m_enable_irq_with_priority(BOARD_RT_CONSOLE_IRQ, 1);
  126. #endif
  127. }
  128. #endif/* RT_USING_CONSOLE */
  129. SDK_DECLARE_MCHTMR_ISR(isr_mchtmr)
  130. void isr_mchtmr(void)
  131. {
  132. mchtmr_delay(HPM_MCHTMR, RT_MCHTMR_FREQ_IN_HZ / RT_TICK_PER_SECOND);
  133. rt_tick_increase();
  134. }
  135. void rt_hw_cpu_reset(void)
  136. {
  137. ppor_reset_mask_set_source_enable(HPM_PPOR, ppor_reset_software);
  138. /* reset after 1us */
  139. ppor_sw_reset(HPM_PPOR, 24);
  140. while(1) {
  141. }
  142. }
  143. MSH_CMD_EXPORT_ALIAS(rt_hw_cpu_reset, reset, reset the board);
  144. /**
  145. * @brief halt cpu when exception occur
  146. *
  147. * @param cause mcause
  148. * @param epc mepc
  149. * @return long
  150. */
  151. long exception_handler(long cause, long epc)
  152. {
  153. (void)cause;
  154. while (1) {
  155. };
  156. return epc;
  157. }