1 |
61 |
csantifort |
/*----------------------------------------------------------------
|
2 |
|
|
// //
|
3 |
|
|
// boot-loader-ethmac.c //
|
4 |
|
|
// //
|
5 |
|
|
// This file is part of the Amber project //
|
6 |
|
|
// http://www.opencores.org/project,amber //
|
7 |
|
|
// //
|
8 |
|
|
// Description //
|
9 |
|
|
// The main functions for the boot loader application. This //
|
10 |
|
|
// application is embedded in the FPGA's SRAM and is used //
|
11 |
|
|
// to load larger applications into the DDR3 memory on //
|
12 |
|
|
// the development board. //
|
13 |
|
|
// //
|
14 |
|
|
// Author(s): //
|
15 |
|
|
// - Conor Santifort, csantifort.amber@gmail.com //
|
16 |
|
|
// //
|
17 |
|
|
//////////////////////////////////////////////////////////////////
|
18 |
|
|
// //
|
19 |
|
|
// Copyright (C) 2011 Authors and OPENCORES.ORG //
|
20 |
|
|
// //
|
21 |
|
|
// This source file may be used and distributed without //
|
22 |
|
|
// restriction provided that this copyright statement is not //
|
23 |
|
|
// removed from the file and that any derivative work contains //
|
24 |
|
|
// the original copyright notice and the associated disclaimer. //
|
25 |
|
|
// //
|
26 |
|
|
// This source file is free software; you can redistribute it //
|
27 |
|
|
// and/or modify it under the terms of the GNU Lesser General //
|
28 |
|
|
// Public License as published by the Free Software Foundation; //
|
29 |
|
|
// either version 2.1 of the License, or (at your option) any //
|
30 |
|
|
// later version. //
|
31 |
|
|
// //
|
32 |
|
|
// This source is distributed in the hope that it will be //
|
33 |
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied //
|
34 |
|
|
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
|
35 |
|
|
// PURPOSE. See the GNU Lesser General Public License for more //
|
36 |
|
|
// details. //
|
37 |
|
|
// //
|
38 |
|
|
// You should have received a copy of the GNU Lesser General //
|
39 |
|
|
// Public License along with this source; if not, download it //
|
40 |
|
|
// from http://www.opencores.org/lgpl.shtml //
|
41 |
|
|
// //
|
42 |
|
|
----------------------------------------------------------------*/
|
43 |
|
|
|
44 |
|
|
#include "utilities.h"
|
45 |
|
|
#include "line-buffer.h"
|
46 |
|
|
#include "timer.h"
|
47 |
|
|
#include "packet.h"
|
48 |
|
|
|
49 |
|
|
line_buf_t* init_line_buffer (int size)
|
50 |
|
|
{
|
51 |
|
|
line_buf_t* lbuf;
|
52 |
|
|
int i;
|
53 |
|
|
|
54 |
|
|
lbuf = malloc(sizeof(line_buf_t));
|
55 |
|
|
lbuf->read_ptr = 0;
|
56 |
|
|
lbuf->write_ptr = 0;
|
57 |
|
|
lbuf->len_bytes = size;
|
58 |
|
|
lbuf->buf = malloc(size);
|
59 |
|
|
return lbuf;
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/* Add a single byte to the line buffer */
|
64 |
|
|
void put_byte (line_buf_t* telnet_buf, char byte, int last)
|
65 |
|
|
{
|
66 |
|
|
|
67 |
|
|
/* Need to pass a pointer to a pointer to the location to
|
68 |
|
|
write the character, to that the pointer to the location
|
69 |
|
|
can be incremented by the final output function
|
70 |
|
|
*/
|
71 |
|
|
telnet_buf->buf[telnet_buf->write_ptr] = byte;
|
72 |
|
|
if (last)
|
73 |
|
|
telnet_buf->buf[telnet_buf->write_ptr+1] = 0;
|
74 |
|
|
else
|
75 |
|
|
telnet_buf->buf[telnet_buf->write_ptr+1] = 1;
|
76 |
|
|
telnet_buf->write_ptr++;
|
77 |
|
|
|
78 |
|
|
/* Wrap the write pointer */
|
79 |
|
|
if ((telnet_buf->write_ptr + MAX_LINE) >= telnet_buf->len_bytes)
|
80 |
|
|
telnet_buf->write_ptr = 0;
|
81 |
|
|
}
|
82 |
|
|
|
83 |
|
|
|
84 |
|
|
/* Add a line to the line buffer */
|
85 |
|
|
void put_line (line_buf_t* telnet_buf, const char *fmt, ...)
|
86 |
|
|
{
|
87 |
|
|
int len;
|
88 |
|
|
char *buf;
|
89 |
|
|
int i;
|
90 |
|
|
|
91 |
|
|
register unsigned long *varg = (unsigned long *)(&fmt);
|
92 |
|
|
*varg++;
|
93 |
|
|
|
94 |
|
|
/* Need to pass a pointer to a pointer to the location to
|
95 |
|
|
write the character, to that the pointer to the location
|
96 |
|
|
can be incremented by the final output function
|
97 |
|
|
*/
|
98 |
|
|
buf = &telnet_buf->buf[telnet_buf->write_ptr];
|
99 |
|
|
len = print(&buf, fmt, varg);
|
100 |
|
|
buf[len] = 0;
|
101 |
|
|
telnet_buf->write_ptr += len;
|
102 |
|
|
|
103 |
|
|
/* Wrap the write pointer */
|
104 |
|
|
if ((telnet_buf->write_ptr + MAX_LINE) >= telnet_buf->len_bytes)
|
105 |
|
|
telnet_buf->write_ptr = 0;
|
106 |
|
|
telnet_buf->buf[telnet_buf->write_ptr] = 0;
|
107 |
|
|
}
|
108 |
|
|
|
109 |
|
|
|
110 |
|
|
|
111 |
|
|
/* Retrieve the next line from a line buffer.
|
112 |
|
|
return 0 if buffer doesn't have a complete line */
|
113 |
|
|
int get_line (line_buf_t* telnet_buf, char** line)
|
114 |
|
|
{
|
115 |
|
|
int len = 0;
|
116 |
|
|
|
117 |
|
|
// Grab these once at the start in case they are changed by an interrupt
|
118 |
|
|
register int write_ptr = telnet_buf->write_ptr;
|
119 |
|
|
|
120 |
|
|
/* Wrap the read pointer */
|
121 |
|
|
if ((telnet_buf->read_ptr + MAX_LINE) >= telnet_buf->len_bytes)
|
122 |
|
|
telnet_buf->read_ptr = 0;
|
123 |
|
|
|
124 |
|
|
*line = telnet_buf->buf + telnet_buf->read_ptr;
|
125 |
|
|
|
126 |
|
|
/* Find the length of the next line in the buffer */
|
127 |
|
|
if (telnet_buf->read_ptr != write_ptr) {
|
128 |
|
|
/* Find next 0 */
|
129 |
|
|
while (telnet_buf->read_ptr+len != write_ptr &&
|
130 |
|
|
telnet_buf->buf[telnet_buf->read_ptr+len]!=0 &&
|
131 |
|
|
telnet_buf->buf[telnet_buf->read_ptr+len]!=1 &&
|
132 |
|
|
len < MAX_LINE)
|
133 |
|
|
len++;
|
134 |
|
|
|
135 |
|
|
/* Check if there are some chars from put_char but not a complete line */
|
136 |
|
|
if (telnet_buf->buf[telnet_buf->read_ptr+len]==1)
|
137 |
|
|
return 0;
|
138 |
|
|
else {
|
139 |
|
|
telnet_buf->read_ptr += len;
|
140 |
|
|
return len;
|
141 |
|
|
}
|
142 |
|
|
}
|
143 |
|
|
else
|
144 |
|
|
return 0;
|
145 |
|
|
}
|
146 |
|
|
|