debugout_ac7840x.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. /* Copyright Statement:
  2. *
  3. * This software/firmware and related documentation ("AutoChips Software") are
  4. * protected under relevant copyright laws. The information contained herein is
  5. * confidential and proprietary to AutoChips Inc. and/or its licensors. Without
  6. * the prior written permission of AutoChips inc. and/or its licensors, any
  7. * reproduction, modification, use or disclosure of AutoChips Software, and
  8. * information contained herein, in whole or in part, shall be strictly
  9. * prohibited.
  10. *
  11. * AutoChips Inc. (C) 2021. All rights reserved.
  12. *
  13. * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
  14. * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("AUTOCHIPS SOFTWARE")
  15. * RECEIVED FROM AUTOCHIPS AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER
  16. * ON AN "AS-IS" BASIS ONLY. AUTOCHIPS EXPRESSLY DISCLAIMS ANY AND ALL
  17. * WARRANTIES, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
  19. * NONINFRINGEMENT. NEITHER DOES AUTOCHIPS PROVIDE ANY WARRANTY WHATSOEVER WITH
  20. * RESPECT TO THE SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY,
  21. * INCORPORATED IN, OR SUPPLIED WITH THE AUTOCHIPS SOFTWARE, AND RECEIVER AGREES
  22. * TO LOOK ONLY TO SUCH THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO.
  23. * RECEIVER EXPRESSLY ACKNOWLEDGES THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO
  24. * OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES CONTAINED IN AUTOCHIPS
  25. * SOFTWARE. AUTOCHIPS SHALL ALSO NOT BE RESPONSIBLE FOR ANY AUTOCHIPS SOFTWARE
  26. * RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
  27. * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND AUTOCHIPS'S
  28. * ENTIRE AND CUMULATIVE LIABILITY WITH RESPECT TO THE AUTOCHIPS SOFTWARE
  29. * RELEASED HEREUNDER WILL BE, AT AUTOCHIPS'S OPTION, TO REVISE OR REPLACE THE
  30. * AUTOCHIPS SOFTWARE AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE
  31. * CHARGE PAID BY RECEIVER TO AUTOCHIPS FOR SUCH AUTOCHIPS SOFTWARE AT ISSUE.
  32. */
  33. /*!
  34. * @file debugout_ac7840x.c
  35. *
  36. * @brief This file provides debug information output integration functions.
  37. *
  38. */
  39. /* =========================================== Includes =========================================== */
  40. #include "device_register.h"
  41. #include "gpio_drv.h"
  42. #include "ckgen_drv.h"
  43. #include "debugout_ac7840x.h"
  44. #include "../src/uart/uart_hw.h"
  45. /* ============================================ Define ============================================ */
  46. #define MAX_DEBUG_BUFF_SIZE 100UL /* define uart max receive buffer size */
  47. #define DEBUG_INSTANCE 1UL
  48. #define DEBUG_UART UART1 /* define uart1 for debug output */
  49. #define DEBUG_UART_IRQ UART1_IRQn /* define uart1 interrupt vector */
  50. #define DEBUG_UART_CLK CLK_UART1 /* define uart1 ckgen clock value */
  51. #define DEBUG_UART_SRST SRST_UART1 /* define uart1 ckgen reset value */
  52. #define DEBUG_UART_BAUDRATE 115200UL
  53. #if 0
  54. #define DEBUG_UART_TX_PIN PORTB, 22UL /* define uart1 tx gpio */
  55. #define DEBUG_UART_RX_PIN PORTB, 23UL /* define uart1 rx gpio */
  56. #define DEBUG_UART_TX_FUNC PORT_MUX_ALT5 /* define uart1 tx pinmux */
  57. #define DEBUG_UART_RX_FUNC PORT_MUX_ALT3 /* define uart1 rx pinmux */
  58. #else
  59. #define DEBUG_UART_TX_PIN PORTC, 9UL /* define uart1 tx gpio */
  60. #define DEBUG_UART_RX_PIN PORTC, 8UL /* define uart1 rx gpio */
  61. #define DEBUG_UART_TX_FUNC PORT_MUX_ALT2 /* define uart1 tx pinmux */
  62. #define DEBUG_UART_RX_FUNC PORT_MUX_ALT2 /* define uart1 rx pinmux */
  63. #endif
  64. /* =========================================== Typedef ============================================ */
  65. /* ========================================== Variables =========================================== */
  66. /* debug uart receive buf */
  67. uint8_t g_debugBuff[MAX_DEBUG_BUFF_SIZE] = {0U};
  68. /* debug init flag */
  69. static uint8_t s_debugInit = 0U;
  70. /* define std FILE struct */
  71. struct __FILE
  72. {
  73. int handle;
  74. };
  75. /* refine stdout,stdin,stderr */
  76. FILE __stdout;
  77. FILE __stdin;
  78. FILE __stderr;
  79. /* ==================================== Functions declaration ===================================== */
  80. /* ====================================== Functions define ======================================== */
  81. /*!
  82. * @brief Refine _sys_exit.
  83. *
  84. * @param[in] x: no use
  85. * @return always 0
  86. */
  87. int _sys_exit(int x)
  88. {
  89. x = x;
  90. (void)x;
  91. return 0;
  92. }
  93. /*!
  94. * @brief Refine _ttywrch.
  95. *
  96. * @param[in] x: no use
  97. * @return always 0
  98. */
  99. int _ttywrch(int x)
  100. {
  101. x = x;
  102. (void)x;
  103. return 0;
  104. }
  105. #ifdef ITM_DEBUG_OUT
  106. /*!
  107. * @brief Put a char to ITM.
  108. *
  109. * @param[in] ch: the char to put
  110. * @param[in] f: file pointer for the std input
  111. * @return return the char of be put
  112. */
  113. int fputc(int ch, FILE *f)
  114. {
  115. if (s_debugInit)
  116. {
  117. ITM_SendChar(ch);
  118. }
  119. return ch;
  120. }
  121. /* for ITM use */
  122. volatile int ITM_RxBuffer;
  123. /*!
  124. * @brief Get a char from ITM.
  125. *
  126. * @param[in] f: file pointer for the std input
  127. * @return -1: not get char, other: the char get from ITM
  128. */
  129. int fgetc(FILE *f)
  130. {
  131. int ch = ITM_ReceiveChar();
  132. return ch;
  133. }
  134. #else
  135. /*!
  136. * @brief Put a char to UART.
  137. *
  138. * @param[in] ch: the char to put
  139. * @param[in] f: file pointer for the std input
  140. * @return return the char of be put
  141. */
  142. int fputc(int ch, FILE *f)
  143. {
  144. if (s_debugInit)
  145. {
  146. while (!UART_GetStatusFlag(DEBUG_UART, UART_TX_DATA_NOT_FULL));
  147. UART_Putchar(DEBUG_UART, (uint8_t)ch);
  148. }
  149. return ch;
  150. }
  151. /*!
  152. * @brief Get a char from UART.
  153. *
  154. * @param[in] f: file pointer for the std input
  155. * @return -1: not get char, other: the char get from UART
  156. */
  157. int fgetc(FILE *f)
  158. {
  159. int ch = -1;
  160. if (s_debugInit)
  161. {
  162. if (UART_GetStatusFlag(DEBUG_UART, UART_RX_DATA_READY))
  163. {
  164. UART_Getchar(DEBUG_UART, (uint8_t *)&ch);
  165. ch &= 0xFF;
  166. }
  167. }
  168. return ch;
  169. }
  170. #endif /* ITM_DEBUG_OUT */
  171. /*!
  172. * @brief Set the debug out is invalid.
  173. *
  174. * @param[in] none
  175. * @return none
  176. */
  177. void DeinitDebug(void)
  178. {
  179. s_debugInit = 0U;
  180. }
  181. /*!
  182. * @brief Init debug out, and set the debug out is valid.
  183. *
  184. * @param[in] none
  185. * @return none
  186. */
  187. void InitDebug(void)
  188. {
  189. #ifndef ITM_DEBUG_OUT
  190. #ifdef DEBUG_CMD_INTERRUPT
  191. NVIC_SetPriority(DEBUG_UART_IRQ, 3U);
  192. NVIC_ClearPendingIRQ(DEBUG_UART_IRQ);
  193. NVIC_EnableIRQ(DEBUG_UART_IRQ);
  194. #endif /* DEBUG_CMD_INTERRUPT */
  195. /* Set pinmux */
  196. GPIO_DRV_SetMuxModeSel(DEBUG_UART_TX_PIN, DEBUG_UART_TX_FUNC);
  197. GPIO_DRV_SetMuxModeSel(DEBUG_UART_RX_PIN, DEBUG_UART_RX_FUNC);
  198. /* Check UART function clock */
  199. if (0U == (CKGEN->PERI_CLK_MUX2 & \
  200. (CKGEN_PERI_CLK_MUX2_UART0_MUX_Msk << ((uint32_t)DEBUG_UART_CLK * 4U))))
  201. {
  202. /* Default set clock to HSIDIV2 */
  203. CKGEN->PERI_CLK_MUX2 |= 5U << ((uint32_t)DEBUG_UART_CLK * 4U);
  204. }
  205. /* Enable UART bus clock and release UART module from reset status */
  206. CKGEN_DRV_Enable(DEBUG_UART_CLK, true);
  207. CKGEN_DRV_SoftReset(DEBUG_UART_SRST, true);
  208. /* Set the baudrate */
  209. (void)UART_DRV_SetBaudRate(DEBUG_INSTANCE, DEBUG_UART_BAUDRATE);
  210. UART_SetBitCountPerChar(DEBUG_UART, UART_8_BITS_PER_CHAR);
  211. UART_SetStopBitCount(DEBUG_UART, UART_ONE_STOP_BIT);
  212. UART_SetParityMode(DEBUG_UART, UART_PARITY_DISABLED);
  213. UART_SetFIFO(DEBUG_UART, true);
  214. /* Enable UART TX and RX */
  215. UART_SetTransmitterCmd(DEBUG_UART, true);
  216. UART_SetReceiverCmd(DEBUG_UART, true);
  217. #endif /* ITM_DEBUG_OUT */
  218. s_debugInit = 1U;
  219. }
  220. /* ============================================= EOF ============================================== */