/**
  ******************************************************************************
 *
 * @file        Sample_RST_TriggerReset.c
 *
 * @brief       RST module reset sample code.
 *
 * @par         Project
 *              MG32
 * @version     V1.02
 * @date        2023/03/30
 * @author      Megawin Software Center
 * @copyright   Copyright (c) 2019 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_DRV.h"

/* Wizard menu ---------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Sample_RST_TriggerReset (void);
void Sample_URT0_Init(void);
void printf_mg( const char *format,...);
/* Exported variables --------------------------------------------------------*/
/* Exported functions --------------------------------------------------------*/
/* External vairables --------------------------------------------------------*/



/**
 *******************************************************************************
 * @brief	   Sample URT0 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 URT0 mode.
 *     \n       6. Set Data line.
 *     \n       7. Set Data buffer control.
 *     \n       8. Enable URT0 interrupt.
 *     \n       9. Enable URT0 
 *     \n      10. Receive data into interrupt function.
 *     \n      11. Send data of receive.
 * @return      
 * @exception   No
 * @note
 * @par         Example
 * @code
 * @endcode
 *******************************************************************************
 */
void Sample_URT0_Init(void)
{
    URT_BRG_TypeDef  URT_BRG;
    URT_Data_TypeDef DataDef;
    PIN_InitTypeDef PINX_InitStruct;
 
    
    
    //==Set CSC init
    //MG32_CSC_Init.h(Configuration Wizard)
    //Select CK_HS source = CK_IHRCO
    //Select IHRCO = 11.0592M
    //Select CK_MAIN Source = CK_HS
    //Configure PLL->Select APB Prescaler = CK_MAIN/1
    //Configure Peripheral On Mode Clock->Port B/URT0 = Enable
    //Configure Peripheral On Mode Clock->URT0->Select URT0_PR Source = CK_APB(11.0592)
    
    //==Set GPIO init
    //MG32_GPIO_Init.h(Configuration Wizard)->Use GPIOB->Pin8/9
    //GPIO port initial is 0xFFFF
    //Pin8 mode is PPO/Pin9 mode is ODO
    //Pin8/9 pull-up resister Enable
    //Pin8/9 function URT0_TX/RX
    
    PINX_InitStruct.PINX_Mode				= PINX_Mode_PushPull_O;         // Setting PB8 URT0_TX
    PINX_InitStruct.PINX_PUResistant        = PINX_PUResistant_Disable;
    PINX_InitStruct.PINX_Speed              = PINX_Speed_Low;
    PINX_InitStruct.PINX_OUTDrive           = PINX_OUTDrive_Level0;
    PINX_InitStruct.PINX_FilterDivider      = PINX_FilterDivider_Bypass;
    PINX_InitStruct.PINX_Inverse            = PINX_Inverse_Disable;
    PINX_InitStruct.PINX_Alternate_Function = 3;
    GPIO_PinMode_Config(PINB(8),&PINX_InitStruct);
    
    
    //=====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 = 155;	            //Set RLR
    URT_BaudRateGenerator_Config(URT0, &URT_BRG);   		//BR115200 = f(CK_URTx)/(PSR+1)/(RLR+1)/(OS_NUM+1)
    URT_BaudRateGenerator_Cmd(URT0, ENABLE);	            //Enable BaudRateGenerator
    //---TX/RX Clock---//
    URT_TXClockSource_Select(URT0, URT_TXClock_Internal);	//URT_TX use BaudRateGenerator
    URT_RXClockSource_Select(URT0, URT_RXClock_Internal);	//URT_RX use BaudRateGenerator
    URT_TXOverSamplingSampleNumber_Select(URT0, 7);	        //Set TX OS_NUM
    URT_RXOverSamplingSampleNumber_Select(URT0, 7);	        //Set RX OS_NUM
    URT_RXOverSamplingMode_Select(URT0, URT_RXSMP_3TIME);
    URT_TX_Cmd(URT0, ENABLE);	                            //Enable TX
    URT_RX_Cmd(URT0, 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(URT0, &DataDef);
    //---Set Mode Select---//
    URT_Mode_Select(URT0, URT_URT_mode);
    //---Set DataLine Select---//
    URT_DataLine_Select(URT0, URT_DataLine_2);
    
    //=====Set Error Control=====//
    // to do...
    
    //=====Set Bus Status Detect Control=====//
    // to do...
    
    //=====Set Data Control=====//
    URT_RXShadowBufferThreshold_Select(URT0, URT_RXTH_1BYTE);
    URT_IdlehandleMode_Select(URT0, URT_IDLEMode_No);
    URT_TXGuardTime_Select(URT0, 0);
    
    //=====Enable URT Interrupt=====//
    URT_IT_Config(URT0, URT_IT_RX, ENABLE);
    URT_ITEA_Cmd(URT0, ENABLE);
    NVIC_EnableIRQ(URT0_IRQn);

    //=====Enable URT=====//
    URT_Cmd(URT0, ENABLE);
    URT_ClearTXData(URT0);
		
	//==See MG32_URT0_IRQ.c when interrupt in
}


/**
 *******************************************************************************
 * @brief       RST_Example
 * @details     RST module reset sample code
 * 	@arg\b      1. Trigger software warm reset
 * 	@arg\b      2. Trigger software cold reset
 * 	@arg\b      3. Trigger low power STOP mode warm reset
 * 	@arg\b      4. Trigger low power STOP mode cold reset
 * 	@arg\b      5. End
 * @return
 * @note        This sample code used file "Sample_Retarget.c" function.
 *              The file "Sample_Retarget.c" must be brought in.
 * @par         Example
 * @code
    Sample_RST_TriggerReset();
 * @endcode
 *******************************************************************************
 */
void Sample_RST_TriggerReset (void)
{
    
    
    
    Sample_URT0_Init();                                         // Initial URT0
    if((SYS_ReadBackupReg() & 0xFFFF0000) != 0xA2170000)        // When BackupReg high half-word not 0xA217
    {
        SYS_WriteBackupReg(0xA2170001);                         // Write BackupReg
    }
    else                                                        // When BackupReg high half-word is 0xA217
    {
        SYS_WriteBackupReg(SYS_ReadBackupReg() + 1);            // BackupReg ++
    }
    
    /*=== 1. Trigger software warm reset ===*/
    if((SYS_ReadBackupReg() & 0x0000000F) == 0x00000001)        // When Execution order is 1.
    {
        printf_mg("\n\nWhen enable SW_WE, disable SW_CE and trigger SW reset  ");
        UnProtectModuleReg(RSTprotect);                         // Unprotect RST module 
        RST_WRstSource_Config(RST_SW_WE, ENABLE);               // Enable software reset event can trigger warm reset
        RST_CRstSource_Config(RST_SW_CE, DISABLE);              // Disable software reset event can trigger cold reset
        __ISB();
        RST_TriggerSystemRst_SW(ENABLE);                        // Trigger software reset
    }
    
    /*=== 2. Trigger software cold reset ===*/
    if((SYS_ReadBackupReg() & 0x0000000F) == 0x00000002)        // When Execution order is 2. 
    {
        printf_mg("\n\nWhen disable SW_WE, enable SW_CE and trigger SW reset  ");
        UnProtectModuleReg(RSTprotect);                         // Unprotect RST module 
        RST_WRstSource_Config(RST_SW_WE, DISABLE);              // Disable software reset event can trigger warm reset
        RST_CRstSource_Config(RST_SW_CE, ENABLE);               // Enable software reset event can trigger cold reset
        __ISB();
        RST_TriggerSystemRst_SW(ENABLE);                        // Trigger software reset
    }
    
    /*=== 3. Trigger low power STOP mode warm reset ===*/
    if((SYS_ReadBackupReg() & 0x0000000F) == 0x00000003)
    {
        printf_mg("\n\nWhen enable LPM_WE, disable LPM_CE and goto STOP mode trigger reset  ");
        UnProtectModuleReg(RSTprotect);                         // Unprotect RST module 
        RST_WRstSource_Config(RST_LPM_WE, ENABLE);              // Disable low power STOP mode event can trigger warm reset
        RST_CRstSource_Config(RST_LPM_CE, DISABLE);             // Enable low power STOP mode event can trigger cold reset
        ProtectModuleReg(RSTprotect);                           // Protect RST module 
        __ISB();
        STOP_WFI();                                             // MCU goto STOP mode
    }
    
    /*=== 4. Trigger low power STOP mode cold reset ===*/
    if((SYS_ReadBackupReg() & 0x0000000F) == 0x00000004)
    {
        printf_mg("\n\nWhen disable LPM_WE, enable LPM_CE and goto STOP mode trigger reset  ");
        UnProtectModuleReg(RSTprotect);                         // Unprotect RST module 
        RST_WRstSource_Config(RST_LPM_WE, DISABLE);             // Disable low power STOP mode event can trigger warm reset
        RST_CRstSource_Config(RST_LPM_CE, ENABLE);              // Enable low power STOP mode event can trigger cold reset
        ProtectModuleReg(RSTprotect);                           // Protect RST module 
        __ISB();
        STOP_WFI();                                             // MCU goto STOP mode
    }
    
    /*=== 5. End ===*/
    if((SYS_ReadBackupReg() & 0x0000000F) == 0x00000005)        // When Execution order is 3. 
    {    
        SYS_WriteBackupReg(0x00000000);                         // Set BackupReg = 0x00000000.
    }
}



