uart_task.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. #include "uart_task.h"
  2. #include "uart_interface.h"
  3. #include "uart_table.h"
  4. #include "utils.h"
  5. #include <stdint.h>
  6. #include <time.h>
  7. uint8_t port_cmd_first(uart_type *p_data)
  8. {
  9. if (p_data->rx_len > 3)
  10. return 0;
  11. if (p_data->rx[2] != 0x04)
  12. return 0;
  13. p_data->device_type = p_data->rx[1];
  14. p_data->tx_len = 10;
  15. p_data->tx[0] = 0x01;
  16. p_data->tx[1] = 0x03;
  17. return 1;
  18. }
  19. uint8_t port_cmd_read(uart_type *p_data)
  20. {
  21. uint8_t len = 0;
  22. uint8_t addr = 0;
  23. // len_check
  24. if (p_data->rx_len > 6)
  25. return 0;
  26. // cec_check
  27. // if (p_data->rx[p_data->rx_len - 1] != cec8(p_data->rx, p_data->rx_len -1));
  28. p_data->device_type = p_data->rx[1];
  29. addr = (p_data->rx[3] << 8) | p_data->rx[2];
  30. len = p_data->rx[4];
  31. if (0 == get_regist_value(p_data, addr, len))
  32. {
  33. return 0;
  34. }
  35. p_data->tx_len = len + 5;
  36. p_data->tx[0] = 0x02;
  37. p_data->tx[1] = p_data->rx[2];
  38. p_data->tx[2] = p_data->rx[3];
  39. p_data->tx[3] = p_data->rx[4];
  40. p_data->tx[p_data->tx_len - 1] = 0x99; // cec8(p_data->tx, p_data->tx_len - 1)
  41. return 1;
  42. }
  43. uint8_t port_cmd_write(uart_type *p_data)
  44. {
  45. uint8_t len = 0;
  46. uint8_t addr = 0;
  47. // len_check
  48. if (p_data->rx_len != (p_data->rx[4] + 6))
  49. return 0;
  50. // cec_check
  51. // if (p_data->rx[p_data->rx_len - 1] != cec8(p_data->rx, p_data->rx_len -1));
  52. p_data->device_type = p_data->rx[1];
  53. addr = (p_data->rx[3] << 8) | p_data->rx[2];
  54. len = p_data->rx[4];
  55. if (1 == set_regist_value(p_data, addr, len))
  56. {
  57. p_data->tx[1] = 0x00;
  58. }
  59. else
  60. {
  61. p_data->tx[1] = 0xFF;
  62. }
  63. p_data->tx_len = 3;
  64. // cmd
  65. p_data->tx[0] = 0x03;
  66. p_data->tx[2] = 0x99; // cec8(p_data->tx, p_data->tx_len - 1)
  67. return 1;
  68. }
  69. const com_protocol_type com_protocol_table[] = {
  70. {0x01, port_cmd_first},
  71. {0x02, port_cmd_read},
  72. {0x03, port_cmd_write},
  73. };
  74. void uart_conn_check(void)
  75. {
  76. static uint16_t count = 0;
  77. if (uart_msg.disconnect_flg)
  78. {
  79. count += 10;
  80. if (count >= 3000)
  81. {
  82. count = 3000;
  83. uart_msg.device_type = 0;
  84. }
  85. }
  86. else
  87. {
  88. if (uart_msg.device_type == 0x02)
  89. {
  90. count = 0;
  91. }
  92. }
  93. }
  94. uint8_t uart_receive_analysis(uart_type *p_data)
  95. {
  96. uint8_t i, len;
  97. len = ARR_SIZE(com_protocol_table);
  98. for (i = 0; i < len; i++)
  99. {
  100. if ((p_data->rx[0] == com_protocol_table[i].cmd) && (com_protocol_table[i].p_func != NULL))
  101. {
  102. if (1 == com_protocol_table[i].p_func(p_data))
  103. {
  104. return 1;
  105. }
  106. }
  107. }
  108. return 0;
  109. }
  110. void uart_task(void)
  111. {
  112. if (uart_msg.rx_finished_flg)
  113. {
  114. uart_msg.rx_finished_flg = 0;
  115. uart_msg.rx_len = uart_msg.rx_count_u8;
  116. uart_msg.rx_count_u8 = 0;
  117. if (1 == uart_receive_analysis(&uart_msg))
  118. {
  119. uart_start_send(&uart_msg);
  120. }
  121. }
  122. uart_conn_check();
  123. }