You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

320 lines
8.1 KiB

4 years ago
  1. /*********************************************************
  2. *Copyright (C), 2015, Shanghai Eastsoft Microelectronics Co., Ltd.
  3. *: lib_printf.c
  4. * : AE Team
  5. * : V1.01
  6. * : 2021/06/09
  7. * : Printf功能库函数
  8. * :
  9. 使
  10. **********************************************************/
  11. #include "lib_printf.h"
  12. #include <stdarg.h>
  13. /***************************************************************
  14. fputc
  15. c库函数printf到UART
  16. ***************************************************************/
  17. /*使用printf()函数需要调用微库:Use MicroLIB*/
  18. int fputc(int ch, FILE *f)
  19. {
  20. uint32_t count = 0;
  21. FlagStatus status = RESET;
  22. #if defined __PRINTF_USE_UART3__
  23. UART_SendByte(UART3, (unsigned char) ch);
  24. do
  25. {
  26. status = UART_GetFlagStatus(UART3, UART_FLAG_TB);
  27. count++;
  28. }
  29. while ((status == RESET) && (count != 0x1CE2));
  30. UART_ClearITPendingBit(UART3, UART_FLAG_TC);
  31. if (count == 0x1CE2)
  32. return -1;
  33. #elif defined __PRINTF_USE_UART2__
  34. UART_SendByte(UART2, (unsigned char) ch);
  35. do
  36. {
  37. status = UART_GetFlagStatus(UART2, UART_FLAG_TC);
  38. count++;
  39. }
  40. while ((status == RESET) && (count != 0x1CE2));
  41. UART_ClearITPendingBit(UART2, UART_FLAG_TC);
  42. if (count == 0x1CE2)
  43. return -1;
  44. #elif defined __PRINTF_USE_UART1__
  45. UART_SendByte(UART1, (unsigned char) ch);
  46. do
  47. {
  48. status = UART_GetFlagStatus(UART1, UART_FLAG_TC);
  49. count++;
  50. }
  51. while ((status == RESET) && (count != 0x1CE2));
  52. UART_ClearITPendingBit(UART1, UART_FLAG_TC);
  53. if (count == 0x1CE2)
  54. return -1;
  55. #else
  56. UART_SendByte(UART0, (unsigned char) ch);
  57. do
  58. {
  59. status = UART_GetFlagStatus(UART0, UART_FLAG_TC);
  60. count++;
  61. }
  62. while ((status == RESET) && (count != 0x1CE2));
  63. UART_ClearITPendingBit(UART0, UART_FLAG_TC);
  64. if (count == 0x1CE2)
  65. return -1;
  66. #endif
  67. return (ch);
  68. }
  69. #ifdef __clang__ /* 当使用的是idesigner编译器时,则不调用微库 */
  70. /***************************************************************
  71. itoa
  72. radix =10 100
  73. value
  74. buf
  75. radix = 10
  76. ***************************************************************/
  77. static char *itoa(int value, char *string, int radix)
  78. {
  79. int i, d;
  80. int flag = 0;
  81. char *ptr = string;
  82. /* This implementation only works for decimal numbers. */
  83. if (radix != 10)
  84. {
  85. *ptr = 0;
  86. return string;
  87. }
  88. if (!value)
  89. {
  90. *ptr++ = 0x30;
  91. *ptr = 0;
  92. return string;
  93. }
  94. /* if this is a negative value insert the minus sign. */
  95. if (value < 0)
  96. {
  97. *ptr++ = '-';
  98. /* Make the value positive. */
  99. value *= -1;
  100. }
  101. for (i = 10000; i > 0; i /= 10)
  102. {
  103. d = value / i;
  104. if (d || flag)
  105. {
  106. *ptr++ = (char)(d + 0x30);
  107. value -= (d * i);
  108. flag = 1;
  109. }
  110. }
  111. /* Null terminate the string. */
  112. *ptr = 0;
  113. return string;
  114. }
  115. /***************************************************************
  116. 使idesigner时调用C库中的printfC库
  117. UARTx
  118. Data
  119. ...
  120. UART_printf("\r\n this is a demo \r\n" );
  121. UART_printf( "\r\n %d \r\n", i );
  122. UART_printf( "\r\n %s \r\n", j );
  123. ***************************************************************/
  124. /* 未调用C库的时候可以使用此函数代替C库中的printf,但功能无printf全,
  125. \r \n %d %s */
  126. ErrorStatus UART_printf(uint8_t *Data, ...)
  127. {
  128. UART_TypeDef *UARTx;
  129. const char *s;
  130. int d;
  131. char buf[16];
  132. uint32_t Count = 0;
  133. ErrorStatus RET = SUCCESS;
  134. FlagStatus Status = RESET;
  135. va_list ap;
  136. /**
  137. **
  138. **使使
  139. **
  140. ***/
  141. #if defined __PRINTF_USE_UART3__
  142. UARTx = UART3;
  143. #elif defined __PRINTF_USE_UART2__
  144. UARTx = UART2;
  145. #elif defined __PRINTF_USE_UART1__
  146. UARTx = UART1;
  147. #else
  148. UARTx = UART0;
  149. #endif
  150. va_start(ap, Data);
  151. while (*Data != 0) /* 判断是否到达字符串结束符 */
  152. {
  153. if (*Data == 0x5c) /* '\' */
  154. {
  155. switch (*++Data)
  156. {
  157. case 'r': /* 回车符 */
  158. Count = 0;
  159. UART_SendByte(UARTx, 0x0d);
  160. do
  161. {
  162. Status = UART_GetFlagStatus(UARTx, UART_FLAG_TB);
  163. Count++;
  164. }
  165. while ((Status == RESET) && (Count != 0x1CE2));
  166. if (Count == 0x1CE2)
  167. RET = ERROR;
  168. ++Data;
  169. break;
  170. case 'n': /* 换行符 */
  171. Count = 0;
  172. UART_SendByte(UARTx, 0x0a);
  173. do
  174. {
  175. Status = UART_GetFlagStatus(UARTx, UART_FLAG_TB);
  176. Count++;
  177. }
  178. while ((Status == RESET) && (Count != 0x1CE2));
  179. if (Count == 0x1CE2)
  180. RET = ERROR;
  181. ++Data;
  182. break;
  183. default:
  184. ++Data;
  185. break;
  186. }
  187. }
  188. else if (*Data == '%')
  189. {
  190. switch (*++Data)
  191. {
  192. case 's': /* 字符串 */
  193. s = va_arg(ap, const char *);
  194. for (; *s; s++)
  195. {
  196. Count = 0;
  197. UART_SendByte(UARTx, *s);
  198. do
  199. {
  200. Status = UART_GetFlagStatus(UARTx, UART_FLAG_TB);
  201. Count++;
  202. }
  203. while ((Status == RESET) && (Count != 0x1CE2));
  204. if (Count == 0x1CE2)
  205. RET = ERROR;
  206. }
  207. ++Data;
  208. break;
  209. case 'd': /* 十进制 */
  210. d = va_arg(ap, int);
  211. itoa(d, buf, 10);
  212. for (s = buf; *s; s++)
  213. {
  214. Count = 0;
  215. UART_SendByte(UARTx, *s);
  216. do
  217. {
  218. Status = UART_GetFlagStatus(UARTx, UART_FLAG_TB);
  219. Count++;
  220. }
  221. while ((Status == RESET) && (Count != 0x1CE2));
  222. if (Count == 0x1CE2)
  223. RET = ERROR;
  224. }
  225. ++Data;
  226. break;
  227. default:
  228. ++Data;
  229. break;
  230. }
  231. }
  232. else
  233. {
  234. Count = 0;
  235. UART_SendByte(UARTx, *Data++);
  236. do
  237. {
  238. Status = UART_GetFlagStatus(UARTx, UART_FLAG_TB);
  239. Count++;
  240. }
  241. while ((Status == RESET) && (Count != 0x1CE2));
  242. if (Count == 0x1CE2)
  243. RET = ERROR;
  244. }
  245. }
  246. return RET;
  247. }
  248. #endif