ac780x_debugout.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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) 2020. 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 ac780x_debugout.c
  35. *
  36. * @brief This file provides debug information output integration functions.
  37. *
  38. */
  39. /* =========================================== Includes =========================================== */
  40. #include "ac780x.h"
  41. #include "ac780x_gpio.h"
  42. #include "ac780x_uart.h"
  43. #include "ac780x_uart_reg.h"
  44. /* ============================================ Define ============================================ */
  45. #define MAX_DEBUG_BUFF_SIZE 100 /* define uart max receive buffer size */
  46. #define DEBUG_UART UART2 /* define uart2 for debug output */
  47. #define DEBUG_UART_IRQ UART2_IRQn /* define uart2 interrupt vector */
  48. #define DEBUG_UART_CLK CLK_UART2 /* define uart2 ckgen clock value */
  49. #define DEBUG_UART_SRST SRST_UART2 /* define uart2 ckgen reset value */
  50. #define DEBUG_UART_SMP 16.0F
  51. #define DEBUG_UART_BAUDRATE 115200.0F
  52. #define DEBUG_UART_TX GPIOB,GPIO_PIN9 /* define uart2 tx gpio */
  53. #define DEBUG_UART_RX GPIOB,GPIO_PIN10 /* define uart2 rx gpio */
  54. /* =========================================== Typedef ============================================ */
  55. /* ========================================== Variables =========================================== */
  56. /* debug uart receive buf */
  57. uint8_t g_debugBuff[MAX_DEBUG_BUFF_SIZE] = {0};
  58. /* debug init flag */
  59. static uint8_t s_debugInit = 0;
  60. /* define std FILE struct */
  61. struct __FILE
  62. {
  63. int handle;
  64. };
  65. /* refine stdout,stdin,stderr */
  66. FILE __stdout;
  67. FILE __stdin;
  68. FILE __stderr;
  69. /* ==================================== Functions declaration ===================================== */
  70. /* ====================================== Functions define ======================================== */
  71. /*!
  72. * @brief refine _sys_exit
  73. *
  74. * @param[in] x: no use
  75. * @return always 0
  76. */
  77. int _sys_exit(int x)
  78. {
  79. x = x;
  80. return 0;
  81. }
  82. /*!
  83. * @brief refine _ttywrch
  84. *
  85. * @param[in] x: no use
  86. * @return always 0
  87. */
  88. int _ttywrch(int x)
  89. {
  90. x = x;
  91. return 0;
  92. }
  93. /*!
  94. * @brief put a char to UART
  95. *
  96. * @param[in] f: file pointer for the std input
  97. * @param[in] ch: the char to put
  98. * @return return the char of be put
  99. */
  100. int fputc(int ch, FILE *f)
  101. {
  102. if (s_debugInit)
  103. {
  104. UART_SendData(DEBUG_UART, ch);
  105. }
  106. return ch;
  107. }
  108. /*!
  109. * @brief get a char
  110. *
  111. * @param[in] f: file pointer for the std input
  112. * @return -1: not get char, other: the char get from UART
  113. */
  114. int fgetc(FILE* f)
  115. {
  116. int ch = -1;
  117. if (s_debugInit)
  118. {
  119. if (UART_RxIsDataReady(DEBUG_UART))
  120. {
  121. ch = UART_ReceiveData(DEBUG_UART);
  122. }
  123. }
  124. return ch;
  125. }
  126. /*!
  127. * @brief set the debug out is invalid
  128. *
  129. * @param[in] none
  130. * @return none
  131. */
  132. void DeinitDebug(void)
  133. {
  134. s_debugInit = 0;
  135. }
  136. /*!
  137. * @brief init debug out , and set the debug out is valid
  138. *
  139. * @param[in] none
  140. * @return none
  141. */
  142. void InitDebug(void)
  143. {
  144. #ifdef DEBUG_CMD_INTERRUPT
  145. NVIC_SetPriority(DEBUG_UART_IRQ, 3);
  146. NVIC_ClearPendingIRQ(DEBUG_UART_IRQ);
  147. NVIC_EnableIRQ(DEBUG_UART_IRQ);
  148. #endif
  149. GPIO_SetFunc(DEBUG_UART_TX, GPIO_FUN3);
  150. GPIO_SetFunc(DEBUG_UART_RX, GPIO_FUN3);
  151. CKGEN_Enable(DEBUG_UART_CLK, ENABLE);
  152. CKGEN_SoftReset(DEBUG_UART_SRST, ENABLE);
  153. UART_SetDivisor(DEBUG_UART, (float)APB_BUS_FREQ / DEBUG_UART_SMP / DEBUG_UART_BAUDRATE);
  154. UART_SetDataBits(DEBUG_UART, UART_WORD_LEN_8BIT);
  155. UART_SetStopBit(DEBUG_UART, UART_STOP_1BIT);
  156. UART_EnableTX(DEBUG_UART, ENABLE);
  157. UART_EnableRX(DEBUG_UART, ENABLE);
  158. UART_Set2ByteFIFO(DEBUG_UART, ENABLE);
  159. UART_SetInterruptEn(DEBUG_UART, 9);
  160. s_debugInit = 1;
  161. }
  162. /* ============================================= EOF ============================================== */