/******************************************************************************
 * @file      gpio_manager.c
 * @brief     初始化gpio接口配置
 * @version   
 * @copyright    
 ******************************************************************************/
#include "gpio_manager.h"
#include "stm32f4xx.h"

//#define TX_DMA_BUFFER_SIZE 800

extern uint8_t dma_tx_buffer[];
extern volatile uint8_t dma_tx_busy;
extern volatile uint8_t current_buffer;

const GpioArray io_array[kGpioType_End] = 
{
	/*12个DI*/
    kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_E, 9),	kGpioPort_E,	kGpioPin_9 ,	//DI1 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_G, 1),	kGpioPort_G,	kGpioPin_1 ,	//DI2 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_G, 0),	kGpioPort_G,	kGpioPin_0 ,	//DI3 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_F, 15),	kGpioPort_F,	kGpioPin_15 ,	//DI4 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_F, 11),	kGpioPort_F,	kGpioPin_11 ,	//DI5 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_F, 12),	kGpioPort_F,	kGpioPin_12 ,	//DI6 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_F, 13),	kGpioPort_F,	kGpioPin_13 ,	//DI7 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_F, 14),	kGpioPort_F,	kGpioPin_14 ,	//DI8 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_C, 4),	kGpioPort_C,	kGpioPin_4 ,	//DI9 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_C, 5),	kGpioPort_C,	kGpioPin_5 ,	//DI10 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 0),	kGpioPort_B,	kGpioPin_0 ,	//DI11 低电平输入信号检测
	kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 1),	kGpioPort_B,	kGpioPin_1 ,	//DI12 低电平输入信号检测

	/*10个DO*/
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 4),	kGpioPort_G,	kGpioPin_4 ,	    //HSD_Ctrl_1 高边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 5),	kGpioPort_G,	kGpioPin_5 ,	    //HSD_Ctrl_2 高边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 6),	kGpioPort_G,	kGpioPin_6 ,	    //HSD_Ctrl_3 高边控制1
	
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 7),	kGpioPort_G,	kGpioPin_7 ,	    //HSD_Ctrl_4 高边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_D, 10),	kGpioPort_D,	kGpioPin_10 ,	    //HSD_Ctrl_5 高边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_D, 11),	kGpioPort_D,	kGpioPin_11 ,	    //HSD_Ctrl_6 高边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_D, 12),	kGpioPort_D,	kGpioPin_12 ,	    //HSD_Ctrl_7 高边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_D, 13),	kGpioPort_D,	kGpioPin_13 ,	    //HSD_Ctrl_8 高边控制1
	
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 8),	kGpioPort_G,	kGpioPin_8 ,	    //LSD_Ctrl_9  低边控制1
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 8),	kGpioPort_A,	kGpioPin_8 ,	    //HSD_Ctrl_10 低边控制1
	
	/*RS485枚举*/
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 10),	kGpioPort_G,	kGpioPin_10 ,	//RS485C 发送使能
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 9),	kGpioPort_G,	kGpioPin_9 ,	//RS485B 发送使能
	kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 11),	kGpioPort_G,	kGpioPin_11 ,	//RS485A 发送使能
	
	kGpioMode_Comm_Rx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 10),	kGpioPort_A, kGpioPin_10 ,  //rs485C Rx(mcu uart1)
	kGpioMode_Comm_Tx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 9),	kGpioPort_A, kGpioPin_9 ,  //rs485C Tx(mcu uart1)
	
	kGpioMode_Comm_Rx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_D, 9),	kGpioPort_D, kGpioPin_9 ,  //rs485B Rx(mcu uart1)
	kGpioMode_Comm_Tx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_D, 8),	kGpioPort_D, kGpioPin_8 ,  //rs485B Tx(mcu uart1)
	
	kGpioMode_Comm_Rx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 1),	kGpioPort_A, kGpioPin_1 ,  //rs485A Rx(mcu uart1)
	kGpioMode_Comm_Tx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 0),	kGpioPort_A, kGpioPin_0 ,  //rs485A Tx(mcu uart1)
	
	kGpioMode_Comm_Rx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 3),	kGpioPort_A, kGpioPin_3,   //Sim卡/wifi/ble Rx(mcu uart2)
	kGpioMode_Comm_Tx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 2),	kGpioPort_A, kGpioPin_2,   //Sim卡/wifi/ble Tx(mcu uart2)
	
	/*CAN枚举*/
	kGpioMode_Comm_Rx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 11),	kGpioPort_A, kGpioPin_11 ,	//can1 rx 
	kGpioMode_Comm_Tx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_A, 12),	kGpioPort_A, kGpioPin_12 ,	//can1 tx 
	kGpioMode_Comm_Rx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_B, 12),	kGpioPort_B, kGpioPin_12 ,	//can2 rx 
	kGpioMode_Comm_Tx,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_B, 13),	kGpioPort_B, kGpioPin_13 ,	//can2 tx 
	
    /*绝缘监测开关--待确认*/
  	kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_E, 15),	kGpioPort_E, kGpioPin_15 ,  	//insu_mos_S1 ISL_EN_MCU 绝缘检测PE开关驱动信号
    kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_E, 14),	kGpioPort_E, kGpioPin_15 ,  	//insu_mos_S2 ISL_HS_MCU 绝缘检测上位开关驱动信号
    kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_E, 13),	kGpioPort_E, kGpioPin_15 ,  	//insu_mos_S3 ISL_LS_MCU 绝缘检测下位开关驱动信号
	
	/*ADC枚举*/
    kGpioMode_Adc,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_F, 7),	kGpioPort_F, kGpioPin_7 ,	//24 电压采样信号
    kGpioMode_Adc,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_F, 6),	kGpioPort_F, kGpioPin_6 ,	//板载温度 NTC
    kGpioMode_Adc,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_F, 9),	kGpioPort_F, kGpioPin_9 ,	//霍尔采集ADC

	/*菊花链采集*/
	kGpioMode_Comm_Tx,		kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_A, 7),	kGpioPort_A, kGpioPin_7 ,	//LT6820_MOSI   SPI1通讯信号
	kGpioMode_Comm_Rx,		kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_A, 6),	kGpioPort_A, kGpioPin_6 ,	//LT6820_MISO	SPI1通讯信号
	kGpioMode_Comm_Tx,		kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_A, 5),	kGpioPort_A, kGpioPin_5 ,	//LT6820_SCK	SPI1通讯信号
	kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_A, 4),	kGpioPort_A, kGpioPin_4 ,	//LT6820 En1
	kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_E, 10),	kGpioPort_E, kGpioPin_10 ,	//LT6820 En2
	
	/*高压采集*/
	kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_G, 13),	kGpioPort_G, kGpioPin_13 ,		//ADC复位信号 低电平
	kGpioMode_Comm_Tx,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 15),		kGpioPort_B, kGpioPin_15 ,		//ADC_MOSI  SPI2通讯信号
	kGpioMode_Comm_Rx,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 14),		kGpioPort_B, kGpioPin_14 ,		//ADC_MISO	 SPI2通讯信号
	kGpioMode_Comm_Tx,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 10),		kGpioPort_B, kGpioPin_10 ,		//ADC_SCK   SPI2通讯信号
	kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 11),	kGpioPort_B, kGpioPin_11 ,		//ADC_CS    SPI2通讯信号
    
	/*网口*/
    kGpioMode_Comm_Tx,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 5),			kGpioPort_B, kGpioPin_5 ,	//W5500_MOSI  
	kGpioMode_Comm_Rx,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 4),			kGpioPort_B, kGpioPin_4 ,	//W5500_MISO
	kGpioMode_Comm_Tx,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_B, 3),			kGpioPort_B, kGpioPin_3 ,	//W5500_SCK
	kGpioMode_Output_PP,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_A, 15),		kGpioPort_A, kGpioPin_15 ,	//W5500_CS
    kGpioMode_Output_PP,	kGpioStatus_Low,	GPIO_PORT_PIN(kGpioPort_G, 14),		kGpioPort_G, kGpioPin_14 ,	//W5500_RST
    kGpioMode_Input_Floating,	kGpioStatus_High,	GPIO_PORT_PIN(kGpioPort_G, 15),	kGpioPort_G, kGpioPin_15 ,	//W5500_INT
};

void reset_dma_tx_state(void)
{
    __disable_irq();
    dma_tx_busy = 0;
    current_buffer = 0;
    __enable_irq();
}

void drv_wireless_dma_init(void)
{
    // 使能 DMA1 和 USART2 的时钟
    RCC->AHB1ENR |= RCC_AHB1ENR_DMA1EN;
    RCC->APB1ENR |= RCC_APB1ENR_USART2EN;

    // 配置 USART2 基本参数
		USART2->CR1 &= ~USART_CR1_UE; // 先禁用USART
    USART2->BRR = SystemCoreClock  / 115200; // 设置波特率
    USART2->CR1 = USART_CR1_TE | USART_CR1_UE;

    // 配置 DMA1 Stream6 通道4 对应 USART2_TX
    DMA1_Stream6->CR &= ~DMA_SxCR_EN;
    while (DMA1_Stream6->CR & DMA_SxCR_EN); // 等待 DMA 关闭

    DMA1_Stream6->PAR = (uint32_t)&USART2->DR;
    DMA1_Stream6->M0AR = (uint32_t)dma_tx_buffer;
    DMA1_Stream6->NDTR = 0;
		DMA1_Stream6->CR = (4 << 25) |    // 通道4
                  DMA_SxCR_MINC |  // 内存递增
                  DMA_SxCR_DIR_0 | // 内存到外设
                  DMA_SxCR_TCIE |  // 传输完成中断
                  DMA_SxCR_TEIE |  // 建议添加传输错误中断
                  (0 << 16);       // 优先级

		reset_dma_tx_state();
    
    DMA1_Stream6->CR |= (1 << 0);  // 启用FIFO
    DMA1_Stream6->FCR = (0x3 << 0); // 全FIFO模式

    NVIC_EnableIRQ(DMA1_Stream6_IRQn);
}

void bms_init_gpio(void)
{
    drv_gpio_init(io_array, kGpioType_End);
		drv_wireless_dma_init();
}


