请教大家, /**************************************************************************** Desripton :read block in dma mode Fuctionname :ReadBlockInDMA() Input :None
Return :None *****************************************************************************/ u8 ReadBlockInDMA(u8 *pBuffer, u32 ReadAddr, u16 NumByteToRead) { //u32 i = 0; DMA_InitTypeDef DMA_InitStructure; u8 rvalue = MSD_RESPONSE_FAILURE; /* MSD chip select low */ MSD_CS_LOW();
/*initial dma channel 2*/ DMA_DeInit(DMA_Channel2);
DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)pBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = NumByteToRead; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA_Channel2, &DMA_InitStructure); SPI_DMACmd(SPI1, SPI_DMAReq_Rx, ENABLE); /* Send CMD17 (MSD_READ_SINGLE_BLOCK) to read one block */
MSD_SendCmd(MSD_READ_SINGLE_BLOCK, ReadAddr, 0xFF); if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR)) { /* Now look for the data token to signify the start of the data */ if (!MSD_GetResponse(MSD_START_DATA_SINGLE_BLOCK_READ)) {
DMA_Cmd(DMA_Channel2,ENABLE); while(!DMA_GetFlagStatus(DMA_FLAG_TC2)) { }
} } /* Get CRC bytes (not really needed by us, but required by MSD) */ MSD_ReadByte(); MSD_ReadByte();
/* Set response value to success */ rvalue = MSD_RESPONSE_NO_ERROR; DMA_Cmd(DMA_Channel2, DISABLE); /* MSD chip select high */ MSD_CS_HIGH();
/* Send dummy byte: 8 Clock pulses of delay */ MSD_WriteByte(DUMMY);
/* Returns the reponse */ return rvalue; }
/****************************************************************************** Description : Write byte in dma mode Fuctionname : WriteBlockInDMA(u8 *pBuffer,u32 WriteAddr, u16 NumByteToWrite) Input : pBuffer -- data buffer for send WriteAddr -- address to write NumByteToWrite -- data length of the data to write Output : None
*********************************************************************************/ u8 WriteBlockInDMA(u8 *pBuffer, u32 WriteAddr, u16 NumByteToWrite) { //u32 i = 0; DMA_InitTypeDef DMA_InitStructure; u8 rvalue = MSD_RESPONSE_FAILURE;
/* MSD chip select low */ MSD_CS_LOW();
/*initial the spi1 in write mode*/ /*initial dma channel 3*/ DMA_DeInit(DMA_Channel3); DMA_InitStructure.DMA_PeripheralBaseAddr = (u32)SPI1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (u32)pBuffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = NumByteToWrite; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA_Channel3, &DMA_InitStructure); SPI_DMACmd(SPI1, SPI_DMAReq_Tx, ENABLE); // SPI_Cmd(SPI1, ENABLE); MSD_SendCmd(MSD_SET_BLOCKLEN,512,0x00); MSD_ReadByte(); MSD_ReadByte(); /* Send CMD24 (MSD_WRITE_BLOCK) to write multiple block */ MSD_SendCmd(MSD_WRITE_BLOCK, WriteAddr, 0xFF);
/* Check if the MSD acknowledged the write block command: R1 response (0x00: no errors) */ if (!MSD_GetResponse(MSD_RESPONSE_NO_ERROR)) { /* Send a dummy byte */ MSD_WriteByte(DUMMY); /* Send the data token to signify the start of the data */ MSD_WriteByte(0xFE); /* Write the block data to MSD : write count data by block */ DMA_Cmd(DMA_Channel3, ENABLE); while(!DMA_GetFlagStatus(DMA_FLAG_TC3)); /* Put CRC bytes (not really needed by us, but required by MSD) */ MSD_ReadByte(); MSD_ReadByte(); /* Read data response */ if (MSD_GetDataResponse() == MSD_DATA_OK) { rvalue = MSD_RESPONSE_NO_ERROR; } } /* MSD chip select high */ MSD_CS_HIGH();
/* Send dummy byte: 8 Clock pulses of delay */ MSD_WriteByte(DUMMY);
DMA_Cmd(DMA_Channel3, DISABLE); /* Returns the reponse */ return rvalue; } 这是分别用dma读写sd卡的读和写一个block的程序,在程序中调用到这俩个程序时,dma读子程序在while(!DMA_GetFlagStatus(DMA_FLAG_TC2)){}这一直等待,也就是说dma传输没有完成。而调用写子程序是完全没问题的,也就是dma时钟等问题以不是问题,不知道还有什么没考虑到的????? 郁闷!!!!! |