ADC采集DMA数据地址空间优化
authorkyz <kyz@kyz.pub>
Tue, 2 Apr 2024 08:32:22 +0000 (16:32 +0800)
committerkyz <kyz@kyz.pub>
Tue, 2 Apr 2024 08:32:22 +0000 (16:32 +0800)
user/user_adc.c

index ba51773a95d5b0939dd67421e1ea6df86133ff3a..050ecd4fea8720f1d442d4ffa90e7b4a689be024 100644 (file)
@@ -6,16 +6,21 @@ void user_adc_task( void* unused );
 //ADC3 INP2温度                10毫秒采集一次,100毫秒DMA中断触发,取平均计算。
 //ADC2 INP18 电流,           100微秒采集一次,500毫秒触发DMA中断,计算电流。
 
-ALIGN_32BYTES (static uint16_t adc1_raw[10*2] ); //2路采集10次, 0.1秒
-ALIGN_32BYTES (static uint16_t adc2_raw[5000] ); //采集5000次,电流采样率10Khz 0.5秒
-ALIGN_32BYTES (static uint16_t adc3_raw[100] );   //采集100次  1秒
+
+#define ADC1_RAW_NRS (10*2)
+#define ADC2_RAW_NRS 5000
+#define ADC3_RAW_NRS 100
+//DMA采集buffer 地址32字节对齐,大小为32字节倍数,以适配cache刷新操作
+ALIGN_32BYTES (static uint16_t adc1_raw[(ADC1_RAW_NRS+15)&~15] ); //2路采集10次, 0.1秒
+ALIGN_32BYTES (static uint16_t adc2_raw[(ADC2_RAW_NRS+15)&~15] ); //采集5000次,电流采样率10Khz 0.5秒
+ALIGN_32BYTES (static uint16_t adc3_raw[(ADC3_RAW_NRS+15)&~15] ); //采集100次  1秒
 
 #define SECTION_DMA_RAM 
 #define  ARRAY_SIZE( arr )   ( sizeof( arr )/sizeof( arr[0] ) ) 
 
-static uint16_t adc1_raw_copy[10*2];
-static uint16_t adc3_raw_copy[100];
-static float adc2_curr[5000];
+static uint16_t adc1_raw_copy[ADC1_RAW_NRS];
+static uint16_t adc3_raw_copy[ADC3_RAW_NRS];
+static float adc2_curr[ADC2_RAW_NRS];
 
 extern dev_info_t g_dev;
 
@@ -23,17 +28,17 @@ void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
 {
     if( hadc == &hadc1 ){
         SCB_InvalidateDCache_by_Addr((uint32_t *)&adc1_raw[0], sizeof( adc1_raw) );
-        memcpy( &adc1_raw_copy[0], &adc1_raw[0], sizeof( adc1_raw) );
+        memcpy( &adc1_raw_copy[0], &adc1_raw[0], sizeof( adc1_raw_copy) );
         osEventFlagsSet( g_dev.ev_group, EVENT_ADC1_UPDATE );
     }else if( hadc == &hadc2 ){
         SCB_InvalidateDCache_by_Addr((uint32_t *)&adc2_raw[0], sizeof( adc2_raw) );
 
-        for( int32_t i=0; i< ARRAY_SIZE( adc2_raw); i++ )
+        for( int32_t i=0; i< ADC2_RAW_NRS; i++ )
             adc2_curr[i] = (adc2_raw[i] * VREF*2/0xffff - 2.5f)/0.625f*400;
         osEventFlagsSet( g_dev.ev_group, EVENT_ADC2_UPDATE );
     }else if( hadc == &hadc3 ){
         SCB_InvalidateDCache_by_Addr((uint32_t *)&adc3_raw[0], sizeof( adc3_raw) );
-        memcpy( &adc3_raw_copy[0], &adc3_raw[0], sizeof( adc3_raw) );
+        memcpy( &adc3_raw_copy[0], &adc3_raw[0], sizeof( adc3_raw_copy) );
         osEventFlagsSet( g_dev.ev_group, EVENT_ADC3_UPDATE );
     }
 }
