

/**
 ******************************************************************************
 *
 * @file        Sample_URT2_UART_Init.c
 * @brief       The demo UART2'S initial sample C file.
 *
 * @par         Project
 *              MG32
 * @version     V1.14
 * @date        2025/08/07
 * @author      Megawin Software Center
 * @copyright   Copyright (c) 2017 MegaWin Technology Co., Ltd.
 *              All rights reserved.
 *  
 ******************************************************************************* 
 * @par Disclaimer
 * The Demo software is provided "AS IS" without any warranty, either
 * expressed or implied, including, but not limited to, the implied warranties
 * of merchantability and fitness for a particular purpose. The author will
 * not be liable for any special, incidental, consequential or indirect
 * damages due to loss of data or any other reason.
 * These statements agree with the world wide and local dictated laws about
 * authorship and violence against these laws.
 *******************************************************************************
 *******************************************************************************
 */
 
 
/* Includes ------------------------------------------------------------------*/
#include "MG32__Common_DRV.h"
#include "MG32_URT_DRV.h"
#include "MG32_EXIC_DRV.h"

/* Wizard menu ---------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define URTX    URT2

#define EXIC_URT123_ID  21
#define EXIC_URT1       0x01
#define EXIC_URT2       0x02
#define EXIC_URT3       0x04
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Sample_URT2_Init(void);
void URT123_IRQHandler(void);

/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/* External vairables --------------------------------------------------------*/

 /**
 *******************************************************************************
 * @brief	   Sample URT2 initial
 * @details     1. Set CSC initial.
 *     \n       2. Set GPIO initial.
 *     \n       3. Set RX/TX Baudrate.
 *     \n       4. Set data character.
 *     \n       5. Set URT2 mode.
 *     \n       6. Set Data line.
 *     \n       7. Set Data buffer control.
 *     \n       8. Enable URT2 interrupt.
 *     \n       9. Enable URT2 
 *     \n      10. Receive data into interrupt function.
 *     \n      11. Send data of receive.
 * @return      
 * @exception   No
 * @note
 * @par         Example
 * @code
 * @endcode
 *******************************************************************************
 */
void Sample_URT2_Init(void)
{
    URT_BRG_TypeDef  URT_BRG;
    URT_Data_TypeDef DataDef;
    
    //==Set CSC init
    //MG32x02z_CSC_Init.h(Configuration Wizard)
    //Select CK_HS source = CK_IHRCO
    //Select IHRCO = 11.0592M
    //Select CK_MAIN Source = CK_HS
    //Configure Select APB Prescaler = CK_MAIN/1
    //Configure Peripheral On Mode Clock->Port B/URT2 = Enable
    //Configure Peripheral On Mode Clock->URT2->Select URT2_PR Source = CK_APB(11.0592)
    
    //==Set GPIO init
    //MG32_GPIO_Init.h(Configuration Wizard)->Use GPIOB->Pin2/3
    //GPIO port initial is 0xFFFF
    //PB2 mode is PPO/PB3 mode is DIN
    //PB3 pull-up resister Enable
    //PB2/PB3 function URT2_TX/RX
    //(URT2 TX/RX pin refer to related data.)    
    
    //=====Set Clock=====//
    //---Set BaudRate---//
    URT_BRG.URT_InternalClockSource    = URT_BDClock_PROC;
    URT_BRG.URT_BaudRateMode           = URT_BDMode_Separated;
    URT_BRG.URT_PrescalerCounterReload = 0;                   //Set PSR
    URT_BRG.URT_BaudRateCounterReload  = 11;                  //Set RLR
    URT_BaudRateGenerator_Config(URTX, &URT_BRG);             //BR115200 = f(CK_URTx)/(PSR+1)/(RLR+1)/(OS_NUM+1)
    URT_BaudRateGenerator_Cmd(URTX, ENABLE);                  //Enable BaudRateGenerator
    //---TX/RX Clock---//
    URT_TXClockSource_Select(URTX, URT_TXClock_Internal);	  //URT_TX use BaudRateGenerator
    URT_RXClockSource_Select(URTX, URT_RXClock_Internal);	  //URT_RX use BaudRateGenerator
    URT_TXOverSamplingSampleNumber_Select(URTX, 7);	          //Set TX OS_NUM
    URT_RXOverSamplingSampleNumber_Select(URTX, 7);	          //Set RX OS_NUM
    URT_RXOverSamplingMode_Select(URTX, URT_RXSMP_3TIME);     
    URT_TX_Cmd(URTX, ENABLE);	                              //Enable TX
    URT_RX_Cmd(URTX, ENABLE);	                              //Enable RX

    //=====Set Mode=====//
    //---Set Data character config---//
    DataDef.URT_TX_DataLength  = URT_DataLength_8;
    DataDef.URT_RX_DataLength  = URT_DataLength_8;
    DataDef.URT_TX_DataOrder   = URT_DataTyped_LSB;
    DataDef.URT_RX_DataOrder   = URT_DataTyped_LSB;
    DataDef.URT_TX_Parity      = URT_Parity_No;
    DataDef.URT_RX_Parity      = URT_Parity_No;
    DataDef.URT_TX_StopBits    = URT_StopBits_1_0;
    DataDef.URT_RX_StopBits    = URT_StopBits_1_0;
    DataDef.URT_TX_DataInverse = DISABLE;
    DataDef.URT_RX_DataInverse = DISABLE;
    URT_DataCharacter_Config(URTX, &DataDef);
    //---Set Mode Select---//
    URT_Mode_Select(URTX, URT_URT_mode);
    //---Set DataLine Select---//
    URT_DataLine_Select(URTX, URT_DataLine_2);
    
    //=====Set Error Control=====//
    // to do...
    
    //=====Set Bus Status Detect Control=====//
    // to do...
    
    //=====Set Data Control=====//
    URT_RXShadowBufferThreshold_Select(URTX, URT_RXTH_1BYTE);
    URT_IdlehandleMode_Select(URTX, URT_IDLEMode_No);
    URT_TXGuardTime_Select(URTX, 0);
    
    //=====Enable URT Interrupt=====//
    URT_IT_Config(URTX, URT_IT_RX, ENABLE);
    URT_ITEA_Cmd(URTX, ENABLE);
    NVIC_EnableIRQ(URT123_IRQn);

    //=====Enable URT=====//
    URT_Cmd(URTX, ENABLE);
}


/**
 *******************************************************************************
 * @brief	    URT123 IRQHandler 
 * @details     
 * @return      
 * @exception   No
 * @note 
 *******************************************************************************
 */
//void URT123_IRQHandler(void)
//{
//    uint32_t URT123_IRQHandler_ITFlag;
//    uint8_t  URT123_IRQHandler_BLen;
//    uint32_t URT123_IRQHandler_Tmp;
//    uint8_t  URT123_IRQHandler_IDTmp;
//    
//    URT123_IRQHandler_IDTmp = EXIC_GetITSourceID(EXIC_URT123_ID);
//    
//    if( URT123_IRQHandler_IDTmp & EXIC_URT2)
//    {
//        URT123_IRQHandler_ITFlag = URT_GetITAllFlagStatus(URTX) & URT_GetITStatus(URTX);
//        
//        if( URT123_IRQHandler_ITFlag & URT_IT_RX)
//        {
//            URT123_IRQHandler_BLen = URT_GetRXDataRegLevel(URTX);
//            URT123_IRQHandler_Tmp  = URT_GetRXData(URTX);
//        
//            URT_SetTXData(URTX,URT123_IRQHandler_BLen,URT123_IRQHandler_Tmp);
//        }
//    }
//}















