struct fs *fat_mount(FILE *device, uint_t mode) { struct fat_header fat_header; fpos_t pos = 0; if (fsetpos(device, &pos) || fread(&fat_header, sizeof(fat_header), 1, device) != 1) { return 0; } if (fat_header.total_sectors_small) { fat_header.total_sectors = fat_header.total_sectors_small; } if (memcmp(fat_header.head.fat12.fs_type, FAT12_FS_TYPE, strlen(FAT12_FS_TYPE)) == 0) { return (struct fs *) fat16_mount(device, mode, &fat_header, 12); } if (memcmp(fat_header.head.fat16.fs_type, FAT16_FS_TYPE, strlen(FAT16_FS_TYPE)) == 0) { return (struct fs *) fat16_mount(device, mode, &fat_header, 16); } if (memcmp(fat_header.head.fat32.fs_type, FAT32_FS_TYPE, strlen(FAT32_FS_TYPE)) == 0) { return (struct fs *) fat32_mount(device, mode, &fat_header); } return 0; }
static void init(void) { long number; struct fat16_dir_t dir; struct fat16_dir_entry_t entry; struct song_t *song_p; uint32_t seconds; fat16_read_t read; fat16_write_t write; void * arg_p; sys_start(); uart_module_init(); uart_init(&uart, &uart_device[0], 38400, qinbuf, sizeof(qinbuf)); uart_start(&uart); sys_set_stdout(&uart.chout); std_printf(sys_get_info()); fs_command_init(&cmd_list, FSTR("/list"), cmd_list_cb, NULL); fs_command_register(&cmd_list); fs_command_init(&cmd_play, FSTR("/play"), cmd_play_cb, NULL); fs_command_register(&cmd_play); fs_command_init(&cmd_pause, FSTR("/pause"), cmd_pause_cb, NULL); fs_command_register(&cmd_pause); fs_command_init(&cmd_next, FSTR("/next"), cmd_next_cb, NULL); fs_command_register(&cmd_next); fs_command_init(&cmd_prev, FSTR("/prev"), cmd_prev_cb, NULL); fs_command_register(&cmd_prev); fs_command_init(&cmd_stop, FSTR("/stop"), cmd_stop_cb, NULL); fs_command_register(&cmd_stop); fs_command_init(&cmd_repeat, FSTR("/repeat"), cmd_repeat_cb, NULL); fs_command_register(&cmd_repeat); fs_command_init(&cmd_set_bits_per_sample, FSTR("/set_bits_per_sample"), cmd_set_bits_per_sample_cb, NULL); fs_command_register(&cmd_set_bits_per_sample); if (storage_init(&read, &write, &arg_p) != 0) { std_printf(FSTR("storage init failed\r\n")); return; } std_printf(FSTR("initializing fat16\r\n")); fat16_init(&fs, read, write, arg_p, 0); std_printf(FSTR("fat16 initialized\r\n")); if (fat16_mount(&fs) != 0) { std_printf(FSTR("failed to mount fat16\r\n")); return; } std_printf(FSTR("fat16 mounted\r\n")); event_init(&event); exti_module_init(); /* Initialize the buttons. */ exti_init(&buttons[0], &exti_d18_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_play, NULL); exti_start(&buttons[0]); exti_init(&buttons[1], &exti_d19_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_prev, NULL); exti_start(&buttons[1]); exti_init(&buttons[2], &exti_d20_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_next, NULL); exti_start(&buttons[2]); exti_init(&buttons[3], &exti_d21_dev, EXTI_TRIGGER_FALLING_EDGE, on_button_stop, NULL); exti_start(&buttons[3]); dac_init(&dac, &dac_0_dev, &pin_dac0_dev, &pin_dac1_dev, 2 * SAMPLES_PER_SOCOND); hash_map_init(&song_map, buckets, membersof(buckets), entries, membersof(entries), hash_number); sem_init(&sem, 0, 1); music_player_init(&music_player, &fs, &dac, get_current_song_path, get_next_song_path, NULL); /* Initialize the song number. */ current_song = FIRST_SONG_NUMBER; number = FIRST_SONG_NUMBER; /* Add songs to the hash map. */ fat16_dir_open(&fs, &dir, ".", O_READ); while (fat16_dir_read(&dir, &entry) == 1) { if (number - FIRST_SONG_NUMBER == membersof(songs)) { std_printf("Maximum number of songs already added. " "Skipping the rest of the songs.\r\n"); break; } /* Skip folders. */ if (entry.is_dir == 1) { continue; } song_p = &songs[number - FIRST_SONG_NUMBER]; /* Initialize the song entry. */ song_p->number = number; strcpy(song_p->name, entry.name); seconds = (entry.size / 4 / SAMPLES_PER_SOCOND); song_p->minutes = (seconds / 60); song_p->seconds = (seconds % 60); std_printf("Adding song %s to playlist.\r\n", entry.name); hash_map_add(&song_map, number, song_p); number++; } fat16_dir_close(&dir); last_song_number = (number - 1); music_player_start(&music_player); }
static int test_mount(struct harness_t *harness_p) { BTASSERT(fat16_mount(&fs) == 0); return (0); }