

/**
 ******************************************************************************
 *
 * @file        Sample_MID_URT_RX_DMA.c
 * @brief       The sample code is that use URT_MID and DMA to receive data
 *      \n      and transmit data.
 * @par         Project
 *              MG32
 * @version     V1.04
 * @date        2025/09/17
 * @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_CSC_Init.h"
#include "MG32_GPIO_MID.h"
#include "MG32_URT_MID.h"

/* Wizard menu ---------------------------------------------------------------*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
static URT_HandleTypeDef     MURT_RX_DMA;
static DMA_HandleTypeDef     DMA_URTRX;

static uint8_t dMID_URTRX_RXBUF[10];

/* Private function prototypes -----------------------------------------------*/
void DMA_IRQHandler(void);
void SysTick_Handler(void);
void Sample_MID_URT_Receive_DMA(void);

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


/**
 *******************************************************************************
 * @brief	  DMA IRQ Handle
 * @details   Call the MID_DMA_IRQHandler() function into DMA_IRQHandler() if 
 *            the DMA_IRQHandler() already  exists, or uncommenting 
 *            the DMA_IRQHandler() function in the sample code.
 * @return      
 * @exception No
 * @note      No
 *******************************************************************************
 */
#if !defined(MG32_DMA_IRQ_)
void DMA_IRQHandler(void)
{
    MID_DMA_IRQHandler(&DMA_URTRX);

} 
#endif

/**
 *******************************************************************************
 * @brief	  Initalize the URT MSP.
 * @details     
 * @param[in] MURT : URT control struct. 
 * @return      
 * @exception No
 * @note      No
 *******************************************************************************
 */
void MID_URT_MspInit(URT_HandleTypeDef* MURT)
{
    GPIO_InitTypeDef PINX_InitStruct;
    
    //==============================================================================
    //CSC Inital :
    //You can:
    //    1. Tick CSC checkbox of MG32_ChipInit in Manage Run-Time Environment.
    //    2. In MG32_ChipInit_Wizard of Project window use MG32_CSC_Init.h file
    //       to select that you want CSC environment.
    
    //==============================================================================
    //GPIO Inital : 
    //    - TX : 
    //        1. Pin : PB8 
    //        2. Mode : Push pull mode.
    //    - RX : 
    //        1. Pin : PB9
    //        2. Mode : Open drain with internal pull up R. 
    PINX_InitStruct.Mode           = GPIO_MODE_OPENDRAIN_O;
    PINX_InitStruct.Pull           = GPIO_PULLUP;
    PINX_InitStruct.Speed          = GPIO_SPEED_LOW;
    PINX_InitStruct.OUTDrive       = GPIO_OUTDRIVE_LEVEL0;
    PINX_InitStruct.FilterDivider  = GPIO_FILTERDIVIDER_BYPASS;
    PINX_InitStruct.Inverse        = GPIO_INVERSE_DISABLE;
    PINX_InitStruct.Alternate      = 3;
    MID_GPIO_Pin_Init(PINB(9),&PINX_InitStruct);
    
    PINX_InitStruct.Mode           = GPIO_MODE_PUSHPULL_O;
    MID_GPIO_Pin_Init(PINB(8),&PINX_InitStruct);
    
    //==============================================================================
    // Prevent unused argument(s) compilation warning 
    UNUSED( MURT);
    
}

/**
 *******************************************************************************
 * @brief	    The sample code is that use DMA mode to receive data.
 * @details     
 * @return      
 * @exception   No
 * @note        No
 *******************************************************************************
 */
