-
Notifications
You must be signed in to change notification settings - Fork 8
/
helper.c
247 lines (192 loc) · 5.86 KB
/
helper.c
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
/*
* Copyright (c) 2013, The DDK Project
* Dmitry Nedospasov <dmitry at nedos dot net>
* Thorsten Schroeder <ths at modzero dot ch>
*
* All rights reserved.
*
* This file is part of Die Datenkrake (DDK).
*
* "THE BEER-WARE LICENSE" (Revision 42):
* <dmitry at nedos dot net> and <ths at modzero dot ch> wrote this file. As
* long as you retain this notice you can do whatever you want with this stuff.
* If we meet some day, and you think this stuff is worth it, you can buy us a
* beer in return. Die Datenkrake Project.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE DDK PROJECT 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: helper.c
* Description: Implementation of helper functions.
*/
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#ifndef build_date
const char *build_date = __DATE__;
#endif
#ifndef build_time
const char *build_time = __TIME__;
#endif
#include "helper.h"
#include "uart.h"
#include "main.h"
#ifndef putc
#define putc(a,b) putchar((b))
#endif
#ifndef stdout
#define stdout 0
#endif
// bin to hexascii nibble
static const unsigned char _tab[] = "0123456789ABCDEF";
void print_hex_byte(char b)
{
putchar( _tab[ (b >> 4) & 0xF ] );
putchar( _tab[ (b >> 0) & 0xF ] );
}
void print_hex_word(int16_t w)
{
putchar( _tab[ (w >> 12) & 0xF ] );
putchar( _tab[ (w >> 8) & 0xF ] );
putchar( _tab[ (w >> 4) & 0xF ] );
putchar( _tab[ (w >> 0) & 0xF ] );
}
void print_hex_dword(int32_t d)
{
putchar( _tab[ (d >> 28) & 0xF ] );
putchar( _tab[ (d >> 24) & 0xF ] );
putchar( _tab[ (d >> 20) & 0xF ] );
putchar( _tab[ (d >> 16) & 0xF ] );
putchar( _tab[ (d >> 12) & 0xF ] );
putchar( _tab[ (d >> 8) & 0xF ] );
putchar( _tab[ (d >> 4) & 0xF ] );
putchar( _tab[ (d >> 0) & 0xF ] );
}
void print_hex_buf(char *s, uint32_t l)
{
while(l--) {
putchar( _tab[ (*s >> 4) & 0xF ] );
putchar( _tab[ (*s >> 0) & 0xF ] );
s++;
}
}
/*
* print data in rows of 16 bytes: offset hex ascii
*
* 00000 47 45 54 20 2f 20 48 54 54 50 2f 31 2e 31 0d 0a GET / HTTP/1.1..
*/
void print_hex_ascii_line(const unsigned char *payload, int slen, int offset)
{
int i;
int gap, len;
const unsigned char *ch, *ch2;
len = 16;
ch2 = ch = payload;
do {
/* offset */
//printf("%05x ", offset);
print_hex_word(offset & 0xffff);
putchar(' ');
/* hex */
if(slen < len)
len=slen;
for(i = 0; i < len; i++) {
//printf("%02x ", *ch);
print_hex_byte(*ch);
putchar(' ');
ch++;
/* print extra space after 8th byte for visual aid */
if (i == 7)
putchar(' ');
}
/* print space to handle line less than 8 bytes */
if (len < 8)
putchar(' ');
/* fill hex gap with spaces if not full line */
if (len < 16) {
gap = 16 - len;
for (i = 0; i < gap; i++) {
printf(" ");
}
}
printf(" ");
/* ascii (if printable) */
for(i = 0; i < len; i++) {
if (isprint(*ch2)){
printf("%c", *ch2);
} else
putchar(' ');
ch2++;
}
puts("");
slen -= 16;
offset += len;
} while( slen > 0 ) ;
return;
}
//-----------------------------------------------------------------------------
// delay
//-----------------------------------------------------------------------------
void _Delay (volatile unsigned long a) { while (--a!=0); }
//-----------------------------------------------------------------------------
// the most important function is implemented right here:
//-----------------------------------------------------------------------------
static void banner(void)
{
int cols = 70;
int rows = 25;
int i = 0;
putchar('\n');
for (i=0;i<rows;i++)
putchar('\n');
for (i=0;i<cols;i++)
putchar('*');
putchar('\n');
printf("* Booting %s %s\n",
PROJECT_NAME, PROJECT_REVISION);
printf("* Built: %s %s\n",
build_date, build_time);
printf("* datenkrake (at) dev . io -- ://datenkrake.org/\n");
printf("* Oh hai!\n");
for (i=0;i<cols;i++)
putchar('*');
putchar('\n');
#ifdef _ROWLEY_CTL
printf("* Information:\n");
printf("* Internal Flash Memory Size (Byte): %d\n", liblpc1000_get_internal_flash_size() );
printf("* Internal Local SRAM Memory Size (Byte): %d\n", liblpc1000_get_local_sram_size() );
printf("* AHB SRAM Memory Size (Byte): %d\n", liblpc1000_get_ahb_sram_size() );
printf("* Oscillator Clock Frequency (Hz): %d\n", (OSCILLATOR_CLOCK_FREQUENCY) );
printf("* Current CPU Clock Frequency (Hz): %d\n", liblpc1000_lpc17xx_get_cclk(OSCILLATOR_CLOCK_FREQUENCY) );
printf("* Current Timer Ticks Per Second: %d\n", ctl_get_ticks_per_second());
#endif
for (i=0;i<cols;i++)
putchar('*');
putchar('\n');
}
char *strtrim (char *s)
{
char *t = s + strlen (s) - 1;
while (t >= s && *t && isspace (*t))
*t-- = '\0';
while (*s && isspace (*s))
s++;
return s;
}
char * index(char *str, char c)
{
while(*str != c)
if(*str == 0)
return str;
else
str++;
return str;
}