factory.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "factory.h"
  2. #include "bsp/bsp_driver.h"
  3. #include "prot/can_message.h"
  4. #include "prot/can_foc_msg.h"
  5. #include "foc/samples.h"
  6. #include "foc/motor/current.h"
  7. #include "libs/utils.h"
  8. #include "libs/logger.h"
  9. #include "os/os_task.h"
  10. static u8 factory_mode = 0;
  11. static void stop_pwm_adc(void);
  12. static bool start_pwm_adc(void) {
  13. pwm_turn_on_low_side();
  14. delay_ms(10);
  15. phase_current_offset_calibrate();
  16. pwm_start();
  17. delay_us(10); //wait for ebrake error
  18. adc_start_convert();
  19. phase_current_calibrate_wait();
  20. if (phase_curr_offset_check()) {
  21. stop_pwm_adc();
  22. return false;
  23. }
  24. return true;
  25. }
  26. static void stop_pwm_adc(void) {
  27. u32 mask = cpu_enter_critical();
  28. adc_stop_convert();
  29. pwm_stop();
  30. pwm_up_enable(true);
  31. cpu_exit_critical(mask);
  32. }
  33. void can_process_factory_message(can_message_t *can_message){
  34. uint8_t response[32];
  35. uint8_t rsplen;
  36. encoder_can_key(response, can_message->key);
  37. response[2] = 0;
  38. rsplen = 3;
  39. switch(can_message->key) {
  40. case BUILD_CMD_KEY(0xE0):
  41. factory_mode = decode_u8(can_message->data);
  42. break;
  43. case BUILD_CMD_KEY(0xE1):
  44. {
  45. if (factory_mode == 0) {
  46. response[2] = 1;
  47. break;
  48. }
  49. u8 item = decode_u8(can_message->data);
  50. if (item == 1) { //3相驱动测试
  51. u8 mask = decode_u8((u8 *)can_message->data + 1);
  52. pwm_3phase_sides(false, mask);
  53. }else if (item == 2) {//获取所有电压的采集值
  54. can_response_vols(can_message->src, can_message->key);
  55. return;
  56. }else if (item == 3) { //读取gpio状态
  57. encode_u16(response + 3, GPIOA_VALUE);
  58. encode_u16(response + 5, GPIOB_VALUE);
  59. encode_u16(response + 7, GPIOC_VALUE);
  60. encode_u16(response + 9, GPIOD_VALUE);
  61. encode_u16(response + 11, GPIOE_VALUE);
  62. rsplen += 10;
  63. }else if (item == 4) { // u phase detect
  64. int count = 20;
  65. s16 uvw[3] = {0, 0, 0};
  66. s16 uvw_total[3] = {0, 0, 0};
  67. if (can_message->len < 2) {
  68. response[2] = 2; //长度错误
  69. break;
  70. }
  71. u8 detect = decode_u8((u8 *)can_message->data + 1);
  72. gpio_phase_u_detect(detect?true:false);
  73. while(count-- > 0) {
  74. delay_us(100);
  75. sample_uvw_phases_raw(uvw);
  76. uvw_total[0] += uvw[0];
  77. uvw_total[1] += uvw[1];
  78. uvw_total[2] += uvw[2];
  79. }
  80. encode_u16(response + 3, uvw_total[0]/20);
  81. encode_u16(response + 5, uvw_total[1]/20);
  82. encode_u16(response + 7, uvw_total[2]/20);
  83. gpio_phase_u_detect(false);
  84. rsplen += 6;
  85. }else if (item == 5) { //phase current test
  86. u8 start = decode_u8((u8 *)can_message->data + 1);
  87. if (start) {
  88. pwm_3phase_sides(true, 0); //use pwm output, disable timer break in
  89. if (!start_pwm_adc()) {
  90. response[2] = 1;
  91. break;
  92. }
  93. }else {
  94. stop_pwm_adc();
  95. pwm_3phase_init();
  96. }
  97. }
  98. break;
  99. }
  100. default:
  101. rsplen = 0;
  102. break;
  103. }
  104. if (rsplen > 0) {
  105. can_send_response(can_message->src, response, rsplen);
  106. }
  107. }
  108. u8 factory_get_mode(void) {
  109. return factory_mode;
  110. }
  111. bool factory_is_running(void) {
  112. return (factory_mode == 0);
  113. }