@@ -204,23 +209,23 @@ void user_adc_task( void* unused )
              t1 = HAL_GetTick() - t0;
              t0 = HAL_GetTick();
              
-            if( HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], ARRAY_SIZE(adc1_raw) ) != HAL_OK){
+            if( HAL_ADC_Start_DMA( &hadc1, (uint32_t*)&adc1_raw[0], ADC1_RAW_NRS ) != HAL_OK){
                 DBG_PRINTF( "start DMA fail\r\n" );
             }
 
             pressure_sum = 0;
             flowrate_sum = 0;
 
-            for( int i=0; i<ARRAY_SIZE(adc1_raw)/2; i++ ){
-                pressure_sum += adc1_raw[ i*2 ];
-                flowrate_sum += adc1_raw[ i*2+1 ];
+            for( int i=0; i<ADC1_RAW_NRS/2; i++ ){
+                pressure_sum += adc1_raw_copy[ i*2 ];
+                flowrate_sum += adc1_raw_copy[ i*2+1 ];
             }
             
-            raw = pressure_sum/(ARRAY_SIZE( adc1_raw)/2);
+            raw = pressure_sum/(ADC1_RAW_NRS/2);
             voltage = raw*VREF/0xffff; //16bit adc1
             voltage *=2; //压力计输出电压 分压 1/2
             g_dev.pressure = vol_to_kpa( voltage );
-            g_dev.flowmeter.flow_speed_raw_ad = vol_to_speed( flowrate_sum/(ARRAY_SIZE(adc1_raw)/2) *VREF/0xffff *2 );
+            g_dev.flowmeter.flow_speed_raw_ad = vol_to_speed( flowrate_sum/(ADC1_RAW_NRS/2) *VREF/0xffff *2 );
 
             if( voltage < 0.2f ){
                 //传感器输出0.2V到4.7v,未通气时按一个大气压101KPa大约0.2 + 101*0.0064 = 0.85V
@@ -243,13 +248,13 @@ void user_adc_task( void* unused )
              t2 = HAL_GetTick();
 
             //电流互感器 ADC2
-            HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], ARRAY_SIZE(adc2_raw) );
+            HAL_ADC_Start_DMA( &hadc2, (uint32_t*)&adc2_raw[0], ADC2_RAW_NRS );
             if( g_dev.debug_report == 2 ){   //发送调试数据到pc,
                 extern uint32_t dbg_report_raw_current;
                 extern uint8_t CDC_Transmit_HS(uint8_t* Buf, uint16_t Len);
-                static  uint8_t dbg_curr_raw[ARRAY_SIZE(adc2_curr)*2+8] = {0x5A, 0xA5, (sizeof(dbg_curr_raw)>>8)&0xff, sizeof(dbg_curr_raw)&0xff, 0x00, 0x24, };
+                static  uint8_t dbg_curr_raw[ADC2_RAW_NRS*2+8] = {0x5A, 0xA5, (sizeof(dbg_curr_raw)>>8)&0xff, sizeof(dbg_curr_raw)&0xff, 0x00, 0x24, };
                 
-                    for( int i=0; i < ARRAY_SIZE(adc2_curr); i++ ){
+                    for( int i=0; i < ADC2_RAW_NRS; i++ ){
                          int16_t curr = adc2_curr[i];
                          dbg_curr_raw[6+i*2+1] = curr&0xff; 
                          dbg_curr_raw[6+i*2] = (curr>>8)&0xff;
@@ -266,7 +271,7 @@ void user_adc_task( void* unused )
             }else{
 //                static uint32_t t10, t11;
 //                t10 = HAL_GetTick();
-                g_dev.welding_current = calc_welding_current( adc2_curr, ARRAY_SIZE(adc2_curr));
+                g_dev.welding_current = calc_welding_current( adc2_curr, ADC2_RAW_NRS);
 //                t11 = HAL_GetTick() - t10;
             }