HW_UART_ATY.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /**
  2. * @file HW_UART_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 uart for STC51
  20. *
  21. * @version
  22. * - 1_01_221231 > ATY
  23. * -# Preliminary version, first Release
  24. * -Undone: over with "\r\n" type
  25. ********************************************************************************
  26. */
  27. #ifndef __HW_UART_ATY_C
  28. #define __HW_UART_ATY_C
  29. #include "HW_UART_ATY.h"
  30. struct _uartMsgStruct uartMsgStruct_t = {0};
  31. /******************************* For user *************************************/
  32. #if defined(__STC51_ATY)
  33. #define BRT (uint32_t)(65536 - (uint32_t)FOSC / BAUD_RATE / 4)
  34. #define FOSC 24000000UL // 24MHz MCU frequency
  35. #define BAUD_RATE 115200
  36. /**
  37. * @brief uart hardware init
  38. * @note put at main init
  39. */
  40. void UartInit(void)
  41. {
  42. SCON = 0x50; // 8bit data, variable baud rate
  43. AUXR |= 0x40; // timer clk 1T mode
  44. AUXR &= 0xFE; // user timer 1 as uart1 baud generate
  45. TMOD &= 0x0F; // set timer mode
  46. TL1 = BRT; // set timer origin value
  47. TH1 = BRT >> 8; // set timer origin value
  48. ET1 = 0; // disable timer IT
  49. TR1 = 1; // start timer 1
  50. // TI = 1;
  51. uartMsgStruct_t.rxCount = 0x00;
  52. uartMsgStruct_t.txCount = 0x00;
  53. uartMsgStruct_t.busyFlag = 0;
  54. ES = 1; // UART interrupt enable
  55. EA = 1; // Global interrupt enable
  56. }
  57. /**
  58. * @brief uart IT callback function
  59. */
  60. void UartIsr(void) INTERRUPT(4)
  61. {
  62. if(TI)
  63. {
  64. TI = 0;
  65. uartMsgStruct_t.busyFlag = 0;
  66. }
  67. if(RI)
  68. {
  69. RI = 0;
  70. if(uartMsgStruct_t.rxCount < UART_RX_MAX_LEN)
  71. {
  72. uartMsgStruct_t.overCount = 0;
  73. uartMsgStruct_t.rx[uartMsgStruct_t.rxCount++] = SBUF;
  74. }
  75. }
  76. }
  77. /**
  78. * @brief uart send byte
  79. * @param byte byte to send
  80. */
  81. void UartSendByte(uint8_t byte)
  82. {
  83. uint8_t errCount = 0;
  84. while(uartMsgStruct_t.busyFlag == 1)
  85. {
  86. errCount++;
  87. if(errCount > 200)
  88. break;
  89. }
  90. uartMsgStruct_t.busyFlag = 1;
  91. SBUF = byte;
  92. }
  93. #elif defined(__STM32_HAL_ATY)
  94. void UartSendByte(uint8_t byte)
  95. {
  96. #ifdef LL
  97. LL_USART_TransmitData8(PRINTF_UART, byte);
  98. while(!LL_USART_IsActiveFlag_TC(PRINTF_UART)){}
  99. #else
  100. HAL_UART_Transmit(&PRINTF_UART, (uint8_t*)&byte, 1, 0xFFFF);
  101. #endif
  102. }
  103. #ifndef __MICROLIB
  104. // for standart lib(not use MicroLIB)
  105. #pragma import(__use_no_semihosting)
  106. // define _sys_exit to avoid semi-host mode
  107. void _sys_exit(int x)
  108. {
  109. x = x;
  110. }
  111. struct __FILE
  112. {
  113. int handle;
  114. };
  115. FILE __stdout;
  116. #endif /* __MICROLIB */
  117. /* In gcc, using printf() output, if there is no "\n" in output data, no data
  118. will be printed on the screen until "\n" is encountered or the buffer overflows
  119. Another way to refresh the output data is to run "fflush(stdout)" after
  120. sending the data to force a refresh of the output stream
  121. */
  122. #ifdef __GNUC__
  123. #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  124. #define GETCHAR_PROTOTYPE int __io_getchar(void)
  125. #else
  126. #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  127. #define GETCHAR_PROTOTYPE int fgetc(FILE *f)
  128. #endif /* __GNUC__ */
  129. PUTCHAR_PROTOTYPE \
  130. {
  131. #ifdef LL
  132. LL_USART_TransmitData8(USART1, ch);
  133. while(!LL_USART_IsActiveFlag_TC(USART1)){}
  134. #else
  135. HAL_UART_Transmit(&PRINTF_UART, (uint8_t*)&ch, 1, 0xFFFF);
  136. #endif
  137. return ch;
  138. }
  139. GETCHAR_PROTOTYPE \
  140. {
  141. uint8_t ch = 0;
  142. HAL_UART_Receive(&PRINTF_UART, &ch, 1, 0xFFFF);
  143. return ch;
  144. }
  145. #ifdef DEBUG_PRINTF_RECEIVE
  146. uint8_t uartPrintfRxBuf[PRINTF_RX_MAX_LEN];
  147. struct _uartMsgStruct uartPrintfMsgStruct = {uartPrintfRxBuf};
  148. /**
  149. * @brief init uart IT and DMA etc.
  150. */
  151. void PrintfReceiveInit(void)
  152. {
  153. HAL_UARTEx_ReceiveToIdle_DMA(&PRINTF_UART, uartPrintfMsgStruct.rx, PRINTF_RX_MAX_LEN);
  154. __HAL_DMA_DISABLE_IT(&PRINTF_DMA, DMA_IT_HT);
  155. }
  156. /**
  157. * @brief uart receive data analysis
  158. * @note put at main while
  159. */
  160. __WEAK_ATY void PrintfReceiveProcess(void)
  161. {
  162. struct _uartMsgStruct* ptr = &uartPrintfMsgStruct;
  163. if(ptr->rcvOverFlag)
  164. {
  165. ptr->rcvOverFlag = 0;
  166. PrintfReceiveProcess_User(ptr);
  167. PrintfReceiveInit();
  168. }
  169. }
  170. __WEAK_ATY void PrintfReceiveProcess_User(struct _uartMsgStruct* ptr)
  171. {
  172. if(memcmp("DSTART", (char*)ptr->rx, strlen("DSTART")) == 0)
  173. {
  174. printf("\r\nDebug START!");
  175. }
  176. else if(memcmp("temp", (char*)ptr->rx, strlen("temp")) == 0)
  177. {
  178. printf("\r\ntemp %c%c%c",
  179. ptr->rx[strlen("temp_")],
  180. ptr->rx[strlen("temp_") + 1],
  181. ptr->rx[strlen("temp_") + 2]);
  182. }
  183. }
  184. void HAL_UARTEx_RxEventCallback(UART_HandleTypeDef* huart, uint16_t Size)
  185. {
  186. HAL_UART_DMAStop(&PRINTF_UART);
  187. uartPrintfMsgStruct.rxCount = PRINTF_RX_MAX_LEN - __HAL_DMA_GET_COUNTER(&PRINTF_DMA);
  188. uartPrintfMsgStruct.rcvOverFlag = 1;
  189. /* if process is not too long, process function can be used at this call back;
  190. otherwise put process at other cycle like while or timer cycle */
  191. // PrintfReceiveProcess();
  192. }
  193. #endif /* DEBUG_PRINTF_RECEIVE */
  194. #endif /* PLATFORM */
  195. /******************************************************************************/
  196. /**
  197. * @brief uart send bytes
  198. * @param bytes bytes to send
  199. * @param len bytes to send
  200. */
  201. void UartSendBytes(uint8_t* bytes, uint16_t len)
  202. {
  203. while(len--)
  204. UartSendByte(*bytes++);
  205. }
  206. /**
  207. * @brief uart send string
  208. * @param str data to send
  209. */
  210. void UartSendStr(uint8_t* str)
  211. {
  212. while(*str)
  213. UartSendByte(*str++);
  214. }
  215. /**
  216. * @brief uart send float data
  217. * @param dataFloat float number
  218. */
  219. void UartSendFloat(float dataFloat)
  220. {
  221. union result
  222. {
  223. float temp_f;
  224. uint8_t temp_uint8[4];
  225. }resultA, resultB;
  226. resultA.temp_f = dataFloat;
  227. resultB.temp_uint8[0] = resultA.temp_uint8[0];
  228. resultB.temp_uint8[1] = resultA.temp_uint8[1];
  229. resultB.temp_uint8[2] = resultA.temp_uint8[2];
  230. resultB.temp_uint8[3] = resultA.temp_uint8[3];
  231. UartSendByte(resultB.temp_uint8[3]);
  232. UartSendByte(resultB.temp_uint8[2]);
  233. UartSendByte(resultB.temp_uint8[1]);
  234. UartSendByte(resultB.temp_uint8[0]);
  235. }
  236. /**
  237. * @brief uart send float data in ASCII character type
  238. * @param dataFloat number to send, use double to get better
  239. * @note float and double is the same in C51
  240. */
  241. // void UartSendFloatStr(double dataFloat)
  242. void UartSendFloatStr(float dataFloat)
  243. {
  244. #define DECIMALS_NUM 4
  245. uint8_t i = 0, j = 0;
  246. uint8_t dataStr[10]; // ulongint 4294967295, 6.4f
  247. unsigned long int tempSaveData = 0;
  248. for(i = 0; i < 10; i++)
  249. dataStr[i] = 0;
  250. if(dataFloat < 0)
  251. {
  252. dataFloat = -dataFloat;
  253. UartSendByte('-');
  254. }
  255. tempSaveData = (dataFloat * 10000) + 0.5;
  256. for(i = 0; tempSaveData != 0; i++)
  257. {
  258. dataStr[10 - 1 - i] = tempSaveData % 10;
  259. tempSaveData /= 10;
  260. }
  261. // UartSendByte(i + 48);
  262. if(i < 5)
  263. i = 5;
  264. while(i--)
  265. {
  266. UartSendByte(dataStr[10 - 1 - i] + 48);
  267. if(i == DECIMALS_NUM)
  268. UartSendByte('.');
  269. }
  270. UartSendByte(' ');
  271. }
  272. /**
  273. * @brief uart receive data process
  274. * @note put at main while
  275. */
  276. __WEAK_ATY void UartReceiveProcess(void)
  277. {
  278. // if(uartMsgStruct_t.overCount >= 10000)
  279. if(uartMsgStruct_t.overCount > 2000)
  280. {
  281. uartMsgStruct_t.overCount = 0;
  282. if(uartMsgStruct_t.rxCount != 0)
  283. {
  284. // UartSendStr(&uartMsgStruct_t.rxCount);
  285. UartReceiveProcess_User(&uartMsgStruct_t);
  286. while(uartMsgStruct_t.rxCount){
  287. uartMsgStruct_t.rx[uartMsgStruct_t.rxCount--] = 0;
  288. }
  289. }
  290. }
  291. else
  292. uartMsgStruct_t.overCount++;
  293. }
  294. // /**
  295. // * @brief uart receive data analysis in user define self
  296. // */
  297. // void UartReceiveProcess_User(struct _uartMsgStruct* ptr)
  298. // {
  299. // if(ptr->rx[0] == 'O' && ptr->rx[1] == 'K')
  300. // UartSendStr("\r\nRC OK!\r\n");
  301. // // UartSendStr(ptr->rx);
  302. // }
  303. // void main()
  304. // {
  305. // UartInit();
  306. // UartSendStr("Uart Test !\r\n");
  307. // while (1)
  308. // {
  309. // UartReceiveProcess();
  310. // }
  311. // }
  312. #endif /* __HW_UART_ATY_C */
  313. /******************************** End Of File *********************************/