-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
300 lines (239 loc) · 9.26 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
#include <FreeRTOS.h>
#include "ff_headers.h"
#include "ff_stdio.h"
#include "ff_ramdisk.h"
#include "ff_sys.h"
#include <task.h>
#include <uart/uart.h>
#include "stdio.h"
#include <unistd.h>
#include "disparity_top.h"
#include "mser.h"
#include "pca.h"
#define mainRAM_DISK_SECTOR_SIZE 512UL /* Currently fixed! */
#define mainRAM_DISK_SECTORS ( ( 5UL * 1024UL * 1024UL ) / mainRAM_DISK_SECTOR_SIZE ) /* 5M bytes. */
#define mainIO_MANAGER_CACHE_SIZE ( 15UL * mainRAM_DISK_SECTOR_SIZE )
#define mainRAM_DISK_NAME "/ram"
static uart_instance_t * const gp_my_uart = &g_uart_0;
static int g_stdio_uart_init_done = 0;
void vApplicationMallocFailedHook( void );
void vApplicationIdleHook( void );
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName );
/* Prepare hardware to run the demo. */
static void prvSetupHardware( void );
/* Send a message to the UART initialised in prvSetupHardware. */
void vSendString( const char * const pcString );
static void printTask( void *pvParameters );
uint8_t s_uart[12] = "init_uart\r\n";
extern void freertos_risc_v_trap_handler( void );
uint8_t print_buffer[256];
static void prvSetupHardware( void )
{
UART_init(gp_my_uart,
STDIO_BAUD_RATE,
UART_DATA_8_BITS | UART_NO_PARITY | UART_ONE_STOP_BIT);
g_stdio_uart_init_done = 1;
UART_polled_tx_string(gp_my_uart, s_uart);
//PLIC_init();
//UART_init( &g_uart, COREUARTAPB0_BASE_ADDR, BAUD_VALUE_115200, ( DATA_8_BITS | NO_PARITY ) );
}
void DIRCommand( const char *pcDirectoryToScan )
{
FF_FindData_t *pxFindStruct;
const char *pcAttrib,
*pcWritableFile = "writable file",
*pcReadOnlyFile = "read only file",
*pcDirectory = "directory";
sprintf(print_buffer, "\r\n\n<===========================>\r\n");
UART_polled_tx_string(gp_my_uart, print_buffer);
/* FF_FindData_t can be large, so it is best to allocate the structure
dynamically, rather than declare it as a stack variable. */
pxFindStruct = ( FF_FindData_t * ) pvPortMalloc( sizeof( FF_FindData_t ) );
/* FF_FindData_t must be cleared to 0. */
memset( pxFindStruct, 0x00, sizeof( FF_FindData_t ) );
/* The first parameter to ff_findfist() is the directory being searched. Do
not add wildcards to the end of the directory name. */
if( ff_findfirst( pcDirectoryToScan, pxFindStruct ) == 0 )
{
do
{
/* Point pcAttrib to a string that describes the file. */
if( ( pxFindStruct->ucAttributes & FF_FAT_ATTR_DIR ) != 0 )
{
pcAttrib = pcDirectory;
}
else if( pxFindStruct->ucAttributes & FF_FAT_ATTR_READONLY )
{
pcAttrib = pcReadOnlyFile;
}
else
{
pcAttrib = pcWritableFile;
}
/* Print the files name, size, and attribute string. */
sprintf(print_buffer, "%s [%s] [size=%d] \r\n", pxFindStruct->pcFileName,
pcAttrib,
pxFindStruct->ulFileSize );
UART_polled_tx_string(gp_my_uart, print_buffer);
} while( ff_findnext( pxFindStruct ) == 0 );
}
sprintf(print_buffer, "<============================>\r\n\n");
UART_polled_tx_string(gp_my_uart, print_buffer);
/* Free the allocated FF_FindData_t structure. */
vPortFree( pxFindStruct );
}
extern uint8_t my_fat_image_start;
extern uint8_t my_fat_image_end;
extern const int my_fat_image_size;
int main( void )
{
uint32_t mhartid;
asm volatile (
"csrr %0, 0xF14\n"
: "=r" (mhartid)
);
__asm__ volatile( "csrw mtvec, %0" :: "r"( freertos_risc_v_trap_handler ) );
if (mhartid != 0) {
while (1) {
asm volatile ("interfering_cores:");
uint64_t readvar2;
volatile uint64_t *array2 = (uint64_t*)(uint64_t)(0x85000000 + (mhartid-1) * 0x01000000);
// 32'd1048576/2 = 0x0010_0000/2 elements.
// Each array element is 64-bit, the array size is 0x0040_0000 = 4MB.
// This will always exhaust the 2MB LLC.
// Each core has 0x0100_0000 (16MB) memory.
// It will iterate over 4MB array from top address to down
#define LEN_NONCUA 32768 //256KB
// #define LEN_NONCUA 524288 //4MB
#define INTF_RD
for (int a_idx = 0; a_idx < LEN_NONCUA; a_idx +=8) {
#ifdef INTF_RD
asm volatile (
"ld %0, 0(%1)\n" // read addr_var data into read_var
: "=r"(readvar2)
: "r"(array2 - a_idx)
);
#elif defined(INTF_WR)
asm volatile (
"sd %1, 0(%0)\n" // read addr_var data into read_var
:: "r"(array2 - a_idx), "r"(readvar2)
);
#endif
}
}
}
prvSetupHardware();
// Use sprintf to format a string into the print_buffer
// sprintf((char*)print_buffer, "Hello, ! The answer is %d.\r\n", 42);
// UART_polled_tx_string(gp_my_uart, print_buffer);
// int32_t cache_buf[1024*1024];
// for (int i = 0; i < 1024*1024; i++){
// cache_buf[i] = cache_buf[i] -2;
// }
static uint8_t ucRAMDisk[ mainRAM_DISK_SECTORS * mainRAM_DISK_SECTOR_SIZE ];
FF_Disk_t * pxDisk;
FF_FILE *pxSourceFile, *pxDestinationFile;
/* Create the RAM disk. */
pxDisk = FF_RAMDiskInit2( mainRAM_DISK_NAME, &my_fat_image_start, 102400, mainIO_MANAGER_CACHE_SIZE );
FF_RAMDiskShowPartition(pxDisk);
DIRCommand("/ram/");
DIRCommand("/ram/disp");
benchmark_disp();
DIRCommand("/ram/mser");
benchmark_mser();
DIRCommand("/ram/pca");
benchmark_pca();
uint8_t ucBuffer[ 50 ];
pxDestinationFile = ff_fopen("/ram/hello.txt", "r");
int xCount = ff_fread( ucBuffer, 1, 10, pxDestinationFile );
ff_fclose(pxDestinationFile);
ucBuffer[11] = '\0';
ucBuffer[10] = '\n';
UART_polled_tx_string(gp_my_uart, ucBuffer);
pxSourceFile = ff_fopen("/ram/abc.txt", "w");
ff_fwrite("hello fat filesystem\n", 20, 1, pxSourceFile);
ff_fclose(pxSourceFile);
pxDestinationFile = ff_fopen("/ram/hello.txt", "r");
xCount = ff_fread( ucBuffer, 1, 10, pxDestinationFile );
ff_fclose(pxDestinationFile);
ucBuffer[11] = '\0';
ucBuffer[10] = '\n';
UART_polled_tx_string(gp_my_uart, ucBuffer);
DIRCommand("/ram/");
while(1) {}
BaseType_t xResult;
xResult = xTaskCreate( printTask,
"Check",
configMINIMAL_STACK_SIZE,
NULL,
2,
NULL );
vTaskStartScheduler();
return 0;
}
/*-----------------------------------------------------------*/
static void printTask( void *pvParameters )
{
TickType_t xNextWakeTime;
for (;;){
UART_polled_tx_string(gp_my_uart, s_uart);
vTaskDelay(1000);
}
}
void vApplicationTickHook( void )
{}
void vApplicationMallocFailedHook( void )
{
/* vApplicationMallocFailedHook() will only be called if
configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h. It is a hook
function that will get called if a call to pvPortMalloc() fails.
pvPortMalloc() is called internally by the kernel whenever a task, queue,
timer or semaphore is created. It is also called by various parts of the
demo application. If heap_1.c or heap_2.c are used, then the size of the
heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
to query the size of free heap space that remains (although it does not
provide information on how the remaining heap might be fragmented). */
taskDISABLE_INTERRUPTS();
__asm volatile( "ebreak" );
for( ;; );
}
/*-----------------------------------------------------------*/
void vApplicationIdleHook( void )
{
/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
to 1 in FreeRTOSConfig.h. It will be called on each iteration of the idle
task. It is essential that code added to this hook function never attempts
to block in any way (for example, call xQueueReceive() with a block time
specified, or call vTaskDelay()). If the application makes use of the
vTaskDelete() API function (as this demo application does) then it is also
important that vApplicationIdleHook() is permitted to return to its calling
function, because it is the responsibility of the idle task to clean up
memory allocated by the kernel to any task that has since been deleted. */
}
/*-----------------------------------------------------------*/
void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
( void ) pcTaskName;
( void ) pxTask;
/* Run time stack overflow checking is performed if
configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2. This hook
function is called if a stack overflow is detected. */
taskDISABLE_INTERRUPTS();
__asm volatile( "ebreak" );
for( ;; );
}
/*-----------------------------------------------------------*/
void vAssertCalled( void )
{
uint8_t s_debug[16] = "vassert called\r\n";
portENTER_CRITICAL();
UART_polled_tx_string(gp_my_uart, s_debug);
portEXIT_CRITICAL();
volatile uint32_t ulSetTo1ToExitFunction = 0;
taskDISABLE_INTERRUPTS();
while( ulSetTo1ToExitFunction != 1 )
{
__asm volatile( "NOP" );
}
}