/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether 
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * Copyright (c) 2019 STMicroelectronics International N.V. 
  * All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without 
  * modification, are permitted, provided that the following conditions are met:
  *
  * 1. Redistribution of source code must retain the above copyright notice, 
  *    this list of conditions and the following disclaimer.
  * 2. Redistributions in binary form must reproduce the above copyright notice,
  *    this list of conditions and the following disclaimer in the documentation
  *    and/or other materials provided with the distribution.
  * 3. Neither the name of STMicroelectronics nor the names of other 
  *    contributors to this software may be used to endorse or promote products 
  *    derived from this software without specific written permission.
  * 4. This software, including modifications and/or derivative works of this 
  *    software, must execute solely and exclusively on microcontroller or
  *    microprocessor devices manufactured by or for STMicroelectronics.
  * 5. Redistribution and use of this software other than as permitted under 
  *    this license is void and will automatically terminate your rights under 
  *    this license. 
  *
  * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
  * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
  * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
  * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */
	/*
	修改记录:
	 2021-1-26：增了终端与中控板交互的0x64的报文头处理，主要修改在：TerminalSlave485.c,
可通过查找	："2021-1-26"来查看相关修改。    ---  modify by Daiyf at 2021-1-26
   2021-1-30：扩展了91命令IO响应的数据位，固定8仓*4=32字节。
                                             ---  modify by Daiyf at 2021-1-30
																						 
  ******************************************************************************
  */
	
/* USER CODE END Header */
/*
更改记录：
Q2021-1-26： 1、福建装车现场反馈主控LED破封报警显示延时长达8S，经常发现是内部判断次数过长，造成延时过长，经过与相关工程师交流，改为约1.8S后报警显示。

             2、对中控板的LED显示作了定义进行调整，方便观察中控固件运行状态
						    LED1：中控反与采集板信指示灯：             连接：闪亮，       断开连接：不亮或长亮；          （注：原LED1 与采集板的通信息，保持一致）
						    LED2：采集板上4卸油阀的的状态状态：        正常：不亮，       异常：长亮或闪亮；              （注：原LED2-4 用于4个卸油阀的的状态在中控板显示，实际意义不大，改用一个LED作指示）
								LED3：中控板OS中通讯任务运行指示灯：       正常：闪亮，       异常：不亮或长亮；
								LED4：采中控板OS中数据处理任务运行指示灯：  正常：不亮，       异常：长亮或闪亮；
								LED5：破封产生指标灯：                    破风成立：长亮，    破风信号产生：闪亮，   无破风：不亮；
								
					  3、相关修改通过查找“2021-2-3”和“Daiyf”可获取。
Note by Daiyf at 2021-2-3  
      
*/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "cmsis_os.h"
#include "dma.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "Data_deal.h"
#include "DS1302.h"
#include "KeySlave485.h"
#include "usart.h"
#include "leaf_ota.h"
#include "obj_spi_sd_stm32.h"
#include "func_fatfs.h"
#include "func_fatfs_record.h"
#include "func_spi_w25qxx.h"
uint32_t sd000,sd001;//浮点数转换为实时显示的参数，4字节
uint16_t uart4RxCounter=0; //串口4接收数据计数器
  
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* 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 */
/* Private variables ---------------------------------------------------------*/

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  * @retval int
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
	extern SemaphoreHandle_t xSemaphore_uart4;
	extern uint8_t USART_RX_BUF_U2[Uart2_BUF_SIZE];   /*为UART2、3专门开一个DMA接收存储区 2021-4-12 by Daiyf*///static
	//int LenMain;
	__set_FAULTMASK(1);//先关闭全局中断
#if	IR_ROM1 == 0x8000000	
	Start_BootLoader();
#else	
	SCB->VTOR = 0x08080000;
	//Start_BootLoader();
	__set_PRIMASK(0);
#endif	
  /* 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 */
	
	if(__HAL_RCC_GET_FLAG(RCC_FLAG_PORRST) != RESET)
	{
		 //这是上电复位
		PrintCnt=99;
	}
	else if (__HAL_RCC_GET_FLAG(RCC_FLAG_PINRST) != RESET)
	{
		 //这是外部RST管脚复位RCC_GetFlagStatus
		PrintCnt=88;
	}
	else if (__HAL_RCC_GET_FLAG(RCC_FLAG_SFTRST)!= RESET)
	{
		 ////软件复位
	}
	else if(__HAL_RCC_GET_FLAG(RCC_FLAG_IWDGRST))//独立看门狗复位
	{
			;//
	}
	//RCC_ClearFlag();//清除RCC中复位标志
	__HAL_RCC_CLEAR_RESET_FLAGS();//清除所有复位标识,用于复位类型的判断
  /* USER CODE END SysInit */
	
  /* Initialize all configured peripherals */
  MX_GPIO_Init();
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_DMA_Init();
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_TIM6_Init();
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_USART2_UART_Init();  // Collect Board commincation port    采集板   采集器 19200
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_USART3_UART_Init();  // DWIN LCD comminucation port  液晶屏  迪文屏    19200
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_UART4_Init();        // Keyboard comminucation port 按键以及打印机，语音播报器 9600
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_TIM2_Init();
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
  MX_USART1_UART_Init();  // Terminal comminucation port终端  9600
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
//  MX_SPI4_Init();
  /* USER CODE BEGIN 2 */
	FM25L16B_Init();
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
	HAL_TIM_Base_Start_IT(&htim2);
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
	HAL_TIM_Base_Start_IT(&htim6);
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
	Uart_Mode_Init();	
	HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_2);//看门狗sp706
	
	
	//HAL_UART_Receive_DMA(&huart2, USART_RX_BUF_U2,Uart2_BUF_SIZE);
  /* USER CODE END 2 */
	
  /* Call init function for freertos objects (in freertos.c) */
  MX_FREERTOS_Init();
	
	/* 创建互斥信号量 */
	xSemaphore_uart4 = xSemaphoreCreateMutex();

  /* Start scheduler */
  osKernelStart();
  
  /* We should never get here as control is now taken by the scheduler */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  RCC_PeriphCLKInitTypeDef PeriphClkInitStruct = {0};

  /**Configure the main internal regulator output voltage 
  */
  __HAL_RCC_PWR_CLK_ENABLE();
  __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLM = 25;
  RCC_OscInitStruct.PLL.PLLN = 432;
  RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
  RCC_OscInitStruct.PLL.PLLQ = 2;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /**Activate the Over-Drive mode 
  */
  if (HAL_PWREx_EnableOverDrive() != HAL_OK)
  {
    Error_Handler();
  }
  /**Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
                              |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV4;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_7) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInitStruct.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
                              |RCC_PERIPHCLK_USART3|RCC_PERIPHCLK_UART4;
  PeriphClkInitStruct.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  PeriphClkInitStruct.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  PeriphClkInitStruct.Usart3ClockSelection = RCC_USART3CLKSOURCE_PCLK1;
  PeriphClkInitStruct.Uart4ClockSelection = RCC_UART4CLKSOURCE_PCLK1;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  Period elapsed callback in non blocking mode
  * @note   This function is called  when TIM1 interrupt took place, inside
  * HAL_TIM_IRQHandler(). It makes a direct call to HAL_IncTick() to increment
  * a global variable "uwTick" used as application time base.
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
  /* USER CODE BEGIN Callback 0 */

  /* USER CODE END Callback 0 */
  if (htim->Instance == TIM1) {
		//add boly SD卡 
		sdcard_systick_timerproc();
		//end boly 
    HAL_IncTick();
  }
  /* USER CODE BEGIN Callback 1 */

  /* USER CODE END Callback 1 */
}

/**
  * @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 */
  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 */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
