소스 검색

add new module get temperature from stm32

balbekova 1 년 전
부모
커밋
44eb379147
4개의 변경된 파일128개의 추가작업 그리고 0개의 파일을 삭제
  1. 2 0
      modules/Makefile
  2. 6 0
      modules/MegaTec/megatec.h
  3. 103 0
      modules/stm32_temperature/stm32_temperature.c
  4. 17 0
      modules/stm32_temperature/stm32_temperature.h

+ 2 - 0
modules/Makefile

@@ -59,6 +59,7 @@ ifneq (,$(filter $(HARDWARE),bt6703 bt6703_rt bt6711 bt6711_v1))
 INCLUDES += -Iradius
 endif
 INCLUDES += -Imbedtls_api
+INCLUDES += -Istm32_temperature
 CSRC += $(wildcard leds/*.c)
 CSRC += $(wildcard buttons/*.c)
 CSRC += $(wildcard jumper/*.c)
@@ -80,6 +81,7 @@ ifneq (,$(filter $(HARDWARE),bt6703 bt6703_rt bt6711 bt6711_v1))
 CSRC += $(wildcard radius/*.c)
 endif
 CSRC += $(wildcard mbedtls_api/*.c)
+CSRC += $(wildcard stm32_temperature/*.c)
 
 CFLAGS += -DOS_FREERTOS
 

+ 6 - 0
modules/MegaTec/megatec.h

@@ -58,6 +58,7 @@ typedef enum{
 	ups_kstar_status,
 	ups_temperature,
 	ups_fault_status,
+	ups_remote_turn_off,
 
 	MegaTec_cmd_max
 } cmdMegaTecEnums_t;
@@ -80,6 +81,7 @@ typedef struct{
 	uint8_t Load;
 	uint8_t Load_test_akb;
 	uint8_t SOC;
+	uint8_t SOC_prev;
 	uint16_t work_time;
 	uint32_t akb_work_time;
 	uint32_t Alarm;
@@ -90,6 +92,8 @@ typedef struct{
 	char model[16];
 	char vertion[22];
 	char serial[15];
+	uint8_t fault_type;
+	uint16_t temp_stm32;
 	uint8_t cnt_err_ups;
 	ups_state_connection_t Present;
 	bool Flag_Present;
@@ -105,6 +109,8 @@ int ups_metac_service_pdu(cmdMegaTecEnums_t command);
 
 void ups_megatec_init(void);
 
+void UPScmd(cmdMegaTecEnums_t cmd);
+
 void UPSReadTestStatus(void);
 
 float voltage_bat_average(void);

+ 103 - 0
modules/stm32_temperature/stm32_temperature.c

@@ -0,0 +1,103 @@
+/*
+ * stm32_temperature.c
+ *
+ *  Created on: 07.11.2023
+ *      Author: balbekova
+ */
+
+#include "common_config.h"
+
+#include "FreeRTOS.h"
+#include "task.h"
+
+#include <stm32f4xx.h>
+#include <stm32f4xx_rcc.h>
+#include <stm32f4xx_adc.h>
+
+#include "megatec.h"
+
+#ifdef PRINTF_STDLIB
+#include <stdio.h>
+#endif
+#ifdef PRINTF_CUSTOM
+#include "tinystdio.h"
+#endif
+#include <string.h>
+#include <stdlib.h>
+
+
+#define DBG if (0)
+
+
+#define TEMPERATURE_SHIFT           15
+
+
+void adc_init() {
+       ADC_InitTypeDef ADC_InitStruct;
+       ADC_CommonInitTypeDef ADC_CommonInitStruct;
+
+       /* сбрасываем настройки АЦП */
+       ADC_DeInit();
+
+       RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
+
+
+
+       ADC_CommonInitStruct.ADC_Mode = ADC_Mode_Independent;
+        ADC_CommonInitStruct.ADC_Prescaler = ADC_Prescaler_Div8;
+        ADC_CommonInitStruct.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
+        ADC_CommonInitStruct.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
+        ADC_CommonInit(&ADC_CommonInitStruct);
+
+        ADC_InitStruct.ADC_Resolution = ADC_Resolution_12b;
+        ADC_InitStruct.ADC_ScanConvMode = DISABLE;
+        ADC_InitStruct.ADC_ContinuousConvMode = ENABLE;
+        ADC_InitStruct.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
+        ADC_InitStruct.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
+        ADC_InitStruct.ADC_DataAlign = ADC_DataAlign_Right;
+        ADC_InitStruct.ADC_NbrOfConversion = 1;
+        ADC_Init(ADC1, &ADC_InitStruct);
+
+        ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_144Cycles);
+
+        ADC_TempSensorVrefintCmd(ENABLE);
+        ADC_Cmd(ADC1, ENABLE);
+
+
+}
+
+
+u16 readADC1(u8 channel){
+   // начинаем работу
+   ADC_SoftwareStartConv(ADC1);
+   // ждём пока преобразуется напряжение в код
+   while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
+   // возвращаем результат
+   return ADC_GetConversionValue(ADC1);
+}
+
+
+//void stm32_temperature_meas(void)
+void stm32_temperature_meas(void *params)
+{
+    float adc_temperature;
+    for (;;) {
+        adc_temperature = readADC1(16);
+        adc_temperature *= 3300;
+        adc_temperature /= 0xfff; //Reading in mV
+        adc_temperature /= 1000.0; //Reading in Volts
+        adc_temperature -= 0.760; // Subtract the reference voltage at 25°C
+        adc_temperature /= .0025; // Divide by slope 2.5mV
+
+        adc_temperature += 25.0; // Add the 25°C
+       UPS.temp_stm32 = adc_temperature - TEMPERATURE_SHIFT;
+       printf("stm32_temperature %d\r\n", UPS.temp_stm32);
+        vTaskDelay(5000);
+    }
+}
+
+void stm32_temperature_init(void)
+{
+    adc_init();
+    xTaskCreate(stm32_temperature_meas, ( char * ) "stm32_temperature_meas", configMINIMAL_STACK_SIZE * 2, NULL, tskIDLE_PRIORITY, NULL);
+}

+ 17 - 0
modules/stm32_temperature/stm32_temperature.h

@@ -0,0 +1,17 @@
+/*
+ * stm32_temperature.h
+ *
+ *  Created on: 07.11.2023
+ *      Author: balbekova
+ */
+
+#ifndef STM32_TEMPERATURE_H_
+#define STM32_TEMPERATURE_H_
+
+#ifdef STM32_TEMPERATURE_H_
+
+void stm32_temperature_init(void);
+
+#endif
+
+#endif /* STM32_TEMPERATURE_H_ */