stm32l431_xl_current/Core/Src/main.c

502 lines
12 KiB
C
Raw Normal View History

2024-07-15 01:27:36 +00:00
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* Copyright (c) 2023 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dac.h"
#include "dma.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "arm_math.h"
2024-07-15 09:07:44 +00:00
#include "uart_dev.h"
#include "frt_protocol.h"
2024-07-23 07:38:51 +00:00
#include "e22.h"
#include "inflash.h"
2024-07-15 01:27:36 +00:00
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
#define DATA_LEN 2048
2024-07-15 09:07:44 +00:00
uint32_t adc_value[DATA_LEN*2];
2024-07-15 01:27:36 +00:00
uint32_t result_data[DATA_LEN];
2024-07-15 09:07:44 +00:00
uint32_t adc_bat_data[DATA_LEN];
2024-07-15 01:27:36 +00:00
//float32_t rms_buf[DATA_LEN];
2024-07-15 09:07:44 +00:00
uint16_t Pulse_Count = 0; //脉冲次数
float32_t bat_v = 0; //电池电压
2024-07-15 01:27:36 +00:00
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
arm_rfft_fast_instance_f32 S;
float32_t fft_rawdata[DATA_LEN];
float32_t fft_outdata[DATA_LEN];
float32_t fft_mag[DATA_LEN>>1];
float32_t window_data[DATA_LEN];
float32_t Vdc= 0;
float32_t V50hz_mA=0;
//float32_t offset= 0;
float32_t max_val,main_freq;
float32_t rms_mV,rms_mA,Vpp;
2024-07-24 02:31:30 +00:00
float32_t max_rms_mA;
2024-07-15 01:27:36 +00:00
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
2024-07-15 09:07:44 +00:00
void bat_v_get(void)
{
float32_t temp_sum = 0;
float32_t temp_f = 0;
for(int i = 0; i < DATA_LEN; i++)
{
temp_sum += adc_bat_data[i];
}
temp_f = temp_sum/DATA_LEN;
bat_v = ((temp_f/4096.0)*3.3)*570.0/100.0;
2024-07-24 02:31:30 +00:00
//存入寄存器
g_stMcs_Para.bat_v = bat_v;
2024-07-15 09:07:44 +00:00
}
2024-07-15 01:27:36 +00:00
void arm_hanning_f32(float32_t * pDst,uint32_t blockSize)
{
float32_t k = 2.0f / ((float32_t) blockSize);
float32_t w;
for(uint32_t i=0;i<blockSize;i++)
{
w = PI * i * k;
w = 0.5f * (1.0f - cosf (w));
pDst[i] = w;
}
}
void Cur_sensor_pwr_on(void)
{
HAL_GPIO_WritePin(GPIOC, SENSOR_PWR_CTRL_Pin, GPIO_PIN_SET);
HAL_Delay(1);
// init dac
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048);
// 延时等待模拟部分启动稳定。具体延时等待测试待测试。
HAL_Delay(800);
}
void Cur_sensor_pwr_off(void)
{
// init dac
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0);
//HAL_DAC_Stop(&hdac1,DAC_CHANNEL_1);
//HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048);
//HAL_Delay(1);
HAL_GPIO_WritePin(GPIOC, SENSOR_PWR_CTRL_Pin, GPIO_PIN_RESET);
}
void HAL_ADC_ConvCpltCallback(ADC_HandleTypeDef* hadc)
{
2024-07-15 09:07:44 +00:00
for(int i = 0;i < DATA_LEN;i++)
{
result_data[i] = adc_value[2*i];
}
2024-07-15 01:27:36 +00:00
2024-07-15 09:07:44 +00:00
for(int i = 0;i < DATA_LEN;i++)
{
adc_bat_data[i] = adc_value[2*i + 1];
}
2024-07-15 01:27:36 +00:00
uint32_t i;
// 关闭模拟电路电源
//Cur_sensor_pwr_off();
//float32_t max_val,main_freq;
uint32_t max_val_freq;
for(i = 0;i<DATA_LEN;i++)
{
// 加窗
fft_rawdata[i]= (float32_t)result_data[i]*window_data[i];
}
//arm_rms_f32(fft_rawdata,DATA_LEN,&rms_mV);
//arm_q15_to_float(result_data,fft_rawdata,DATA_LEN);
arm_rfft_fast_f32(&S,fft_rawdata,fft_outdata,0);
arm_cmplx_mag_f32(fft_outdata,fft_mag,DATA_LEN>>1);
arm_scale_f32(fft_mag,4.0f/DATA_LEN,fft_mag,DATA_LEN>>1);
Vdc = fft_mag[0]/2.0f;
arm_max_f32(&fft_mag[5],(DATA_LEN>>2)-1,&max_val,&max_val_freq);
main_freq = ((float32_t)max_val_freq+5.0)/DATA_LEN*2000.0;
// 单独50Hz频率信号的有效值 无需归零校准
V50hz_mA = fft_mag[51]/4096.0*3300.0f*0.0380/1.414;
// 信号频率为50Hz左右
// if(main_freq<75.0f && main_freq>35.0f)
// {
for(i = 0;i<DATA_LEN;i++)
{
fft_rawdata[i]= result_data[i]-Vdc;
}
// 去除直流信号后标准方式计算有效值此数据容易受噪声影响有底噪RMS?
arm_rms_f32(fft_rawdata,DATA_LEN,&rms_mV);
rms_mV = rms_mV/4096.0*3300.0f;
// 300匝 交流互感 0.0380
// 350匝 交流互感 0.04412
rms_mA = rms_mV*0.0380;
// }else
// {
// //rms_mV = 0;
// /// rms_mA = 0;
// }
2024-07-24 02:31:30 +00:00
// 最大泄漏电流
max_rms_mA = max_rms_mA > rms_mA ? max_rms_mA : rms_mA;
2024-07-15 09:07:44 +00:00
// 获取脉冲次数
if(rms_mA > 10.0)
{
Pulse_Count++;
}
2024-07-24 02:31:30 +00:00
//电池电压获取
2024-07-15 09:07:44 +00:00
bat_v_get();
2024-07-24 02:31:30 +00:00
//存入寄存器
g_stMcs_Para.current = rms_mA;
g_stMcs_Para.max_current = max_rms_mA;
g_stMcs_Para.pulse_count = Pulse_Count;
2024-07-15 01:27:36 +00:00
}
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int RF_AUX_RISE = 1;
2024-07-24 02:31:30 +00:00
int RF_AUX_DOWN = 0;
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_5)
{
// 上升
if (HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_5) == GPIO_PIN_SET)
{
if(RF_AUX_RISE)
{
RF_AUX_RISE = 0;//处理数据时不进中断了
read_and_process_uart_data(g_lora_uart_handle);
RF_AUX_RISE = 1;//处理完再进中断
}
2024-07-24 02:31:30 +00:00
}
// 下降
else
{
RF_AUX_DOWN = 1;
}
// HAL_GPIO_EXTI_ClearIT(GPIO_PIN_5);
}
}
void enter_sleep()
{
//关闭时钟
HAL_SuspendTick();//关闭系统systick中断防止睡眠被systick中断打断
__HAL_RCC_GPIOA_CLK_DISABLE();
__HAL_RCC_GPIOC_CLK_DISABLE();
}
void out_sleep()
{
//启动时钟
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
HAL_ResumeTick();//打开系统systic中断
}
2024-07-15 01:27:36 +00:00
/* USER CODE END 0 */
2024-09-18 01:56:28 +00:00
// 100ms触发一次
// 1天重启一次
int8_t time_100ms = 0;
int8_t time_1s = 0;
int8_t time_1m = 0;
int8_t time_1h = 0;
int8_t time_1d = 0;
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
UNUSED(htim);
if(htim == &htim7)
{
time_100ms ++;
if(time_100ms >=10)
{
time_100ms = 0;
time_1s ++;
}
if(time_1s >=60)
{
time_1s = 0;
time_1m ++;
}
if(time_1m >=60)
{
time_1m = 0;
time_1h ++;
}
if(time_1h >=24)
{
time_1h = 0;
time_1d ++;
}
if(time_1d >=1)
{
time_1d = 0;
__iar_builtin_set_FAULTMASK(1);
NVIC_SystemReset();
}
}
}
2024-07-15 01:27:36 +00:00
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
arm_rfft_fast_init_f32(&S, DATA_LEN);
arm_hanning_f32(window_data,DATA_LEN);
/* USER CODE END SysInit */
2024-08-30 01:17:47 +00:00
2024-07-15 01:27:36 +00:00
/* Initialize all configured peripherals */
2024-07-23 02:00:15 +00:00
HAL_GPIO_WritePin(RF_PWR_CTRL_GPIO_Port, RF_PWR_CTRL_Pin, GPIO_PIN_SET);//使能e22
2024-07-15 01:27:36 +00:00
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_TIM6_Init();
2024-09-18 01:56:28 +00:00
MX_TIM7_Init();
2024-07-15 01:27:36 +00:00
MX_DAC1_Init();
2024-07-23 07:38:51 +00:00
// 串口初始化
init_lora_uart();
2024-07-31 02:06:50 +00:00
// init_term_uart();
2024-07-15 01:27:36 +00:00
/* USER CODE BEGIN 2 */
// HAL_PWREx_EnterSTOP0Mode(PWR_STOPENTRY_WFI);
HAL_PWREx_EnableBatteryCharging(PWR_BATTERY_CHARGING_RESISTOR_1_5);
HAL_ADCEx_Calibration_Start(&hadc1,ADC_SINGLE_ENDED);
// 模拟电路电源没开之前 dac输出0 保证不会串电流
HAL_DAC_Start(&hdac1,DAC_CHANNEL_1);
// 开启缓冲后 设置为0 实测有40mV左右电压 猜测和缓冲器Vos有关
HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,0);
// 开启模拟电路电源,抬参考源
Cur_sensor_pwr_on();
2024-07-15 09:07:44 +00:00
HAL_ADC_Start_DMA(&hadc1,(uint32_t *)adc_value,DATA_LEN * 2);
2024-07-15 01:27:36 +00:00
HAL_TIM_Base_Start(&htim6);
//开启定时器7中断
HAL_TIM_Base_Start_IT(&htim7);
HAL_TIM_Base_Start(&htim7);
// 读配置
read_config_info();
// term_printf("Current Sensor.\r\n");
// term_printf("Version 1.0.0 Build: %s %s\r\n",__DATE__,__TIME__);
2024-07-23 07:38:51 +00:00
// 初始化E22
e22_init();
2024-07-15 01:27:36 +00:00
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
2024-07-23 07:38:51 +00:00
2024-07-15 01:27:36 +00:00
/* USER CODE BEGIN 3 */
// HAL_DAC_SetValue(&hdac1,DAC_CHANNEL_1,DAC_ALIGN_12B_R,2048);
//HAL_ADC_Start_DMA(&hadc1,(uint32_t *)result_data,DATA_LEN);
// HAL_GPIO_TogglePin(GPIO_LED_GPIO_Port, GPIO_LED_Pin);
2024-08-21 08:53:45 +00:00
HAL_PWR_EnterSLEEPMode(PWR_MAINREGULATOR_ON,PWR_SLEEPENTRY_WFI);
2024-07-31 02:06:50 +00:00
2024-07-15 01:27:36 +00:00
// HAL_TIM_Base_Start(&htim6);
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
if (HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2) != HAL_OK)
{
Error_Handler();
}
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSE;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
HAL_StatusTypeDef HAL_InitTick(uint32_t TickPriority)
{
HAL_StatusTypeDef status = HAL_OK;
/* Check uwTickFreq for MisraC 2012 (even if uwTickFreq is a enum type that doesn't take the value zero)*/
if ((uint32_t)uwTickFreq != 0U)
{
/*Configure the SysTick to have interrupt in 1ms time basis*/
if (HAL_SYSTICK_Config(SystemCoreClock / (1000U / (uint32_t)uwTickFreq)) == 0U)
{
/* Configure the SysTick IRQ priority */
if (TickPriority < (1UL << __NVIC_PRIO_BITS))
{
HAL_NVIC_SetPriority(SysTick_IRQn, 13, 0U);
uwTickPrio = TickPriority;
}
else
{
status = HAL_ERROR;
}
}
else
{
status = HAL_ERROR;
}
}
else
{
status = HAL_ERROR;
}
/* Return function status */
return status;
}