SAMV71 Xplained Ultra Software Package 1.4

dbg_console.c

Go to the documentation of this file.
00001 /* ----------------------------------------------------------------------------
00002  *         SAM Software Package License
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2012, 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  * \file
00032  *
00033  * Implements UART console.
00034  *
00035  */
00036 
00037 /*----------------------------------------------------------------------------
00038  *        Headers
00039  *----------------------------------------------------------------------------*/
00040 
00041 #include "board.h"
00042 
00043 #include <stdio.h>
00044 #include <stdint.h>
00045 
00046 /*----------------------------------------------------------------------------
00047  *        Definitions
00048  *----------------------------------------------------------------------------*/
00049 
00050 /** Console baud rate always using 115200. */
00051 
00052 
00053 #define CONSOLE_BAUDRATE    115200
00054 
00055 /** EDBG used USART1 as the console, but LON support on USART1 only */
00056 #ifndef USART_LON
00057 #define CONSOLE_EDBG 
00058 #endif
00059 
00060 #if defined CONSOLE_EDBG
00061 #define CONSOLE_ON_USART
00062 #else
00063 #define CONSOLE_ON_UART
00064 #endif
00065 
00066 #if defined CONSOLE_ON_UART
00067 #if defined SSC_AUDIO || defined USART_LON
00068 /** Usart Hw interface used by the console (UART4). */
00069 #warning Please use UART4 pins for debug consol as UART0 pins are used in SSC \
00070         audio for SAM V71 Xplained Ultra board
00071 #define CONSOLE_UART       UART4
00072 
00073 /** Pins description corresponding to Rxd,Txd, (UART pins) */
00074 #define CONSOLE_PINS        {PINS_UART4}
00075 
00076 #define CONSOLE_ID          ID_UART4
00077 #else
00078 /** Usart Hw interface used by the console (UART0). */
00079 #define CONSOLE_UART       UART0
00080 
00081 /** Pins description corresponding to Rxd,Txd, (UART pins) */
00082 #define CONSOLE_PINS        {PINS_UART0}
00083 
00084 #define CONSOLE_ID          ID_UART0
00085 
00086 #endif
00087 #endif
00088 
00089 #if defined CONSOLE_ON_USART
00090 
00091 /** USART1 pin RX */
00092 #define PIN_USART1_RXD_DBG \
00093         {PIO_PA21A_RXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
00094 /** USART1 pin TX */
00095 #define PIN_USART1_TXD_DBG \
00096         {PIO_PB4D_TXD1, PIOB, ID_PIOB, PIO_PERIPH_D, PIO_DEFAULT}
00097 #define PINS_USART1        PIN_USART1_TXD_DBG, PIN_USART1_RXD_DBG
00098 
00099 /** Usart Hw interface used by the console (Usart0). */
00100 #define CONSOLE_Usart      USART1
00101 
00102 /** Pins description corresponding to Rxd,Txd, (Usart pins) */
00103 #define CONSOLE_PINS      {PINS_USART1}
00104 
00105 #define CONSOLE_ID        ID_USART1
00106 #endif
00107 
00108 
00109 /*----------------------------------------------------------------------------
00110  *        Variables
00111  *----------------------------------------------------------------------------*/
00112 
00113 /** Is Console Initialized. */
00114 static uint8_t _ucIsConsoleInitialized = 0;
00115 
00116 /**
00117  * \brief Configures an USART peripheral with the specified parameters.
00118  *
00119  * \param baudrate  Baudrate at which the USART should operate (in Hz).
00120  * \param masterClock  Frequency of the system master clock (in Hz).
00121  */
00122 extern void DBG_Configure( uint32_t baudrate, uint32_t masterClock)
00123 {
00124 
00125     const Pin pPins[] = CONSOLE_PINS;
00126 #if defined CONSOLE_ON_UART
00127     Uart *pUart = CONSOLE_UART;
00128     /* Configure PIO */
00129     PIO_Configure( pPins, PIO_LISTSIZE( pPins ) );
00130     
00131     // Reset & disable receiver and transmitter, disable interrupts
00132     pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RSTSTA;
00133     pUart->UART_IDR = 0xFFFFFFFF;
00134     PMC_EnablePeripheral(CONSOLE_ID);
00135     pUart->UART_BRGR = (masterClock / baudrate) / 16;
00136     // Configure mode register
00137     pUart->UART_MR 
00138             = (UART_MR_CHMODE_NORMAL | UART_MR_PAR_NO
00139                     | UART_MR_BRSRCCK_PERIPH_CLK);
00140     // Enable receiver and transmitter
00141     pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;
00142 #endif
00143 
00144 #if defined CONSOLE_ON_USART
00145     Usart *pUsart = CONSOLE_Usart;
00146     // Disable the MATRIX registers write protection
00147     MATRIX->MATRIX_WPMR  = MATRIX_WPMR_WPKEY_PASSWD;
00148     MATRIX->CCFG_SYSIO |= CCFG_SYSIO_SYSIO4;
00149   
00150     PIO_Configure( pPins, PIO_LISTSIZE( pPins ) );
00151     
00152     // Reset & disable receiver and transmitter, disable interrupts
00153     pUsart->US_CR = US_CR_RSTRX | US_CR_RSTTX | US_CR_RSTSTA;
00154     pUsart->US_IDR = 0xFFFFFFFF;
00155     PMC_EnablePeripheral(CONSOLE_ID);
00156     pUsart->US_BRGR = (masterClock / baudrate) / 16;
00157    
00158     // Configure mode register
00159     pUsart->US_MR 
00160             = (US_MR_USART_MODE_NORMAL | US_MR_PAR_NO| US_MR_USCLKS_MCK 
00161                     | US_MR_CHRL_8_BIT);
00162 
00163     // Enable receiver and transmitter
00164     pUsart->US_CR = US_CR_RXEN | US_CR_TXEN;
00165 #endif
00166     _ucIsConsoleInitialized = 1;
00167 
00168     /* Disable buffering for printf(). */
00169 #if ( defined (__GNUC__) && !defined (__SAMBA__) )
00170     setvbuf(stdout, (char *)NULL, _IONBF, 0);
00171 #endif
00172 }
00173 
00174 /**
00175  * \brief Outputs a character on the UART line.
00176  *
00177  * \note This function is synchronous (i.e. uses polling).
00178  * \param c  Character to send.
00179  */
00180 extern void DBG_PutChar( uint8_t c )
00181 {
00182 #if defined CONSOLE_ON_UART
00183     Uart *pUart=CONSOLE_UART;
00184     if ( !_ucIsConsoleInitialized )
00185     {
00186         DBG_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
00187     }
00188     // Wait for the transmitter to be ready
00189     while ((pUart->UART_SR & UART_SR_TXEMPTY) == 0);
00190 
00191     // Send character
00192     pUart->UART_THR = c;
00193     // Wait for the transfer to complete
00194     while ((pUart->UART_SR & UART_SR_TXEMPTY) == 0);
00195 #endif
00196 
00197 #if defined CONSOLE_ON_USART
00198     Usart *pUsart=CONSOLE_Usart;
00199     if ( !_ucIsConsoleInitialized )
00200     {
00201         DBG_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
00202     }
00203     // Wait for the transmitter to be ready
00204     while ((pUsart->US_CSR & US_CSR_TXEMPTY) == 0);
00205 
00206     // Send character
00207     pUsart->US_THR = c;
00208 
00209     // Wait for the transfer to complete
00210     while ((pUsart->US_CSR & US_CSR_TXEMPTY) == 0);
00211 #endif
00212 }
00213 
00214 /**
00215  * \brief Input a character from the UART line.
00216  *
00217  * \note This function is synchronous
00218  * \return character received.
00219  */
00220 extern uint32_t DBG_GetChar( void )
00221 {
00222 #if defined CONSOLE_ON_UART
00223     Uart *pUart= CONSOLE_UART;
00224 
00225     if ( !_ucIsConsoleInitialized )
00226     {
00227         DBG_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
00228     }
00229 
00230     while ((pUart->UART_SR & UART_SR_RXRDY) == 0);
00231     return pUart->UART_RHR;
00232 #endif
00233 
00234 #if defined CONSOLE_ON_USART
00235     Usart *pUsart= CONSOLE_Usart;
00236 
00237     if ( !_ucIsConsoleInitialized )
00238     {
00239         DBG_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
00240     }
00241 
00242     while ((pUsart->US_CSR & US_CSR_RXRDY) == 0);
00243     return pUsart->US_RHR;
00244 #endif
00245 }
00246 
00247 /**
00248  * \brief Check if there is Input from UART line.
00249  *
00250  * \return true if there is Input.
00251  */
00252 extern uint32_t DBG_IsRxReady( void )
00253 {
00254 #if defined CONSOLE_ON_UART
00255     Uart *pUart=CONSOLE_UART;
00256 
00257     if ( !_ucIsConsoleInitialized )
00258     {
00259         DBG_Configure( CONSOLE_BAUDRATE, BOARD_MCK );
00260     }
00261     return (pUart->UART_SR & UART_SR_RXRDY);
00262 #endif
00263 
00264 #if defined CONSOLE_ON_USART
00265     Usart *pUsart=CONSOLE_Usart;
00266 
00267     if ( !_ucIsConsoleInitialized )
00268     {
00269         DBG_Configure( CONSOLE_BAUDRATE, BOARD_MCK );
00270     }
00271 
00272     return (pUsart->US_CSR & US_CSR_RXRDY);
00273 #endif
00274 }
00275 
00276 /**
00277  *  Displays the content of the given frame on the UART0.
00278  *
00279  *  \param pucFrame Pointer to the frame to dump.
00280  *  \param dwSize   Buffer size in bytes.
00281  */
00282 extern void DBG_DumpFrame( uint8_t* pucFrame, uint32_t dwSize )
00283 {
00284     uint32_t dw;
00285 
00286     for ( dw=0; dw < dwSize; dw++ )
00287     {
00288         printf( "%02X ", pucFrame[dw] );
00289     }
00290 
00291     printf( "\n\r" );
00292 }
00293 
00294 /**
00295  *  Displays the content of the given buffer on the UART0.
00296  *
00297  *  \param pucBuffer  Pointer to the buffer to dump.
00298  *  \param dwSize     Buffer size in bytes.
00299  *  \param dwAddress  Start address to display
00300  */
00301 extern void DBG_DumpMemory( uint8_t* pucBuffer, uint32_t dwSize, 
00302                 uint32_t dwAddress )
00303 {
00304     uint32_t i;
00305     uint32_t j;
00306     uint32_t dwLastLineStart;
00307     uint8_t* pucTmp;
00308 
00309     for (i=0; i < (dwSize / 16); i++ )
00310     {
00311         printf( "0x%08X: ", (unsigned int)(dwAddress + (i*16)));
00312         pucTmp = (uint8_t*)&pucBuffer[i*16];
00313 
00314         for (j=0; j < 4; j++)
00315         {
00316             printf( "%02X%02X%02X%02X ",
00317                     pucTmp[0], pucTmp[1], pucTmp[2], pucTmp[3]);
00318             pucTmp += 4;
00319         }
00320 
00321         pucTmp=(uint8_t*)&pucBuffer[i*16];
00322 
00323         for (j=0; j < 16; j++)
00324         {
00325             DBG_PutChar( *pucTmp++);
00326         }
00327 
00328         printf( "\n\r" );
00329     }
00330 
00331     if ( (dwSize%16) != 0 )
00332     {
00333         dwLastLineStart=dwSize - (dwSize%16);
00334 
00335         printf( "0x%08X: ", (unsigned int)(dwAddress + dwLastLineStart));
00336         for (j=dwLastLineStart; j < dwLastLineStart+16; j++)
00337         {
00338             if ( (j!=dwLastLineStart) && (j%4 == 0) )
00339             {
00340                 printf( " " );
00341             }
00342 
00343             if ( j < dwSize )
00344             {
00345                 printf( "%02X", pucBuffer[j] );
00346             }
00347             else
00348             {
00349                 printf("  ");
00350             }
00351         }
00352 
00353         printf( " " );
00354         for (j=dwLastLineStart; j < dwSize; j++)
00355         {
00356             DBG_PutChar( pucBuffer[j] );
00357         }
00358 
00359         printf( "\n\r" );
00360     }
00361 }
00362 
00363 /**
00364  * Reads an integer
00365  *
00366  * \param pdwValue  Pointer to a integer variable to contain the input value.
00367  *
00368  * \return success(1) or failure(0)
00369  */
00370 extern uint32_t DBG_GetInteger( int32_t* pdwValue )
00371 {
00372     uint8_t ucKey;
00373     uint8_t ucNum = 0;
00374     int32_t dwValue = 0;
00375     int32_t sign = 1;
00376 
00377     while (1)
00378     {
00379         ucKey=DBG_GetChar();
00380         DBG_PutChar( ucKey );
00381 
00382         if (((ucKey == '-') || (ucKey == '+')) && (ucNum == 0))
00383         {
00384             if (ucKey == '-')
00385             {
00386                 sign = -1;
00387             }
00388             else
00389             {
00390                 sign = 1;
00391             }
00392             ucNum++;
00393         }
00394         else
00395         {
00396             if (ucKey >= '0' && ucKey <= '9')
00397             {
00398                 dwValue = (dwValue * 10) + (ucKey - '0');
00399                 ucNum++;
00400             }
00401             else
00402             {
00403                 if (ucKey == 0x0D || ucKey == ' ')
00404                 {
00405                     if ( ucNum == 0 )
00406                     {
00407                         printf("\n\rWrite a number and press ENTER or SPACE!\n\r");
00408                         return 0;
00409                     }
00410                     else
00411                     {
00412                         printf( "\n\r" );
00413                         *pdwValue = dwValue * sign;
00414 
00415                         return 1;
00416                     }
00417                 }
00418                 else
00419                 {
00420                     printf("\n\r'%c' not a number or sign(+/-)!\n\r", ucKey);
00421                     return 0;
00422                 }
00423             }
00424         }
00425     }
00426 }
00427 
00428 /**
00429  * Reads an integer and check the value
00430  *
00431  * \param pdwValue  Pointer to a integer variable to contain the input value.
00432  * \param dwMin     Minimum value
00433  * \param dwMax     Maximum value
00434  *
00435  * \return success(1) or failure(0)
00436  */
00437 extern uint32_t DBG_GetIntegerMinMax(int32_t* pdwValue, int32_t dwMin,
00438                 int32_t dwMax)
00439 {
00440     int32_t dwValue = 0;
00441 
00442     if ( DBG_GetInteger( &dwValue ) == 0 )
00443     {
00444         return 0;
00445     }
00446 
00447     if ( dwValue < dwMin || dwValue > dwMax )
00448     {
00449         printf( "\n\rThe number have to be between %d and %d\n\r",
00450                 (int)dwMin, (int)dwMax );
00451 
00452         return 0;
00453     }
00454 
00455     printf( "\n\r" );
00456 
00457     *pdwValue = dwValue;
00458 
00459     return 1;
00460 }
00461 
00462 /**
00463  *  Reads an hexadecimal number
00464  *
00465  *  \param pdwValue  Pointer to the uint32_t variable to contain the input value.
00466  */
00467 extern uint32_t DBG_GetHexa32( uint32_t* pdwValue )
00468 {
00469     uint8_t ucKey;
00470     uint32_t dw = 0;
00471     uint32_t dwValue = 0;
00472 
00473     for ( dw=0; dw < 8; dw++ )
00474     {
00475         ucKey = DBG_GetChar();
00476         DBG_PutChar( ucKey );
00477 
00478         if ( ucKey >= '0' &&  ucKey <= '9' )
00479         {
00480             dwValue = (dwValue * 16) + (ucKey - '0');
00481         }
00482         else
00483         {
00484             if ( ucKey >= 'A' &&  ucKey <= 'F' )
00485             {
00486                 dwValue = (dwValue * 16) + (ucKey - 'A' + 10);
00487             }
00488             else
00489             {
00490                 if ( ucKey >= 'a' &&  ucKey <= 'f' )
00491                 {
00492                     dwValue = (dwValue * 16) + (ucKey - 'a' + 10);
00493                 }
00494                 else
00495                 {
00496                     printf( "\n\rIt is not a hexadecimal character!\n\r" );
00497 
00498                     return 0;
00499                 }
00500             }
00501         }
00502     }
00503 
00504     printf("\n\r" );
00505     *pdwValue = dwValue;
00506 
00507     return 1;
00508 }
00509 
00510 #if defined __ICCARM__ /* IAR Ewarm 5.41+ */
00511 /**
00512  * \brief Outputs a character on the UART.
00513  *
00514  * \param c  Character to output.
00515  *
00516  * \return The character that was output.
00517  */
00518 extern WEAK signed int putchar( signed int c )
00519 {
00520     DBG_PutChar( c );
00521 
00522     return c;
00523 }
00524 
00525 #endif  // defined __ICCARM__
00526 extern WEAK int puts(const char *ptr )
00527 {
00528 
00529     for (; *ptr != 0; ptr++ )
00530     {
00531         DBG_PutChar( *ptr );
00532     }
00533 
00534     return 0;
00535 
00536 }
00537 
00538 extern WEAK char * gets(char *ptr)
00539 {
00540     uint8_t ch = 0;
00541     while (ch != '\r' )
00542     {
00543         ch = DBG_GetChar();
00544         DBG_PutChar( ch );
00545         *(ptr++) = ch;
00546     }
00547     *ptr = '\0';
00548     return 0;
00549 
00550 }
00551 
00552 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines