/**
  ******************************************************************************
  * File Name          : USART.h
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2020 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
  *
  ******************************************************************************
  */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __usart_H
#define __usart_H
#ifdef __cplusplus
 extern "C" {
#endif

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

/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "usbd_cdc_if.h"
/* USER CODE END Includes */

extern UART_HandleTypeDef huart5;
extern UART_HandleTypeDef huart1;
extern UART_HandleTypeDef huart2;
extern UART_HandleTypeDef huart3;
extern UART_HandleTypeDef huart6;

/* USER CODE BEGIN Private defines */
#define RFID_USART					huart1
//#define METER_USART					huart2
#define IAP_USART					huart3
#define BLE_USART					huart5

#ifdef FUNC_RS485_SLAVE
#define RS485_USART					huart2
#endif

#define WIFI_USART					huart5
#define DEBUG_USART					huart6
#define UART_BUFFER_SIZE		    ((uint32_t)2048)
#define TX_TIMEOUT                  ((uint32_t)0x010)
#define RX_TIMEOUT                  ((uint32_t)0xFFFFFFFF)

/*
    UART protocol constant declare
*/
#define PROTOCOL_HEAD			        0xaa

#ifdef FUNC_RS485_SLAVE
//Internal Module Communication Protocol / 2.2 Slave Address
//0x01	Aux. power module	    Aux. power module in DC EVSE system
//0x02	Fan control module	    System fan control module in DC EVSE system
//0x03	Relay control module 	Output relay control module in DC EVSE system
//0x04	IO Extend module	    IO extend module in DC EVSE system
//0x05	AC mainboard module	    AC mainboard module in DC EVSE system (*)
//0x06	LED control module	    LED control module in DC EVSE system
//0x07	Relay RC_1 module	    Output relay module in 360KW Power Cabinet
//0x08	Relay RC_2 module	    Output relay module in 360KW Power Cabinet
//0x09	Relay control module	Output relay module in 360KW Dispenser
//0xFF	Broadcast 	            Don!|t need to address module or broadcast usage

//#define PROTOCOL_ADDR				    0x05
#define PROTOCOL_ADDR				    0xff
#define PROTOCOL_ADDR_AC_MAINBOARD      0x05
#else
#define PROTOCOL_ADDR				    0xff	// 0x01:AUX PWR     0x02: FAN BD    0x03:RLY BD			0xff:Any
#endif //FUNC_RS485_SLAVE

#define PROTOCOL_ADDR_BROADCAST		    0xff

#define PROTOCOL_MESSAGE_QUERY_FW_VER			            0x01
#define PROTOCOL_MESSAGE_QUERY_HW_VER				        0x02
#define PROTOCOL_MESSAGE_QUERY_PRESENT_INPUT_VOLTAGE	    0x03
#define PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_VOLTAGE	    0x04
#define PROTOCOL_MESSAGE_QUERY_FAN_SPEED			        0x05
#define PROTOCOL_MESSAGE_QUERY_TEMPERATURE			        0x06
#define PROTOCOL_MESSAGE_QUERY_AUX_POWER_VOLTAGE		    0x07
#define PROTOCOL_MESSAGE_QUERY_RELAY_OUTPUT_STATUS		    0x08
#define PROTOCOL_MESSAGE_QUERY_GFD_ADC_VALUE    	        0x09
#define PROTOCOL_MESSAGE_QUERY_INPUT_GPIO_STATUS	        0x0A
#define PROTOCOL_MESSAGE_QUERY_ALARM_LOG			        0x22
#define PROTOCOL_MESSAGE_QUERY_SN				            0x23
#define PROTOCOL_MESSAGE_QUERY_MODEL_NAME			        0x24
#define PROTOCOL_MESSAGE_QUERY_PARAMETER			        0x25
#define PROTOCOL_MESSAGE_QUERY_RTC      			        0x26
#define PROTOCOL_MESSAGE_QUERY_PRESENT_OUTPUT_CURRENT      	0x27

//edward 2019_08_27
#define PROTOCOL_MESSAGE_QUERY_EVSE_STATE  					0x28
#define PROTOCOL_MESSAGE_QUERY_EVSE_ALARM               	0x29
#define PROTOCOL_MESSAGE_QUERY_BLE_STATE      	        	0x2A
#define PROTOCOL_MESSAGE_QUERY_BLE_LOGIC_CENTRAL_ID     	0x2B
#define PROTOCOL_MESSAGE_QUERY_POWER_CONSUME            	0x2C
#define PROTOCOL_MESSAGE_QUERY_GUN_PLUGIN_TIMES         	0x2D
#define PROTOCOL_MESSAGE_QUERY_EVSE_AUTH_MODE           	0x2E
#define PROTOCOL_MESSAGE_QUERY_PRESENT_LEAK_CURRENT     	0x2F


#define PROTOCOL_MESSAGE_CONFIG_FAN_SPEED			        0x81
#define PROTOCOL_MESSAGE_CONFIG_SN				            0x82
#define PROTOCOL_MESSAGE_CONFIG_MODEL_NAME			        0x83
#define PROTOCOL_MESSAGE_CONFIG_PARAMETER			        0x84
#define PROTOCOL_MESSAGE_CONFIG_RELAY_OUTPUT    		    0x85
#define PROTOCOL_MESSAGE_CONFIG_GPIO_OUTPUT			        0x86
#define PROTOCOL_MESSAGE_CONFIG_RTC 			            0x87
#define PROTOCOL_MESSAGE_CONFIG_LED_ACTION_ALARM 	        0x88
#define PROTOCOL_MESSAGE_CONFIG_MAX_CURRENT_PWM_DUTY 	    0x89
#define PROTOCOL_MESSAGE_CONFIG_LEGACY_REQUEST         	    0x8A
#define PROTOCOL_MESSAGE_CONFIG_MCU_RESET                   0x8C
#define PROTOCOL_MESSAGE_CONFIG_BREATHE_LED_TIMING          0x8D
#define PROTOCOL_MESSAGE_CONFIG_LED_BRIGHTNESS              0x8E
#define PROTOCOL_MESSAGE_CONFIG_EVSE_AUTH_MODE              0x8F
#define PROTOCOL_MESSAGE_CONFIG_ERASE_MEMORY                0x94
#ifdef FUNC_RESET_CONNECTOR_PLUGIN_TIMES
#define PROTOCOL_MESSAGE_CONFIG_RESET_CONNECTOR_PLUGIN_TIMES    0xA0
#endif

#define PROTOCOL_MESSAGE_UPGRADE_START				        0xe0
#define PROTOCOL_MESSAGE_UPGRADE_ABOARD				        0xe1
#define PROTOCOL_MESSAGE_UPGRADE_TRANS				        0xe2
#define PROTOCOL_MESSAGE_UPGRADE_STOP				        0xe3

#define INPUT_L1_AC_VOLTAGE         0x01
#define INPUT_L2_AC_VOLTAGE         0x02
#define INPUT_L3_AC_VOLTAGE         0x03
#define OUTPUT_L1_CURRENT           0x11
#define OUTPUT_L2_CURRENT           0x12
#define OUTPUT_L3_CURRENT           0x13
#define LEAK_CURRENT                0x21
#define GROUND_FAULT_VOLTAGE        0x31
//#define ERROR_RESTORE_POINT         0xFE //Hao: Not Used
//#define ERROR_TIGGER_POINT          0xFF //Hao: Not Used

#ifdef FUNC_SIMU_CHARGE_CURRENT_OUTPUT
#define SIMU_CHARGE_CURRENT_CMD     0xFC
#define SIMU_CHARGE_CURRENT_ON      0xC1
#define SIMU_CHARGE_CURRENT_ON_PWD  0x1234
#endif

#ifdef FUNC_SWITCH_RELAY_WELDING_PROTECT
#define SIMU_SWITCH_RELAY_WELDING_CMD       0xFD
#define SIMU_SWITCH_RELAY_WELDING_DISABLE   0xD1
#define SIMU_SWITCH_RELAY_WELDING_PWD       0x5678
#endif

#define SETTING_LEGACY_REQUEST      0x01

/*
    RFID module protocol constant declare
*/
#define RFID_CMD_AUTO_DETECT                        0x1D
#define RFID_CMD_LED                                0x14
#define RFID_CMD_REQUEST_SN_14443A                  0x20
#define RFID_CMD_REQUEST_SN_14443B                  0x60
#define RFID_CMD_REQUEST_SN_FELICA                  0x2f
#define RFID_CMD_BLOCK_READ                         0x21
#define RFID_CMD_BLOCK_WRITE                        0x22
#define RFID_CMD_HALT_14443A                        0x28
#define RFID_CMD_HALT_14443B                        0x62

#define BLE_INIT_DELAY                              10
#define BLE_CMD_SET_RESTORE                         0x00
#define BLE_CMD_SET_DTR                             0x01
#define BLE_CMD_SET_DSR                             0x02
#define BLE_CMD_SET_PAIRING_MODE                    0x03
#define BLE_CMD_SET_SECURITY_MODE                   0x04
#define BLE_CMD_SET_SECURITY_TYPE                   0x05
#define BLE_CMD_SET_DISCOVERABILITY                 0x06
#define BLE_CMD_SET_CONNECTABILITY                  0x07
#define BLE_CMD_SET_ROLE                            0x08
#define BLE_CMD_SET_DATA_MODE                       0x09
#define BLE_CMD_SET_NAME                            0x0a
#define BLE_CMD_SET_MAX_POWER                       0x0b
#define BLE_CMD_SET_MIN_INTERVAL                    0x0c
#define BLE_CMD_SET_MAX_INTERVAL                    0x0d
#define BLE_CMD_SET_TX_PHY                          0x0e
#define BLE_CMD_SET_RX_PHY                          0x0f
#define BLE_CMD_SET_DISCONNECT_DEVICE               0x10
#define BLE_CMD_GET_ADDR                            0x11

#define BLE_PROTOCOL_MESSAGE_SIGN_IN                0x01
#define BLE_PROTOCOL_MESSAGE_PUK_REG                0x02
#define BLE_PROTOCOL_MESSAGE_PIN_GET                0x03
#define BLE_PROTOCOL_MESSAGE_PIN_RENEW              0x04
#define BLE_PROTOCOL_MESSAGE_PIN_DEL                0x05
#define BLE_PROTOCOL_MESSAGE_PIN_QUERY              0x06
#define BLE_PROTOCOL_MESSAGE_CHARGING_START         0x11
#define BLE_PROTOCOL_MESSAGE_CHARGING_STOP          0x12
#define BLE_PROTOCOL_MESSAGE_STATUS_QUERY           0x13
#define BLE_PROTOCOL_MESSAGE_RESERVATION            0x14
#define BLE_PROTOCOL_MESSAGE_TIME_QUERY             0x21
#define BLE_PROTOCOL_MESSAGE_TIME_CONFIG            0x22
#define BLE_PROTOCOL_MESSAGE_LED_QUERY              0x23
#define BLE_PROTOCOL_MESSAGE_LED_CONFIG             0x24
#define BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_QUERY    0x25
#define BLE_PROTOCOL_MESSAGE_CURRENT_LIMIT_CONFIG   0x26
#define BLE_PROTOCOL_MESSAGE_FIRMWARE_VERSION_QUERY 0x31
#define BLE_PROTOCOL_MESSAGE_FIRMWARE_UPDATE_REQ    0x32
#define BLE_PROTOCOL_MESSAGE_FIRMWARE_TRANS_REQ     0x33
#define BLE_PROTOCOL_MESSAGE_FIRMWARE_END_REQ       0x34


#define WIFI_INIT_DELAY                                 10
#define WIFI_CMD_SET_RESTORE                            0x00
#define WIFI_CMD_SET_DTR                                0x01
#define WIFI_CMD_SET_DSR                                0x02
#define WIFI_CMD_GET_ADDR                               0x03
#define WIFI_CMD_SET_SSID                               0x04
#define WIFI_CMD_SET_CHANNEL                            0x05
#define WIFI_CMD_SET_SECURITY_MODE                      0x06
#define WIFI_CMD_SET_PASSWORD                           0x07
#define WIFI_CMD_SET_DHCPSERVER                         0x08
#define WIFI_CMD_SET_TCPSETVER_CONFIG                   0x09
#define WIFI_CMD_SET_AP_ACTIVE                          0x0a
#define WIFI_CMD_SET_DATA_MODE                          0x0b

#define WIFI_PROTOCOL_MESSAGE_SIGN_IN                   0x01
#define WIFI_PROTOCOL_MESSAGE_PUK_REG                   0x02
#define WIFI_PROTOCOL_MESSAGE_PIN_GET                   0x03
#define WIFI_PROTOCOL_MESSAGE_PIN_RENEW                 0x04
#define WIFI_PROTOCOL_MESSAGE_PIN_DEL                   0x05
#define WIFI_PROTOCOL_MESSAGE_PIN_QUERY                 0x06
#define WIFI_PROTOCOL_MESSAGE_CHARGING_START            0x11
#define WIFI_PROTOCOL_MESSAGE_CHARGING_STOP             0x12
#define WIFI_PROTOCOL_MESSAGE_STATUS_QUERY              0x13
#define WIFI_PROTOCOL_MESSAGE_RESERVATION               0x14
#define WIFI_PROTOCOL_MESSAGE_TIME_QUERY                0x21
#define WIFI_PROTOCOL_MESSAGE_TIME_CONFIG               0x22
#define WIFI_PROTOCOL_MESSAGE_LED_QUERY                 0x23
#define WIFI_PROTOCOL_MESSAGE_LED_CONFIG                0x24
#define WIFI_PROTOCOL_MESSAGE_CURRENT_LIMIT_QUERY       0x25
#define WIFI_PROTOCOL_MESSAGE_CURRENT_LIMIT_CONFIG      0x26
#define WIFI_PROTOCOL_MESSAGE_FIRMWARE_VERSION_QUERY    0x31
#define WIFI_PROTOCOL_MESSAGE_FIRMWARE_UPDATE_REQ       0x32
#define WIFI_PROTOCOL_MESSAGE_FIRMWARE_TRANS_REQ        0x33
#define WIFI_PROTOCOL_MESSAGE_FIRMWARE_END_REQ          0x34

/* USER CODE END Private defines */

void MX_UART5_Init(void);
void MX_USART1_UART_Init(void);
void MX_USART2_UART_Init(void);
void MX_USART3_UART_Init(void);
void MX_USART6_UART_Init(void);

/* USER CODE BEGIN Prototypes */
void serialPutString(uint8_t *fmt, ...);

extern uint8_t UART_RFID_rx_buffer[UART_BUFFER_SIZE>>4];
extern uint8_t UART_RFID_recv_end_flag;
extern uint16_t UART_RFID_rx_len;

#ifdef FUNC_RS485_SLAVE
//huart2
extern uint8_t UART_RS485_rx_buffer[UART_BUFFER_SIZE];
extern uint8_t UART_RS485_recv_end_flag;
extern uint16_t UART_RS485_rx_len;
#endif

//huart3
extern uint8_t UART_IAP_rx_buffer[UART_BUFFER_SIZE];
extern uint8_t UART_IAP_recv_end_flag;
extern uint16_t UART_IAP_rx_len;
extern uint8_t isUSB_CDC_IAP;

extern uint8_t UART_BLE_rx_buffer[UART_BUFFER_SIZE];
extern uint8_t UART_BLE_recv_end_flag;
extern uint16_t UART_BLE_rx_len;
extern uint32_t UART_BLE_Receive_Tick;
extern uint8_t UART_BLE_Init_OK;

extern uint8_t UART_WIFI_rx_buffer[UART_BUFFER_SIZE];
extern uint8_t UART_WIFI_recv_end_flag;
extern uint16_t UART_WIFI_rx_len;
extern uint32_t UART_WIFI_Receive_Tick;
extern uint8_t UART_WIFI_Init_OK;

extern char *BLE_CMD[];
extern char *WIFI_CMD[];
/* USER CODE END Prototypes */

#ifdef __cplusplus
}
#endif
#endif /*__ usart_H */

/**
  * @}
  */

/**
  * @}
  */

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