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 spi SPI example
00032  *
00033  *  \section Purpose
00034  *
00035  * This example shows control of the SPI in loop back mode.
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  *  \section Usage
00044  *
00045  *  -# Build the program and download it inside the board.
00046  *     Please refer to the Getting Started with SAM V71/E70 Microcontrollers.pdf
00047  *  -# On the computer, open and configure a terminal application
00048  *     (e.g. HyperTerminal on Microsoft Windows) with these settings:
00049  *    - 115200 baud rate
00050  *    - 8 bits of data
00051  *    - No parity
00052  *    - 1 stop bit
00053  *    - No flow control
00054  *  -# Start the application.
00055  *  -# In the terminal window, the following text should appear (values depend
00056  *  on the board and chip used):
00057  *     \code
00058  *      -- SPI Example xxx --
00059  *      -- xxxxxx-xx
00060  *      -- Compiled: xxx xx xxxx xx:xx:xx --
00061  *     Menu :
00062  *      ------
00063  *      0: Set SPCK =  500000 Hz
00064  *      1: Set SPCK = 1000000 Hz
00065  *      2: Set SPCK = 5000000 Hz
00066  *      s: Perform SPI transfer start
00067  *      d: Perform SPI Dma Transfer
00068  *      h: Display menu
00069  *     \endcode
00070  *
00071  * The user can then choose any of the available options to perform
00072  * the described action.
00073  *
00074  *  \section References
00075  *  - spi/main.c
00076  *  - pio.h
00077  *  - pio_it.h
00078  *  - board.h
00079  */
00080 
00081 /** \file
00082  *
00083  *  This file contains all the specific code for the SPI example.
00084  *
00085  */
00086 
00087 /*----------------------------------------------------------------------------
00088  *        Headers
00089  *----------------------------------------------------------------------------*/
00090 
00091 #include "board.h"
00092 
00093 #include <stdbool.h>
00094 #include <stdio.h>
00095 #include <stdlib.h>
00096 #include <string.h>
00097 
00098 /*----------------------------------------------------------------------------
00099  *        Local definitions
00100  *----------------------------------------------------------------------------*/
00101 
00102 /** Pins to configure for the application. */
00103 static const Pin spi_pins[] = {
00104     PIN_SPI_MISO,
00105     PIN_SPI_MOSI,
00106     PIN_SPI_SPCK,
00107     PIN_SPI_NPCS3
00108 };
00109 
00110 /*----------------------------------------------------------------------------
00111  *        Local variables
00112  *----------------------------------------------------------------------------*/
00113 
00114 /** Global timestamps in milliseconds since start of application */
00115 volatile uint32_t dwTimeStamp = 0;
00116 
00117 /** SPI Clock setting (Hz) */
00118 static uint32_t spiClock = 500000;
00119 
00120 /** Global DMA driver for all transfer */
00121 static Spid SpiDma;
00122 static SpidCmd SpiCommand;
00123 static sXdmad Dma;
00124 
00125 #define SPI0_CS0  0
00126 #define SPI0_CS1  1
00127 #define SPI0_CS2  2
00128 #define SPI0_CS3  3
00129 
00130 /** SPI clock configuration */
00131 static const uint32_t clockConfigurations[3] = { 500000, 1000000, 5000000};
00132 
00133 /*----------------------------------------------------------------------------
00134  *        Local functions
00135  *----------------------------------------------------------------------------*/
00136 COMPILER_ALIGNED(32) uint8_t pTxBuffer[] = "This is SPI LoopBack Test Buffer";
00137 COMPILER_ALIGNED(32) uint8_t pRxBuffer[30];
00138 
00139 /**
00140  *  \brief Handler for SPI0.
00141  *
00142  *  Process SPI interrupts
00143  */
00144 void SPI0_Handler(void)
00145 {
00146     printf("%c", (char) SPI_Read(SPI0));
00147 }
00148 
00149 /**
00150  *  \brief Handler for XDMAC.
00151  *
00152  *  Process XDAMC interrupts
00153  */
00154 void XDMAC_Handler(void)
00155 {
00156     XDMAD_Handler(&Dma);
00157 }
00158 
00159 /**
00160  * \brief Sets the specified SPI clock configuration.
00161  * \param configuration  Index of the configuration to set.
00162  */
00163 static void SetClockConfiguration(uint8_t configuration)
00164 {
00165     spiClock = clockConfigurations[configuration];
00166     printf("Setting SPI master clock #%u ... \n\r",
00167             (unsigned int)clockConfigurations[configuration]);
00168 }
00169 
00170 /**
00171  * \brief Perform SPI transfer with interrupt in SPI loop back mode.
00172  */
00173 static void SpiLoopBack(void)
00174 {
00175     uint8_t i;
00176 
00177     printf( "\n\r-I- Configure SPI master\n\r" );
00178     SPI_Configure(SPI0, ID_SPI0, (SPI_MR_MSTR | SPI_MR_MODFDIS
00179                     | SPI_MR_LLB | SPI_PCS( SPI0_CS3 )));
00180     SPI_ConfigureNPCS( SPI0,
00181             SPI0_CS3,
00182             SPI_DLYBCT( 1000, BOARD_MCK ) |
00183             SPI_DLYBS(1000, BOARD_MCK) |
00184             SPI_SCBR( spiClock, BOARD_MCK) );
00185 
00186     /* Configure and enable interrupt on RC compare */
00187     NVIC_ClearPendingIRQ(SPI0_IRQn);
00188     NVIC_SetPriority(SPI0_IRQn ,1);
00189     NVIC_EnableIRQ(SPI0_IRQn);
00190 
00191     SPI_EnableIt(SPI0, SPI_IER_RDRF);
00192     SPI_Enable(SPI0);
00193 
00194     for (i = 0; ;i++) {
00195         SPI_Write(SPI0, SPI0_CS3 , (uint16_t)pTxBuffer[i]);
00196         if (pTxBuffer[i] =='\0')
00197             break;
00198     }
00199     if (SPI_IsFinished(SPI0)) {
00200         SPI_Disable(SPI0);
00201     }
00202 }
00203 
00204 /**
00205  * \brief Perform SPI transfer with DMA in SPI loop back mode.
00206  */
00207 static void SpiLoopBackDma(void)
00208 {
00209     printf( "\n\r-I- Configure SPI master\n\r" );
00210     Dma.pXdmacs = XDMAC;
00211 
00212     SpiCommand.TxSize = 30;
00213     SpiCommand.pTxBuff = (uint8_t *)pTxBuffer;
00214     SpiCommand.RxSize= 30;
00215     SpiCommand.pRxBuff = (uint8_t *)pRxBuffer;
00216     SpiCommand.spiCs = SPI0_CS3;
00217 
00218 
00219     SPID_Configure(&SpiDma, SPI0, ID_SPI0, (SPI_MR_MSTR | SPI_MR_MODFDIS
00220                     | SPI_MR_LLB | SPI_PCS( SPI0_CS3 )), &Dma);
00221     SPI_ConfigureNPCS(SPI0,
00222                     SPI0_CS3,
00223                     SPI_DLYBCT( 1000, BOARD_MCK ) |
00224                     SPI_DLYBS(1000, BOARD_MCK) |
00225                     SPI_SCBR( spiClock, BOARD_MCK));
00226 
00227     SPI_Enable(SPI0);
00228     SPID_SendCommand(&SpiDma, &SpiCommand);
00229 }
00230 
00231 
00232 /**
00233  * \brief Displays the user menu on the DBGU.
00234  */
00235 static void DisplayMenu( void )
00236 {
00237     uint32_t i;
00238 
00239     printf("\n\rMenu :\n\r");
00240     printf("------\n\r");
00241 
00242     for (i = 0; i < 3; i++) {
00243         printf("  %u: Set SPCK = %7u Hz\n\r",
00244                 (unsigned int)i, (unsigned int)clockConfigurations[i]);
00245     }
00246     printf("  s: Perform SPI transfer start\n\r");
00247     printf("  d: Perform SPI DMA Transfer (first 30 bytes of Tx buffer)\n\r");
00248     printf("  h: Display menu \n\r\n\r");
00249 }
00250 
00251 /*----------------------------------------------------------------------------
00252  *        Exported functions
00253  *----------------------------------------------------------------------------*/
00254 /**
00255  *  \brief Application entry point.
00256  *
00257  *  \return Unused (ANSI-C compatibility).
00258  */
00259 extern int main(void)
00260 {
00261     uint8_t ucKey;
00262 
00263     /* Disable watchdog */
00264     WDT_Disable(WDT);
00265 
00266     /* Output example information */
00267     printf("\n\r-- SPI Example %s --\n\r", SOFTPACK_VERSION);
00268     printf("-- %s\n\r", BOARD_NAME);
00269     printf("-- Compiled: %s %s With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00270 
00271     /* Enable I and D cache */
00272     SCB_EnableICache();
00273     SCB_EnableDCache();
00274 
00275     PIO_Configure(spi_pins, PIO_LISTSIZE(spi_pins));
00276     /* Display menu */
00277     DisplayMenu();
00278 
00279     while (1) {
00280         ucKey = DBG_GetChar();
00281 
00282         switch (ucKey) {
00283         case 'h':
00284             DisplayMenu();
00285             break;
00286         case 's':
00287             SpiLoopBack();
00288             break;
00289 
00290         case 'd':
00291             SpiLoopBackDma();
00292         default:
00293             /* Set SPI clock configuration #n */
00294             if ((ucKey >= '0') && (ucKey <= ('0' + 2))) {
00295                 SetClockConfiguration(ucKey - '0');
00296             }
00297             break;
00298         }
00299     }
00300 }
00301 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines