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 uart UART example with DMA
00032  *
00033  *  \section Purpose
00034  *
00035  *  The UART example show how to use UART peripheral on
00036  *  SAMV7 family of microcontrollers. This basic application shows how to send
00037  *  and receive a buffer..
00038  *
00039  *  \section Requirements
00040  *
00041  *  This package can be used with SAM V71 Xplained Ultra board or SAME70 Xplained board.
00042  *
00043  *  \section Description
00044  *
00045  *  The demonstration program makes transfer buffer over DMA or via interrupt
00046  *  in local loop-back mode of UART
00047  *
00048  *  \section Usage
00049  *
00050  *  -# Build the program and download it inside the board.
00051  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00052  *  -# On the computer, open and configure a terminal application
00053  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00054  *    - 115200 baud rate
00055  *    - 8 bits of data
00056  *    - No parity
00057  *    - 1 stop bit
00058  *    - No flow control
00059  *  -# Start the application.
00060  *  -# In the terminal window, the following text should appear (values depend
00061 *  on the board and chip used):
00062  *     \code
00063  *      -- uart Example xxx --
00064  *      -- xxxxxx-xx
00065  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00066  *    Menu :
00067  *      ========================
00068  *       UART is Configured in : LoopBack
00069  *       I: Perform UART transfer with interrupt
00070  *       D: Perform UART DMA transfer in local loopback
00071  *       H: Display menu
00072  *     \endcode
00073  *  -# The user can then choose any of the available options to perform the
00074  *  described action.
00075  *
00076  *  \section References
00077  *  - uart/main.c
00078  *  - board.h
00079  */
00080 
00081 /** \file
00082  *
00083  *  This file contains all the specific code for the UART example.
00084  *
00085  */
00086 
00087 /*----------------------------------------------------------------------------
00088  *        Headers
00089  *----------------------------------------------------------------------------*/
00090 
00091 #include "board.h"
00092 
00093 #include <stdbool.h>
00094 #include <stdio.h>
00095 #include <string.h>
00096 
00097 /*----------------------------------------------------------------------------
00098  *        Local definitions
00099  *----------------------------------------------------------------------------*/
00100 #define BASE_UART       UART4
00101 #define BASE_ID         ID_UART4
00102 #define BASE_IRQ        UART4_IRQn
00103 
00104 #define RX_SIZE         30
00105 
00106 /*----------------------------------------------------------------------------
00107  *        Local variables
00108  *----------------------------------------------------------------------------*/
00109 const Pin BASE_UART_PINS[] = {PINS_UART4};
00110 /** Global DMA driver for all transfer */
00111 static UartDma Uartd;
00112 static UartChannel UartTx, UartRx;
00113 static sXdmad dmad;
00114 
00115 COMPILER_ALIGNED(32) uint8_t pTxBuffer[] = {"This is UART Tx Buffer.........\n\r"};
00116 COMPILER_ALIGNED(32) uint8_t pRxBuffer[RX_SIZE];
00117 
00118 /*----------------------------------------------------------------------------
00119  *        Local functions
00120  *----------------------------------------------------------------------------*/
00121 
00122 /**
00123  *  \brief Handler for UART4.
00124  *
00125  *  Process UART4 interrupts
00126  */
00127 void UART4_Handler(void)
00128 {
00129     uint32_t Status = UART_GetStatus(BASE_UART);
00130 
00131     if (Status & (UART_SR_OVRE | UART_SR_FRAME | UART_SR_PARE)) {
00132         BASE_UART->UART_CR = UART_CR_RSTSTA;
00133         printf("Error \n\r");
00134     }
00135 
00136     printf("%c", (char)BASE_UART->UART_RHR);
00137 }
00138 
00139 /**
00140  *  \brief Handler for XDMAC.
00141  *
00142  *  Process XDMA interrupts
00143  */
00144 void XDMAC_Handler(void)
00145 {
00146     XDMAD_Handler(&dmad);
00147 }
00148 
00149 /**
00150  * \brief Displays the user menu on the DBGU.
00151  */
00152 static void DisplayMenu(void)
00153 {
00154     printf("\n\rMenu :\n\r");
00155     printf("========================\n\r");
00156     printf(" UART is Configured in : LoopBack\n\r");
00157     printf("  I: Perform UART transfer with interrupt\n\r");
00158     printf("  D: Perform UART DMA transfer in local loopback\n\r");
00159     printf("  H: Display menu \n\r\n\r");
00160     printf("========================\n\r");
00161 }
00162 
00163 /**
00164  * \brief UART transfer with interrupt in UART loop back mode
00165  */
00166 static void UartTransfer(void)
00167 {
00168     uint8_t *pBuffer = &pTxBuffer[0];
00169 
00170     PMC_EnablePeripheral(BASE_ID);
00171     UART_Configure(BASE_UART, (UART_MR_PAR_NO | UART_MR_CHMODE_LOCAL_LOOPBACK),
00172             115200, BOARD_MCK);
00173 
00174     NVIC_ClearPendingIRQ(BASE_IRQ);
00175     NVIC_SetPriority(BASE_IRQ ,1);
00176 
00177     /* Enables the UART to transfer and receive data. */
00178     UART_SetTransmitterEnabled (BASE_UART , 1);
00179     UART_SetReceiverEnabled (BASE_UART , 1);
00180 
00181     UART_EnableIt(BASE_UART, (UART_IER_RXRDY | UART_IER_OVRE | UART_IER_FRAME
00182             | UART_IER_PARE));
00183     /* Enable interrupt  */
00184     NVIC_EnableIRQ(BASE_IRQ);
00185 
00186     while (*pBuffer != '\0') {
00187         UART_PutChar(BASE_UART, *pBuffer);
00188         pBuffer++;
00189     }
00190     UART_PutChar(BASE_UART, *pBuffer);
00191 }
00192 
00193 /**
00194  * \brief UART transfer with DMA in UART loop back mode
00195  */
00196 static void _UartdConfigLB(void)
00197 {
00198     uint32_t mode = 0
00199         | UART_MR_PAR_NO
00200         | UART_MR_BRSRCCK_PERIPH_CLK
00201         | UART_MR_CHMODE_LOCAL_LOOPBACK;
00202 
00203     dmad.pXdmacs = XDMAC;
00204 
00205     memset(&UartTx, 0, sizeof(UartChannel));
00206     memset(&UartRx, 0, sizeof(UartChannel));
00207     UartTx.BuffSize = 25;
00208     UartTx.pBuff = pTxBuffer;
00209     UartRx.BuffSize= 25;
00210     UartRx.pBuff = pRxBuffer;
00211     UartTx.sempaphore = 1;
00212     UartRx.sempaphore = 1;
00213 
00214     Uartd.pRxChannel = &UartRx;
00215     Uartd.pTxChannel = &UartTx;
00216     Uartd.pXdmad = &dmad;
00217     PMC_EnablePeripheral(BASE_ID);
00218     UARTD_Configure(&Uartd, BASE_ID, mode, 115200, BOARD_MCK);
00219 }
00220 
00221 /*----------------------------------------------------------------------------
00222  *        Exported functions
00223  *----------------------------------------------------------------------------*/
00224 /**
00225  *  \brief Application entry point for UART example.
00226  *
00227  *  \return Unused (ANSI-C compatibility).
00228  */
00229 extern int main(void)
00230 {
00231     uint8_t ucKey;
00232 
00233     /* Disable watchdog */
00234     WDT_Disable(WDT);
00235 
00236     /* Output example information */
00237     printf("-- UART Example %s --\n\r", SOFTPACK_VERSION);
00238     printf("-- %s\n\r", BOARD_NAME);
00239     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00240 
00241     /* Enable I and D cache */
00242     SCB_EnableICache();
00243     SCB_EnableDCache();
00244 
00245     /* Display menu */
00246     DisplayMenu();
00247 
00248     while (1) {
00249         ucKey = DBG_GetChar();
00250 
00251         switch (ucKey) {
00252         case 'h':
00253             DisplayMenu();
00254             break;
00255         case 'i':
00256         case 'I':
00257             printf("\n\rSending Tx Buffer.. \n\r");
00258             UartTransfer();
00259             break;
00260 
00261         case 'd':
00262         case 'D':
00263             memset(pRxBuffer,'X' ,30);
00264             pRxBuffer[28] = '\n';
00265             pRxBuffer[29] = '\r';
00266             printf("\n\rRx Buffer before transfer is \n\r");
00267             puts((char*)pRxBuffer);
00268             _UartdConfigLB();
00269             UARTD_EnableRxChannels(&Uartd, &UartRx);
00270             UARTD_EnableTxChannels(&Uartd, &UartTx);
00271             UARTD_RcvData(&Uartd);
00272             UARTD_SendData(&Uartd);
00273 
00274             printf("\n\rRx Buffer after transfer is \n\r");
00275 
00276             while (Uartd.pRxChannel->sempaphore == 0);
00277             puts((char*)pRxBuffer);
00278             UARTD_DisableRxChannels(&Uartd, &UartRx);
00279             UARTD_DisableTxChannels(&Uartd, &UartTx);
00280             break;
00281 
00282         default :
00283             break;
00284         }
00285     }
00286 }
00287 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines