aboutsummaryrefslogtreecommitdiffstats
path: root/firmware/libboard/common/source/uart_console.c
blob: 3ce733199a3cb2f1bbcfd8a7ab467229134d1adc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support
 * ----------------------------------------------------------------------------
 * Copyright (c) 2009, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ----------------------------------------------------------------------------
 */

/**
 * \file
 *
 * Implements UART console.
 *
 */

/*----------------------------------------------------------------------------
 *        Headers
 *----------------------------------------------------------------------------*/

#include "board.h"

#include <stdio.h>
#include <stdint.h>

#include "ringbuffer.h"

/*----------------------------------------------------------------------------
 *        Definitions
 *----------------------------------------------------------------------------*/

/*----------------------------------------------------------------------------
 *        Variables
 *----------------------------------------------------------------------------*/

/** Is Console Initialized. */
static uint8_t _ucIsConsoleInitialized=0;
/** Ring buffer to queue data to be sent */
static ringbuf uart_tx_buffer;

/**
 * \brief Configures an USART peripheral with the specified parameters.
 *
 * \param baudrate  Baudrate at which the USART should operate (in Hz).
 * \param masterClock  Frequency of the system master clock (in Hz).
 */
extern void UART_Configure( uint32_t baudrate, uint32_t masterClock)
{
	const Pin pPins[] = CONSOLE_PINS;
	Uart *pUart = CONSOLE_UART;

	/* Configure PIO */
	PIO_Configure(pPins, PIO_LISTSIZE(pPins));

	/* Configure PMC */
	PMC->PMC_PCER0 = 1 << CONSOLE_ID;

	/* Reset and disable receiver & transmitter */
	pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX
				   | UART_CR_RXDIS | UART_CR_TXDIS;

	/* Configure mode */
	pUart->UART_MR =  UART_MR_PAR_NO;

	/* Configure baudrate */
	/* Asynchronous, no oversampling */
	pUart->UART_BRGR = (masterClock / baudrate) / 16;

	/* Disable PDC channel */
	pUart->UART_PTCR = UART_PTCR_RXTDIS | UART_PTCR_TXTDIS;

	/* Reset transmit ring buffer */
	rbuf_reset(&uart_tx_buffer);

	/* Enable TX interrupts */
	pUart->UART_IER = UART_IER_TXRDY;
	NVIC_EnableIRQ(CONSOLE_IRQ);
	
	/* Enable receiver and transmitter */
	pUart->UART_CR = UART_CR_RXEN | UART_CR_TXEN;

	/* Remember the configuration is complete */
	_ucIsConsoleInitialized=1 ;
}

/**
 * \brief Disables the USART peripheral and related IRQ
 */
void UART_Exit(void)
{
	if (!_ucIsConsoleInitialized) {
		return;
	}

	Uart *pUart = CONSOLE_UART;
	pUart->UART_IDR = UART_IDR_TXRDY;
	pUart->UART_CR = UART_CR_RSTRX | UART_CR_RSTTX | UART_CR_RXDIS | UART_CR_TXDIS | UART_CR_RSTSTA;
	PMC->PMC_PCDR0 = 1 << CONSOLE_ID;
	NVIC_DisableIRQ(CONSOLE_IRQ);
}

/** Interrupt Service routine to transmit queued data */
void CONSOLE_ISR(void)
{
	Uart *uart = CONSOLE_UART;
	if (uart->UART_SR & UART_SR_TXRDY) {
		if (!rbuf_is_empty(&uart_tx_buffer)) {
			uart->UART_THR = rbuf_read(&uart_tx_buffer);
		} else {
			uart->UART_IDR = UART_IER_TXRDY;
		}
	}
}

/**
 * \brief Outputs a character on the UART line.
 *
 * \note This function is synchronous (i.e. uses polling).
 * \param c  Character to send.
 */
extern void UART_PutChar( uint8_t c )
{
	Uart *pUart = CONSOLE_UART ;

	/* Initialize console is not already done */
	if ( !_ucIsConsoleInitialized )
	{
		UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
	}

	/* Only store input if buffer is not full, else drop it */
	if (!rbuf_is_full(&uart_tx_buffer)) {
		rbuf_write(&uart_tx_buffer, c);
		if (!(pUart->UART_IMR & UART_IMR_TXRDY)) {
			pUart->UART_IER = UART_IER_TXRDY;
			CONSOLE_ISR();
		}
	}
}

/**
 * \brief Input a character from the UART line.
 *
 * \note This function is synchronous
 * \return character received.
 */
extern uint32_t UART_GetChar( void )
{
	Uart *pUart = CONSOLE_UART ;

	if ( !_ucIsConsoleInitialized )
	{
		UART_Configure(CONSOLE_BAUDRATE, BOARD_MCK);
	}

	while ( (pUart->UART_SR & UART_SR_RXRDY) == 0 )
		WDT_Restart(WDT);

	return pUart->UART_RHR ;
}

/**
 * \brief Check if there is Input from UART line.
 *
 * \return true if there is Input.
 */
extern uint32_t UART_IsRxReady( void )
{
	Uart *pUart = CONSOLE_UART;

	if ( !_ucIsConsoleInitialized )
	{
		UART_Configure( CONSOLE_BAUDRATE, BOARD_MCK ) ;
	}

	return (pUart->UART_SR & UART_SR_RXRDY) > 0 ;
}

/**
 *  Displays the content of the given frame on the UART0.
 *
 *  \param pucFrame Pointer to the frame to dump.
 *  \param dwSize   Buffer size in bytes.
 */
extern void UART_DumpFrame( uint8_t* pucFrame, uint32_t dwSize )
{
	uint32_t dw ;

	for ( dw=0 ; dw < dwSize ; dw++ )
	{
		printf( "%02X ", pucFrame[dw] ) ;
	}

	printf( "\n\r" ) ;
}

/**
 *  Displays the content of the given buffer on the UART0.
 *
 *  \param pucBuffer  Pointer to the buffer to dump.
 *  \param dwSize     Buffer size in bytes.
 *  \param dwAddress  Start address to display
 */
extern void UART_DumpMemory( uint8_t* pucBuffer, uint32_t dwSize, uint32_t dwAddress )
{
	uint32_t i ;
	uint32_t j ;
	uint32_t dwLastLineStart ;
	uint8_t* pucTmp ;

	for ( i=0 ; i < (dwSize / 16) ; i++ )
	{
		printf( "0x%08X: ", (unsigned int)(dwAddress + (i*16)) ) ;
		pucTmp = (uint8_t*)&pucBuffer[i*16] ;

		for ( j=0 ; j < 4 ; j++ )
		{
			printf( "%02X%02X%02X%02X ", pucTmp[0], pucTmp[1], pucTmp[2], pucTmp[3] ) ;
			pucTmp += 4 ;
		}

		pucTmp=(uint8_t*)&pucBuffer[i*16] ;

		for ( j=0 ; j < 16 ; j++ )
		{
			UART_PutChar( *pucTmp++ ) ;
		}

		printf( "\n\r" ) ;
	}

	if ( (dwSize%16) != 0 )
	{
		dwLastLineStart=dwSize - (dwSize%16) ;

		printf( "0x%08X: ", (unsigned int)(dwAddress + dwLastLineStart) ) ;
		for ( j=dwLastLineStart ; j < dwLastLineStart+16 ; j++ )
		{
			if ( (j!=dwLastLineStart) && (j%4 == 0) )
			{
				printf( " " ) ;
			}

			if ( j < dwSize )
			{
				printf( "%02X", pucBuffer[j] ) ;
			}
			else
			{
				printf("  ") ;
			}
		}

		printf( " " ) ;
		for ( j=dwLastLineStart ; j < dwSize ; j++ )
		{
			UART_PutChar( pucBuffer[j] ) ;
		}

		printf( "\n\r" ) ;
	}
}

/**
 *  Reads an integer
 *
 *  \param pdwValue  Pointer to the uint32_t variable to contain the input value.
 */
extern uint32_t UART_GetInteger( uint32_t* pdwValue )
{
	uint8_t ucKey ;
	uint8_t ucNbNb=0 ;
	uint32_t dwValue=0 ;

	while ( 1 )
	{
		ucKey=UART_GetChar() ;
		UART_PutChar( ucKey ) ;

		if ( ucKey >= '0' &&  ucKey <= '9' )
		{
			dwValue = (dwValue * 10) + (ucKey - '0');
			ucNbNb++ ;
		}
		else
		{
			if ( ucKey == 0x0D || ucKey == ' ' )
			{
				if ( ucNbNb == 0 )
				{
					printf( "\n\rWrite a number and press ENTER or SPACE!\n\r" ) ;
					return 0 ;
				}
				else
				{
					printf( "\n\r" ) ;
					*pdwValue=dwValue ;

					return 1 ;
				}
			}
			else
			{
				printf( "\n\r'%c' not a number!\n\r", ucKey ) ;

				return 0 ;
			}
		}
		WDT_Restart(WDT);
	}
}

/**
 *  Reads an integer and check the value
 *
 *  \param pdwValue  Pointer to the uint32_t variable to contain the input value.
 *  \param dwMin     Minimum value
 *  \param dwMax     Maximum value
 */
extern uint32_t UART_GetIntegerMinMax( uint32_t* pdwValue, uint32_t dwMin, uint32_t dwMax )
{
	uint32_t dwValue=0 ;

	if ( UART_GetInteger( &dwValue ) == 0 )
	{
		return 0 ;
	}

	if ( dwValue < dwMin || dwValue > dwMax )
 {
		printf( "\n\rThe number have to be between %d and %d\n\r", (int)dwMin, (int)dwMax ) ;

		return 0 ;
	}

	printf( "\n\r" ) ;

	*pdwValue = dwValue ;

	return 1 ;
}

/**
 *  Reads an hexadecimal number
 *
 *  \param pdwValue  Pointer to the uint32_t variable to contain the input value.
 */
extern uint32_t UART_GetHexa32( uint32_t* pdwValue )
{
	uint8_t ucKey ;
	uint32_t dw = 0 ;
	uint32_t dwValue = 0 ;

	for ( dw=0 ; dw < 8 ; dw++ )
	{
		ucKey = UART_GetChar() ;
		UART_PutChar( ucKey ) ;

		if ( ucKey >= '0' &&  ucKey <= '9' )
		{
			dwValue = (dwValue * 16) + (ucKey - '0') ;
		}
		else
		{
			if ( ucKey >= 'A' &&  ucKey <= 'F' )
			{
				dwValue = (dwValue * 16) + (ucKey - 'A' + 10) ;
			}
			else
			{
				if ( ucKey >= 'a' &&  ucKey <= 'f' )
				{
					dwValue = (dwValue * 16) + (ucKey - 'a' + 10) ;
				}
				else
				{
					printf( "\n\rIt is not a hexa character!\n\r" ) ;

					return 0 ;
				}
			}
		}
	}

	printf("\n\r" ) ;
	*pdwValue = dwValue ;

	return 1 ;
}

#if defined __ICCARM__ /* IAR Ewarm 5.41+ */
/**
 * \brief Outputs a character on the UART.
 *
 * \param c  Character to output.
 *
 * \return The character that was output.
 */
extern WEAK signed int putchar( signed int c )
{
	UART_PutChar( c ) ;

	return c ;
}
#endif // defined __ICCARM__