SAM3S8/SD8 Software Package: examples/usart_synchronous/main.c Source File

SAM3S8/SD8 Software Package 1.2

# main.c

Go to the documentation of this file.

    00001 /* ----------------------------------------------------------------------------
    00002  *         ATMEL Microcontroller Software Support
    00003  * ----------------------------------------------------------------------------
    00004  * Copyright (c) 2009, 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_synchronous  USART Synchronous Mode Example
    00032  *
    00033  *  \section Purpose
    00034  *
    00035  *  This example demonstrates the Synchronous  mode provided by the USART
    00036  *  peripherals on SAM3S8/SD8.
    00037  *
    00038  *  \section Requirements
    00039  *
    00040  *  This example can be used on sam3s evaluation kit two and requires 2 boards to
    00041  *   be connected directly through populated usart1 pins.
    00042  *   - <b> master  --  slave</b>
    00043  *   - TXD(PA22)   --  RXD(PA21)
    00044  *   - RXD(PA21)   --  TXD(PA22)
    00045  *   - SCK(PA23)   --  SCK(PA23)
    00046  *   - GND         --  GND
    00047  *
    00048  *  \section Description
    00049  *
    00050  *  This application gives an example of how to use USART in synchronous mode.
    00051  *  Synchronous operations provide a high speed transfer capability. The
    00052  *  transfer under this mode needs a pair of master and slave, which is
    00053  *  determined by which one offers the clock source.
    00054  *
    00055  *  The example initialized USART1 as master by default. To enable the
    00056  *  communication between each side of the connection,the user should change
    00057  *  the mode of another side to slave through user interface. If well configured,
    00058  *  transfer could be started by typing 'r' and  'w' from terminal application.
    00059  *  This example also leaves the interface to select the clock frequency.
    00060  *
    00061  *  The meaning of each input character could be found in items of the main menu.
    00062  *
    00063  *
    00064  * \section Usage
    00065  *
    00066  *  -# Build the program and download it inside the evaluation board. Please
    00067  *     refer to the <a href="http://www.atmel.com/dyn/resources/prod_documents/6421B.pdf">SAM-BA User Guide</a>,
    00068  *     the <a href="http://www.atmel.com/dyn/resources/prod_documents/doc6310.pdf">GNU-Based Software Development</a>
    00069  *     application note or to the <a href="http://www.iar.com/website1/1.0.1.0/78/1/">IAR EWARM User and reference guides</a>,
    00070  *     depending on your chosen solution.
    00071  *  -# Connect a serial cable to the UART port on the evaluation kit.
    00072  *  -# On the computer, open and configure a terminal application (e.g.
    00073  *     HyperTerminal on Microsoft Windows) with these settings:
    00074  *        - 115200 bauds
    00075  *        - 8 data bits
    00076  *        - No parity
    00077  *        - 1 stop bit
    00078  *        - No flow control
    00079  *  -# Start the application. The following traces shall appear on the terminal:
    00080  *     \code
    00081  *     -- USART Synchronous Mode Example  xxx --
    00082  *     -- xxxxxx-xx
    00083  *     -- Compiled: xxx xx xxxx xx:xx:xx --
    00084  *     -- Menu Choices for this example --
    00085  *     -- [0-3]:Select clock frequency of master --
    00086  *     -- i: Display configuration info
    00087  *     -- w: Write data block .--
    00088  *     -- r: Read data block.--
    00089  *     -- s: Switch between master and slave mode.--
    00090  *     -- m: Display this menu again.--
    00091  *     --USART1 in MASTER mode--
    00092  *
    00093  *     \endcode
    00094  *  -# The main menu will guide the user to configure the device and conduct
    00095  *     operations.
    00096  *
    00097  *  \section References
    00098  *  - usart_synchronous/main.c
    00099  *  - pio.h
    00100  *  - usart.h
    00101  */
    00102
    00103 /** \file
    00104  *
    00105  *  This file contains all the specific code for the usart_synchronous example.
    00106  *
    00107  */
    00108 /*----------------------------------------------------------------------------
    00109  *         Headers
    00110  *----------------------------------------------------------------------------*/
    00111
    00112 #include "board.h"
    00113
    00114 #include <stdio.h>
    00115 #include <stdbool.h>
    00116 #include <string.h>
    00117
    00118 /*----------------------------------------------------------------------------
    00119  *         Local definition
    00120  *----------------------------------------------------------------------------*/
    00121
    00122
    00123 /** size of the receive buffer used by the PDC, in bytes.*/
    00124 #define BUFFER_SIZE         2000
    00125
    00126
    00127 /** USART1 synchronous master*/
    00128 #define SYNC_MASTER    1
    00129 /** USART1 synchronous slave*/
    00130 #define SYNC_SLAVE    0
    00131
    00132 /** USART1 is reading*/
    00133 #define STATE_READ    0
    00134
    00135 /** USART1 is writing*/
    00136 #define STATE_WRITE   1
    00137
    00138 #define FREQ_OPTIONS_NUM 4
    00139
    00140 /*----------------------------------------------------------------------------
    00141  *         Local variables
    00142  *----------------------------------------------------------------------------*/
    00143
    00144 /** Pins to configure for the application.*/
    00145 const Pin pins[] = { BOARD_PIN_USART_RXD,
    00146                      BOARD_PIN_USART_TXD,
    00147                      PIN_USART1_SCK,
    00148                      };
    00149
    00150 /** Transmit buffer. */
    00151 char
    00152 Buffer[BUFFER_SIZE]=
    00153
    00154  "DESCRIPTION of this example: \n\r \
    00155  **************************************************************************\n\r\
    00156  *  This application gives an example of how to use USART in synchronous mode.\n\r\
    00157  *  Synchronous operations provide a high speed transfer capability. The\n\r\
    00158  *  transfer under this mode needs a pair of master and slave, which is\n\r\
    00159  *  determined by which one offers the clock source.\n\r\
    00160  *  \n\r\
    00161  *  The example initialized USART1 as master by default. To enable the\n\r\
    00162  *  communication between each side of the connection. The user should change\n\r\
    00163  *  the mode of another side to slave through user interface. If well configured,\n\r\
    00164  *  transfer could be started by typing 'r' and  'w' from terminal application.\n\r\
    00165  *  This example also leaves the interface to select the clock frequency.\n\r\
    00166  *  \n\r\
    00167  *  The meaning of each input character could be found in items of the main menu.\n\r\
    00168  *  \n\r\
    00169  **************************************************************************\n\r\
    00170  END of DESCRIPTION \n\r\
    00171  ";
    00172
    00173
    00174 /** buffer for receiving */
    00175 char pRecvBufferUSART1[BUFFER_SIZE]= { 0 };
    00176
    00177 /** reception done*/
    00178 volatile bool recvDone = false;
    00179 /** sending done*/
    00180 volatile bool sentDone = false;
    00181
    00182 /** mode for usart1 and spi ,0 means usart1 as master 1 for
    00183  another state*/
    00184 uint8_t transfer_mode = SYNC_MASTER;
    00185
    00186 /** state of reading or writing*/
    00187 uint8_t state = STATE_WRITE;
    00188
    00189 /** clock frequency*/
    00190 uint32_t frequency[FREQ_OPTIONS_NUM]={1000000UL,4000000UL,10000000UL,16000000UL};
    00191
    00192 /** present freqency index in list frequency[]*/
    00193 uint8_t freq_index = 0;
    00194 /*----------------------------------------------------------------------------
    00195  *         Local functions
    00196  *----------------------------------------------------------------------------*/
    00197
    00198 /* \brief USART1 IRQ handler
    00199  *
    00200  * Interrupt handler for USART. After reception is done,set recvDone to true,
    00201  * and if transmission done, set sentDone to true.
    00202  *
    00203  *----------------------------------------------------------------------------*/
    00204 void USART1_IrqHandler(void)
    00205 {
    00206     uint32_t status;
    00207
    00208     /*  Read USART status */
    00209     status = BOARD_USART_BASE->US_CSR;
    00210     /* Receive buffer is full */
    00211     if ((status & US_CSR_RXBUFF) == US_CSR_RXBUFF && state == STATE_READ) {
    00212         recvDone = true;
    00213         USART_DisableIt(USART1, US_IDR_RXBUFF);
    00214     }
    00215     if ((status & US_CSR_TXBUFE) == US_CSR_TXBUFE && state == STATE_WRITE){
    00216         sentDone = true;
    00217         USART_DisableIt(USART1, US_IDR_TXBUFE);
    00218     }
    00219
    00220 }
    00221
    00222
    00223 /**
    00224  * \brief Configures USART in synchronous mode,8N1
    00225  * \param mode 1 for  master, 0 for slave
    00226  */
    00227 static void _ConfigureUsart( uint8_t isMaster, uint32_t freq )
    00228 {
    00229     uint32_t mode = US_MR_USART_MODE_NORMAL | US_MR_USCLKS_MCK
    00230                     | US_MR_CHMODE_NORMAL | US_MR_CLKO
    00231                     | US_MR_SYNC | US_MR_MSBF
    00232                     | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT| US_MR_PAR_NO;
    00233
    00234     if ( !isMaster )
    00235     {
    00236         mode = US_MR_USART_MODE_NORMAL | US_MR_USCLKS_SCK
    00237         | US_MR_CHMODE_NORMAL
    00238         | US_MR_SYNC | US_MR_MSBF
    00239         | US_MR_CHRL_8_BIT | US_MR_NBSTOP_1_BIT| US_MR_PAR_NO;
    00240     }
    00241     /* Enable the peripheral clock in the PMC */
    00242     PMC_EnablePeripheral( BOARD_ID_USART ) ;
    00243
    00244     /* Configure the USART in the desired mode @USART_SPI_CLK bauds*/
    00245     USART_Configure( BOARD_USART_BASE, mode, freq, BOARD_MCK ) ;
    00246
    00247     /* enable USART1 interrupt */
    00248     NVIC_EnableIRQ( USART1_IRQn ) ;
    00249
    00250     /* Enable receiver & transmitter */
    00251     USART_SetTransmitterEnabled( BOARD_USART_BASE, 1 ) ;
    00252     USART_SetReceiverEnabled( BOARD_USART_BASE, 1 ) ;
    00253 }
    00254 /**
    00255  * \brief Display main menu.
    00256  */
    00257 static void _DisplayMainmenu( void )
    00258 {
    00259     printf("-- Menu Choices for this example --\n\r");
    00260     printf("-- [0-3]:Select clock frequency of master --\n\r");
    00261     printf("-- i: Display configuration info\n\r");
    00262     printf("-- w: Write data block .--\n\r");
    00263     printf("-- r: Read data block.--\n\r");
    00264     printf("-- s: Switch between master and slave mode.--\n\r");
    00265     printf("-- m: Display this menu again.--\n\r");
    00266 }
    00267
    00268 /**
    00269  * \brief Dump buffer to uart
    00270  *
    00271  */
    00272 static void _DumpInfo( char *buf, uint32_t size )
    00273 {
    00274     uint32_t i = 0 ;
    00275
    00276     while ( (i < size) && (buf[i] != 0) )
    00277     {
    00278         printf( "%c", buf[i++] ) ;
    00279     }
    00280 }
    00281
    00282 /*------------------------------------------------------------------------------
    00283  *         Global functions
    00284  *------------------------------------------------------------------------------*/
    00285
    00286
    00287 /**
    00288  * \brief Application entry point.
    00289  *
    00290  * Configures USART1 in synchronous master/slave mode start a transmission
    00291  * between two boards.
    00292  * \return Unused.
    00293  */
    00294 extern int main( void )
    00295 {
    00296     char c;
    00297
    00298     /* Disable watchdog */
    00299     WDT_Disable( WDT ) ;
    00300
    00301     /*  Configure pins */
    00302     PIO_Configure( pins, PIO_LISTSIZE( pins ) ) ;
    00303
    00304     /* Example information log */
    00305     printf( "-- USART Synchronous Mode Example %s --\n\r", SOFTPACK_VERSION ) ;
    00306     printf( "-- %s\n\r", BOARD_NAME ) ;
    00307     printf( "-- Compiled: %s %s --\n\r", __DATE__, __TIME__ ) ;
    00308
    00309     /* display main menu*/
    00310     _DisplayMainmenu() ;
    00311
    00312     /* configure USART1 in  Master and SPI in slave mode*/
    00313     _ConfigureUsart( SYNC_MASTER, frequency[freq_index] ) ;
    00314
    00315     transfer_mode = SYNC_MASTER ;
    00316
    00317     state = STATE_WRITE ;
    00318
    00319     printf( "--USART1 in MASTER mode--\n\r" ) ;
    00320
    00321     while ( 1 )
    00322     {
    00323         c = UART_GetChar() ;
    00324
    00325         switch ( c )
    00326         {
    00327             case '0':
    00328             case '1':
    00329             case '2':
    00330             case '3':
    00331                 freq_index = c - '0';
    00332                 printf("-- The clock frequency is: %u\n\r", (unsigned int)frequency[freq_index] ) ;
    00333                 _ConfigureUsart( SYNC_MASTER, frequency[freq_index] ) ;
    00334             break ;
    00335
    00336             case 'i':
    00337             case 'I':
    00338             if ( transfer_mode == SYNC_MASTER )
    00339             {
    00340                 printf( "-- USART1 is MASTER at %u Hz.\n\r", (unsigned int)frequency[freq_index] ) ;
    00341             }
    00342             else
    00343             {
    00344                 printf( "-- USART1 is SLAVE \n\r" ) ;
    00345             }
    00346             break;
    00347
    00348             case 's':
    00349             case 'S':
    00350             if ( transfer_mode == SYNC_MASTER )
    00351             {
    00352                 transfer_mode = SYNC_SLAVE ;
    00353                 _ConfigureUsart( SYNC_SLAVE, frequency[freq_index] ) ;
    00354                 printf( "--USART1 in SLAVE mode--\n\r" ) ;
    00355             }
    00356             else
    00357             {
    00358                 if ( transfer_mode == SYNC_SLAVE )
    00359                 {
    00360                     transfer_mode = SYNC_MASTER;
    00361                     _ConfigureUsart(SYNC_MASTER,frequency[freq_index]);
    00362                     printf("--USART1 in MASTER mode--\n\r");
    00363                 }
    00364             }
    00365             break;
    00366
    00367             case 'w':
    00368             case 'W':
    00369                 state = STATE_WRITE;
    00370                 USART_WriteBuffer(BOARD_USART_BASE, Buffer, BUFFER_SIZE);
    00371                 USART_EnableIt(BOARD_USART_BASE,US_IER_TXBUFE);
    00372                 while(!sentDone);
    00373                 if(sentDone){
    00374                     printf(" -- %s sent done\n\r", state? "MASTER":"SLAVE" );
    00375
    00376                 }
    00377                 break;
    00378             case 'r':
    00379             case 'R':
    00380                state = STATE_READ;
    00381             if(transfer_mode == SYNC_MASTER){
    00382                 printf("----USART1 MASTER Read----\n\r");
    00383
    00384             }else{
    00385                 printf("----USART1 SLAVE Read----\n\r");
    00386             }
    00387             USART_ReadBuffer(BOARD_USART_BASE, pRecvBufferUSART1, BUFFER_SIZE);
    00388             USART_EnableIt(BOARD_USART_BASE,US_IER_RXBUFF);
    00389             while(!recvDone);
    00390             if(recvDone) {
    00391
    00392                 if(strncmp(pRecvBufferUSART1,Buffer,BUFFER_SIZE)){
    00393                     printf(" -F-: Failed!\n\r");
    00394                 }else{
    00395                     /* successfully received*/
    00396                     _DumpInfo(pRecvBufferUSART1,BUFFER_SIZE);
    00397                 }
    00398                 printf("----END of read----\n\r");
    00399                 memset(pRecvBufferUSART1,0,sizeof(pRecvBufferUSART1));
    00400                 recvDone = false;
    00401
    00402             }
    00403             break;
    00404
    00405
    00406             case 'm':
    00407             case 'M':
    00408                 _DisplayMainmenu() ;
    00409             break;
    00410
    00411         }
    00412
    00413     }
    00414 }
    00415

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines