/*
 * bsp_UART.c
 *
 *  Created on: Oct 26, 2023
 *      Author: shiya
 */
#include "BSP/bsp_UART.h"

uint8_t RS485_1_TxDMA_buf[UART_Transmit_MAX_NUM];
uint8_t RS485_2_TxDMA_buf[UART_Transmit_MAX_NUM];
uint8_t RS485_3_TxDMA_buf[UART_Transmit_MAX_NUM];
uint8_t RS485_4_TxDMA_buf[UART_Transmit_MAX_NUM];
uint8_t Debug_TxDMA_buf[UART_Transmit_MAX_NUM];
uint8_t ExternSerial_TxDMA_buf[UART_Transmit_MAX_NUM];

uint8_t GF_BSP_UART_Init(void)
{
	return 1;
}

void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
	if(huart->Instance == UART4)			//**DEBUG
	{
		//HAL_UART_Transmit_DMA(&huart4, NC_Test, 1);
		//HAL_UART_Receive_IT(&huart4,NC_Test,1);
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_DEBUG_RxCpltCallback);
	}
	else if(huart->Instance == USART1)		//**RS485_1
	{
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_1_RxCpltCallback);
//		HAL_UART_Transmit_DMA(&huart1, NC_Test, 1);
//		HAL_UART_Receive_IT(&huart1,NC_Test,1);
	}
	else if(huart->Instance == USART3)		//**RS485_2
	{
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_2_RxCpltCallback);
	}
	else if(huart->Instance == USART6)		//**RS485_3
	{
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_3_RxCpltCallback);
	}
	else if(huart->Instance == UART7)		//**RS485_4
	{
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_RS485_4_RxCpltCallback);
	}
	else if(huart->Instance == USART2)		//**E22	// External serial port
	{
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_E22_Serial_RxCpltCallback);
	}
	else if(huart->Instance == UART5)		//**E28	// SBUS
	{
		GF_BSP_Interrupt_Run_CallBack(DF_BSP_InterCall_E28_SBUS_RxFifo0Callback);
	}
}

void HAL_UART_TxCpltCallback(UART_HandleTypeDef *huart)
{
	{
		if(huart->Instance == UART4)			//**DEBUG
		{

		}
		else if(huart->Instance == USART1)		//**RS485_1
		{
			HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_RESET);
		}
		else if(huart->Instance == USART3)		//**RS485_2
		{
			HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_RESET);
		}
		else if(huart->Instance == USART6)		//**RS485_3
		{
			HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_RESET);
		}
		else if(huart->Instance == UART7)		//**RS485_4
		{
			HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_RESET);
		}
		else if(huart->Instance == USART2)		//**E22	// External serial port
		{
			HAL_GPIO_WritePin(E22_RST_GPIO_Port, E22_RST_Pin, GPIO_PIN_RESET);
		}
		else if(huart->Instance == UART5)		//**E28	// SBUS
		{

		}
	}
}

//RS485_Index:1-4 RS485; 5 ExternSerial; 6 Debug;
void GF_BSP_UART_Transmit(const uint8_t RS485_Index,const uint8_t *pData, uint16_t Size)
{
	UART_HandleTypeDef *huart;
	if(Size>UART_Transmit_MAX_NUM) Size=UART_Transmit_MAX_NUM;

	switch(RS485_Index)
	{
	case 1:
		huart=&huart1;
		HAL_GPIO_WritePin(RS485_1_DIR_GPIO_Port, RS485_1_DIR_Pin, GPIO_PIN_SET);
		memcpy(RS485_1_TxDMA_buf,pData,Size);
		SCB_CleanInvalidateDCache_by_Addr((uint32_t *)RS485_1_TxDMA_buf, Size);
		HAL_UART_Transmit_DMA(huart, RS485_1_TxDMA_buf, Size);
	break;
	case 2:
		huart=&huart3;
		HAL_GPIO_WritePin(RS485_2_DIR_GPIO_Port, RS485_2_DIR_Pin, GPIO_PIN_SET);
		memcpy(RS485_2_TxDMA_buf,pData,Size);
		SCB_CleanInvalidateDCache_by_Addr((uint32_t *)RS485_2_TxDMA_buf, Size);
		HAL_UART_Transmit_DMA(huart, RS485_2_TxDMA_buf, Size);
	break;
	case 3:
		huart=&huart6;
		HAL_GPIO_WritePin(RS485_3_DIR_GPIO_Port, RS485_3_DIR_Pin, GPIO_PIN_SET);
		memcpy(RS485_3_TxDMA_buf,pData,Size);
		SCB_CleanInvalidateDCache_by_Addr((uint32_t *)RS485_3_TxDMA_buf, Size);
		HAL_UART_Transmit_DMA(huart, RS485_3_TxDMA_buf, Size);
	break;
	case 4:
		huart=&huart7;
		HAL_GPIO_WritePin(RS485_4_DIR_GPIO_Port, RS485_4_DIR_Pin, GPIO_PIN_SET);
		memcpy(RS485_4_TxDMA_buf,pData,Size);
		SCB_CleanInvalidateDCache_by_Addr((uint32_t *)RS485_4_TxDMA_buf, Size);
		HAL_UART_Transmit_DMA(huart, RS485_4_TxDMA_buf, Size);
	break;
	case 5:
		huart=&huart2;
		memcpy(ExternSerial_TxDMA_buf,pData,Size);
		SCB_CleanInvalidateDCache_by_Addr((uint32_t *)ExternSerial_TxDMA_buf, Size);
		HAL_UART_Transmit_DMA(huart, ExternSerial_TxDMA_buf, Size);
	break;
	default:
		huart=&huart4;
		memcpy(Debug_TxDMA_buf,pData,Size);
		SCB_CleanInvalidateDCache_by_Addr((uint32_t *)Debug_TxDMA_buf, Size);
		HAL_UART_Transmit_DMA(huart, Debug_TxDMA_buf, Size);
	break;
	}
}
