فهرست منبع

blueshark motor can run

Signed-off-by: huhui <huhui@sharkgulf.com>
huhui 4 سال پیش
والد
کامیت
bb9a27e285
40فایلهای تغییر یافته به همراه591 افزوده شده و 358 حذف شده
  1. 14 2
      Applications/app/app.c
  2. 46 0
      Applications/app/nv_storage.c
  3. 25 0
      Applications/app/nv_storage.h
  4. 1 1
      Applications/bsp/adc.c
  5. 7 0
      Applications/bsp/bsp.c
  6. 5 2
      Applications/bsp/bsp.h
  7. 45 12
      Applications/bsp/can.c
  8. 9 8
      Applications/bsp/fmc_flash.c
  9. 2 2
      Applications/bsp/fmc_flash.h
  10. 4 0
      Applications/bsp/mc_hall_gpio.c
  11. 6 0
      Applications/bsp/mc_hall_gpio.h
  12. 9 8
      Applications/bsp/pwm.c
  13. 1 1
      Applications/bsp/pwm.h
  14. 113 0
      Applications/bsp/timer_count32.c
  15. 22 0
      Applications/bsp/timer_count32.h
  16. 1 1
      Applications/config.h
  17. 9 5
      Applications/foc/foc_api.c
  18. 1 2
      Applications/foc/foc_api.h
  19. 20 5
      Applications/foc/foc_core.c
  20. 4 1
      Applications/foc/foc_stm.c
  21. 5 2
      Applications/foc/foc_type.h
  22. 6 9
      Applications/foc/gas_sensor.c
  23. 4 2
      Applications/foc/gas_sensor.h
  24. 23 124
      Applications/foc/hall_sensor.c
  25. 3 3
      Applications/foc/ntc_sensor.c
  26. 1 1
      Applications/foc/vbus_sensor.c
  27. 1 1
      Applications/libs/logger.h
  28. 9 5
      Applications/os/co_task.c
  29. 4 0
      Applications/os/co_task.h
  30. 7 0
      Applications/os/cpu.c
  31. 2 2
      Applications/os/port/cortex-m4.c
  32. 2 2
      Applications/os/queue.c
  33. 4 2
      Applications/os/queue.h
  34. 17 60
      Applications/prot/can_message.c
  35. 1 12
      Applications/prot/can_message.h
  36. 3 3
      Librarys/CMSIS/GD/GD32F30x/Source/system_gd32f30x.c
  37. 145 80
      Project/MC100_OS.uvoptx
  38. 10 0
      Project/MC100_OS.uvprojx
  39. BIN
      Simulink/DQSVPWM.slx
  40. BIN
      Simulink/SVPWM_MOS.slx

+ 14 - 2
Applications/app/app.c

@@ -6,19 +6,31 @@
 #include "prot/can_message.h"
 #include "foc/foc_api.h"
 
+
 static void _test_timer_handler(timer_t *t);
+static void _log_test(void *args);
+
 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);
+	co_task_create(_log_test, NULL, 512);
+	timer_post(&test_timer, 200);
 	co_task_schedule();
 }
 
 static void _test_timer_handler(timer_t *t){
-	foc_start_motor();
+	//foc_start_motor();
 }
 
 
+static void _log_test(void *args) {
+	int count = 0;
+	while(1) {
+		sys_debug("log test count %d\n", count++);
+		co_task_delay(500);
+	}
+}
+

+ 46 - 0
Applications/app/nv_storage.c

@@ -0,0 +1,46 @@
+#include "app/nv_storage.h"
+#include "bsp/fmc_flash.h"
+#include "libs/crc16.h"
+
+static void mc_config_default(void);
+
+static mc_config_t g_config;
+
+void config_set_hall_offset(s16 offset) {
+	g_config.hall_offset = offset;
+}
+
+mc_config_t *mc_config_get(void) {
+	return &g_config;
+}
+
+void store_config(void) {
+	u16 crc = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
+	g_config.crc16 = crc;
+	fmc_write_data(0, (u8 *)&g_config, sizeof(g_config));
+	fmc_write_data(1, (u8 *)&g_config, sizeof(g_config));
+}
+
+void restore_config(void) {
+	fmc_read_data(0, (u8 *)&g_config, sizeof(g_config));
+	u16 crc0 = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
+	if (crc0 != g_config.crc16) {
+		fmc_read_data(1, (u8 *)&g_config, sizeof(g_config));
+		crc0 = crc16_get((u8 *)&g_config, sizeof(g_config) - 2);
+		if (crc0 != g_config.crc16) {
+			mc_config_default();
+			return;
+		}
+		fmc_write_data(0, (u8 *)&g_config, sizeof(g_config));
+	}else {
+		fmc_write_data(1, (u8 *)&g_config, sizeof(g_config));
+	}
+}
+
+static void mc_config_default(void) {
+	g_config.hall_offset = 0;
+	for (int i = 0; i < 3; i++) {
+		g_config.phase_op[i].op_i_k = 1;
+		g_config.phase_op[i].op_i_offset = 0;;
+	}
+}

+ 25 - 0
Applications/app/nv_storage.h

@@ -0,0 +1,25 @@
+#ifndef _NV_Storage_H__
+#define _NV_Storage_H__
+#include "os/os_type.h"
+
+#pragma  pack (push,1)
+
+typedef struct {
+	int op_i_k; /* 斜率 */
+	int op_i_offset; /* 截距 */
+}op_cali_t;
+
+typedef struct {
+	s16 hall_offset; /* hall 和A相之间的电角度偏移量 */
+	op_cali_t phase_op[3]; /* 三相电流采集的校准系数 */
+	u16 crc16;
+}mc_config_t;
+#pragma pack(pop)
+
+mc_config_t *mc_config_get(void);
+void config_set_hall_offset(s16 offset);
+void store_config(void);
+void restore_config(void);
+
+#endif /* _NV_Storage_H__ */
+

+ 1 - 1
Applications/bsp/adc.c

@@ -26,7 +26,7 @@ void adc_init(void){
 
 	adc_config_trigger(ADC_TRIGGER_PHASE);
 
-	nvic_irq_enable(ADC0_1_IRQn, 0, 0);
+	nvic_irq_enable(ADC0_1_IRQn, ADC_IRQ_PRIORITY, 0);
 }
 
 static void _gpio_init(void) {

+ 7 - 0
Applications/bsp/bsp.c

@@ -1,7 +1,9 @@
 #include "bsp/bsp.h"
+#include "bsp/gd32_bkp.h"
 #include "libs/logger.h"
 #include "os/os_type.h"
 #include "bsp/uart.h"
+#include "bsp/timer_count32.h"
 #include "version.h"
 
 static void wdog_enable(void);
@@ -9,7 +11,12 @@ static void normal_task_timer_init(void);
 
 void bsp_init(void){
 	wdog_enable();
+	gd32_bkp_init();
+	dbg_periph_enable(DBG_TIMER0_HOLD);
+	dbg_periph_enable(DBG_TIMER1_HOLD);
+	dbg_periph_enable(DBG_TIMER2_HOLD);
 	cpu_counts_enable();
+	timer_count32_init();
 #if LOG_UART==1
 	shark_uart_init(SHARK_UART0);
 #endif

+ 5 - 2
Applications/bsp/bsp.h

@@ -14,7 +14,7 @@
 #include "bsp/can.h"
 
 
-#define ADC_REFERENCE_VOLTAGE  3.30f
+#define ADC_REFERENCE_VOLTAGE  3300.0f
 
 
 #define SYSTEM_CLOCK (120000000L) //system clk 120M Hz
@@ -24,7 +24,7 @@
 #define ADC_CLOCK_MHz (30)
 #define NS_PER_TCLK (8) /* (1/120000000 * 1000000000) */
 #define NS_2_TCLK(ns) ((ns/NS_PER_TCLK) + 1) //ns תΪpwmʹ�õ��Ǹ�TIM��clk count
-#define FOC_PWM_FS (30 * 1000)
+#define FOC_PWM_FS (20 * 1000)
 #define FOC_PWM_period (TIM_CLOCK/FOC_PWM_FS)
 #define FOC_PWM_Half_Period (FOC_PWM_period/2)
 #define MAX_VBUS (12.f) //12v
@@ -54,6 +54,9 @@
 
 #define USER_ITMER_BRAKE 0
 
+#define TIMER_UP_IRQ_PRIORITY 0
+#define ADC_IRQ_PRIORITY 1
+
 void bsp_init(void);
 void wdog_reload(void);
 void system_reboot(void);

+ 45 - 12
Applications/bsp/can.c

@@ -2,6 +2,7 @@
 #include "bsp.h"
 #include "can.h"
 #include "libs/utils.h"
+#include "os/queue.h"
 
 #define CAN_RX_MESSAGE_RX_ID 1
 #define CAN_SEND_QUEUE_SIZE 32
@@ -10,22 +11,38 @@
 #define CAN_SEND_ERROR -1
 #define CAN_SEND_WAIT_TIMEOUT -2
 
-static can_receive_message_struct can_message;
+
+static co_queue_t tx_queue;
+static co_queue_t rx_queue;
 static int shark_send_can0_to_fifo(can_trasnmit_message_struct *P_message);
 static int shark_send_can0_data(can_trasnmit_message_struct *P_message);
-
+static void _can_tx_task(void *args);
+static void _can_rx_task(void *args);
 /* this function can be overide by app, which need recv the can frame */
-__weak void put_raw_message(can_id_t id, uint8_t *data, int len){
+__weak void handle_can_frame(can_id_t id, uint8_t *data, int len){
 
 }
 
+static void _can_rx_task(void *args){
+	can_receive_message_struct message;
+	while(1) {
+		if (queue_get(rx_queue, &message)) {
+			can_id_t can_id;
+			can_id.id = message.rx_efid;
+			handle_can_frame(can_id, message.rx_data, message.rx_dlen);		
+		}
+		co_task_yield();
+	}
+}
+
+
 
 static __inline__ void can_fifo_recv(int fifo){
-	can_receive_message_struct *message = &can_message;
+	can_receive_message_struct message;
 
-	can_message_receive(CAN0, fifo, message);  
+	can_message_receive(CAN0, fifo, &message);  
 
-	put_raw_message((can_id_t)message->rx_efid, message->rx_data, message->rx_dlen);
+	queue_put(rx_queue, &message);
 }
 
 void USBD_LP_CAN0_RX0_IRQHandler(void)
@@ -41,14 +58,14 @@ void CAN0_RX1_IRQHandler(void)
 static void shark_can0_txrx_pin_config(void){
     /* enable can clock */
     rcu_periph_clock_enable(RCU_CAN0);
-    rcu_periph_clock_enable(RCU_GPIOB);	
+    rcu_periph_clock_enable(RCU_GPIOA);	
     rcu_periph_clock_enable(RCU_AF);
 
-	gpio_pin_remap_config(GPIO_CAN_FULL_REMAP,DISABLE);
-	gpio_pin_remap_config(GPIO_CAN_PARTIAL_REMAP,ENABLE);
+	//gpio_pin_remap_config(GPIO_CAN_FULL_REMAP,DISABLE);
+	//gpio_pin_remap_config(GPIO_CAN_PARTIAL_REMAP,ENABLE);
 	
-    gpio_init(GPIOB, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_9);
-	gpio_init(GPIOB, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_8); 
+    gpio_init(GPIOA, GPIO_MODE_AF_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_12);
+	gpio_init(GPIOA, GPIO_MODE_IPU, GPIO_OSPEED_50MHZ, GPIO_PIN_11); 
 }
 
 
@@ -107,9 +124,21 @@ static void shark_can0_config(void)
 
 
 static int shark_send_can0_data(can_trasnmit_message_struct *P_message){
-	return shark_send_can0_to_fifo(P_message);
+	if (queue_put(tx_queue, P_message)){
+		return CAN_SEND_OK;
+	}
+	return CAN_SEND_ERROR;
 }
 
+static void _can_tx_task(void *args){
+	can_trasnmit_message_struct trasnmit_msg;
+	while(1) {
+		if (queue_get(tx_queue, &trasnmit_msg)) {
+			shark_send_can0_to_fifo(&trasnmit_msg);
+		}
+		co_task_yield();
+	}
+}
 
 /*when system scheduler is not start or stoped, we MUST not use FreeRTOS 
 system api which can cause sleep,context switch */
@@ -172,6 +201,10 @@ void shark_can0_deinit(void){
 }
 
 void shark_can0_init(void){
+	tx_queue = queue_create(32, sizeof(can_trasnmit_message_struct));
+	rx_queue = queue_create(32, sizeof(can_trasnmit_message_struct));
+	co_task_create(_can_tx_task, NULL, 256);
+	co_task_create(_can_rx_task, NULL, 512);
 	shark_can0_txrx_pin_config();
 	shark_can0_config();
 }

+ 9 - 8
Applications/bsp/fmc_flash.c

@@ -8,13 +8,14 @@
 #endif
 
 #define one_page_size 2048
-#define sn_page_index 3
+#define sn_page_index 4
+#define data_bk_page_index 3
 #define data_page_index 2
 #define magic_page_index 1 //must is the last page in 256K eara
 static void _fmc_write_data(uint32_t addr, uint8_t *data, int len);
 static void _fmc_read_data(uint32_t addr, uint8_t *data, int len);
 static uint32_t _sn_addr(void);
-static uint32_t _data_addr(void);
+static uint32_t _data_addr(int index);
 static uint32_t _maigc_addr(void);
 
 void fmc_write_sn(uint8_t *sn, int len){
@@ -25,12 +26,12 @@ void fmc_read_sn(uint8_t *sn, int len){
 	_fmc_read_data(_sn_addr(), sn, len);
 }
 
-void fmc_write_data(uint8_t *data, int len){
-	_fmc_write_data(_data_addr(), data, len);
+void fmc_write_data(int index, uint8_t *data, int len){
+	_fmc_write_data(_data_addr(index), data, len);
 }
 
-void fmc_read_data(uint8_t *data, int len){
-	_fmc_read_data(_data_addr(), data, len);
+void fmc_read_data(int index, uint8_t *data, int len){
+	_fmc_read_data(_data_addr(index), data, len);
 }
 
 static __inline__ void _fmc_flag_clear(void) {
@@ -90,8 +91,8 @@ static uint32_t _sn_addr(void){
 	return 0x08000000 + (_flash_capatity() - one_page_size * sn_page_index);
 }
 
-static uint32_t _data_addr(void){
-	return 0x08000000 + (_flash_capatity() - one_page_size * data_page_index);
+static uint32_t _data_addr(int index){
+	return 0x08000000 + (_flash_capatity() - one_page_size * (data_page_index + index));
 }
 
 static uint32_t _maigc_addr(void){

+ 2 - 2
Applications/bsp/fmc_flash.h

@@ -5,8 +5,8 @@
 void fmc_write_sn(uint8_t *sn, int len);
 void fmc_read_sn(uint8_t *sn, int len);
 
-void fmc_write_data(uint8_t *data, int len);
-void fmc_read_data(uint8_t *data, int len);
+void fmc_write_data(int index, uint8_t *data, int len);
+void fmc_read_data(int index, uint8_t *data, int len);
 void fmc_write_magic(uint32_t magic);
 uint32_t fmc_read_magic(void);
 

+ 4 - 0
Applications/bsp/mc_hall_gpio.c

@@ -21,7 +21,11 @@ int get_hall_stat(int samples) {
 		h2 += READ_HALL2();
 		h3 += READ_HALL3();
 	}
+#if HALL_PLACE==DEGREES_60	
+	return (((h2>tres)^1) << 2) | ((h3 > tres) << 1) | (h2 > tres);
+#else
 	return (h1 > tres) | ((h2 > tres) << 1) | ((h3 > tres) << 2);
+#endif
 }
 
 static void _gpio_irq_enable(void){

+ 6 - 0
Applications/bsp/mc_hall_gpio.h

@@ -10,6 +10,12 @@
 #define HALL_3_PIN GPIO_PIN_8
 #define HALL_3_GROUP GPIOB
 
+#define DEGREES_120 0u
+#define DEGREES_60 1u
+
+#define HALL_PLACE DEGREES_120
+
+
 #define READ_HALL1() (gpio_input_bit_get(HALL_1_GROUP, HALL_1_PIN) == SET ?1:0)
 #define READ_HALL2() (gpio_input_bit_get(HALL_2_GROUP, HALL_2_PIN) == SET ?1:0)
 #define READ_HALL3() (gpio_input_bit_get(HALL_3_GROUP, HALL_3_PIN) == SET ?1:0)

+ 9 - 8
Applications/bsp/pwm.c

@@ -49,7 +49,8 @@ static void _pwm_gpio_config(void)
     rcu_periph_clock_enable(RCU_GPIOA);
     rcu_periph_clock_enable(RCU_GPIOB);
     rcu_periph_clock_enable(RCU_AF);
-
+	//gpio_pin_remap_config(GPIO_TIMER0_PARTIAL_REMAP, ENABLE);
+	//gpio_pin_remap_config(GPIO_TIMER0_FULL_REMAP, DISABLE);
     /*configure PA8 PA9 PA10(TIMER0 CH0 CH1 CH2) as alternate function*/
     gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_8);
     gpio_init(GPIOA,GPIO_MODE_AF_PP,GPIO_OSPEED_50MHZ,GPIO_PIN_9);
@@ -92,8 +93,8 @@ static void _init_pwm_timer(void) {
     /* CH1,CH2 and CH3 configuration in PWM mode */
     timer_ocintpara.outputstate  = TIMER_CCX_ENABLE;
     timer_ocintpara.outputnstate = TIMER_CCXN_ENABLE;
-    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_HIGH;
-    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_HIGH;
+    timer_ocintpara.ocpolarity   = TIMER_OC_POLARITY_LOW;
+    timer_ocintpara.ocnpolarity  = TIMER_OCN_POLARITY_LOW;
     timer_ocintpara.ocidlestate  = TIMER_OC_IDLE_STATE_LOW;
     timer_ocintpara.ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
 
@@ -113,10 +114,10 @@ static void _init_pwm_timer(void) {
     timer_channel_output_shadow_config(timer,TIMER_CH_2,TIMER_OC_SHADOW_ENABLE);
     
 
-    /* chan3 trigger adc O3CPRE is alwary active high, adc trigger is falling */
+    /* chan3 trigger adc O3CPRE is alwary active high, adc trigger is rising */
     timer_channel_output_config(timer,TIMER_CH_3,&timer_ocintpara);
     timer_channel_output_pulse_value_config(timer,TIMER_CH_3,half_period);
-    timer_channel_output_mode_config(timer,TIMER_CH_3,TIMER_OC_MODE_PWM0);
+    timer_channel_output_mode_config(timer,TIMER_CH_3,TIMER_OC_MODE_PWM1);
     timer_channel_output_shadow_config(timer,TIMER_CH_3,TIMER_OC_SHADOW_ENABLE);
 
 #if USER_ITMER_BRAKE==1
@@ -145,13 +146,13 @@ static void _init_pwm_timer(void) {
 	}else {
 		timer_master_slave_mode_config(timer,TIMER_MASTER_SLAVE_MODE_DISABLE);
 	}
-    //timer_primary_output_config(timer,ENABLE);
+    timer_primary_output_config(timer,ENABLE);
     /* auto-reload preload enable */
     timer_auto_reload_shadow_enable(timer);
 
 	timer_interrupt_disable(timer, TIMER_INT_UP);
 	timer_interrupt_flag_clear(timer, TIMER_INT_FLAG_UP);
-	nvic_irq_enable(TIMER0_UP_IRQn, 1, 0);	
+	nvic_irq_enable(TIMER0_UP_IRQn, TIMER_UP_IRQ_PRIORITY, 0);	
 	
     timer_enable(timer);
 }
@@ -272,7 +273,7 @@ void pwm_turn_on_low_side(void)
 	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(); 
+  	pwm_enable_channel();
 }
 
 

+ 1 - 1
Applications/bsp/pwm.h

@@ -21,7 +21,7 @@
 	do {\
 		ch0_update_duty(dutyA);\
 		ch1_update_duty(dutyB);\
-		ch1_update_duty(dutyC);\
+		ch2_update_duty(dutyC);\
 		update_adc_trigger(time);\
 	}while(0)
 #define update_sample_time(samp1, samp2) do{adc_timer_dma_buf[2] = samp1; adc_timer_dma_buf[0] = samp2;}while(0)

+ 113 - 0
Applications/bsp/timer_count32.c

@@ -0,0 +1,113 @@
+#include "bsp/bsp.h"
+#include "timer_count32.h"
+
+static void init_master_timer(void);
+static void init_slave_timer(void);
+
+#define MASTER TIMER1
+#define MASTER_CK RCU_TIMER1
+
+#define SLAVE  TIMER2
+#define SLAVE_CK RCU_TIMER2
+
+#define COUNT_CLK (10 * 1000000)
+#define COUNT_2_US(c) (c/10)
+
+void timer_count32_init(void) {
+
+	init_master_timer();
+	init_slave_timer();
+
+	timer_enable(MASTER);
+	timer_enable(SLAVE);
+}
+
+static void init_master_timer(void){
+	timer_parameter_struct timer_initpara;
+	
+	rcu_periph_clock_enable(MASTER_CK);
+
+	timer_deinit(MASTER);
+
+	memset(&timer_initpara, 0, sizeof(timer_initpara));
+	timer_initpara.prescaler		= TIM_CLOCK/COUNT_CLK - 1; //clk 10MHz, 100ns/0.1us
+	timer_initpara.alignedmode		= TIMER_COUNTER_EDGE;
+	timer_initpara.counterdirection = TIMER_COUNTER_UP;
+	timer_initpara.period		   = 0xFFFF;
+	timer_initpara.clockdivision	= TIMER_CKDIV_DIV1;
+	timer_initpara.repetitioncounter = 0;
+	timer_init(MASTER,&timer_initpara);
+	timer_counter_value_config(MASTER, 0);
+	timer_autoreload_value_config(MASTER, 0xFFFF);
+	timer_counter_up_direction(MASTER);
+	timer_auto_reload_shadow_enable(MASTER);
+	timer_master_slave_mode_config(MASTER, TIMER_MASTER_SLAVE_MODE_ENABLE);
+	timer_master_output_trigger_source_select(MASTER, TIMER_TRI_OUT_SRC_UPDATE);
+}
+
+static void init_slave_timer(void) {
+	timer_parameter_struct timer_initpara;
+	
+	rcu_periph_clock_enable(SLAVE_CK);
+
+	timer_deinit(SLAVE);
+
+	memset(&timer_initpara, 0, sizeof(timer_initpara));
+	timer_initpara.prescaler		= 0; //clk 10MHz/65535
+	timer_initpara.alignedmode		= TIMER_COUNTER_EDGE;
+	timer_initpara.counterdirection = TIMER_COUNTER_UP;
+	timer_initpara.period		   = 0xFFFF;
+	timer_initpara.clockdivision	= TIMER_CKDIV_DIV1;
+	timer_initpara.repetitioncounter = 0;
+	timer_init(SLAVE,&timer_initpara);
+	timer_counter_value_config(SLAVE, 0);
+	timer_autoreload_value_config(SLAVE, 0xFFFF);
+	timer_counter_up_direction(SLAVE);
+	timer_auto_reload_shadow_enable(SLAVE);
+	timer_master_slave_mode_config(SLAVE, TIMER_MASTER_SLAVE_MODE_ENABLE);
+	timer_slave_mode_select(SLAVE, TIMER_SLAVE_MODE_EXTERNAL0);
+	timer_input_trigger_source_select(SLAVE, TIMER_SMCFG_TRGSEL_ITI1); //select timer1 output as the trigger input
+
+}
+
+static __inline__ u32 _timer_count32_get(void) {
+	u16 high;
+	u16 low;
+	do {
+		high = TIMER_CNT(SLAVE);
+		low = TIMER_CNT(MASTER);
+	}while((high != TIMER_CNT(SLAVE)) /*|| (low != TIMER_CNT(MASTER))*/);
+
+	return (high<<16) | low;
+}
+
+
+u32 timer_count32_get(void) {
+	return _timer_count32_get();
+}
+
+
+u32 timer_count32_delta_us(u32 count, u32 *p_update) {
+	u32 now = timer_count32_get();
+	u32 delta = now - count;
+	if (now < count) { //wrap
+		delta = 0xFFFFFFFF - count + now + 1;
+	}
+	if (p_update) {
+		*p_update = now;
+	}
+	return COUNT_2_US(delta);
+}
+
+void time_measure_start(measure_time_t *m){
+	m->intval_time = timer_count32_delta_us(m->intval_count, &m->intval_count);
+	m->exec_count = timer_count32_get();
+}
+void time_measure_end(measure_time_t *m) {
+	m->exec_time = timer_count32_delta_us(m->exec_count, NULL);
+	if (m->exec_time > m->exec_max_time) {
+		m->exec_time_error ++;
+		m->exec_max_error_time = m->exec_time;
+	}
+}
+

+ 22 - 0
Applications/bsp/timer_count32.h

@@ -0,0 +1,22 @@
+#ifndef _TIMER_COUNT32_H__
+#define _TIMER_COUNT32_H__
+#include "os/os_type.h"
+
+typedef struct {
+	u32 exec_count;
+	u32 exec_time;
+	u32 intval_count;
+	u32 intval_time;
+	u32 exec_max_time;
+	u32 exec_max_error_time;
+	u32 exec_time_error;
+}measure_time_t;
+
+void timer_count32_init(void);
+u32  timer_count32_get(void);
+u32 timer_count32_delta_us(u32 prev_count, u32 *p_update);
+void time_measure_start(measure_time_t *m);
+void time_measure_end(measure_time_t *m);
+
+#endif	/* _TIMER_COUNT32_H__ */
+

+ 1 - 1
Applications/config.h

@@ -28,7 +28,7 @@
 #define  CAN_NODE_ADDR_FF	 		0x7F	/*broadcast*/
 #define  CAN_NODE_BATT_MCAST        0x73    /* 电池信息的组播 */
 #define  CAN_NODE_VINFO_MCAST       0x74    /* 车辆信息的组播,电门,设法,报警等 */
-#define  CAN_MY_ADDRESS  			CAN_NODE_ADDR_DCU
+#define  CAN_MY_ADDRESS  			CAN_NODE_ADDR_MCU
 
 #define IAP_MAGIC_SUCCESS		0x11223344
 #define IAP_MAGIC_FLASH			0xAABBCCDD

+ 9 - 5
Applications/foc/foc_api.c

@@ -33,7 +33,8 @@ static void foc_defulat_value(void){
 	g_foc.dq_command.Id = 0;
 	g_foc.dq_command.Iq = 0;
 	g_foc.foc_fault = foc_success;
-	g_foc.speed_command = -1;
+	g_foc.speed_command.speed = -1;
+	g_foc.speed_command.delta_ts = 0;
 	memset(&g_foc.phase_time, 0, sizeof(g_foc.phase_time));
 	g_foc.sector = 0;
 	g_foc.dq_last.Id = 0;
@@ -75,13 +76,15 @@ void foc_set_dq_command(float d, float q) {
 }
 
 void foc_set_voltage_ramp(float final){
-	printf("voltage %f\n", final);
 	ramp_set_target(&g_foc.voltage_ramp, g_foc.dq_last.Vq, final, START_RAMP_DURATION);
 	ramp_exc(&g_foc.voltage_ramp);
 }
 
 void foc_set_speed_ramp(u16 rpm){
-	ramp_set_target(&g_foc.speed_ramp, foc_get_speed(), rpm, SPEED_RAMP_DURATION);
+	if ((g_foc.speed_command.delta_ts == 0) || (g_foc.speed_command.delta_ts > START_RAMP_DURATION)) {
+		g_foc.speed_command.delta_ts = START_RAMP_DURATION;
+	}
+	ramp_set_target(&g_foc.speed_ramp, foc_get_speed(), rpm, g_foc.speed_command.delta_ts);
 	ramp_exc(&g_foc.speed_ramp);
 }
 
@@ -89,11 +92,12 @@ void foc_set_controller_mode(control_mode_t mode) {
 	g_foc.mode = mode;
 }
 
-void foc_set_speed(u16 rpm) {
+void foc_set_speed(u16 rpm, u32 delta_ms) {
 	if (g_foc.state == IDLE || g_foc.state == ANY_STOP) {
 		return;
 	}
-	g_foc.speed_command = rpm;
+	g_foc.speed_command.speed = rpm;
+	g_foc.speed_command.delta_ts = delta_ms;
 	if (g_foc.mode == FOC_MODE_OPEN_LOOP) {
 		foc_set_voltage_ramp(speed_to_voltage(rpm));
 	}

+ 1 - 2
Applications/foc/foc_api.h

@@ -2,7 +2,6 @@
 #define _FOC_API_H__
 #include "foc/foc_type.h"
 
-
 void foc_init(void);
 void foc_clear(void);
 void foc_set_controller_mode(control_mode_t mode);
@@ -18,7 +17,7 @@ void foc_current_calibrate(void);
 u32 foc_get_speed(void);
 float foc_get_vbus_voltage(void);
 bool foc_motor_is_started(void);
-void foc_set_speed(u16 rpm);
+void foc_set_speed(u16 rpm, u32 delta_ms);
 float speed_to_voltage(u16 rpm);
 float speed_to_current(u16 rpm);
 #endif /* _FOC_API_H__ */

+ 20 - 5
Applications/foc/foc_core.c

@@ -12,6 +12,7 @@
 #include "ntc_sensor.h"
 #include "gas_sensor.h"
 #include "svpwm.h"
+#include "bsp/timer_count32.h"
 
 motor_foc_t g_foc = {
 	.motor_param = {
@@ -44,11 +45,13 @@ motor_foc_t g_foc = {
 
 
 static void foc_measure_task(void*);
+static void foc_gas_task(void *args);
 
 void foc_core_init(void) {
 	vbus_sensor_init();
 	ntc_sensor_init();
 	co_task_create(foc_measure_task, NULL, 256);
+	co_task_create(foc_gas_task, NULL, 256);
 }
 
 #if 1
@@ -141,7 +144,6 @@ static void __inline Debug_dq(dq_t *dq){
 #endif	
 }
 
-
 /* FOC 主控制任务 */
 void FOC_Fast_Task(motor_foc_t *foc){
 	current_samp_t *c_sample = &foc->current_samp;
@@ -199,10 +201,10 @@ void Foc_Calc_Current_Ref(motor_foc_t *foc) {
 }
 
 void Foc_Speed_Ramp(motor_foc_t *foc){
-	if (foc->speed_command >= 0 && foc->mode != FOC_MODE_OPEN_LOOP){
+	if (foc->speed_command.speed >= 0 && foc->mode != FOC_MODE_OPEN_LOOP){
 		u16 current_rpm = foc_get_speed();
-		u16 ref_rpm = foc->speed_command;
-		foc->speed_command = -1;
+		u16 ref_rpm = foc->speed_command.speed;
+		foc->speed_command.speed = -1;
 		
 		if (ref_rpm + RPM_FOR_CLOSE_LOOP < current_rpm){
 			foc_set_voltage_ramp(speed_to_voltage(ref_rpm));
@@ -221,7 +223,7 @@ void foc_pwm_up_handler(void){
 	phase_current_adc_triger(&g_foc.current_samp);
 }
 
-
+measure_time_t g_meas_foc = {.exec_max_time = 25,};
 void mc_phase_current_irq(void) {
 	if (g_foc.current_samp.is_calibrating_offset) {
 		phase_current_offset(&g_foc.current_samp);
@@ -230,7 +232,9 @@ void mc_phase_current_irq(void) {
 		phase_Rds_calibrate(&g_foc.current_samp);
 		return;
 	}
+	time_measure_start(&g_meas_foc);
 	FOC_Fast_Task(&g_foc);
+	time_measure_end(&g_meas_foc);
 }
 
 void foc_pwm_start(bool start) {
@@ -258,4 +262,15 @@ static void foc_measure_task(void *args){
 	}
 }
 
+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);
+	}
+}
+
 

+ 4 - 1
Applications/foc/foc_stm.c

@@ -3,6 +3,7 @@
 #include "bsp/pwm.h"
 #include "foc/foc_api.h"
 #include "foc/foc_core.h"
+#include "foc/hall_sensor.h"
 
 extern motor_foc_t g_foc;
 
@@ -62,8 +63,10 @@ void FOC_Normal_Task(motor_foc_t *foc) {
 			break;
 		case READY_TO_RUN:
 			foc_pwm_start(true);
+			//hall_sensor_calibrate(1.0f, NULL);
 			foc_stm_nextstate(RAMPING_START);
-			ramp_exc(&foc->voltage_ramp);
+			//ramp_exc(&foc->voltage_ramp);
+			foc_set_voltage_ramp(1.0f);
 			break;
 		case RAMPING_START:
 			foc_set_dq_command(0.0f, ramp_get_target(&foc->voltage_ramp));

+ 5 - 2
Applications/foc/foc_type.h

@@ -104,13 +104,16 @@ typedef enum {
 	foc_brake_error = 100,
 }foc_fault_t;
 
-
+typedef struct {
+	int  speed;
+	u32  delta_ts; //ms
+}speed_cmd_t;
 typedef struct foc_s {
 	bool is_motor_start;
 	alpha_beta_t alpha_beta;
 	current_samp_t current_samp; /*三相电流采集 */
 	dq_t dq_command; 
-	int  speed_command; 
+	speed_cmd_t speed_command;
 	dq_t dq_last;
 	u8   sector; //svpwm 扇区
 	float vbus; //母线电压

+ 6 - 9
Applications/foc/gas_sensor.c

@@ -1,6 +1,7 @@
 #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){
@@ -9,19 +10,15 @@ void gas_sensor_init(void){
 	gas_sample_voltage();
 }
 
-void gas_sample_voltage(void){
+float gas_sample_voltage(void){
 	u32 vadc = adc_sample_regular_channel(HANDLERBAR_CHAN, 16);
-	_gas.voltage = ((float)vadc)/(65536.0f) * ADC_REFERENCE_VOLTAGE;
-}
-
-
-float gas_get_value(void){
-
-	return _gas.voltage/ADC_REFERENCE_VOLTAGE;
+	_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_get_value() >= 1.0f;
+	return _gas.voltage > MIN_GAS_VALUE;
 }
 

+ 4 - 2
Applications/foc/gas_sensor.h

@@ -7,9 +7,11 @@ typedef struct {
 	float lowpass;
 }gas_t;
 
+#define MIN_GAS_VALUE 1000.0F
+#define MAX_GAS_VALUE 5000.0F
+
 void gas_sensor_init(void);
-void gas_sample_voltage(void);
-float gas_get_value(void); //归一化
+float gas_sample_voltage(void);
 bool gas_detect_speed_signal(void);
 
 #endif /* _GAS_SENSOR_H__ */

+ 23 - 124
Applications/foc/hall_sensor.c

@@ -6,6 +6,8 @@
 #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
 /* 
@@ -26,24 +28,20 @@
 110
 4,5,1,3,2,6,4
 */
-static u16 _hall_table[] = {0xFFFF, 292/*1*/, 54/*2*/, 1/*3*/, 180/*4*/, 229/*5*/, 115/*6*/, 0xFFFF};
-//static u16 _hall_table[] = {0xFFFF, 257/*1*/, 36/*2*/, 344/*3*/, 159/*4*/, 222/*5*/, 88/*6*/, 0xFFFF};
+//static u16 _hall_table[] = {0xFFFF, 292/*1*/, 54/*2*/, 1/*3*/, 180/*4*/, 229/*5*/, 115/*6*/, 0xFFFF};
+static u16 _hall_table[] = {0xFFFF, 121/*1*/, 240/*2*/, 190/*3*/, 13/*4*/, 58/*5*/, 306/*6*/, 0xFFFF};
 static hall_t _hall;
 static hall_sample_t h_samples;
 
 #define read_hall(h,t) {h = get_hall_stat(HALL_READ_TIMES); t = _hall_table[h];}
-#define tick_2_s(tick) ((float)tick / (float)SYSTEM_CLOCK)
+#define us_2_s(tick) ((float)tick / 1000000.0f)
 
-static u32 __inline delta_ticks(u32 prev) {
-	u32 now = cpu_counts_abs();
-	if (now >= prev) {
-		return (now - prev);
-	}
-	return (0xFFFFFFFFU - prev + now) + 1;
+static float __inline _delta_seconds(u32 prev) {
+	return (float)timer_count32_delta_us(prev, NULL)/1000000.0f;
 }
 
 static u32 __inline get_seconds(void) {
-	return co_task_sys64_ticks()/1000;
+	return ticks_2_ms(co_task_sys64_ticks()/1000);
 }
 
 static void _hall_put_sample(float angle, u32 ticks) {
@@ -68,16 +66,14 @@ static float __inline _hall_avg_speed(void){
 	if (t_ticks == 0.0f) {
 		return 0.0f;
 	}
-	return (t_angle / tick_2_s(t_ticks));
+	return (t_angle / us_2_s(t_ticks));
 }
 
 void hall_sensor_init(void) {
 	mc_hall_init();
 	memset(&_hall, 0, sizeof(_hall));
 	read_hall(_hall.state, _hall.theta);
-#ifdef HALL_PLACE_OFFSET	
-	_hall.phase_offset = HALL_PLACE_OFFSET;
-#endif
+	_hall.phase_offset = 180.0f;//mc_config_get()->hall_offset;
 }
 
 
@@ -99,7 +95,7 @@ float hall_sensor_get_theta(void){
 		read_hall(_hall.state, _hall.theta);
 		return _hall.theta;
 	}
-	_hall.est_theta = tick_2_s(delta_ticks(_hall.ticks)) * _hall.degree_per_s + _hall.theta;
+	_hall.est_theta = _delta_seconds(_hall.ticks) * _hall.degree_per_s + _hall.theta;
 	float est_delta = _hall.est_theta - _hall.theta;
 	if (est_delta > PHASE_60_DEGREE) {
 		_hall.est_theta = _hall.theta + PHASE_60_DEGREE;
@@ -132,13 +128,16 @@ float hall_sensor_avg_speed(void) {
 
 
 int hall_sensor_calibrate(float voltage, u16 *hall_table){
+	if (hall_table == NULL) {
+		hall_table = _hall_table;
+	}
 	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 < 1000;i++) {
 		foc_set_dq_command((float)i * voltage / 1000.0f, 0.0f);
-		delay_ms(1000);
+		delay_ms(1);
 	}
 	float sin_hall[8];
 	float cos_hall[8];
@@ -146,12 +145,12 @@ int hall_sensor_calibrate(float voltage, u16 *hall_table){
 	memset(sin_hall, 0, sizeof(sin_hall));
 	memset(cos_hall, 0, sizeof(cos_hall));
 	memset(hall_iterations, 0, sizeof(hall_iterations));
-	delay_ms(50 * 1000);
+	delay_ms(5 * 1000);
 	// Forwards
 	for (int i = 0;i < 3;i++) {
 		for (int j = 0;j < 360;j++) {
 			hall_sensor_set_theta(true, j);
-			delay_ms(10 * 1000);
+			delay_ms(5);
 
 			int hall = get_hall_stat(7);
 			float s, c;
@@ -166,7 +165,7 @@ int hall_sensor_calibrate(float voltage, u16 *hall_table){
 	for (int i = 0;i < 3;i++) {
 		for (int j = 360;j >= 0;j--) {
 			hall_sensor_set_theta(true, j);
-			delay_ms(10 * 1000);
+			delay_ms(5);
 
 			int hall = get_hall_stat(7);
 			float s, c;
@@ -193,7 +192,7 @@ int hall_sensor_calibrate(float voltage, u16 *hall_table){
 	return fails == 2;	
 }
 
-#ifdef HALL_PLACE_OFFSET
+
 void hall_sensor_handler(void) {
 	u8 state_now = get_hall_stat(HALL_READ_TIMES);
 	float theta_now = _hall_table[state_now];
@@ -204,7 +203,7 @@ void hall_sensor_handler(void) {
 			_hall.working = true;
 			_hall.state = state_now;
 			_hall.theta = theta_now;
-			_hall.ticks = cpu_counts_abs();
+			_hall.ticks = timer_count32_get();
 		}
 		return;
 	}
@@ -269,19 +268,19 @@ void hall_sensor_handler(void) {
 	}
 
 
-	float delta_time = tick_2_s(delta_ticks(_hall.ticks));
+	float delta_time = _delta_seconds(_hall.ticks);
 	if (delta_time == 0.0f) { //may be errors ???
 		return;
 	}
 	float delta_theta = (_hall.direction == POSITIVE)?60.0f : -60.0f;
-	_hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
+	_hall_put_sample(delta_theta, timer_count32_delta_us(_hall.ticks, NULL));
 	if (!h_samples.full) {
 		_hall.degree_per_s = delta_theta / delta_time;
 	}else {
 		_hall.degree_per_s = _hall_avg_speed();
 		_hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
 	}
-	_hall.ticks = cpu_counts_abs();
+	_hall.ticks = timer_count32_get();
 	_hall.second = get_seconds();
 	_hall.theta = theta_now;
 	_hall.state = state_now;
@@ -289,104 +288,4 @@ void hall_sensor_handler(void) {
 	//printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
 }
 
-#else
-void hall_sensor_handler1(void) {
-	u8 state_now = get_hall_stat(HALL_READ_TIMES);
-	float theta_now = _hall_table[state_now];
-	u8 state_prev = _hall.state;
-	float theta_prev = _hall.theta;
-
-	if (!_hall.working) {		
-		if(theta_now != 0xFFFF) {
-			_hall.working = true;
-			_hall.state = state_now;
-			_hall.theta = theta_now;
-			_hall.ticks = task_ticks_abs();
-		}
-		return;
-	}
-	//printf("hall %d, %d\n", state_now, state_prev);
-	//{0xFFFF, 257/*1*/, 36/*2*/, 344/*3*/, 159/*4*/, 222/*5*/, 88/*6*/, 0xFFFF};
-	float delta_theta = 360.0f;
-	switch (state_now) {
-		case STATE_1:
-			if (state_prev == STATE_5) {
-				_hall.direction = POSITIVE;
-				delta_theta = theta_now - theta_prev;
-			}else if (state_prev == STATE_3) {
-				_hall.direction = NEGATIVE;
-				delta_theta = 360 - theta_now + theta_prev;
-			}
-			break;
-		case STATE_2:
-			if (state_prev == STATE_3) {
-				_hall.direction = POSITIVE;
-				delta_theta = theta_now - theta_prev;
-			}else if (state_prev == STATE_6) {
-				_hall.direction = NEGATIVE;
-				delta_theta = theta_prev - theta_now;
-			}
-			break;
-		case STATE_3:
-			if (state_prev == STATE_1) {
-				_hall.direction = POSITIVE;
-				delta_theta = 360 - theta_prev + theta_now;
-			}else if (state_prev == STATE_2) {
-				_hall.direction = NEGATIVE;
-				delta_theta = theta_prev - theta_now;
-			}
-			break;
-		case STATE_4:
-			if (state_prev == STATE_6) {
-				_hall.direction = POSITIVE;
-				delta_theta = theta_now - theta_prev;
-			}else if (state_prev == STATE_5) {
-				_hall.direction = NEGATIVE;
-				delta_theta = theta_prev - theta_now;
-			}
-			break;
-		case STATE_5:
-			if (state_prev == STATE_4) {
-				_hall.direction = POSITIVE;
-				delta_theta = theta_now - theta_prev;
-			}else if (state_prev == STATE_1) {
-				_hall.direction = NEGATIVE;
-				delta_theta = theta_prev - theta_now;
-			}
-			break;
-		case STATE_6:
-			if (state_prev == STATE_2) {
-				_hall.direction = POSITIVE;
-				delta_theta = theta_now - theta_prev;
-			}else if (state_prev == STATE_4) {
-				_hall.direction = NEGATIVE;
-				delta_theta = theta_prev - theta_now;
-			}
-			break;
-		default:
-			break;
-	}
-	if (delta_theta == 360.0f) { //no vilid hall 
-		return;
-	}
-
-	float delta_time = tick_2_s(delta_ticks(_hall.ticks));
-	if (delta_time == 0.0f) { //may be errors ???
-		return;
-	}
-	delta_theta = 60.0f;
-	_hall_put_sample(delta_theta, delta_ticks(_hall.ticks));
-	if (!h_samples.full) {
-		_hall.degree_per_s = delta_theta / delta_time;
-	}else {
-		_hall.degree_per_s = _hall_avg_speed();
-		_hall.e_filted_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
-	}
-	_hall.ticks = task_ticks_abs();
-	_hall.theta += delta_theta;
-	_hall.state = state_now;
-	_hall.e_rpm = _hall.degree_per_s / 360.0f * 60.0f; //电角速度
-	//printf("speed :%.4f - %.4f - %.4f - %d\n", _hall.degree_per_s, delta_theta, delta_time, (int)_hall.e_rpm);
-}
-#endif
 

+ 3 - 3
Applications/foc/ntc_sensor.c

@@ -10,10 +10,10 @@ void ntc_sensor_init(void) {
 }
 
 void ntc_sensor_sample(void){
-		u16 w_temp = adc_sample_regular_channel(MOTOR_TEMP_CHAN, 16);
-    w_temp -= ( s32 )(V0_V *65536/ ADC_REFERENCE_VOLTAGE );
+	u16 w_temp = adc_sample_regular_channel(MOTOR_TEMP_CHAN, 16);
+    w_temp -= ( s32 )(V0_V *4096/ ADC_REFERENCE_VOLTAGE );
     w_temp *= (ADC_REFERENCE_VOLTAGE/dV_dT);
-    w_temp = w_temp / 65536 + ( s32 )( T0_C );
+    w_temp = w_temp / 4096 + ( s32 )( T0_C );
 
 	_ntc.temp_avg = w_temp * _ntc.low_pass_filter + _ntc.temp_avg * (1.0f - _ntc.low_pass_filter);
 }

+ 1 - 1
Applications/foc/vbus_sensor.c

@@ -13,7 +13,7 @@ void vbus_sensor_init(void){
 void vbus_sample_voltage(void){
 	u32 vadc = adc_sample_regular_channel(VBUS_V_CHAN, 16);
 
-	_vbus.voltage_avg = ((float)vadc)/(65536.0f) * ADC_REFERENCE_VOLTAGE / VBUS_PARTITIONING_FACTOR;
+	_vbus.voltage_avg = ((float)vadc)/(4096.0f) * ADC_REFERENCE_VOLTAGE / VBUS_PARTITIONING_FACTOR;
 	if (_vbus.voltage_filted == 0.0f) {
 		_vbus.voltage_filted = _vbus.voltage_avg;
 	}else {

+ 1 - 1
Applications/libs/logger.h

@@ -21,7 +21,7 @@
 #define MOD_BLE      3 //for ble
 #define MOD_SYSTEM    4
 
-#define LOG_UART 1
+#define LOG_UART 0
 
 extern void set_log_level(int mod, int l);
 extern void log_debug(int mod, char *fmt, ...);

+ 9 - 5
Applications/os/co_task.c

@@ -25,7 +25,7 @@ extern void arch_save_context(void);
 extern void arch_restore_context(void);
 extern void arch_start_first_task(void);
 extern u32 arch_stack_init(volatile u32 *stack_base, u16 stack_size, void *fptr, void *p_arg);
-extern void co_task_tick_init(void);
+extern void co_task_tick_init(int ticks);
 
 static tcb_t *_next_task(void);
 static void _task_add(tcb_t *tcb);
@@ -44,7 +44,7 @@ void *co_task_create(task_func func, void *args, u16 stack_size){
 }
 
 void co_task_schedule(void){
-	co_task_tick_init();
+	co_task_tick_init(TICKS_PER_HZ);
 	current_tcb = g_tcb_head;
 	arch_start_first_task();
 	while(1);
@@ -58,7 +58,7 @@ void co_task_yield(void){
 }
 
 void co_task_delay(int ms){
-	current_tcb->start_tm_ms = ms + g_task_ticks;
+	current_tcb->start_tm_ms = ms + ticks_2_ms(g_task_ticks);
 	while(1) {
 		tcb_t *next = _next_task();
 		if (next != NULL){
@@ -77,6 +77,10 @@ u64 co_task_sys64_ticks(void) {
 	return g_task_ticks64;
 }
 
+u64 co_task_sys64_ts(void) {
+	return ticks_2_ms(g_task_ticks64);
+}
+
 /* peak next task to run */
 static tcb_t *_next_task(void) {
 	tcb_t *next = current_tcb->next;
@@ -84,11 +88,11 @@ static tcb_t *_next_task(void) {
 		if (next->stat != Task_Ready) {
 			continue;
 		}
-		if (next->start_tm_ms <= g_task_ticks) {
+		if (next->start_tm_ms <= ticks_2_ms(g_task_ticks)){
 			return next;
 		}
 	}
-	if (current_tcb->start_tm_ms <= g_task_ticks) {
+	if (current_tcb->start_tm_ms <= ticks_2_ms(g_task_ticks)) {
 		return (tcb_t *)current_tcb;
 	}	
 	return NULL;

+ 4 - 0
Applications/os/co_task.h

@@ -5,12 +5,16 @@
 /*
 Task如果没有调用task_yield/delay 永远执行下去
 */
+
+#define TICKS_PER_HZ 1000
+#define ticks_2_ms(ticks) (ticks*(1000/TICKS_PER_HZ))
 typedef void (*task_func)(void *);
 
 void cpu_counts_enable(void);
 u32 cpu_counts_abs(void);
 void cpu_udelay(u32 delay);
 u64 co_task_sys64_ticks(void);
+u64 co_task_sys64_ts(void);
 void *co_task_create(task_func func, void *param, u16 stack_size);
 void co_task_delay(int ms); //task 延时
 void co_task_yield(void); //task 让出cpu

+ 7 - 0
Applications/os/cpu.c

@@ -35,3 +35,10 @@ void cpu_udelay(u32 delay)
 }
 
 
+u32 cpu_count_delta_us(u32 count) {
+	u32 cpu_c = cpu_counts_rel(count);
+	float us = (float)cpu_c/120;
+	
+	return (u32)us;
+}
+

+ 2 - 2
Applications/os/port/cortex-m4.c

@@ -1,9 +1,9 @@
 #include "os/os_type.h"
 #include "bsp/bsp.h"
 
-void co_task_tick_init(void){
+void co_task_tick_init(int ticks){
 	/* setup systick timer for 1000Hz interrupts */
-	SysTick_Config(SystemCoreClock / 1000);
+	SysTick_Config(SystemCoreClock / ticks);
 	/* configure the systick handler priority */
 	NVIC_SetPriority(SysTick_IRQn, 0x00U);
 }

+ 2 - 2
Applications/os/queue.c

@@ -38,7 +38,7 @@ void *queue_create(u16 queue_count, u16 esize){
 	queue->w_pos = queue->r_pos = 0;
 	return queue;
 }
-bool queue_put(void *vqueue, void *data){
+bool queue_put(co_queue_t vqueue, void *data){
 	queue_t *queue = (queue_t *)vqueue;
 	if (!_have_write_space(queue)){
 		return false;
@@ -54,7 +54,7 @@ bool queue_put(void *vqueue, void *data){
 
 }
 
-bool queue_get(void *vqueue, void *data) {
+bool queue_get(co_queue_t vqueue, void *data) {
 	queue_t *queue = (queue_t *)vqueue;
 	if(!_have_read_space(queue)){
 		return false;

+ 4 - 2
Applications/os/queue.h

@@ -2,9 +2,11 @@
 #define _Queue_H__
 #include "os/os_type.h"
 
+typedef void * co_queue_t;
+
 void *queue_create(u16 queue_count, u16 esize);
-bool queue_put(void *queue, void *data);
-bool queue_get(void *queue, void *data);
+bool queue_put(co_queue_t queue, void *data);
+bool queue_get(co_queue_t queue, void *data);
 
 #endif /* _Queue_H__ */
 

+ 17 - 60
Applications/prot/can_message.c

@@ -5,61 +5,18 @@
 #include "can_message.h"
 #include "wait_queue.h"
 
-#define MAX_CAN_MESSAGE 4
-static can_message_t messages[MAX_CAN_MESSAGE];
-static msg_list_t msg_list[10];
-static wait_queue_t wait_queue;
-
-
-static node_list_t _pending_list = LIST_HEAD_INIT(_pending_list);
-static node_list_t _idle_list = LIST_HEAD_INIT(_idle_list);
-
 static void can_process_message(can_message_t *message);
 static void free_can_message(can_message_t *message);
-static void _can_poll_task(void*);
-static void handle_can_frame(void);
+
+#define MAX_CAN_MESSAGE 10
+static can_message_t messages[MAX_CAN_MESSAGE];
+static wait_queue_t wait_queue;
 
 void can_message_init(void){
-	for(int i = 0; i < ARRAY_SIZE(msg_list); i++) {
-		init_list_node(&msg_list[i].list);
-		list_add_tail(&_idle_list, &msg_list[i].list);
-	}
+	wait_queue_init(&wait_queue, 32);
 	shark_can0_init();
-	co_task_create(_can_poll_task, NULL, 256);
-}
-
-void put_raw_message(can_id_t id, uint8_t *data, int len) {
-	if (list_empty(&_idle_list)){
-		return;
-	}
-	msg_list_t *raw = (msg_list_t *)_idle_list.next;
-	list_del(&raw->list);
-	raw->msg.id = id;
-	raw->msg.len = len;
-	memcpy(raw->msg.data, data, len);
-	list_add_tail(&_pending_list, &raw->list);
 }
 
-static msg_list_t *_get_raw_message(void) {
-	if (list_empty(&_pending_list)){
-		return NULL;
-	}
-	msg_list_t *raw = (msg_list_t *)_pending_list.next;
-	list_del(&raw->list);
-	return raw;
-}
-static void free_raw_message(msg_list_t *raw) {
-	list_add_tail(&_idle_list, &raw->list);
-}
-
-static void _can_poll_task(void *args) {
-	while(1) {
-		handle_can_frame();
-		co_task_yield();
-	}
-}
-
-
 static can_message_t *get_message_by_id(can_id_t id){
 	can_message_t *idle_msg = NULL;
 	can_message_t *src_msg = NULL;
@@ -105,15 +62,7 @@ s32 can_send_message(uint32_t can_id, u8 *data, int len, s32 timeout){
 
 }
 
-static void handle_can_frame(void){
-	msg_list_t *raw = _get_raw_message();
-	if (raw == NULL) {
-		return;
-	}
-	can_id_t id = raw->msg.id;
-	uint8_t *data = raw->msg.data;
-	int len = raw->msg.len;
-	
+void handle_can_frame(can_id_t id, uint8_t *data, int len){
 	can_message_t *message = get_message_by_id(id);
 	if (message == NULL) {
 		return ;
@@ -127,10 +76,14 @@ static void handle_can_frame(void){
 			&& (idx <= total)){
 		if ((idx == 1) && (len >= 2)) { //first frame for message
 			key = decoder_can_key(data);
+			if (message->data) {
+				co_free(message->data);
+			}
 
 			message->key = key;
 			message->dest = id.dest;
 			message->src = id.src;
+			message->data = co_malloc(total * CAN_DLC_LENGTH);
 			message->len = 0;
 			message->idx = idx;
 			message->type= id.type;
@@ -143,7 +96,7 @@ static void handle_can_frame(void){
 				memcpy(message->data + message->len, data, len);
 				message->len += len;
 				if (idx == total) { //last frame
-					if (id.type == ptype_response) {
+					if (message->type == ptype_response) {
 						wait_queue_key_acked(&wait_queue, message->key);
 					}
 					can_process_message(message);
@@ -154,11 +107,11 @@ static void handle_can_frame(void){
 			}
 		}
 	}
-	free_raw_message(raw);
 }
 
 
 static void can_process_message(can_message_t *message){
+	sys_debug("can %x [%x -> %x], len = %d\n", message->key, message->src, message->dest, message->len);
 	if (message->key & 0xFF >= 0xF0) {
 		//do iap update
 	}else if (message->src == 0x42){ //只处理后控的指令
@@ -166,14 +119,18 @@ static void can_process_message(can_message_t *message){
 	}
 	free_can_message(message);
 }
+
 static void free_can_message(can_message_t *message){
+	if (message->data) {
+		co_free(message->data);
+	}
+	message->data = NULL;
 	message->src = 0;
 	message->len = 0;
 	message->idx = 0xFF;
 	message->total_frame = 0xFF;
 }
 
-
 void can_send_response(uint8_t can_add, uint8_t *data, int len){
 	can_send_message(get_reponse_can_id(can_add), data, len, 50);
 }

+ 1 - 12
Applications/prot/can_message.h

@@ -6,22 +6,11 @@
 
 #define can_payload_offset  2
 
-typedef struct {
-	uint8_t data[8];
-	int len;
-	can_id_t id;
-}can_raw_msg_t;
-
-typedef struct {
-	node_list_t list;
-	can_raw_msg_t msg;
-}msg_list_t;
-
 typedef struct {
 	uint16_t key;
 	uint8_t dest;
 	uint8_t src;
-	uint8_t data[256];
+	uint8_t *data;
 	uint16_t len;
 	uint8_t idx;
 	uint8_t total_frame;	

+ 3 - 3
Librarys/CMSIS/GD/GD32F30x/Source/system_gd32f30x.c

@@ -39,7 +39,7 @@
 /* system frequency define */
 #define __IRC8M           (IRC8M_VALUE)            /* internal 8 MHz RC oscillator frequency */
 #define __HXTAL           (HXTAL_VALUE)            /* high speed crystal oscillator frequency */
-#define __SYS_OSC_CLK     (__IRC8M)                /* main oscillator frequency */
+#define __SYS_OSC_CLK     (__HXTAL)                /* main oscillator frequency */
 
 /* select a system clock by uncommenting the following line */
 /* use IRC8M */
@@ -47,14 +47,14 @@
 //#define __SYSTEM_CLOCK_48M_PLL_IRC8M            (uint32_t)(48000000)
 //#define __SYSTEM_CLOCK_72M_PLL_IRC8M            (uint32_t)(72000000)
 //#define __SYSTEM_CLOCK_108M_PLL_IRC8M           (uint32_t)(108000000)
-#define __SYSTEM_CLOCK_120M_PLL_IRC8M           (uint32_t)(120000000)
+//#define __SYSTEM_CLOCK_120M_PLL_IRC8M           (uint32_t)(120000000)
 
 /* use HXTAL(XD series CK_HXTAL = 8M, CL series CK_HXTAL = 25M) */
 //#define __SYSTEM_CLOCK_HXTAL                    (uint32_t)(__HXTAL)
 //#define __SYSTEM_CLOCK_48M_PLL_HXTAL            (uint32_t)(48000000)
 //#define __SYSTEM_CLOCK_72M_PLL_HXTAL            (uint32_t)(72000000)
 //#define __SYSTEM_CLOCK_108M_PLL_HXTAL           (uint32_t)(108000000)
-//#define __SYSTEM_CLOCK_120M_PLL_HXTAL           (uint32_t)(120000000)
+#define __SYSTEM_CLOCK_120M_PLL_HXTAL           (uint32_t)(120000000)
 
 #define SEL_IRC8M       0x00U
 #define SEL_HXTAL       0x01U

+ 145 - 80
Project/MC100_OS.uvoptx

@@ -135,7 +135,7 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>DLGUARM</Key>
-          <Name></Name>
+          <Name>d</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
@@ -148,31 +148,42 @@
           <Name>UL2CM3(-S0 -C0 -P0 -FD20000000 -FC1000 -FN1 -FF0GD32F30x_HD -FS08000000 -FL010000 -FP0($$Device:GD32F303CC$Flash\GD32F30x_HD.FLM))</Name>
         </SetRegEntry>
       </TargetDriverDllRegistry>
-      <Breakpoint>
-        <Bp>
-          <Number>0</Number>
-          <Type>0</Type>
-          <LineNumber>16</LineNumber>
-          <EnabledFlag>1</EnabledFlag>
-          <Address>134232614</Address>
-          <ByteObject>0</ByteObject>
-          <HtxType>0</HtxType>
-          <ManyObjects>0</ManyObjects>
-          <SizeOfObject>0</SizeOfObject>
-          <BreakByAccess>0</BreakByAccess>
-          <BreakIfRCount>1</BreakIfRCount>
-          <Filename>..\Applications\main.c</Filename>
-          <ExecCommand></ExecCommand>
-          <Expression>\\MC100\../Applications/main.c\16</Expression>
-        </Bp>
-      </Breakpoint>
+      <Breakpoint/>
       <WatchWindow1>
         <Ww>
           <count>0</count>
           <WinNumber>1</WinNumber>
           <ItemText>g_tcb_head</ItemText>
         </Ww>
+        <Ww>
+          <count>1</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>g_meas_foc,0x0A</ItemText>
+        </Ww>
+        <Ww>
+          <count>2</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>SystemCoreClock,0x0A</ItemText>
+        </Ww>
+        <Ww>
+          <count>3</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>g_foc,0x0A</ItemText>
+        </Ww>
+        <Ww>
+          <count>4</count>
+          <WinNumber>1</WinNumber>
+          <ItemText>_hall_table,0x0A</ItemText>
+        </Ww>
       </WatchWindow1>
+      <MemoryWindow1>
+        <Mm>
+          <WinNumber>1</WinNumber>
+          <SubType>1</SubType>
+          <ItemText>0x20009240</ItemText>
+          <AccSizeX>0</AccSizeX>
+        </Mm>
+      </MemoryWindow1>
       <Tracepoint>
         <THDelay>0</THDelay>
       </Tracepoint>
@@ -215,6 +226,36 @@
       <pszMrulep></pszMrulep>
       <pSingCmdsp></pSingCmdsp>
       <pMultCmdsp></pMultCmdsp>
+      <SystemViewers>
+        <Entry>
+          <Name>System Viewer\ADC0</Name>
+          <WinId>35903</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\ADC1</Name>
+          <WinId>35902</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\AFIO</Name>
+          <WinId>35899</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\GPIOA</Name>
+          <WinId>35900</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\TIMER0</Name>
+          <WinId>35901</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\TIMER1</Name>
+          <WinId>35905</WinId>
+        </Entry>
+        <Entry>
+          <Name>System Viewer\TIMER2</Name>
+          <WinId>35904</WinId>
+        </Entry>
+      </SystemViewers>
     </TargetOption>
   </Target>
 
@@ -248,6 +289,18 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>1</GroupNumber>
+      <FileNumber>3</FileNumber>
+      <FileType>1</FileType>
+      <tvExp>0</tvExp>
+      <tvExpOptDlg>0</tvExpOptDlg>
+      <bDave2>0</bDave2>
+      <PathWithFileName>..\Applications\app\nv_storage.c</PathWithFileName>
+      <FilenameWithoutPath>nv_storage.c</FilenameWithoutPath>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -258,7 +311,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>3</FileNumber>
+      <FileNumber>4</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -270,7 +323,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>4</FileNumber>
+      <FileNumber>5</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -282,7 +335,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>5</FileNumber>
+      <FileNumber>6</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -294,7 +347,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>6</FileNumber>
+      <FileNumber>7</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -306,7 +359,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>7</FileNumber>
+      <FileNumber>8</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -318,7 +371,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>8</FileNumber>
+      <FileNumber>9</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -330,7 +383,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>9</FileNumber>
+      <FileNumber>10</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -342,7 +395,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>10</FileNumber>
+      <FileNumber>11</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -354,7 +407,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>11</FileNumber>
+      <FileNumber>12</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -366,7 +419,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>12</FileNumber>
+      <FileNumber>13</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -378,7 +431,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>13</FileNumber>
+      <FileNumber>14</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -390,7 +443,7 @@
     </File>
     <File>
       <GroupNumber>2</GroupNumber>
-      <FileNumber>14</FileNumber>
+      <FileNumber>15</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -410,7 +463,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>15</FileNumber>
+      <FileNumber>16</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -422,7 +475,7 @@
     </File>
     <File>
       <GroupNumber>3</GroupNumber>
-      <FileNumber>16</FileNumber>
+      <FileNumber>17</FileNumber>
       <FileType>4</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -436,13 +489,13 @@
 
   <Group>
     <GroupName>Proto</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>17</FileNumber>
+      <FileNumber>18</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -454,7 +507,7 @@
     </File>
     <File>
       <GroupNumber>4</GroupNumber>
-      <FileNumber>18</FileNumber>
+      <FileNumber>19</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -468,13 +521,13 @@
 
   <Group>
     <GroupName>OS</GroupName>
-    <tvExp>1</tvExp>
+    <tvExp>0</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>19</FileNumber>
+      <FileNumber>20</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -486,7 +539,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>20</FileNumber>
+      <FileNumber>21</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -498,7 +551,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>21</FileNumber>
+      <FileNumber>22</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -510,7 +563,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>22</FileNumber>
+      <FileNumber>23</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -522,7 +575,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>23</FileNumber>
+      <FileNumber>24</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -534,7 +587,7 @@
     </File>
     <File>
       <GroupNumber>5</GroupNumber>
-      <FileNumber>24</FileNumber>
+      <FileNumber>25</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -554,7 +607,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>25</FileNumber>
+      <FileNumber>26</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -566,7 +619,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>26</FileNumber>
+      <FileNumber>27</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -578,7 +631,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>27</FileNumber>
+      <FileNumber>28</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -590,7 +643,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>28</FileNumber>
+      <FileNumber>29</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -602,7 +655,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>29</FileNumber>
+      <FileNumber>30</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -614,7 +667,7 @@
     </File>
     <File>
       <GroupNumber>6</GroupNumber>
-      <FileNumber>30</FileNumber>
+      <FileNumber>31</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -634,7 +687,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>31</FileNumber>
+      <FileNumber>32</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -646,7 +699,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>32</FileNumber>
+      <FileNumber>33</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -658,7 +711,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>33</FileNumber>
+      <FileNumber>34</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -670,7 +723,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>34</FileNumber>
+      <FileNumber>35</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -682,7 +735,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>35</FileNumber>
+      <FileNumber>36</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -694,7 +747,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>36</FileNumber>
+      <FileNumber>37</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -706,7 +759,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>37</FileNumber>
+      <FileNumber>38</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -718,7 +771,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>38</FileNumber>
+      <FileNumber>39</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -730,7 +783,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>39</FileNumber>
+      <FileNumber>40</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -742,7 +795,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>40</FileNumber>
+      <FileNumber>41</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -754,7 +807,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>41</FileNumber>
+      <FileNumber>42</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -766,7 +819,7 @@
     </File>
     <File>
       <GroupNumber>7</GroupNumber>
-      <FileNumber>42</FileNumber>
+      <FileNumber>43</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -776,6 +829,18 @@
       <RteFlg>0</RteFlg>
       <bShared>0</bShared>
     </File>
+    <File>
+      <GroupNumber>7</GroupNumber>
+      <FileNumber>44</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>
+      <RteFlg>0</RteFlg>
+      <bShared>0</bShared>
+    </File>
   </Group>
 
   <Group>
@@ -786,7 +851,7 @@
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>43</FileNumber>
+      <FileNumber>45</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -798,7 +863,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>44</FileNumber>
+      <FileNumber>46</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -810,7 +875,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>45</FileNumber>
+      <FileNumber>47</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -822,7 +887,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>46</FileNumber>
+      <FileNumber>48</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -834,7 +899,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>47</FileNumber>
+      <FileNumber>49</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -846,7 +911,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>48</FileNumber>
+      <FileNumber>50</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -858,7 +923,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>49</FileNumber>
+      <FileNumber>51</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -870,7 +935,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>50</FileNumber>
+      <FileNumber>52</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -882,7 +947,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>51</FileNumber>
+      <FileNumber>53</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -894,7 +959,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>52</FileNumber>
+      <FileNumber>54</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -906,7 +971,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>53</FileNumber>
+      <FileNumber>55</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -918,7 +983,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>54</FileNumber>
+      <FileNumber>56</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -930,7 +995,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>55</FileNumber>
+      <FileNumber>57</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -942,7 +1007,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>56</FileNumber>
+      <FileNumber>58</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -954,7 +1019,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>57</FileNumber>
+      <FileNumber>59</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -966,7 +1031,7 @@
     </File>
     <File>
       <GroupNumber>8</GroupNumber>
-      <FileNumber>58</FileNumber>
+      <FileNumber>60</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -980,13 +1045,13 @@
 
   <Group>
     <GroupName>StartUp</GroupName>
-    <tvExp>0</tvExp>
+    <tvExp>1</tvExp>
     <tvExpOptDlg>0</tvExpOptDlg>
     <cbSel>0</cbSel>
     <RteFlg>0</RteFlg>
     <File>
       <GroupNumber>9</GroupNumber>
-      <FileNumber>59</FileNumber>
+      <FileNumber>61</FileNumber>
       <FileType>1</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>
@@ -998,7 +1063,7 @@
     </File>
     <File>
       <GroupNumber>9</GroupNumber>
-      <FileNumber>60</FileNumber>
+      <FileNumber>62</FileNumber>
       <FileType>2</FileType>
       <tvExp>0</tvExp>
       <tvExpOptDlg>0</tvExpOptDlg>

+ 10 - 0
Project/MC100_OS.uvprojx

@@ -393,6 +393,11 @@
               <FileType>1</FileType>
               <FilePath>..\Applications\app\app.c</FilePath>
             </File>
+            <File>
+              <FileName>nv_storage.c</FileName>
+              <FileType>1</FileType>
+              <FilePath>..\Applications\app\nv_storage.c</FilePath>
+            </File>
           </Files>
         </Group>
         <Group>
@@ -623,6 +628,11 @@
               <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>
         <Group>

BIN
Simulink/DQSVPWM.slx


BIN
Simulink/SVPWM_MOS.slx