SAMV71 Xplained Ultra Software Package 1.5

main.c

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------- */
00002 /*                  Atmel Microcontroller Software Support                      */
00003 /*                       SAM Software Package License                           */
00004 /* ---------------------------------------------------------------------------- */
00005 /* Copyright (c) 2015, Atmel Corporation                                        */
00006 /*                                                                              */
00007 /* All rights reserved.                                                         */
00008 /*                                                                              */
00009 /* Redistribution and use in source and binary forms, with or without           */
00010 /* modification, are permitted provided that the following condition is met:    */
00011 /*                                                                              */
00012 /* - Redistributions of source code must retain the above copyright notice,     */
00013 /* this list of conditions and the disclaimer below.                            */
00014 /*                                                                              */
00015 /* Atmel's name may not be used to endorse or promote products derived from     */
00016 /* this software without specific prior written permission.                     */
00017 /*                                                                              */
00018 /* DISCLAIMER:  THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR   */
00019 /* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF */
00020 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE   */
00021 /* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,      */
00022 /* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT */
00023 /* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,  */
00024 /* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    */
00025 /* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING         */
00026 /* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, */
00027 /* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.                           */
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 SAMV71 Xplained Ultra board or SAME70 Xplained 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 board.
00049  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00050  *  -# On the computer, open and configure a terminal application
00051  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00052  *    - 115200 baud rate
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 COMPILER_ALIGNED(32) uint8_t pTxBuffer[] = {"This is first USART TX Buffer\n\r"};
00123 COMPILER_ALIGNED(32) uint8_t pRxBuffer[30], BuffRx, BuffTx;
00124 
00125 /**  Pins to configure for the application.*/
00126 const Pin pins[] = {PINS_USART};
00127 
00128 #define PIN0 {PIO_PA6, PIOA, ID_PIOA, PIO_OUTPUT_0, PIO_DEFAULT}
00129 #define PIN1 {PIO_PD11, PIOD, ID_PIOD, PIO_OUTPUT_0, PIO_DEFAULT}
00130 
00131 #define PINS  PIN0, PIN1
00132 const Pin pins_test[] = {PINS};
00133 
00134 
00135 void XDMAC_Handler(void)
00136 {
00137     XDMAD_Handler(&ChDma);
00138 }
00139 
00140 void USART0_Handler(void)
00141 {
00142     uint32_t status = BASE_USART->US_CSR;
00143 
00144     if (status & US_CSR_TIMEOUT) {
00145         USART_AcknowledgeRxTimeOut(BASE_USART, 0);
00146         USART_AcknowledgeRxTimeOut(BASE_USART, 1); // generate periodic interrupt
00147         printf("\r %u sec without char on USART Rx", (unsigned int)Timeout++);
00148     }
00149 }
00150 
00151 /**
00152  * \brief Displays the user menu on the DBGU.
00153  */
00154 static void DisplayMenu(void)
00155 {
00156     printf("\n\rMenu :\n\r");
00157     printf("========================\n\r");
00158     printf("  L: Perform USART DMA LoopBack Normal transfer\n\r");
00159     printf("  T: Perform USART DMA Normal transfer(remote loopback) \n\r");
00160     //(need to connect USART to D14 and D15 on board)
00161     printf("  W: Perform USART Timeout  \n\r");
00162     //(need to connect USART to D14 and D15 on board)
00163     printf("  H: Display menu \n\r\n\r");
00164     printf("========================\n\r");
00165 }
00166 
00167 static void _UsartdConfig(void)
00168 {
00169     uint32_t mode = 0
00170                 | US_MR_USART_MODE_NORMAL
00171                 | US_MR_CHRL_8_BIT
00172                 | US_MR_PAR_NO
00173                 | US_MR_NBSTOP_1_BIT
00174                 | US_MR_CHMODE_NORMAL;
00175 
00176     memset(&UsartTx, 0, sizeof(UsartChannel));
00177     memset(&UsartRx, 0, sizeof(UsartChannel));
00178 
00179     UsartTx.BuffSize = 1;
00180     UsartTx.pBuff = &BuffTx;
00181     UsartRx.BuffSize = 1;
00182     UsartRx.pBuff = &BuffRx;
00183     UsartTx.dmaProgress = 1;
00184     UsartRx.dmaProgress = 1;
00185 
00186     UsartTx.dmaProgrammingMode = XDMAD_SINGLE;
00187     UsartRx.dmaProgrammingMode = XDMAD_SINGLE;
00188 
00189     Usartd.pXdmad = &ChDma;
00190     Usartd.pRxChannel = &UsartRx;
00191     Usartd.pTxChannel = &UsartTx;
00192     USARTD_Configure(&Usartd, ID_USART, mode, 115200, BOARD_MCK);
00193 }
00194 
00195 static void _UsartdConfigLB(void)
00196 {
00197     uint32_t mode = 0
00198                 | US_MR_USART_MODE_NORMAL
00199                 | US_MR_CHRL_8_BIT
00200                 | US_MR_PAR_NO
00201                 | US_MR_NBSTOP_1_BIT
00202                 | US_MR_CHMODE_LOCAL_LOOPBACK;
00203     memset(&UsartTx, 0, sizeof(UsartChannel));
00204     memset(&UsartRx, 0, sizeof(UsartChannel));
00205 
00206     UsartTx.BuffSize = 30;
00207     UsartTx.pBuff = pTxBuffer;
00208     UsartRx.BuffSize= 30;
00209     UsartRx.pBuff = pRxBuffer;
00210     UsartTx.dmaProgress = 1;
00211     UsartRx.dmaProgress = 1;
00212 
00213     UsartTx.dmaProgrammingMode = XDMAD_SINGLE;
00214     UsartRx.dmaProgrammingMode = XDMAD_SINGLE;
00215 
00216     UsartTx.callback = 0;
00217     UsartRx.callback = 0;
00218 
00219     Usartd.pXdmad = &ChDma;
00220     Usartd.pRxChannel = &UsartRx;
00221     Usartd.pTxChannel = &UsartTx;
00222     USARTD_Configure(&Usartd, ID_USART, mode, 115200, BOARD_MCK);
00223 }
00224 
00225 /*----------------------------------------------------------------------------
00226  *        Exported functions
00227  *----------------------------------------------------------------------------*/
00228 /**
00229  *  \brief getting-started Application entry point.
00230  *
00231  *  \return Unused (ANSI-C compatibility).
00232  */
00233 extern int main( void )
00234 {
00235 
00236     uint8_t ucKey;
00237 
00238     /* Disable watchdog */
00239     WDT_Disable(WDT);
00240 
00241     SCB_EnableICache();
00242     SCB_EnableDCache();
00243 
00244     /* Output example information */
00245     printf("-- USART Example %s --\n\r", SOFTPACK_VERSION);
00246     printf("-- %s\n\r", BOARD_NAME);
00247     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00248 
00249 
00250     /* Configure pins*/
00251     PIO_Configure(pins, PIO_LISTSIZE(pins));
00252 
00253     /*Configure pins to watch cache coherence time*/
00254     PIO_Configure(pins_test, PIO_LISTSIZE(pins_test));
00255 
00256     /* Display menu */
00257     DisplayMenu();
00258 
00259     while (1) {
00260         ucKey = DBG_GetChar();
00261 
00262         switch (ucKey) {
00263         case 'H' :
00264         case 'h' :
00265             DisplayMenu();
00266             break;
00267 
00268         case 'l' :
00269         case 'L' :
00270             memset(pRxBuffer,'X' ,30);
00271             pRxBuffer[28] = '\n';
00272             pRxBuffer[29] = '\r';
00273             printf("\n\rRx Buffer  transfer is \n\r");
00274 
00275             puts((const char *)pTxBuffer);
00276 
00277             printf("\n\rRx Buffer before transfer is \n\r");
00278             puts((const char *)pRxBuffer);
00279             _UsartdConfigLB();
00280             USARTD_EnableRxChannels(&Usartd, &UsartRx);
00281             USARTD_EnableTxChannels(&Usartd, &UsartTx);
00282             USARTD_RcvData(&Usartd);
00283             USARTD_SendData(&Usartd);
00284             while (UsartRx.dmaProgress != 1);
00285             printf("Rx Buffer after transfer is \n\r");
00286             puts((const char *)pRxBuffer);
00287             USARTD_DisableRxChannels(&Usartd, &UsartRx);
00288             USARTD_DisableTxChannels(&Usartd, &UsartTx);
00289             break;
00290 
00291         case 't' :
00292         case 'T' :
00293             printf("Press Q on USART terminal to quit\n\r");
00294             _UsartdConfig();
00295             while ('Q' != BuffTx) {
00296                 USARTD_EnableRxChannels(&Usartd, &UsartRx);
00297                 USARTD_EnableTxChannels(&Usartd, &UsartTx);
00298                 USARTD_RcvData(&Usartd);
00299                 while (UsartRx.dmaProgress != 1);
00300                 BuffTx = BuffRx;
00301                 USARTD_SendData(&Usartd);
00302                 while (UsartTx.dmaProgress != 1);
00303             }
00304             printf("Exit \n\r");
00305             DisplayMenu();
00306             break;
00307 
00308         case 'w' :
00309         case 'W' :
00310             printf("\n\r It will Quit if no character received within 10 secs \
00311                 (Need to send at least one byte to initiate timeout)\n\r");
00312             _UsartdConfig();
00313             NVIC_ClearPendingIRQ(USART_IRQ);
00314             NVIC_SetPriority(USART_IRQ , 1);
00315             USART_EnableIt(BASE_USART, US_IER_TIMEOUT);
00316 
00317             NVIC_EnableIRQ(USART_IRQ);
00318             USART_EnableRecvTimeOut( BASE_USART, USART_TIMEOUT);
00319             USARTD_EnableRxChannels(&Usartd, &UsartRx);
00320             USARTD_EnableTxChannels(&Usartd, &UsartTx);
00321             USARTD_RcvData(&Usartd);
00322             while (Timeout < 10) {
00323                 if (UsartRx.dmaProgress) {
00324                     BuffTx = BuffRx;
00325                     Timeout = 0;
00326                     USARTD_SendData(&Usartd);
00327                     while (UsartTx.dmaProgress != 1);
00328                     USARTD_EnableRxChannels(&Usartd, &UsartRx);
00329                     USARTD_EnableTxChannels(&Usartd, &UsartTx);
00330                     USARTD_RcvData(&Usartd);
00331                 }
00332             }
00333             USART_DisableIt(BASE_USART, US_IER_TIMEOUT);
00334             NVIC_DisableIRQ(USART_IRQ);
00335             printf("Exit \n\r");
00336             DisplayMenu();
00337             break;
00338 
00339         default :
00340             break;
00341         }
00342     }
00343 }
00344 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines