retarget.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*******************************************************************************
  2. * (c) Copyright 2013 Microsemi SoC Products Group. All rights reserved.
  3. *
  4. * Redirection of the standard library I/O to one of the SmartFusion2
  5. * MMUART.
  6. *
  7. * SVN $Revision: 7375 $
  8. * SVN $Date: 2015-05-01 14:57:40 +0100 (Fri, 01 May 2015) $
  9. */
  10. /*==============================================================================
  11. * The content of this source file will only be compiled if either one of the
  12. * following two defined symbols are defined in the project settings:
  13. * - MICROSEMI_STDIO_THRU_MMUART0
  14. * - MICROSEMI_STDIO_THRU_MMUART1
  15. *
  16. */
  17. #ifdef MICROSEMI_STDIO_THRU_MMUART0
  18. #ifndef MICROSEMI_STDIO_THRU_UART
  19. #define MICROSEMI_STDIO_THRU_UART
  20. #endif
  21. #endif /* MICROSEMI_STDIO_THRU_MMUART0 */
  22. #ifdef MICROSEMI_STDIO_THRU_MMUART1
  23. #ifndef MICROSEMI_STDIO_THRU_UART
  24. #define MICROSEMI_STDIO_THRU_UART
  25. #endif
  26. #endif /* MICROSEMI_STDIO_THRU_MMUART1 */
  27. /*==============================================================================
  28. * Actual implementation.
  29. */
  30. #ifdef MICROSEMI_STDIO_THRU_UART
  31. #include <stdio.h>
  32. #include <rt_misc.h>
  33. #include "m2sxxx.h"
  34. #include "mss_uart.h"
  35. #include "core_uart_apb.h"
  36. /*
  37. * The baud rate will default to 57600 baud if no baud rate is specified though the
  38. * MICROSEMI_STDIO_BAUD_RATE define.
  39. */
  40. #ifndef MICROSEMI_STDIO_BAUD_RATE
  41. #define MICROSEMI_STDIO_BAUD_RATE MSS_UART_115200_BAUD
  42. #endif
  43. #ifdef MICROSEMI_STDIO_THRU_MMUART0
  44. static mss_uart_instance_t * const gp_my_uart = &g_mss_uart0;
  45. #else
  46. static mss_uart_instance_t * const gp_my_uart = &g_mss_uart1;
  47. #endif
  48. /*==============================================================================
  49. * Flag used to indicate if the UART driver needs to be initialized.
  50. */
  51. static int g_stdio_uart_init_done = 0;
  52. #define LSR_THRE_MASK 0x20u
  53. /*
  54. * Disable semihosting apis
  55. */
  56. #pragma import(__use_no_semihosting_swi)
  57. /*==============================================================================
  58. * sendchar()
  59. */
  60. int sendchar(int ch)
  61. {
  62. uint32_t tx_ready;
  63. //第一次调用时,初始化串口
  64. if(!g_stdio_uart_init_done)
  65. {
  66. MSS_UART_init(gp_my_uart,
  67. MICROSEMI_STDIO_BAUD_RATE,
  68. MSS_UART_DATA_8_BITS | MSS_UART_NO_PARITY);
  69. g_stdio_uart_init_done = 1;
  70. }
  71. do {
  72. tx_ready = gp_my_uart->hw_reg->LSR & LSR_THRE_MASK;
  73. } while(!tx_ready);
  74. gp_my_uart->hw_reg->THR = ch;
  75. return (ch);
  76. }
  77. /*==============================================================================
  78. *
  79. */
  80. struct __FILE { int handle; /* Add whatever you need here */ };
  81. FILE __stdout;
  82. FILE __stdin;
  83. /*==============================================================================
  84. * fputc()
  85. */
  86. int fputc(int ch, FILE *f)
  87. {
  88. return (sendchar(ch));
  89. }
  90. /*==============================================================================
  91. * fgetc()
  92. */
  93. int fgetc(FILE *f)
  94. {
  95. uint8_t rx_size;
  96. uint8_t rx_byte;
  97. do {
  98. rx_size = MSS_UART_get_rx(gp_my_uart, &rx_byte, 1);
  99. } while(0u == rx_size);
  100. return rx_byte;
  101. }
  102. /*==============================================================================
  103. * ferror()
  104. */
  105. int ferror(FILE *f)
  106. {
  107. /* Your implementation of ferror */
  108. return EOF;
  109. }
  110. /*==============================================================================
  111. * _ttywrch()
  112. */
  113. void _ttywrch(int ch)
  114. {
  115. sendchar(ch);
  116. }
  117. /*==============================================================================
  118. * _sys_exit()
  119. */
  120. void _sys_exit(int return_code)
  121. {
  122. for(;;)
  123. {
  124. ; /* endless loop */
  125. }
  126. }
  127. #endif /* MICROSEMI_STDIO_THRU_UART */