/**
 * @file main.c
 * @author WHXY
 * @brief
 * @version 0.1
 * @date 2022-01-12
 *
 * @copyright Copyright (c) 2022
 *
 */
/*******************************************************************************
*
* 代码许可和免责信息
* 武汉芯源半导体有限公司授予您使用所有编程代码示例的非专属的版权许可，您可以由此
* 生成根据您的特定需要而定制的相似功能。根据不能被排除的任何法定保证，武汉芯源半
* 导体有限公司及其程序开发商和供应商对程序或技术支持（如果有）不提供任何明示或暗
* 含的保证或条件，包括但不限于暗含的有关适销性、适用于某种特定用途和非侵权的保证
* 或条件。
* 无论何种情形，武汉芯源半导体有限公司及其程序开发商或供应商均不对下列各项负责，
* 即使被告知其发生的可能性时，也是如此：数据的丢失或损坏；直接的、特别的、附带的
* 或间接的损害，或任何后果性经济损害；或利润、业务、收入、商誉或预期可节省金额的
* 损失。
* 某些司法辖区不允许对直接的、附带的或后果性的损害有任何的排除或限制，因此某些或
* 全部上述排除或限制可能并不适用于您。
*
*******************************************************************************/
/******************************************************************************
 * Include files
 ******************************************************************************/
#include "main.h"
/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/
//UARTy
#define  UARTy                         CW_UART1
#define  UARTy_CLK                     RCC_APB2_PERIPH_UART1
#define  UARTy_APBClkENx               RCC_APBPeriphClk_Enable2

#define  UARTy_GPIO_CLK                RCC_AHB_PERIPH_GPIOA
#define  UARTy_GPIO                    CW_GPIOA
#define  UARTy_TxPin                   GPIO_PIN_8
#define  UARTy_RxPin                   GPIO_PIN_9
#define  UARTy_AFTX                    PA08_AFx_UART1TXD()
#define  UARTy_AFRX                    PA09_AFx_UART1RXD()

//UARTz
#define  UARTz                         CW_UART2
#define  UARTz_CLK                     RCC_APB1_PERIPH_UART2
#define  UARTz_APBClkENx               RCC_APBPeriphClk_Enable1

#define  UARTz_GPIO_CLK                RCC_AHB_PERIPH_GPIOA
#define  UARTz_GPIO                    CW_GPIOA
#define  UARTz_TxPin                   GPIO_PIN_6
#define  UARTz_RxPin                   GPIO_PIN_7
#define  UARTz_AFTX                    PA06_AFx_UART2TXD()
#define  UARTz_AFRX                    PA07_AFx_UART2RXD()

#define  UARTyz_BaudRate               9600
#define  UARTyz_UclkFreq               8000000

//中断
#define  UARTz_IRQ                     UART2_IRQn

#define  BufferSize                    ARRAY_SZ(TxBuffer1)

/******************************************************************************
 * Global variable definitions (declared in header file with 'extern')
 ******************************************************************************/

/******************************************************************************
 * Local type definitions ('typedef')
 ******************************************************************************/
typedef enum {FAILED = 0, PASSED = !FAILED} TestStatus;
/******************************************************************************
 * Local function prototypes ('static')
 ******************************************************************************/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void UART_Configuration(void);
void NVIC_Configuration(void);
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength);

/******************************************************************************
 * Local variable definitions ('static')                                      *
 ******************************************************************************/
uint8_t TxBuffer1[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
                       0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
                       0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
                       0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
                       0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23
                      };
uint8_t TxBuffer2[] = {0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67,
                       0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E,
                       0x6F, 0x70, 0x71, 0x72, 0x73, 0x74, 0x75,
                       0x76, 0x77, 0x78, 0x79, 0x7A, 0x7B, 0x7C,
                       0x7D, 0x7E, 0x7F, 0x80, 0x81, 0x82, 0x83
                      };
uint8_t RxBuffer1[BufferSize];
uint8_t RxBuffer2[BufferSize];
uint8_t TxCounter = 0, RxCounter = 0;
volatile TestStatus TransferStatus1 = FAILED, TransferStatus2 = FAILED;

uint8_t sADDR = 0xA5;
uint8_t sMASK = 0xFF;
/******************************************************************************
 * Local pre-processor symbols/macros ('#define')
 ******************************************************************************/

/*****************************************************************************
 * Function implementation - global ('extern') and local ('static')
 ******************************************************************************/

/**
 ******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 ******************************************************************************/
int32_t main(void)
{
    //配置RCC
    RCC_Configuration();

    //配置GPIO
    GPIO_Configuration();

    //配置UART
    UART_Configuration();

    //配置NVIC
    NVIC_Configuration();

    //UARTz RC中断
    UART_ITConfig(UARTz, UART_IT_RC, ENABLE);

    //配置多机通信 UARTz
    UART_SetMultiMode(UARTz, sADDR, sMASK);

    /**********************UARTy -> UARTz*************************/
    //UARTy发送地址帧，寻址到UARTz
    UART_SendData(UARTy, 0x100 | sADDR);
    while(UART_GetFlagStatus(UARTy, UART_FLAG_TXE) == RESET);

    while(TxCounter < BufferSize)
    {
        //UARTy 发送
        UART_SendData(UARTy, TxBuffer1[TxCounter]);
        while(UART_GetFlagStatus(UARTy, UART_FLAG_TXE) == RESET);

        //UARTZ 中断接收

        //UARTy 接收
        while(UART_GetFlagStatus(UARTy, UART_FLAG_RC) == RESET);
        RxBuffer1[RxCounter++] = UART_ReceiveData(UARTy);
        UART_ClearFlag(UARTy, UART_FLAG_RC);
    }

    //检查收发数据一致性
    TransferStatus1 = Buffercmp(TxBuffer1, RxBuffer2, BufferSize);
    TransferStatus2 = Buffercmp(TxBuffer2, RxBuffer1, BufferSize);

    if(TransferStatus1 == PASSED && TransferStatus2 == PASSED) //PASSED
    {
        //LED1亮
        PA15_SETHIGH();
    }
    else //FAILED
    {
        //LED2亮
        PC10_SETHIGH();
    }

    while(1)
    {

    }
}

/**
 * @brief 配置RCC
 *
 */
void RCC_Configuration(void)
{
    //SYSCLK = HSI = 8MHz = HCLK = PCLK
    RCC_HSI_Enable(RCC_HSIOSC_DIV6);

    //外设时钟使能
    RCC_AHBPeriphClk_Enable(UARTy_GPIO_CLK | UARTz_GPIO_CLK | RCC_AHB_PERIPH_GPIOC, ENABLE);
    UARTy_APBClkENx(UARTy_CLK, ENABLE);
    UARTz_APBClkENx(UARTz_CLK, ENABLE);
}

/**
 * @brief 配置GPIO
 *
 */
void GPIO_Configuration(void)
{
    GPIO_InitTypeDef GPIO_InitStructure = {0};

    //UART TX RX 复用
    UARTy_AFTX;
    UARTy_AFRX;
    UARTz_AFTX;
    UARTz_AFRX;

    GPIO_InitStructure.Pins = UARTy_TxPin;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_Init(UARTy_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = UARTz_TxPin;
    GPIO_Init(UARTz_GPIO, &GPIO_InitStructure);

    //打开GPIO时钟
    REGBITS_SET(CW_SYSCTRL->AHBEN, SYSCTRL_AHBEN_GPIOA_Msk | SYSCTRL_AHBEN_GPIOC_Msk);

    /* Configure the GPIO_LED pin */
    GPIO_InitStructure.Pins = GPIO_PIN_10;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_Init(CW_GPIOC, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = GPIO_PIN_15;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
    GPIO_Init(CW_GPIOA, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = UARTy_RxPin;
    GPIO_InitStructure.Mode = GPIO_MODE_INPUT_PULLUP;
    GPIO_Init(UARTy_GPIO, &GPIO_InitStructure);

    GPIO_InitStructure.Pins = UARTz_RxPin;
    GPIO_Init(UARTz_GPIO, &GPIO_InitStructure);

    //LED灭
    PA15_SETLOW();
    PC10_SETLOW();
}

/**
 * @brief 配置UART
 *
 */
void UART_Configuration(void)
{
    UART_InitTypeDef UART_InitStructure = {0};

    UART_InitStructure.UART_BaudRate = UARTyz_BaudRate;
    UART_InitStructure.UART_Over = UART_Over_16;
    UART_InitStructure.UART_Source = UART_Source_PCLK;
    UART_InitStructure.UART_UclkFreq = UARTyz_UclkFreq;
    UART_InitStructure.UART_StartBit = UART_StartBit_FE;
    UART_InitStructure.UART_StopBits = UART_StopBits_1;
    UART_InitStructure.UART_Parity = UART_Parity_User ;  //自定义校验
    UART_InitStructure.UART_HardwareFlowControl = UART_HardwareFlowControl_None;
    UART_InitStructure.UART_Mode = UART_Mode_Rx | UART_Mode_Tx;
    UART_Init(UARTy, &UART_InitStructure);
    UART_Init(UARTz, &UART_InitStructure);
}

/**
 * @brief 配置NVIC
 *
 */
void NVIC_Configuration(void)
{
    //优先级，无优先级分组
    NVIC_SetPriority(UARTz_IRQ, 0);
    //UARTx中断使能
    NVIC_EnableIRQ(UARTz_IRQ);
}

/**
 * @brief Compares two buffers
 *
 * @param pBuffer1 :buffers to be compared
 * @param pBuffer2 :buffers to be compared
 * @param BufferLength :buffer's length
 * @return TestStatus
 *     @arg PASSED: pBuffer1 identical to pBuffer2
 *     @arg FAILED: pBuffer1 differs from pBuffer2
 */
TestStatus Buffercmp(uint8_t* pBuffer1, uint8_t* pBuffer2, uint16_t BufferLength)
{
    while(BufferLength--)
    {
        if(*pBuffer1 != *pBuffer2)
        {
            return FAILED;
        }

        pBuffer1++;
        pBuffer2++;
    }

    return PASSED;
}

/******************************************************************************
 * EOF (not truncated)
 ******************************************************************************/
#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 */

