SAMV71 Xplained Ultra Software Package 1.5

main.c

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_spi USART SPI example with DMA
00032  *
00033  *  This example demonstrates the SPI mode provided by the USART peripherals on
00034  *  SAMV7/E7 Microcontrollers.
00035  *
00036  *  \section Requirements
00037  *
00038  *  This package can be used with SAMV71 Xplained Ultra board or SAME70 Xplained board.
00039  *  Please connect the USART and SPI pins on one board as following matching table:
00040  *  - <b> USART0 -- SPI0 </b> (for USART0 as SPI master)
00041  *   - SCK0(PB13 pin05 on J504)            - 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 J504)            - 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 board.
00071  *  Please refer to the Getting Started with SAM V71/E70 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 baud rate
00076  *        - 8 bits of data
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(void)
00154 {
00155     SPI_GetStatus(SPI);
00156     printf("%c", (char) SPI_Read(SPI));
00157 }
00158 
00159 /**
00160  * \brief USART handler
00161  */
00162 void USART_Handler(void)
00163 {
00164     volatile uint32_t status;
00165     status = USART_GetStatus(USART);
00166     if ((status & 0x1) == 1)
00167         printf("%c", USART_Read(USART, 0));
00168 }
00169 
00170 /**
00171  * \brief Configures an USART baudrate when USART_MODE=SPI.
00172  *
00173  *
00174  *  \param pUsart  Pointer to the USART peripheral to configure.
00175  *  \param baudrate  Baudrate at which the USART should operate (in Hz).
00176  *  \param masterClock  Frequency of the system master clock (in Hz).
00177  */
00178 static void USART_SPI_SetBaudrate(Usart *pUsart,
00179                                 uint32_t baudrate,
00180                                 uint32_t masterClock)
00181 {
00182     unsigned int CD, FP;
00183 
00184     /* Configure baudrate*/
00185     CD = (masterClock / baudrate);
00186     FP = ((masterClock / baudrate) - CD);
00187 
00188     pUsart->US_BRGR = (US_BRGR_CD(CD) | US_BRGR_FP(FP));
00189 }
00190 
00191 /**
00192  * \brief Configures an USART peripheral with the specified parameters.
00193  *
00194  *
00195  *  \param pUsart  Pointer to the USART peripheral to configure.
00196  *  \param mode  Desired value for the USART mode register (see the datasheet).
00197  *  \param baudrate  Baudrate at which the USART should operate (in Hz).
00198  *  \param masterClock  Frequency of the system master clock (in Hz).
00199  */
00200 static void USART_SPI_Configure(Usart *pUsart,
00201         uint32_t mode,
00202         uint32_t baudrate,
00203         uint32_t masterClock)
00204 {
00205 
00206     /* Reset and disable receiver & transmitter*/
00207     pUsart->US_CR = US_CR_RSTRX | US_CR_RSTTX
00208         | US_CR_RXDIS | US_CR_TXDIS | US_CR_RSTSTA;
00209 
00210     pUsart->US_IDR = 0xFFFFFFFF;
00211 
00212     /* Configure baudrate*/
00213     USART_SPI_SetBaudrate(pUsart, baudrate, masterClock);
00214 
00215     /* Configure mode*/
00216     pUsart->US_MR = mode;
00217 
00218     /* Enable receiver and transmitter*/
00219     pUsart->US_CR = US_CR_RXEN | US_CR_TXEN;
00220 
00221 
00222     /* Disable buffering for printf(). */
00223 #if ( defined (__GNUC__) && !defined (__SAMBA__) )
00224     setvbuf(stdout, (char *)NULL, _IONBF, 0);
00225 #endif
00226 
00227 }
00228 
00229 /**
00230  * \brief Configures spi in slave mode.
00231  */
00232 static void _ConfigureSpiSlave( void )
00233 {
00234     /* Configure SPI slave mode */
00235     SPI_Configure(SPI, ID_SPI, SPI_PCS(0));
00236 
00237     NVIC_ClearPendingIRQ(SPI_IRQn);
00238     NVIC_SetPriority(SPI_IRQn ,1);
00239     NVIC_EnableIRQ(SPI_IRQn);
00240     SPI_DisableIt(SPI, 0xffffffff);
00241 
00242     SPI_ConfigureNPCS(SPI, 0, 0);
00243 }
00244 
00245 /**
00246  * \brief Configures USART in spi master mode
00247  */
00248 static void _ConfigureUsartAsSpiMaster(void)
00249 {
00250     uint32_t usartMode;
00251 
00252     /* Configure usart master mode */
00253     usartMode = 0
00254         | US_MR_USART_MODE_SPI_MASTER
00255         | US_MR_USCLKS_MCK
00256         | US_MR_CHRL_8_BIT
00257         | US_SPI_BPMODE_1
00258         | US_MR_CLKO;
00259 
00260     PMC_EnablePeripheral(ID_USART);
00261     USART_SPI_Configure(USART, usartMode, baudRate, BOARD_MCK);
00262 
00263     NVIC_ClearPendingIRQ(USART_IRQn);
00264     NVIC_SetPriority(USART_IRQn ,1);
00265     NVIC_EnableIRQ(USART_IRQn);
00266     USART_DisableIt(USART, 0xffffffff);
00267 }
00268 
00269 /**
00270  * \brief Configures spi in master mode.
00271  */
00272 static void _ConfigureSpiMaster(void)
00273 {
00274     /* Configure SPI master mode */
00275     SPI_Configure(SPI, ID_SPI, (SPI_MR_MSTR | SPI_MR_MODFDIS | SPI_PCS(1)));
00276 
00277     NVIC_ClearPendingIRQ(SPI_IRQn);
00278     NVIC_SetPriority(SPI_IRQn ,1);
00279     NVIC_EnableIRQ(SPI_IRQn);
00280     SPI_DisableIt(SPI, 0xffffffff);
00281 
00282     SPI_ConfigureNPCS(SPI, 1,
00283                       SPI_DLYBCT( 100, BOARD_MCK ) |
00284                       SPI_DLYBS(100, BOARD_MCK) |
00285                       SPI_SCBR( spiClock, BOARD_MCK) |
00286                       SPI_CSR_BITS_8_BIT);
00287 }
00288 
00289 /**
00290  * \brief Configures USART in spi slave mode
00291  */
00292 static void _ConfigureUsartAsSpiSlave(void)
00293 {
00294     uint32_t usartMode;
00295 
00296     /* Configure usart slave mode */
00297     usartMode = 0
00298         | US_MR_USART_MODE_SPI_SLAVE
00299         | US_MR_CHRL_8_BIT
00300         | US_SPI_BPMODE_1;
00301     PMC_EnablePeripheral(ID_USART);
00302     USART_SPI_Configure(USART, usartMode, spiClock, BOARD_MCK);
00303 
00304     NVIC_ClearPendingIRQ(USART_IRQn);
00305     NVIC_SetPriority(USART_IRQn ,1);
00306     NVIC_EnableIRQ(USART_IRQn);
00307     USART_DisableIt(USART, 0xffffffff);
00308 }
00309 
00310 /**
00311  * \brief Display main menu.
00312  */
00313 static void _DisplayMainmenu(void)
00314 {
00315     printf("\n\rMenu :\n\r");
00316     printf("------\n\r");
00317     printf(" - M: Configure USART as spi master\n\r");
00318     printf(" - S: Configure USART as spi slave\n\r");
00319     printf(" - H: Display this menu \n\r");
00320 }
00321 
00322 /*----------------------------------------------------------------------------
00323  *        Exported functions
00324  *----------------------------------------------------------------------------*/
00325 /**
00326  *  \brief usart_spi Application entry point.
00327  *
00328  *  \return Unused (ANSI-C compatibility).
00329  */
00330 extern int main(void)
00331 {
00332     uint8_t ucKey, i;
00333 
00334     /* Disable watchdog */
00335     WDT_Disable(WDT);
00336 
00337     SCB_EnableICache();
00338     SCB_EnableDCache();
00339 
00340     /* Configure systick for 1 ms. */
00341     TimeTick_Configure();
00342 
00343     /* Output example information */
00344     printf("-- USART SPI Example %s --\n\r", SOFTPACK_VERSION);
00345     printf("-- %s\n\r", BOARD_NAME);
00346     printf("-- Compiled: %s %s  With %s--\n\r", __DATE__, __TIME__, COMPILER_NAME);
00347 
00348     /* Display menu */
00349     _DisplayMainmenu();
00350 
00351     while (1) {
00352         ucKey = DBG_GetChar();
00353         switch (ucKey) {
00354             /*usart as spi master*/
00355         case 'm':
00356         case 'M':
00357             /* Configure pins*/
00358             PIO_Configure(pins1, PIO_LISTSIZE(pins1));
00359             /* Configure USART as SPI master */
00360             _ConfigureUsartAsSpiMaster();
00361 
00362             /* Configure SPi slave */
00363             _ConfigureSpiSlave();
00364             printf("-I- Configure USART as spi master ...\n\r");
00365             SPI_EnableIt(SPI, SPI_IER_RDRF);
00366             SPI_Enable(SPI);
00367 
00368             USART_EnableIt(USART, UART_IER_RXRDY);
00369 
00370             for (i = 0; (pTxBuffer1[i]!='\0' && pTxBuffer2[i]!='\0'); i++) {
00371                 while ((SPI->SPI_SR & SPI_SR_TXEMPTY) == 0);
00372                 SPI->SPI_TDR = ((uint16_t)pTxBuffer2[i]) | SPI_PCS( 0 );
00373                 USART_Write( USART, pTxBuffer1[i], 0);
00374             }
00375             break;
00376 
00377             /*usart as spi slave*/
00378         case 's':
00379         case 'S':
00380             printf("-I- Configure USART as spi slave...\n\r");
00381             /* Configure pins*/
00382             PIO_Configure(pins2, PIO_LISTSIZE(pins2));
00383             /* Configure USART as SPI slave */
00384             _ConfigureUsartAsSpiSlave();
00385             /* Configure SPI master */
00386             _ConfigureSpiMaster();
00387             USART_EnableIt(USART, UART_IER_RXRDY);
00388             SPI_EnableIt(SPI, SPI_IER_RDRF);
00389             SPI_Enable(SPI);
00390             for (i = 0; (pTxBuffer1[i]!='\0' && pTxBuffer2[i]!='\0'); i++) {
00391                 USART_Write(USART, (uint16_t)pTxBuffer2[i], 0);
00392                 SPI_Write( SPI, 1, (uint16_t)pTxBuffer1[i]);
00393             }
00394             break;
00395 
00396         case 'h':
00397         case 'H':
00398             _DisplayMainmenu();
00399             break;
00400         }
00401     }
00402 }
00403 /** \endcond */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines