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.

93 lines
2.5 KiB

  1. #include "servo_operation.h"
  2. #include <stdbool.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. static UART_HandleTypeDef* m_uart;
  6. static uint8_t tx_buf[PROCESS_MAX_LEN];
  7. static uint8_t rx_buf[PROCESS_MAX_LEN];
  8. static uint8_t tx_send_len;
  9. static uint8_t cmd_type;
  10. static uint8_t check_sum;
  11. // ����Ҫһ����Я���Ĵ�����Ϣ�ı���
  12. static reg_info_t reg_info;
  13. /**
  14. * @description: ڳʼ
  15. * @param {UART_HandleTypeDef*} uart
  16. * @return {*}
  17. */
  18. void servo_uart_init(UART_HandleTypeDef* uart) { m_uart = uart; }
  19. /**
  20. * @description: д
  21. * @param {servo_obj_t*} servo
  22. * @return {*}
  23. */
  24. bool Write_operete(uint8_t id, uint8_t cmd, param_t* param) {
  25. reg_distinguish(param, &reg_info);
  26. if (cmd != SYC_W_DATA)
  27. cmd_type = non_syn;
  28. else
  29. cmd_type = syn;
  30. write_reg(id, cmd, param);
  31. return true;
  32. }
  33. void Ping_operete(uint8_t id) {
  34. cmd_basic_t* cmd_basic = (cmd_basic_t*)tx_buf;
  35. cmd_basic->head = 0xffff;
  36. cmd_basic->id = id;
  37. cmd_basic->valid_len = 0x02;
  38. cmd_basic->cmd = PING;
  39. check_sum = check_sum_calculate(&tx_buf[2], NULL);
  40. memcpy(&tx_buf[BASCI_LEN], &check_sum, 1);
  41. tx_send_len = BASCI_LEN + 1; // 1 = check_sum
  42. packet_process(m_uart, tx_buf, rx_buf, tx_send_len);
  43. }
  44. // ��ʼ�����޷�ȷ�����Ͳ����ij��ȣ�����һ�����鵫�ǵ���ָ�������������Ը��˸�����
  45. /**
  46. * @description:
  47. * @param {uint8_t} id
  48. * @param {uint8_t} cmd
  49. * @param {param_t*} param
  50. * @return {*}
  51. */
  52. void write_reg(uint8_t id, uint8_t cmd, param_t* param) {
  53. uint8_t param_real_len = param->count * reg_info.data_len; // ��������*ÿ���������� = ʵ�ʲ�������
  54. cmd_basic_t* cmd_basic = (cmd_basic_t*)tx_buf; // �����������ݲ�����������
  55. uint8_t* cmd_param = &tx_buf[5];
  56. cmd_basic->head = 0xffff;
  57. cmd_basic->id = id;
  58. if (cmd_type) {
  59. cmd_basic->valid_len = param_real_len + 3; // cmd + id + check_sum = 3
  60. cmd_basic->cmd = cmd;
  61. cmd_param[0] = param->addr;
  62. // �жϲ������ݳ���
  63. if (reg_info.data_len == byte) {
  64. memcpy(&cmd_param[1], param->data, param->count);
  65. } else if (reg_info.data_len == word) {
  66. // �ߵ�λ�����������ļĴ�����ַ����Ҫ����
  67. high_low_exchange(param, &cmd_param[1], &reg_info);
  68. }
  69. }
  70. // else ͬ���ͷ�ͬ���޷��ϲ�Ϊһ������
  71. check_sum = check_sum_calculate(&tx_buf[2], param);
  72. // ��checksum����ָ����ĩβ
  73. memcpy(&tx_buf[BASCI_LEN + param_real_len + 1], &check_sum, 1); // tx_buff[5] == addr 1 = addr
  74. tx_send_len = BASCI_LEN + param_real_len + 2; // 2 = addr + check_sum
  75. packet_process(m_uart, tx_buf,rx_buf, tx_send_len);
  76. }