void Sample_MID_URT_Receive_DMA(void)
{
    //==============================================================================
    //Select URT : The sample select UART0 
    MURT_RX_DMA.Instance            =  URT0;
    
    //==============================================================================
    //URT standard inital :
    //    - Baudrate.
    //    - Data Size.  
    //    - Hardware Flow Control. (CTS / RTS)
    //    - Sample time.
    //    - Parity bit.
    //    - Stop bit.
    MURT_RX_DMA.Init.BaudRate       =  115200;                            
    MURT_RX_DMA.Init.WordLength     =  UART_WORDLENGTH_8B;
    MURT_RX_DMA.Init.HWFlowCtrl     =  URT_HWCONTROL_NONE;
    MURT_RX_DMA.Init.Mode           =  URT_TX_RX;
    MURT_RX_DMA.Init.OneBitSampling =  URT_ONEBIT_SAMPLE_THREE;
    MURT_RX_DMA.Init.Parity         =  URT_PARITY_NO;
    MURT_RX_DMA.Init.StopBits       =  URT_STOPBIT_1_0;
    
    //==============================================================================
    //URT advanced inital. 
    
    //-------------------------------------------------------------
    //Exsample 1 :
    //    - No need Advanced inital.
    MURT_RX_DMA.AdvancedInit.AdvFeatureInit     = URT_ADVFEATURE_NO_INIT;
    
    
    //-------------------------------------------------------------
    //Exsample 2 :
    //    - AdvFeatureInit select to deal with item :
    //        -1. TXDataInvert : Control TX Data bit whether inverse or not.
    //        -2. RXDataInvert : Control RX Data bit whether inverse or not.
    //        -3. TXMSBFirst   : Control TX Data bit order is MSB or LSB.
    //        -4. RXMSBFirst   : Control RX Data bit order is MSB or LSB.
   
//    MURT_DMA.AdvancedInit.AdvFeatureInit     = (URT_ADVFEATURE_TXDATAINVERT_INIT | URT_ADVFEATURE_TXMSBFIRST_INIT | \
//                                                URT_ADVFEATURE_RXDATAINVERT_INIT | URT_ADVFEATURE_RXMSBFIRST_INIT); 

//    MURT_DMA.AdvancedInit.TXDataInvert       = URT_ADVFEATURE_TXDATAINV_ENABLE;
//    MURT_DMA.AdvancedInit.RXDataInvert       = URT_ADVFEATURE_RXDATAINV_ENABLE;
//    MURT_DMA.AdvancedInit.TXMSBFirst         = URT_ADVFEATURE_MSBFIRST;
//    MURT_DMA.AdvancedInit.RXMSBFirst         = URT_ADVFEATURE_MSBFIRST;

    MID_URT_Init( &MURT_RX_DMA);
    

    //==============================================================================
    //DMA relationship control ( DMA part) 
    //    - The part can refer DMA sample code.
    //-------------------------------------------------------------
    //Enable DMA_Channel1 to handle URT0 RX
    DMA_URTRX.Instance		   = DMA;
    DMA_URTRX.DMAChannelIndex  = DMAChannel0;             // only DMA channel0 can support M2M mode  
    DMA_URTRX.Init.SrcPeri	   = MID_DMA_URT0_READ;       // Source symbol is memory
    DMA_URTRX.Init.DesPeri	   = MID_DMA_MEMORY_WRITE;    // Destination symbol is memory
    DMA_URTRX.Init.BSize 	   = DMA_BSIZE_1BYTE;         // Burst size is 1 byte
    DMA_URTRX.Init.MEMMode	   = MID_DMA_MEMORY_NORMAL;   // Normal memory access mode    
    DMA_URTRX.Init.LoopMode	   = DMA_LOOP_DISABLE;        // DISABLE Loop mode
    
    DMA_URTRX.Parent = &MURT_RX_DMA;
    
    MID_DMA_Init(&DMA_URTRX);
    
    //==============================================================================
    //Enable NVIC IRQ :
    //You can :
    //    1. Tick IRQ handler checkbox of MG32_ChipInit_Wizard and Tick Enable DMA NVIC of
    //       MG32_IRQ_Handler in Run-Time Environment.
    //    or    
    //    2. Use NVIC_EnableIRQ() to enable.
    NVIC_EnableIRQ(DMA_IRQn);
    
    
    //==============================================================================
    //DMA relationship control ( URT part).
    MURT_RX_DMA.mDMA_RX = &DMA_URTRX;

    MID_URT_Receive_DMA(&MURT_RX_DMA , &dMID_URTRX_RXBUF[0],10);   //Receive 10 byte to dMID_URTRX_RXBUF[] by DMA.

    //=============================================================================
    //Wait URT RX DMA complete.
    
    while( MURT_RX_DMA.RxState!= MID_URT_STATE_READY);
     

}
