-
Notifications
You must be signed in to change notification settings - Fork 8
/
main.c
347 lines (271 loc) · 8.33 KB
/
main.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
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
/*
* 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: main.c
* Description: Implementation of main.
*/
#include <stdio.h>
/* Scheduler includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "portable.h"
#include "main.h"
#include "led.h"
#include "buffer.h"
#include "uart.h"
#include "io.h"
#include "tests.h"
#include "cli.h"
#include "lpc17xx_libcfg_default.h"
#include "lpc17xx_systick.h"
#include "jtag_1149.h"
/*-----------------------------------------------------------*/
/* The time between cycles of the 'check' functionality (defined within the
tick hook). */
#define mainCHECK_DELAY ( ( portTickType ) 5000 / portTICK_RATE_MS )
/************************** PRIVATE VARIABLES *************************/
/* SysTick Counter */
volatile unsigned long SysTickCnt;
/************************** PRIVATE FUNCTIONS *************************/
void SysTick_Handler (void);
void xPortSysTickHandler(void);
void Delay (unsigned long tick);
/*
* Configure the hardware for the demo.
*/
static void prvSetupHardware( void );
xTaskHandle task_handles[TASKHANDLE_LAST];
unsigned int g_test_done = 0;
void ddk_init(void);
#define MEMORY_BARRIER __asm__ volatile ( \
"" : /* inline assembly code: effectively nothing */ \
: /* list of outputs: empty */ \
: /* list of inputs: empty */ \
"memory" /* clobbered data: memory */ \
)
static inline void ddk_delay(unsigned long microseconds)
{
unsigned long ticks = microseconds * 100;
while(ticks--)
MEMORY_BARRIER;
}
int main(void)
{
portBASE_TYPE ret = 0;
unsigned int v=0;
ddk_init();
// call HW function tests and FPGA ROM programming. This will program the FPGA bitstream
// into flash ROM if FPGA is uninitialized.
hw_test_main();
led1_clr();
led2_clr();
led3_clr();
led4_clr();
// error
while(g_test_done != 1) {
led1_set();
led2_set();
led3_set();
led4_set();
ddk_delay(50000);
led1_clr();
led2_clr();
led3_clr();
led4_clr();
ddk_delay(50000);
}
puts("[*] init done.");
ret = xTaskCreate( vBlinkTask,
_STR "BLINK",
mainBASIC_BLINK_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY,
&task_handles[TASK_BLINK_MAIN]
);
if(ret != pdTRUE)
for(;;);
/*
ret = xTaskCreate( vUART0Task,
_STR "UART0",
mainBASIC_UART0_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY,
&task_handles[TASK_UART0_MAIN]
);
if(ret != pdTRUE)
for(;;);
ret = xTaskCreate( vUART1Task,
_STR "UART1",
mainBASIC_UART1_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY,
&task_handles[TASK_UART1_MAIN]
);
if(ret != pdTRUE)
for(;;);
*/
#ifdef WITH_UART2
ret = xTaskCreate( vUART2Task,
_STR "UART2",
mainBASIC_UART2_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY,
&task_handles[TASK_UART2_MAIN]
);
if(ret != pdTRUE)
for(;;);
#endif
#ifdef WITH_UART3
ret = xTaskCreate( vUART3Task,
_STR "UART3",
mainBASIC_UART3_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY,
&task_handles[TASK_UART3_MAIN]
);
if(ret != pdTRUE)
for(;;);
#endif
ret = xTaskCreate( vCLITask,
_STR"CLI",
mainBASIC_CLI_STACK_SIZE,
( void * ) NULL,
tskIDLE_PRIORITY,
&task_handles[TASK_CLI_MAIN] );
if(ret != pdTRUE)
for(;;);
vTaskStartScheduler();
while (1) {
// power down can go here if supported
v++;
}
return 0;
}
void ddk_init(void)
{
// 1. first, bring buffer enable into well defined state
buffer_init();
buf1_enable();
buf2_enable();
buf3_disable();
buf4_disable();
buf5_disable();
buf6_disable();
buf7_disable();
buf8_disable();
// 2. visuals
led_init();
led1_clr();
led2_clr();
led3_clr();
led4_clr();
// 3. comms
uart0_init(115200, 0);
uart1_init(115200, 0);
#ifdef WITH_UART2
uart2_init(115200, 0); // temporarily disabled and used as i/o
#endif
#ifdef WITH_UART3
uart3_init(115200, 0); // temporarily disabled and used as i/o
#endif
// 4. enable certain buffers if necessary
// 5. init misc i/o
io_init();
// 6. start fpga
io_fpga_enable();
//io_fpga_disable();
// enable systick interrupt. (1msec)
SysTick_Config(SystemCoreClock/1000 - 1); /* Generate interrupt each 1 ms */
SYSTICK_Cmd(DISABLE);
}
void SysTick_Handler (void) {
//vApplicationTickHook();
xPortSysTickHandler();
}
/*-------------------------PRIVATE FUNCTIONS------------------------------*/
/*********************************************************************//**
* @brief Delay function
* @param[in] tick - number milisecond of delay time
* @return None
**********************************************************************/
void Delay (unsigned long tick)
{
unsigned long systickcnt;
systickcnt = SysTickCnt;
while ((SysTickCnt - systickcnt) < tick);
}
/*-----------------------------------------------------------*/
void vApplicationTickHook( void )
{
SysTickCnt++;
#if 0
static unsigned long ulTicksSinceLastDisplay = 0;
/* Called from every tick interrupt as described in the comments at the top
of this file.
Have enough ticks passed to make it time to perform our health status
check again? */
ulTicksSinceLastDisplay++;
if( ulTicksSinceLastDisplay >= mainCHECK_DELAY )
{
/* Reset the counter so these checks run again in mainCHECK_DELAY
ticks time. */
ulTicksSinceLastDisplay = 0;
}
#endif
}
/*-----------------------------------------------------------*/
void vApplicationStackOverflowHook( xTaskHandle *pxTask, signed char *pcTaskName )
{
/* This function will get called if a task overflows its stack. */
( void ) pxTask;
( void ) pcTaskName;
for( ;; );
}
/*-----------------------------------------------------------*/
#if 0
void v_ConfigureTimerForRunTimeStats( void )
{
const unsigned long TCR_COUNT_RESET = 2, CTCR_CTM_TIMER = 0x00, TCR_COUNT_ENABLE = 0x01;
/* This function configures a timer that is used as the time base when
collecting run time statistical information - basically the percentage
of CPU time that each task is utilising. It is called automatically when
the scheduler is started (assuming configGENERATE_RUN_TIME_STATS is set
to 1). */
/* Power up and feed the timer. */
LPC_SC->PCONP |= 0x02UL;
LPC_SC->PCLKSEL0 = (LPC_SC->PCLKSEL0 & (~(0x3<<2))) | (0x01 << 2);
/* Reset Timer 0 */
LPC_TIM0->TCR = TCR_COUNT_RESET;
/* Just count up. */
LPC_TIM0->CTCR = CTCR_CTM_TIMER;
/* Prescale to a frequency that is good enough to get a decent resolution,
but not too fast so as to overflow all the time. */
LPC_TIM0->PR = ( configCPU_CLOCK_HZ / 10000UL ) - 1UL;
/* Start the counter. */
LPC_TIM0->TCR = TCR_COUNT_ENABLE;
}
/*-----------------------------------------------------------*/
#endif