SAMV71 Xplained Ultra Software Package 1.3

main.c

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_spi USART SPI example with DMA
00032  *
00033  *  This example demonstrates the SPI mode provided by the USART peripherals on
00034  *  SAMV7 Microcontrollers.
00035  *
00036  *  \section Requirements
00037  *
00038  *  This example can be used on SAM V71 Xplained Ultra board and USART and SPI
00039  *  pins should be connected on one board as following matching table:
00040  *  - <b> USART0 -- SPI0 </b> (for USART0 as SPI master)
00041  *   - SCK0(PB13 pin05 on J400)            - SCK  (SCK on J506)
00042  *   - TXD0(PB01 pin14 on EXT1)            - MOSI (MOSI on J506)
00043  *   - RXD0(PB00 pin13 on EXT1)            - MISO (MISO on J506)
00044  *   - RTS0(PB03 PIN05 on EXT1)            - NSS  (PB02 pin06 on EXT1)
00045  *
00046  *  - <b> USART0 -- SPI0 </b> (for USART0 as SPI slave)
00047  *   - SCK0(PB13 pin05 on J400)            - SCK  (SCK on J506)
00048  *   - RXD0(PB00 pin13 on EXT1)            - MOSI (MOSI on J506)
00049  *   - TXD0(PB01 pin14 on EXT1)            - MISO (MISO on J506)
00050  *   - CTS0(PB02 PIN06 on EXT1)            - NPCS1(PD25 pin15 on EXT1)
00051  *
00052  *  \section Description
00053  *
00054  * This example demonstrates how to use USART in SPI mode. The USART is
00055  * configured as SPI master and slave. Meanwhile, the SPI peripheral in the
00056  * Microcontroller is configured respectively, making it to communicate with the
00057  * USART peripheral. 
00058  *
00059  * The application first initializes DBGU as the interface to interact with
00060  * users. 
00061  * The application waits for input from DBGU:
00062  * 
00063  * Menu :
00064  * ------
00065  *  - M: Configure USART as spi master
00066  *  - S: Configure USART as spi slave
00067  *  - H: Display this menu
00068  * \section Usage
00069  *
00070  *  -# Build the program and download it inside the SAM V71 Xplained Ultra board.
00071  * Please refer to the Getting Started with SAM V71 Microcontrollers.pdf
00072  *  -# Connect a serial cable to the DBGU port on the evaluation kit.
00073  *  -# On the computer, open and configure a terminal application (e.g.
00074  *     HyperTerminal on Microsoft Windows) with these settings:
00075  *        - 115200 bauds
00076  *        - 8 data bits
00077  *        - No parity
00078  *        - 1 stop bit
00079  *        - No flow control
00080  *  -# Start the application. The following traces shall appear on the terminal:
00081  *     \code
00082  *     -- USART SPI Example xxx --
00083  *     -- SAMxxxxx-xx
00084  *     -- Compiled: xxx xx xxxx xx:xx:xx --
00085  *     \endcode
00086  *
00087  *  \section References
00088  *  - usart_spi/main.c
00089  *  - pio.h
00090  *  - usart.h
00091  */
00092 
00093 /*----------------------------------------------------------------------------
00094  *        Headers
00095  *----------------------------------------------------------------------------*/
00096 
00097 #include "board.h"
00098 
00099 #include <stdbool.h>
00100 #include <stdio.h>
00101 #include <string.h>
00102 
00103 /*----------------------------------------------------------------------------
00104  *        Local definitions
00105  *----------------------------------------------------------------------------*/
00106 
00107 /** Size of the receive buffer in bytes.*/
00108 
00109 /** Pins for USART */
00110 #define PINS_USART_MASTER PIN_USART0_TXD, PIN_USART0_RXD, PIN_USART0_SCK, PIN_USART0_RTS 
00111 #define PINS_USART_SLAVE  PIN_USART0_TXD, PIN_USART0_RXD, PIN_USART0_SCK, PIN_USART0_CTS
00112 
00113 /** Pins for SPI */
00114 #define PINS_SPI0_MASTER  PIN_SPI_MISO, PIN_SPI_MOSI, PIN_SPI_SPCK,  PIN_SPI_NPCS1 
00115 #define PINS_SPI0_SLAVE   PIN_SPI_MISO, PIN_SPI_MOSI, PIN_SPI_SPCK,  PIN_SPI_NPCS0 
00116 
00117 /** Register base for USART */
00118 #define USART             USART0
00119 #define ID_USART          ID_USART0
00120 #define USART_IRQn        USART0_IRQn
00121 #define USART_Handler     USART0_Handler
00122 
00123 /** Register base for SPI */
00124 #define SPI               SPI0
00125 #define ID_SPI            ID_SPI0
00126 #define SPI_IRQn          SPI0_IRQn
00127 #define SPI_Handler       SPI0_Handler
00128 
00129 
00130 /*----------------------------------------------------------------------------
00131  *        Local variables
00132  *----------------------------------------------------------------------------*/
00133 
00134 char pTxBuffer1[] = {"ABCDEFGHIJKLMNOPQRSTUVWXYZ\n\r"};
00135 char pTxBuffer2[] = {"abcdefghijklmnopqrstuvwxyz\n\r"};
00136 
00137 /**  Pins to configure for the application.*/
00138 const Pin pins1[] = {PINS_USART_MASTER, PINS_SPI0_SLAVE}; 
00139 const Pin pins2[] = {PINS_USART_SLAVE, PINS_SPI0_MASTER}; 
00140 
00141 /** Clock -- SPI as master */
00142 static uint32_t spiClock = 5000000;
00143 
00144 /** Clock -- usart as SPI master */
00145 static uint32_t baudRate = 5000000; 
00146 /*----------------------------------------------------------------------------
00147  *        Local functions
00148  *----------------------------------------------------------------------------*/
00149 
00150 /**
00151  * \brief SPI handler
00152  */
00153 void SPI_Handler()
00154 {   
00155     SPI_GetStatus(SPI);
00156 
00157     printf("%c", (char) SPI_Read(SPI));     
00158 }
00159 
00160 /**
00161  * \brief USART handler
00162  */
00163 void USART_Handler(void)
00164 {
00165     volatile uint32_t status;
00166     status = USART_GetStatus(USART);
00167     if ((status & 0x1) == 1)
00168     {
00169       printf("%c", USART_Read( USART, 0));
00170     }    
00171 }
00172 
00173 /**
00174  * \brief Configures an USART baudrate when USART_MODE=SPI.
00175  *
00176  *
00177  *  \param pUsart  Pointer to the USART peripheral to configure.
00178  *  \param baudrate  Baudrate at which the USART should operate (in Hz).
00179  *  \param masterClock  Frequency of the system master clock (in Hz).
00180  */
00181 static void USART_SPI_SetBaudrate(Usart *pUsart,
00182                            uint32_t baudrate,
00183                            uint32_t masterClock)
00184 {
00185     unsigned int CD, FP;
00186 
00187     /* Configure baudrate*/  
00188     CD = (masterClock / baudrate);
00189     FP = ((masterClock / baudrate) - CD);     
00190     
00191     pUsart->US_BRGR = ( US_BRGR_CD(CD) | US_BRGR_FP(FP));    
00192 }
00193 
00194 /**
00195  * \brief Configures an USART peripheral with the specified parameters.
00196  *
00197  *
00198  *  \param pUsart  Pointer to the USART peripheral to configure.
00199  *  \param mode  Desired value for the USART mode register (see the datasheet).
00200  *  \param baudrate  Baudrate at which the USART should operate (in Hz).
00201  *  \param masterClock  Frequency of the system master clock (in Hz).
00202  */
00203 static void USART_SPI_Configure(Usart *pUsart,
00204         uint32_t mode,
00205         uint32_t baudrate,
00206         uint32_t masterClock)
00207 {
00208 
00209     /* Reset and disable receiver & transmitter*/
00210     pUsart->US_CR = US_CR_RSTRX | US_CR_RSTTX
00211         | US_CR_RXDIS | US_CR_TXDIS | US_CR_RSTSTA;
00212 
00213     pUsart->US_IDR = 0xFFFFFFFF;
00214 
00215     /* Configure baudrate*/  
00216     USART_SPI_SetBaudrate(pUsart, baudrate, masterClock);
00217 
00218     /* Configure mode*/
00219     pUsart->US_MR = mode;
00220 
00221     /* Enable receiver and transmitter*/
00222     pUsart->US_CR = US_CR_RXEN | US_CR_TXEN;
00223 
00224 
00225     /* Disable buffering for printf(). */
00226 #if ( defined (__GNUC__) && !defined (__SAMBA__) )
00227     setvbuf(stdout, (char *)NULL, _IONBF, 0);
00228 #endif
00229 
00230 }
00231 
00232 /**
00233  * \brief Configures spi in slave mode.
00234  */
00235 static void _ConfigureSpiSlave( void )
00236 {
00237     /* Configure SPI slave mode */
00238     SPI_Configure(SPI, ID_SPI, SPI_PCS( 0 ));
00239     
00240     NVIC_ClearPendingIRQ(SPI_IRQn);
00241     NVIC_SetPriority( SPI_IRQn ,1);
00242     NVIC_EnableIRQ(SPI_IRQn);
00243     SPI_DisableIt(SPI, 0xffffffff);
00244     
00245     SPI_ConfigureNPCS( SPI, 0, 0);
00246 }
00247 
00248 /**
00249  * \brief Configures USART in spi master mode 
00250  */
00251 static void _ConfigureUsartAsSpiMaster(void)
00252 {
00253     uint32_t usartMode;
00254     /* Configure usart master mode */
00255     usartMode = 0 
00256         | US_MR_USART_MODE_SPI_MASTER
00257         | US_MR_USCLKS_MCK
00258         | US_MR_CHRL_8_BIT
00259         | US_SPI_BPMODE_1
00260         | US_MR_CLKO;
00261      //   | US_MR_WRDBT;
00262     
00263     PMC_EnablePeripheral(ID_USART);
00264     USART_SPI_Configure ( USART, usartMode, baudRate, BOARD_MCK);
00265     
00266     NVIC_ClearPendingIRQ(USART_IRQn);
00267     NVIC_SetPriority( USART_IRQn ,1);
00268     NVIC_EnableIRQ(USART_IRQn);
00269     USART_DisableIt(USART, 0xffffffff);    
00270 }
00271 
00272 /**
00273  * \brief Configures spi in master mode.
00274  */
00275 static void _ConfigureSpiMaster( void )
00276 {
00277     /* Configure SPI master mode */
00278     SPI_Configure(SPI, ID_SPI, (SPI_MR_MSTR | SPI_MR_MODFDIS | SPI_PCS( 1 )));
00279     
00280     NVIC_ClearPendingIRQ(SPI_IRQn);
00281     NVIC_SetPriority( SPI_IRQn ,1);
00282     NVIC_EnableIRQ(SPI_IRQn);
00283     SPI_DisableIt(SPI, 0xffffffff);
00284     
00285     SPI_ConfigureNPCS( SPI, 1,
00286                       SPI_DLYBCT( 100, BOARD_MCK ) | 
00287                       SPI_DLYBS(100, BOARD_MCK) | 
00288                       SPI_SCBR( spiClock, BOARD_MCK) |
00289                       SPI_CSR_BITS_8_BIT ) ;
00290 }
00291 
00292 /**
00293  * \brief Configures USART in spi slave mode 
00294  */
00295 static void _ConfigureUsartAsSpiSlave(void)
00296 {
00297     uint32_t usartMode;
00298     /* Configure usart slave mode */
00299     usartMode = 0 
00300         | US_MR_USART_MODE_SPI_SLAVE
00301         | US_MR_CHRL_8_BIT
00302         | US_SPI_BPMODE_1;
00303     PMC_EnablePeripheral(ID_USART);
00304     USART_SPI_Configure ( USART, usartMode, spiClock, BOARD_MCK);
00305     
00306     NVIC_ClearPendingIRQ(USART_IRQn);
00307     NVIC_SetPriority( USART_IRQn ,1);
00308     NVIC_EnableIRQ(USART_IRQn);
00309     USART_DisableIt(USART, 0xffffffff);
00310 }
00311 
00312 /**
00313  * \brief Display main menu.
00314  */
00315 static void _DisplayMainmenu( void )
00316 {
00317     printf("\n\rMenu :\n\r");
00318     printf("------\n\r");
00319     printf(" - M: Configure USART as spi master\n\r");
00320     printf(" - S: Configure USART as spi slave\n\r");
00321     printf(" - H: Display this menu \n\r");
00322 }
00323 /*----------------------------------------------------------------------------
00324  *        Exported functions
00325  *----------------------------------------------------------------------------*/
00326 /**
00327  *  \brief usart_spi Application entry point.
00328  *
00329  *  \return Unused (ANSI-C compatibility).
00330  */
00331 extern int main( void )
00332 {
00333 
00334     uint8_t ucKey, i ;
00335       
00336     /* Disable watchdog */
00337     WDT_Disable( WDT ) ;
00338 
00339     SCB_EnableICache();
00340     SCB_EnableDCache();
00341 
00342     /* Configure systick for 1 ms. */
00343     TimeTick_Configure ();
00344     
00345     /* Output example information */
00346     printf( "-- USART SPI Example %s --\n\r", SOFTPACK_VERSION ) ;
00347     printf( "-- %s\n\r", BOARD_NAME ) ;
00348     printf( "-- Compiled: %s %s  With %s--\n\r", __DATE__, __TIME__ , COMPILER_NAME) ;
00349 
00350     /* Display menu */
00351     _DisplayMainmenu();
00352 
00353     while ( 1 )
00354     {
00355         ucKey = DBG_GetChar() ;
00356         switch ( ucKey )
00357         {
00358             /*usart as spi master*/
00359             case 'm': case 'M':
00360                 {                    
00361                     /* Configure pins*/
00362                     PIO_Configure( pins1, PIO_LISTSIZE( pins1 ) ) ;
00363                   
00364                     /* Configure USART as SPI master */
00365                     _ConfigureUsartAsSpiMaster();
00366 
00367                     /* Configure SPi slave */
00368                     _ConfigureSpiSlave();
00369                     
00370                     printf( "-I- Configure USART as spi master ...\n\r" ) ;
00371                     
00372                     SPI_EnableIt(SPI, SPI_IER_RDRF);
00373                     SPI_Enable(SPI);
00374                  
00375                     USART_EnableIt(USART, UART_IER_RXRDY);
00376                    
00377                     for (i = 0;(pTxBuffer1[i]!='\0' && pTxBuffer2[i]!='\0'); i++)        
00378                     {  
00379                        while ( (SPI->SPI_SR & SPI_SR_TXEMPTY) == 0 ) ;
00380                        SPI->SPI_TDR = ((uint16_t)pTxBuffer2[i]) | SPI_PCS( 0 ) ;
00381 
00382                        USART_Write( USART, pTxBuffer1[i], 0);
00383                        
00384                     }
00385                     break ;
00386                 }
00387             /*usart as spi slave*/
00388            case 's': case 'S':
00389                 {
00390                     printf( "-I- Configure USART as spi slave...\n\r" ) ;
00391 
00392                     /* Configure pins*/
00393                     PIO_Configure( pins2, PIO_LISTSIZE( pins2 ) ) ;                    
00394                     
00395                     /* Configure USART as SPI slave */
00396                     _ConfigureUsartAsSpiSlave();
00397                     
00398                     /* Configure SPI master */
00399                     _ConfigureSpiMaster();
00400   
00401                     USART_EnableIt(USART, UART_IER_RXRDY);
00402                     
00403                     SPI_EnableIt(SPI,  SPI_IER_RDRF);
00404                     SPI_Enable(SPI);
00405                     
00406                     for (i = 0;(pTxBuffer1[i]!='\0' && pTxBuffer2[i]!='\0'); i++)
00407                     {
00408                       USART_Write( USART, (uint16_t)pTxBuffer2[i], 0);   
00409                       
00410                       SPI_Write( SPI, 1, (uint16_t)pTxBuffer1[i]);  
00411                     }
00412                     break;
00413                 }
00414            case 'h': case 'H':
00415                 {
00416                     _DisplayMainmenu();
00417                     break;
00418                 }
00419         }
00420     }    
00421 }
00422 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines