signed char FAT_get_BS_data(void) { signed char resp; struct MBR_struct *mbr; uint32_t firstSec=0,uliTemp; char buf[16]; resp=sd_readSector(0,SD_SECSIZE); if(resp==-1) return resp; if(sd_buf[0]!=0xE9 && sd_buf[0]!=0xEB) { if(sd_buf[510]==0x55 && sd_buf[511]==0xAA) { mbr=(struct MBR_struct *)sd_buf; firstSec=(mbr->partitionData[11]<<24)|(mbr->partitionData[10]<<16)|(mbr->partitionData[9]<<8)|(mbr->partitionData[8]); resp=sd_readSector(firstSec,SD_SECSIZE); } else return -1; //NOT BS nor MBR } if(sd_buf[0x52+3]=='3') { FATType=FAT32; usart1_puts("\n\rVOLUME Label: "); for(resp=0;resp<11;resp++) usart1_putch(sd_buf[0x47+resp]); usart1_puts("\n\rFAT Type:"); for(resp=0;resp<8;resp++) usart1_putch(sd_buf[0x52+resp]); F32_getParameters(sd_buf,firstSec); } else { FATType=FAT16; usart1_puts("\n\rVOLUME Label: "); for(resp=0;resp<11;resp++) usart1_putch(sd_buf[0x2b+resp]); usart1_puts("\n\rFAT Type:"); for(resp=0;resp<8;resp++) usart1_putch(sd_buf[0x36+resp]); //F16_getParameters(sd_buf); } return 0; }
euint32 read_file_chain_length(SDfile fileInfo) { euint32 current_address = (fileInfo.startL | (euint32)fileInfo.startH<<16); euint32 FAT_start = LBAbegin + Part_Boot.res_sec; euint8 buffer1[size]; euint32 length = 0; euint32 modulus = 0; while(current_address < 0x0FFFFFF8) { //NOTE 0x0FFFFFF7 means the cluster is bad so we may need to skip euint32 FATNumber = (current_address*4)/512;// will floor for us // Read in the new sector sd_readSector(FAT_start+FATNumber,buffer1); //reads FAT // Find the offset from start of FATNumber as per the modulus modulus = (current_address/4)%512; // Increment now to try and catch the last entry as well length++; // Get next address current_address = (euint32)buffer1[current_address*4+3] << 24 | (euint32)buffer1[current_address*4+2] << 16 | (euint32)buffer1[current_address*4+1] << 8 | (euint32)buffer1[current_address*4]; } // vvv For debugging only vvv printf("Length of Chain: %d\r\n", length); return length; }
short if_readBuf(hwInterface* file,euint32 address,unsigned char* buf) { short r; r=sd_readSector(file,address,buf,512); if(r!=0)printf("ERROR READING SECTOR %i\n",address); return(r); }
euint8 read_rootTable(euint32 LBABegin,euint32 ResSect) { euint8 buffer[size]; sd_readSector(LBABegin+ResSect+3,buffer); printf("\n"); return 0; }
euint8 read_mboot(euint8* buffer) { if(sd_readSector(mboot,buffer) != 0) { printf("failed sector read\n"); return(-1); } return(0); }
esint8 if_readBuf(hwInterface* file,euint32 address,euint8* buf) { esint8 res; if_spiSelectDevice(file); res = sd_readSector(file,address,buf,512); if_spiUnselectDevice(file); return(res); }
void writeTest() { unsigned long int secNum; secNum=F32_getFirstSector(8); sd_writeSector(secNum,sd_buf); //uart0_dump(sd_buf,512,32); usart1_puts("\n\r"); sd_readSector(secNum,512); //uart0_dump(sd_buf,512,32); }
euint8 read_part_boot() { euint8 buffer[size]; if(sd_readSector(LBAbegin,buffer) != 0) { printf("failed sector read\n"); return(-1); } Part_Boot.bytes_per_sec = (buffer[11]|((euint16) buffer[12])<<8); Part_Boot.sec_per_clust = (euint8)buffer[13]; Part_Boot.res_sec = (buffer[14]|((euint16) buffer[15])<<8); Part_Boot.FAT_num = (euint8)buffer[16]; Part_Boot.sec_num_small =(buffer[19]|((euint16) buffer[20])<<8); Part_Boot.sec_num_big = ((euint32)buffer[32]|((euint32) buffer[33])<<8|((euint32) buffer[34])<<16|((euint32) buffer[35])<<24); Part_Boot.sec_per_FAT = ((euint32)buffer[36]|((euint32) buffer[37])<<8|((euint32) buffer[38])<<16|((euint32) buffer[39])<<24); Part_Boot.clust_num = ((euint32)buffer[44]|((euint32) buffer[45])<<8|((euint32) buffer[46])<<16|((euint32) buffer[47])<<24); return(0); }
//returns the address stored in FAT table in the address'th entry of the FAT table euint32 next_sect_address(euint32 address,euint32* next_FAT_address) { euint8 buffer[512]; euint32 FATnumber = (address*4)/512; euint32 FAT_address; euint32 relative_address; euint32 next_address; euint8 i; FAT_address = FAT_start + FATnumber; relative_address = (address*4 + FAT_start*512) - FAT_address*512; relative_address = (address*4)%512; sd_readSector(FAT_address,buffer); next_address = (euint32)buffer[relative_address] | ((euint32)buffer[relative_address+1])<<8 | ((euint32)buffer[relative_address+2])<<16 | ((euint32)buffer[relative_address+3])<<24; *next_FAT_address = next_address; next_address = (next_address -2)*Part_Boot.sec_per_clust + clust_start; return(next_address); }
esint8 if_readBuf(hwInterface* file,euint32 address,euint8* buf) { return(sd_readSector(file,address,buf,512)); }
euint8 open_wav(SDfile fileinfo) { euint8 buff[600]; euint32 buff2[512]; euint32 right[512]; euint32 left[512]; euint8 leftovers[512]; euint8 leftoverCount=0; euint32 count = 0; euint32 i,zz; euint32 tempright; euint32 temp[5]; euint32 overflow_amount = 0; euint32 LoopC = 0; euint32 stuff_to_write = 0; euint32 ints_to_send = 0; euint32 sample_rate = 0; euint32 bitdepth = 0; euint32 channels = 0; euint32 formatcode = 0; euint32 blocksize = 0; euint32 header_size = 0; euint32 next_address = 0; euint32 clust_address = 0; euint32 FAT_address = 0; euint32 filesize = 0; euint32 end_offset = 0; euint32 bytes_read = 0; euint8 end_of_file = 0; alt_up_audio_dev* audio_dev = alt_up_audio_open_dev(AUDIO_NAME); alt_up_audio_reset_audio_core(audio_dev); filesize = fileinfo.file_size; FAT_address = (euint32)fileinfo.startL | ((euint32)fileinfo.startH)<<16; clust_address = (FAT_address-2)*Part_Boot.sec_per_clust + clust_start; sd_readSector(clust_address,buff); //read in wav header info bytes_read += 512; //address = next_sect_address(address); header_size = get_wav_header(buff,&sample_rate,&bitdepth,&channels,&formatcode,&blocksize); //psuedo code for reading in wav details, returns wav info if(formatcode != 1) { UART_write("Error: incorrect wav format type, please use PCM\n\r"); return(1); } if(channels != 2) { UART_write("Error, number of channels must be 2\n\r"); return(1); } if((bitdepth != 32)&&(bitdepth != 16)&&(bitdepth != 24)&&(bitdepth != 8)) { UART_write("Error, bitdepth is incorrect\n\r"); return(1); } if(sample_rate != 8000) { UART_write("Error, sample rate is incorrect\n\r"); return(1); } switch(sample_rate) { case 8000: AUDIO_SetSampleRate(RATE_ADC8K_DAC8K_USB); break; case 32000: AUDIO_SetSampleRate(RATE_ADC32K_DAC32K_USB); break; case 44100: AUDIO_SetSampleRate(RATE_ADC44K_DAC44K_USB); break; case 48000: AUDIO_SetSampleRate(RATE_ADC48K_DAC48K_USB); break; case 96000: AUDIO_SetSampleRate(RATE_ADC96K_DAC96K_USB); break; default: printf("Non-standard sampling rate\n"); return -1; } count = count + header_size; while(end_of_file == 0) //start reading bytes from offset position, if there are bytes to be read, continue { //convert char buffer into 32 bit buffer while(LoopC < Part_Boot.sec_per_clust) { if(bitdepth == 32) { for(i=0;i<128;i++) { *((unsigned char*)&buff2[i]+0) = buff[count+0]; *((unsigned char*)&buff2[i]+1) = buff[count+1]; *((unsigned char*)&buff2[i]+2) = buff[count+2]; *((unsigned char*)&buff2[i]+3) = buff[count+3]; count += 4; if(count > 512) { ints_to_send = i+1; break; } if(i==127) ints_to_send = i+1; } count = 0; } if(bitdepth == 24) { for(i=0;i<170;i++) // PROBLEM HERE PROBS SINCE 512/3 ISNT WHOLE NUMBER { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = buff[count+0]; *((unsigned char*)&buff2[i]+2) = buff[count+1]; *((unsigned char*)&buff2[i]+3) = buff[count+2]; count += 3; if(count >= 512) { count = (count-512)+3; ints_to_send = i+1; break; } } } if(bitdepth == 16) { for(i=0;i<256;i++) { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = 0; *((unsigned char*)&buff2[i]+2) = buff[count+0]; *((unsigned char*)&buff2[i]+3) = buff[count+1]; count += 2; if(count > 512) { ints_to_send = i+1; break; } if(i==255) ints_to_send = i+1; } count = 0; } if(bitdepth == 8) { for(i=0;i<512;i++) { *((unsigned char*)&buff2[i]+0) = 0; *((unsigned char*)&buff2[i]+1) = 0; *((unsigned char*)&buff2[i]+2) = 0; *((unsigned char*)&buff2[i]+3) = buff[count+0]; count++; if(count > 512) { ints_to_send = i+1; break; } if(i==511) ints_to_send = i+1; } count = 0; } // pass to left and right channels for(i=0;i<256;i++) { left[i] = buff2[i*2]; right[i] = buff2[i*2+1]; } if(end_offset != 0) { stuff_to_write = end_offset/(1024/ints_to_send); end_of_file = 1; end_offset = 0; } else { stuff_to_write = ints_to_send/2; } //is there space in the fifo? while(alt_up_audio_write_fifo_space(audio_dev,0) < stuff_to_write); //send to audio channels alt_up_audio_write_fifo(audio_dev,right,stuff_to_write,ALT_UP_AUDIO_RIGHT); alt_up_audio_write_fifo(audio_dev,left,stuff_to_write,ALT_UP_AUDIO_LEFT); clust_address++; sd_readSector(clust_address,buff); bytes_read += 512; LoopC++; if(bytes_read >= filesize) { end_of_file = 1; //end_offset = 512 - (bytes_read-filesize); } } LoopC = 0; clust_address = next_sect_address(FAT_address,&next_address); FAT_address = next_address; if(bytes_read >= filesize) { end_of_file = 1; //end_offset = 512 - (bytes_read-filesize); } } return(0); }
int8_t if_readBuf(hwInterface* file, uint32_t address,uint8_t * buf) { return(sd_readSector(file,address,buf,512)); }