Uart.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #include "Uart.h"
  2. void UartDataRecvSet(uint8_t val);
  3. uint8_t UartDataRecvGet(void);
  4. void Uart2_Data_Send(uint8_t* data,uint8_t size);
  5. void Uart1_Data_Send(uint8_t* data,uint8_t size);
  6. int _write (int file, uint8_t *ptr, uint16_t len);
  7. void Uart_dataCheck(uint8_t Usart_Num ,uint8_t* cnt);
  8. void QueueCheck(uint8_t Usart_Num,uint8_t* header,uint8_t* tail);
  9. void UartDataBufferCheck(void);
  10. uint8_t UartDataisReved;
  11. uint8_t rx1_data[1];
  12. uint8_t rx2_data[1];
  13. uint8_t count_in1 = 0, count_out1 = 0;
  14. uint8_t count_in2 = 0, count_out2 = 0;
  15. uint8_t buf[USART_CNT][buf_size] = {0,};
  16. uint8_t Uart_RxData[USART_CNT][buf_size] = {0,};
  17. uint8_t Uart_Rxcnt = 0;
  18. typedef struct{
  19. uint8_t Rx_Header;
  20. uint8_t Rx_Tail;
  21. uint8_t buf[USART_CNT][buf_size];
  22. }BlueUsart_t;
  23. void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
  24. {
  25. if(huart->Instance == USART1)
  26. {
  27. buf[USART1_CNT][count_in1] = rx1_data[0];
  28. if(++count_in1>=100){ count_in1 = 0; }
  29. HAL_UART_Receive_IT(&huart1,&rx1_data[0],1);
  30. }
  31. if(huart->Instance == USART2)
  32. {
  33. buf[USART2_CNT][count_in2] = rx2_data[0];
  34. if(++count_in2>=100){ count_in2 = 0; }
  35. HAL_UART_Receive_IT(&huart2,&rx2_data[0],1);
  36. }
  37. }
  38. void QueueCheck(uint8_t Usart_Num,uint8_t* header,uint8_t* tail){
  39. if(*tail != *header){
  40. Uart_RxData[Usart_Num][Uart_Rxcnt++] = buf[Usart_Num][(*tail)++];
  41. printf("%02x ",Uart_RxData[Usart_Num][Uart_Rxcnt - 1]);
  42. if(*tail>= 100){ *tail = 0; }
  43. UartTimerCnt = 0;
  44. UartDataRecvSet(Usart_Num + 1);
  45. }
  46. }
  47. void UartDataBufferCheck(void){
  48. QueueCheck(USART1_CNT,&count_in1,&count_out1);
  49. QueueCheck(USART2_CNT,&count_in2,&count_out2);
  50. }
  51. void UartDataRecvSet(uint8_t val){
  52. UartDataisReved = val;
  53. }
  54. uint8_t UartDataRecvGet(void){
  55. return UartDataisReved;
  56. }
  57. void Uart2_Data_Send(uint8_t* data,uint8_t size){
  58. HAL_UART_Transmit(&huart2, data,size, 10);
  59. }
  60. void Uart1_Data_Send(uint8_t* data,uint8_t size){
  61. HAL_UART_Transmit(&huart1, data,size, 10);
  62. }
  63. int _write (int file, uint8_t *ptr, uint16_t len)
  64. {
  65. HAL_UART_Transmit (&huart1, ptr, len, 10);
  66. return len;
  67. }
  68. void Uart_dataCheck(uint8_t Usart_Num ,uint8_t* cnt){
  69. etError crccheck = 0;
  70. #if 0
  71. for(uint8_t i = 0; i < (* cnt); i++){
  72. printf("%02x ",buf[i]);
  73. }
  74. printf("\r\n");
  75. #endif
  76. crccheck = STH30_CheckCrc(&Uart_RxData[Usart_Num][blucell_type],Uart_RxData[Usart_Num][blucell_length],Uart_RxData[Usart_Num][Uart_RxData[Usart_Num][blucell_length] + 1]);
  77. if(crccheck == CHECKSUM_ERROR){
  78. for(uint8_t i = 0; i < (*cnt); i++){
  79. printf("%02x ",Uart_RxData[Usart_Num][i]);
  80. }
  81. // printf("Original CRC : %02x RecvCRC : %02x \r\n",crccheck,Uart_RxData[Usart_Num][Uart_RxData[Usart_Num][blucell_length] + 1]);
  82. }
  83. else if(crccheck == NO_ERROR){
  84. RGB_Controller_Func(&Uart_RxData[Usart_Num][blucell_stx]);\
  85. UartDataRecvSet(0);
  86. }
  87. else{
  88. printf("What Happen?\r\n");
  89. /*NOP*/
  90. }
  91. *cnt = 0;
  92. memset(Uart_RxData[Usart_Num],0x00,buf_size);
  93. }