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.

321 lines
8.1 KiB

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