Bläddra i källkod

follow sim1201

Ye Jin 1 år sedan
förälder
incheckning
17892453c0

BIN
User project.zip


+ 4 - 0
User project/1.FrameLayer/Source/main.c

@@ -38,6 +38,7 @@
 #include "app.h"
 #include "Temp.h"
 #include "enviolo_can.h"
+#include "profiler.h"
 /***************************
 *********************************************
  Exported Functions:
@@ -76,6 +77,8 @@ int main(void)
     stl_voRunTimeChecksInit();
     /* Watchdog 1s */
     hw_voIWDGInit(FWDGT_PSC_DIV32,1250);//1s
+    /* Program load rate testing init */ 
+    profiler_init();
     //Enviolo gear sensor init
     GearBox_Init();
     /* Timer enable */
@@ -88,6 +91,7 @@ int main(void)
     /* Enter infinite loop */
     while (1)
     {
+        PROFILER_BG();
         AppLoop();
     }
     

+ 1 - 0
User project/1.FrameLayer/Source/sys_task.c

@@ -7,6 +7,7 @@
 
 void SysTask1Ms()
 {
+    profiler_server();
     cp_ulSystickCnt ++;
     /* MCU Self Check*/
     clas_ubSystickFlg = 1; 

+ 1 - 0
User project/1.FrameLayer/Source/tbs.c

@@ -22,6 +22,7 @@ Revising History (ECL of this file):
 *************************************************************************/
 #include "user.h"
 #include "FSM_1st.h"
+#include "profiler.h"
 /************************************************************************
  Constant Table (N/A)
 *************************************************************************/

+ 11 - 22
User project/5.Api_rt/api_rt_pwm.c

@@ -6,7 +6,7 @@
 #include <stdint.h>
 #include "api_rt_adc.h"
 #include "classB.h"
-
+#include "profiler.h"
 ApiRtPwm_Handle Pwms[1];
 
 /* ========================================================================== */
@@ -74,11 +74,14 @@ void iRtPwm_UpdateIsr(uint8_t devIndex)
     {           
         if((TIMER_CTL0(base) & TIMER_CTL0_DIR) == 0) // When Counting Up
         { 
+            PROFILER_START(1);
             /* TBC Up interrupt */
-            iRtPwm_CountZeroIsr(devIndex);             
+            iRtPwm_CountZeroIsr(devIndex);   
+            PROFILER_END(1);
         }
         else
-        {               
+        {      
+            PROFILER_START(2);
             /* Reset Timer6 counts*/
             iTimer_Reset(HW_TBC_HALF_UPDATE_TIMER); 
             /* TBC Down interrupt */
@@ -97,9 +100,10 @@ void iRtPwm_UpdateIsr(uint8_t devIndex)
             /* Software trigger for regular sampling*/
             iAdc_Convert(0);                
             /* Compara value load */   
-            iRtPwm_ActivateCompareValues(0);             
+            iRtPwm_ActivateCompareValues(0);   
             timer_dma_enable(base,TIMER_DMA_UPD);
-            dma_channel_enable(DMA0,DMA_CH4);  
+            dma_channel_enable(DMA0,DMA_CH4);      
+            PROFILER_END(2);
         }
     } 
     /* Timer0 update interrupt flag clear */
@@ -137,7 +141,7 @@ void iRtPwm_SyncSamplingAdcIsr(uint8_t devIndex)
     }
     else
     {
-        //do nothing
+        //do noting
     }
 
 }
@@ -496,22 +500,7 @@ void iPwm_DisableOutput(uint8_t devIndex)
 
 int8_t iPwm_GetChannelEnableState(uint8_t devIndex, uint8_t channelIndex)
 {
-//    ASSERT_LESS(devIndex, 1);
-//    ////ASSERT_LESS(channelIndex, 4);
-//    
-//    uint32_t base = Pwms[devIndex].PwmBase;
-//    uint32_t value = 0;
-//
-//    if (channelIndex < 64)
-//    {
-//        value = ((uint32_t)0x01) << (channelIndex * 4);
-//    }
-//    else
-//    {
-//        value = ((uint32_t)0x04) << ((channelIndex - 64) * 4);
-//    }
-//    
-//    return ((TIMER_CHCTL2(base) & value) >> value);
+
 }
 
 void iPwm_EnableChannelOutput(uint8_t devIndex, uint8_t channelIndex)

