/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
  * All rights reserved.</center></h2>
  *
  * This software component is licensed by ST under Ultimate Liberty license
  * SLA0044, the "License"; You may not use this file except in compliance with
  * the License. You may obtain a copy of the License at:
  *                             www.st.com/SLA0044
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "usb_device.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "fdilink_decode.h"
#include "FDILink.h"
#include "FDI_send.h"
#include "FDI_config.h"
#include "FDI_Search_Function.h"
/* 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 ---------------------------------------------------------*/
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
DMA_HandleTypeDef hdma_usart1_rx;
DMA_HandleTypeDef hdma_usart1_tx;
DMA_HandleTypeDef hdma_usart2_rx;
DMA_HandleTypeDef hdma_usart2_tx;

/* USER CODE BEGIN PV */

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_DMA_Init(void);
static void MX_USART1_UART_Init(void);
void MX_USART2_UART_Init(uint32_t baudrate);
/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
static int count = 0;
static uint8_t USB_Tx_Buffer[2048];
extern uint8_t CDC_Transmit_FS(uint8_t* Buf, uint16_t Len);
static FDILink_Status_t _FDILink;
static uint8_t RxBuffer[256];
extern uint8_t 	USART2_RX_BUF[256];     //接收缓冲,最大USART_REC_LEN个字节.（一般给200，接收数据量大就增加）
extern uint8_t		Res;
extern uint16_t    USART2_RX_STA;       			  //接收状态标记
extern uint16_t    USART2_RX_LEN;


/**
  * @brief 串口1接受数据
  * @param[in] huart
  */ 
void UART_RxIdleCallback(UART_HandleTypeDef *huart)
{
	 /* 判断是哪个串口触发的中断 */
	if(huart ->Instance == USART1)
	{
		int length = 256 - __HAL_DMA_GET_COUNTER(huart->hdmarx);
		fdiComProtocolReceive(&_FDILink, RxBuffer, length);
		HAL_UART_Receive_DMA(huart, RxBuffer, 256);
	}
}

/* USER CODE END 0 */

/**
  * @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();


	
/******************************************
							功能查询函数
	
	--->void fdiComSearchFunction(void);<---

******************************************/
	


  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_DMA_Init();
  MX_USART1_UART_Init();
	MX_USART2_UART_Init(921600);
  MX_USB_DEVICE_Init();
  /* USER CODE BEGIN 2 */
  __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);
  HAL_UART_Receive_DMA(&huart1, RxBuffer, 256);
	HAL_UART_Receive_IT(&huart2, &Res, 1);
	fdiComSetConfig();

	while(huart1.gState != HAL_UART_STATE_READY){};
	
//配置设备数据口的波特率
	fdiComSetConfigBaud(COM1,COMM_BAUD_921600);				//(1.设置COM口，2.设置波特率)
//配置设备数据口的功能
	fdiComSetConfigType(COM1,COMM_STREAM_TYPE_NAV);		//(1.设置COM口，2.设置数据口功能)
//配置USER_DEFINE数据	
	fdiComSetConfigUserDefine(USER_DEFINE_CAN_ID,1);	//(1.USE_DEFINE_ID,2.NUM)
//配置GPIOs功能
	fdiComSetConfigGPIOs(GPIO2, GPIOS_1PPS_OUTPUT);		//(1.设置GPIO口,2.GPIO口的模式)
//配置AID开关		
	fdiComSetConfigAID(AID_ACCEL_GRAVITY, ENABLE);		//(1.设置AID功能,2.开关)
//查询轴的安装方向
	fdiComGetAxis();
//配置AID角度方向
	fdiComSetConfigAxis("x", 0);											//(1.设置轴功能,2.设置角度)
//查询轴的安装方向		
	fdiComGetAxis();
//查询双天线航向与载体前向夹角		
	fdiComGetAnte();
//配置双天线航向偏角	
	fdiComSetConfigAnteHeadbias(0);										//(设置航向偏角)
//配置双天线之间的基线长度
	fdiComSetConfigAnteBaseline(0);										//(设置基线长度)
//配置GNSS主天线到IMU的杆臂命令
	fdiComSetConfigAnteArm(0, 0, 0);									//(1.x轴,2.y轴,3.z轴)
//查询双天线航向与载体前向夹角
	fdiComGetAnte();
//配置IMU校准
	fdiComSetConfigImucailed(Gyro);										//(设置IMU校准)
//配置发送的数据包内容及频率
		fdiComSetConfigPacketSentMsg(MSG_UNIX_TIME, 20);				//(1.需要数据包内容，2.频率)
//查询数据
	fdiComGetParam("MSG_IMU");
//查询数据
	fdiComGetParam("COMM_BAUD3");
//配置参数数值
	fdiSetParam("COMM_BAUD3", 8);                     //(1.参数名称，2.参数数值)
//查询数据	
	fdiComGetParam("COMM_BAUD3");
//配置AIDmag3DMagetic开关
	fdiComConfigAIDmag3DMagetic(Enable);							//(开关)
//配置保存
	fdiSetSave();
//配置重启
	fdiSetReboot();
	
//	fdiComGetVersionInformation();											//读取固件信息


		fdiComSetConfigPacketSentMsg(MSG_UNIX_TIME, 50);				//(1.需要数据包内容，2.频率)
		fdiGetPacket(Unix_Time_Packet_ID);
  /* USER CODE END 2 */	 
  while (1)
  {
		
   }
}

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

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != 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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
  PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USB;
  PeriphClkInit.UsbClockSelection = RCC_USBCLKSOURCE_PLL_DIV1_5;
  if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  {
    Error_Handler();
  }
}

/**
  * @brief USART1 Initialization Function
  * @param None
  * @retval None
  */
static void MX_USART1_UART_Init(void)
{

  /* USER CODE BEGIN USART1_Init 0 */

  /* USER CODE END USART1_Init 0 */

  /* USER CODE BEGIN USART1_Init 1 */

  /* USER CODE END USART1_Init 1 */
  huart1.Instance = USART1;
  huart1.Init.BaudRate = 921600;
  huart1.Init.WordLength = UART_WORDLENGTH_8B;
  huart1.Init.StopBits = UART_STOPBITS_1;
  huart1.Init.Parity = UART_PARITY_NONE;
  huart1.Init.Mode = UART_MODE_TX_RX;
  huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart1) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART1_Init 2 */

  /* USER CODE END USART1_Init 2 */

}

/**
  * @brief USART2 Initialization Function
  * @param None
  * @retval None
  */
void MX_USART2_UART_Init(uint32_t baudrate)
{
  /* USER CODE BEGIN USART2_Init 0 */

  /* USER CODE END USART2_Init 0 */

  /* USER CODE BEGIN USART2_Init 1 */

  /* USER CODE END USART2_Init 1 */
  huart2.Instance = USART2;
  huart2.Init.BaudRate = baudrate;
  huart2.Init.WordLength = UART_WORDLENGTH_8B;
  huart2.Init.StopBits = UART_STOPBITS_1;
  huart2.Init.Parity = UART_PARITY_NONE;
  huart2.Init.Mode = UART_MODE_TX_RX;
  huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&huart2) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN USART2_Init 2 */

  /* USER CODE END USART2_Init 2 */

}


/** 
  * Enable DMA controller clock
  */
static void MX_DMA_Init(void) 
{
  /* DMA controller clock enable */
  __HAL_RCC_DMA1_CLK_ENABLE();

  /* DMA interrupt init */
  /* DMA1_Channel4_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel4_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel4_IRQn);
  /* DMA1_Channel5_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel5_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel5_IRQn);
  /* DMA1_Channel6_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel6_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel6_IRQn);
  /* DMA1_Channel7_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(DMA1_Channel7_IRQn, 0, 0);
  HAL_NVIC_EnableIRQ(DMA1_Channel7_IRQn);

}

/**
  * @brief GPIO Initialization Function
  * @param None
  * @retval None
  */
static void MX_GPIO_Init(void)
{

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOD_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

}

/**
  * @brief 串口发送
  * @param [1]ch
  * @param [2]FILE * fp
  * @retval None
  */
int fputc(int ch,FILE * fp)
{
 HAL_UART_Transmit(&huart1,(uint8_t * )&ch,1,0xffff);
 return ch;
}

/**
  * @brief 串口接收
  * @param [1]ch
  * @param [2]FILE * fp
  * @retval None
  */
int fgetc(FILE *f)
{
int ch;
/* 等待串口输入数据 */
while (__HAL_UART_GET_FLAG(&huart1, UART_FLAG_RXNE) == RESET);
HAL_UART_Receive(&huart1, (uint8_t *)&ch, 1, 0xFFFF);
return (ch);
}

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

  /* 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,
     tex: 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****/
