Ver código fonte

文件上库

yidongFan 2 semanas atrás
pai
commit
037f937b38

+ 53 - 0
Core/Inc/iwdg.h

@@ -0,0 +1,53 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file    iwdg.h
+  * @brief   This file contains all the function prototypes for
+  *          the iwdg.c file
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2026 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 */
+/* Define to prevent recursive inclusion -------------------------------------*/
+#ifndef __IWDG_H__
+#define __IWDG_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/* Includes ------------------------------------------------------------------*/
+#include "main.h"
+
+/* USER CODE BEGIN Includes */
+
+/* USER CODE END Includes */
+
+extern IWDG_HandleTypeDef hiwdg;
+
+/* USER CODE BEGIN Private defines */
+
+/* USER CODE END Private defines */
+
+void MX_IWDG_Init(void);
+
+/* USER CODE BEGIN Prototypes */
+void Refresh_IWDG(void);
+unsigned int CheckIsIwdgReset();
+/* USER CODE END Prototypes */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __IWDG_H__ */
+

+ 75 - 0
Core/Src/iwdg.c

@@ -0,0 +1,75 @@
+/* USER CODE BEGIN Header */
+/**
+  ******************************************************************************
+  * @file    iwdg.c
+  * @brief   This file provides code for the configuration
+  *          of the IWDG instances.
+  ******************************************************************************
+  * @attention
+  *
+  * Copyright (c) 2026 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 "iwdg.h"
+
+/* USER CODE BEGIN 0 */
+
+/* USER CODE END 0 */
+
+IWDG_HandleTypeDef hiwdg;
+
+/* IWDG init function */
+void MX_IWDG_Init(void)
+{
+
+  /* USER CODE BEGIN IWDG_Init 0 */
+#if defined(MINI7_V2_DEBUG)
+	return ;	
+#endif
+  /* USER CODE END IWDG_Init 0 */
+
+  /* USER CODE BEGIN IWDG_Init 1 */
+
+  /* USER CODE END IWDG_Init 1 */
+  hiwdg.Instance = IWDG;
+  hiwdg.Init.Prescaler = IWDG_PRESCALER_64;
+  hiwdg.Init.Reload = 1230;
+  if (HAL_IWDG_Init(&hiwdg) != HAL_OK)
+  {
+    Error_Handler();
+  }
+  /* USER CODE BEGIN IWDG_Init 2 */
+
+  /* USER CODE END IWDG_Init 2 */
+
+}
+
+/* USER CODE BEGIN 1 */
+
+void Refresh_IWDG(void){
+
+#if defined(MINI7_V2_DEBUG)
+	return ;	
+#endif
+  HAL_IWDG_Refresh(&hiwdg);
+}
+
+
+unsigned int  CheckIsIwdgReset()
+{
+	if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST)){
+		__HAL_RCC_CLEAR_RESET_FLAGS();
+		return 1;
+	}
+	return 0;	
+}
+/* USER CODE END 1 */
+

+ 282 - 0
DriverInterface/van_BSP_Temp_mts01.c

@@ -0,0 +1,282 @@
+/******************************************************************************
+*Copyright(C),2024-2024,万众一芯生
+*van_BSP_Temp_mts01.c
+*mts01 温度相关驱动接口
+*修改历史
+author				范义东			新建				2024/01/27
+******************************************************************************/
+
+
+#include "onechip_include_header.h"
+
+
+
+
+#define MTS_IIC_ERR_TEMP (float)-60 
+#define MTS_MAX_ERR_NUMBER 5
+#define MTS_REINIT_TIME 5*MTS_MAX_ERR_NUMBER
+#define MTS_RETRY_TIME 20000*MTS_MAX_ERR_NUMBER
+VAN_TEMP_MTS01_CON_ST_Def gTemp_mts01_demo_V1[] = {
+    {TEMP_MTS01NAMEANDID(TEMP_BLOCK), &hi2c1, MTS01_ADDR0, TEMP_MTS01_MAX_AVG_TIME, 0},
+};
+
+const unsigned int gTemp_mts01_demo_V1_Num = ARR_LEN(gTemp_mts01_demo_V1);
+
+P_VAN_TEMP_MTS01_CON_ST_Def gTemp_mts01;
+unsigned int gTemp_mts01Num;
+
+VAN_TEMP_CHANNEl_CON_ST_Def gTemp_channel_demo_V1[] = {
+    {EnmFuntionIdType_Idhost, 1 << TEMP_BLOCK},
+};
+
+const unsigned int gTemp_channel_demo_V1_Num = ARR_LEN(gTemp_channel_demo_V1);
+
+static float mst01_samp[ARR_LEN(gTemp_mts01_demo_V1)] = {0};  //传感器采集数据
+static unsigned short last_valve[ARR_LEN(gTemp_mts01_demo_V1)] = {0};
+static uint32_t count[ARR_LEN(gTemp_mts01_demo_V1)] = {0};
+static uint32_t errorConter[ARR_LEN(gTemp_mts01_demo_V1)] = {0};
+
+
+
+uint32_t printMts_temp = 0;
+extern void DelayMS(uint32_t ms);
+
+unsigned int  MTS01_Init(P_VAN_TEMP_MTS01_CON_ST_Def pmts01);
+
+/****************************************************************
+ 函数名:    loadPidCfg
+ 创建时间:  2024/01/08
+ 创建人:    范义东
+ 函数说明:加载配置文件
+ 参数:
+
+ 返回值 :  加载成功
+****************************************************************/
+unsigned int loadTemp_mts01Cfg(void) {
+  unsigned int res;
+  gTemp_mts01 = gTemp_mts01_demo_V1;
+  gTemp_mts01Num = gTemp_mts01_demo_V1_Num;
+  P_VAN_TEMP_MTS01_CON_ST_Def pmts01 = gTemp_mts01;
+  for (int i = 0; i < gTemp_mts01Num; i++) {
+    pmts01->header = 0;
+    memset(pmts01->temp, 0, sizeof(pmts01->temp));
+   res = MTS01_Init(pmts01);
+    pmts01++;
+  }
+  return res;
+}
+
+unsigned int MY_CRC8(unsigned char data[], unsigned char nbrOfBytes) {
+  unsigned char bit;         // Bit mask
+  unsigned char crc = 0xFF;  // Calculated checksum
+  unsigned char byteCtr;     // Byte counter
+
+  // Calculates 8-Bit checksum with given polynomial
+  for (byteCtr = 0; byteCtr < nbrOfBytes; byteCtr++) {
+    crc ^= (data[byteCtr]);
+    for (bit = 8; bit > 0; --bit) {
+      if (crc & 0x80)
+        crc = (crc << 1) ^ POLYNOMIAL;
+      else
+        crc = (crc << 1);
+    }
+  }
+
+  return crc;
+}
+
+/****************************************************************
+ 函数名:   MTS01_Init
+ 创建时间:  2024/01/08
+ 创建人:    范义东
+ 函数说明:模块初始化
+ 参数:
+
+ 返回值 :  加载成功
+****************************************************************/
+unsigned int MTS01_Reg_Write(P_VAN_TEMP_MTS01_CON_ST_Def pmts01, unsigned int cmd, unsigned char *pData, unsigned int Size) {
+  uint32_t ret;
+  unsigned char wrbuf[16];
+  wrbuf[0] = cmd >> 8;
+  wrbuf[1] = cmd;
+  for (unsigned int i = 0; i < Size; i++) {
+    wrbuf[2 + i] = pData[i];
+  }
+
+  ret = HAL_I2C_Master_Transmit(pmts01->I2C, pmts01->addr, wrbuf, (Size + 2), 1000);
+  return ret;
+}
+
+/**-----------------------------------------------------------------------
+  * @brief  Receive register data from I2C
+  * @param  DeviceAddr:I2C address cmd:I2C command *pdata:data received Size:total length
+  * @retval success status
+-------------------------------------------------------------------------*/
+unsigned int MTS01_Reg_Read(P_VAN_TEMP_MTS01_CON_ST_Def pmts01, unsigned int cmd, unsigned char *pData, unsigned int Size) {
+  uint32_t ret;
+  ret = MTS01_Reg_Write(pmts01, cmd, NULL, 0);
+  if (ret == HAL_OK) {
+    ret = HAL_I2C_Master_Receive(pmts01->I2C, pmts01->addr, pData, Size, 1000);
+  }
+  return ret;
+  
+}
+
+/****************************************************************
+ 函数名:   MTS01_Init
+ 创建时间:  2024/01/08
+ 创建人:    范义东
+ 函数说明:模块初始化
+ 参数:
+
+ 返回值 :  加载成功
+****************************************************************/
+unsigned int MTS01_Init(P_VAN_TEMP_MTS01_CON_ST_Def pmts01) {
+  unsigned char scr_rd[3], scr_wr[3] = {0};
+  pmts01->isload = 0;
+  if(HAL_OK == MTS01_Reg_Read(pmts01, READ_STATUSCONFIG, scr_rd, sizeof(scr_rd))){
+		pmts01->isload = 1;
+  }else{
+	return 0;
+  }
+  
+  scr_wr[0] |= CFG_Repeatbility_Medium;
+  scr_wr[0] = (scr_wr[0] & ~CFG_MPS_Mask);
+  scr_wr[0] |= CFG_MPS_10;
+  scr_wr[1] = 0xFF;
+  scr_wr[2] = MY_CRC8(scr_wr, 2);
+  MTS01_Reg_Write(pmts01, WRITE_CONFIG, scr_wr, sizeof(scr_rd));
+  MTS01_Reg_Write(pmts01, CONVERT_T, NULL, 0);
+  return 1;
+}
+
+/****************************************************************
+ * 函数名:    MTS01_FindChale
+ * 创建时间:    2024/01/08
+ * 创建人:    范义东
+ * 函数说明:通过 ID,查找Mts配置文件
+ * 输入参数:     runMtsId  ID
+ * 输出参数: pMts01 电机参数
+ * 返回值 :   有配置文件返回true,返回失败
+ ****************************************************************/
+bool MTS01_FindChale(unsigned char runMtsId, P_VAN_TEMP_MTS01_CON_ST_Def *pMts01) {
+  unsigned char MtsId;
+  /*寻找配置文件*/
+  for (MtsId = 0; MtsId < gTemp_mts01Num; MtsId++) {
+    if (runMtsId == gTemp_mts01[MtsId].tempID) {
+      *pMts01 = &gTemp_mts01[MtsId];
+      return true;
+    }
+  }
+  *pMts01 = NULL;
+  return false;
+}
+
+/****************************************************************
+ * 函数名:    MTS01_ReadTemp
+ * 创建时间:    2024/01/08
+ * 创建人:    范义东
+ * 函数说明:通过温度通道 获取当前温度
+ * 输入参数:     runMtsId 通道
+ * 输出参数: pTemp 当前温度
+ * 返回值 :   成功rue,失败返回false
+ ****************************************************************/
+bool MTS01_ReadTemp(unsigned char runMtsId, float *pTemp) {
+  unsigned char data[3] = {0};
+  P_VAN_TEMP_MTS01_CON_ST_Def pMts01;
+
+  if (MTS01_FindChale(runMtsId, &pMts01)) {
+  	if(pMts01->isload == 0){
+		*pTemp = -104;
+		return False;
+		};
+    I2C_HandleTypeDef *I2C = pMts01->I2C;
+    HAL_StatusTypeDef ret = HAL_I2C_Master_Receive(pMts01->I2C, pMts01->addr, data, sizeof(data), 1000);
+    if (ret != HAL_OK)  {
+      errorConter[runMtsId]++;
+      if ((errorConter[runMtsId] % MTS_MAX_ERR_NUMBER) == 0) {
+        if ((errorConter[runMtsId] < MTS_REINIT_TIME)) {
+          *pTemp = MTS_IIC_ERR_TEMP-1;/*比异常低一度,便于异常判断*/
+          float temp = (*pTemp - 40)*256;
+          *(short *)&last_valve[runMtsId] = temp;
+          HAL_I2C_DeInit(I2C);
+          HAL_Delay(300U);
+		      HAL_I2C_Init(I2C);
+          HAL_Delay(300U);
+          MTS01_Init(pMts01);
+          HAL_Delay(300U);
+        }
+
+      } else if (errorConter[runMtsId] > MTS_RETRY_TIME) {
+        errorConter[runMtsId] = 0;
+      }
+      return False;
+    }
+	unsigned char clcCrc = MY_CRC8(data, 2);
+    if (data[2] == clcCrc) {
+      unsigned short tmpraw = (uint16_t)data[0] << 8 | data[1];
+      //值相同的次数++
+      if (last_valve[runMtsId] == tmpraw) {
+        count[runMtsId]++;
+        if (count[runMtsId] > 5) {
+          //需要重新初始化
+        count[runMtsId] = 0;
+        HAL_I2C_DeInit(I2C);
+        HAL_Delay(300U);
+        HAL_I2C_Init(I2C);
+        HAL_Delay(300U);
+        MTS01_Init(pMts01);
+        HAL_Delay(300U);
+          
+        }
+      } else {
+        count[runMtsId] = 0;
+        errorConter[runMtsId] = 0;
+      }
+      last_valve[runMtsId] = tmpraw;
+      float temp = (short)tmpraw;
+      temp = temp / 256;
+      *pTemp = (temp + 40);
+      if (*pTemp > (float)200.0) {
+      }
+      return True;
+    } else {
+      if (MTS_MAX_ERR_NUMBER > errorConter[runMtsId]) {
+        float temp = (short)last_valve[runMtsId];
+        temp = temp / 256;
+        *pTemp = (temp + 40);
+      } else if (MTS_MAX_ERR_NUMBER == errorConter[runMtsId]) {
+        *pTemp = -101;
+        errorConter[runMtsId] = 0;
+      }
+      errorConter[runMtsId]++;
+    }
+  } else {
+    *pTemp = -103;
+  }
+  return False;
+}
+
+
+
+/****************************************************************
+ * 函数名:    MTS01_Temp_get
+ * 创建时间:    2024/01/08
+ * 创建人:    范义东
+ * 函数说明:通过温度通道 获取当前温度
+ * 输入参数:     runMtsId 通道
+ * 输出参数: pTemp 当前温度
+ * 返回值 :   -1001:IIC 不回; -1002:IIC校验错误; -1003:没有传感器
+ ****************************************************************/
+bool MTS01_Temp_get(uint32_t runMtsId, float *Value) {
+  P_VAN_TEMP_CHANNEl_CON_ST_Def p = gTemp_channel_demo_V1;
+
+  for (int i = 0; i < gTemp_channel_demo_V1_Num; i++) {
+    if (p->id == runMtsId) {
+      *Value = p->temp;
+      return true;
+    }
+    p++;
+  }
+  return False;
+}

+ 115 - 0
DriverInterface/van_BSP_Temp_mts01.h

@@ -0,0 +1,115 @@
+/******************************************************************************
+*Copyright(C),2024-2024,万众一芯生
+*van_BSP_Temp_mts01.h
+*mts01 温度相关驱动接口
+*修改历史
+author				范义东			新建				2024/01/27
+******************************************************************************/
+
+#ifndef __VAN_BSP_TEMP_MTS01_H
+#define __VAN_BSP_TEMP_MTS01_H
+
+#ifndef bool
+typedef enum 
+{
+  false          = 0,  
+  true           = 1,  
+  False          = 0,  
+  True           = 1,  
+} bool;
+#endif
+#define ARR_LEN(array)(sizeof(array) / sizeof(array[0]))
+
+
+typedef enum 
+{
+  CONVERT_T            	= 0xCC44,   //Measure temperature
+  BREAK                	= 0x3093,   //Abort period measurement
+  READ_STATUSCONFIG     	= 0xf32d, //Read status and configuration registers
+  CLEAR_STATUS         	= 0x3041,   //Clear status register
+  WRITE_CONFIG         	= 0x5206,	  //Write configuration register
+  READ_ALERT_HI_SET    	= 0xe11f,   //Read the upper alarm thresholdvalue
+  READ_ALERT_LO_SET    	= 0xe102,   //Read the lower alarm threshold value
+  WRITE_ALERT_HI_SET   	= 0x611d,   //Set the upper alarm threshold value
+  WRITE_ALERT_LO_SET   	= 0x6100,   //Set the lower alarm threshold value
+	
+  WRITE_ALERT_LO_UNSET	= 0x610b,   //Set the alarm clear threshold lower threshold
+	WRITE_ALERT_HI_UNSET	= 0x6116,   //Set the alarm clear threshold upper threshold
+  READ_ALERT_HI_UNSET	    = 0xe114, //Read Alarm Clear Threshold Upper Threshold
+  READ_ALERT_LO_UNSET	    = 0xe109, //Read Alarm Clear Threshold Lower Threshold
+  COPY_PAGE0           	= 0xcc48,   //Save page0 to EEPROM
+  RECALL_EE            	= 0xccb8,   //Recall EEPROM area
+  RECALL_PAGE0_RES     	= 0xccbb,	  //Restore the residual area of page0
+  SOFT_RST            	= 0x30a2,	    //Software reset
+} I2C_CMD;
+
+/*Bit definition of config register*/
+#define CFG_MPS_Mask   		       (0x1C)
+#define CFG_Repeatbility_Mask    (0x03)
+
+#define CFG_MPS_Single  		     (0x00)
+#define CFG_MPS_Half  		       (0x04)
+#define CFG_MPS_1  			         (0x08)
+#define CFG_MPS_2 			         (0x0C)
+#define CFG_MPS_4 			         (0x10)
+#define CFG_MPS_10 			         (0x14)
+
+#define CFG_Repeatbility_Low   	 (0x00)
+#define CFG_Repeatbility_Medium  (0x01)
+#define CFG_Repeatbility_High 	 (0x02)
+
+#define POLYNOMIAL 0x131  // 100110001
+
+/*Definition of conversion time corresponding to repeatability setting*/
+/*The default low/medium/high repeatability corresponds to the conversion time of 4/6/11ms respectively. 
+  Considering the delay deviation of different users of the microcontroller, reserve 2ms to ensure sufficient conversion time*/
+#define tConvert_LowRepeat  	  	(6)	/* ms. */
+#define tConvert_MedRepeat   	    (8)	/* ms. */
+#define tConvert_HighRepeat    		(13)	/* ms. */
+
+
+
+
+#define TEMP_MTS01_MAX_AVG_TIME  10 /*最多10次缓存用于求平均*/
+
+#define MTS01_ADDR0 		   (0x44<<1 )  /* Addr */
+#define MTS01_ADDR1 		   (0x45<<1 ) /* Addr */
+
+
+#define TEMP_MTS01NAMEANDID(chanle) .tempID = chanle, .Name = #chanle
+typedef enum
+{
+	TEMP_BLOCK,
+} TEMP_MTS01_V1;	
+	
+typedef enum _EnmFuntionIdType {
+	EnmFuntionIdType_IdNull = 0,
+	EnmFuntionIdType_Idhost = 1, /* 主机 */
+} EnmFuntionIdType;
+
+	
+typedef struct
+{
+  unsigned int		tempID;			//温区编号
+	unsigned char		Name[16];		//温区名称
+	void  *I2C; 	  /*IIC端口 端口*/
+	unsigned int	   addr;	  /*CS 端口*/
+	unsigned int	   maxlen;	  /*CS 端口*/
+	unsigned int	   header;	  /*CS 端口*/
+	float temp[TEMP_MTS01_MAX_AVG_TIME];
+	unsigned int	   isload;	  /*CS 端口*/
+} VAN_TEMP_MTS01_CON_ST_Def, *P_VAN_TEMP_MTS01_CON_ST_Def;
+
+typedef struct
+{
+    unsigned int		id;			//设备id
+    unsigned int		feed_channel;			//测温通道
+    float		temp;			//温度值
+} VAN_TEMP_CHANNEl_CON_ST_Def, *P_VAN_TEMP_CHANNEl_CON_ST_Def;
+
+bool MTS01_ReadTemp(unsigned char runMtsId, float *pTemp);
+bool MTS01_Temp_get(uint32_t runMtsId, float *Value);
+unsigned int loadTemp_mts01Cfg(void);
+
+
+#endif