SAMV71 Xplained Ultra Software Package 1.3

main.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2014, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 /**
00031  *  \page usart USART example with DMA
00032  *
00033  *  \section Purpose
00034  *
00035  *  The USART example shows the how to transfer/receive buffer over USART.
00036  *
00037  *  \section Requirements
00038  *
00039  *  This package can be used with SAM V71 Xplained Ultra board.
00040  *
00041  *  \section Description
00042  *
00043  *  The demonstration program transfer a buffer via USART in local loopback 
00044  *  mode and receives the buffer.
00045  *
00046  *  \section Usage
00047  *
00048  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board. 
00049  *     Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00050  *  -# On the computer, open and configure a terminal application
00051  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00052  *    - 115200 bauds
00053  *    - 8 bits of data
00054  *    - No parity
00055  *    - 1 stop bit
00056  *    - No flow control
00057  *  -# Start the application.
00058  *  -# In the terminal window, the following text should appear (values depend 
00059  *  on the board and chip used):
00060  *     \code
00061  *      -- USART Example Example xxx --
00062  *      -- xxxxxx-xx
00063  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00064  *
00065  *      Menu :
00066  *      ========================
00067  *       UART is Configured in : LoopBack
00068  *       D: Perform USART DMA LoopBack Normal transfer
00069  *       T: Perform USART DMA Normal transfer(remote loopback)
00070  *       H: Display menu
00071  *     \endcode
00072  *  -# The user can then choose any of the available options to perform the 
00073  *  described action.
00074  *  \section References
00075  *  - USART/main.c
00076  *  - pio.h
00077  *  - pio_it.h
00078  *  - usart_dma.h
00079  *  - trace.h
00080  */
00081 
00082 /** \file
00083  *
00084  *  This file contains all the specific code for the USART example.
00085  *
00086  */
00087 
00088 /*----------------------------------------------------------------------------
00089  *        Headers
00090  *----------------------------------------------------------------------------*/
00091 
00092 #include "board.h"
00093 
00094 #include <stdbool.h>
00095 #include <stdio.h>
00096 #include <string.h>
00097 
00098 /*----------------------------------------------------------------------------
00099  *        Local definitions
00100  *----------------------------------------------------------------------------*/
00101 
00102 /** Pins for USART */
00103 #define PINS_USART  PIN_USART0_TXD, PIN_USART0_RXD
00104 
00105 /** Register base for USART */
00106 
00107 #define BASE_USART              USART0
00108 #define ID_USART                ID_USART0
00109 #define USART_IRQ               USART0_IRQn
00110 #define USART_TIMEOUT           115200
00111 
00112 /*----------------------------------------------------------------------------
00113  *        Local functions
00114  *----------------------------------------------------------------------------*/
00115 
00116 /** Global DMA driver for all transfer */
00117 static UsartDma Usartd;
00118 static UsartChannel UsartTx, UsartRx;
00119 static sXdmad ChDma;
00120 static volatile uint32_t Timeout = 0;
00121 
00122 uint8_t pTxBuffer[] = {"This is first USART TX Buffer\n\r"};
00123 uint8_t pRxBuffer[30], BuffRx, BuffTx;
00124 
00125 /**  Pins to configure for the application.*/
00126 const Pin pins[] = {PINS_USART};
00127 
00128 
00129 void XDMAC_Handler(void)
00130 {
00131     XDMAD_Handler(&ChDma);
00132 }
00133 
00134 void USART0_Handler(void)
00135 {
00136     uint32_t status = BASE_USART->US_CSR;
00137     
00138     if(status & US_CSR_TIMEOUT) {
00139         USART_AcknowledgeRxTimeOut(BASE_USART, 0); 
00140         USART_AcknowledgeRxTimeOut(BASE_USART, 1); // generate periodic interrupt
00141         printf("\r %u sec without char on USART Rx", Timeout++);
00142     }
00143 }
00144 
00145 /**
00146  * \brief Displays the user menu on the DBGU.
00147  */
00148 static void DisplayMenu( void )
00149 {
00150     printf( "\n\rMenu :\n\r" );
00151     printf( "========================\n\r" );
00152     printf( "  L: Perform USART DMA LoopBack Normal transfer\n\r" ) ;
00153     printf( "  T: Perform USART DMA Normal transfer(remote loopback) \n\r" ) ; 
00154     //(need to connect USART to D14 and D15 on board) 
00155     printf( "  W: Perform USART Timeout  \n\r" ) ;
00156     //(need to connect USART to D14 and D15 on board) 
00157     printf( "  H: Display menu \n\r\n\r" ) ;
00158     printf( "========================\n\r" );
00159 }
00160 
00161 static void _UsartdConfig(void)
00162 {
00163     uint32_t mode = 0
00164                 | US_MR_USART_MODE_NORMAL
00165                 | US_MR_CHRL_8_BIT
00166                 | US_MR_PAR_NO
00167                 | US_MR_NBSTOP_1_BIT
00168                 | US_MR_CHMODE_NORMAL ;
00169 
00170     memset(&UsartTx, 0, sizeof(UsartChannel));
00171     memset(&UsartRx, 0, sizeof(UsartChannel));
00172 
00173     UsartTx.BuffSize = 1;
00174     UsartTx.pBuff = &BuffTx;
00175     UsartRx.BuffSize= 1;
00176     UsartRx.pBuff = &BuffRx;
00177     UsartTx.dmaProgress = 1;
00178     UsartRx.dmaProgress = 1;
00179   
00180     UsartTx.dmaProgrammingMode = XDMAD_SINGLE;
00181     UsartRx.dmaProgrammingMode = XDMAD_SINGLE;
00182   
00183     Usartd.pXdmad = &ChDma;
00184     Usartd.pRxChannel = &UsartRx;
00185     Usartd.pTxChannel = &UsartTx;
00186     USARTD_Configure(&Usartd, ID_USART, mode, 115200, BOARD_MCK);
00187 }
00188 
00189 static void _UsartdConfigLB(void)
00190 {
00191     uint32_t mode = 0
00192                 | US_MR_USART_MODE_NORMAL
00193                 | US_MR_CHRL_8_BIT
00194                 | US_MR_PAR_NO
00195                 | US_MR_NBSTOP_1_BIT
00196                 | US_MR_CHMODE_LOCAL_LOOPBACK ;
00197     memset(&UsartTx, 0, sizeof(UsartChannel));
00198     memset(&UsartRx, 0, sizeof(UsartChannel));
00199     
00200     UsartTx.BuffSize = 30;
00201     UsartTx.pBuff = pTxBuffer;
00202     UsartRx.BuffSize= 30;
00203     UsartRx.pBuff = pRxBuffer;
00204     UsartTx.dmaProgress = 1;
00205     UsartRx.dmaProgress = 1;
00206 
00207     UsartTx.dmaProgrammingMode = XDMAD_SINGLE;
00208     UsartRx.dmaProgrammingMode = XDMAD_SINGLE;
00209 
00210     UsartTx.callback = 0;
00211     UsartRx.callback = 0;
00212     
00213     Usartd.pXdmad = &ChDma;
00214     Usartd.pRxChannel = &UsartRx;
00215     Usartd.pTxChannel = &UsartTx;
00216     USARTD_Configure(&Usartd, ID_USART, mode, 115200, BOARD_MCK);
00217 }
00218 
00219 /*----------------------------------------------------------------------------
00220  *        Exported functions
00221  *----------------------------------------------------------------------------*/
00222 /**
00223  *  \brief getting-started Application entry point.
00224  *
00225  *  \return Unused (ANSI-C compatibility).
00226  */
00227 extern int main( void )
00228 {
00229   
00230     uint8_t ucKey;
00231 
00232     /* Disable watchdog */
00233     WDT_Disable( WDT ) ;
00234    
00235     SCB_EnableICache();
00236     SCB_EnableDCache();
00237     
00238     /* Output example information */
00239     printf( "-- USART Example %s --\n\r", SOFTPACK_VERSION ) ;
00240     printf( "-- %s\n\r", BOARD_NAME ) ;
00241     printf( "-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME);
00242 
00243     
00244     /* Configure pins*/
00245     PIO_Configure( pins, PIO_LISTSIZE( pins ) ) ;
00246   
00247     /* Display menu */
00248     DisplayMenu() ;
00249        
00250     while ( 1 ) {
00251         ucKey = DBG_GetChar() ;
00252 
00253         switch ( ucKey ) {
00254         case 'H' :case 'h' :
00255             DisplayMenu() ;
00256             break ;
00257         case 'l' :case 'L' :
00258             memset(pRxBuffer,'X' ,30);
00259             pRxBuffer[28] = '\n';
00260             pRxBuffer[29] = '\r';
00261             printf("\n\rRx Buffer before transfer is \n\r");
00262             puts((const char *)pRxBuffer);
00263             _UsartdConfigLB();
00264             USARTD_EnableRxChannels(&Usartd, &UsartRx) ;
00265             USARTD_EnableTxChannels(&Usartd, &UsartTx) ;
00266             USARTD_RcvData(&Usartd) ;
00267             USARTD_SendData(&Usartd) ;
00268             
00269             printf("Rx Buffer after transfer is \n\r") ;
00270             puts((const char *)pRxBuffer);
00271             USARTD_DisableRxChannels(&Usartd, &UsartRx) ;
00272             USARTD_DisableTxChannels(&Usartd, &UsartTx) ;
00273             break ;
00274         case 't' :case 'T' :
00275             printf("Press Q on USART terminal to quit\n\r") ;
00276             _UsartdConfig();
00277             while ('Q' != BuffTx) {
00278                 USARTD_EnableRxChannels(&Usartd, &UsartRx) ;
00279                 USARTD_EnableTxChannels(&Usartd, &UsartTx) ;
00280                 USARTD_RcvData(&Usartd);
00281                 while(UsartRx.dmaProgress != 1 ) ;
00282                 BuffTx = BuffRx ;
00283                 USARTD_SendData(&Usartd) ;
00284                 while(UsartTx.dmaProgress != 1 ) ;
00285             }
00286             printf("Exit \n\r");
00287             DisplayMenu() ;
00288             
00289             break ;
00290         case 'w' :case 'W' :
00291             printf("\n\r It will Quit if no character received within 10 secs \
00292                 (Need to send at least one byte to initiate timeout)\n\r");
00293             _UsartdConfig();
00294             NVIC_ClearPendingIRQ(USART_IRQ);
00295             NVIC_SetPriority( USART_IRQ , 1);
00296             USART_EnableIt(BASE_USART, US_IER_TIMEOUT);
00297             
00298             NVIC_EnableIRQ(USART_IRQ);
00299             USART_EnableRecvTimeOut( BASE_USART, USART_TIMEOUT);
00300             USARTD_EnableRxChannels(&Usartd, &UsartRx);
00301             USARTD_EnableTxChannels(&Usartd, &UsartTx);
00302             USARTD_RcvData(&Usartd);
00303             while(Timeout < 10) {
00304                 if(UsartRx.dmaProgress) {
00305                     BuffTx = BuffRx;
00306                     Timeout = 0;
00307                     USARTD_SendData(&Usartd);
00308                     while(UsartTx.dmaProgress != 1);
00309                     USARTD_EnableRxChannels(&Usartd, &UsartRx);
00310                     USARTD_EnableTxChannels(&Usartd, &UsartTx);
00311                     USARTD_RcvData(&Usartd);
00312                 }
00313             }
00314             USART_DisableIt(BASE_USART, US_IER_TIMEOUT);
00315             NVIC_DisableIRQ(USART_IRQ);
00316             printf("Exit \n\r");
00317             DisplayMenu() ;
00318             
00319             break ;
00320         default :
00321             break ;
00322         }
00323     }
00324 }
00325 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines