app.c 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "app/app.h"
  2. #include "bsp/bsp.h"
  3. #include "os/timer.h"
  4. #include "os/co_task.h"
  5. #include "libs/logger.h"
  6. #include "libs/utils.h"
  7. #include "prot/can_foc_msg.h"
  8. #include "prot/can_message.h"
  9. #include "foc/core/foc_api.h"
  10. #include "bsp/timer_count32.h"
  11. #include "app/nv_storage.h"
  12. static void _app_low_task(void *args);
  13. extern measure_time_t g_meas_hall;
  14. extern measure_time_t g_meas_foc;
  15. void app_start(void){
  16. set_log_level(MOD_SYSTEM, L_debug);
  17. can_message_init();
  18. restore_config();
  19. foc_init();
  20. log_start_task();
  21. co_task_create(_app_low_task, NULL, 512);
  22. co_task_schedule();
  23. }
  24. static void _can_report_info(void) {
  25. can_report_speed(0x45, foc_get_speed());
  26. current_samp_t *s = foc_get_current_sample();
  27. can_report_phase_current(0x45, F2I(s->Ia * 1000), F2I(s->Ib * 1000), F2I(s->Ic * 1000));
  28. //sys_debug("phase current %f %f %f %f\n", s->Ia, s->Ib, s->Ic, s->max_Ia);
  29. sys_debug("phase offset %d %d %d\n", s->adc_offset_a, s->adc_offset_b, s->adc_offset_c);
  30. s->max_Ia = 0.0f;
  31. }
  32. static void print_backtrace(void){
  33. if (gd32_bkp_btrace_valid()){
  34. uint32_t bt[16];
  35. uint32_t bt_over;
  36. uint32_t bt_dep;
  37. uint16_t line;
  38. gd32_bkp_get_backtrace(bt, &bt_over, &bt_dep, &line);
  39. sys_error("system backtrace:\n");
  40. sys_error("stack overflow %d, stack dep = %d, line = %d\n", bt_over, bt_dep, line);
  41. for(bt_over = 0; bt_over < bt_dep; bt_over++){
  42. sys_error("0x%x ", bt[bt_over]);
  43. }
  44. sys_error("system backtrace end!\n");
  45. }else{
  46. sys_error("no backtrace\n");
  47. }
  48. }
  49. static void _app_low_task(void *args) {
  50. sys_debug("reset src: 0x%x\n", gd32_get_reset_source());
  51. print_backtrace();
  52. while(1) {
  53. wdog_reload();
  54. _can_report_info();
  55. ///sys_debug("foc exec time %d, intval %d, max %d, error %d\n", g_meas_foc.exec_time, g_meas_foc.intval_time, g_meas_foc.exec_max_error_time, g_meas_foc.intval_time_error);
  56. //sys_debug("hall exec time %d, intval %d\n", g_meas_hall.exec_time, g_meas_hall.intval_time);
  57. //sys_debug("vbus voltage: %f\n", foc_get_vbus_voltage());
  58. //hall_debug_log();
  59. co_task_delay(1000);
  60. }
  61. }