From a01fb28290559a7216c11924a93d14312349ffe2 Mon Sep 17 00:00:00 2001 From: kyz Date: Tue, 2 Apr 2024 16:32:22 +0800 Subject: [PATCH] =?utf8?q?ADC=E9=87=87=E9=9B=86DMA=E6=95=B0=E6=8D=AE?= =?utf8?q?=E5=9C=B0=E5=9D=80=E7=A9=BA=E9=97=B4=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- user/user_adc.c | 43 ++++++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/user/user_adc.c b/user/user_adc.c index ba51773..050ecd4 100644 --- a/user/user_adc.c +++ b/user/user_adc.c @@ -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>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; } -- 2.34.1