STM32_HAL_PRINTF_ATY.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @file STM32_HAL_PRINTF_ATY.c
  3. *
  4. * @param Project DEVICE_GENERAL_ATY_LIB
  5. *
  6. * @author ATY
  7. *
  8. * @copyright
  9. * - Copyright 2017 - 2023 MZ-ATY
  10. * - This code follows:
  11. * - MZ-ATY Various Contents Joint Statement -
  12. * <a href="https://mengze.top/MZ-ATY_VCJS">
  13. * https://mengze.top/MZ-ATY_VCJS</a>
  14. * - CC 4.0 BY-NC-SA -
  15. * <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/">
  16. * https://creativecommons.org/licenses/by-nc-sa/4.0/</a>
  17. * - Your use will be deemed to have accepted the terms of this statement.
  18. *
  19. * @brief Familiar functions of printf for STM32 device HAL lib
  20. *
  21. * @version
  22. * - 1_01_221029 > ATY
  23. * -# Preliminary version, first Release
  24. ********************************************************************************
  25. */
  26. #ifndef __STM32_HAL_PRINTF_ATY_C
  27. #define __STM32_HAL_PRINTF_ATY_C
  28. #include "STM32_HAL_PRINTF_ATY.h"
  29. #include "usart.h"
  30. /******************************* For user *************************************/
  31. #if defined(__STM32_HAL_ATY) && defined(DEBUG_PRINTF_RECEIVE)
  32. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t Size)
  33. {
  34. HAL_UART_DMAStop(&PRINTF_UART);
  35. uartPrintfMsgStruct.rxCount = PRINTF_RX_MAX_LEN - __HAL_DMA_GET_COUNTER(&PRINTF_DMA);
  36. uartPrintfMsgStruct.rcvOverFlag = 1;
  37. /* if process is not too long, process function can be used at this call back;
  38. otherwise put process at other cycle like while or timer cycle */
  39. // PrintfReceiveProcess();
  40. }
  41. #endif /* __STM32_HAL_ATY && DEBUG_PRINTF_RECEIVE */
  42. /******************************************************************************/
  43. #ifndef __MICROLIB
  44. // for standart lib(not use MicroLIB)
  45. #pragma import(__use_no_semihosting)
  46. // define _sys_exit to avoid semi-host mode
  47. void _sys_exit(int x)
  48. {
  49. x = x;
  50. }
  51. struct __FILE
  52. {
  53. int handle;
  54. };
  55. FILE __stdout;
  56. #endif /* __MICROLIB */
  57. /* In gcc, using printf() output, if there is no "\n" in output data, no data
  58. will be printed on the screen until "\n" is encountered or the buffer overflows
  59. Another way to refresh the output data is to run "fflush(stdout)" after
  60. sending the data to force a refresh of the output stream
  61. */
  62. #ifdef __GNUC__
  63. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  64. #define GETCHAR_PROTOTYPE int __io_getchar(void)
  65. #else
  66. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  67. #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
  68. #endif /* __GNUC__ */
  69. PUTCHAR_PROTOTYPE \
  70. {
  71. #ifdef LL
  72. LL_USART_TransmitData8(USART1, ch);
  73. while(!LL_USART_IsActiveFlag_TC(USART1)){}
  74. #else
  75. HAL_UART_Transmit(&PRINTF_UART, (uint8_t*)&ch, 1, 0xFFFF);
  76. #endif
  77. return ch;
  78. }
  79. GETCHAR_PROTOTYPE \
  80. {
  81. uint8_t ch = 0;
  82. HAL_UART_Receive(&PRINTF_UART, &ch, 1, 0xFFFF);
  83. return ch;
  84. }
  85. #ifdef DEBUG_PRINTF_RECEIVE
  86. uint8_t uartPrintfRxBuf[PRINTF_RX_MAX_LEN];
  87. struct _uartMsgStruct uartPrintfMsgStruct = {uartPrintfRxBuf};
  88. /**
  89. * @brief init uart IT and DMA etc.
  90. */
  91. void PrintfReceiveInit(void)
  92. {
  93. HAL_UARTEx_ReceiveToIdle_DMA(&PRINTF_UART, uartPrintfMsgStruct.rx, PRINTF_RX_MAX_LEN);
  94. __HAL_DMA_DISABLE_IT(&PRINTF_DMA, DMA_IT_HT);
  95. }
  96. /**
  97. * @brief uart receive data analysis
  98. * @note put at main while
  99. */
  100. __WEAK_ATY void PrintfReceiveProcess(void)
  101. {
  102. struct _uartMsgStruct* ptr = &uartPrintfMsgStruct;
  103. if(ptr->rcvOverFlag)
  104. {
  105. ptr->rcvOverFlag = 0;
  106. PrintfReceiveProcess_User(ptr);
  107. PrintfReceiveInit();
  108. }
  109. }
  110. __WEAK_ATY void PrintfReceiveProcess_User(struct _uartMsgStruct* ptr)
  111. {
  112. if(memcmp("DSTART", (char*)ptr->rx, strlen("DSTART")) == 0)
  113. {
  114. printf("\r\nDebug START!");
  115. }
  116. else if(memcmp("temp", (char*)ptr->rx, strlen("temp")) == 0)
  117. {
  118. printf("\r\ntemp %c%c%c",
  119. ptr->rx[strlen("temp_")],
  120. ptr->rx[strlen("temp_") + 1],
  121. ptr->rx[strlen("temp_") + 2]);
  122. }
  123. }
  124. #endif /* DEBUG_PRINTF_RECEIVE */
  125. #endif /* __STM32_HAL_PRINTF_ATY_C */
  126. /******************************** End Of File *********************************/