Просмотр исходного кода

adc work OK

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 4 лет назад
Родитель
Сommit
f4653f6212

+ 10 - 0
Applications/app/app.c

@@ -5,10 +5,20 @@
 #include "prot/can_message.h"
 #include "libs/task.h"
 #include "foc/foc_api.h"
+
+static void _test_timer_handler(timer_t *t);
+static timer_t test_timer = TIMER_INIT(test_timer, _test_timer_handler);
+
 void app_start(void){
 	set_log_level(MOD_SYSTEM, L_debug);
 	can_message_init();
 	foc_init();
+	timer_post(&test_timer, 2000);
 	task_run();
 }
 
+static void _test_timer_handler(timer_t *t){
+	//foc_start_motor();
+}
+
+

+ 35 - 34
Applications/bsp/adc.c

@@ -39,11 +39,10 @@ static void _adc0_init(void) {
 	u32 adc_dev = ADC0;
     /* enable ADC1 clock */
     rcu_periph_clock_enable(RCU_ADC0);
-
     /* ADC mode config,adc0 master, adc1 slave */
-    adc_mode_config(ADC_DAUL_INSERTED_PARALLEL); 
+    adc_mode_config(ADC_DAUL_INSERTED_PARALLEL);
     /* ADC special function config */
-    adc_special_function_config(adc_dev, ADC_SCAN_MODE, DISABLE);
+    adc_special_function_config(adc_dev, ADC_SCAN_MODE, ENABLE);
     adc_special_function_config(adc_dev, ADC_CONTINUOUS_MODE, DISABLE);  
     /* ADC data alignment config */
     adc_data_alignment_config(adc_dev, ADC_DATAALIGN_RIGHT);
@@ -62,12 +61,11 @@ static void _adc1_init(void) {
 	u32 adc_dev = ADC1;
     /* enable ADC1 clock */
     rcu_periph_clock_enable(RCU_ADC1);
-
     /* ADC mode config,adc0 master, adc1 slave */
     adc_mode_config(ADC_DAUL_INSERTED_PARALLEL); 
     /* ADC special function config */
-    adc_special_function_config(adc_dev, ADC_SCAN_MODE, DISABLE);
-    adc_special_function_config(adc_dev, ADC_CONTINUOUS_MODE, ENABLE);  
+    adc_special_function_config(adc_dev, ADC_SCAN_MODE, ENABLE);
+    adc_special_function_config(adc_dev, ADC_CONTINUOUS_MODE, DISABLE);  
     /* ADC data alignment config */
     adc_data_alignment_config(adc_dev, ADC_DATAALIGN_RIGHT);
 	/* init insert chans*/
@@ -82,60 +80,62 @@ static void _adc1_init(void) {
 
 }
 
-/* ADC0 insert chan sample vbus I */
+/* ADC0 insert chan sample phase I(use two chan, selected by foc) */
 static void _adc0_insert_chan_init(void) {
 	u32 adc_dev = ADC0;
-	adc_discontinuous_mode_config(adc_dev, ADC_INSERTED_CHANNEL, 1); //每次转化一个
+	//adc_discontinuous_mode_config(adc_dev, ADC_INSERTED_CHANNEL, 0);	
     /* ADC channel length config */
-    adc_channel_length_config(adc_dev, ADC_INSERTED_CHANNEL, 2);
-    /* ADC inserted channel config */
-    adc_inserted_channel_config(adc_dev, 0, VBUS_I_CHAN, ADC_SAMPLETIME_7POINT5);
-	adc_inserted_channel_config(adc_dev, 1, VBUS_I_CHAN, ADC_SAMPLETIME_7POINT5);
+    adc_channel_length_config(adc_dev, ADC_INSERTED_CHANNEL, 1);
+    /* ADC inserted channel ran config, use ISQ2,ISQ3 */
+	adc0_update_insert_sample_rank(U_PHASE_I_CHAN);
+	/* config inserted channel sample time */
+	adc0_update_insert_sample_time(U_PHASE_I_CHAN, ADC_SAMPLETIME_7POINT5);
+	adc0_update_insert_sample_time(V_PHASE_I_CHAN, ADC_SAMPLETIME_7POINT5);
+	adc0_update_insert_sample_time(W_PHASE_I_CHAN, ADC_SAMPLETIME_7POINT5);
     /* ADC trigger config */
-//    adc_external_trigger_source_config(adc_dev, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T1_CH0); 
+    adc_external_trigger_source_config(adc_dev, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T0_CH3); 
+	/* ADC external trigger enable */
+    adc_external_trigger_config(adc_dev, ADC_INSERTED_CHANNEL, ENABLE);	
 }
 
-/* ADC1 insert chan sample phase I(use two chan, selected by foc) */
+/* ADC1 insert chan sample vbus I */
 static void _adc1_insert_chan_init(void) {
 	u32 adc_dev = ADC1;
-	adc_discontinuous_mode_config(adc_dev, ADC_INSERTED_CHANNEL, 1); //每次转化一个
+	//adc_discontinuous_mode_config(adc_dev, ADC_INSERTED_CHANNEL, 0);
     /* ADC channel length config */
-    adc_channel_length_config(adc_dev, ADC_INSERTED_CHANNEL, 2);
-    /* ADC inserted channel ran config, use ISQ2,ISQ3 */
-	adc1_update_insert_sample_rank(U_PHASE_I_CHAN, V_PHASE_I_CHAN);
-	/* config inserted channel sample time */
-	adc1_update_insert_sample_time(U_PHASE_I_CHAN, ADC_SAMPLETIME_7POINT5);
-	adc1_update_insert_sample_time(V_PHASE_I_CHAN, ADC_SAMPLETIME_7POINT5);
-	adc1_update_insert_sample_time(W_PHASE_I_CHAN, ADC_SAMPLETIME_7POINT5);
-    /* ADC trigger config */
-//    adc_external_trigger_source_config(adc_dev, ADC_INSERTED_CHANNEL, ADC0_1_EXTTRIG_INSERTED_T1_CH0); 
+    adc_channel_length_config(adc_dev, ADC_INSERTED_CHANNEL, 1);
+    /* ADC inserted channel config */
+    adc_inserted_channel_config(adc_dev, 0, VBUS_I_CHAN, ADC_SAMPLETIME_7POINT5);
+	//adc_inserted_channel_config(adc_dev, 1, VBUS_I_CHAN, ADC_SAMPLETIME_7POINT5);
+    /* ADC trigger config, slave must config to software trigger */
+    adc_external_trigger_source_config(adc_dev, ADC_INSERTED_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE); 
+	/* ADC external trigger enable */
+    adc_external_trigger_config(adc_dev, ADC_INSERTED_CHANNEL, ENABLE);
 }
 
-
 static void _adc0_regular_chan_init(void) {
 	adc_discontinuous_mode_config(ADC0, ADC_REGULAR_CHANNEL, 1); //每次转化一个
 	/* ADC channel length config */
 	adc_channel_length_config(ADC0, ADC_REGULAR_CHANNEL, 1);
-	adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, DISABLE);
+	adc_external_trigger_config(ADC0, ADC_REGULAR_CHANNEL, ENABLE);
+	adc_external_trigger_source_config(ADC0, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE); 
 	adc_regular_channel_config(ADC0, 0, MOTOR_TEMP_CHAN, ADC_SAMPLETIME_55POINT5);
-	adc_regular_channel_config(ADC0, 1, HANDLERBAR_CHAN, ADC_SAMPLETIME_55POINT5);
-	adc_regular_channel_config(ADC0, 2, VBUS_V_CHAN, ADC_SAMPLETIME_55POINT5);
+	//adc_regular_channel_config(ADC0, 1, HANDLERBAR_CHAN, ADC_SAMPLETIME_55POINT5);
+	//adc_regular_channel_config(ADC0, 2, VBUS_V_CHAN, ADC_SAMPLETIME_55POINT5);
 }
 
 static void _adc1_regular_chan_init(void) {
 	adc_discontinuous_mode_config(ADC1, ADC_REGULAR_CHANNEL, 1); //每次转化一个
 	/* ADC channel length config */
 	adc_channel_length_config(ADC1, ADC_REGULAR_CHANNEL, 1);
-	adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, DISABLE);
+	adc_external_trigger_config(ADC1, ADC_REGULAR_CHANNEL, ENABLE);
+	adc_external_trigger_source_config(ADC1, ADC_REGULAR_CHANNEL, ADC0_1_2_EXTTRIG_REGULAR_NONE);
 	adc_regular_channel_config(ADC1, 0, W_PHASE_V_CHAN, ADC_SAMPLETIME_55POINT5);
-	adc_regular_channel_config(ADC1, 1, V_PHASE_V_CHAN, ADC_SAMPLETIME_55POINT5);
-	adc_regular_channel_config(ADC1, 2, U_PHASE_V_CHAN, ADC_SAMPLETIME_55POINT5);
+	//adc_regular_channel_config(ADC1, 1, V_PHASE_V_CHAN, ADC_SAMPLETIME_55POINT5);
+	//adc_regular_channel_config(ADC1, 2, U_PHASE_V_CHAN, ADC_SAMPLETIME_55POINT5);
 }
 
 void adc_start_insert_convert(void) {
-    /* ADC external trigger enable */
-    adc_external_trigger_config(ADC0, ADC_INSERTED_CHANNEL, ENABLE);
-    adc_external_trigger_config(ADC1, ADC_INSERTED_CHANNEL, ENABLE);
     /* clear the ADC flag */
     adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOC);
     adc_interrupt_flag_clear(ADC0, ADC_INT_FLAG_EOIC);
