/** * @brief Initializes the SD Card and put it into StandBy State (Ready for * data transfer). * @retval None */ void SD_IO_Init(void) { GPIO_InitTypeDef gpioinitstruct = {0}; uint8_t counter = 0; /* SD_CS_GPIO Periph clock enable */ SD_CS_GPIO_CLK_ENABLE(); /* Configure SD_CS_PIN pin: SD Card CS pin */ gpioinitstruct.Pin = SD_CS_PIN; gpioinitstruct.Mode = GPIO_MODE_OUTPUT_PP; gpioinitstruct.Pull = GPIO_PULLUP; gpioinitstruct.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(SD_CS_GPIO_PORT, &gpioinitstruct); /*------------Put SD in SPI mode--------------*/ /* SD SPI Config */ SPIx_Init(); /* SD chip select high */ SD_CS_HIGH(); /* Send dummy byte 0xFF, 10 times with CS high */ /* Rise CS and MOSI for 80 clocks cycles */ for (counter = 0; counter <= 9; counter++) { /* Send dummy byte 0xFF */ SD_IO_WriteByte(SD_DUMMY_BYTE); } }
/** * @brief Sends dummy byte with CS High * @retval None */ void SD_IO_WriteDummy(void) { /* SD chip select high */ SD_CS_HIGH(); /* Send Dummy byte 0xFF */ SD_IO_WriteByte(SD_DUMMY_BYTE); }
/** * @brief Write a buffer (many blocks) in the SD card. * @note The amount of data to write should be a multiple of SD card BLOCK * size (512 Byte). * @param pBuffer : pointer to the buffer containing the data to be written on the SD. * @param WriteAddr : address to write on. * @param NumByteToWrite : number of data to write. * @retval SD Response: * - SD_RESPONSE_FAILURE: Sequence failed. * - SD_RESPONSE_NO_ERROR: Sequence succeed. */ uint8_t SD_WriteBuffer(uint8_t *pBuffer, uint32_t WriteAddr, uint32_t NumByteToWrite) { uint32_t i = 0, NbrOfBlock = 0, Offset = 0; uint8_t rvalue = SD_RESPONSE_FAILURE; /* Calculate number of blocks to write */ NbrOfBlock = NumByteToWrite / SD_BLOCK_SIZE; /* SD chip select low */ SD_CS_LOW(); /* Data transfer */ while (NbrOfBlock--) { /* Send CMD24 (SD_WRITE_BLOCK) to write blocks */ SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr + Offset, 0xFF); /* Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */ if (SD_GetResponse(SD_RESPONSE_NO_ERROR)) { return SD_RESPONSE_FAILURE; } /* Send dummy byte */ SD_WriteByte(SD_DUMMY_BYTE); /* Send the data token to signify the start of the data */ SD_WriteByte(SD_START_DATA_SINGLE_BLOCK_WRITE); /* Write the block data to SD : write count data by block */ for (i = 0; i < SD_BLOCK_SIZE; i++) { /* Send the pointed byte */ SD_WriteByte(*pBuffer); /* Point to the next location where the byte read will be saved */ pBuffer++; } /* Set next write address */ Offset += 512; /* Put CRC bytes (not really needed by us, but required by SD) */ SD_ReadByte(); SD_ReadByte(); /* Read data response */ if (SD_GetDataResponse() == SD_DATA_OK) { /* Set response value to success */ rvalue = SD_RESPONSE_NO_ERROR; } else { /* Set response value to failure */ rvalue = SD_RESPONSE_FAILURE; } } /* SD chip select high */ SD_CS_HIGH(); /* Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /* Returns the response */ return rvalue; }
/** * @brief Reads multiple block of data from the SD. * @param pBuffer: pointer to the buffer that receives the data read from the * SD. * @param ReadAddr: SD's internal address to read from. * @param BlockSize: the SD card Data block size. * @param NumberOfBlocks: number of blocks to be read. * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks) { uint32_t i = 0, Offset = 0; SD_Error rvalue = SD_RESPONSE_FAILURE; /*!< SD chip select low */ SD_CS_LOW(); /*!< Data transfer */ while (NumberOfBlocks--) { if(flag_SDHC == 1) { /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */ SD_SendCmd (SD_CMD_READ_SINGLE_BLOCK,(ReadAddr + Offset)/512, 0xFF); } else { /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */ SD_SendCmd (SD_CMD_READ_SINGLE_BLOCK, ReadAddr + Offset, 0xFF); } /*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */ if (SD_GetResponse(SD_RESPONSE_NO_ERROR)) { return SD_RESPONSE_FAILURE; } /*!< Now look for the data token to signify the start of the data */ if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ)) { /*!< Read the SD block data : read NumByteToRead data */ for (i = 0; i < BlockSize; i++) { /*!< Read the pointed data */ *pBuffer = SD_ReadByte(); /*!< Point to the next location where the byte read will be saved */ pBuffer++; } /*!< Set next read address*/ Offset += 512; /*!< get CRC bytes (not really needed by us, but required by SD) */ SD_ReadByte(); SD_ReadByte(); /*!< Set response value to success */ rvalue = SD_RESPONSE_NO_ERROR; } else { /*!< Set response value to failure */ rvalue = SD_RESPONSE_FAILURE; } } /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Returns the reponse */ return rvalue; }
/** * @brief Writes a block on the SD * @param pBuffer: pointer to the buffer containing the data to be written on * the SD. * @param WriteAddr: address to write on. * @param BlockSize: the SD card Data block size. * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ uint8_t SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize) { uint32_t i = 0; uint8_t rvalue = SD_RESPONSE_FAILURE; /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write multiple block */ SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr, 0xFF); /*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */ if (!SD_GetResponse(SD_RESPONSE_NO_ERROR)) { /*!< Send a dummy byte */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Send the data token to signify the start of the data */ SD_WriteByte(0xFE); /*!< Write the block data to SD : write count data by block */ for (i = 0; i < BlockSize; i++) { /*!< Send the pointed byte */ SD_WriteByte(*pBuffer); /*!< Point to the next location where the byte read will be saved */ pBuffer++; } /* Send DUMMY bytes when the number of data to be written are lower than the SD card BLOCK size (512 Byte) */ for (; i != SD_BLOCK_SIZE; i++) { /* Send the pointed byte */ SD_WriteByte(SD_DUMMY_BYTE); } /*!< Put CRC bytes (not really needed by us, but required by SD) */ SD_ReadByte(); SD_ReadByte(); /*!< Read data response */ if (SD_GetDataResponse() == SD_DATA_OK) { rvalue = SD_RESPONSE_NO_ERROR; } } /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Returns the response */ return rvalue; }
void SD_IO_CSState(uint8_t val) { if(val == 1) { SD_CS_HIGH(); } else { SD_CS_LOW(); } }
/** * @brief Put SD in Idle state. * @param None * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_GoIdleState(void) { /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode */ SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95); /*!< Wait for In Idle State Response (R1 Format) equal to 0x01 */ if (SD_GetResponse(SD_IN_IDLE_STATE)) { /*!< No Idle State Response: return response failue */ return SD_RESPONSE_FAILURE; } /*----------Activates the card initialization process-----------*/ do { /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send Dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD1 (Activates the card process) until response equal to 0x0 */ SD_SendCmd(SD_CMD_SEND_OP_COND, 0, 0xFF); /*!< Wait for no error Response (R1 Format) equal to 0x00 */ } while (SD_GetResponse(SD_RESPONSE_NO_ERROR)); /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); return SD_RESPONSE_NO_ERROR; }
/** * @brief Reads a block of data from the SD. * @param pBuffer: pointer to the buffer that receives the data read from the * SD. * @param ReadAddr: SD's internal address to read from. * @param BlockSize: the SD card Data block size. * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ uint8_t SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize) { uint32_t i = 0; uint8_t rvalue = SD_RESPONSE_FAILURE; /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */ SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, ReadAddr, 0xFF); /*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */ if (!SD_GetResponse(SD_RESPONSE_NO_ERROR)) { /*!< Now look for the data token to signify the start of the data */ if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ)) { /*!< Read the SD block data : read NumByteToRead data */ for (i = 0; i < BlockSize; i++) { /*!< Save the received data */ *pBuffer = SD_ReadByte(); /*!< Point to the next location where the byte read will be saved */ pBuffer++; } /*!< Get CRC bytes (not really needed by us, but required by SD) */ SD_ReadByte(); SD_ReadByte(); /*!< Set response value to success */ rvalue = SD_RESPONSE_NO_ERROR; } } /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Returns the response */ return rvalue; }
/** * @brief Initializes the SD Card and put it into StandBy State (Ready for * data transfer). * @param None * @retval None */ void SD_IO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; uint8_t counter; /* SD_CS_GPIO Periph clock enable */ SD_CS_GPIO_CLK_ENABLE(); /* Configure SD_CS_PIN pin: SD Card CS pin */ GPIO_InitStruct.Pin = SD_CS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct); /* LCD chip select line perturbs SD also when the LCD is not used */ /* this is a workaround to avoid sporadic failures during r/w operations */ LCD_CS_GPIO_CLK_ENABLE(); GPIO_InitStruct.Pin = LCD_CS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; HAL_GPIO_Init(LCD_CS_GPIO_PORT, &GPIO_InitStruct); LCD_CS_HIGH(); /*------------Put SD in SPI mode--------------*/ /* SD SPI Config */ SPIx_Init(); /* SD chip select high */ SD_CS_HIGH(); /* Send dummy byte 0xFF, 10 times with CS high */ /* Rise CS and MOSI for 80 clocks cycles */ for (counter = 0; counter <= 9; counter++) { /* Send dummy byte 0xFF */ SD_IO_WriteByte(SD_DUMMY_BYTE); } }
/** * @brief Initializes the SD Card and put it into StandBy State (Ready for * data transfer). * @param None * @retval None */ void SD_IO_Init(void) { GPIO_InitTypeDef GPIO_InitStructureure; uint8_t counter; /* SD_CS_GPIO and SD_DETECT_GPIO Periph clock enable */ SD_CS_GPIO_CLK_ENABLE(); SD_DETECT_GPIO_CLK_ENABLE(); /* Configure SD_CS_PIN pin: SD Card CS pin */ GPIO_InitStructureure.Pin = SD_CS_PIN; GPIO_InitStructureure.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStructureure.Pull = GPIO_PULLUP; GPIO_InitStructureure.Speed = GPIO_SPEED_HIGH; HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStructureure); /* Configure SD_DETECT_PIN pin: SD Card detect pin */ GPIO_InitStructureure.Pin = SD_DETECT_PIN; GPIO_InitStructureure.Mode = GPIO_MODE_IT_RISING_FALLING; GPIO_InitStructureure.Pull = GPIO_PULLUP; HAL_GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructureure); /* Enable and set SD EXTI Interrupt to the lowest priority */ HAL_NVIC_SetPriority(SD_DETECT_EXTI_IRQn, 0x0F, 0); HAL_NVIC_EnableIRQ(SD_DETECT_EXTI_IRQn); /*------------Put SD in SPI mode--------------*/ /* SD SPI Config */ SPIx_Init(); /* SD chip select high */ SD_CS_HIGH(); /* Send dummy byte 0xFF, 10 times with CS high */ /* Rise CS and MOSI for 80 clocks cycles */ for (counter = 0; counter <= 9; counter++) { /* Send dummy byte 0xFF */ SD_IO_WriteByte(SD_DUMMY_BYTE); } }
/** * @brief Returns the SD status. * @param None * @retval The SD status. */ uint16_t SD_GetStatus(void) { uint16_t Status = 0; /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD13 (SD_SEND_STATUS) to get SD status */ SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF); Status = SD_ReadByte(); Status |= (uint16_t)(SD_ReadByte() << 8); /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); return Status; }
/** * @brief Initializes the SD/SD communication. * @param None * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_Init(void) { uint32_t i = 0; /*!< Initialize SD_SPI */ SD_LowLevel_Init(); /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte 0xFF, 10 times with CS high */ /*!< Rise CS and MOSI for 80 clocks cycles */ for (i = 0; i <= 9; i++) { /*!< Send dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); } /*------------Put SD in SPI mode--------------*/ /*!< SD initialized and set to SPI mode properly */ return (SD_GoIdleState()); }
/** * @brief Initializes the SD/SD communication. * @param None * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_Init(void) { uint32_t TimeOut, i = 0; SD_Error Status = SD_RESPONSE_NO_ERROR; /*!< Initialize SD_SPI 初始化SPI相应管脚与处理器的SPI接口 */ //printf("(SD Init in SPI mode)"); SD_LowLevel_Init(); SD_SPI_SetSpeedLow(); //rt_kprintf("sd_init 0\n"); /*------------Put SD in SPI mode--------------*/ /*!< SD initialized and set to SPI mode properly */ TimeOut = 0; do { /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte 0xFF, 10 times with CS high */ /*!< Rise CS and MOSI for 80 clocks cycles */ for (i = 0; i <= 9; i++) { /*!< Send dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); } Status = SD_GoIdleState(); //rt_kprintf("sd_init 1 %d\n",(uint32_t)Status); if(TimeOut > 6) { break; } TimeOut++; }while(Status); SD_SPI_SetSpeedHi(); return (Status); }
/** * @brief Initializes the SD Card and put it into StandBy State (Ready for * data transfer). * @param None * @retval None */ void SD_IO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct; uint8_t counter; /* SD_CS_GPIO and SD_DETECT_GPIO Periph clock enable */ SD_CS_GPIO_CLK_ENABLE(); /* Configure SD_CS_PIN pin: SD Card CS pin */ GPIO_InitStruct.Pin = SD_CS_PIN; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_PULLUP; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM ; HAL_GPIO_Init(SD_CS_GPIO_PORT, &GPIO_InitStruct); /* Configure SD_DETECT_PIN pin: SD Card detect pin (Via MFX) */ if (BSP_IO_Init() == IO_OK) { BSP_IO_ConfigPin(SD_DETECT_PIN, IO_MODE_INPUT_PU); } /*------------Put SD in SPI mode--------------*/ /* SD SPI Config */ SPIx_Init(); /* SD chip select high */ SD_CS_HIGH(); /* Send dummy byte 0xFF, 10 times with CS high */ /* Rise CS and MOSI for 80 clocks cycles */ for (counter = 0; counter <= 9; counter++) { /* Send dummy byte 0xFF */ SD_IO_WriteByte(SD_DUMMY_BYTE); } }
/** * @brief Read the CID card register. * Reading the contents of the CID register in SPI mode is a simple * read-block transaction. * @param SD_cid: pointer on an CID register structure * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_GetCIDRegister(SD_CID* SD_cid) { uint32_t i = 0; SD_Error rvalue = SD_RESPONSE_FAILURE; uint8_t CID_Tab[16]; /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD10 (CID register) */ SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF); /*!< Wait for response in the R1 format (0x00 is no errors) */ if (!SD_GetResponse(SD_RESPONSE_NO_ERROR)) { if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ)) { /*!< Store CID register value on CID_Tab */ for (i = 0; i < 16; i++) { CID_Tab[i] = SD_ReadByte(); } } /*!< Get CRC bytes (not really needed by us, but required by SD) */ SD_WriteByte(SD_DUMMY_BYTE); SD_WriteByte(SD_DUMMY_BYTE); /*!< Set response value to success */ rvalue = SD_RESPONSE_NO_ERROR; } /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Byte 0 */ SD_cid->ManufacturerID = CID_Tab[0]; /*!< Byte 1 */ SD_cid->OEM_AppliID = CID_Tab[1] << 8; /*!< Byte 2 */ SD_cid->OEM_AppliID |= CID_Tab[2]; /*!< Byte 3 */ SD_cid->ProdName1 = CID_Tab[3] << 24; /*!< Byte 4 */ SD_cid->ProdName1 |= CID_Tab[4] << 16; /*!< Byte 5 */ SD_cid->ProdName1 |= CID_Tab[5] << 8; /*!< Byte 6 */ SD_cid->ProdName1 |= CID_Tab[6]; /*!< Byte 7 */ SD_cid->ProdName2 = CID_Tab[7]; /*!< Byte 8 */ SD_cid->ProdRev = CID_Tab[8]; /*!< Byte 9 */ SD_cid->ProdSN = CID_Tab[9] << 24; /*!< Byte 10 */ SD_cid->ProdSN |= CID_Tab[10] << 16; /*!< Byte 11 */ SD_cid->ProdSN |= CID_Tab[11] << 8; /*!< Byte 12 */ SD_cid->ProdSN |= CID_Tab[12]; /*!< Byte 13 */ SD_cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4; SD_cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8; /*!< Byte 14 */ SD_cid->ManufactDate |= CID_Tab[14]; /*!< Byte 15 */ SD_cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1; SD_cid->Reserved2 = 1; /*!< Return the reponse */ return rvalue; }
/** * @brief Read the CSD card register. * Reading the contents of the CSD register in SPI mode is a simple * read-block transaction. * @param SD_csd: pointer on an SCD register structure * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_GetCSDRegister(SD_CSD* SD_csd) { uint32_t i = 0; SD_Error rvalue = SD_RESPONSE_FAILURE; uint8_t CSD_Tab[16]; /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD9 (CSD register) or CMD10(CSD register) */ SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF); /*!< Wait for response in the R1 format (0x00 is no errors) */ if (!SD_GetResponse(SD_RESPONSE_NO_ERROR)) { if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ)) { for (i = 0; i < 16; i++) { /*!< Store CSD register value on CSD_Tab */ CSD_Tab[i] = SD_ReadByte(); } } /*!< Get CRC bytes (not really needed by us, but required by SD) */ SD_WriteByte(SD_DUMMY_BYTE); SD_WriteByte(SD_DUMMY_BYTE); /*!< Set response value to success */ rvalue = SD_RESPONSE_NO_ERROR; } /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Byte 0 */ SD_csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6; SD_csd->SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2; SD_csd->Reserved1 = CSD_Tab[0] & 0x03; /*!< Byte 1 */ SD_csd->TAAC = CSD_Tab[1]; /*!< Byte 2 */ SD_csd->NSAC = CSD_Tab[2]; /*!< Byte 3 */ SD_csd->MaxBusClkFrec = CSD_Tab[3]; /*!< Byte 4 */ SD_csd->CardComdClasses = CSD_Tab[4] << 4; /*!< Byte 5 */ SD_csd->CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4; SD_csd->RdBlockLen = CSD_Tab[5] & 0x0F; /*!< Byte 6 */ SD_csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7; SD_csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6; SD_csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5; SD_csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4; SD_csd->Reserved2 = 0; /*!< Reserved */ SD_csd->DeviceSize = (CSD_Tab[6] & 0x03) << 10; /*!< Byte 7 */ SD_csd->DeviceSize |= (CSD_Tab[7]) << 2; /*!< Byte 8 */ SD_csd->DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6; SD_csd->MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3; SD_csd->MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07); /*!< Byte 9 */ SD_csd->MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5; SD_csd->MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2; SD_csd->DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1; /*!< Byte 10 */ SD_csd->DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7; SD_csd->EraseGrSize = (CSD_Tab[10] & 0x40) >> 6; SD_csd->EraseGrMul = (CSD_Tab[10] & 0x3F) << 1; /*!< Byte 11 */ SD_csd->EraseGrMul |= (CSD_Tab[11] & 0x80) >> 7; SD_csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F); /*!< Byte 12 */ SD_csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7; SD_csd->ManDeflECC = (CSD_Tab[12] & 0x60) >> 5; SD_csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2; SD_csd->MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2; /*!< Byte 13 */ SD_csd->MaxWrBlockLen |= (CSD_Tab[13] & 0xC0) >> 6; SD_csd->WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5; SD_csd->Reserved3 = 0; SD_csd->ContentProtectAppli = (CSD_Tab[13] & 0x01); /*!< Byte 14 */ SD_csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7; SD_csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6; SD_csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5; SD_csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4; SD_csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2; SD_csd->ECC = (CSD_Tab[14] & 0x03); /*!< Byte 15 */ SD_csd->CSD_CRC = (CSD_Tab[15] & 0xFE) >> 1; SD_csd->Reserved4 = 1; /*!< Return the reponse */ return rvalue; }
/** * @brief Read the CSD card register. * Reading the contents of the CSD register in SPI mode is a simple * read-block transaction. * @param SD_csd: pointer on an SCD register structure * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_GetCSDRegister(SD_CSD* SD_csd) { uint32_t i = 0; SD_Error rvalue = SD_RESPONSE_FAILURE; uint8_t CSD_Tab[16]; uint32_t CardCapacity; static uint32_t times = 0; /*!< SD chip select low */ SD_CS_LOW(); /*!< Send CMD9 (CSD register) or CMD10(CSD register) */ SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF); /*!< Wait for response in the R1 format (0x00 is no errors) */ if (!SD_GetResponse(SD_RESPONSE_NO_ERROR)) { if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ)) { for (i = 0; i < 16; i++) { /*!< Store CSD register value on CSD_Tab */ CSD_Tab[i] = SD_ReadByte(); //if(0 == times) //{ // printf("\n\r CSD_Tab[%d] 0x%X", i, CSD_Tab[i]); //} } } /*!< Get CRC bytes (not really needed by us, but required by SD) */ SD_WriteByte(SD_DUMMY_BYTE); SD_WriteByte(SD_DUMMY_BYTE); /*!< Set response value to success */ rvalue = SD_RESPONSE_NO_ERROR; } else { return rvalue; } /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte: 8 Clock pulses of delay */ SD_WriteByte(SD_DUMMY_BYTE); /*!< Byte 0 */ SD_csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6; SD_csd->SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2; SD_csd->Reserved1 = CSD_Tab[0] & 0x03; if(0 == times) { if(SD_csd->CSDStruct == 1) { //printf("\n\r SDHC CSD Version 2.0 Acess AS SD High Capacity."); flag_SDHC = 1; } else { //printf("\n\r SDSC CSD Version 1.0 Acess AS SD Standard Capacity."); flag_SDHC = 0; } } /*!< Byte 1 */ SD_csd->TAAC = CSD_Tab[1]; /*!< Byte 2 */ SD_csd->NSAC = CSD_Tab[2]; /*!< Byte 3 */ SD_csd->MaxBusClkFrec = CSD_Tab[3]; /*!< Byte 4 */ SD_csd->CardComdClasses = CSD_Tab[4] << 4; /*!< Byte 5 */ SD_csd->CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4; SD_csd->RdBlockLen = CSD_Tab[5] & 0x0F; /*!< Byte 6 */ SD_csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7; SD_csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6; SD_csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5; SD_csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4; SD_csd->Reserved2 = 0; /*!< Reserved */ if(flag_SDHC == 0) { SD_csd->DeviceSize = (CSD_Tab[6] & 0x03) << 10; /*!< Byte 7 */ SD_csd->DeviceSize |= (CSD_Tab[7]) << 2; /*!< Byte 8 */ SD_csd->DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6; }
unsigned char SD_INIT(void){ unsigned int count =0; char byte; char i; /// cant get the card to communicate properly // SPI_INIT(40000); // slower speed still problems SPI_Toggle_MasterSS(0); // use master ss // enable CS on SD // SD_INIT_DDR(); SD_CS_LOW(); Timer_Delay_1ms(5); /// wait 1ms // Set DI and CS high and apply 74 clocks SD_CS_HIGH(); /* Init the card in SPI mode by sending at least 72 clocks Data and CS lines must be set to 1*/ for(i = 0; i < 10; i++) SPI_Master_Transmit_Recieve(0xFF); /// send FF pull the data line high SD_CS_LOW(); printf("Successfully Sent Dummy Clocks \r"); /* Init the CS output */ printf("Turning on Chip \r"); Timer_Delay_10ms(100); // DELAY /* Send CMD0 GO_IDLE_STATE - Resets the card puts it in idle state if CS is pulled low and CMD0 is sent the card is put in spi mode*/ SPI_Master_Transmit_Bytes(SD_CMD0, MMC_CMD_SIZE); if (SDRead_Response_R1(0x01)) // MMC responds with a R1 response Idle State bit (0x01) { printf("CMD0 timeout Exiting!\r"); return 1; } //SPI mode, the CRC feature is disabled and the CRC printf("Successfully sent CMD0- device is in idle mode\r"); /* Send some dummy clocks after GO_IDLE_STATE */ SD_CS_HIGH(); SPI_Recieve_Char(0xFF); SD_CS_LOW() printf("send CMD1\r"); /* Send CMD1 SEND_OP_COND until response is 0 */ i = 0xFF; while((SDRead_Response_R1(0xFF) != 0) && (i > 0)) { SPI_Master_Transmit_Bytes(SD_CMD1, MMC_CMD_SIZE); i--; } if (i == 0){ printf("CMD1 timeout\r"); return 2; } /* Send some dummy clocks after GO_IDLE_STATE */ SD_CS_HIGH(); SPI_Recieve_Char(0xFF); SD_CS_LOW() SPI_Master_Transmit_Bytes(SD_CMD16, MMC_CMD_SIZE); if ((SDRead_Response_R1(0xFF)) == 1)printf("CMD16 timeout\r"); else printf("CMD16 Sent 512 bytes \r"); SD_CS_HIGH(); SPI_Recieve_Char(0x00); printf("mmc_init end\r"); return 0; }