ВходНаше всё Теги codebook 无线电组件 Поиск Опросы Закон Воскресенье
24 ноября
51538
veter (16.02.2006 15:49, просмотров: 1893)
--- HELP --- проект на ARM-е и вопросы по MultiMediaCard (MMC) Люди Здравствуйте! Кто работал с MMC????? вопрос снова про работу с MMC картой. проблема такая - не хочет нормально писать и после читать. микроконтроллер - at91sam7s256 компилятор - GCC/linux описываю ситуевину: дело в том что запись/чтение работают, но всего один раз. после этого с карточки ни чего не читается, а именно после смены адреса блока. как проверяю я делаю запрос по USART и ответ записываю в - usart1_buffer[] далее для загрузки в ММС используется другой буфер - buffer[] и в него я копирую содержимое из usart1_buffer[] после передаю в карточку. после этого зразу же читаю и вывожу на ком-порт для проверки что бы узнать прочитал он чтото или выдал старое знгачение - предискажаю значение как for (c=0; c<512; c++){ mmc_buffer[c]='-'; } если было чтения (а мне изветно чтол должно быть там) то я увижу содержимое в терминале иначе увижу 512 символов "-" так вот я вижу то что мне надо только по адресу 0 в MMC а в остальные адреса толи не пишется, то ли не читается от туда, не пойму. и после второго раза чтения(на третьей потывке записи, тоесть по в блок номер 2) эта операция гдето зависает... помогите ПЛЗ! вот кусок кода к которомм производится сначало запись, и потом чтение из этого же блока. while(1){ if (global_temp=='W'){ for (c=0; c<512; c++){ buffer[c]=usart1_buffer[c]; } lcd_str("cp 512-RAM ",1,0); c=mmcWriteBlock(Xtemp); lcd_str("wr MMC OK ",1,0); global_temp='R'; } if (global_temp=='R'){ for (c=0; c<512; c++){ mmc_buffer[c]='-'; } mmcReadBlock(Xtemp,512); for (c=0; c<512; c++){ global_TX=mmc_buffer[c]; global_TX_priznak=1; Tx1_IRQ_on(); if (c>=511){ global_TX=10; global_TX_priznak=1; Tx1_IRQ_on(); global_temp=0xaa; if (Xtemp<1439){ lcd_str("rd MMC OK ",1,0); Xtemp++; lcd_sniffit(Xtemp,0,4); global_TX_priznak=0; Tx1_IRQ_on(); } } } } usart1_complite=0; } вот файл в котором находятся процедуры для работы с карточкой сразу придупреждаю - файл не мой - взятый от кудато с интернета. // mmc.c : MultiMediaCard functions: init, read, write ... // Rolf Freitag 5/2003 // // MMC Lib #ifndef _MMCLIB_C #define _MMCLIB_C //--------------------------------------------------------------------- #include "mmc.h" #include "at91sam7s64.h" //#include "math.h" #include "string.h" #include "variables.h" AT91PS_SPI s_pSpi = AT91C_BASE_SPI; AT91PS_PIO s_pPio = AT91C_BASE_PIOA; AT91PS_PMC s_pPMC = AT91C_BASE_PMC; AT91PS_PDC s_pPDC = AT91C_BASE_PDC_SPI; AT91PS_SYS s_pSys = AT91C_BASE_SYS; char mmcGetResponse(void); char mmcGetXXResponse(const char resp); char mmcCheckBusy(void); void initSSP (void); //char mmc_buffer[512] = { 0 }; // Buffer for mmc i/o for data and registers extern char card_state; // 0 for no card found, 1 for card found (init successfull) //--------------------------------------------------------------------- /*********************************************/ void Delays (unsigned long a) { while (--a!=0); } void Init_CP_WP (void) { // pull up resistor s_pSys->PIOA_PPUDR = 0xffffffff; // Disable Pull-up resistor //Card present -> CP - PA15 s_pPio->PIO_ODR = BIT15; //Configure in Input s_pPio->PIO_PER = BIT15; //Enable PA15 //Write protect -> WP - PA16 s_pPio->PIO_ODR = BIT16; //Configure in Input s_pPio->PIO_PER = BIT16; //Enable PA16 } /*********************************************/ // Initialisieren char init_MMC (void){ //raise SS and MOSI for 80 clock cycles //SendByte(0xff) 10 times with SS high //RAISE SS int i; char response=0x01; // debug_printf("Start iniMMC......"); // init_SPI(); //initialization sequence on PowerUp ///CS_HIGH(); for(i=0;i<=9;i++) spiSendByte(0xff); ///CS_LOW(); //Send Command 0 to put MMC in SPI mode mmcSendCmd(0x00,0,0x95); //Now wait for READY RESPONSE if(mmcGetResponse()!=0x01); // debug_printf("no responce"); while(response==0x01){ // debug_printf("Sending Command 1"); //CS_HIGH(); spiSendByte(0xff); //CS_LOW(); mmcSendCmd(0x01,0x00,0xff); response=mmcGetResponse(); } //CS_HIGH(); spiSendByte(0xff); // debug_printf("MMC INITIALIZED AND SET TO SPI MODE PROPERLY."); return MMC_SUCCESS; } /*********************************************/ void mmcSendCmd (const char cmd, unsigned long data, const char crc){ char frame[6]; char temp; int i; frame[0]=(cmd|0x40); for(i=3;i>=0;i--){ temp=(char)(data>>(8*i)); frame[4-i]=(temp); } frame[5]=(crc); for(i=0;i<6;i++) spiSendByte(frame[i]); } /*********************************************/ // Ti added mmc Get Responce char mmcGetResponse(void){ //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i=0; char response; while(i<=64) { response=spiSendByte(0xff); if(response==0x00)break; if(response==0x01)break; i++; } return response; } /*********************************************/ char mmcGetXXResponse(const char resp){ //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i=0; char response; while(i<=500) { response=spiSendByte(0xff); if(response==resp)break; i++; } return response; } /*********************************************/ char mmcCheckBusy(void){ //Response comes 1-8bytes after command //the first bit will be a 0 //followed by an error code //data will be 0xff until response int i=0; char response; char rvalue; while(i<=64) { response=spiSendByte(0xff); response &= 0x1f; switch(response){ case 0x05: rvalue=MMC_SUCCESS;break; case 0x0b: return(MMC_CRC_ERROR); case 0x0d: return(MMC_WRITE_ERROR); default: rvalue = MMC_OTHER_ERROR; break; } if(rvalue==MMC_SUCCESS)break; i++; } i=0; do { response=spiSendByte(0xff); i++; }while(response==0); return response; } // The card will respond with a standard response token followed by a data // block suffixed with a 16 bit CRC. /*********************************************/ // Ti Modification: long int -> long ; int -> long char mmcReadBlock(const unsigned long address, const unsigned long count){ unsigned long i = 0,cnt; char rvalue = MMC_RESPONSE_ERROR; // Set the block length to read if (mmcSetBlockLength (count) == MMC_SUCCESS) // block length could be set { //SS = LOW (on) //CS_LOW (); // send read command MMC_READ_SINGLE_BLOCK=CMD17 mmcSendCmd (17,address, 0xFF); // Send 8 Clock pulses of delay, check if the MMC acknowledged the read block command // it will do this by sending an affirmative response // in the R1 format (0x00 is no errors) //for(cnt=0; cnt< 511; cnt++){ mmc_buffer[cnt]=511-cnt&0xff; } if (mmcGetResponse() == 0x00){ // now look for the data token to signify the start of // the data if (mmcGetXXResponse(MMC_START_DATA_BLOCK_TOKEN) == MMC_START_DATA_BLOCK_TOKEN) { // clock the actual data transfer and receive the bytes; spi_read automatically finds the Data Block for (i = 0; i < 512; i++) mmc_buffer[i] = spiSendByte(0xff); // is executed with card inserted // get CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); rvalue = MMC_SUCCESS; } else { // the data token was never received rvalue = MMC_DATA_TOKEN_ERROR; // 3 } } else { // the MMC never acknowledge the read command rvalue = MMC_RESPONSE_ERROR; // 2 } } else { rvalue = MMC_BLOCK_SET_ERROR; // 1 } //CS_HIGH (); spiSendByte(0xff); return rvalue; } // mmc_read_block /*********************************************/ //--------------------------------------------------------------------- // Ti Modification: long int -> long //char mmcWriteBlock (const unsigned long address){ char mmcWriteBlock (unsigned long address){ unsigned long i = 0; char rvalue = MMC_RESPONSE_ERROR; // MMC_SUCCESS; //char c = 0x00; // Set the block length to read if (mmcSetBlockLength (512) == MMC_SUCCESS) // block length could be set { // SS = LOW (on) //CS_LOW (); // send write command mmcSendCmd (24,address, 0xFF); // check if the MMC acknowledged the write block command // it will do this by sending an affirmative response // in the R1 format (0x00 is no errors) if (mmcGetXXResponse(MMC_R1_RESPONSE) == MMC_R1_RESPONSE){ spiSendByte(0xff); // send the data token to signify the start of the data spiSendByte(0xfe); // clock the actual data transfer and transmitt the bytes for (i = 0; i < 512; i++) // spiSendByte(mmc_buffer[i]); // mmc_buffer[i]; Test: i & 0xff spiSendByte(buffer[i]); // mmc_buffer[i]; Test: i & 0xff // put CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); // read the data response xxx01 : status 010: Data accected, status 101: Data // rejected due to a crc error, status 110: Data rejected due to a Write error. mmcCheckBusy(); }else{ // the MMC never acknowledge the write command // rvalue = MMC_RESPONSE_ERROR; // 2 } } else { rvalue = MMC_BLOCK_SET_ERROR; // 1 } //give the MMC the required clocks to finish up what ever it needs to do //for (i = 0; i < 9; ++i) //spiSendByte(0xff); //CS_HIGH (); // Send 8 Clock pulses of delay. spiSendByte(0xff); return rvalue; /* unsigned long i = 0; char rvalue = MMC_RESPONSE_ERROR; // MMC_SUCCESS; //char c = 0x00; // Set the block length to read if (mmcSetBlockLength (512) == MMC_SUCCESS) // block length could be set { } else { return MMC_BLOCK_SET_ERROR; // 1 } // SS = LOW (on) //CS_LOW (); // send write command mmcSendCmd (24,address, 0xFF); // check if the MMC acknowledged the write block command // it will do this by sending an affirmative response // in the R1 format (0x00 is no errors) if (mmcGetXXResponse(MMC_R1_RESPONSE) == MMC_R1_RESPONSE){ }else{ // the MMC never acknowledge the write command return MMC_RESPONSE_ERROR; // 2 } spiSendByte(0xff); // send the data token to signify the start of the data spiSendByte(0xfe); // clock the actual data transfer and transmitt the bytes for (i = 0; i < 512; i++) // spiSendByte(mmc_buffer[i]); // mmc_buffer[i]; Test: i & 0xff spiSendByte(buffer[i]); // mmc_buffer[i]; Test: i & 0xff // put CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); // read the data response xxx01 : status 010: Data accected, status 101: Data // rejected due to a crc error, status 110: Data rejected due to a Write error. mmcCheckBusy(); //give the MMC the required clocks to finish up what ever it needs to do //for (i = 0; i < 9; ++i) //spiSendByte(0xff); //CS_HIGH (); // Send 8 Clock pulses of delay. spiSendByte(0xff); // return rvalue; */ }// mmc_write_block //--------------------------------------------------------------------- /*********************************************/ //--------------- set blocklength 2^n ------------------------------------------------------ // Ti Modification: long int-> long char mmcSetBlockLength (const unsigned long blocklength){ //char rValue = MMC_TIMEOUT_ERROR; //char i = 0; // SS = LOW (on) //CS_LOW (); // Set the block length to read //MMC_SET_BLOCKLEN =CMD16 mmcSendCmd(16, blocklength, 0xFF); // get response from MMC - make sure that its 0x00 (R1 ok response format) if(mmcGetResponse()!=0x00); //CS_HIGH (); // Send 8 Clock pulses of delay. spiSendByte(0xff); return MMC_SUCCESS; } /*********************************************/ unsigned char spiSendByte(const unsigned char data){ unsigned int spib; while((s_pSpi->SPI_SR & AT91C_SPI_TDRE) == 0); // Wait for the transfer to complete s_pSpi->SPI_TDR = (data & 0xFFFF); // Send the data while((s_pSpi->SPI_SR & AT91C_SPI_RDRF) == 0); // Wait until the character can be sent spib = ((s_pSpi->SPI_RDR) & 0xFFFF); // Get the data received return spib; } /*********************************************/ // Reading the contents of the CSD and CID registers in SPI mode is a simple // read-block transaction. char mmcReadRegister (const char cmd_register, const unsigned char length){ char uc = 0; char rvalue = MMC_TIMEOUT_ERROR; // char i = 0; if (mmcSetBlockLength (length) == MMC_SUCCESS){ //CS_LOW (); // CRC not used: 0xff as last byte mmcSendCmd(cmd_register, 0x000000, 0xff); // wait for response // in the R1 format (0x00 is no errors) if (mmcGetResponse() == 0x00){ if (mmcGetXXResponse(0xfe)== 0xfe) for (uc = 0; uc < length; uc++) mmc_buffer[uc] = spiSendByte(0xff); // get CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); } else rvalue = MMC_RESPONSE_ERROR; //CS=HIGH (off) //CS_HIGH (); // Send 8 Clock pulses of delay. spiSendByte(0xff); } //CS_HIGH (); return rvalue; }// mmc_read_register /*********************************************/ //--------------------------------------------------------------------- #endif /* _MMCLIB_C */ и заголовочный файл /* mmc.h: Dekcarations for Communikation with the MMC (see mmc.c) in unprotected spi mode. Pin configuration at MSP430F149: -------------------------------- MC MC Pin MMC MMC Pin P5.4 48 ChipSelect 1 P5.1 / SlaveInMasterOut 45 DataIn 2 . GND 3 (0 V) . VDD 4 (3.3 V) P5.3 UCLK1 / SlaveCLocK 47 Clock 5 . GND 6 (0 V) P5.2 / SlaveOutMasterIn 46 DataOut 7 --------------------------------------------------------------------- Revisions Date Author Revision 11. May 2003 Rolf Freitag 0.02 (2004: corrected MC pin numbers (switched only 45, 46)) */ #ifndef _MMCLIB_H #define _MMCLIB_H #ifndef TXEPT // transmitter-empty flag #define TEXPT 0x01 #endif // macro defines #define HIGH(a) ((a>>8)&0xFF) // high byte from word #define LOW(a) (a&0xFF) // low byte from word //#define CS_LOW() GPPOA &= ~0x00000080 //#define CS_HIGH() GPPOA |= 0x00000080 #define SPI_RXC (IFG2 & URXIFG1) #define SPI_TXC (IFG2 & UTXIFG1) #define SPI_RX_COMPLETE (IFG2 & URXIFG1) #define SPI_TX_READY (IFG2 & UTXIFG1) #define DUMMY 0xff // Tokens (nessisary because at nop/idle (and CS active) only 0xff is on the data/command line) #define MMC_START_DATA_BLOCK_TOKEN 0xfe // Data token start byte, Start Single Block Read #define MMC_START_DATA_MULTIPLE_BLOCK_READ 0xfe // Data token start byte, Start Multiple Block Read #define MMC_START_DATA_BLOCK_WRITE 0xfe // Data token start byte, Start Single Block Write #define MMC_START_DATA_MULTIPLE_BLOCK_WRITE 0xfc // Data token start byte, Start Multiple Block Write #define MMC_STOP_DATA_MULTIPLE_BLOCK_WRITE 0xfd // Data toke stop byte, Stop Multiple Block Write // an affirmative R1 response (no errors) #define MMC_R1_RESPONSE 0x00 // this variable will be used to track the current block length // this allows the block length to be set only when needed // unsigned long _BlockLength = 0; // error/success codes #define MMC_SUCCESS 0x00 #define MMC_BLOCK_SET_ERROR 0x01 #define MMC_RESPONSE_ERROR 0x02 #define MMC_DATA_TOKEN_ERROR 0x03 #define MMC_INIT_ERROR 0x04 #define MMC_CRC_ERROR 0x10 #define MMC_WRITE_ERROR 0x11 #define MMC_OTHER_ERROR 0x12 #define MMC_TIMEOUT_ERROR 0xFF // commands: first bit 0 (start bit), second 1 (transmission bit); CMD-number + 0ffsett 0x40 #define MMC_GO_IDLE_STATE 0x40 //CMD0 #define MMC_SEND_OP_COND 0x41 //CMD1 #define MMC_READ_CSD 0x49 //CMD9 #define MMC_SEND_CID 0x4a //CMD10 #define MMC_STOP_TRANSMISSION 0x4c //CMD12 #define MMC_SEND_STATUS 0x4d //CMD13 #define MMC_SET_BLOCKLEN 0x50 //CMD16 Set block length for next read/write #define MMC_READ_SINGLE_BLOCK 0x51 //CMD17 Read block from memory #define MMC_READ_MULTIPLE_BLOCK 0x52 //CMD18 #define MMC_CMD_WRITEBLOCK 0x54 //CMD20 Write block to memory #define MMC_WRITE BLOCK 0x58 //CMD25 #define MMC_WRITE_MULTIPLE_BLOCK 0x59 //CMD?? #define MMC_WRITE_CSD 0x5b //CMD27 PROGRAM_CSD #define MMC_SET_WRITE_PROT 0x5c //CMD28 #define MMC_CLR_WRITE_PROT 0x5d //CMD29 #define MMC_SEND_WRITE_PROT 0x5e //CMD30 #define MMC_TAG_SECTOR_START 0x60 //CMD32 #define MMC_TAG_SECTOR_END 0x61 //CMD33 #define MMC_UNTAG_SECTOR 0x62 //CMD34 #define MMC_TAG_EREASE_GROUP_START 0x63 //CMD35 #define MMC_TAG_EREASE_GROUP_END 0x64 //CMD36 #define MMC_UNTAG_EREASE_GROUP 0x65 //CMD37 #define MMC_EREASE 0x66 //CMD38 #define MMC_READ_OCR 0x67 //CMD39 #define MMC_CRC_ON_OFF 0x68 //CMD40 #define BIT0 0x00000001 #define BIT1 0x00000002 #define BIT2 0x00000004 #define BIT3 0x00000008 #define BIT4 0x00000010 #define BIT5 0x00000020 #define BIT6 0x00000040 #define BIT7 0x00000080 #define BIT8 0x00000100 #define BIT9 0x00000200 #define BIT10 0x00000400 #define BIT11 0x00000800 #define BIT12 0x00001000 #define BIT13 0x00002000 #define BIT14 0x00004000 #define BIT15 0x00008000 #define BIT16 0x00010000 #define BIT17 0x00020000 #define BIT18 0x00040000 #define BIT19 0x00080000 #define BIT20 0x00100000 #define BIT21 0x00200000 #define BIT22 0x00400000 #define BIT23 0x00800000 #define BIT24 0x01000000 #define BIT25 0x02000000 #define BIT26 0x04000000 #define BIT27 0x08000000 #define BIT28 0x10000000 #define BIT29 0x20000000 #define BIT30 0x40000000 #define BIT31 0x80000000 //TI added sub function for top two spi_xxx //my unsigned char spiReadByte(); void spiWriteByte(const unsigned char data); void initSPI(void); // Card present, Write protect void Init_CP_WP (void); // mmc init char initMMC (void); // send command to MMC void mmcSendCmd (const char cmd, unsigned long data, const char crc); // set MMC block length of count=2^n Byte char mmcSetBlockLength (const unsigned long); // read a size Byte big block beginning at the address. char mmcReadBlock(const unsigned long address, const unsigned long count); // write a 512 Byte big block beginning at the (aligned) adress //char mmcWriteBlock (const unsigned long address); char mmcWriteBlock (unsigned long address); // Register arg1 der Laenge arg2 auslesen (into the buffer) char mmcReadRegister(const char, const unsigned char); //transmit character via SPI unsigned char spiSendByte(const unsigned char data); #endif /* _MMCLIB_H */