Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041 #include "board.h"
00042
00043 #include <stdio.h>
00044 #include <stdint.h>
00045
00046
00047
00048
00049
00050
00051
00052
00053 #define CONSOLE_BAUDRATE 115200
00054
00055
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
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
00074 #define CONSOLE_PINS {PINS_UART4}
00075
00076 #define CONSOLE_ID ID_UART4
00077 #else
00078
00079 #define CONSOLE_UART UART0
00080
00081
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
00092 #define PIN_USART1_RXD_DBG \
00093 {PIO_PA21A_RXD1, PIOA, ID_PIOA, PIO_PERIPH_A, PIO_DEFAULT}
00094
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
00100 #define CONSOLE_Usart USART1
00101
00102
00103 #define CONSOLE_PINS {PINS_USART1}
00104
00105 #define CONSOLE_ID ID_USART1
00106 #endif
00107
00108
00109
00110
00111
00112
00113
00114 static uint8_t _ucIsConsoleInitialized = 0;
00115
00116
00117
00118
00119
00120
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
00129 PIO_Configure( pPins, PIO_LISTSIZE( pPins ) );
00130
00131
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
00137 pUart->UART_MR
00138 = (UART_MR_CHMODE_NORMAL | UART_MR_PAR_NO
00139 | UART_MR_BRSRCCK_PERIPH_CLK);
00140
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
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
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
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
00164 pUsart->US_CR = US_CR_RXEN | US_CR_TXEN;
00165 #endif
00166 _ucIsConsoleInitialized = 1;
00167
00168
00169 #if ( defined (__GNUC__) && !defined (__SAMBA__) )
00170 setvbuf(stdout, (char *)NULL, _IONBF, 0);
00171 #endif
00172 }
00173
00174
00175
00176
00177
00178
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
00189 while ((pUart->UART_SR & UART_SR_TXEMPTY) == 0);
00190
00191
00192 pUart->UART_THR = c;
00193
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
00204 while ((pUsart->US_CSR & US_CSR_TXEMPTY) == 0);
00205
00206
00207 pUsart->US_THR = c;
00208
00209
00210 while ((pUsart->US_CSR & US_CSR_TXEMPTY) == 0);
00211 #endif
00212 }
00213
00214
00215
00216
00217
00218
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
00249
00250
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
00278
00279
00280
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
00296
00297
00298
00299
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
00365
00366
00367
00368
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
00430
00431
00432
00433
00434
00435
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
00464
00465
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__
00511
00512
00513
00514
00515
00516
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