/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @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 "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */

#include "usart.h"
#include "adc.h"
#include "gpio.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 ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
#ifdef iap_
typedef struct _T2C_RemoteCaliData
{
	uint16_t   FrameHead;   		//帧头 0-1
	uint32_t   AddrExtModule; 	//4字节扩展地址   2--5
	uint16_t   FunctionCode;		//功能码2字节   只能是0x0003、0x0006或者0x0010    6--7
	uint16_t   AddrRegStart;		//寄存器起始地址2字节：合法范围为0x0000~0xffff    8--9
	uint16_t   NumberOfRegs;		//数据个数2字节：最多读取0xffff字节；实际取决于终端限制，目前为256字节  10--11
	uint32_t   ReserveData001;	//保留字4字节 12--15
	uint8_t    PayLoadData[44+32+32];	//有效载荷具体有三种：44，或者76字节，或者108字节；字节 16--59
	uint16_t   ReserveData002;	//保留字2字节    60--61
	uint16_t   CheckSum;				//校验2字节      62--63
}sT2C_RemoteCaliDat;

typedef struct{
	uint16_t USE_RKG;								//控制器占用人孔盖总线
	uint16_t USE_XYF;								//控制器占用卸油阀总线
	uint16_t KZQ_ErrorCnt;					//校验错误计数
	uint16_t KZQ_Error;							//控制器通信错误
	uint8_t sensor_addr;						//操作的传感器地址
	uint8_t sensor_RW;							//操作的传感器读写属性
	uint16_t sensor_reg;							//操作的传感器寄存器地址，即功能码
	uint8_t sensor_datelen;					//操作数据长度
	uint8_t data_buf[70];          //下发数据缓存 
}KZQ_Inf;
#define 	ADDR_Is_In_ElecFence	0x05D0	//	1488		车是否在电子围栏内
 
 sT2C_RemoteCaliDat T2C_RemoteCaliDat001  =
{
 
	0x3901,
	0x9551000,
	0x0003,
	ADDR_Is_In_ElecFence,//0x0000,
	0x0001,        //操作的数据个数
	0x55AA55AA,    //保留字4字节
	{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,\
	25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44},
	0x7788,//保留字2字节
	0x99AA   //校验2字节
};
sT2C_RemoteCaliDat *pT2C_RemoteCaliData = &T2C_RemoteCaliDat001; 

	uint8_t CMD_KZQ[256] =
	{0x39,0x01,0x95,0x50,0x00,0x01,0x00,0x03,
	 0x00,0x00,0x00,0x00,0x95,0x05,0xAA,0xAA,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
	};
	int flagU2Rx=0;
	int data_lengthU2;
	uint8_t* ptx = CMD_KZQ;   
	uint16_t ModbusCRC   = 0;
	uint16_t SetSuccess  = 0;
	uint16_t ModbusCRC1  = 0;
	uint16_t SetSuccess1 = 0;
	uint16_t SetSuccess2 = 0;
	uint16_t SetSuccess3 = 0;                 

	int i000;
	uint8_t* send_ptr;
	uint16_t send_len;
	uint32_t tmpU32;
	KZQ_Inf kzq_inf;
	KZQ_Inf* pkzq = &kzq_inf;
	uint8_t USART2_RX_BUF003[128];
	uint8_t F_STATE[70] = {0};
	uint8_t ptxCang01Temp[150];
	uint32_t KZQ_RTerror = 0;
	
void RstCPU(void)
{
  uint16_t j;
  __disable_irq();
  HAL_NVIC_SystemReset();
	
	while(1)
	{
		__disable_irq();	
		NVIC_SystemReset();  
		while(j --)
		{
	    __nop(); __nop(); __nop(); __nop(); 
		}
	}
}


#endif
/* USER CODE END Variables */
osThreadId iapTaskHandle;
osThreadId myTask02Handle;
osThreadId myTask03Handle;
osThreadId myTask04Handle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartiapTask(void const * argument);
void StartTask02(void const * argument);
void StartTask03(void const * argument);
void StartTask04(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of iapTask */
  osThreadDef(iapTask, StartiapTask, osPriorityRealtime, 0, 128);
  iapTaskHandle = osThreadCreate(osThread(iapTask), NULL);

  /* definition and creation of myTask02 */
  osThreadDef(myTask02, StartTask02, osPriorityRealtime, 0, 128);
  myTask02Handle = osThreadCreate(osThread(myTask02), NULL);

  /* definition and creation of myTask03 */
  osThreadDef(myTask03, StartTask03, osPriorityRealtime, 0, 128);
  myTask03Handle = osThreadCreate(osThread(myTask03), NULL);

  /* definition and creation of myTask04 */
  osThreadDef(myTask04, StartTask04, osPriorityRealtime, 0, 128);
  myTask04Handle = osThreadCreate(osThread(myTask04), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartiapTask */
/**
  * @brief  Function implementing the iapTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartiapTask */
void StartiapTask(void const * argument)
{
  /* USER CODE BEGIN StartiapTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
#ifdef iap_
	if ((data_lengthU2 > 300) || flagU2Rx == 1) {
		flagU2Rx = 0;
		if ((USART2_RX_BUF002[0] == 0xA5) && (USART2_RX_BUF002[1] == 0x5A))
		{
			tmpU32 = (USART2_RX_BUF002[0] << 24) | (USART2_RX_BUF002[1] << 16) | (USART2_RX_BUF002[2] << 8) | (USART2_RX_BUF002[3]);
			if (StartBytes_IAP == tmpU32)
			{
				Process_CMD_IAP_Update();
				continue;
			}
		}
	}
#endif
  }
  /* USER CODE END StartiapTask */
}

/* USER CODE BEGIN Header_StartTask02 */
/**
* @brief Function implementing the myTask02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask02 */
void StartTask02(void const * argument)
{
  /* USER CODE BEGIN StartTask02 */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
	HAL_Delay(1);  
	HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
	
	gpioaStatus = GetPaInputStatus();
	gpiobStatus = GetPbOutputStatus();
	GetADCResults(&hadc1); 
  }
  /* USER CODE END StartTask02 */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the myTask03 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void const * argument)
{
  /* USER CODE BEGIN StartTask03 */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
	ModBus();
  }
  /* USER CODE END StartTask03 */
}

/* USER CODE BEGIN Header_StartTask04 */
/**
* @brief Function implementing the myTask04 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask04 */
void StartTask04(void const * argument)
{
  /* USER CODE BEGIN StartTask04 */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);

  }
  /* USER CODE END StartTask04 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

