SdErrorCode initCard() { if (!sd_raw_init()) { if (!sd_raw_available()) { reset(); return SD_ERR_NO_CARD_PRESENT; } else { reset(); return SD_ERR_INIT_FAILED; } } else if (!openPartition()) { reset(); return SD_ERR_PARTITION_READ; } else if (!openFilesys()) { reset(); return SD_ERR_OPEN_FILESYSTEM; } else if (!openRoot()) { reset(); return SD_ERR_NO_ROOT; /* we need to keep locked as the last check */ } else if (sd_raw_locked()) { return SD_ERR_CARD_LOCKED; } return SD_SUCCESS; }
int main (void) { boot_up(); //Initialize USB port pins and set up the UART rprintf("Boot up complete\n"); if(IOPIN0 & (1<<23)) //Check to see if the USB cable is plugged in { main_msc(); //If so, run the USB device driver. } else{ rprintf("No USB Detected\n"); } //Init SD if(sd_raw_init()) //Initialize the SD card { openroot(); //Open the root directory on the SD card rprintf("Root open\n"); if(root_file_exists(FW_FILE)) //Check to see if the firmware file is residing in the root directory { rprintf("New firmware found\n"); load_fw(FW_FILE); //If we found the firmware file, then program it's contents into memory. rprintf("New firmware loaded\n"); } } else{ //Didn't find a card to initialize rprintf("No SD Card Detected\n"); delay_ms(250); } rprintf("Boot Done. Calling firmware...\n"); call_firmware(); //Run the new code! while(1); }
uint8_t SDCardClass::init() { if (isInit) return 0; if (fs != NULL) { fat_close(fs); fs = NULL; } if (partition != NULL) { partition_close(partition); partition = NULL; } if (!sd_raw_init()) { return 1; } partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 0); if (!partition) { return 2; } fs = fat_open(partition); if (!fs) { return 3; } isInit = true; return 0; }
char init_filesystem(void) { //setup sd card slot if(!sd_raw_init()) { return 0; } //open first partition partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 0 ); if(!partition) { //If the partition did not open, assume the storage device //is a "superfloppy", i.e. has no MBR. partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!partition) { return 0; } } //Open file system fs = fat_open(partition); if(!fs) { return 0; } //Open root directory fat_get_dir_entry_of_path(fs, "/", &dir_entry); dd=fat_open_dir(fs, &dir_entry); if(!dd) { return 0; } return 1; }
/****************************************************************************** * User API ******************************************************************************/ char FAT::initialize() { //setup sd card slot if(!sd_raw_init()) { return 0; } PORTB |= (1<<0); //open first partition _partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 0 ); if(!_partition) { //If the partition did not open, assume the storage device //is a "superfloppy", i.e. has no MBR. _partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, -1 ); if(!_partition) { return 0; } } //Open file system //struct fat_fs_struct* fs = fat_open(partition); _fs = fat_open(_partition); if(!_fs) { return 0; } //Open root directory //struct fat_dir_entry_struct directory; fat_get_dir_entry_of_path(_fs, "/", &_directory); //struct fat_dir_struct* dd = fat_open_dir(fs, &directory); _dd=fat_open_dir(_fs, &_directory); if(!_dd) { return 0; } return 1; }
int8_t sd_open() { sd_close(); /* setup sd card slot */ if(!sd_raw_init()) return SD_ERROR_INIT; /* open first partition */ sd_partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, 0 ); if(!sd_partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ sd_partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, sd_raw_write_interval, -1 ); if(!sd_partition) return SD_ERROR_PARTITION; } /* open file system */ sd_fs = fat16_open(sd_partition); if(!sd_fs) { sd_close(); return SD_ERROR_FS; } /* open root directory */ struct fat16_dir_entry_struct directory; fat16_get_dir_entry_of_path(sd_fs, "/", &directory); sd_dd = fat16_open_dir(sd_fs, &directory); if(!sd_dd) { sd_close(); return SD_ERROR_ROOTDIR; } return SD_ERROR_NONE; }
bool FatFsClass::initialize(void) { /* setup sd card slot */ if(!sd_raw_init()){ return false; } /* open first partition */ _partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 0 ); if(!_partition){ /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ _partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!_partition){ return false; } } /* open file system */ _fs = fat_open(_partition); if(!_fs){ return false; } /* open root directory */ return changeDirectory("/"); }
int main() { /* we will just use ordinary idle mode */ set_sleep_mode(SLEEP_MODE_IDLE); /* setup uart */ uart_init(); while(1) { /* setup sd card slot */ if(!sd_raw_init()) { #if DEBUG uart_puts_p(PSTR("MMC/SD initialization failed\n")); #endif continue; } /* open first partition */ struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif 0 ); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, #if SD_RAW_WRITE_SUPPORT sd_raw_write, sd_raw_write_interval, #else 0, 0, #endif -1 ); if(!partition) { #if DEBUG uart_puts_p(PSTR("opening partition failed\n")); #endif continue; } } /* open file system */ struct fat_fs_struct* fs = fat_open(partition); if(!fs) { #if DEBUG uart_puts_p(PSTR("opening filesystem failed\n")); #endif continue; } /* open root directory */ struct fat_dir_entry_struct directory; fat_get_dir_entry_of_path(fs, "/", &directory); struct fat_dir_struct* dd = fat_open_dir(fs, &directory); if(!dd) { #if DEBUG uart_puts_p(PSTR("opening root directory failed\n")); #endif continue; } /* print some card information as a boot message */ print_disk_info(fs); /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strcmp_P(command, PSTR("init")) == 0) { break; } else if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &subdir_entry)) { struct fat_dir_struct* dd_new = fat_open_dir(fs, &subdir_entry); if(dd_new) { fat_close_dir(dd); dd = dd_new; continue; } } uart_puts_p(PSTR("directory not found: ")); uart_puts(command); uart_putc('\n'); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* print directory listing */ struct fat_dir_entry_struct dir_entry; while(fat_read_dir(dd, &dir_entry)) { uint8_t spaces = sizeof(dir_entry.long_name) - strlen(dir_entry.long_name) + 4; uart_puts(dir_entry.long_name); uart_putc(dir_entry.attributes & FAT_ATTRIB_DIR ? '/' : ' '); while(spaces--) uart_putc(' '); uart_putdw_dec(dir_entry.file_size); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat_read_file(fd, buffer, sizeof(buffer)) > 0) { uart_putdw_hex(offset); uart_putc(':'); for(uint8_t i = 0; i < 8; ++i) { uart_putc(' '); uart_putc_hex(buffer[i]); } uart_putc('\n'); offset += 8; } fat_close_file(fd); } else if(strcmp_P(command, PSTR("disk")) == 0) { if(!print_disk_info(fs)) uart_puts_p(PSTR("error reading disk info\n")); } #if FAT_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat_delete_file(fs, &file_entry)) continue; } uart_puts_p(PSTR("error deleting file: ")); uart_puts(command); uart_putc('\n'); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct file_entry; if(!fat_create_file(dd, command, &file_entry)) { uart_puts_p(PSTR("error creating file: ")); uart_puts(command); uart_putc('\n'); } } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { uart_puts_p(PSTR("error opening ")); uart_puts(command); uart_putc('\n'); continue; } int32_t offset = strtolong(offset_value); if(!fat_seek_file(fd, &offset, FAT_SEEK_SET)) { uart_puts_p(PSTR("error seeking on ")); uart_puts(command); uart_putc('\n'); fat_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { uart_puts_p(PSTR("error writing to file\n")); break; } } fat_close_file(fd); } else if(strncmp_P(command, PSTR("mkdir "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat_dir_entry_struct dir_entry; if(!fat_create_dir(dd, command, &dir_entry)) { uart_puts_p(PSTR("error creating directory: ")); uart_puts(command); uart_putc('\n'); } } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) uart_puts_p(PSTR("error syncing disk\n")); } #endif else if(strcmp_P(command, PSTR("sync")) == 0) { // vytvor adresar mereni_teploty // nekonecna smycka - pomoci RTCka kazdou minutu odmer teplotu } else { uart_puts_p(PSTR("unknown command: ")); uart_puts(command); uart_putc('\n'); } } /* close directory */ fat_close_dir(dd); /* close file system */ fat_close(fs); /* close partition */ partition_close(partition); } return 0; }
// intialization functions void bootARM(void) { // had to steal some code from the original SparkFun code because I don't // understand it yet. rprintf_devopen(putc_serial0); // Init rprintf delay_ms(30); // wait for power to stabilize // bring up SD / FAT * if (! (sd_raw_init()) ) { rprintf("SD init error!\n"); } if ( openroot() ) { rprintf("openroot error!\n"); } // set up I/O pins * //Setup the MP3 I/O Lines IODIR0 |= MP3_XCS; IODIR0 &= ~MP3_DREQ; PINSEL1 |= 0x00000C00; // Set the MP3_DREQ Pin to be a capture pin IODIR1 |= MP3_XDCS | MP3_GPIO0 | MP3_XRES; //Setupt the FM Trans. Lines IODIR1 |= FM_LA; // FM trans outputs (Leave SPI pins IODIR1 |= FM_CS; // unconfigured for now) //Setup the SD Card I/O Lines IODIR0 |= SD_CS; // SD card outputs //Setup the Accelerometer I/O Lines IODIR0 |= (GS1 | GS2); // accelerometer outputs // ensure ADC pins have ADC Functions selected PINSEL0 |= (MMA_X_PINSEL | MMA_Y_PINSEL | MMA_Z_PINSEL); IOCLR0 = (GS1 | GS2); // initialize acceleration to 1.5 G mode //Setup the LCD I/O Lines IODIR0 |= (LCD_RES | LCD_CS); // LCD Outputs //Setup the LED Lines IODIR0 |= (LED_BLU | LED_RED | LED_GRN); ledBlueOff(); ledRedOff(); ledGrnOff(); //Setup the Buttons IODIR1 &= (~SW_UP & ~SW_DWN & ~SW_MID); //Button Inputs IODIR0 &= ~(1<<23); //Set the Vbus line as an input //Setupt the Interrupts VPBDIV = 1; // Set PCLK equal to the system clock VICIntSelect = ~0x30; // Timer 0 AND TIMER 1 interrupt is an IRQ interrupt VICIntEnable = 0x10; // Enable Timer 0 interrupts // (don't start sending song data with Timer 1) VICVectCntl0= 0x25; // Use slot 0 for timer 1 interrupt VICVectAddr0 = (unsigned int)timer1ISR; // Set the address of ISR for slot 1 VICVectCntl1 = 0x24; // Use slot 1 for timer 0 interrupt VICVectAddr1 = (unsigned int)timer0ISR; // Set the address of ISR for slot 1 //Configure Timer0 T0PR = 1500; // divide clock(60MHz) by 1500 for 40kHz PS T0TCR |=0X01; // enable the clock T0CTCR=0; // timer node T0MCR=0x0003; // interrupt and reset timer on match T0MR0=1000; // interrupt on 40Hz //Configure Timer1 T1PR = 200; // divide clock by 300 for 40kHz PS T1TCR |=0X01; // enable the clock T1CTCR=0; // timer mode T1CCR=0x0A00; // capture and interrupt on the // rising edge of DREQ //Setup the SPI Port S0SPCCR = 64; // SCK = 1 MHz, counter > 8 and even S0SPCR = 0x20; // Master, no interrupt enable, 8 bits // set up MP3 decoder vs1002Config(); // set up ARM <-> VS1002 communication vs1002Reset(); // hardware reset vs1002Init(); // initialization return; }
uint8_t AF_Wave::init_card(void) { return sd_raw_init(); }
int main() { /* setup uart */ uart_init(); /* setup stdio */ uart_connect_stdio(); /* setup sd card slot */ if(!sd_raw_init()) { #if DEBUG printf_P(PSTR("MMC/SD initialization failed\n")); #endif return 1; } /* open first partition */ struct partition_struct* partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, 0); if(!partition) { /* If the partition did not open, assume the storage device * is a "superfloppy", i.e. has no MBR. */ partition = partition_open(sd_raw_read, sd_raw_read_interval, sd_raw_write, -1 ); if(!partition) { #if DEBUG printf_P(PSTR("opening partition failed\n")); #endif return 1; } } /* open file system */ struct fat16_fs_struct* fs = fat16_open(partition); if(!fs) { #if DEBUG printf_P(PSTR("opening filesystem failed\n")); #endif return 1; } /* open root directory */ struct fat16_dir_entry_struct directory; fat16_get_dir_entry_of_path(fs, "/", &directory); struct fat16_dir_struct* dd = fat16_open_dir(fs, &directory); if(!dd) { #if DEBUG printf_P(PSTR("opening root directory failed\n")); #endif return 1; } /* provide a simple shell */ char buffer[24]; while(1) { /* print prompt */ uart_putc('>'); uart_putc(' '); /* read command */ char* command = buffer; if(read_line(command, sizeof(buffer)) < 1) continue; /* execute command */ if(strncmp_P(command, PSTR("cd "), 3) == 0) { command += 3; if(command[0] == '\0') continue; /* change directory */ struct fat16_dir_entry_struct subdir_entry; if(find_file_in_dir(fs, dd, command, &subdir_entry)) { struct fat16_dir_struct* dd_new = fat16_open_dir(fs, &subdir_entry); if(dd_new) { fat16_close_dir(dd); dd = dd_new; continue; } } printf_P(PSTR("directory not found: %s\n"), command); } else if(strcmp_P(command, PSTR("ls")) == 0) { /* print directory listing */ struct fat16_dir_entry_struct dir_entry; while(fat16_read_dir(dd, &dir_entry)) { printf_P(PSTR("%10lu %s%c\n"), dir_entry.file_size, dir_entry.long_name, (dir_entry.attributes & FAT16_ATTRIB_DIR) ? '/' : ' ' ); } } else if(strncmp_P(command, PSTR("cat "), 4) == 0) { command += 4; if(command[0] == '\0') continue; /* search file in current directory and open it */ struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { printf_P(PSTR("error opening %s\n"), command); continue; } /* print file contents */ uint8_t buffer[8]; uint32_t offset = 0; while(fat16_read_file(fd, buffer, sizeof(buffer)) > 0) { printf_P(PSTR("%08lx: %02x %02x %02x %02x %02x %02x %02x %02x\n"), offset, buffer[0], buffer[1], buffer[2], buffer[3], buffer[4], buffer[5], buffer[6], buffer[7] ); offset += 8; } fat16_close_file(fd); } #if FAT16_WRITE_SUPPORT else if(strncmp_P(command, PSTR("rm "), 3) == 0) { command += 3; if(command[0] == '\0') continue; struct fat16_dir_entry_struct file_entry; if(find_file_in_dir(fs, dd, command, &file_entry)) { if(fat16_delete_file(fs, &file_entry)) continue; } printf_P(PSTR("error deleting file: %s\n"), command); } else if(strncmp_P(command, PSTR("touch "), 6) == 0) { command += 6; if(command[0] == '\0') continue; struct fat16_dir_entry_struct file_entry; if(!fat16_create_file(dd, command, &file_entry)) printf_P(PSTR("error creating file: %s\n"), command); } else if(strncmp_P(command, PSTR("write "), 6) == 0) { command += 6; if(command[0] == '\0') continue; char* offset_value = command; while(*offset_value != ' ' && *offset_value != '\0') ++offset_value; if(*offset_value == ' ') *offset_value++ = '\0'; else continue; /* search file in current directory and open it */ struct fat16_file_struct* fd = open_file_in_dir(fs, dd, command); if(!fd) { printf_P(PSTR("error opening %s\n"), command); continue; } int32_t offset = strtol(offset_value, 0, 0); if(!fat16_seek_file(fd, &offset, FAT16_SEEK_SET)) { printf_P(PSTR("error seeking on %s\n"), command); fat16_close_file(fd); continue; } /* read text from the shell and write it to the file */ uint8_t data_len; while(1) { /* give a different prompt */ uart_putc('<'); uart_putc(' '); /* read one line of text */ data_len = read_line(buffer, sizeof(buffer)); if(!data_len) break; /* write text to file */ if(fat16_write_file(fd, (uint8_t*) buffer, data_len) != data_len) { printf_P(PSTR("error writing to file\n")); break; } } fat16_close_file(fd); } #endif #if SD_RAW_WRITE_BUFFERING else if(strcmp_P(command, PSTR("sync")) == 0) { if(!sd_raw_sync()) printf_P(PSTR("error syncing disk\n")); } #endif else { printf_P(PSTR("unknown command: %s\n"), command); } } /* close file system */ fat16_close(fs); /* close partition */ partition_close(partition); return 0; }
uint8_t RepRapSDCard::init_card(void) { return sd_raw_init(); }