📄 main.c
字号:
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include "io.h"
#include "system.h"
#include "sd_controller.h"
#include "sys/alt_alarm.h"
#include "lcd_terminal_window.h"
#include "alt_video_display.h"
#include "alt_tpo_lcd.h"
#include "alt_touchscreen.h"
#include "simple_graphics.h"
#include "fonts.h"
#define DATA_BUFF_SIZE 16384
void DumpMem( int memory_base, int memory_size );
int do_lseek_test( char* file_path );
extern sd_card_info_struct* sd_card_global;
// This is our global LCD display
alt_video_display* global_display;
int main ( void )
{
int i;
int ret_code = 0;
alt_u8* data_buffer;
alt_u8 filelist_buffer[500];
alt_u8 infile_path[100];
alt_u8 outfile_path[100];
alt_u8 filename[100];
int volumes_mounted;
int fat_file_handle_1, fat_file_handle_2; // FAT file
volatile int time_at_start, time_at_finish, total_read_time, total_write_time;
int kbytes_per_second_read, kbytes_per_second_write;
int offset, num_files, bytes_read;
int filesize = -1;
// This is our LCD display
alt_video_display* display;
// This is our LCD configuration port
alt_tpo_lcd lcd_serial;
// This is our touch panel
alt_touchscreen touchscreen;
// Initialize and start the LCD display
display = alt_video_display_init( LCD_SGDMA_NAME, // char* sgdma_name
800, // int width
480, // int height
32, // int color_depth
ALT_VIDEO_DISPLAY_USE_HEAP, // int buffer_location
ALT_VIDEO_DISPLAY_USE_HEAP, // int descriptor_location
2 ); // int num_buffers
global_display = display;
// Set the Gamma Curve and Resolution of the LCD
lcd_serial.scen_pio = LCD_I2C_EN_BASE;
lcd_serial.scl_pio = LCD_I2C_SCL_BASE;
lcd_serial.sda_pio = LCD_I2C_SDAT_BASE;
alt_tpo_lcd_init( &lcd_serial, 800, 480 );
// Initialize the touch panel
alt_touchscreen_init ( &touchscreen,
TOUCH_PANEL_SPI_BASE,
TOUCH_PANEL_SPI_IRQ,
TOUCH_PANEL_PEN_IRQ_N_BASE,
60, // 60 samples/sec
ALT_TOUCHSCREEN_SWAP_XY);
// Calibrate the touch panel
alt_touchscreen_calibrate_upper_right (&touchscreen,
3946, 3849, // ADC readings
799, 0 ); // pixel coords
alt_touchscreen_calibrate_lower_left (&touchscreen,
132, 148, // ADC readings
0, 479 ); // pixel coords
PrerenderFont( couriernewbold_24, WHITE_24, BLACK_24 );
printf_lcd_stdout( "****************************************************\n" );
printf_lcd_stdout( " SD Card Controller Test\n" );
printf_lcd_stdout( "****************************************************\n\n" );
// Initialize and mount the filesystem.
volumes_mounted = sd_fat_mount_all();
if( volumes_mounted <= 0 )
{
ret_code = -1;
printf_lcd_stdout( " ERROR: Couldn't mount FAT\n" );
printf_lcd_stdout( " error code: 0x%X\n", sd_card_global->error_code );
printf_lcd_stdout( " num SD Card init tries: %d\n", sd_card_global->init_tries );
}
else
{
{
num_files = sd_list( "/Input_Files", filelist_buffer );
printf_lcd_stdout( "Found %d files in \"/Input_Files\" directory\n", num_files );
printf_lcd_stdout( "Copying files to \"/Output_Files\" directory\n", num_files );
ret_code = sd_mkdir( "/Output_Files", O_RDWR );
offset = 0;
for( i = 0; i < num_files; i++ )
{
strcpy( filename, filelist_buffer + offset );
offset += ( strlen( filename )) + 1;
if( filename[0] != '.' )
{
// input file
sprintf( infile_path, "/dev/sd_card_controller/Input_Files/%s", filename );
fat_file_handle_1 = open( infile_path, O_RDONLY );
if( fat_file_handle_1 >= 0 )
{
// Quick test of fstat()
struct stat filestats;
if( !fstat( fat_file_handle_1, &filestats ))
{
filesize = filestats.st_size;
}
else
{
printf_lcd_stdout( " ERROR: fstat() failed for file %s\n", infile_path );
}
// output file
sprintf( outfile_path, "/dev/sd_card_controller/Output_Files/%s", filename );
fat_file_handle_2 = open( outfile_path, ( O_RDWR | O_CREAT ));
if( fat_file_handle_2 >= 0 )
{
printf_lcd_stdout( " %s (%d bytes)\n", filename, filesize );
// Get our buffer
data_buffer = malloc( filesize );
if( data_buffer == NULL )
{
printf_lcd_stdout( " ERROR: Could not allocate data buffer of %d bytes\n", filesize );
continue;
}
else
{
time_at_start = alt_nticks();
bytes_read = read( fat_file_handle_1, data_buffer, filesize );
time_at_finish = alt_nticks();
total_read_time = time_at_finish - time_at_start;
time_at_start = alt_nticks();
write( fat_file_handle_2, data_buffer, bytes_read );
time_at_finish = alt_nticks();
total_write_time = time_at_finish - time_at_start;
total_read_time = ( total_read_time * 1000 ) / alt_ticks_per_second();
kbytes_per_second_read = ((( filesize / total_read_time ) * 1000 ) / 1024 );
total_write_time = ( total_write_time * 1000 ) / alt_ticks_per_second();
kbytes_per_second_write = ((( filesize / total_write_time ) * 1000 ) / 1024 );
printf_lcd_stdout( " read - %dms (%d KB/s)\n", total_read_time, kbytes_per_second_read );
printf_lcd_stdout( " write - %dms (%d KB/s)\n", total_write_time, kbytes_per_second_write );
free( data_buffer );
}
close( fat_file_handle_2 );
}
else
{
printf_lcd_stdout( " ERROR: Could not open file %s\n", outfile_path );
}
close( fat_file_handle_1 );
}
else
{
printf_lcd_stdout( " ERROR: Could not open file %s\n", infile_path );
}
}
}
}
// Perform lseek test on last file found (should still be in "filename")
do_lseek_test( infile_path );
}
FreeRenderedFont( );
printf_lcd_stdout("\nSD Card Controller Test Finished\n");
return( 0 );
}
int do_lseek_test( char* file_path )
{
alt_u8* data_buffer;
alt_u8 lseek_buffer[100];
int seek_distance1, seek_distance2, seek_distance3, file_pos;
int fat_file_handle_1;
int ret_code = 0;
int bytes_read;
printf_lcd_stdout( "Starting lseek() test.\n" );
printf_lcd_stdout( " %s\n", file_path );
// Get our buffers
data_buffer = malloc( DATA_BUFF_SIZE );
if( data_buffer == NULL )
{
ret_code = -1;
printf_lcd_stdout( " ERROR: Could not allocate data buffer\n" );
}
else
{
fat_file_handle_1 = open( file_path, O_RDONLY );
if( fat_file_handle_1 >= 0 )
{
// Do an lseek test - SEEK_SET, SEEK_CUR
// Go to beginning of file and read a full buffers worth of data.
file_pos = lseek( fat_file_handle_1, 0, SEEK_SET );
bytes_read = read( fat_file_handle_1, data_buffer, DATA_BUFF_SIZE );
// Now move up half the size of the data buffer and read 100 bytes.
printf_lcd_stdout( " - testing absolute lseek()... " );
seek_distance1 = DATA_BUFF_SIZE / 2;
file_pos = lseek( fat_file_handle_1, seek_distance1, SEEK_SET );
if( file_pos == seek_distance1 )
{
bytes_read = read( fat_file_handle_1, lseek_buffer, 100 );
}
// Compare!
if( memcmp(( data_buffer + ( seek_distance1 )), lseek_buffer, 100 ))
{
printf_lcd_stdout( "FAILED.\n" );
}
else
{
printf_lcd_stdout( "passed.\n" );
}
// Now do a relative lseek()
printf_lcd_stdout( " - testing relative forward lseek()... " );
seek_distance2 = ( 1235 );
file_pos = lseek( fat_file_handle_1, seek_distance2, SEEK_CUR );
if( file_pos == ((seek_distance1 + seek_distance2 ) + 100 ))
{
bytes_read = read( fat_file_handle_1, lseek_buffer, 100 );
}
// Compare!
if( memcmp(( data_buffer + ( seek_distance1 + seek_distance2 + 100 )), lseek_buffer, 100 ))
{
printf_lcd_stdout( "FAILED.\n" );
}
else
{
printf_lcd_stdout( "passed.\n" );
}
// Now do a backwards relative lseek()
printf_lcd_stdout( " - testing relative backward lseek()... " );
seek_distance3 = ( -1723 );
file_pos = lseek( fat_file_handle_1, seek_distance3, SEEK_CUR );
if( file_pos == (((seek_distance1 + seek_distance2 ) + seek_distance3 ) + 200 ))
{
bytes_read = read( fat_file_handle_1, lseek_buffer, 100 );
}
// Compare!
if( memcmp(( data_buffer + (( seek_distance1 + seek_distance2 ) + seek_distance3 + 200 )), lseek_buffer, 100 ))
{
printf_lcd_stdout( "FAILED.\n" );
}
else
{
printf_lcd_stdout( "passed.\n" );
}
// End lseek() test...
close ( fat_file_handle_1 );
}
else
{
printf_lcd_stdout( " ERROR: Could not open file %s\n", file_path );
}
free( data_buffer );
}
return( ret_code );
}
void insert_char_in_buff( alt_u8 data, alt_u8* buffer, int index )
{
char non_valid_flag = 0;
char non_valid_chars[4] = { 0x0, 0x4, '\n', '\r' };
int i;
for( i = 0; i < 4; i++ )
{
if( data == non_valid_chars[i] )
{
non_valid_flag = 1;
break;
}
}
if( non_valid_flag == 0 )
{
buffer[index] = data;
}
else
{
buffer[index] = '.';
}
}
void DumpMem( int memory_base, int memory_size )
{
int i;
alt_u8* data;
char ascii_buffer[17];
int ascii_buffer_index = 0;
ascii_buffer[16] = 0x0;
data = (alt_u8*)memory_base;
for(i = 0; i < memory_size; i++)
{
if (i % 0x10 == 0)
{
printf_lcd_stdout("\n 0x%8.8X: ", i );
}
insert_char_in_buff( *(data+i), ascii_buffer, ascii_buffer_index );
if ((i & 0xF) == 0xF)
{
printf_lcd_stdout("%2.2X %s", *(data + i), ascii_buffer );
ascii_buffer_index = 0;
}
else
{
printf_lcd_stdout("%2.2X ", *(data + i));
ascii_buffer_index++;
}
}
printf_lcd_stdout("\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -