00001
00002
00003
00004
00005
00006
00007
00008
00047 #include "fat16.h"
00048 #include "partition.h"
00049
00050 #include "config.h"
00051
00052 #include "sd_mmc.h"
00053 #include "sd_mmc_config.h"
00054 #include "vs1011.h"
00055 #include "uart.h"
00056
00057 #include <string.h>
00058 #include <stdio.h>
00059 #include <avr/interrupt.h>
00060
00061 #include "delay.h"
00062 #include <util/delay.h>
00063
00064 #include "spi.h"
00065 #include "vs1011.h"
00066
00067 #define DEBUG 1
00068 #define UART_BAUDRATE 19200
00069
00070
00071 extern uint8_t play_song( struct fat16_file_struct* fd );
00072 uint8_t print_disk_info(const struct fat16_fs_struct* fs);
00073 void ddd( void ) ;
00074
00075
00076
00077 #ifdef TEST_READ_SEC_0
00078 uint8_t buffer[512];
00079 #endif
00080
00088 void ddd( void ) {
00089 PORTA |= _BV(PA5);
00090 _delay_loop_2(10);
00091
00092 PORTA &= ~_BV(PA5);
00093 _delay_loop_2(10);
00094 PORTA |= _BV(PA5);
00095 _delay_loop_2(10);
00096
00097 PORTA &= ~_BV(PA5);
00098 _delay_loop_2(10);
00099 PORTA |= _BV(PA5);
00100 _delay_loop_2(10);
00101
00102 PORTA &= ~_BV(PA5);
00103 _delay_loop_2(10);
00104 PORTA |= _BV(PA5);
00105 _delay_loop_2(10);
00106
00107 PORTA &= ~_BV(PA5);
00108 _delay_loop_2(10);
00109 PORTA |= _BV(PA5);
00110 _delay_loop_2(10);
00111
00112 PORTA &= ~_BV(PA5);
00113 _delay_loop_2(10);
00114 PORTA |= _BV(PA5);
00115 _delay_loop_2(10);
00116
00117 PORTA &= ~_BV(PA5);
00118 _delay_loop_2(10);
00119 }
00120
00133 uint8_t find_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name, struct fat16_dir_entry_struct* dir_entry)
00134 {
00135 while(fat16_read_dir(dd, dir_entry))
00136 {
00137 if(strcmp(dir_entry->long_name, name) == 0)
00138 {
00139 fat16_reset_dir(dd);
00140 return 1;
00141 }
00142 }
00143
00144 return 0;
00145 }
00146
00158 struct fat16_file_struct* open_file_in_dir(struct fat16_fs_struct* fs, struct fat16_dir_struct* dd, const char* name)
00159 {
00160 struct fat16_dir_entry_struct file_entry;
00161 if(!find_file_in_dir(fs, dd, name, &file_entry))
00162 return 0;
00163
00164 return fat16_open_file(fs, &file_entry);
00165 }
00166
00167 int main()
00168 {
00169 uint8_t b;
00170
00171 uart_init(UART_BAUD_SELECT(UART_BAUDRATE,F_CPU));
00172 sei();
00173 uart_puts("\n\rmptry $Revision: 111 $\n\r");
00174
00175
00176 DDRA |= (1 << PA5);
00177 PORTA &= ~_BV(PA5);
00178
00179
00180 spi_init();
00181
00182
00183 unselect_card();
00184
00185
00186 vs1011_port_init();
00187 vs1011_xcs_deselect();
00188
00189
00190 loop_until_bit_is_clear(PINA,VS1011_DREQ);
00191 uart_puts("before hw init vs\n\r");
00192
00193 vs1011_xcs_select();
00194
00195 vs1011_init();
00196
00197
00198 uart_puts("after hw init vs\n\r");
00199
00200
00201 vs1011_xcs_deselect();
00202
00203
00204 uart_puts("before sw init vs\n\r");
00205 vs1011_sw_reset();
00206 uart_puts("after sw init vs\n\r");
00207
00208
00209
00210
00211
00212 uart_puts("starting card things\n\r");
00213
00214 if(sd_init()!=0) {
00215 uart_puts("MMC/SD initialization failed\n\r");
00216
00217 }
00218 uart_puts("raw_init ok\n\r");
00219
00220
00221 print_disk_info(0);
00222 #ifdef TEST_READ_SEC_0
00223
00224 if (sd_read( 0, buffer, 512 ) == 0) {
00225 uart_puts("MMC/SD read failed\n\r");
00226
00227 }
00228 display_buffer( buffer, 512 );
00229 #endif
00230
00231
00232 struct partition_struct* partition = partition_open(sd_read,
00233 sd_read_interval,
00234 sd_write,
00235 sd_write_interval,
00236 0
00237 );
00238 if(!partition) {
00239
00240
00241
00242 partition = partition_open(sd_read,
00243 sd_read_interval,
00244 sd_write,
00245 sd_write_interval,
00246 -1
00247 );
00248 if(!partition) {
00249 uart_puts("opening partition failed\n\r");
00250
00251 }
00252 }
00253
00254
00255 struct fat16_fs_struct* fs = fat16_open(partition);
00256 if(!fs) {
00257 uart_puts("opening filesystem failed\n\r");
00258
00259 }
00260
00261
00262 struct fat16_dir_entry_struct directory;
00263 fat16_get_dir_entry_of_path(fs, "/", &directory);
00264
00265 struct fat16_dir_struct* dd = fat16_open_dir(fs, &directory);
00266 if(!dd) {
00267 uart_puts("opening root directory failed\n");
00268
00269 }
00270
00271
00272 print_disk_info(fs);
00273
00274
00275 struct fat16_dir_entry_struct dir_entry;
00276 while(fat16_read_dir(dd, &dir_entry)) {
00277 uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4;
00278
00279 uart_puts(dir_entry.long_name);
00280 uart_putc(dir_entry.attributes & FAT16_ATTRIB_DIR ? '/' : ' ');
00281 while(spaces--)
00282 uart_putc(' ');
00283
00284 uart_puts("\n\r");
00285 }
00286
00287
00288 struct fat16_file_struct* fd = open_file_in_dir(fs, dd, "mamma.mp3");
00289
00290
00291 if(!fd) {
00292 uart_puts("error opening ");
00293 uart_puts("file");
00294 uart_putc('\n');
00295
00296 }
00297 uart_puts("try to play mp3\n\r");
00298
00299 play_song(fd);
00300
00301 fat16_close_file(fd);
00302
00303
00304
00305 fd = open_file_in_dir(fs, dd, "chiquitita.mp3");
00306
00307 if(!fd) {
00308 uart_puts("error opening ");
00309 uart_puts("file");
00310 uart_putc('\n');
00311
00312 }
00313 uart_puts("try to play mp3\n\r");
00314
00315 play_song(fd);
00316
00317 fat16_close_file(fd);
00318
00319
00320
00321
00322 fd = open_file_in_dir(fs, dd, "Hitchhiker Melodie.mp3");
00323 if(!fd) {
00324 uart_puts("error opening ");
00325 uart_puts("file");
00326 uart_putc('\n');
00327
00328 }
00329 uart_puts("try to play mp3\n\r");
00330
00331 play_song(fd);
00332
00333 fat16_close_file(fd);
00334
00335 uart_puts("Finished. entering endless loop...\n\r");
00336 for (;;)
00337 ;
00338
00339 }
00340
00346 uint8_t play_song( struct fat16_file_struct* fd ) {
00347
00348 uint8_t buffer[32];
00349
00350
00351
00352 while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0) {
00353
00354 for(uint16_t i = 0; i < sizeof(buffer); ++i) {
00355
00356 VS_LOOP_UNTIL_DREQ;
00357
00358 vs1011_xdcs_select();
00359
00360 spi_write_byte(buffer[i]);
00361
00362 vs1011_xdcs_deselect();
00363 }
00364 }
00365 return 0;
00366 }
00367
00375 void display_buffer( uint8_t * ptr, uint32_t size) {
00376 uint32_t i;
00377 uint16_t j;
00378 uint8_t *local;
00379 char sbuf[16];
00380
00381 local = ptr;
00382 for (i=0; i < size; i++) {
00383 if (!(i % 16)) {
00384 if (i) {
00385 for (j=0;j<16;j++) {
00386 if (*local < ' ')
00387 uart_putc('.');
00388 else
00389 uart_putc(*local);
00390 local++;
00391 }
00392 }
00393
00394 sprintf( sbuf, "\r\n%4X - ", (unsigned int) i );
00395 uart_puts( sbuf );
00396 }
00397
00398
00399 sprintf( sbuf, "%02x ", *ptr++ );
00400 uart_puts( sbuf );
00401
00402 }
00403
00404 for (j=0;j<16;j++) {
00405 if (*local < ' ')
00406 uart_putc('.');
00407 else
00408 uart_putc(*local);
00409 local++;
00410 }
00411 uart_puts("\r\n");
00412 }
00413
00421 uint8_t print_disk_info(const struct fat16_fs_struct* fs)
00422 {
00423 struct sd_raw_info disk_info;
00424
00425
00426 if(!sd_get_info(&disk_info))
00427 return 0;
00428
00429 uart_puts("manuf: 0x"); uart_putc_hex(disk_info.manufacturer);
00430 uart_puts("\n\roem: "); uart_puts((char*) disk_info.oem);
00431 uart_puts("\n\rprod: "); uart_puts((char*) disk_info.product);
00432 uart_puts("\n\rrev: "); uart_putc_hex(disk_info.revision);
00433 uart_puts("\n\rserial: 0x"); uart_putdw_dec(disk_info.serial);
00434 uart_puts("\n\rdate: "); uart_putw_dec(disk_info.manufacturing_month);
00435 uart_putw_dec(disk_info.manufacturing_year);
00436 uart_puts("\n\rsize: "); uart_putdw_dec(disk_info.capacity);
00437 uart_puts("\n\rcopy: "); uart_putw_dec(disk_info.flag_copy);
00438 uart_puts("\n\rwr.pr.: "); uart_putw_dec(disk_info.flag_write_protect_temp);
00439 uart_putw_dec(disk_info.flag_write_protect);
00440 uart_puts("\n\rformat: "); uart_putw_dec(disk_info.format);
00441
00442 if (fs) {
00443 uart_puts("\n\rfree: "); uart_putdw_dec(fat16_get_fs_free(fs));
00444 uart_puts(" of "); uart_putdw_dec(fat16_get_fs_size(fs));
00445 }
00446 uart_puts("\n\r");
00447 return 1;
00448 }