+ 1 - 5
User project/5.Api_rt/api_rt_uart.c

@@ -107,15 +107,11 @@ int16_t iUart_Write(uint8_t devIndex, uint8_t *data, int16_t count)
         while (writeCount < count)
         {
             USART_DATA(Uarts[devIndex].UartBase) = data[writeCount];
+            writeCount++;
             while ((USART_STAT0(Uarts[devIndex].UartBase) & 0x80) == 0)
             {
                 ;
             }
-//            Uarts[devIndex].UartBase->TDR = data[writeCount];
-//            while ((Uarts[devIndex].UartBase->ISR & 0x80) == 0)
-//            {
-//                ;
-//            }
         }
     }
     return writeCount;

+ 183 - 0
User project/5.Api_rt/profiler.c

@@ -0,0 +1,183 @@
+#include "profiler.h"
+#include "profiler_port.h"
+#include <stdint.h>
+
+typedef struct
+{
+    uint8_t level;
+    uint32_t counter;
+} ProfilerEvent;
+
+uint32_t ProfilerDurationTicks;
+
+#if PROFILER_TYPE == PROFILER_TYPE_STATISTICS
+
+uint32_t ProfilerTime[PROFILER_MAX_LEVEL + 1];
+
+uint32_t ProfilerCounts[PROFILER_MAX_LEVEL + 1];
+
+uint8_t ProfilerStack[PROFILER_MAX_LEVEL + 1];
+
+uint8_t ProfilerSp;
+
+ProfilerEvent ProfilerBuffer;
+
+uint32_t ProfilerStartTicks = 0;
+
+#endif
+
+uint8_t ProfilerEnable = 0;
+
+uint8_t ProfilerRunning = 0;
+
+int8_t ProfilerError = 0;
+
+void profiler_init()
+{
+    ProfilerEnable = 0;
+    ProfilerError = 0;
+
+    ProfilerDurationTicks = PROFILER_CLOCK * (float)PROFILER_DURATION;
+
+#if PROFILER_TYPE == PROFILER_TYPE_STATISTICS
+    for (int i = 0; i < PROFILER_MAX_LEVEL; i++)
+    {
+        ProfilerTime[i] = 0;
+        ProfilerCounts[i] = 0;
+    }
+
+    ProfilerSp = 0;
+#elif PROFILER_TYPE == PROFILER_TYPE_TRACE
+
+#endif
+
+    profiler_init_hw();
+}
+
+void profiler_enable()
+{
+    ProfilerEnable = 1;
+
+    profiler_enable_hw();
+}
+
+void profiler_bg()
+{
+#if PROFILER_TYPE == PROFILER_TYPE_STATISTICS
+    if (!ProfilerEnable)
+    {
+        return;
+    }
+    else
+    {
+        PROFILER_DISABLE_IRQ();
+        uint32_t ticks = profiler_getTicks();
+        if (!ProfilerRunning)
+        {
+            /* If counter overflow, return directly */
+            if (ticks + 2 * ProfilerDurationTicks > ticks)
+            {
+                ProfilerSp = 0;
+                ProfilerStack[ProfilerSp] = 0;
+                ProfilerSp += 1;
+                ProfilerCounts[0] += 1;
+                ProfilerBuffer.counter = ticks;
+                ProfilerBuffer.level = 0;
+                ProfilerStartTicks = ticks;
+                ProfilerRunning = 1;
+            }
+        }
+        else
+        {
+            uint32_t ticks = profiler_getTicks();
+            int32_t delta = ticks - ProfilerBuffer.counter;
+            if(delta<0)
+            {
+                delta+=72000;
+            }
+            ProfilerTime[0] += delta;
+            ProfilerBuffer.counter = ticks;
+            ProfilerBuffer.level = 0;
+            if (ticks > ProfilerStartTicks + ProfilerDurationTicks)
+            {
+                ProfilerRunning = 0;
+                ProfilerEnable = 0;
+            }
+            else
+            {
+                ProfilerCounts[0] += 1;
+                ProfilerBuffer.counter = ticks;
+                ProfilerBuffer.level = 0;
+            }
+            if (ProfilerSp != 1)
+            {
+                ProfilerError = -1;
+            }
+        }
+        PROFILER_ENABLE_IRQ();
+    }
+#elif PROFILER_TYPE == PROFILER_TYPE_TRACE
+
+#endif
+}
+
+void  profiler_start(uint8_t lv)
+{
+#if PROFILER_TYPE == PROFILER_TYPE_STATISTICS
+    if (!ProfilerRunning)
+    {
+        return;
+    }
+    if (lv <= 0 || lv > PROFILER_MAX_LEVEL)
+    {
+        return;
+    }
+
+    PROFILER_DISABLE_IRQ();
+    uint32_t ticks = profiler_getTicks();
+    int32_t delta = ticks - ProfilerBuffer.counter;
+    if(delta<0)
+    {
+        delta+=72000;
+    }
+    ProfilerTime[ProfilerStack[ProfilerSp - 1]] += delta;
+    ProfilerCounts[lv] += 1;
+    ProfilerStack[ProfilerSp] = lv;
+    ProfilerSp += 1;
+    ProfilerBuffer.counter = ticks;
+    ProfilerBuffer.level = lv;
+    PROFILER_ENABLE_IRQ();
+
+#elif PROFILER_TYPE == PROFILER_TYPE_TRACE
+
+#endif
+}
+
+void profiler_end(uint8_t lv)
+{
+#if PROFILER_TYPE == PROFILER_TYPE_STATISTICS
+    if (!ProfilerRunning)
+    {
+        return;
+    }
+    if (lv <= 0 || lv > PROFILER_MAX_LEVEL)
+    {
+        return;
+    }
+
+    PROFILER_DISABLE_IRQ();
+    uint32_t ticks = profiler_getTicks();
+    int32_t delta = ticks - ProfilerBuffer.counter;
+    if(delta<0)
+    {
+        delta+=72000;
+    }
+    ProfilerTime[ProfilerStack[ProfilerSp - 1]] += delta;
+    ProfilerSp -= 1;
+    ProfilerBuffer.counter = ticks;
+    ProfilerBuffer.level = lv;
+    PROFILER_ENABLE_IRQ();
+#elif PROFILER_TYPE == PROFILER_TYPE_TRACE
+
+#endif
+}

+ 105 - 0
User project/5.Api_rt/profiler.h

@@ -0,0 +1,105 @@
+/**
+ * @file profiler.h
+ * @author Xiao Lifan (xiaolf6@midea.com)
+ * @brief 
+ * @version 0.1
+ * @date 2022-03-15
+ * 
+ * @copyright Copyright (c) 2022
+ * 
+ * @details Mcu profiling tool, only for none-RTOS platform
+ * 
+ */
+
+#ifndef _PROFILER_H_
+#define _PROFILER_H_
+
+#include <stdint.h>
+#include "profiler_port.h"
+
+/**
+ * @brief The clock of profiler counter
+ * 
+ */
+
+#define PROFILER_TYPE_TRACE         2
+#define PROFILER_TYPE_STATISTICS    1
+#define PROFILER_TYPE_NONE          0
+
+/**
+ * @brief Profile type
+ * 
+ */
+#define PROFILER_TYPE               PROFILER_TYPE_STATISTICS
+
+/**
+ * @brief Profile duration
+ * @note Profile will automaticlly stop after duration
+ * 
+ */
+#define PROFILER_DURATION           1
+
+/**
+ * @brief The maximum profiling event id
+ * 
+ */
+#define PROFILER_MAX_LEVEL          8
+
+/**
+ * @brief Profile background task macro, should be call in the main loop
+ * 
+ */
+#define PROFILER_BG()               profiler_bg()
+
+/**
+ * @brief Profile start macro
+ * 
+ */
+#define PROFILER_START(X)           profiler_start(X)  
+
+/**
+ * @brief profile end macro
+ * 
+ */
+#define PROFILER_END(X)             profiler_end(X)  
+
+/**
+ * @brief Enable profile
+ * @note The profile will actually start at next calling of PROFILER_BG().
+ * 
+ */
+void profiler_enable();
+
+/**
+ * @brief Initialize profile data structure
+ * 
+ */
+void profiler_init();
+
+/**
+ * @brief Profiling background task, should be call in the main loop
+ * 
+ */
+void profiler_bg();
+
+/**
+ * @brief Start profile of level lv, the delta ticks between "start" and "end" will be calculated 
+ * 
+ * @param lv profiling level
+ */
+void profiler_start(uint8_t lv);
+
+/**
+ * @brief End profile of level lv
+ * 
+ * @param lv profiling level
+ */
+void profiler_end(uint8_t lv);
+
+extern uint32_t ProfilerTime[PROFILER_MAX_LEVEL + 1];
+
+extern uint32_t ProfilerCounts[PROFILER_MAX_LEVEL + 1];
+
+extern int8_t ProfilerError;
+
+#endif

+ 125 - 0
User project/5.Api_rt/profiler_port.c

@@ -0,0 +1,125 @@
+#include "profiler_port.h"
+
+#include "gd32f30x.h"
+
+#ifdef x86
+
+uint32_t Win32_Ticks = 0;
+
+void profiler_server()
+{
+    Win32_Ticks += 1;
+}
+
+uint32_t profiler_getTicks()
+{
+    return Win32_Ticks;
+}
+
+void profiler_disable_irq()
+{
+
+}
+
+void profiler_enable_irq()
+{
+    
+}
+
+#endif
+
+#if defined __CORTEX_M && (__CORTEX_M == 4U)
+
+/**
+ * @brief Debug Exception and Monitor Control Register Base Address
+ *
+ */
+#define CORTEX_DEMCR (*(uint32_t *) 0xE000EDFC)
+
+/**
+ * @brief Data Watchpoint and Trace Register Control Register Base Address
+ *
+ */
+#define CORTEX_DWT_CTRL (*(uint32_t *) 0xE0001000)
+
+/**
+ * @brief Data Watchpoint and Trace Register Cycle Count Register Address
+ *
+ */
+#define CORTEX_DWT_CYCCNT (*(volatile uint32_t *) 0xE0001004)
+
+void profiler_init_hw()
+{
+//	CORTEX_DEMCR |= (1 << 24);
+//	CORTEX_DWT_CTRL |= (1 << 0);
+//	CORTEX_DWT_CYCCNT = 0;
+}
+
+void profiler_enable_hw()
+{
+//	CORTEX_DWT_CYCCNT = 0;
+}
+
+uint32_t profilerServrCnt=0;
+void profiler_server()
+{
+    profilerServrCnt++;
+}
+
+uint32_t profiler_getTicks()
+{
+//	return CORTEX_DWT_CYCCNT;
+   return (((72000-SysTick->VAL) * 14) >> 10)+ profilerServrCnt*1000;
+  
+}
+
+void profiler_disable_irq()
+{
+	__disable_irq();
+}
+
+void profiler_enable_irq()
+{
+	__enable_irq();
+}
+
+#endif
+
+
+#if defined __CORTEX_M && (__CORTEX_M == 3U)
+
+
+
+void profiler_init_hw()
+{
+
+}
+
+void profiler_enable_hw()
+{
+}
+
+uint32_t profilerServrCnt=0;
+
+void profiler_server()
+{
+    profilerServrCnt++;
+
+}
+
+uint32_t profiler_getTicks()
+{
+    return (((72000-SysTick->VAL) * 14) >> 10)+ profilerServrCnt*1000;
+}
+
+void profiler_disable_irq()
+{
+	__disable_irq();
+}
+
+void profiler_enable_irq()
+{
+	__enable_irq();
+}
+
+#endif

+ 24 - 0
User project/5.Api_rt/profiler_port.h

@@ -0,0 +1,24 @@
+#ifndef _PROFILER_PORT_H_
+#define _PROFILER_PORT_H_       
+
+#include <stdint.h>
+
+#define PROFILER_CLOCK              1000000UL
+
+#define PROFILER_DISABLE_IRQ()      profiler_disable_irq()
+
+#define PROFILER_ENABLE_IRQ()       profiler_enable_irq()
+
+void profiler_init_hw();
+
+void profiler_enable_hw();
+
+void profiler_server();
+
+uint32_t profiler_getTicks();
+
+void profiler_disable_irq();
+
+void profiler_enable_irq();
+
+#endif

+ 12 - 0
WLMCP_PACKED.ewp

@@ -2910,6 +2910,18 @@
             <file>
                 <name>$PROJ_DIR$\User project\5.Api_rt\api_rt_uart.h</name>
             </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\profiler.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\profiler.h</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\profiler_port.c</name>
+            </file>
+            <file>
+                <name>$PROJ_DIR$\User project\5.Api_rt\profiler_port.h</name>
+            </file>
         </group>
     </group>
 </project>