@@ -156,6 +156,7 @@ s32 adc_sample_regular_channel(int channel, int times) {
 	int min = 0xFFFFF;
 	int max = -0xFFFFF;
 	u64 start_time;
+	adc_regular_channel_config(adc_device, 0, channel, ADC_SAMPLETIME_55POINT5);
 	while(count < times){
 restart:		
 		start_time = get_mseconds();

+ 69 - 66
Applications/bsp/adc.h

@@ -21,122 +21,125 @@ inserted ADC 由timer0 ch3触发,
 #define ISO3_OFFSET 15
 #define IL_OFFSET   20
 
-#define ADC_RANK_CHANNEL(c1, c2) ((c1)<<ISQ2_OFFSET | (c2)<<ISO3_OFFSET | 1<<IL_OFFSET)
-
-static u32 adc_rank_channels[6] = {
-	ADC_RANK_CHANNEL(V_PHASE_I_CHAN, U_PHASE_I_CHAN),
-	ADC_RANK_CHANNEL(U_PHASE_I_CHAN, V_PHASE_I_CHAN),
-	ADC_RANK_CHANNEL(W_PHASE_I_CHAN, V_PHASE_I_CHAN),
-	ADC_RANK_CHANNEL(V_PHASE_I_CHAN, W_PHASE_I_CHAN),
-	ADC_RANK_CHANNEL(U_PHASE_I_CHAN, W_PHASE_I_CHAN),
-	ADC_RANK_CHANNEL(W_PHASE_I_CHAN, U_PHASE_I_CHAN),
+//#define ADC_RANK_CHANNEL(c1, c2, l) ((c1)<<ISQ2_OFFSET | (c2)<<ISO3_OFFSET | (l)<<IL_OFFSET)
+#define ADC_RANK_CHANNEL(c)  ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET) 
+#define ADC_CALI_RANK_CHANEL(c)  ((c)<<ISO3_OFFSET | (0)<<IL_OFFSET) 
+static u32 adc0_rank_channels[6] = {
+	ADC_RANK_CHANNEL(V_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(U_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(W_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(V_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(U_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(W_PHASE_I_CHAN),
+};
+static u32 adc1_rank_channels[6] = {
+	ADC_RANK_CHANNEL(U_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(V_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(V_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(W_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(W_PHASE_I_CHAN),
+	ADC_RANK_CHANNEL(U_PHASE_I_CHAN),
 };
 
+static u32 adc0_cali_rank_channels[3] = {
+	ADC_CALI_RANK_CHANEL(U_PHASE_I_CHAN),
+	ADC_CALI_RANK_CHANEL(V_PHASE_I_CHAN),
+	ADC_CALI_RANK_CHANEL(W_PHASE_I_CHAN),
+};
+static u32 adc1_cali_rank_channels[3] = {
+	ADC_CALI_RANK_CHANEL(VBUS_I_CHAN),
+	ADC_CALI_RANK_CHANEL(VBUS_I_CHAN),
+	ADC_CALI_RANK_CHANEL(VBUS_I_CHAN),
+};
 
-#define PHASE_I_ADC ADC1
+#define PHASE_I_ADC ADC0
 static u32 volatile * adc_phase_reg1[6] = {
-	&ADC_IDATA2(PHASE_I_ADC),
-	&ADC_IDATA3(PHASE_I_ADC),
-	&ADC_IDATA2(PHASE_I_ADC),
-	&ADC_IDATA3(PHASE_I_ADC),
-	&ADC_IDATA2(PHASE_I_ADC),
-	&ADC_IDATA3(PHASE_I_ADC),
+	&ADC_IDATA0(ADC0),
+	&ADC_IDATA0(ADC1),
+	&ADC_IDATA0(ADC0),
+	&ADC_IDATA0(ADC1),
+	&ADC_IDATA0(ADC0),
+	&ADC_IDATA0(ADC1),
 };
 static u32 volatile * adc_phase_reg2[6] = {
-	&ADC_IDATA3(PHASE_I_ADC),
-	&ADC_IDATA2(PHASE_I_ADC),
-	&ADC_IDATA3(PHASE_I_ADC),
-	&ADC_IDATA2(PHASE_I_ADC),
-	&ADC_IDATA3(PHASE_I_ADC),
-	&ADC_IDATA2(PHASE_I_ADC),
-};
-#define VBUS_I_ADC ADC0
-static u32 volatile * adc_vbus_reg1[6] = {
-	&ADC_IDATA2(VBUS_I_ADC),
-	&ADC_IDATA3(VBUS_I_ADC),
-	&ADC_IDATA2(VBUS_I_ADC),
-	&ADC_IDATA3(VBUS_I_ADC),
-	&ADC_IDATA2(VBUS_I_ADC),
-	&ADC_IDATA3(VBUS_I_ADC),
-};
-static u32 volatile * adc_vbus_reg2[6] = {
-	&ADC_IDATA3(VBUS_I_ADC),
-	&ADC_IDATA2(VBUS_I_ADC),
-	&ADC_IDATA3(VBUS_I_ADC),
-	&ADC_IDATA2(VBUS_I_ADC),
-	&ADC_IDATA3(VBUS_I_ADC),
-	&ADC_IDATA2(VBUS_I_ADC),
+	&ADC_IDATA0(ADC1),
+	&ADC_IDATA0(ADC0),
+	&ADC_IDATA0(ADC1),
+	&ADC_IDATA0(ADC0),
+	&ADC_IDATA0(ADC1),
+	&ADC_IDATA0(ADC0),
 };
 
-void __inline adc_phase_current_read(u8 sector, u32 *v1, u32 *v2) {
+static void __inline adc_phase_current_read(u8 sector, u32 *v1, u32 *v2) {
 	*v1 = (*adc_phase_reg1[sector]) << 4;
 	*v2 = (*adc_phase_reg2[sector]) << 4;
 }
 
-u32 __inline adc_phase_read_1(u8 sector) {
-	return (*adc_phase_reg1[sector]) << 4;
+static void __inline adc_cali_current_read(u32 *v1, u32 *v2) {
+	*v1 = ADC_IDATA0(ADC0);
+	*v2 = ADC_IDATA0(ADC1);
 }
 
-void __inline adc_vbus_current_read(u8 sector, u32 *v1, u32 *v2) {
-	*v1 = (*adc_vbus_reg1[sector]) << 4;
-	*v2 = (*adc_vbus_reg2[sector]) << 4;
-}
 
-u32 __inline adc_vbus_read_1(u8 sector) {
-	return (*adc_vbus_reg1[sector]) << 4;
+static void __inline adc_phase_inserted_config(u8 sector) {
+	ADC_ISQ(ADC0) = adc0_rank_channels[sector];
+	ADC_ISQ(ADC1) = adc1_rank_channels[sector];
 }
 
 
-void __inline adc_phase_inserted_config(u8 sector) {
-	ADC_ISQ(ADC1) = adc_rank_channels[sector];
+static void __inline adc_cali_inserted_config(u8 invert) {
+	ADC_ISQ(ADC0) = adc0_cali_rank_channels[invert];
+	ADC_ISQ(ADC1) = adc1_cali_rank_channels[invert];
 }
 
 #define ADC_TRIGGER_PHASE ADC0_1_EXTTRIG_INSERTED_T0_CH3
 #define ADC_TRIGGER_VBUS ADC0_1_EXTTRIG_INSERTED_T1_CH0
 
 
-void __inline adc_config_trigger(u32 trigger) {
+static void __inline adc_config_trigger(u32 trigger) {
 	ADC_CTL1(ADC0) &= ~((uint32_t)ADC_CTL1_ETSIC);
 	ADC_CTL1(ADC0) |= (uint32_t)trigger;
+
 	ADC_CTL1(ADC1) &= ~((uint32_t)ADC_CTL1_ETSIC);
 	ADC_CTL1(ADC1) |= (uint32_t)trigger;
 }
 
-bool __inline adc_is_trigged_vbus(void) {
-	if (ADC_CTL1(ADC0) & ADC_TRIGGER_VBUS == ADC_TRIGGER_VBUS) {
+static bool __inline adc_is_trigged_vbus(void) {
+	if (ADC_CTL1(ADC1) & ADC_TRIGGER_VBUS == ADC_TRIGGER_VBUS) {
 		return true;
 	}
 	return false;
 }
 
 /* insert len fixed to 2(IL=1), ISQ2 >> ISQ3*/
-static __inline__ void adc1_update_insert_sample_rank(u8 channel1, u8 channel2) {
-    u32 isq;
-        
-    isq = ADC_ISQ(ADC1);
-    isq &= 0xFFF0000;
-    isq |= (channel1 << 10) | (channel2 << 15);
-    ADC_ISQ(ADC1) = isq;
+static __inline__ void adc0_update_insert_sample_rank(u8 channel) {
+    ADC_ISQ(ADC0) = ADC_RANK_CHANNEL(channel);
+}
+
+static __inline__ void adc1_update_insert_sample_rank(u8 channel) {
+    ADC_ISQ(ADC1) = ADC_RANK_CHANNEL(channel);
 }
 
-static __inline__ void adc1_update_insert_sample_time(uint8_t adc_channel , uint32_t sample_time)
+static __inline__ void adc0_update_insert_sample_time(uint8_t adc_channel , uint32_t sample_time)
 {
     uint32_t sampt;
     /* ADC sampling time config */  
     if(adc_channel < 10U){
-        sampt = ADC_SAMPT1(ADC1);
+        sampt = ADC_SAMPT1(ADC0);
         sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*adc_channel)));
         sampt |= (u32) sample_time << (3U*adc_channel);
-        ADC_SAMPT1(ADC1) = sampt;
+        ADC_SAMPT1(ADC0) = sampt;
     }else if(adc_channel < 18U){
-        sampt = ADC_SAMPT0(ADC1);
+        sampt = ADC_SAMPT0(ADC0);
         sampt &= ~((u32)(ADC_SAMPTX_SPTN << (3U*(adc_channel-10U))));
         sampt |= ((u32)sample_time << (3U*(adc_channel-10U)));
-        ADC_SAMPT0(ADC1) = sampt;
+        ADC_SAMPT0(ADC0) = sampt;
     }
 }
 
-
+static __inline__ void adc_insert_continue_mode(u32 adc_periph) {
+	ADC_CTL0(adc_periph) &= ~((uint32_t)(ADC_CTL0_DISIC ));
+}
 void adc_init(void);
 s32 adc_sample_regular_channel(int chan, int times);
 void adc_start_insert_convert(void);

+ 27 - 0
Applications/bsp/bsp.c

@@ -6,6 +6,7 @@
 
 static void wdog_enable(void);
 static void sys_tick_init(void);
+static void task_timer_init(void);
 
 void bsp_init(void){
 	wdog_enable();
@@ -14,6 +15,7 @@ void bsp_init(void){
 #if LOG_UART==1
 	shark_uart_init(SHARK_UART0);
 #endif
+	task_timer_init();
 }
 
 
@@ -80,3 +82,28 @@ int wdog_set_timeout(int wdog_time)
 	return 0;
 }
 
+//10 ms
+static void task_timer_init(void) {
+    timer_parameter_struct timer_initpara;
+    u32 timer = TIMER5;
+    rcu_periph_clock_enable(RCU_TIMER5);
+
+    timer_deinit(timer);
+	
+	memset(&timer_initpara, 0, sizeof(timer_initpara));
+    timer_initpara.prescaler        = 12000 - 1; //clk 10000
+    timer_initpara.alignedmode      = TIMER_COUNTER_EDGE;
+    timer_initpara.period          = 100;
+    timer_initpara.clockdivision    = TIMER_CKDIV_DIV1;
+    timer_initpara.repetitioncounter = 0;
+    timer_init(timer,&timer_initpara);
+	timer_counter_value_config(timer, 0);
+	timer_autoreload_value_config(timer, 100);
+	timer_counter_up_direction(timer);
+	timer_auto_reload_shadow_enable(timer);
+	timer_interrupt_enable(timer, TIMER_INT_UP);
+	timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
+	nvic_irq_enable(TIMER5_IRQn, 5, 0);
+	timer_enable(timer);
+}
+

+ 26 - 0
Applications/bsp/mc_irqs.c

@@ -1,3 +1,4 @@
+#include <stdbool.h>
 #include "bsp.h"
 /*!
     \brief      this function handles NMI exception
@@ -75,6 +76,10 @@ extern void mc_phase_current_irq(void);
 extern void mc_brake_irq(void);
 extern void system_tick_handler(void);
 extern void hall_sensor_handler(void);
+extern void foc_normal_task(void);
+extern void foc_brake_handler(bool brake);
+extern void foc_pwm_up_handler(void);
+
 void SysTick_Handler(void) {
 	system_tick_handler();
 }
@@ -92,6 +97,27 @@ void ADC0_1_IRQHandler(void)
 	}	
 }
 
+void TIMER0_UP_IRQHandler(void) {
+	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_UP)) {
+		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_UP);
+		foc_pwm_up_handler();
+	}
+}
+
+void TIMER0_BRK_IRQHandler(void) {
+	if (timer_interrupt_flag_get(TIMER0, TIMER_INT_FLAG_BRK)) {
+		timer_interrupt_flag_clear(TIMER0, TIMER_INT_FLAG_BRK);
+		foc_brake_handler(true);
+	}
+}
+
+void TIMER5_IRQHandler(void) {
+	if (timer_interrupt_flag_get(TIMER5, TIMER_INT_FLAG_UP)) {
+		timer_interrupt_flag_clear(TIMER5, TIMER_INT_FLAG_UP);
+		foc_normal_task();
+	}
+}
+
 
 void EXTI2_IRQHandler(void)
 {

+ 33 - 14
Applications/bsp/pwm.c

@@ -80,6 +80,8 @@ static void _init_pwm_timer(void) {
 	timer_deinit(timer);
 
     /* TIMER0 configuration */
+	memset(&timer_initpara, 0, sizeof(timer_initpara));
+	memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
     timer_initpara.prescaler        = 0;
     timer_initpara.alignedmode      = TIMER_COUNTER_CENTER_BOTH;
     timer_initpara.period          = half_period;
@@ -118,7 +120,7 @@ static void _init_pwm_timer(void) {
     timer_channel_output_shadow_config(timer,TIMER_CH_3,TIMER_OC_SHADOW_ENABLE);
 
 #if USER_ITMER_BRAKE==1
-		timer_break_parameter_struct timer_breakpara;
+	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 ;
@@ -128,6 +130,9 @@ static void _init_pwm_timer(void) {
     timer_breakpara.protectmode     = TIMER_CCHP_PROT_0;
     timer_breakpara.breakstate      = TIMER_BREAK_ENABLE;
     timer_break_config(TIMER0,&timer_breakpara);
+	timer_interrupt_enable(timer, TIMER_INT_BRK);
+	timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_BRK);
+	nvic_irq_enable(TIMER0_BRK_IRQn, 1, 0);		
 #else
 	_gpio_brakein_irq_enable();
 #endif
@@ -144,7 +149,9 @@ static void _init_pwm_timer(void) {
     /* auto-reload preload enable */
     timer_auto_reload_shadow_enable(timer);
 
-	pwm_disable_channel();
+	timer_interrupt_enable(timer, TIMER_INT_UP);
+	timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
+	nvic_irq_enable(TIMER0_UP_IRQn, 1, 0);	
 	
     timer_enable(timer);
 }
@@ -158,6 +165,8 @@ static void _init_adc_timer(void) {
     rcu_periph_clock_enable(_rcu_clk(timer));
 
     timer_deinit(timer);
+	memset(&timer_initpara, 0, sizeof(timer_initpara));
+	memset(&timer_ocintpara, 0, sizeof(timer_ocintpara));
 
     timer_initpara.prescaler        = 0;
     timer_initpara.alignedmode      = TIMER_COUNTER_CENTER_UP;
@@ -184,6 +193,7 @@ static void _init_adc_timer(void) {
     /* slave mode selection: enabled when time2 is enable*/
     timer_slave_mode_select(timer,TIMER_SLAVE_MODE_EVENT);
     timer_input_trigger_source_select(timer,TIMER_SMCFG_TRGSEL_ITI2);
+	
     timer_primary_output_config(timer,ENABLE);
 
     /* auto-reload preload enable */
@@ -214,6 +224,7 @@ static void _init_aux_timer(void) {
     rcu_periph_clock_enable(_rcu_clk(timer));
 
     timer_deinit(timer);
+	memset(&timer_initpara, 0, sizeof(timer_initpara));
 
     timer_initpara.prescaler        = 0;
     timer_initpara.alignedmode      = TIMER_COUNTER_CENTER_UP;
@@ -231,27 +242,35 @@ static void _init_aux_timer(void) {
 
 
 void pwm_start(void){
-	ch0_update_duty(FOC_PWM_Half_Period);
-	ch1_update_duty(FOC_PWM_Half_Period);
-	ch2_update_duty(FOC_PWM_Half_Period);
-	pwm_enable_channel();
-	timer_enable(aux_timer);//this will trige time0/time1
-	adc_start_insert_convert();
+	pwm_update_duty(FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period/2, FOC_PWM_Half_Period-5);
+	/* wait for a new PWM period to flush last HF task */
+	timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
+	while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET )
+	{}
+	/* Clear Update Flag */
+	timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
+	timer_primary_output_config(pwm_timer,ENABLE);
+	timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
+	timer_interrupt_enable(pwm_timer, TIMER_INT_UP);
 }
 
 void pwm_stop(void){
-	pwm_disable_channel();
-	timer_disable(aux_timer);
-	timer_disable(pwm_timer);
-	timer_disable(adc_timer);
+	timer_primary_output_config(pwm_timer,DISABLE);
+	timer_interrupt_disable(pwm_timer, TIMER_INT_UP);
+	/* wait for a new PWM period to flush last HF task */
+	timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
+	while ( timer_flag_get(pwm_timer, TIMER_FLAG_UP) == RESET )
+	{}
+	/* Clear Update Flag */
+	timer_flag_clear(pwm_timer, TIMER_FLAG_UP);
 }
 
 /*open low side of the mosfet*/
 void pwm_turn_on_low_side(void)
 {  
 	pwm_update_duty(0, 0, 0, FOC_PWM_Half_Period);
-	timer_flag_clear(pwm_timer,TIMER_FLAG_CH0);
-  	while (timer_flag_get(pwm_timer,TIMER_FLAG_CH0) == RESET );
+	timer_flag_clear(pwm_timer,TIMER_FLAG_UP);
+  	while (timer_flag_get(pwm_timer,TIMER_FLAG_UP) == RESET );
   	/* Main PWM Output Enable */
   	pwm_enable_channel(); 
 }

+ 17 - 26
Applications/foc/foc_api.c

@@ -10,30 +10,28 @@
 #include "foc/foc_stm.h"
 #include "foc/phase_current.h"
 #include "foc/hall_sensor.h"
-
+#include "foc/gas_sensor.h"
 extern motor_foc_t g_foc;
 static void foc_defulat_value(void);
 
 void foc_init(void) {
 	foc_defulat_value();
-	foc_core_init();
 	adc_init();
 	pwm_3phase_init();
-	hall_sensor_init();	
+	hall_sensor_init();
+	foc_core_init();
 }
 
 static void foc_defulat_value(void){
 	g_foc.state = IDLE;
 	g_foc.mosfec_gate = false;
 	g_foc.vbus = MAX_VBUS_VOLTAGE;
-	g_foc.state = IDLE;
 	g_foc.mode = FOC_MODE_OPEN_LOOP;
 	g_foc.alpha_beta.alpha = 0;
 	g_foc.alpha_beta.beta = 0;
 	g_foc.dq_command.Id = 0;
 	g_foc.dq_command.Iq = 0;
 	g_foc.foc_fault = foc_success;
-	g_foc.is_ready = false;
 	g_foc.speed_command = -1;
 	memset(&g_foc.phase_time, 0, sizeof(g_foc.phase_time));
 	g_foc.sector = 0;
@@ -69,20 +67,6 @@ u32 foc_get_speed(void) {
 	return abs(speed);
 }
 
-bool foc_is_ready(void){
-	return g_foc.is_ready;
-}
-
-foc_fault_t foc_set_ready(bool ready) {
-	if (ready == foc_is_ready()) {
-		return foc_success;
-	}
-	if (foc_stm_nextstate(ready?START:ANY_STOP) == NoError) {
-		g_foc.is_ready = ready;
-		return foc_success;
-	}	
-	return foc_not_allowed;
-}
 
 void foc_set_dq_command(float d, float q) {
 	g_foc.dq_command.Vd = d;
@@ -103,12 +87,8 @@ void foc_set_speed_ramp(u16 rpm){
 void foc_set_controller_mode(control_mode_t mode) {
 	g_foc.mode = mode;
 }
-/*
-void foc_set_start_ramp(float v) {
-	ramp_set_target(&g_foc.voltage_ramp, g_foc.dq_v.Vq, v, START_RAMP_DURATION);
-}*/
 
-void foc_set_ref_speed(u16 rpm) {
+void foc_set_speed(u16 rpm) {
 	if (g_foc.state == IDLE || g_foc.state == ANY_STOP) {
 		return;
 	}
@@ -119,13 +99,23 @@ void foc_set_ref_speed(u16 rpm) {
 }
 
 foc_fault_t foc_start_motor(void){
+	if (gas_detect_speed_signal()) {
+		return foc_start_with_gas_error;
+	}
 	return foc_stm_nextstate(START);
 }
 
 foc_fault_t foc_stop_motor(void) {
+	if (foc_get_speed() > 0) {
+		return foc_stop_with_speed_error;
+	}
 	return foc_stm_nextstate(ANY_STOP);
 }
 
+bool foc_motor_is_started(void) {
+	return g_foc.state >= START;
+}
+
 void foc_current_calibrate(void){
 	g_foc.current_samp.adc_offset_a = 0;
 	g_foc.current_samp.adc_offset_b = 0;
@@ -133,7 +123,7 @@ void foc_current_calibrate(void){
 	g_foc.current_samp.adc_offset_ivbus = 0;
 
 	pwm_disable_channel();
-	//foc_pwm_start(false);
+	foc_pwm_start(false);
 
 	task_udelay(10);
 	
@@ -141,6 +131,7 @@ void foc_current_calibrate(void){
 	g_foc.current_samp.is_calibrating_offset = true;
 	g_foc.current_samp.sector = SECTOR_5;
 	foc_pwm_start(true);
+	adc_start_insert_convert();
 	while(g_foc.current_samp.offset_sample_count != 0){};
 	
 	foc_pwm_start(false);
@@ -150,7 +141,7 @@ void foc_current_calibrate(void){
 	foc_pwm_start(true);
 	while(g_foc.current_samp.offset_sample_count != 0){};
 	g_foc.current_samp.is_calibrating_offset = false;
-	foc_pwm_start(false);
+	pwm_enable_channel();
 }
 
 float foc_get_vbus_voltage(void){

+ 2 - 10
Applications/foc/foc_api.h

@@ -12,21 +12,13 @@ foc_fault_t foc_start_motor(void);
 foc_fault_t foc_stop_motor(void);
 int foc_hall_detect(float current, u16 *hall_table);
 void foc_set_dq_command(float d, float q);
-/*
-void foc_overide_theta(bool enable);
-void foc_overide_vdq(bool enable);
-void foc_overide_set_theta(float theta);
-void foc_overide_set_vdq(float d, float q);
-*/
 void foc_set_voltage_ramp(float final);
 void foc_set_speed_ramp(u16 rpm);
-//void foc_set_start_ramp(float v);
 void foc_current_calibrate(void);
 u32 foc_get_speed(void);
 float foc_get_vbus_voltage(void);
-bool foc_is_ready(void);
-foc_fault_t foc_set_ready(bool ready);
-void foc_set_ref_speed(u16 rpm);
+bool foc_motor_is_started(void);
+void foc_set_speed(u16 rpm);
 float speed_to_voltage(u16 rpm);
 float speed_to_current(u16 rpm);
 #endif /* _FOC_API_H__ */

+ 2 - 4
Applications/foc/foc_core.c

@@ -42,14 +42,13 @@ motor_foc_t g_foc = {
 	},	
 };
 
-static u32 foc_normal_task(void);
+
 static u32 foc_measure_task(void);
 
 void foc_core_init(void) {
 	vbus_sensor_init();
 	ntc_sensor_init();
 	task_start(foc_measure_task, 0);
-	task_start(foc_normal_task, 0);
 }
 
 #if 1
@@ -246,9 +245,8 @@ void foc_pwm_start(bool start) {
 	g_foc.mosfec_gate = start;	
 }
 
-static u32 foc_normal_task(void) {
+void foc_normal_task(void) {
 	FOC_Normal_Task(&g_foc);
-	return 10; //10ms run
 }
 
 static u32 foc_measure_task(void){

+ 5 - 3
Applications/foc/foc_type.h

@@ -48,13 +48,13 @@ typedef struct _phase_time {
 
 typedef enum {
 	IDLE = 0,
+	ANY_STOP,
 	START,
 	CURRENT_CALIBRATE,
 	CHARGER_BOOT_CAP,
 	READY_TO_RUN,
 	RAMPING_START,
-	RUNNING,
-	ANY_STOP
+	RUNNING
 }foc_state_t;
 
 typedef enum {
@@ -99,12 +99,14 @@ typedef struct _override {
 typedef enum {
 	foc_success = 0,
 	foc_not_allowed = 1,
+	foc_start_with_gas_error, //ready的时候检测到转把信号
+	foc_stop_with_speed_error,
 	foc_brake_error = 100,
 }foc_fault_t;
 
 
 typedef struct foc_s {
-	bool is_ready;
+	bool is_motor_start;
 	alpha_beta_t alpha_beta;
 	current_samp_t current_samp; /*三相电流采集 */
 	dq_t dq_command; 

+ 5 - 0
Applications/foc/gas_sensor.c

@@ -20,3 +20,8 @@ float gas_get_value(void){
 	return _gas.voltage/ADC_REFERENCE_VOLTAGE;
 }
 
+/* 检测到有效转把信号 */
+bool gas_detect_speed_signal(void) {
+	return gas_get_value() >= 1.0f;
+}
+

+ 2 - 1
Applications/foc/gas_sensor.h

@@ -1,6 +1,6 @@
 #ifndef _GAS_SENSOR_H__
 #define _GAS_SENSOR_H__
-
+#include "libs/os.h"
 
 typedef struct {
 	float voltage;
@@ -10,6 +10,7 @@ typedef struct {
 void gas_sensor_init(void);
 void gas_sample_voltage(void);
 float gas_get_value(void); //归一化
+bool gas_detect_speed_signal(void);
 
 #endif /* _GAS_SENSOR_H__ */
 

+ 14 - 7
Applications/foc/phase_current.c

@@ -1,6 +1,8 @@
 #include "bsp/adc.h"
 #include "foc_type.h"
 
+#define MOS_Rds_Calibrate 1
+
 #define NB_OFFSET_SAMPLES 32
 
 #define Rvbus  0.0005f
@@ -44,15 +46,12 @@ void phase_current_offset(current_samp_t *cs) {
 		}
 		if (cs->sector == SECTOR_1 && cs->offset_sample_count >= 0) {
 			cs->adc_offset_b += phase_current1;
-			adc_vbus_current_read(cs->sector, &phase_current1, &phase_current2);
 			cs->adc_offset_ivbus += phase_current1;
 			if (cs->offset_sample_count == 0) {
 				cs->adc_offset_b = cs->adc_offset_b / NB_OFFSET_SAMPLES;
-				cs->adc_offset_ivbus = cs->adc_offset_ivbus / NB_OFFSET_SAMPLES;
 			}
 		}
 	}
-
 }
 
 void phase_current_sample(current_samp_t *cs){
@@ -90,8 +89,9 @@ void phase_current_sample(current_samp_t *cs){
 /* 校准mos的 drain-source 内阻 */
 void phase_Rds_calibrate(current_samp_t *cs) {	
 	if (cs->vbus_i_invert != INVERT_NO) {
-		u32 adcm = adc_phase_read_1(cs->sector);
-		u32 adcv = adc_vbus_read_1(cs->sector);
+		u32 adcm;
+		u32 adcv;
+		adc_cali_current_read(&adcm, &adcv);
 		float vbus_i = VBUS_VOL(adcv)/Rvbus;
 		if (cs->vbus_i_invert == INVERT_A) {
 			_calc_mos_rds(adcm, vbus_i, &cs->Rds_a);
@@ -102,8 +102,8 @@ void phase_Rds_calibrate(current_samp_t *cs) {
 		}
 		cs->vbus_i_invert = INVERT_NO;		
 	}
-
 	adc_config_trigger(ADC_TRIGGER_PHASE);
+	adc_phase_inserted_config(cs->sector);
 }
 
 u32 get_phase_sample_point(current_samp_t *cs, phase_time_t *time, u8 sector){
@@ -133,6 +133,13 @@ u32 get_vbus_sample_point(current_samp_t *cs, phase_time_t *time){
 }
 
 void phase_current_adc_triger(current_samp_t *cs){
-	adc_phase_inserted_config(cs->sector);
+	if (cs->vbus_i_invert != INVERT_NO) {
+		adc_config_trigger(ADC_TRIGGER_VBUS);
+		adc_cali_inserted_config(cs->vbus_i_invert-1);
+	}else {
+		adc_config_trigger(ADC_TRIGGER_PHASE);
+		adc_phase_inserted_config(cs->sector);
+		cs->sector = (cs->sector + 1)%6;
+	}
 }
 

+ 6 - 3
Applications/os/task.h

@@ -4,12 +4,15 @@
 Task如果没有调用task_yield/delay 永远执行下去
 */
 typedef void (*task_func)(void *);
-
+typedef enum {
+	Task_Pending,
+	Task_Blocking,
+	Task_Running
+}Task_Stat_t;
 typedef struct {
-	u32 regs[16];//save the context
 	u32 pstack; //stack addr
 	u16 stack_size; //stack size
-	u8  run_stat; //task stat, running, pending, blocked
+	Task_Stat_t  stat; //task stat, running, pending, blocked
 }tcb_t;
 
 void task_create(void *stack, u16 stack_size, task_func func, void *param);

+ 4 - 0
Applications/prot/can_message.c

@@ -128,7 +128,11 @@ static void handle_can_frame(void){
 
 
 static void can_process_message(can_message_t *message){
+	if (message->key & 0xFF >= 0xF0) {
+		//do iap update
+	}else if (message->src == 0x42){ //只处理后控的指令
 
+	}
 	free_can_message(message);
 }
 static void free_can_message(can_message_t *message){

+ 24 - 2
Project/MC100.uvoptx

@@ -155,7 +155,7 @@
       <DebugFlag>
         <trace>0</trace>
         <periodic>0</periodic>
-        <aLwin>1</aLwin>
+        <aLwin>0</aLwin>
         <aCover>0</aCover>
         <aSer1>0</aSer1>
         <aSer2>0</aSer2>
@@ -191,6 +191,28 @@
       <pszMrulep></pszMrulep>
       <pSingCmdsp></pSingCmdsp>
       <pMultCmdsp></pMultCmdsp>
+      <SystemViewers>
+        <Entry>
+          <Name>System Viewer\ADC0</Name>
+          <WinId>35902</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\ADC1</Name>
+          <WinId>35901</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\DBG</Name>
+          <WinId>35904</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\TIMER0</Name>
+          <WinId>35903</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\TIMER5</Name>
+          <WinId>35905</WinId>
+        </Entry>
+      </SystemViewers>
     </TargetOption>
   </Target>
 
@@ -228,7 +250,7 @@
 
   <Group>
     <GroupName>Foc</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>