


/**
 ******************************************************************************
 *
 * @file        Sample_MID_URT_RXTX_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.03
 * @date        2024/01/18
 * @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_RXTX_DMA;
static DMA_HandleTypeDef     DMA_TX;
static DMA_HandleTypeDef     DMA_RX;

static uint8_t dMID_URTRXTX_RXBUF[10];
static uint8_t dMID_URTRXTX_TXBUF[25]= {"Megawin Technology Inc.\n"};

/* Private function prototypes -----------------------------------------------*/
void URT0_IRQHandler(void);
void DMA_IRQHandler(void);

void Sample_MID_URT_Transfer_DMA(void);

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


/**
 *******************************************************************************
 * @brief	  URT IRQ Handle
 * @details   Call the MID_URT_IRQHandler() function into URT0_IRQHandler() if 
 *            the URT0_IRQHandler() already  exists, or uncommenting 
 *            the URT0_IRQHandler() function in the sample code. 
 * @return      
 * @exception No
 * @note      No
 *******************************************************************************
 */
#if !defined(MG32_URT0_IRQ_)
void URT0_IRQHandler(void)
{
    MID_URT_IRQHandler(&MURT_RXTX_DMA);
}
#endif
/**
 *******************************************************************************
 * @brief	  DMA IRQ Handle
 * @details   Call the MID_DMA_IRQHandler() & 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_RX);
    MID_DMA_IRQHandler(&DMA_TX);
}
#endif
/**
 *******************************************************************************
 * @brief	    Initalize the URT MSP.
 * @details     
 * @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 interrupt mode to receive data and transmit data.
 * @details     
 * @return      
 * @exception   No
 * @note        No
 *******************************************************************************
 */
void Sample_MID_URT_Transfer_DMA(void)
{
    //==============================================================================
    //MG32F02A032 no support the sample code because the device only 1 channel DMA.
    #if defined(MG32_2ND)
        #error no support the sample code because only one DMA.
    #endif
    //==============================================================================
    //Select URT : The sample select UART0 
    MURT_RXTX_DMA.Instance            =  URT0;
    
    //==============================================================================
    //URT standard inital :
    //    - Baudrate.
    //    - Data Size.  
    //    - Hardware Flow Control. (CTS / RTS)
    //    - Sample time.
    //    - Parity bit.
    //    - Stop bit.
    MURT_RXTX_DMA.Init.BaudRate       =  115200;                            
    MURT_RXTX_DMA.Init.WordLength     =  UART_WORDLENGTH_8B;
    MURT_RXTX_DMA.Init.HWFlowCtrl     =  URT_HWCONTROL_NONE;
    MURT_RXTX_DMA.Init.Mode           =  URT_TX_RX;
    MURT_RXTX_DMA.Init.OneBitSampling =  URT_ONEBIT_SAMPLE_THREE;
    MURT_RXTX_DMA.Init.Parity         =  URT_PARITY_NO;
    MURT_RXTX_DMA.Init.StopBits       =  URT_STOPBIT_1_0;
    
    //==============================================================================
    //URT advanced inital. 
    
    //-------------------------------------------------------------
    //Exsample 1 :
    //    - No need Advanced inital.
    MURT_RXTX_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_RXTX_DMA);
    
    //==============================================================================
    //Enable NVIC IRQ :
    //You can :
    //    1. Tick IRQ handler checkbox of MG32_ChipInit_Wizard and Tick Enable URT0 NVIC of
    //       MG32_IRQ_Handler in Run-Time Environment.
    //    or    
    //    2. Use NVIC_EnableIRQ() to enable.
    NVIC_EnableIRQ( URT0_IRQn);
   
    //==============================================================================
    //DMA relationship control ( DMA part) 
    //    - The part can refer DMA sample code.
    //-------------------------------------------------------------
    //Enable DMA_Channel1 to handle URT0 TX
    DMA_TX.Instance       = DMA;
    DMA_TX.DMAChannelIndex= DMAChannel1;          // only DMA channel0 can support M2M mode
    DMA_TX.Init.SrcPeri   = MID_DMA_MEMORY_READ;  // Source symbol is memory
    DMA_TX.Init.DesPeri   = MID_DMA_URT0_WRITE;   // Destination symbol is memory
    DMA_TX.Init.BSize     = DMA_BSIZE_1BYTE;      // Burst size is 1 byte
    DMA_TX.Init.MEMMode   = MID_DMA_MEMORY_NORMAL;// Normal memory access mode    
    DMA_TX.Init.LoopMode  = DMA_LOOP_DISABLE;     // DISABLE Loop mode
    
    DMA_TX.Parent = &MURT_RXTX_DMA;
    
    MID_DMA_Init(&DMA_TX);

    //-------------------------------------------------------------
    //Enable DMA_Channel1 to handle URT0 RX
    DMA_RX.Instance		    = DMA;
    DMA_RX.DMAChannelIndex  = DMAChannel0;             // only DMA channel0 can support M2M mode  
    DMA_RX.Init.SrcPeri	    = MID_DMA_URT0_READ;       // Source symbol is memory
    DMA_RX.Init.DesPeri	    = MID_DMA_MEMORY_WRITE;    // Destination symbol is memory
    DMA_RX.Init.BSize 	    = DMA_BSIZE_1BYTE;         // Burst size is 1 byte
    DMA_RX.Init.MEMMode	    = MID_DMA_MEMORY_NORMAL;   // Normal memory access mode    
    DMA_RX.Init.LoopMode	= DMA_LOOP_DISABLE;        // DISABLE Loop mode
    
    DMA_RX.Parent = &MURT_RXTX_DMA;
    
    MID_DMA_Init(&DMA_RX);
    
    //==============================================================================
    //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_RXTX_DMA.mDMA_RX = &DMA_RX;
    MURT_RXTX_DMA.mDMA_TX = &DMA_TX;

    MID_URT_Receive_DMA(&MURT_RXTX_DMA , &dMID_URTRXTX_RXBUF[0], 10);
    MID_URT_Transmit_DMA(&MURT_RXTX_DMA, &dMID_URTRXTX_TXBUF[0], 24);
    

    
    //=============================================================================
    //Wait URT RX DAM and URT TX DMA complete.
    
    while( MURT_RXTX_DMA.gState != MID_URT_STATE_READY  || MURT_RXTX_DMA.RxState!= MID_URT_STATE_READY);
     
}










