Przeglądaj źródła

add gd32 demo && add motor dir

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 4 lat temu
rodzic
commit
ee14387b97
56 zmienionych plików z 856 dodań i 1091 usunięć
  1. 22 2
      Applications/app/app.c
  2. 10 0
      Applications/app/nv_storage.c
  3. 3 0
      Applications/app/nv_storage.h
  4. 16 2
      Applications/bsp/adc.c
  5. 13 2
      Applications/bsp/adc.h
  6. 1 0
      Applications/bsp/bsp.c
  7. 3 3
      Applications/bsp/bsp.h
  8. 2 3
      Applications/bsp/gd32_bkp.c
  9. 6 1
      Applications/bsp/gpio.c
  10. 1 46
      Applications/bsp/gpio.h
  11. 23 2
      Applications/bsp/mc_hall_gpio.c
  12. 10 0
      Applications/bsp/mc_hall_gpio.h
  13. 12 0
      Applications/bsp/mc_irqs.c
  14. 26 10
      Applications/bsp/pwm.c
  15. 8 7
      Applications/foc/commands.c
  16. 0 0
      Applications/foc/commands.h
  17. 0 0
      Applications/foc/core/circle_limitation.c
  18. 0 0
      Applications/foc/core/circle_limitation.h
  19. 27 18
      Applications/foc/core/foc_api.c
  20. 2 1
      Applications/foc/core/foc_api.h
  21. 20 55
      Applications/foc/core/foc_core.c
  22. 0 0
      Applications/foc/core/foc_core.h
  23. 5 4
      Applications/foc/core/foc_fsm.c
  24. 2 2
      Applications/foc/core/foc_fsm.h
  25. 1 11
      Applications/foc/core/foc_type.h
  26. 1 1
      Applications/foc/core/park_clark.h
  27. 0 0
      Applications/foc/core/pi_controller.c
  28. 0 0
      Applications/foc/core/pi_controller.h
  29. 0 0
      Applications/foc/core/ramp_ctrl.c
  30. 0 0
      Applications/foc/core/ramp_ctrl.h
  31. 1 1
      Applications/foc/core/svpwm.c
  32. 1 1
      Applications/foc/core/svpwm.h
  33. 0 24
      Applications/foc/gas_sensor.c
  34. 0 18
      Applications/foc/gas_sensor.h
  35. 0 400
      Applications/foc/hall_sensor_old.c
  36. 0 67
      Applications/foc/hall_sensor_old.h
  37. 1 1
      Applications/foc/motor/current.c
  38. 1 1
      Applications/foc/motor/current.h
  39. 170 103
      Applications/foc/motor/hall.c
  40. 12 6
      Applications/foc/motor/hall.h
  41. 78 0
      Applications/foc/motor/motor.c
  42. 24 0
      Applications/foc/motor/motor.h
  43. 1 1
      Applications/foc/motor/ntc.c
  44. 0 0
      Applications/foc/motor/ntc.h
  45. 61 0
      Applications/foc/samples.c
  46. 12 0
      Applications/foc/samples.h
  47. 0 27
      Applications/foc/vbus_sensor.c
  48. 0 16
      Applications/foc/vbus_sensor.h
  49. 3 2
      Applications/libs/backtrace.c
  50. 9 0
      Applications/os/co_task.c
  51. 2 0
      Applications/os/co_task.h
  52. 1 1
      Applications/prot/can_foc_msg.c
  53. 1 1
      Applications/prot/can_message.c
  54. 162 154
      Project/MC100_OS.uvoptx
  55. 102 97
      Project/MC100_OS.uvprojx
  56. BIN
      Simulink/FOC.slx

+ 22 - 2
Applications/app/app.c

@@ -6,8 +6,9 @@
 #include "libs/utils.h"
 #include "prot/can_foc_msg.h"
 #include "prot/can_message.h"
-#include "foc/foc_api.h"
+#include "foc/core/foc_api.h"
 #include "bsp/timer_count32.h"
+#include "app/nv_storage.h"
 
 static void _app_low_task(void *args);
 extern measure_time_t g_meas_hall;
@@ -15,6 +16,7 @@ extern measure_time_t g_meas_foc;
 void app_start(void){
 	set_log_level(MOD_SYSTEM, L_debug);
 	can_message_init();
+	restore_config();
 	foc_init();
 	log_start_task();
 	co_task_create(_app_low_task, NULL, 512);
@@ -30,9 +32,27 @@ static void _can_report_info(void) {
 	s->max_Ia = 0.0f;
 }
 
-extern void hall_debug_log(void);
+static void print_backtrace(void){
+    if (gd32_bkp_btrace_valid()){
+		uint32_t bt[16];
+		uint32_t bt_over;
+		uint32_t bt_dep;
+		uint16_t line;
+		gd32_bkp_get_backtrace(bt, &bt_over, &bt_dep, &line);
+		sys_error("system backtrace:\n");
+		sys_error("stack overflow %d, stack dep = %d, line = %d\n", bt_over, bt_dep, line);
+		for(bt_over = 0; bt_over < bt_dep; bt_over++){
+			sys_error("0x%x ", bt[bt_over]);
+		}
+		sys_error("system backtrace end!\n");
+	}else{
+		sys_error("no backtrace\n");
+	}
+}
+
 static void _app_low_task(void *args) {
 	sys_debug("reset src: 0x%x\n", gd32_get_reset_source());
+	print_backtrace();
 	while(1) {
 		wdog_reload();
 		_can_report_info();

+ 10 - 0
Applications/app/nv_storage.c

@@ -14,6 +14,16 @@ mc_config_t *mc_config_get(void) {
 	return &g_config;
 }
 
+void store_hall_table(u16 *hall_table) {
+	memcpy((char *)g_config.hall_table, (char *)hall_table, sizeof(g_config.hall_table));
+	store_config();
+}
+
+void store_hall_offset(s16 offset) {
+	g_config.hall_offset = offset;
+	store_config();
+}
+
 void store_config(void) {
 	u16 crc = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
 	g_config.crc16 = crc;

+ 3 - 0
Applications/app/nv_storage.h

@@ -11,6 +11,7 @@ typedef struct {
 
 typedef struct {
 	s16 hall_offset; /* hall 和A相之间的电角度偏移量 */
+	u16 hall_table[8];
 	op_cali_t phase_op[3]; /* 三相电流采集的校准系数 */
 	u16 crc16;
 }mc_config_t;
@@ -20,6 +21,8 @@ mc_config_t *mc_config_get(void);
 void config_set_hall_offset(s16 offset);
 void store_config(void);
 void restore_config(void);
+void store_hall_table(u16 *hall_table);
+void store_hall_offset(s16 offset);
 
 #endif /* _NV_Storage_H__ */
 

+ 16 - 2
Applications/bsp/adc.c

@@ -1,3 +1,4 @@
+#include "bsp/bsp.h"
 #include "bsp/adc.h"
 #include "libs/utils.h"
 #include "os/co_task.h"
@@ -84,12 +85,25 @@ void adc_init(void){
 static void _gpio_init(void) {
 	rcu_periph_clock_enable(RCU_GPIOA);
 	rcu_periph_clock_enable(RCU_GPIOB);
+#ifdef GD32_DEMO
+	/* configure ADC pin, bus voltage sampling -- ADC_IN0(PA0) */
+	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
+
+	/* configure ADC pin, temperature sampling -- ADC_IN11(PC1) */
+	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
+
+	/* configure ADC pin, current sampling -- ADC_IN1(PA1) ADC_IN12(PC2) ADC_IN13(PC3) */
+	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
+	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
+	gpio_init(GPIOC, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_3);
+#else
 	gpio_init(GPIOA, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
 	gpio_init(GPIOB, GPIO_MODE_AIN, GPIO_OSPEED_50MHZ, GPIO_PIN_0|GPIO_PIN_1);
+#endif	
 }
 
 
-void adc_start_insert_convert(void) {
+void adc_start_convert(void) {
 	int drop = 2;
     /* clear the ADC flag */
     adc_flag_clear(ADC0, ADC_FLAG_EOIC);
@@ -110,7 +124,7 @@ void adc_start_insert_convert(void) {
 
 }
 
-void adc_stop_insert_convert(void) {
+void adc_stop_convert(void) {
 	adc_disable_ext_trigger();
     /* disable ADC interrupt */
 #if SHUNT_NUM==THREE_SHUNTS_SAMPLE

+ 13 - 2
Applications/bsp/adc.h

@@ -7,6 +7,16 @@
 inserted ADC 由timer0 ch3触发,
 注意:adc所有外部触发都是下降沿触发 
 */
+
+#ifdef GD32_DEMO
+#define U_PHASE_I_CHAN  ADC_CHANNEL_1
+#define V_PHASE_I_CHAN  ADC_CHANNEL_12
+#define W_PHASE_I_CHAN  ADC_CHANNEL_13
+
+#define MOTOR_TEMP_CHAN ADC_CHANNEL_11
+#define VBUS_V_CHAN 	ADC_CHANNEL_0
+
+#else
 #define MOTOR_TEMP_CHAN ADC_CHANNEL_0
 #define HANDLERBAR_CHAN ADC_CHANNEL_1 //转把信号
 #define VBUS_V_CHAN 	ADC_CHANNEL_2
@@ -17,6 +27,7 @@ inserted ADC 由timer0 ch3触发,
 #define V_PHASE_I_CHAN  ADC_CHANNEL_7
 #define U_PHASE_I_CHAN  ADC_CHANNEL_8
 #define VBUS_I_CHAN     ADC_CHANNEL_9
+#endif
 #define ISQ2_OFFSET 10
 #define ISO3_OFFSET 15
 #define IL_OFFSET   20
@@ -186,7 +197,7 @@ static __inline void adc_update_ext_trigger(u32 trigger) {
 
 void adc_init(void);
 s32 adc_sample_regular_channel(int chan, int times);
-void adc_start_insert_convert(void);
-void adc_stop_insert_convert(void);
+void adc_start_convert(void);
+void adc_stop_convert(void);
 
 #endif /* _ADC_H__ */

+ 1 - 0
Applications/bsp/bsp.c

@@ -17,6 +17,7 @@ void bsp_init(void){
 	dbg_periph_enable(DBG_TIMER2_HOLD);
 	cpu_counts_enable();
 	timer_count32_init();
+	gpio_pin_init();
 	shark_uart_init(SHARK_UART0);
 	normal_task_timer_init();
 }

+ 3 - 3
Applications/bsp/bsp.h

@@ -13,6 +13,8 @@
 #include "bsp/fmc_flash.h"
 #include "bsp/can.h"
 
+#define GD32_DEMO 1
+#define USER_ITMER_BRAKE 1
 
 #define ADC_REFERENCE_VOLTAGE  3300.0f
 
@@ -47,15 +49,13 @@
 #define SPEED_SAMPLE_INVAL (100) //ת�Ѳɼ��ļ��,ms
 #define SPEED_RAMP_DURATION SPEED_SAMPLE_INVAL //�����ٶȵ�б��ʱ�䣬�ٶ�ƽ���������½�
 
-#define MAX_VBUS_VOLTAGE 52.0f
+#define MAX_VBUS_VOLTAGE 54.0f
 #define MAX_CURRENT      50.0F
 
 #define pwm_timer TIMER0
 #define adc_timer TIMER1
 #define aux_timer TIMER2
 
-#define USER_ITMER_BRAKE 0
-
 #define TIMER_UP_IRQ_PRIORITY 0
 #define ADC_IRQ_PRIORITY 1
 #define HALL_IRQ_PRIORITY 2

+ 2 - 3
Applications/bsp/gd32_bkp.c

@@ -78,9 +78,8 @@ void gd32_bkp_get_backtrace(uint32_t *backtrace, uint32_t *stack_over, uint32_t
 
 uint32_t  gd32_get_reset_source(void)
 {
-	uint32_t reset_source = 0;
-
-	reset_source = RCU_RSTSCK;
+	uint32_t reset_source = RCU_RSTSCK;
+	rcu_all_reset_flag_clear();
 	return reset_source;
 }
 

+ 6 - 1
Applications/bsp/gpio.c

@@ -7,7 +7,12 @@
 */
 
 void gpio_pin_init(void){
-
+	rcu_periph_clock_enable(RCU_GPIOA);
+    rcu_periph_clock_enable(RCU_GPIOB);
+	rcu_periph_clock_enable(RCU_GPIOC);
+	rcu_periph_clock_enable(RCU_GPIOD);
+	rcu_periph_clock_enable(RCU_GPIOF);
+	rcu_periph_clock_enable(RCU_AF);
 }
 
 

+ 1 - 46
Applications/bsp/gpio.h

@@ -14,51 +14,6 @@ typedef struct {
 }gpio_pin_config_t;
 
 void gpio_pin_init(void);
-void gpio_pin_set_low(int pin);
-void gpio_pin_set_high(int pin);
-void gpio_pin_set_active_high(int pin, int active);
-void gpio_pin_set_active_low(int pin, int active);
-void can_enable_transmit(int enable);
-int can_power_is_enabled(void);
-void can_backup_power_enable(int enable);
-void spi_flash_cs(int cs);
-void power_4v_output_enable(int enable);
-void sync_line_irq_enable(int enable);
-void left_light_enable(int enable);
-void right_light_enable(int enable);
-void day_light_enable(int enable);
-void near_light_enable(int enable);
-void far_light_enable(int enable);
-void far_led_enable(int enable);
-int sys_12v_is_enabled(void);
-int acc_12v_is_enabled(void);
-int charger_working(void);
-void charger_enable(int enable);
-void audio_classK_enable(int enable);
-int phase_detect(void);
-void ble_power_enable(int enable);
-void aux_mcu_power_enable(int enable);
-int slock_is_locked(void);//×øÍ°Ëø×´Ì¬¼ì²â
-void wireless_433_power_enable(int enable);
-int charger_is_enabled(void);
-void sync_allow_vehicle_sleep(int enable);
-void  wheel_detect_irq_enable(int enable);
-void  battery_plug_irq_enable(int enable);
-void ready_key_irq_enable(int enable);
-void ble_wakeup_irq_enable(int enable);
-void gsensor_irq_enable(int enable);
-void wireless_433_irq_enable(int enable);
-int is_ready_key_detect(void);
-void ready_led_enable(int enable);
-int hlock_is_locked(void);
-void slock_out(int enable);
-void slock_in(int enable);
-void sync_allow_vehicle_sleep(int enable);
-void sync_line_pulse_enable(int enable);
-void sync_line_pulse(void);
-void hlock_out(int enable);
-void hlock_in(int enable);
-void mcu_indicat_sleep(int sleep);
-int ble_is_busy(void);
+
 
 #endif /* _GPIO_PIN_H__ */

+ 23 - 2
Applications/bsp/mc_hall_gpio.c

@@ -2,7 +2,6 @@
 static void _gpio_irq_enable(void);
 
 void mc_hall_init(void){
-	rcu_periph_clock_enable(HALL_GPOI_CLK);
 	gpio_init(HALL_1_GROUP,  GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, HALL_1_PIN);
 	gpio_init(HALL_2_GROUP,  GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, HALL_2_PIN);
 	gpio_init(HALL_3_GROUP,  GPIO_MODE_IN_FLOATING, GPIO_OSPEED_50MHZ, HALL_3_PIN);
@@ -36,6 +35,28 @@ int get_hall_stat(int samples) {
 }
 
 #endif
+#ifdef GD32_DEMO
+static void _gpio_irq_enable(void){
+	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_6);
+	exti_init(EXTI_6, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
+	exti_interrupt_flag_clear(EXTI_6);
+	exti_interrupt_enable(EXTI_6);
+
+	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOA, GPIO_PIN_SOURCE_7);
+	exti_init(EXTI_7, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
+	exti_interrupt_flag_clear(EXTI_7);
+	exti_interrupt_enable(EXTI_7);
+
+	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_0);
+	exti_init(EXTI_0, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
+	exti_interrupt_flag_clear(EXTI_0);
+	exti_interrupt_enable(EXTI_0);
+
+	nvic_irq_enable(EXTI5_9_IRQn, HALL_IRQ_PRIORITY, 0U);
+	nvic_irq_enable(EXTI0_IRQn, HALL_IRQ_PRIORITY, 0U);
+}
+
+#else
 static void _gpio_irq_enable(void){
 	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_6);
 	exti_init(EXTI_6, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
@@ -54,5 +75,5 @@ static void _gpio_irq_enable(void){
 
 	nvic_irq_enable(EXTI5_9_IRQn, HALL_IRQ_PRIORITY, 0U);
 }
-
+#endif
 

+ 10 - 0
Applications/bsp/mc_hall_gpio.h

@@ -5,6 +5,15 @@
 //51ms, 77.6
 //52.2ms 
 //25.2
+#ifdef GD32_DEMO
+#define HALL_GPOI_CLK RCU_GPIOB
+#define HALL_1_PIN GPIO_PIN_6
+#define HALL_1_GROUP GPIOA
+#define HALL_2_PIN GPIO_PIN_7
+#define HALL_2_GROUP GPIOA
+#define HALL_3_PIN GPIO_PIN_0
+#define HALL_3_GROUP GPIOB
+#else
 #define HALL_GPOI_CLK RCU_GPIOB
 #define HALL_1_PIN GPIO_PIN_8
 #define HALL_1_GROUP GPIOB
@@ -13,6 +22,7 @@
 #define HALL_3_PIN GPIO_PIN_6
 #define HALL_3_GROUP GPIOB
 
+#endif
 #define DEGREES_120 0u
 #define DEGREES_60 1u
 

+ 12 - 0
Applications/bsp/mc_irqs.c

@@ -111,6 +111,16 @@ void TIMER5_IRQHandler(void) {
 	}
 }
 
+#ifdef GD32_DEMO
+void EXTI0_IRQHandler(void)
+{
+	if(RESET != exti_interrupt_flag_get(EXTI_0)){
+		exti_interrupt_flag_clear(EXTI_0);
+		hall_sensor_handler();
+	}	
+}
+
+#endif
 
 void EXTI2_IRQHandler(void)
 {
@@ -151,7 +161,9 @@ void EXTI5_9_IRQHandler(void){
 	}
 	if(RESET != exti_interrupt_flag_get(EXTI_8)){
 		exti_interrupt_flag_clear(EXTI_8);
+#ifndef GD32_DEMO		
 		hall_sensor_handler();
+#endif
 	}
 	if(RESET != exti_interrupt_flag_get(EXTI_9)){
 		exti_interrupt_flag_clear(EXTI_9);

+ 26 - 10
Applications/bsp/pwm.c

@@ -1,3 +1,4 @@
+#include "bsp/bsp.h"
 #include "bsp/pwm.h"
 #include "bsp/adc.h"
 #include "os/timer.h"
@@ -25,8 +26,9 @@ static void _init_aux_timer(void);
 static void timer0_dma_config(void);
 #endif
 static void _pwm_gpio_config(void);
+#if USER_ITMER_BRAKE==0
 static void _gpio_brakein_irq_enable(void);
-
+#endif
 u16 timer_update_buffer[6] = {0};
 
 void pwm_3phase_init(void){
@@ -72,11 +74,20 @@ static void _pwm_gpio_config(void)
     gpio_init(GPIOB,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_15);
 	/*configure BRAKE IN*/
 #if USER_ITMER_BRAKE==1
+#ifdef GD32_DEMO
+    /* TIMER0 BKIN */
+    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
+#else
 	gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_6);
+#endif
 #else
 	gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_4);//高刹车
 	gpio_init(GPIOB,GPIO_MODE_IN_FLOATING,GPIO_OSPEED_50MHZ,GPIO_PIN_5);//低刹车
 #endif
+#ifdef GD32_DEMO
+	/* IR2136S enable pin */
+    gpio_init(GPIOA, GPIO_MODE_OUT_PP, GPIO_OSPEED_2MHZ, GPIO_PIN_12);
+#endif
 }
 
 
@@ -140,14 +151,13 @@ static void _init_pwm_timer(void) {
 
 #if USER_ITMER_BRAKE==1
 	timer_break_parameter_struct timer_breakpara;
-    /* automatic output enable, break, dead time and lock configuration*/
-    timer_breakpara.runoffstate     = TIMER_ROS_STATE_DISABLE;
-    timer_breakpara.ideloffstate    = TIMER_IOS_STATE_DISABLE ;
-    timer_breakpara.deadtime        = TDead; // one Time of DTS, abort deadtime
-    timer_breakpara.breakpolarity    = TIMER_BREAK_POLARITY_LOW;
-    timer_breakpara.outputautostate  = TIMER_OUTAUTO_ENABLE;
-    timer_breakpara.protectmode     = TIMER_CCHP_PROT_0;
-    timer_breakpara.breakstate      = TIMER_BREAK_ENABLE;
+    timer_breakpara.runoffstate        = TIMER_ROS_STATE_ENABLE;
+    timer_breakpara.ideloffstate       = TIMER_IOS_STATE_ENABLE;
+    timer_breakpara.protectmode        = TIMER_CCHP_PROT_OFF; 
+    timer_breakpara.deadtime           = 50;
+    timer_breakpara.breakstate         = TIMER_BREAK_ENABLE;
+    timer_breakpara.breakpolarity      = TIMER_BREAK_POLARITY_HIGH;
+    timer_breakpara.outputautostate    = TIMER_OUTAUTO_DISABLE;
     timer_break_config(TIMER0,&timer_breakpara);
 	timer_interrupt_enable(timer, TIMER_INT_BRK);
 	timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_BRK);
@@ -182,6 +192,11 @@ static void _init_pwm_timer(void) {
 #if SHUNT_NUM==THREE_SHUNTS_SAMPLE	
     timer_enable(timer);
 #endif
+#ifdef GD32_DEMO
+	/* IR2136S enable */
+    gpio_bit_set(GPIOA, GPIO_PIN_12);
+#endif
+
 }
 
 #ifdef ENABLE_AUX_TIMER
@@ -286,6 +301,7 @@ static void timer0_dma_config(void)
 }
 #endif
 
+#if USER_ITMER_BRAKE==0
 static void _gpio_brakein_irq_enable(void){
 	gpio_exti_source_select(GPIO_PORT_SOURCE_GPIOB, GPIO_PIN_SOURCE_4);
 	exti_init(EXTI_4, EXTI_INTERRUPT, EXTI_TRIG_BOTH);
@@ -299,7 +315,7 @@ static void _gpio_brakein_irq_enable(void){
 	exti_interrupt_flag_clear(EXTI_5);
 	exti_interrupt_enable(EXTI_5);	
 }
-
+#endif
 
 void pwm_start(void){
 #if SHUNT_NUM==THREE_SHUNTS_SAMPLE

+ 8 - 7
Applications/foc/foc_cmd.c → Applications/foc/commands.c

@@ -6,12 +6,12 @@
 #include "bsp/bsp.h"
 #include "bsp/pwm.h"
 #include "bsp/adc.h"
-#include "foc/foc_api.h"
-#include "foc/foc_core.h"
-#include "foc/hall_sensor.h"
-#include "foc/foc_cmd.h"
+#include "foc/core/foc_api.h"
+#include "foc/core/foc_core.h"
+#include "foc/motor/hall.h"
+#include "foc/commands.h"
 #include "prot/can_foc_msg.h"
-
+#include "app/nv_storage.h"
 extern motor_foc_t g_foc;
 
 static void foc_cmd_task(void *args);
@@ -48,9 +48,10 @@ static void do_hall_calibrate(u8 can_addr, float vd) {
 		can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Cali_Hall_Phase), 1);
 		pwm_turn_on_low_side();
 		delay_ms(10);
-		foc_pwm_start(true);
-		adc_start_insert_convert();
 		int result = hall_sensor_calibrate(vd);
+		if (result) {
+			store_hall_table(hall_get_table());
+		}
 		sys_debug("hall phase cali %d\n", result);
 		can_send_ack(can_addr, CMD_2_CAN_KEY(Foc_Hall_Phase_Cali_Result), result);
 	}else {

+ 0 - 0
Applications/foc/foc_cmd.h → Applications/foc/commands.h


+ 0 - 0
Applications/foc/circle_limitation.c → Applications/foc/core/circle_limitation.c


+ 0 - 0
Applications/foc/circle_limitation.h → Applications/foc/core/circle_limitation.h


+ 27 - 18
Applications/foc/foc_api.c → Applications/foc/core/foc_api.c

@@ -5,15 +5,16 @@
 #include "bsp/bsp.h"
 #include "bsp/adc.h"
 #include "bsp/pwm.h"
-#include "foc/foc_api.h"
-#include "foc/park_clark.h"
-#include "foc/svpwm.h"
-#include "foc/foc_core.h"
-#include "foc/foc_fsm.h"
-#include "foc/phase_current.h"
-#include "foc/hall_sensor.h"
-#include "foc/gas_sensor.h"
-#include "foc/foc_cmd.h"
+#include "foc/core/foc_api.h"
+#include "foc/core/park_clark.h"
+#include "foc/core/svpwm.h"
+#include "foc/core/foc_fsm.h"
+#include "foc/core/foc_core.h"
+#include "foc/motor/current.h"
+#include "foc/motor/hall.h"
+#include "foc/motor/motor.h"
+#include "foc/samples.h"
+#include "foc/commands.h"
 
 extern motor_foc_t g_foc;
 static void foc_defulat_value(void);
@@ -52,6 +53,10 @@ static void foc_defulat_value(void){
 	pi_clear(&g_foc.speed_controller);
 }
 
+static bool gas_detect_speed_signal(void) {
+	return false;
+}
+
 current_samp_t *foc_get_current_sample(void) {
 	return &g_foc.current_samp;
 }
@@ -68,12 +73,20 @@ float speed_to_current(u16 rpm) {
 
 void foc_stop(void) {
 	pwm_stop();
-	adc_stop_insert_convert();
+	foc_start_adc(false);
 	g_foc.mosfec_gate = false;
 	foc_defulat_value();
 	hall_sensor_clear();
 }
 
+void foc_start_adc(bool start) {
+	if (start) {
+		adc_start_convert();
+	}else {
+		adc_stop_convert();
+	}
+}
+
 void foc_clear(void) {
 	g_foc.alpha_beta.alpha = 0;
 	g_foc.alpha_beta.beta = 0;
@@ -96,7 +109,7 @@ void foc_clear(void) {
 }
 
 u32 foc_get_speed(void) {
-	float speed = hall_sensor_avg_speed()/(g_foc.motor_param.poles);
+	u32 speed = motor_get_speed()/(motor_params.poles);
 	return abs(speed);
 }
 
@@ -162,19 +175,15 @@ void foc_current_calibrate(void){
 	g_foc.current_samp.is_calibrating_offset = true;
 	g_foc.current_samp.sector = SECTOR_5;
 	adc_current_sample_config(g_foc.current_samp.sector);
-	foc_pwm_start(true);
-
-	cpu_udelay(50 * 1000);
-
-	adc_start_insert_convert();
+	motor_start();
 	while(g_foc.current_samp.offset_sample_count != 0){};
 #if SHUNT_NUM==THREE_SHUNTS_SAMPLE
-	foc_pwm_start(false);
+	motor_stop();
 	cpu_udelay(100);
 	phase_current_init(&g_foc.current_samp);	
 	g_foc.current_samp.sector = SECTOR_1;
 	adc_current_sample_config(g_foc.current_samp.sector);
-	foc_pwm_start(true);
+	motor_start();
 	while(g_foc.current_samp.offset_sample_count != 0){};
 #endif
 	g_foc.current_samp.is_calibrating_offset = false;

+ 2 - 1
Applications/foc/foc_api.h → Applications/foc/core/foc_api.h

@@ -1,6 +1,6 @@
 #ifndef _FOC_API_H__
 #define _FOC_API_H__
-#include "foc/foc_type.h"
+#include "foc/core/foc_type.h"
 
 void foc_init(void);
 void foc_clear(void);
@@ -21,6 +21,7 @@ bool foc_motor_is_started(void);
 void foc_set_speed(u16 rpm, u32 delta_ms);
 float speed_to_voltage(u16 rpm);
 float speed_to_current(u16 rpm);
+void foc_start_adc(bool start);
 current_samp_t *foc_get_current_sample(void);
 
 #endif /* _FOC_API_H__ */

+ 20 - 55
Applications/foc/foc_core.c → Applications/foc/core/foc_core.c

@@ -1,29 +1,21 @@
 #include "os/co_task.h"
 #include "bsp/pwm.h"
 #include "bsp/adc.h"
-#include "foc_core.h"
-#include "foc_api.h"
-#include "foc_fsm.h"
-#include "phase_current.h"
-#include "park_clark.h"
-#include "hall_sensor.h"
-#include "circle_limitation.h"
-#include "vbus_sensor.h"
-#include "ntc_sensor.h"
-#include "gas_sensor.h"
-#include "svpwm.h"
+#include "foc/core/foc_core.h"
+#include "foc/core/foc_api.h"
+#include "foc/core/foc_fsm.h"
+#include "foc/motor/current.h"
+#include "foc/core/park_clark.h"
+#include "foc/motor/hall.h"
+#include "foc/motor/ntc.h"
+#include "foc/motor/motor.h"
+#include "foc/core/circle_limitation.h"
+#include "foc/samples.h"
+#include "foc/core/svpwm.h"
 #include "bsp/timer_count32.h"
 #include "libs/logger.h"
 
 motor_foc_t g_foc = {
-	.motor_param = {
-		.poles = 5,
-		.ld = 0.578477f,
-		.lq = 5.78477f,
-		.rs = 1.088f,
-		.inertia = 3.319367f,
-		.b_emf = 4.332566f,
-	},
 	.id_controller = {
 		.Kp_gain = 9,
 		.Ki_gain = 1071,
@@ -45,14 +37,12 @@ motor_foc_t g_foc = {
 };
 
 
-static void foc_measure_task(void*);
-static void foc_gas_task(void *args);
+static void foc_sample_task(void*);
 
 void foc_core_init(void) {
-	vbus_sensor_init();
+	samples_init();
 	ntc_sensor_init();
-	co_task_create(foc_measure_task, NULL, 256);
-	co_task_create(foc_gas_task, NULL, 256);
+	co_task_create(foc_sample_task, NULL, 256);
 }
 
 #if 1
@@ -174,22 +164,9 @@ void do_motor_foc(motor_foc_t *foc){
 	/* 死区补偿 */
 	deadtime_compensation(c_sample, &c_sample->time);
 	/* 更新 TIM1的CCR0-2,生成互补pwm, 相电流更新采样点 */
-
-#if SHUNT_NUM==THREE_SHUNTS_SAMPLE	
-	pwm_update_duty(c_sample->time.A, c_sample->time.B, c_sample->time.C);
-#else
-	pwm_wait_and_clear_updata();
-	pwm_update_duty_dma(c_sample->time.A, c_sample->time.B, c_sample->time.C, c_sample->time.A_next, c_sample->time.B_next, c_sample->time.C_next);
-#endif
-	pwm_update_2smaples(c_sample->time.Samp_p1, c_sample->time.Samp_p2);
-#ifdef ENABLE_AUX_TIMER
-	if (c_sample->time.Samp_p1 < FOC_PWM_Half_Period) {
-		adc_update_ext_trigger(ADC_TRIGGER_PHASE);
-	}else {
-		adc_update_ext_trigger(ADC_TRIGGER_PHASE2);
-	}
-#endif	
-	adc_current_sample_config(c_sample->sector);
+	motor_update_duty(c_sample->time.A, c_sample->time.B, c_sample->time.C, c_sample->time.A_next, c_sample->time.B_next, c_sample->time.C_next);
+	/* 更新采样点 */
+	motor_update_sample(c_sample->time.Samp_p1, c_sample->time.Samp_p2, c_sample->sector);
 
 	Debug_Log(foc);
 
@@ -265,25 +242,13 @@ void foc_normal_task(void) {
 	foc_fsm(&g_foc);
 }
 
-static void foc_measure_task(void *args){
+static void foc_sample_task(void *args){
+//	u64 ts = co_task_sys64_ts();
 	while(1) {
-		vbus_sample_voltage();
 		ntc_sensor_sample();
-		g_foc.vbus = vbus_get_filted_voltage();
+		g_foc.vbus = get_vbus_sample();
 		wdog_reload();
 		co_task_yield();
 	}
 }
 
-static void foc_gas_task(void *args) {
-	u64 ts = co_task_sys64_ts();
-	while(1) {
-		float value = gas_sample_voltage();
-		u16 speed = (value - MIN_GAS_VALUE) * (MAX_SPEED_RPM) / (MAX_GAS_VALUE - MIN_GAS_VALUE);
-		//foc_set_speed(speed, co_task_sys64_ts() - ts);
-		ts = co_task_sys64_ts();
-		co_task_delay(50);
-	}
-}
-
-

+ 0 - 0
Applications/foc/foc_core.h → Applications/foc/core/foc_core.h


+ 5 - 4
Applications/foc/foc_fsm.c → Applications/foc/core/foc_fsm.c

@@ -1,9 +1,10 @@
 #include <string.h>
 #include "bsp/bsp.h"
 #include "bsp/pwm.h"
-#include "foc/foc_api.h"
-#include "foc/foc_core.h"
-#include "foc/hall_sensor.h"
+#include "foc/core/foc_api.h"
+#include "foc/core/foc_core.h"
+#include "foc/motor/hall.h"
+#include "foc/motor/motor.h"
 
 extern motor_foc_t g_foc;
 
@@ -55,7 +56,7 @@ void foc_fsm(motor_foc_t *foc) {
 			break;
 		case START:
 			foc_clear();
-			pwm_turn_on_low_side();
+			motor_drvier_low_side(true);
 			foc_fsm_next_state(CURRENT_CALIBRATE);
 			break;
 		case CURRENT_CALIBRATE:

+ 2 - 2
Applications/foc/foc_fsm.h → Applications/foc/core/foc_fsm.h

@@ -1,7 +1,7 @@
 #ifndef _FOC_STM_H__
 #define _FOC_STM_H__
-#include "foc/foc_api.h"
-#include "foc/foc_core.h"
+#include "foc/core/foc_api.h"
+#include "foc/core/foc_core.h"
 
 foc_state_t foc_fsm_state(void);
 foc_fault_t foc_fsm_next_state(foc_state_t state);

+ 1 - 11
Applications/foc/foc_type.h → Applications/foc/core/foc_type.h

@@ -21,16 +21,6 @@ typedef struct _dqaix {
 	};
 }dq_t;
 
-typedef struct _motor_p {
-	int   poles; //电机极对数
-	float  ld;    //q轴电感
-	float  lq;    //d轴电感
-	float  rs;     //定子内阻
-	float  flux_linkage; //永磁磁链
-	float  inertia; 
-	float  b_emf;
-}motor_param_t;
-
 typedef struct _motor_s {
 	float theta;//ת�ӵ�Ƕ�, PI
 	int angle;//ת�ӵ�Ƕ�, ��
@@ -128,8 +118,8 @@ typedef struct foc_s {
 	dq_t dq_last;
 	u8   sector; //svpwm 扇区
 	float vbus; //母线电压
-	motor_param_t motor_param;
 	motor_stat_t  motor_stat;
+//	hall_param_t  hall_param;
 	phase_time_t phase_time;
 	pi_controller_t    id_controller;
 	pi_controller_t    iq_controller;

+ 1 - 1
Applications/foc/park_clark.h → Applications/foc/core/park_clark.h

@@ -1,7 +1,7 @@
 #ifndef _Park_Clark_H__
 #define _Park_Clark_H__
 #include "bsp/bsp.h"
-#include "foc/foc_type.h"
+#include "foc/core/foc_type.h"
 #include "math/fast_math.h"
 /* ·´Park ±ä»» */
 static __INLINE void Rev_Park(dq_t *dq, float angle, alpha_beta_t *alpha_bata) {

+ 0 - 0
Applications/foc/pi_controller.c → Applications/foc/core/pi_controller.c


+ 0 - 0
Applications/foc/pi_controller.h → Applications/foc/core/pi_controller.h


+ 0 - 0
Applications/foc/ramp_ctrl.c → Applications/foc/core/ramp_ctrl.c


+ 0 - 0
Applications/foc/ramp_ctrl.h → Applications/foc/core/ramp_ctrl.h


+ 1 - 1
Applications/foc/svpwm.c → Applications/foc/core/svpwm.c

@@ -1,4 +1,4 @@
-#include "foc/svpwm.h"
+#include "foc/core/svpwm.h"
 #include "math/fast_math.h"
 
 /* 7段式SVPWM

+ 1 - 1
Applications/foc/svpwm.h → Applications/foc/core/svpwm.h

@@ -1,6 +1,6 @@
 #ifndef _SVPWM_H__
 #define _SVPWM_H__
-#include "foc/foc_type.h"
+#include "foc/core/foc_type.h"
 
 void svpwm(alpha_beta_t *alpha_beta, float vbus, u32 PWW_half_period, phase_time_t *phase_out, u8 *sector_out);
 void SVPWM_7(alpha_beta_t *alpha_beta, float vbus, u32 PWM_half_period, phase_time_t *phase_out, u8 *sector_out);

+ 0 - 24
Applications/foc/gas_sensor.c

@@ -1,24 +0,0 @@
-#include "gas_sensor.h"
-#include "bsp/bsp.h"
-#include "bsp/adc.h"
-#include "libs/utils.h"
-
-static gas_t _gas;
-void gas_sensor_init(void){
-	_gas.voltage = 0;
-	_gas.lowpass = 5;
-	gas_sample_voltage();
-}
-
-float gas_sample_voltage(void){
-	u32 vadc = adc_sample_regular_channel(HANDLERBAR_CHAN, 16);
-	_gas.voltage = vadc * ADC_REFERENCE_VOLTAGE /4096.0f;
-	
-	return MAX(min(_gas.voltage, MIN_GAS_VALUE), MAX_GAS_VALUE);
-}
-
-/* 检测到有效转把信号 */
-bool gas_detect_speed_signal(void) {
-	return _gas.voltage > MIN_GAS_VALUE;
-}
-

+ 0 - 18
Applications/foc/gas_sensor.h

@@ -1,18 +0,0 @@
-#ifndef _GAS_SENSOR_H__
-#define _GAS_SENSOR_H__
-#include "os/os_type.h"
-
-typedef struct {
-	float voltage;
-	float lowpass;
-}gas_t;
-
-#define MIN_GAS_VALUE 1000.0F
-#define MAX_GAS_VALUE 5000.0F
-
-void gas_sensor_init(void);
-float gas_sample_voltage(void);
-bool gas_detect_speed_signal(void);
-
-#endif /* _GAS_SENSOR_H__ */
-

+ 0 - 400
Applications/foc/hall_sensor_old.c

@@ -1,400 +0,0 @@
-#include <string.h>
-#include "bsp/bsp.h"
-#include "bsp/mc_hall_gpio.h"
-#include "os/co_task.h"
-#include "os/timer.h"
-#include "libs/utils.h"
-#include "libs/logger.h"
-#include "math/fast_math.h"
-#include "hall_sensor.h"
-#include "foc/foc_api.h"
-#include "app/nv_storage.h"
-#include "bsp/timer_count32.h"
-
-
-#define HALL_READ_TIMES 3
-
-static void _hall_detect_task(void *args);
-static void _hall_init_el_angle(void);
-
-#define PWM_T  (0.000033f)
-
-#define HALL_PLACE_OFFSET (315)//(345) //315
-/* 
-100
-101
-001
-011
-010
-110
-4,5,1,3,2,6,4
-*/
-
-static hall_sensor_t _sensor_hander;
-
-measure_time_t g_meas_hall = {.exec_max_time = 6,};
-
-#define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
-#define us_2_s(tick) ((float)tick / 1000000.0f)
-
-#define rand_angle(a) {if (a >= PHASE_360_DEGREE) a-=PHASE_360_DEGREE;else if (a < 0) a +=PHASE_360_DEGREE;};
-
-static float hall_speed[8];
-static float  hall_angle[8];
-static void __inline _hall_put_sample(u32 ticks, u8 hall) {
-	hall_sample_t *s = &_sensor_hander.samples[hall];
-	s->ticks_sum -= s->ticks[s->index];
-	s->ticks[s->index] = ticks;
-	s->ticks_sum += s->ticks[s->index];
-	
-	s->index += 1;
-	if (s->index >= SAMPLE_MAX_COUNT) {
-		s->full = true;
-		s->index = 0;
-	}
-}
-
-static u32 __inline _hall_angle_us_speed(u8 hall){
-	hall_sample_t *s = &_sensor_hander.samples[hall];
-	if (s->ticks_sum == 0) {
-		return 0;
-	}
-
-	if (!s->full) {
-		return (s->ticks[s->index-1]);
-	}else {
-		return s->ticks_sum/SAMPLE_MAX_COUNT;
-	}
-}
-
-static float __inline _hall_angle_speed(void){
-	u32 sum = 0 ;
-	u32 num = 0;
-	for (int hall = 1; hall < 7; hall++) {
-		hall_sample_t *s = &_sensor_hander.samples[hall];
-		if (s->ticks_sum == 0) {
-			continue;
-		}
-	
-		if (!s->full) {
-			sum +=(s->ticks[s->index-1]);
-			num ++;
-		}else {
-			sum  += s->ticks_sum;
-			num += SAMPLE_MAX_COUNT;
-		}
-	}
-	return (float)PHASE_60_DEGREE * (float)num / us_2_s(sum);
-}
-
-
-static bool __inline _hall_data_empty(u8 hall) {
-	hall_sample_t *s = &_sensor_hander.samples[hall];
-	if ((!s->full) && (s->index == 0)){
-		return true;
-	}
-	return false;
-}
-static void hall_sensor_default(void) {
-	memset(&_sensor_hander, 0, sizeof(_sensor_hander));
-	_sensor_hander.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
-	_hall_init_el_angle();
-}
-
-void hall_sensor_init(void) {
-	mc_hall_init();
-	hall_sensor_default();
-	co_task_create(_hall_detect_task, NULL, 512);
-}
-
-void hall_sensor_clear(void) {
-	hall_sensor_default();
-}
-
-
-void hall_debug_log(void) {
-	for (int i = 0; i < 8; i++) {
-		if (i != 0 && i != 7) {
-			sys_debug("hall speed: %d,  %f - %f, %d\n", i, hall_speed[i], hall_angle[i], _sensor_hander.sensor_error);
-		}
-	}
-	sys_debug("angle dir %d\n", _sensor_hander.direction);
-	
-}
-
-static void _hall_detect_task(void *args) {
-	while(1) {
-		if (_sensor_hander.el_speed != 0) {
-			u32 ticks_now = timer_count32_get();
-			u32 delta_us = timer_count32_getus(ticks_now, _sensor_hander.hall_ticks);
-			if (delta_us >= (1200*1000)) {
-				hall_sensor_clear();
-			}
-		}
-		co_task_delay(100);
-	}
-}
-
-float hall_sensor_get_theta(void){
-	if (_sensor_hander.is_override_angle) {
-		return _sensor_hander.override_el_angle;
-	}
-
-	u32 us_now = timer_count32_delta_us(_sensor_hander.estimate_time_ticks, NULL);
-	float ration = (float)us_now / (float)_sensor_hander.speed_us_for_estimate;
-	
-	float angle_step = (float)PHASE_60_DEGREE * ration;
-	_sensor_hander.estimate_delta_angle = angle_step;
-	if (angle_step >= PHASE_60_DEGREE) {
-		angle_step = PHASE_60_DEGREE;
-	}
-
-	if (_sensor_hander.direction == POSITIVE) {
-		_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle + angle_step;
-	}else {
-		_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle - angle_step;
-	}
-
-	rand_angle(_sensor_hander.estimate_el_angle);
-
-	//log_chan_value(1, (int)_sensor_hander.estimate_el_angle);
-
-	return _sensor_hander.estimate_el_angle;
-}
-
-
-void hall_sensor_set_theta(bool override, float theta){
-	_sensor_hander.is_override_angle = override;
-	_sensor_hander.override_el_angle = theta;
-}
-
-float hall_sensor_get_speed(void) {
-	_sensor_hander.rpm = _sensor_hander.el_speed / 360.0f * 60.0f;
-	return _sensor_hander.rpm;
-}
-
-float hall_sensor_avg_speed(void) {
-	return _sensor_hander.el_speed / 360 * 60.0f;
-}
-
-int hall_offset_increase(int inc) {
-	if (_sensor_hander.phase_offset + inc >= 360) {
-		_sensor_hander.phase_offset = _sensor_hander.phase_offset + inc - 360;
-	}else {
-		_sensor_hander.phase_offset += inc;
-	}
-	return _sensor_hander.phase_offset;
-}
-
-
-int hall_sensor_calibrate(float voltage){
-	foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
-	hall_sensor_set_theta(true, 0.0f);
-	foc_set_dq_command(0.0f, 0.0f);
-	foc_pwm_start(true);
-	for (int i = 0;i < 100;i++) {
-		foc_set_dq_command((float)i * voltage / 100.0f, 0.0f);
-		co_task_delay(1);
-		wdog_reload();
-	}
-	float sin_hall[8];
-	float cos_hall[8];
-	int hall_iterations[8];
-	memset(sin_hall, 0, sizeof(sin_hall));
-	memset(cos_hall, 0, sizeof(cos_hall));
-	memset(hall_iterations, 0, sizeof(hall_iterations));
-	co_task_delay(2 * 1000);
-	// Forwards
-	for (int i = 0;i < 5;i++) {
-		for (int j = 0;j < 360;j++) {
-			hall_sensor_set_theta(true, j);
-			co_task_delay(50);
-			wdog_reload();
-			int hall = get_hall_stat(7);
-			float s, c;
-			normal_sincosf(degree_2_pi(j), &s, &c);
-			sin_hall[hall] += s;
-			cos_hall[hall] += c;
-			hall_iterations[hall]++;
-		}
-	}
-	//hall_sensor_set_theta(true, 360);
-	//co_task_delay(2 * 1000);
-	sys_debug("Revers\n");
-	// Reverse
-	for (int i = 0;i < 5;i++) {
-		for (int j = 360;j >= 0;j--) {
-			hall_sensor_set_theta(true, j);
-			co_task_delay(50);
-			wdog_reload();
-			int hall = get_hall_stat(7);
-			float s, c;
-			normal_sincosf(degree_2_pi(j), &s, &c);
-			sin_hall[hall] += s;
-			cos_hall[hall] += c;
-			hall_iterations[hall]++;
-		}
-	}
-	foc_pwm_start(false);
-	hall_sensor_set_theta(false, 0.0f);
-	foc_set_dq_command(0.0f, 0.0f);
-	int fails = 0;
-	for(int i = 0;i < 8;i++) {
-		if (hall_iterations[i] > 30) {
-		} else {
-			fails++;
-		}
-	}
-	return fails == 2;	
-}
-
-
-static void _hall_init_el_angle(void) {
-	_sensor_hander.hall_stat = get_hall_stat(HALL_READ_TIMES);
-  	switch ( _sensor_hander.hall_stat )
-  	{
-    case STATE_5:
-      	_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE/2;
-      	break;
-    case STATE_1:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + PHASE_60_DEGREE / 2;
-      	break;
-    case STATE_3:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + PHASE_60_DEGREE / 2;
-      	break;
-    case STATE_2:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_180_DEGREE + PHASE_60_DEGREE / 2;
-      	break;
-    case STATE_6:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_240_DEGREE + PHASE_60_DEGREE / 2;
-      	break;
-    case STATE_4:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_300_DEGREE + PHASE_60_DEGREE / 2;
-      	break;
-    default:
-      	/* Bad hall sensor configutarion so update the speed reliability */
-      	_sensor_hander.sensor_error ++;
-      	break;
- 	}
-  	/* Initialize the measured angle */
-	rand_angle(_sensor_hander.measured_el_angle);
-  	_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
-	_sensor_hander.hall_ticks = timer_count32_get();
-}
-
-/* 4,5,1,3,2,6,4 */
-static s32 _hall_position(u8 state_now, u8 state_prev) {
-	s32 theta_now =  0xFFFFFFFF;
-	switch (state_now) {
-		case STATE_1:
-			if (state_prev == STATE_5) {
-				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
-			}else if (state_prev == STATE_3) {
-				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
-			}
-			break;
-		case STATE_2:
-			if (state_prev == STATE_3) {
-				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
-			}else if (state_prev == STATE_6) {
-				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
-			}
-			break;
-		case STATE_3:
-			if (state_prev == STATE_1) {
-				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
-			}else if (state_prev == STATE_2) {
-				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
-			}
-			break;
-		case STATE_4:
-			if (state_prev == STATE_6) {
-				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
-			}else if (state_prev == STATE_5) {
-				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset;
-			}
-			break;
-		case STATE_5:
-			if (state_prev == STATE_4) {
-				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset;
-			}else if (state_prev == STATE_1) {
-				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
-			}
-			break;
-		case STATE_6:
-			if (state_prev == STATE_2) {
-				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
-			}else if (state_prev == STATE_4) {
-				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
-			}
-			break;
-		default:
-			_sensor_hander.sensor_error ++;
-			return 0xFFFFFFFF;
-	}
-	rand_angle(theta_now);
-	return theta_now;
-}
-
-void hall_sensor_handler(void) {
-	if (_sensor_hander.is_override_angle) {
-		sys_debug("%d:%d\n", (int)get_hall_stat(HALL_READ_TIMES), (int)_sensor_hander.override_el_angle);
-		return;
-	}
-	time_measure_start(&g_meas_hall);
-	u8 hall_stat_now = get_hall_stat(HALL_READ_TIMES);
-	u8 hall_stat_prev = _sensor_hander.hall_stat;
-	u32 hall_ticks_now = timer_count32_get();	
-
-	s32 theta_now = _hall_position(hall_stat_now, hall_stat_prev);
-	if (theta_now == 0xFFFFFFFF) {
-		return;
-	}
-
-	u32 delta_us = timer_count32_getus(hall_ticks_now, _sensor_hander.hall_ticks);
-	if (delta_us == 0) {
-		return;
-	}
-
-	float delta_time = us_2_s(delta_us);
-	_hall_put_sample(delta_us, hall_stat_prev);
-	u32 us_speed_prev = _sensor_hander.speed_us_for_estimate;
-	u32 us_speed_now = 0;
-	if (_hall_data_empty(hall_stat_now)) {
-		us_speed_now = delta_us;
-	}else {
-		us_speed_now = _hall_angle_us_speed(hall_stat_now);
-	}
-	if (us_speed_now != 0) {
-		hall_speed[hall_stat_now] = (float)us_speed_prev / (float)us_speed_now;
-	}
-	hall_angle[hall_stat_now] = (float)_sensor_hander.estimate_delta_angle/(float)PHASE_60_DEGREE;
-	os_disable_irq();
-	
-	_sensor_hander.estimate_delta_angle = 0;
-	_sensor_hander.measured_el_angle = theta_now;	
-	_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
-
-	_sensor_hander.estimate_time_ticks = hall_ticks_now;
-	_sensor_hander.speed_us_for_estimate = us_speed_now;
-	os_enable_irq();
-	_sensor_hander.el_speed = _hall_angle_speed();
-	_sensor_hander.hall_stat = hall_stat_now;
-	_sensor_hander.hall_ticks = hall_ticks_now;
-	
-	time_measure_end(&g_meas_hall);
-}
-
-

+ 0 - 67
Applications/foc/hall_sensor_old.h

@@ -1,67 +0,0 @@
-#ifndef _HALL_SENSOR_H__
-#define _HALL_SENSOR_H__
-#include "os/os_type.h"
-#include "bsp/bsp.h"
-
-#define NEGATIVE          (int8_t)-1
-#define POSITIVE          (int8_t)1
-
-#define PHASE_60_DEGREE (60)
-#define PHASE_120_DEGREE (120)
-#define PHASE_180_DEGREE (180)
-#define PHASE_240_DEGREE (240)
-#define PHASE_300_DEGREE (300)
-#define PHASE_360_DEGREE (360)
-
-#define STATE_0 (uint8_t)0
-#define STATE_1 (uint8_t)1
-#define STATE_2 (uint8_t)2
-#define STATE_3 (uint8_t)3
-#define STATE_4 (uint8_t)4
-#define STATE_5 (uint8_t)5
-#define STATE_6 (uint8_t)6
-#define STATE_7 (uint8_t)7
-
-#define THETA_NONE        (float)0xFFFF
-#define SAMPLE_MAX_COUNT 3
-
-typedef struct {
-	u32   ticks[SAMPLE_MAX_COUNT];
-	u32   ticks_sum;
-	u32   index;
-	bool  full;
-}hall_sample_t;
-
-typedef struct {
-	float estimate_el_angle; //60度区间内的估计电角度
-	u32   estimate_time_ticks;
-	s32   estimate_delta_angle;//for debug
-	s32   measured_el_angle; //hall测量到的电角度
-	u32   speed_us_for_estimate; //hall经过60°的时间,给角度估计使用
-	float el_speed; 		//当前的电角速度, 单位:rad/s
-	float rpm;        		//当前的电速度, 单位:RPM
-	u8    hall_stat;
-	u32   hall_ticks;
-	bool  working;
-	s8    direction;
-	s32   phase_offset;
-	bool  is_override_angle;
-	float override_el_angle;
-	hall_sample_t samples[8];
-	u32  sensor_error;
-}hall_sensor_t;
-
-
-
-
-void hall_sensor_init(void);
-void hall_sensor_clear(void);
-float hall_sensor_get_theta(void); //return degree
-float hall_sensor_get_speed(void); //return rpm
-float hall_sensor_avg_speed(void);
-int hall_sensor_calibrate(float voltage);
-void hall_sensor_set_theta(bool override, float theta);
-int hall_offset_increase(int inc);
-
-#endif /* _HALL_SENSOR_H__ */
-

+ 1 - 1
Applications/foc/phase_current.c → Applications/foc/motor/current.c

@@ -1,6 +1,6 @@
 #include <math.h>
 #include "bsp/adc.h"
-#include "phase_current.h"
+#include "foc/motor/current.h"
 #include "libs/utils.h"
 #include "libs/logger.h"
 

+ 1 - 1
Applications/foc/phase_current.h → Applications/foc/motor/current.h

@@ -1,6 +1,6 @@
 #ifndef _PHASE_CURRENT_H__
 #define _PHASE_CURRENT_H__
-#include "foc_type.h"
+#include "foc/core/foc_type.h"
 /* boundary zone definition */
 #define REGULAR         ((uint8_t)0u)
 #define BOUNDARY_1      ((uint8_t)1u)  /* Two small, one big */

+ 170 - 103
Applications/foc/hall_sensor.c → Applications/foc/motor/hall.c

@@ -6,27 +6,21 @@
 #include "libs/utils.h"
 #include "libs/logger.h"
 #include "math/fast_math.h"
-#include "hall_sensor.h"
-#include "foc/foc_api.h"
+#include "foc/motor/hall.h"
+#include "foc/core/foc_api.h"
 #include "app/nv_storage.h"
 #include "bsp/timer_count32.h"
 
+#define USE_DETECTED_ANGLE 1
 
 #define HALL_READ_TIMES 3
 
 static void _hall_detect_task(void *args);
 static void _hall_init_el_angle(void);
 
-#define PWM_T  (0.000033f)
 
-#define HALL_PLACE_OFFSET (315)//(345) //315
+#define HALL_PLACE_OFFSET (315) //(345) //315
 /* 
-100
-101
-001
-011
-010
-110
 4,5,1,3,2,6,4
 */
 
@@ -41,12 +35,14 @@ measure_time_t g_meas_hall = {.exec_max_time = 6,};
 
 static float hall_speed[8];
 static float  hall_angle[8];
-static void __inline _hall_put_sample(u32 ticks, u8 hall) {
-	hall_sample_t *s = &_sensor_hander.samples[hall];
+static void __inline _hall_put_sample(u32 ticks, float angle) {
+	hall_sample_t *s = &_sensor_hander.samples;
 	s->ticks_sum -= s->ticks[s->index];
+	s->angles_sum -= s->angles[s->index];
 	s->ticks[s->index] = ticks;
+	s->angles[s->index] = angle;
 	s->ticks_sum += s->ticks[s->index];
-	
+	s->angles_sum += s->angles[s->index];
 	s->index += 1;
 	if (s->index >= SAMPLE_MAX_COUNT) {
 		s->full = true;
@@ -54,51 +50,45 @@ static void __inline _hall_put_sample(u32 ticks, u8 hall) {
 	}
 }
 
-static u32 __inline _hall_angle_us_speed(u8 hall){
-	hall_sample_t *s = &_sensor_hander.samples[hall];
+static float __inline _hall_angle_speed(void){
+	hall_sample_t *s = &_sensor_hander.samples;
 	if (s->ticks_sum == 0) {
-		return 0;
+		return 0.0f;
 	}
-
+	
 	if (!s->full) {
-		return (s->ticks[s->index-1]);
+		return s->angles[s->index - 1] / us_2_s(s->ticks[s->index-1]);
 	}else {
-		return s->ticks_sum/SAMPLE_MAX_COUNT;
+		return s->angles_sum / us_2_s(s->ticks_sum);
 	}
 }
 
-static float __inline _hall_angle_speed(void){
-	u32 sum = 0 ;
-	u32 num = 0;
-	for (int hall = 1; hall < 7; hall++) {
-		hall_sample_t *s = &_sensor_hander.samples[hall];
-		if (s->ticks_sum == 0) {
-			continue;
-		}
-	
-		if (!s->full) {
-			sum +=(s->ticks[s->index-1]);
-			num ++;
-		}else {
-			sum  += s->ticks_sum;
-			num += SAMPLE_MAX_COUNT;
+void hall_debug_log(void) {
+	for (int i = 0; i < 8; i++) {
+		if (i != 0 && i != 7) {
+			sys_debug("hall speed: %d,  %f - %f, %d\n", i, hall_speed[i], hall_angle[i], _sensor_hander.sensor_error);
+			sys_debug("hall angle %d\n", _sensor_hander.angle_table[i]);
 		}
 	}
-	return (float)PHASE_60_DEGREE * (float)num / us_2_s(sum);
+	sys_debug("angle dir %d\n", _sensor_hander.direction);
 }
 
-
-static bool __inline _hall_data_empty(u8 hall) {
-	hall_sample_t *s = &_sensor_hander.samples[hall];
+/*
+static bool __inline _hall_data_empty(void) {
+	hall_sample_t *s = &_sensor_hander.samples;
 	if ((!s->full) && (s->index == 0)){
 		return true;
 	}
 	return false;
 }
+*/
+
 static void hall_sensor_default(void) {
 	memset(&_sensor_hander, 0, sizeof(_sensor_hander));
 	_sensor_hander.phase_offset = HALL_PLACE_OFFSET;//mc_config_get()->hall_offset;
+	memcpy((char *)_sensor_hander.angle_table, (char *)mc_config_get()->hall_table, sizeof(_sensor_hander.angle_table));
 	_hall_init_el_angle();
+	hall_debug_log();
 }
 
 void hall_sensor_init(void) {
@@ -112,16 +102,6 @@ void hall_sensor_clear(void) {
 }
 
 
-void hall_debug_log(void) {
-	for (int i = 0; i < 8; i++) {
-		if (i != 0 && i != 7) {
-			sys_debug("hall speed: %d,  %f - %f, %d\n", i, hall_speed[i], hall_angle[i], _sensor_hander.sensor_error);
-		}
-	}
-	sys_debug("angle dir %d\n", _sensor_hander.direction);
-	
-}
-
 static void _hall_detect_task(void *args) {
 	while(1) {
 		if (_sensor_hander.el_speed != 0) {
@@ -140,19 +120,15 @@ float hall_sensor_get_theta(void){
 		return _sensor_hander.override_el_angle;
 	}
 
-	u32 us_now = timer_count32_delta_us(_sensor_hander.estimate_time_ticks, NULL);
-	float ration = (float)us_now / (float)_sensor_hander.speed_us_for_estimate;
+	u32 us_now = timer_count32_delta_us(_sensor_hander.estimate_time_ticks, &_sensor_hander.estimate_time_ticks);
 	
-	float angle_step = (float)PHASE_60_DEGREE * ration;
-	_sensor_hander.estimate_delta_angle = angle_step;
-	if (angle_step >= PHASE_60_DEGREE) {
-		angle_step = PHASE_60_DEGREE;
-	}
+	float angle_step = _sensor_hander.estimate_el_speed * us_2_s(us_now);
+	_sensor_hander.estimate_delta_angle += angle_step;
 
 	if (_sensor_hander.direction == POSITIVE) {
-		_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle + angle_step;
+		_sensor_hander.estimate_el_angle += angle_step;
 	}else {
-		_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle - angle_step;
+		_sensor_hander.estimate_el_angle -= angle_step;
 	}
 
 	rand_angle(_sensor_hander.estimate_el_angle);
@@ -169,14 +145,9 @@ void hall_sensor_set_theta(bool override, float theta){
 }
 
 float hall_sensor_get_speed(void) {
-	_sensor_hander.rpm = _sensor_hander.el_speed / 360.0f * 60.0f;
 	return _sensor_hander.rpm;
 }
 
-float hall_sensor_avg_speed(void) {
-	return _sensor_hander.el_speed / 360 * 60.0f;
-}
-
 int hall_offset_increase(int inc) {
 	if (_sensor_hander.phase_offset + inc >= 360) {
 		_sensor_hander.phase_offset = _sensor_hander.phase_offset + inc - 360;
@@ -186,12 +157,16 @@ int hall_offset_increase(int inc) {
 	return _sensor_hander.phase_offset;
 }
 
+u16 *hall_get_table(void) {
+	return _sensor_hander.angle_table;
+}
 
 int hall_sensor_calibrate(float voltage){
 	foc_set_controller_mode(FOC_MODE_OPEN_LOOP);
 	hall_sensor_set_theta(true, 0.0f);
 	foc_set_dq_command(0.0f, 0.0f);
 	foc_pwm_start(true);
+	foc_start_adc(true);
 	for (int i = 0;i < 100;i++) {
 		foc_set_dq_command((float)i * voltage / 100.0f, 0.0f);
 		co_task_delay(1);
@@ -239,10 +214,15 @@ int hall_sensor_calibrate(float voltage){
 			hall_iterations[hall]++;
 		}
 	}
-#endif	
+#endif
+	for (int i = 99;i >= 0;i--) {
+		foc_set_dq_command((float)i * voltage / 100.0f, 0.0f);
+		co_task_delay(1);
+		wdog_reload();
+	}
 	foc_pwm_start(false);
+	foc_start_adc(false);
 	hall_sensor_set_theta(false, 0.0f);
-	foc_set_dq_command(0.0f, 0.0f);
 	int fails = 0;
 	for(int i = 0;i < 8;i++) {
 		if (hall_iterations[i] > 30) {
@@ -250,6 +230,7 @@ int hall_sensor_calibrate(float voltage){
 			fast_norm_angle(&ang);
 			_sensor_hander.angle_table[i] = (u16)ang;
 			sys_debug("%d: %d\n", i, _sensor_hander.angle_table[i]);
+			co_task_delay(10);
 		} else {
 			fails++;
 			_sensor_hander.angle_table[i] = 0xFFFF;
@@ -262,37 +243,55 @@ int hall_sensor_calibrate(float voltage){
 
 static void _hall_init_el_angle(void) {
 	_sensor_hander.hall_stat = get_hall_stat(HALL_READ_TIMES);
+#ifdef USE_DETECTED_ANGLE
+	if (_sensor_hander.hall_stat == 0 || _sensor_hander.hall_stat == 7) {
+		_sensor_hander.sensor_error ++;
+		return;
+	}
+	_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + _sensor_hander.angle_table[_sensor_hander.hall_stat];
+#else
+	s32 sector_center = PHASE_60_DEGREE/2;
   	switch ( _sensor_hander.hall_stat )
   	{
     case STATE_5:
-      	_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE/2;
+      	_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + sector_center;
       	break;
     case STATE_1:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + PHASE_60_DEGREE / 2;
+		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_60_DEGREE + sector_center;
       	break;
     case STATE_3:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + PHASE_60_DEGREE / 2;
+		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_120_DEGREE + sector_center;
       	break;
     case STATE_2:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_180_DEGREE + PHASE_60_DEGREE / 2;
+		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_180_DEGREE + sector_center;
       	break;
     case STATE_6:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_240_DEGREE + PHASE_60_DEGREE / 2;
+		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_240_DEGREE + sector_center;
       	break;
     case STATE_4:
-		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_300_DEGREE + PHASE_60_DEGREE / 2;
+		_sensor_hander.measured_el_angle = _sensor_hander.phase_offset + PHASE_300_DEGREE + sector_center;
       	break;
     default:
       	/* Bad hall sensor configutarion so update the speed reliability */
       	_sensor_hander.sensor_error ++;
-      	break;
+      	return;
  	}
+#endif
+	_sensor_hander.sensor_error = 0;
   	/* Initialize the measured angle */
 	rand_angle(_sensor_hander.measured_el_angle);
   	_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
 	_sensor_hander.hall_ticks = timer_count32_get();
+	_sensor_hander.estimate_time_ticks = timer_count32_get();
 }
 
+static __inline__ s32 _get_angle(u8 state, s32 added) {
+#ifdef USE_DETECTED_ANGLE
+	return _sensor_hander.phase_offset + _sensor_hander.angle_table[state];
+#else
+	return _sensor_hander.phase_offset + added;
+#endif
+}
 /* 4,5,1,3,2,6,4 */
 static s32 _hall_position(u8 state_now, u8 state_prev) {
 	s32 theta_now =  0xFFFFFFFF;
@@ -300,55 +299,55 @@ static s32 _hall_position(u8 state_now, u8 state_prev) {
 		case STATE_1:
 			if (state_prev == STATE_5) {
 				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_60_DEGREE);//_sensor_hander.phase_offset + PHASE_60_DEGREE;
 			}else if (state_prev == STATE_3) {
 				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_120_DEGREE);//_sensor_hander.phase_offset + PHASE_120_DEGREE;
 			}
 			break;
 		case STATE_2:
 			if (state_prev == STATE_3) {
 				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_180_DEGREE);//_sensor_hander.phase_offset + PHASE_180_DEGREE;
 			}else if (state_prev == STATE_6) {
 				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_240_DEGREE);//_sensor_hander.phase_offset + PHASE_240_DEGREE;
 			}
 			break;
 		case STATE_3:
 			if (state_prev == STATE_1) {
 				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_120_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_120_DEGREE);//_sensor_hander.phase_offset + PHASE_120_DEGREE;
 			}else if (state_prev == STATE_2) {
 				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_180_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_180_DEGREE);//_sensor_hander.phase_offset + PHASE_180_DEGREE;
 			}
 			break;
 		case STATE_4:
 			if (state_prev == STATE_6) {
 				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_300_DEGREE);//_sensor_hander.phase_offset + PHASE_300_DEGREE;
 			}else if (state_prev == STATE_5) {
 				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset;
+				theta_now = _get_angle(state_now, PHASE_0_DEGREE);//_sensor_hander.phase_offset + PHASE_0_DEGREE;
 			}
 			break;
 		case STATE_5:
 			if (state_prev == STATE_4) {
 				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset;
+				theta_now = _get_angle(state_now, PHASE_0_DEGREE);//_sensor_hander.phase_offset + PHASE_0_DEGREE;
 			}else if (state_prev == STATE_1) {
 				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_60_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_60_DEGREE);//_sensor_hander.phase_offset + PHASE_60_DEGREE;
 			}
 			break;
 		case STATE_6:
 			if (state_prev == STATE_2) {
 				_sensor_hander.direction = POSITIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_240_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_240_DEGREE);//_sensor_hander.phase_offset + PHASE_240_DEGREE;
 			}else if (state_prev == STATE_4) {
 				_sensor_hander.direction = NEGATIVE;
-				theta_now = _sensor_hander.phase_offset + PHASE_300_DEGREE;
+				theta_now = _get_angle(state_now, PHASE_300_DEGREE);//_sensor_hander.phase_offset + PHASE_300_DEGREE;
 			}
 			break;
 		default:
@@ -359,6 +358,66 @@ static s32 _hall_position(u8 state_now, u8 state_prev) {
 	return theta_now;
 }
 
+static __inline u8 _next_hall(u8 hall_now) {
+	switch (hall_now) {
+		case STATE_1:
+			if (_sensor_hander.direction == POSITIVE) {
+				return STATE_3;
+			}else {
+				return STATE_5;
+			}
+		case STATE_2:
+			if (_sensor_hander.direction == POSITIVE) {
+				return STATE_6;
+			}else {
+				return STATE_3;
+			}
+		case STATE_3:
+			if (_sensor_hander.direction == POSITIVE) {
+				return STATE_2;
+			}else {
+				return STATE_1;
+			}
+		case STATE_4:
+			if (_sensor_hander.direction == POSITIVE) {
+				return STATE_5;
+			}else {
+				return STATE_6;
+			}
+		case STATE_5:
+			if (_sensor_hander.direction == POSITIVE) {
+				return STATE_1;
+			}else {
+				return STATE_4;
+			}
+		case STATE_6:
+			if (_sensor_hander.direction == POSITIVE) {
+				return STATE_4;
+			}else {
+				return STATE_2;
+			}
+		default: //not reached here
+			return STATE_1;
+	}
+
+} 
+
+#ifdef USE_DETECTED_ANGLE
+static __inline__ float _get_delta_angle(u8 now, u8 next) {
+	float delta_angle = _sensor_hander.angle_table[next] - _sensor_hander.angle_table[now];
+	if (_sensor_hander.direction == POSITIVE) {
+		if (delta_angle < 0) { //process cross 360 degree
+			delta_angle += 360.0f;
+		}
+	}else if (_sensor_hander.direction == NEGATIVE) {
+		if (delta_angle > 0) { //process cross 360 degree
+			delta_angle -= 360.0f;
+		}
+		delta_angle = -delta_angle;
+	}
+	return delta_angle;
+}
+#endif
 void hall_sensor_handler(void) {
 	if (_sensor_hander.is_override_angle) {
 		sys_debug("irq: %d:%d\n", (int)get_hall_stat(HALL_READ_TIMES), (int)_sensor_hander.override_el_angle);
@@ -369,41 +428,49 @@ void hall_sensor_handler(void) {
 	u8 hall_stat_prev = _sensor_hander.hall_stat;
 	u32 hall_ticks_now = timer_count32_get();	
 
+	/*获取当前转子角度*/
 	s32 theta_now = _hall_position(hall_stat_now, hall_stat_prev);
 	if (theta_now == 0xFFFFFFFF) {
 		return;
 	}
-
+	//获取两次中断的时间间隔,估计速度
 	u32 delta_us = timer_count32_getus(hall_ticks_now, _sensor_hander.hall_ticks);
 	if (delta_us == 0) {
 		return;
 	}
-
+	//获取两次中断之间转子转过的角度,获取预期的下次hall状态变换转过的角度
+#ifdef USE_DETECTED_ANGLE
+	float delta_angle = _get_delta_angle(hall_stat_prev, hall_stat_now);
+	float next_delta_angle = _get_delta_angle(hall_stat_now, _next_hall(hall_stat_now));
+#else
+	float delta_angle = (float)PHASE_60_DEGREE;
+	float next_delta_angle = delta_angle;
+#endif
 	float delta_time = us_2_s(delta_us);
-	_hall_put_sample(delta_us, hall_stat_prev);
-	u32 us_speed_prev = _sensor_hander.speed_us_for_estimate;
-	u32 us_speed_now = 0;
-	if (_hall_data_empty(hall_stat_now)) {
-		us_speed_now = delta_us;
+	float prev_imme_el_speed = _sensor_hander.immediately_el_speed;
+	_sensor_hander.immediately_el_speed = delta_angle/delta_time;
+	float delta_el_speed = fabs(_sensor_hander.immediately_el_speed - prev_imme_el_speed);
+	if (delta_el_speed/prev_imme_el_speed >= 0.1f) { //即时速度增加10%,认为不稳定,需要使用即时速度估计转子位置
+		_sensor_hander.trns_detect = true;
 	}else {
-		us_speed_now = _hall_angle_us_speed(hall_stat_now);
+		_sensor_hander.trns_detect = false;
 	}
-	if (us_speed_now != 0) {
-		hall_speed[hall_stat_now] = (float)us_speed_prev / (float)us_speed_now;
-	}
-	hall_angle[hall_stat_now] = (float)_sensor_hander.estimate_delta_angle/(float)PHASE_60_DEGREE;
+	_hall_put_sample(delta_us, delta_angle);
 	os_disable_irq();
-	
-	_sensor_hander.estimate_delta_angle = 0;
-	_sensor_hander.measured_el_angle = theta_now;	
-	_sensor_hander.estimate_el_angle = _sensor_hander.measured_el_angle;
-
-	_sensor_hander.estimate_time_ticks = hall_ticks_now;
-	_sensor_hander.speed_us_for_estimate = us_speed_now;
+	_sensor_hander.el_speed = _hall_angle_speed();	
+	_sensor_hander.estimate_delta_angle = _sensor_hander.estimate_delta_angle - delta_angle;
+	/*通过上次预估的转子位置,对当前的预估速度进行补偿*/
+	if (_sensor_hander.trns_detect) {
+		_sensor_hander.estimate_el_speed = (next_delta_angle - _sensor_hander.estimate_delta_angle)/delta_angle * _sensor_hander.immediately_el_speed;
+	}else {
+		_sensor_hander.estimate_el_speed = (next_delta_angle - _sensor_hander.estimate_delta_angle)/delta_angle * _sensor_hander.el_speed;
+	}
+	_sensor_hander.next_delta_angle = next_delta_angle;
+	//_sensor_hander.measured_el_angle = theta_now;
 	os_enable_irq();
-	_sensor_hander.el_speed = _hall_angle_speed();
 	_sensor_hander.hall_stat = hall_stat_now;
 	_sensor_hander.hall_ticks = hall_ticks_now;
+	_sensor_hander.rpm = _sensor_hander.el_speed / 360.0f * 60.0f;
 	
 	time_measure_end(&g_meas_hall);
 }

+ 12 - 6
Applications/foc/hall_sensor.h → Applications/foc/motor/hall.h

@@ -6,6 +6,7 @@
 #define NEGATIVE          (int8_t)-1
 #define POSITIVE          (int8_t)1
 
+#define PHASE_0_DEGREE (0)
 #define PHASE_60_DEGREE (60)
 #define PHASE_120_DEGREE (120)
 #define PHASE_180_DEGREE (180)
@@ -27,27 +28,31 @@
 
 typedef struct {
 	u32   ticks[SAMPLE_MAX_COUNT];
+	float angles[SAMPLE_MAX_COUNT];
 	u32   ticks_sum;
+	float angles_sum;
 	u32   index;
 	bool  full;
 }hall_sample_t;
 
 typedef struct {
 	float estimate_el_angle; //60度区间内的估计电角度
-	u32   estimate_time_ticks;
-	s32   estimate_delta_angle;//for debug
+	s32   estimate_delta_angle;
+	float estimate_el_speed;
+	u32   estimate_time_ticks;	
 	s32   measured_el_angle; //hall测量到的电角度
-	u32   speed_us_for_estimate; //hall经过60°的时间,给角度估计使用
-	float el_speed; 		//当前的电角速度, 单位:rad/s
+	float immediately_el_speed; //当前的即时速度,主要用来判断电机转动是否达到稳定
+	float el_speed; 		//当前的平均效果的电角速度, 单位:rad/s
+	float next_delta_angle;
 	float rpm;        		//当前的电速度, 单位:RPM
+	bool  trns_detect;     //速度变化超过阈值
 	u8    hall_stat;
 	u32   hall_ticks;
-	bool  working;
 	s8    direction;
 	s32   phase_offset;
 	bool  is_override_angle;
 	float override_el_angle;
-	hall_sample_t samples[8];
+	hall_sample_t samples;
 	u32  sensor_error;
 	u16  angle_table[8];
 }hall_sensor_t;
@@ -60,6 +65,7 @@ float hall_sensor_avg_speed(void);
 int hall_sensor_calibrate(float voltage);
 void hall_sensor_set_theta(bool override, float theta);
 int hall_offset_increase(int inc);
+u16 *hall_get_table(void);
 
 #endif /* _HALL_SENSOR_H__ */
 

+ 78 - 0
Applications/foc/motor/motor.c

@@ -0,0 +1,78 @@
+#include "foc/motor/motor.h"
+#include "foc/motor/hall.h"
+#include "math/fast_math.h"
+#include "bsp/bsp.h"
+#include "bsp/adc.h"
+#include "bsp/pwm.h"
+
+const motor_param_t motor_params = {
+	.poles = 5,
+	.ld = 0.578477f,
+	.lq = 5.78477f,
+	.rs = 1.088f,
+	.inertia = 3.319367f,
+	.b_emf = 4.332566f,
+};
+
+static bool _motor_started = false;
+
+void motor_start() {
+	if (_motor_started) {
+		return;
+	}
+	pwm_start();
+	adc_start_convert();
+	_motor_started = true;
+}
+
+void motor_stop() {
+	if (!_motor_started) {
+		return;
+	}
+	adc_stop_convert();
+	pwm_stop();
+	_motor_started = false;
+}
+
+void motor_drvier_low_side(bool on){
+	if (on) {
+		pwm_turn_on_low_side();
+	}
+}
+
+u32  motor_get_raw_speed(void) {
+	return (u32)hall_sensor_get_speed();
+}
+
+u32  motor_get_speed(void) {
+	static u32 _speed = 0;
+	float raw_speed = hall_sensor_get_speed();
+	if (raw_speed == 0.0f) {
+		_speed = 0;
+	}
+	_speed = LowPass_Filter(_speed, raw_speed, 0.8f);
+	return _speed;
+}
+
+
+void motor_update_duty(s32 A, s32 B, s32 C, s32 next_a, s32 next_b, s32 next_c) {
+#if SHUNT_NUM==THREE_SHUNTS_SAMPLE	
+	pwm_update_duty(A, B, C);
+#else
+	pwm_wait_and_clear_updata();
+	pwm_update_duty_dma(A, B, C, next_a, next_b, next_c);
+#endif
+}
+void motor_update_sample(u32 samp1, u32 samp2, u8 sector) {
+	pwm_update_2smaples(samp1, samp2);
+#ifdef ENABLE_AUX_TIMER
+	if (samp2 < FOC_PWM_Half_Period) {
+		adc_update_ext_trigger(ADC_TRIGGER_PHASE);
+	}else {
+		adc_update_ext_trigger(ADC_TRIGGER_PHASE2);
+	}
+#endif	
+	adc_current_sample_config(sector);
+}
+
+

+ 24 - 0
Applications/foc/motor/motor.h

@@ -0,0 +1,24 @@
+#ifndef _MOTOR_H__
+#define _MOTOR_H__
+#include "os/os_type.h"
+
+typedef struct _motor_p {
+	int   poles; //电机极对数
+	float  ld;    //q轴电感
+	float  lq;    //d轴电感
+	float  rs;     //定子内阻
+	float  flux_linkage; //永磁磁链
+	float  inertia; 
+	float  b_emf;
+}motor_param_t;
+
+extern const motor_param_t motor_params;
+void motor_start(void);
+void motor_stop(void);
+void motor_drvier_low_side(bool on);
+void motor_update_duty(s32 A, s32 B, s32 C, s32 next_a, s32 next_b, s32 next_c);
+void motor_update_sample(u32 samp1, u32 samp2, u8 sector);
+u32  motor_get_raw_speed(void); //给pid使用
+u32  motor_get_speed(void);
+#endif /* _MOTOR_H__ */
+

+ 1 - 1
Applications/foc/ntc_sensor.c → Applications/foc/motor/ntc.c

@@ -1,4 +1,4 @@
-#include "ntc_sensor.h"
+#include "ntc.h"
 #include "bsp/bsp.h"
 #include "bsp/adc.h"
 

+ 0 - 0
Applications/foc/ntc_sensor.h → Applications/foc/motor/ntc.h


+ 61 - 0
Applications/foc/samples.c

@@ -0,0 +1,61 @@
+#include "foc/samples.h"
+#include "bsp/bsp.h"
+#include "bsp/adc.h"
+#include "math/fast_math.h"
+#include "os/co_task.h"
+#include "os/timer.h"
+
+typedef struct {
+	float value;
+	float filted_value;
+	float lowpass;
+}samples_t;
+
+static void sample_vbus(void);
+static void sample_gas(void);
+static void sample_timer_handler(timer_t *);
+static samples_t _vbus;
+static samples_t _gas;
+static timer_t   sample_timer = TIMER_INIT(sample_timer, sample_timer_handler);
+void samples_init(void){
+	_vbus.filted_value = MAX_VBUS_VOLTAGE;
+	_vbus.value = MAX_VBUS_VOLTAGE;
+	_vbus.lowpass = 0.2f;
+	_gas.filted_value = 0.0f;
+	_gas.value = 0.0f;
+	_gas.lowpass = 0.2f;
+
+	sample_vbus();
+	sample_gas();
+	timer_post(&sample_timer, 1);
+}
+
+float get_vbus_sample(void) {
+	return _vbus.filted_value;
+}
+
+float get_gas_sample(void) {
+	return _gas.filted_value;
+}
+
+
+static void sample_timer_handler(timer_t *timer) {
+	sample_vbus();
+	sample_gas();
+	timer_post(&sample_timer, 5);
+}
+
+static void sample_vbus(void){
+	u32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
+	_vbus.value = ((float)vadc)/(4096.0f) * ADC_REFERENCE_VOLTAGE * 45 / 1000; //1:44
+	LowPass_Filter(_vbus.filted_value, _vbus.value, _vbus.lowpass);
+}
+
+static void sample_gas(void){
+#ifdef GD32_DEMO
+#else
+	u32 vadc = adc_sample_regular_channel(HANDLERBAR_CHAN, 16);
+	_gas.value = vadc * ADC_REFERENCE_VOLTAGE /4096.0f;
+	LowPass_Filter(_gas.filted_value, _gas.value, _gas.lowpass);
+#endif	
+}

+ 12 - 0
Applications/foc/samples.h

@@ -0,0 +1,12 @@
+#ifndef _SAMPLES_H__
+#define _SAMPLES_H__
+
+#define MIN_GAS_VALUE 1000.0F
+#define MAX_GAS_VALUE 5000.0F
+
+void samples_init(void);
+float get_vbus_sample(void);
+float get_gas_sample(void);
+
+#endif /* _SAMPLES_H__ */
+

+ 0 - 27
Applications/foc/vbus_sensor.c

@@ -1,27 +0,0 @@
-#include "vbus_sensor.h"
-#include "bsp/bsp.h"
-#include "bsp/adc.h"
-#include "math/fast_math.h"
-
-static vbus_t _vbus;
-void vbus_sensor_init(void){
-	_vbus.voltage = 0;
-	_vbus.voltage_filted = MAX_VBUS_VOLTAGE;
-	_vbus.avg_count = 5;
-	vbus_sample_voltage();
-}
-
-void vbus_sample_voltage(void){
-	u32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
-
-	_vbus.voltage = ((float)vadc)/(4096.0f) * ADC_REFERENCE_VOLTAGE * 45 / 1000; //1:44
-
-	LowPass_Filter(_vbus.voltage_filted, _vbus.voltage, 0.1f);
-}
-
-
-float vbus_get_filted_voltage(void){
-
-	return _vbus.voltage_filted;
-}
-

+ 0 - 16
Applications/foc/vbus_sensor.h

@@ -1,16 +0,0 @@
-#ifndef _VBUS_SENSOR_H__
-#define _VBUS_SENSOR_H__
-
-
-typedef struct {
-	float voltage;
-	float voltage_filted;
-	int avg_count;
-}vbus_t;
-
-void vbus_sensor_init(void);
-void vbus_sample_voltage(void);
-float vbus_get_filted_voltage(void);
-
-#endif /* _VBUS_SENSOR_H__ */
-

+ 3 - 2
Applications/libs/backtrace.c

@@ -3,6 +3,7 @@
 #include <stdio.h>
 #include "libs/backtrace.h"
 #include "bsp/gd32_bkp.h"
+#include "os/co_task.h"
 
 #if defined(__CC_ARM)
     #define SECTION_START(_name_)                _name_##$$Base
@@ -32,8 +33,8 @@
 
 //extern int STACK$$Base;
 static void get_cur_thread_stack_info(uint32_t sp, uint32_t *start_addr, size_t *size) {
-    *start_addr = SECTION_START(STACK);//(uint32_t)vTaskStackAddr();
-    *size = SECTION_END(STACK) - SECTION_START(STACK);//vTaskStackSize() * sizeof( StackType_t );
+    *start_addr = co_task_current_stack();
+    *size = co_task_stack_size();
 }
 
 #ifdef __TARGET_FPU_VFP

+ 9 - 0
Applications/os/co_task.c

@@ -9,6 +9,7 @@ typedef enum {
 typedef struct _tcb{
 	volatile u32 fp;
 	volatile u32 *pstack; //stack addr
+	u32          stack_size;
 	u32 start_tm_ms;
 	Task_Stat_t  stat; //task stat, running, pending, blocked
 	struct _tcb *next;
@@ -57,6 +58,14 @@ void co_task_yield(void){
 	}
 }
 
+u32 co_task_current_stack(void) {
+	return (u32)current_tcb->pstack;
+}
+
+u32 co_task_stack_size(void) {
+	return current_tcb->stack_size;
+}
+
 void co_task_delay(int ms){
 	current_tcb->start_tm_ms = ms + ticks_2_ms(g_task_ticks);
 	while(1) {

+ 2 - 0
Applications/os/co_task.h

@@ -27,6 +27,8 @@ void *co_malloc(u32 size);
 void co_free(void *ptr);
 float cpu_count_times(u32 prev);
 float cpu_times_bew_counts(u32 now, u32 prev);
+u32 co_task_current_stack(void);
+u32 co_task_stack_size(void);
 
 #define cpu_count_2_time(c) ((float)c / (float)SystemCoreClock)
 

+ 1 - 1
Applications/prot/can_foc_msg.c

@@ -1,7 +1,7 @@
 #include "libs/utils.h"
 #include "prot/can_message.h"
 #include "prot/can_foc_msg.h"
-#include "foc/foc_cmd.h"
+#include "foc/commands.h"
 
 void can_report_speed(int can, int rpm) {
 	u8 data[6];

+ 1 - 1
Applications/prot/can_message.c

@@ -5,7 +5,7 @@
 #include "can_message.h"
 #include "wait_queue.h"
 #include "can_pc_message.h"
-#include "foc/foc_cmd.h"
+#include "foc/commands.h"
 static void can_process_message(can_message_t *message);
 static void free_can_message(can_message_t *message);
 

+ 162 - 154
Project/MC100_OS.uvoptx

@@ -240,7 +240,7 @@
 
   <Group>
     <GroupName>Foc</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
@@ -251,7 +251,7 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\circle_limitation.c</PathWithFileName>
+      <PathWithFileName>..\Applications\foc\core\circle_limitation.c</PathWithFileName>
       <FilenameWithoutPath>circle_limitation.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
@@ -263,7 +263,7 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\foc_api.c</PathWithFileName>
+      <PathWithFileName>..\Applications\foc\core\foc_api.c</PathWithFileName>
       <FilenameWithoutPath>foc_api.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
@@ -275,7 +275,7 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\foc_core.c</PathWithFileName>
+      <PathWithFileName>..\Applications\foc\core\foc_core.c</PathWithFileName>
       <FilenameWithoutPath>foc_core.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
@@ -287,8 +287,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\gas_sensor.c</PathWithFileName>
-      <FilenameWithoutPath>gas_sensor.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\core\foc_fsm.c</PathWithFileName>
+      <FilenameWithoutPath>foc_fsm.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -299,8 +299,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\hall_sensor.c</PathWithFileName>
-      <FilenameWithoutPath>hall_sensor.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\core\pi_controller.c</PathWithFileName>
+      <FilenameWithoutPath>pi_controller.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -311,8 +311,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\ntc_sensor.c</PathWithFileName>
-      <FilenameWithoutPath>ntc_sensor.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\core\ramp_ctrl.c</PathWithFileName>
+      <FilenameWithoutPath>ramp_ctrl.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -323,8 +323,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\phase_current.c</PathWithFileName>
-      <FilenameWithoutPath>phase_current.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\core\svpwm.c</PathWithFileName>
+      <FilenameWithoutPath>svpwm.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -335,8 +335,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\pi_controller.c</PathWithFileName>
-      <FilenameWithoutPath>pi_controller.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\commands.c</PathWithFileName>
+      <FilenameWithoutPath>commands.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -347,99 +347,99 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\ramp_ctrl.c</PathWithFileName>
-      <FilenameWithoutPath>ramp_ctrl.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\samples.c</PathWithFileName>
+      <FilenameWithoutPath>samples.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+  </Group>
+
+  <Group>
+    <GroupName>Motor</GroupName>
+    <tvExp>0</tvExp>
+    <tvExpOptDlg>0</tvExpOptDlg>
+    <cbSel>0</cbSel>
+    <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>2</GroupNumber>
+      <GroupNumber>3</GroupNumber>
       <FileNumber>13</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\svpwm.c</PathWithFileName>
-      <FilenameWithoutPath>svpwm.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\motor\current.c</PathWithFileName>
+      <FilenameWithoutPath>current.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>2</GroupNumber>
+      <GroupNumber>3</GroupNumber>
       <FileNumber>14</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\vbus_sensor.c</PathWithFileName>
-      <FilenameWithoutPath>vbus_sensor.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\motor\hall.c</PathWithFileName>
+      <FilenameWithoutPath>hall.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>2</GroupNumber>
+      <GroupNumber>3</GroupNumber>
       <FileNumber>15</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\foc_cmd.c</PathWithFileName>
-      <FilenameWithoutPath>foc_cmd.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\motor\motor.c</PathWithFileName>
+      <FilenameWithoutPath>motor.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>2</GroupNumber>
+      <GroupNumber>3</GroupNumber>
       <FileNumber>16</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\foc\foc_fsm.c</PathWithFileName>
-      <FilenameWithoutPath>foc_fsm.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\foc\motor\ntc.c</PathWithFileName>
+      <FilenameWithoutPath>ntc.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
   </Group>
 
   <Group>
-    <GroupName>Math</GroupName>
+    <GroupName>Proto</GroupName>
     <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>3</GroupNumber>
+      <GroupNumber>4</GroupNumber>
       <FileNumber>17</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\math\fast_math.c</PathWithFileName>
-      <FilenameWithoutPath>fast_math.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\prot\can_message.c</PathWithFileName>
+      <FilenameWithoutPath>can_message.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>3</GroupNumber>
+      <GroupNumber>4</GroupNumber>
       <FileNumber>18</FileNumber>
-      <FileType>4</FileType>
+      <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\math\arm_cortexM4lf_math.lib</PathWithFileName>
-      <FilenameWithoutPath>arm_cortexM4lf_math.lib</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\prot\wait_queue.c</PathWithFileName>
+      <FilenameWithoutPath>wait_queue.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
-  </Group>
-
-  <Group>
-    <GroupName>Proto</GroupName>
-    <tvExp>1</tvExp>
-    <tvExpOptDlg>0</tvExpOptDlg>
-    <cbSel>0</cbSel>
-    <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>4</GroupNumber>
       <FileNumber>19</FileNumber>
@@ -447,8 +447,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\prot\can_message.c</PathWithFileName>
-      <FilenameWithoutPath>can_message.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\prot\can_pc_message.c</PathWithFileName>
+      <FilenameWithoutPath>can_pc_message.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -459,123 +459,123 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\prot\wait_queue.c</PathWithFileName>
-      <FilenameWithoutPath>wait_queue.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\prot\can_foc_msg.c</PathWithFileName>
+      <FilenameWithoutPath>can_foc_msg.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+  </Group>
+
+  <Group>
+    <GroupName>Math</GroupName>
+    <tvExp>0</tvExp>
+    <tvExpOptDlg>0</tvExpOptDlg>
+    <cbSel>0</cbSel>
+    <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>4</GroupNumber>
+      <GroupNumber>5</GroupNumber>
       <FileNumber>21</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\prot\can_pc_message.c</PathWithFileName>
-      <FilenameWithoutPath>can_pc_message.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\math\fast_math.c</PathWithFileName>
+      <FilenameWithoutPath>fast_math.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>4</GroupNumber>
+      <GroupNumber>5</GroupNumber>
       <FileNumber>22</FileNumber>
-      <FileType>1</FileType>
+      <FileType>4</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\prot\can_foc_msg.c</PathWithFileName>
-      <FilenameWithoutPath>can_foc_msg.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\math\arm_cortexM4lf_math.lib</PathWithFileName>
+      <FilenameWithoutPath>arm_cortexM4lf_math.lib</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
   </Group>
 
   <Group>
-    <GroupName>OS</GroupName>
+    <GroupName>BSP</GroupName>
     <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>5</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>23</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\os\port\cortex-m4.c</PathWithFileName>
-      <FilenameWithoutPath>cortex-m4.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\bsp.c</PathWithFileName>
+      <FilenameWithoutPath>bsp.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>5</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>24</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\os\co_task.c</PathWithFileName>
-      <FilenameWithoutPath>co_task.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\gd32_bkp.c</PathWithFileName>
+      <FilenameWithoutPath>gd32_bkp.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>5</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>25</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\os\cpu.c</PathWithFileName>
-      <FilenameWithoutPath>cpu.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\gd32_rtc.c</PathWithFileName>
+      <FilenameWithoutPath>gd32_rtc.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>5</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>26</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\os\heap_4.c</PathWithFileName>
-      <FilenameWithoutPath>heap_4.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\gpio.c</PathWithFileName>
+      <FilenameWithoutPath>gpio.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>5</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>27</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\os\timer.c</PathWithFileName>
-      <FilenameWithoutPath>timer.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\i2c.c</PathWithFileName>
+      <FilenameWithoutPath>i2c.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>5</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>28</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\os\queue.c</PathWithFileName>
-      <FilenameWithoutPath>queue.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\can.c</PathWithFileName>
+      <FilenameWithoutPath>can.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
-  </Group>
-
-  <Group>
-    <GroupName>Libs</GroupName>
-    <tvExp>0</tvExp>
-    <tvExpOptDlg>0</tvExpOptDlg>
-    <cbSel>0</cbSel>
-    <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>6</GroupNumber>
       <FileNumber>29</FileNumber>
@@ -583,8 +583,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\libs\backtrace.c</PathWithFileName>
-      <FilenameWithoutPath>backtrace.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\fmc_flash.c</PathWithFileName>
+      <FilenameWithoutPath>fmc_flash.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -595,8 +595,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\libs\circle_buffer.c</PathWithFileName>
-      <FilenameWithoutPath>circle_buffer.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\pwm.c</PathWithFileName>
+      <FilenameWithoutPath>pwm.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -607,8 +607,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\libs\logger.c</PathWithFileName>
-      <FilenameWithoutPath>logger.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\mc_irqs.c</PathWithFileName>
+      <FilenameWithoutPath>mc_irqs.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -619,8 +619,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\libs\crc16.c</PathWithFileName>
-      <FilenameWithoutPath>crc16.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\mc_hall_gpio.c</PathWithFileName>
+      <FilenameWithoutPath>mc_hall_gpio.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -631,8 +631,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\libs\byte_queue.c</PathWithFileName>
-      <FilenameWithoutPath>byte_queue.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\adc.c</PathWithFileName>
+      <FilenameWithoutPath>adc.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -643,31 +643,31 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\libs\gtime.c</PathWithFileName>
-      <FilenameWithoutPath>gtime.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\uart.c</PathWithFileName>
+      <FilenameWithoutPath>uart.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
-  </Group>
-
-  <Group>
-    <GroupName>BSP</GroupName>
-    <tvExp>1</tvExp>
-    <tvExpOptDlg>0</tvExpOptDlg>
-    <cbSel>0</cbSel>
-    <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>6</GroupNumber>
       <FileNumber>35</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\bsp.c</PathWithFileName>
-      <FilenameWithoutPath>bsp.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\bsp\timer_count32.c</PathWithFileName>
+      <FilenameWithoutPath>timer_count32.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+  </Group>
+
+  <Group>
+    <GroupName>Libs</GroupName>
+    <tvExp>0</tvExp>
+    <tvExpOptDlg>0</tvExpOptDlg>
+    <cbSel>0</cbSel>
+    <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>7</GroupNumber>
       <FileNumber>36</FileNumber>
@@ -675,8 +675,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\gd32_bkp.c</PathWithFileName>
-      <FilenameWithoutPath>gd32_bkp.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\libs\backtrace.c</PathWithFileName>
+      <FilenameWithoutPath>backtrace.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -687,8 +687,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\gd32_rtc.c</PathWithFileName>
-      <FilenameWithoutPath>gd32_rtc.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\libs\circle_buffer.c</PathWithFileName>
+      <FilenameWithoutPath>circle_buffer.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -699,8 +699,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\gpio.c</PathWithFileName>
-      <FilenameWithoutPath>gpio.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\libs\logger.c</PathWithFileName>
+      <FilenameWithoutPath>logger.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -711,8 +711,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\i2c.c</PathWithFileName>
-      <FilenameWithoutPath>i2c.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\libs\crc16.c</PathWithFileName>
+      <FilenameWithoutPath>crc16.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -723,8 +723,8 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\can.c</PathWithFileName>
-      <FilenameWithoutPath>can.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\libs\byte_queue.c</PathWithFileName>
+      <FilenameWithoutPath>byte_queue.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -735,80 +735,88 @@
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\fmc_flash.c</PathWithFileName>
-      <FilenameWithoutPath>fmc_flash.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\libs\gtime.c</PathWithFileName>
+      <FilenameWithoutPath>gtime.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+  </Group>
+
+  <Group>
+    <GroupName>OS</GroupName>
+    <tvExp>0</tvExp>
+    <tvExpOptDlg>0</tvExpOptDlg>
+    <cbSel>0</cbSel>
+    <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>8</GroupNumber>
       <FileNumber>42</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\pwm.c</PathWithFileName>
-      <FilenameWithoutPath>pwm.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\os\port\cortex-m4.c</PathWithFileName>
+      <FilenameWithoutPath>cortex-m4.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>8</GroupNumber>
       <FileNumber>43</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\mc_irqs.c</PathWithFileName>
-      <FilenameWithoutPath>mc_irqs.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\os\co_task.c</PathWithFileName>
+      <FilenameWithoutPath>co_task.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>8</GroupNumber>
       <FileNumber>44</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\mc_hall_gpio.c</PathWithFileName>
-      <FilenameWithoutPath>mc_hall_gpio.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\os\cpu.c</PathWithFileName>
+      <FilenameWithoutPath>cpu.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>8</GroupNumber>
       <FileNumber>45</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\adc.c</PathWithFileName>
-      <FilenameWithoutPath>adc.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\os\heap_4.c</PathWithFileName>
+      <FilenameWithoutPath>heap_4.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>8</GroupNumber>
       <FileNumber>46</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\uart.c</PathWithFileName>
-      <FilenameWithoutPath>uart.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\os\timer.c</PathWithFileName>
+      <FilenameWithoutPath>timer.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>7</GroupNumber>
+      <GroupNumber>8</GroupNumber>
       <FileNumber>47</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
       <bDave2>0</bDave2>
-      <PathWithFileName>..\Applications\bsp\timer_count32.c</PathWithFileName>
-      <FilenameWithoutPath>timer_count32.c</FilenameWithoutPath>
+      <PathWithFileName>..\Applications\os\queue.c</PathWithFileName>
+      <FilenameWithoutPath>queue.c</FilenameWithoutPath>
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
@@ -821,7 +829,7 @@
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>48</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -833,7 +841,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>49</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -845,7 +853,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>50</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -857,7 +865,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>51</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -869,7 +877,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>52</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -881,7 +889,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>53</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -893,7 +901,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>54</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -905,7 +913,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>55</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -917,7 +925,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>56</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -929,7 +937,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>57</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -941,7 +949,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>58</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -953,7 +961,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>59</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -965,7 +973,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>60</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -977,7 +985,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>61</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -989,7 +997,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>62</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -1001,7 +1009,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>8</GroupNumber>
+      <GroupNumber>9</GroupNumber>
       <FileNumber>63</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -1016,12 +1024,12 @@
 
   <Group>
     <GroupName>StartUp</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
-      <GroupNumber>9</GroupNumber>
+      <GroupNumber>10</GroupNumber>
       <FileNumber>64</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
@@ -1033,7 +1041,7 @@
       <bShared>0</bShared>
     </File>
     <File>
-      <GroupNumber>9</GroupNumber>
+      <GroupNumber>10</GroupNumber>
       <FileNumber>65</FileNumber>
       <FileType>2</FileType>
       <tvExp>0</tvExp>

+ 102 - 97
Project/MC100_OS.uvprojx

@@ -406,67 +406,97 @@
             <File>
               <FileName>circle_limitation.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\circle_limitation.c</FilePath>
+              <FilePath>..\Applications\foc\core\circle_limitation.c</FilePath>
             </File>
             <File>
               <FileName>foc_api.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\foc_api.c</FilePath>
+              <FilePath>..\Applications\foc\core\foc_api.c</FilePath>
             </File>
             <File>
               <FileName>foc_core.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\foc_core.c</FilePath>
+              <FilePath>..\Applications\foc\core\foc_core.c</FilePath>
             </File>
             <File>
-              <FileName>gas_sensor.c</FileName>
+              <FileName>foc_fsm.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\gas_sensor.c</FilePath>
+              <FilePath>..\Applications\foc\core\foc_fsm.c</FilePath>
             </File>
             <File>
-              <FileName>hall_sensor.c</FileName>
+              <FileName>pi_controller.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\hall_sensor.c</FilePath>
+              <FilePath>..\Applications\foc\core\pi_controller.c</FilePath>
             </File>
             <File>
-              <FileName>ntc_sensor.c</FileName>
+              <FileName>ramp_ctrl.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\ntc_sensor.c</FilePath>
+              <FilePath>..\Applications\foc\core\ramp_ctrl.c</FilePath>
             </File>
             <File>
-              <FileName>phase_current.c</FileName>
+              <FileName>svpwm.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\phase_current.c</FilePath>
+              <FilePath>..\Applications\foc\core\svpwm.c</FilePath>
             </File>
             <File>
-              <FileName>pi_controller.c</FileName>
+              <FileName>commands.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\pi_controller.c</FilePath>
+              <FilePath>..\Applications\foc\commands.c</FilePath>
             </File>
             <File>
-              <FileName>ramp_ctrl.c</FileName>
+              <FileName>samples.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\foc\samples.c</FilePath>
+            </File>
+          </Files>
+        </Group>
+        <Group>
+          <GroupName>Motor</GroupName>
+          <Files>
+            <File>
+              <FileName>current.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\ramp_ctrl.c</FilePath>
+              <FilePath>..\Applications\foc\motor\current.c</FilePath>
             </File>
             <File>
-              <FileName>svpwm.c</FileName>
+              <FileName>hall.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\svpwm.c</FilePath>
+              <FilePath>..\Applications\foc\motor\hall.c</FilePath>
             </File>
             <File>
-              <FileName>vbus_sensor.c</FileName>
+              <FileName>motor.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\vbus_sensor.c</FilePath>
+              <FilePath>..\Applications\foc\motor\motor.c</FilePath>
             </File>
             <File>
-              <FileName>foc_cmd.c</FileName>
+              <FileName>ntc.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\foc_cmd.c</FilePath>
+              <FilePath>..\Applications\foc\motor\ntc.c</FilePath>
             </File>
+          </Files>
+        </Group>
+        <Group>
+          <GroupName>Proto</GroupName>
+          <Files>
             <File>
-              <FileName>foc_fsm.c</FileName>
+              <FileName>can_message.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\prot\can_message.c</FilePath>
+            </File>
+            <File>
+              <FileName>wait_queue.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\prot\wait_queue.c</FilePath>
+            </File>
+            <File>
+              <FileName>can_pc_message.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\prot\can_pc_message.c</FilePath>
+            </File>
+            <File>
+              <FileName>can_foc_msg.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\foc\foc_fsm.c</FilePath>
+              <FilePath>..\Applications\prot\can_foc_msg.c</FilePath>
             </File>
           </Files>
         </Group>
@@ -486,62 +516,72 @@
           </Files>
         </Group>
         <Group>
-          <GroupName>Proto</GroupName>
+          <GroupName>BSP</GroupName>
           <Files>
             <File>
-              <FileName>can_message.c</FileName>
+              <FileName>bsp.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\prot\can_message.c</FilePath>
+              <FilePath>..\Applications\bsp\bsp.c</FilePath>
             </File>
             <File>
-              <FileName>wait_queue.c</FileName>
+              <FileName>gd32_bkp.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\prot\wait_queue.c</FilePath>
+              <FilePath>..\Applications\bsp\gd32_bkp.c</FilePath>
             </File>
             <File>
-              <FileName>can_pc_message.c</FileName>
+              <FileName>gd32_rtc.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\prot\can_pc_message.c</FilePath>
+              <FilePath>..\Applications\bsp\gd32_rtc.c</FilePath>
             </File>
             <File>
-              <FileName>can_foc_msg.c</FileName>
+              <FileName>gpio.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\prot\can_foc_msg.c</FilePath>
+              <FilePath>..\Applications\bsp\gpio.c</FilePath>
             </File>
-          </Files>
-        </Group>
-        <Group>
-          <GroupName>OS</GroupName>
-          <Files>
             <File>
-              <FileName>cortex-m4.c</FileName>
+              <FileName>i2c.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\os\port\cortex-m4.c</FilePath>
+              <FilePath>..\Applications\bsp\i2c.c</FilePath>
             </File>
             <File>
-              <FileName>co_task.c</FileName>
+              <FileName>can.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\os\co_task.c</FilePath>
+              <FilePath>..\Applications\bsp\can.c</FilePath>
             </File>
             <File>
-              <FileName>cpu.c</FileName>
+              <FileName>fmc_flash.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\os\cpu.c</FilePath>
+              <FilePath>..\Applications\bsp\fmc_flash.c</FilePath>
             </File>
             <File>
-              <FileName>heap_4.c</FileName>
+              <FileName>pwm.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\os\heap_4.c</FilePath>
+              <FilePath>..\Applications\bsp\pwm.c</FilePath>
             </File>
             <File>
-              <FileName>timer.c</FileName>
+              <FileName>mc_irqs.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\os\timer.c</FilePath>
+              <FilePath>..\Applications\bsp\mc_irqs.c</FilePath>
             </File>
             <File>
-              <FileName>queue.c</FileName>
+              <FileName>mc_hall_gpio.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\os\queue.c</FilePath>
+              <FilePath>..\Applications\bsp\mc_hall_gpio.c</FilePath>
+            </File>
+            <File>
+              <FileName>adc.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\bsp\adc.c</FilePath>
+            </File>
+            <File>
+              <FileName>uart.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\bsp\uart.c</FilePath>
+            </File>
+            <File>
+              <FileName>timer_count32.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\bsp\timer_count32.c</FilePath>
             </File>
           </Files>
         </Group>
@@ -581,72 +621,37 @@
           </Files>
         </Group>
         <Group>
-          <GroupName>BSP</GroupName>
+          <GroupName>OS</GroupName>
           <Files>
             <File>
-              <FileName>bsp.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\bsp.c</FilePath>
-            </File>
-            <File>
-              <FileName>gd32_bkp.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\gd32_bkp.c</FilePath>
-            </File>
-            <File>
-              <FileName>gd32_rtc.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\gd32_rtc.c</FilePath>
-            </File>
-            <File>
-              <FileName>gpio.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\gpio.c</FilePath>
-            </File>
-            <File>
-              <FileName>i2c.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\i2c.c</FilePath>
-            </File>
-            <File>
-              <FileName>can.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\can.c</FilePath>
-            </File>
-            <File>
-              <FileName>fmc_flash.c</FileName>
-              <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\fmc_flash.c</FilePath>
-            </File>
-            <File>
-              <FileName>pwm.c</FileName>
+              <FileName>cortex-m4.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\pwm.c</FilePath>
+              <FilePath>..\Applications\os\port\cortex-m4.c</FilePath>
             </File>
             <File>
-              <FileName>mc_irqs.c</FileName>
+              <FileName>co_task.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\mc_irqs.c</FilePath>
+              <FilePath>..\Applications\os\co_task.c</FilePath>
             </File>
             <File>
-              <FileName>mc_hall_gpio.c</FileName>
+              <FileName>cpu.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\mc_hall_gpio.c</FilePath>
+              <FilePath>..\Applications\os\cpu.c</FilePath>
             </File>
             <File>
-              <FileName>adc.c</FileName>
+              <FileName>heap_4.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\adc.c</FilePath>
+              <FilePath>..\Applications\os\heap_4.c</FilePath>
             </File>
             <File>
-              <FileName>uart.c</FileName>
+              <FileName>timer.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\uart.c</FilePath>
+              <FilePath>..\Applications\os\timer.c</FilePath>
             </File>
             <File>
-              <FileName>timer_count32.c</FileName>
+              <FileName>queue.c</FileName>
               <FileType>1</FileType>
-              <FilePath>..\Applications\bsp\timer_count32.c</FilePath>
+              <FilePath>..\Applications\os\queue.c</FilePath>
             </File>
           </Files>
         </Group>

BIN
Simulink/FOC.slx