summaryrefslogtreecommitdiff
path: root/target/linux/at91-2.6/image/dfboot/src/com.c
blob: 38a2898f7ebc78430ca58e434fdf2c1e2acc44ba (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
/*----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support  -  ROUSSET  -
 *----------------------------------------------------------------------------
 * The software is delivered "AS IS" without warranty or condition of any
 * kind, either express, implied or statutory. This includes without
 * limitation any warranty or condition with respect to merchantability or
 * fitness for any particular purpose, or against the infringements of
 * intellectual property rights of others.
 *----------------------------------------------------------------------------
 * File Name           : com.c
 * Object              : 
 * Creation            : HIi   03/27/2003
 *
 *----------------------------------------------------------------------------
 */
#include "AT91RM9200.h"
#include "lib_AT91RM9200.h"
#include "config.h"
#include "com.h"
#include "stdio.h"

static char erase_seq[] = "\b \b";		/* erase sequence	*/

unsigned int usa[2] = {(unsigned int)AT91C_BASE_DBGU, (unsigned int)AT91C_ALTERNATE_USART};
unsigned int us;
int port_detected;

void at91_init_uarts(void)
{
	int i;

	port_detected = 0;
	AT91F_DBGU_CfgPIO();
	AT91F_US0_CfgPIO();
	AT91F_US0_CfgPMC();

	for(i=0; i<2; i++) {
		us = usa[i];
		AT91F_US_ResetRx((AT91PS_USART)us);
		AT91F_US_ResetTx((AT91PS_USART)us);

		// Configure DBGU
		AT91F_US_Configure(
			(AT91PS_USART)us, // DBGU base address
			AT91C_MASTER_CLOCK,            // 60 MHz
			AT91C_US_ASYNC_MODE,           // mode Register to be programmed
			115200,                        // baudrate to be programmed
			0                              // timeguard to be programmed
			);

		// Enable Transmitter
		AT91F_US_EnableTx((AT91PS_USART)us);
		// Enable Receiver
		AT91F_US_EnableRx((AT91PS_USART)us);
	}
	us = usa[0];
}

int at91_serial_putc(int ch)
{
	if (ch == '\n')
		at91_serial_putc('\r');
	while (!AT91F_US_TxReady((AT91PS_USART)us));
	AT91F_US_PutChar((AT91PS_USART)us, (char)ch);
	return ch;
}

/* This getc is modified to be able work on more than one port. On certain
 * boards (i.e. Figment Designs VersaLink), the debug port is not available
 * once the unit is in it's enclosure, so, if one needs to get into dfboot
 * for any reason it is impossible. With this getc, it scans between the debug
 * port and another port and once it receives a character, it sets that port
 * as the debug port. */
int at91_serial_getc()
{
	while(1) {
		if (!port_detected) {
			if (us == usa[0]) {
				us = usa[1];
			}
			else {
				us = usa[0];
			}
		}
		if(AT91F_US_RxReady((AT91PS_USART)us)) {
			port_detected = 1;
			return((int)AT91F_US_GetChar((AT91PS_USART)us));
		}
	}
}

/*-----------------------------------------------------------------------------
 * Function Name       : AT91F_ReadLine()
 * Object              : 
 * Input Parameters    : 
 * Return value		: 
 *-----------------------------------------------------------------------------
 */
int AT91F_ReadLine (const char *const prompt, char *console_buffer)
{
	char *p = console_buffer;
	int	n = 0;					/* buffer index		*/
	int	plen = strlen (prompt);	/* prompt length	*/
	int	col;					/* output column cnt	*/
	char	c;

	/* print prompt */
	if (prompt)
		printf(prompt);
	col = plen;

	for (;;) {
		c = getc();

		switch (c) {
			case '\r':				/* Enter		*/
			case '\n':
				*p = '\0';
				puts ("\n");
				return (p - console_buffer);

			case 0x03:				/* ^C - break	*/
				console_buffer[0] = '\0';	/* discard input */
				return (-1);

			case 0x08:				/* ^H  - backspace	*/
			case 0x7F:				/* DEL - backspace	*/
				if (n) {
					--p;
					printf(erase_seq);
					col--;
					n--;
					}
				continue;

			default:
			/*
			 * Must be a normal character then
			 */
			if (n < (AT91C_CB_SIZE -2)) 
			{
				++col;		/* echo input		*/
				putc(c);
				*p++ = c;
				++n;
			} 
			else 
			{			/* Buffer full		*/
				putc('\a');
			}
		}
	}
}


/*-----------------------------------------------------------------------------
 * Function Name       : AT91F_WaitKeyPressed()
 * Object              : 
 * Input Parameters    : 
 * Return value		: 
 *-----------------------------------------------------------------------------
 */
void AT91F_WaitKeyPressed(void)
{
	int c;
    	puts("KEY");
		c = getc();
	putc('\n');
}

int puts(const char *str)
{
  while(*str != 0) {
		at91_serial_putc(*str);
		str++;
		}
	return 1;
}

int putc(int c)
{
  return at91_serial_putc(c);
}

int putchar(c)
{
	return putc(c);
}

int getc()
{
  return at91_serial_getc();
}

int strlen(const char *str)
{
  int len = 0;

  if(str == (char *)0)
    return 0;

  while(*str++ != 0)
    len++;

  return len;
}

#define ZEROPAD 1               /* pad with zero */
#define SIGN    2               /* unsigned/signed long */
#define LEFT    4              /* left justified */
#define LARGE   8              /* use 'ABCDEF' instead of 'abcdef' */

#define do_div(n,base) ({ \
        int __res; \
        __res = ((unsigned) n) % (unsigned) base; \
        n = ((unsigned) n) / (unsigned) base; \
        __res; \
})

static int number(int num, int base, int size,
                  int precision, int type)
{
  char c, sign, tmp[66];
  const char *digits="0123456789ABCDEF";
  int i;

  if (type & LEFT)
    type &= ~ZEROPAD;
  if (base < 2 || base > 16)
    return 0;
  c = (type & ZEROPAD) ? '0' : ' ';
  sign = 0;

  if(type & SIGN && num < 0)
    {
      sign = '-';
      num = -num;
      size--;
    }
  
  i = 0;
  if(num == 0)
    tmp[i++] = digits[0];
  else while(num != 0)
    tmp[i++] = digits[do_div(num, base)];

  if(i > precision)
    precision = i;
  size -= precision;
  
  if(!(type&(ZEROPAD+LEFT)))
    while(size-->0)
      putc(' ');
  
  if(sign)
    putc(sign);

  if (!(type & LEFT))
    while (size-- > 0)
      putc(c);

  while (i < precision--)
    putc('0');
  
  while (i-- > 0)
    putc(tmp[i]);

  while (size-- > 0)
    putc(' ');;

  return 1;
}

int hvfprintf(const char *fmt, va_list va)
{
  char *s;

	do {
		if(*fmt == '%')	{
			bool done = false;

			int type = 0;
			int precision = 0;

			do {
				fmt++;
				switch(*fmt) {
				case '0' :
					if(!precision)
						type |= ZEROPAD;
				case '1' :
				case '2' :
				case '3' :
				case '4' :
				case '5' :
				case '6' :
				case '7' :
				case '8' :
				case '9' :
					precision = precision * 10 + (*fmt - '0');
					break;
				case '.' :
					break;
				case 's' :
					s = va_arg(va, char *);
					if(!s)
						puts("<NULL>");
					else
						puts(s);
					done = true;
					break;
				case 'c' :
					putc(va_arg(va, int));
					done = true;
					break;
				case 'd' :
					number(va_arg(va, int), 10, 0, precision, type);
					done = true;
					break;
				case 'x' :
				case 'X' :
					number(va_arg(va, int), 16, 0, precision, type);
					done = true;
					break;
				case '%' :
					putc(*fmt);
					done = true;
				default: 
					putc('%');
					putc(*fmt);
					done = true;
					break;
				} 
			} while(!done);
		} else if(*fmt == '\\') {
			fmt++;
			if(*fmt == 'r') {
				putc('\r');
			} else if(*fmt == 'n') { 
				putc('\n');
			}
        	} else {
         		putc(*fmt);
        	}
		fmt++;
	} while(*fmt != 0);
  
  return 0;
}

int printf(const char *fmt, ...)
{
  va_list ap;
  int i;

  va_start(ap, fmt);
  i = hvfprintf(fmt, ap);
  va_end(ap);

  return i;
}