12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316 |
- /******************************************************************//**
- * @file drv_sdcard.c
- * @brief Memory card driver (SPI mode) of RT-Thread RTOS for using EFM32 USART module
- * COPYRIGHT (C) 2011, RT-Thread Development Team
- * @author onelife
- * @version 0.4 beta
- **********************************************************************
- * @section License
- * The license and distribution terms for this file may be found in the file LICENSE in this
- * distribution or at http://www.rt-thread.org/license/LICENSE
- **********************************************************************
- * @section Change Logs
- * Date Author Notes
- * 2011-05-13 onelife Initial creation for using EFM32 USART module
- *********************************************************************/
- /******************************************************************//**
- * @addtogroup efm32_dk
- * @{
- *********************************************************************/
- /* Includes -------------------------------------------------------------------*/
- #include "board.h"
- #include "drv_usart.h"
- #include "drv_sdcard.h"
- #if defined(EFM32_USING_SPISD)
- #include <dfs_fs.h>
- /* Private typedef -------------------------------------------------------------*/
- /* Private define --------------------------------------------------------------*/
- /* Private macro --------------------------------------------------------------*/
- #ifdef EFM32_SDCARD_DEBUG
- #define sdcard_debug(format,args...) rt_kprintf(format, ##args)
- #else
- #define sdcard_debug(format,args...)
- #endif
- /* Private constants -----------------------------------------------------------*/
- /* Private variables ------------------------------------------------------------*/
- static struct rt_device sd_device;
- static struct dfs_partition sdPart;
- static rt_device_t spi = RT_NULL;
- static rt_uint16_t sdType;
- static rt_bool_t sdAutoCs = true;
- static rt_timer_t sdTimer = RT_NULL;
- static rt_bool_t sdInTime = true;
- /* Private function prototypes ---------------------------------------------------*/
- /* Private functions ------------------------------------------------------------*/
- /******************************************************************//**
- * @brief
- * Memory device timeout interrupt handler
- *
- * @details
- *
- * @note
- *
- * @param[in] parameter
- * Parameter
- *********************************************************************/
- static void efm_spiSd_timer(void* parameter)
- {
- sdInTime = false;
- }
- /******************************************************************//**
- * @brief
- * Set/Clear chip select
- *
- * @details
- *
- * @note
- *
- * @param[in] enable
- * Chip select pin status
- *********************************************************************/
- static void efm_spiSd_cs(rt_uint8_t enable)
- {
- if (!sdAutoCs)
- {
- if (enable)
- {
- GPIO_PinOutClear(SD_CS_PORT, SD_CS_PIN);
- }
- else
- {
- GPIO_PinOutSet(SD_CS_PORT, SD_CS_PIN);
- }
- }
- }
- /******************************************************************//**
- * @brief
- * Set operation speed level
- *
- * @details
- *
- * @note
- *
- * @param[in] level
- * Set SD speed level
- *********************************************************************/
- static void efm_spiSd_speed(rt_uint8_t level)
- {
- RT_ASSERT(spi != RT_NULL);
- struct efm32_usart_device_t *usart;
- rt_uint32_t baudrate;
-
- usart = (struct efm32_usart_device_t *)(spi->user_data);
- if (level == SD_SPEED_HIGH)
- {
- baudrate = EFM32_SDCLK_HIGH;
- }
- else
- {
- baudrate = EFM32_SDCLK_LOW;
- }
- USART_BaudrateSyncSet(usart->usart_device, 0, baudrate);
- }
- /******************************************************************//**
- * @brief
- * Read raw data from memory device
- *
- * @details
- *
- * @note
- *
- * @param[in] buffer
- * Poniter to the buffer
- *
- * @param[in] size
- * Buffer size in byte
- *
- * @return
- * Number of read bytes
- *********************************************************************/
- static rt_size_t efm_spiSd_read(void *buffer, rt_size_t size)
- {
- RT_ASSERT(spi != RT_NULL);
- rt_uint8_t buf_read[5], ret;
- /* Build instruction buffer */
- buf_read[0] = 0x00;
- *(rt_uint8_t **)(&buf_read[1]) = buffer;
- /* Read data */
- efm_spiSd_cs(1);
- if ((ret = spi->read(spi, EFM32_NO_DATA, buf_read, size)) == 0)
- {
- sdcard_debug("SPISD: Read failed!\n");
- }
- efm_spiSd_cs(0);
- return ret;
- }
- /******************************************************************//**
- * @brief
- * Send command to memory device
- *
- * @details
- *
- * @note
- *
- * @param[in] cmd
- * Command index
- *
- * @param[in] arg
- * Argument
- *
- * @param[in] trail
- * Pointer to the buffer to store trailing data
- *
- * @return
- * Command response
- *********************************************************************/
- static rt_uint16_t efm_spiSd_cmd(
- rt_uint8_t cmd,
- rt_uint32_t arg,
- rt_uint8_t *trail)
- {
- RT_ASSERT(spi != RT_NULL);
- rt_uint8_t buf_ins[11];
- rt_uint8_t buf_res[32]; /* Expect (x+1+4) bytes for CRC, (x+1+19) for CSD/CID */
- rt_uint8_t len_trl, i, j;
- rt_uint16_t ret;
- rt_bool_t skip;
- ret = 0xffff;
- rt_memset(buf_res, 0xff, sizeof(buf_res));
- sdcard_debug("SPISD: Send command %d(%x)\n", cmd, arg);
- do
- {
- /* Build instruction buffer */
- buf_ins[0] = 6; /* Instruction length */
- buf_ins[1] = 0x40 | cmd; /* Command index */
- buf_ins[2] = (arg >> 24) & 0x000000ff; /* Argument: MSB first */
- buf_ins[3] = (arg >> 16) & 0x000000ff;
- buf_ins[4] = (arg >> 8) & 0x000000ff;
- buf_ins[5] = arg & 0x000000ff;
- if (cmd == CMD0)
- {
- buf_ins[6] = 0x95; /* Valid CRC for CMD0(0) */
- }
- else if (cmd == CMD8)
- {
- buf_ins[6] = 0x87; /* Valid CRC for CMD8(0x1AA) */
- }
- else if (cmd == CMD58)
- {
- buf_ins[6] = 0x01; /* Dummy CRC + Stop */
- }
- else
- {
- buf_ins[6] = 0x01; /* Dummy CRC + Stop */
- }
- *(rt_uint8_t **)(&buf_ins[7]) = buf_res; /* Pointer to RX buffer */
- /* Set trail length */
- if (cmd == CMD8)
- {
- len_trl = 4; /* R7 response */
- }
- else if (cmd == CMD9)
- {
- len_trl = SD_BLOCK_SIZE_CSD;
- }
- else if (cmd == CMD10)
- {
- len_trl = SD_BLOCK_SIZE_CID;
- }
- else if (cmd == CMD58)
- {
- len_trl = SD_BLOCK_SIZE_OCR; /* R3 response */
- }
- else
- {
- len_trl = 0;
- }
- /* Send command and get response */
- efm_spiSd_cs(1);
- if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
- {
- sdcard_debug("SPISD: Send command failed!\n");
- break;
- }
- efm_spiSd_cs(0);
- /* Skip a stuff byte when stop reading */
- if (cmd == CMD12)
- {
- skip = true;
- }
- else
- {
- skip = false;
- }
- /* Find valid response: The response is sent back within command response time
- (NCR), 0 to 8 bytes for SDC, 1 to 8 bytes for MMC */
- for (i = 0; i < sizeof(buf_res); i++)
- {
- if (buf_res[i] != 0xff)
- {
- if (skip)
- {
- skip = false;
- sdcard_debug("SPISD: Skip %x (at %d)\n", buf_res[i], i);
- continue;
- }
- if (cmd == ACMD13 & 0x7f)
- {
- ret = (rt_uint16_t)buf_res[i]; /* R2 response */
- }
- else
- {
- ret = (rt_uint8_t)buf_res[i];
- }
- break;
- }
- }
- sdcard_debug("SPISD: Response %x (at %d)\n", ret, i);
- i++;
- /* Copy the trailing data */
- if ((ret != 0xffff) && len_trl && trail)
- {
- if (cmd == CMD9 || cmd == CMD10)
- {
- /* Wait for data block */
- for (; i < sizeof(buf_res); i++)
- {
- if (buf_res[i] == 0xfe)
- {
- break;
- }
- }
- /* Check if valid */
- if (i >= sizeof(buf_res))
- {
- sdcard_debug("SPISD: Token is not found!\n");
- ret = 0xffff;
- break;
- }
- i++;
- }
- /* Copy the data */
- for (j = 0; j < len_trl; j++)
- {
- trail[j] = buf_res[i + j];
- }
- }
- } while(0);
- return ret;
- }
- /******************************************************************//**
- * @brief
- * Read a block of data from memory device. This function is used to handle the responses of
- * specified commands (e.g. ACMD13, CMD17 and CMD18)
- *
- * @details
- *
- * @note
- *
- * @param[in] buffer
- * Poniter to the buffer
- *
- * @param[in] size
- * Buffer size in byte
- *
- * @return
- * Error code
- *********************************************************************/
- static rt_err_t efm_spiSd_readBlock(void *buffer, rt_size_t size)
- {
- RT_ASSERT(spi != RT_NULL);
- rt_uint8_t buf_ins[5];
- rt_uint8_t buf_res[8]; /* Expect 2 bytes for CRC */
- rt_uint8_t i, len_copy;
- rt_bool_t start;
- start = false;
- do
- {
- /* Build instruction buffer */
- buf_ins[0] = 0; /* Instruction length */
- *(rt_uint8_t **)(&buf_ins[1]) = buf_res; /* Pointer to RX buffer */
- while(1)
- {
- /* Send read command */
- efm_spiSd_cs(1);
- if (spi->read(spi, EFM32_NO_DATA, buf_ins, \
- sizeof(buf_res)) == 0)
- {
- sdcard_debug("SPISD: Get read command response failed!\n");
- break;
- }
- efm_spiSd_cs(0);
- /* Wait for data */
- for (i = 0; i < sizeof(buf_res); i++)
- {
- if (buf_res[i] != 0xff)
- {
- start = true;
- break;
- }
- }
- if (start)
- {
- break;
- }
- };
- /* Ckeck if valid */
- if (!start || (buf_res[i] != 0xfe))
- {
- sdcard_debug("SPISD: Token is invalid! (%x)\n", buf_res[i]);
- break;
- }
- /* Copy data to buffer and read the rest */
- len_copy = sizeof(buf_res) - i - 1;
- rt_memcpy(buffer, &buf_res[i + 1], len_copy);
- sdcard_debug("SPISD: Read block start at %d, copy %d bytes\n", i, \
- len_copy);
-
- /* Build instruction buffer */
- buf_ins[0] = 0; /* Instruction length */
- *(rt_uint8_t **)(&buf_ins[1]) = (rt_uint8_t *)buffer + len_copy; /* Pointer to RX buffer */
- /* Send read command */
- efm_spiSd_cs(1);
- if (spi->read(spi, EFM32_NO_DATA, buf_ins, size - len_copy) == 0)
- {
- sdcard_debug("SPISD: Read data block failed!\n");
- break;
- }
- *(rt_uint8_t **)(&buf_ins[1]) = buf_res; /* Pointer to RX buffer */
- if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
- {
- sdcard_debug("SPISD: Read CRC failed!\n");
- break;
- }
- sdcard_debug("SPISD: Read CRC %x %x\n", buf_res[0], buf_res[1]);
- efm_spiSd_cs(0);
- return RT_EOK;
- } while(0);
- sdcard_debug("SPISD: Read block failed!\n");
- return -RT_ERROR;
- }
- /******************************************************************//**
- * @brief
- * Write a block of data to memory device. This function is used to send data and control
- * tokens for block write commands (e.g. CMD24 and CMD25)
- *
- * @details
- *
- * @note
- *
- * @param[in] buffer
- * Poniter to the buffer
- *
- * @param[in] token
- * Control token
- *
- * @return
- * Error code
- *********************************************************************/
- static rt_err_t efm_spiSd_writeBlock(void *buffer, rt_uint8_t token)
- {
- RT_ASSERT(spi != RT_NULL);
- rt_err_t ret;
- rt_uint8_t buf_ins[11];
- rt_uint8_t buf_res[8]; /* Expect a byte for data response */
- rt_uint8_t i;
- ret = RT_ERROR;
- do
- {
- /* Initialize timer */
- sdInTime = true;
- rt_timer_start(sdTimer);
- /* Wait for card ready */
- do
- {
- efm_spiSd_read(buf_res, sizeof(buf_res));
- } while (sdInTime && (buf_res[sizeof(buf_res) - 1] != 0xff));
- if (buf_res[sizeof(buf_res) - 1] != 0xff)
- {
- sdcard_debug("SPISD: Card is busy before writing! (%x)\n", \
- buf_res[sizeof(buf_res) - 1]);
- ret = -RT_EBUSY;
- break;
- }
- rt_timer_stop(sdTimer);
- /* Send token */
- buf_ins[0] = token;
- efm_spiSd_cs(1);
- if (spi->write(spi, EFM32_NO_DATA, buf_ins, 1) == 0)
- {
- sdcard_debug("SPISD: Write token failed!\n");
- break;
- }
- /* Send data */
- if (token != 0xfd)
- {
- if (spi->write(spi, EFM32_NO_DATA, buffer, SD_SECTOR_SIZE) == 0)
- {
- sdcard_debug("SPISD: Write data failed!\n");
- break;
- }
- /* Build instruction buffer */
- buf_ins[0] = 2; /* Instruction length */
- buf_ins[1] = 0xff; /* CRC (Dummy) */
- buf_ins[2] = 0xff;
- *(rt_uint8_t **)(&buf_ins[3]) = buf_res; /* Pointer to RX buffer */
- /* Send CRC and read a byte */
- if (spi->read(spi, EFM32_NO_DATA, buf_ins, sizeof(buf_res)) == 0)
- {
- sdcard_debug("SPISD: Write CRC failed!\n");
- break;
- }
- efm_spiSd_cs(0);
- /* Check if accepted */
- for (i = 0; i < sizeof(buf_res); i++)
- {
- if (buf_res[i] != 0xff)
- {
- buf_res[i] &= 0x1f;
- break;
- }
- }
- if (buf_res[i] != 0x05)
- {
- sdcard_debug("SPISD: Writing is not accepted! (%x at %d)\n", \
- buf_res[i], i);
- break;
- }
- }
- else
- {
- /* Initialize timer */
- sdInTime = true;
- rt_timer_start(sdTimer);
- /* Wait for card ready */
- do
- {
- efm_spiSd_read(buf_res, sizeof(buf_res));
- } while (sdInTime && (buf_res[sizeof(buf_res) - 1] != 0xff));
- if (buf_res[sizeof(buf_res) - 1] != 0xff)
- {
- sdcard_debug("SPISD: Card is busy after writing! (%x)\n", \
- buf_res[sizeof(buf_res) - 1] );
- ret = -RT_EBUSY;
- break;
- }
- rt_timer_stop(sdTimer);
- }
- return RT_EOK;
- } while(0);
- sdcard_debug("SPISD: Write block failed!\n");
- return ret;
- }
- /******************************************************************//**
- * @brief
- * Wrapper function of send command to memory device
- *
- * @details
- *
- * @note
- *
- * @param[in] cmd
- * Command index
- *
- * @param[in] arg
- * Argument
- *
- * @param[in] trail
- * Pointer to the buffer to store trailing data
- *
- * @return
- * Command response
- *********************************************************************/
- rt_uint16_t efm_spiSd_sendCmd(
- rt_uint8_t cmd,
- rt_uint32_t arg,
- rt_uint8_t *trail)
- {
- rt_uint16_t ret;
- /* ACMD<n> is the command sequense of CMD55-CMD<n> */
- if (cmd & 0x80)
- {
- cmd &= 0x7f;
- ret = efm_spiSd_cmd(CMD55, 0x00000000, EFM32_NO_POINTER);
- if (ret > 0x01)
- {
- return ret;
- }
- }
- return efm_spiSd_cmd(cmd, arg, trail);
- }
- /******************************************************************//**
- * @brief
- * Initialize memory card device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @return
- * Error code
- *********************************************************************/
- static rt_err_t rt_spiSd_init(rt_device_t dev)
- {
- RT_ASSERT(spi != RT_NULL);
- rt_uint8_t type, cmd, tril[4];
- rt_uint8_t *buf_res;
- type = 0;
- buf_res = RT_NULL;
- do
- {
- /* Setup timer */
- if ((sdTimer = rt_timer_create(
- "sdTimer",
- efm_spiSd_timer,
- RT_NULL,
- SD_WAIT_PERIOD,
- RT_TIMER_FLAG_ONE_SHOT)) == RT_NULL)
- {
- sdcard_debug("SPISD: Creat timer failed!\n");
- break;
- }
- /* Open SPI device */
- if (spi->open(spi, RT_DEVICE_OFLAG_RDWR) != RT_EOK)
- {
- break;
- }
- /* Switch to low speed */
- efm_spiSd_speed(SD_SPEED_LOW);
- /* 80 dummy clocks */
- efm_spiSd_read(RT_NULL, 80);
- /* Enter Idle state */
- if (efm_spiSd_sendCmd(CMD0, 0x00000000, EFM32_NO_POINTER) != 0x01)
- {
- break;
- }
- /* Check if SDv2 */
- if (efm_spiSd_sendCmd(CMD8, 0x000001AA, tril) == 0x01)
- {
- /* SDv2, Vdd: 2.7-3.6V */
- if (tril[2] == 0x01 && tril[3] == 0xAA)
- {
- /* Initialize timer */
- sdInTime = true;
- rt_timer_start(sdTimer);
- /* Wait for leaving idle state (ACMD41 with HCS bit) */
- while (efm_spiSd_sendCmd(ACMD41, 0x40000000, EFM32_NO_POINTER) \
- && sdInTime);
- /* Check CCS bit (bit 30) in the OCR */
- if (sdInTime && efm_spiSd_sendCmd(CMD58, 0x00000000, tril) \
- == 0x00)
- {
- type = (tril[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;
- }
- }
- }
- else
- {
- if (efm_spiSd_sendCmd(ACMD41, 0x00000000, EFM32_NO_POINTER) <= 0x01)
- {
- /* SDv1 */
- type = CT_SD1;
- cmd = ACMD41;
- }
- else
- {
- /* MMCv3 */
- type = CT_MMC;
- cmd = CMD1;
- }
- /* Initialize timer */
- sdInTime = true;
- rt_timer_start(sdTimer);
- /* Wait for leaving idle state */
- while (efm_spiSd_sendCmd(cmd, 0x00000000, EFM32_NO_POINTER) && \
- sdInTime);
- /* Set read/write block length to SD_BLOCK_SIZE */
- if (!sdInTime || \
- (efm_spiSd_sendCmd(CMD16, SD_SECTOR_SIZE, EFM32_NO_POINTER) \
- != 0x00))
- {
- type = 0;
- break;
- }
- }
- rt_timer_stop(sdTimer);
- /* Check type */
- sdType = type;
- if (sdType)
- {
- /* Initialization succeded */
- efm_spiSd_speed(SD_SPEED_HIGH);
- }
- else
- {
- break;
- }
- /* Allocate buffer */
- if ((buf_res = rt_malloc(SD_SECTOR_SIZE)) == RT_NULL)
- {
- sdcard_debug("SPISD: No memory for sector buffer\n");
- break;
- }
- /* Read the first sector for partition table */
- if (dev->read(dev, 0, buf_res, 1) != 1)
- {
- sdcard_debug("SPISD: Read first sector failed!\n");
- break;
- }
- /* Fetch the partition table */
- if (dfs_filesystem_get_partition(&sdPart, buf_res, 0) != RT_EOK)
- {
- sdPart.offset = 0;
- sdPart.size = 0;
- sdcard_debug("SPISD: No partition table\n");
- }
- /* Release buffer */
- rt_free(buf_res);
- sdcard_debug("SPISD: Init OK, card type %x\n", sdType);
- return RT_EOK;
- } while (0);
- /* Release buffer */
- if (buf_res)
- {
- rt_free(buf_res);
- }
- efm_spiSd_deinit();
- rt_kprintf("SPISD: Init failed!\n");
- return -RT_ERROR;
- }
- /******************************************************************//**
- * @brief
- * Open memory card device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] oflag
- * Device open flag
- *
- * @return
- * Error code
- *********************************************************************/
- static rt_err_t rt_spiSd_open(rt_device_t dev, rt_uint16_t oflag)
- {
- sdcard_debug("SPISD: Open, flag %x\n", sd_device.flag);
- return RT_EOK;
- }
- /******************************************************************//**
- * @brief
- * Close memory card device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @return
- * Error code
- *********************************************************************/
- static rt_err_t rt_spiSd_close(rt_device_t dev)
- {
- sdcard_debug("SPISD: Close, flag %x\n", sd_device.flag);
- return RT_EOK;
- }
- /******************************************************************//**
- * @brief
- * Read from memory card device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] sector
- * Start sector number (LBA)
- *
- * @param[in] buffer
- * Pointer to the buffer
- *
- * @param[in] count
- * Sector count (1..255)
- *
- * @return
- * Number of read sectors
- *********************************************************************/
- static rt_size_t rt_spiSd_read(
- rt_device_t dev,
- rt_off_t sector,
- void *buffer,
- rt_size_t count)
- {
- rt_uint8_t buf_ins[11], buf_res[12];
- rt_uint8_t *ptr;
- rt_uint8_t cmd, i;
- rt_size_t cnt;
- ptr = (rt_uint8_t *)buffer;
- cnt = count;
- sdcard_debug("SPISD: ****** Read Data ******\n");
- if (!(sdType & CT_BLOCK))
- {
- /* Convert to byte address if needed */
- sector *= SD_SECTOR_SIZE;
- }
- do
- {
- if (cnt == 1)
- {
- /* Single block read */
- cmd = CMD17;
- sdcard_debug("SPISD: Read single block\n");
- }
- else
- {
- /* Multiple block read */
- cmd = CMD18;
- sdcard_debug("SPISD: Read multiple blocks\n");
- }
- if (efm_spiSd_sendCmd(cmd, sector, EFM32_NO_POINTER))
- {
- sdcard_debug("SPISD: Read command error!\n");
- break;
- }
-
- /* Read data */
- do
- {
- if (efm_spiSd_readBlock(ptr, SD_SECTOR_SIZE))
- {
- break;
- }
- ptr += SD_SECTOR_SIZE;
- } while(--cnt);
- /* Stop transmission */
- if (cmd == CMD18)
- {
- if (efm_spiSd_sendCmd(CMD12, 0x00000000, EFM32_NO_POINTER))
- {
- break;
- }
- }
- return (count);
- } while(0);
- return (0);
- }
- /******************************************************************//**
- * @brief
- * Write to memory card device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] sector
- * Start sector number (LBA)
- *
- * @param[in] buffer
- * Pointer to the buffer
- *
- * @param[in] count
- * Sector count (1..255)
- *
- * @return
- * Number of written sectors
- *********************************************************************/
- static rt_size_t rt_spiSd_write (
- rt_device_t dev,
- rt_off_t sector,
- const void *buffer,
- rt_size_t count)
- {
- rt_uint8_t buf_ins[11], buf_res[12];
- rt_uint8_t *ptr;
- rt_uint8_t cmd, token, i;
- rt_size_t cnt;
- ptr = (rt_uint8_t *)buffer;
- cnt = count;
- sdcard_debug("SPISD: ****** Write Data ******\n");
- if (!(sdType & CT_BLOCK))
- {
- /* Convert to byte address if needed */
- sector *= SD_SECTOR_SIZE;
- }
- do
- {
- if (cnt == 1)
- {
- /* Single block write */
- cmd = CMD24;
- token = 0xfe;
- sdcard_debug("SPISD: Write single block\n");
- }
- else
- {
- /* Multiple block write */
- cmd = CMD25;
- token = 0xfc;
- sdcard_debug("SPISD: Write multiple blocks\n");
- if (sdType & CT_SDC)
- {
- if (efm_spiSd_sendCmd(ACMD23, count, EFM32_NO_POINTER))
- {
- break;
- }
- }
- }
- if (efm_spiSd_sendCmd(cmd, sector, EFM32_NO_POINTER))
- {
- sdcard_debug("SPISD: Write command error!\n");
- break;
- }
- /* Write data */
- do
- {
- if (efm_spiSd_writeBlock(ptr, token))
- {
- break;
- }
- ptr += SD_SECTOR_SIZE;
- } while(--cnt);
- /* Stop transmission token */
- if (efm_spiSd_writeBlock(EFM32_NO_POINTER, 0xfd))
- {
- break;
- }
- return (count);
- } while(0);
- return (0);
- }
- /******************************************************************//**
- * @brief
- * Configure memory card device
- *
- * @details
- *
- * @note
- *
- * @param[in] dev
- * Pointer to device descriptor
- *
- * @param[in] ctrl
- * Memory card control command
- *
- * @param[in] buffer
- * Pointer to the buffer of in/out data
- *
- * @return
- * Error code
- *********************************************************************/
- static rt_err_t rt_spiSd_control (
- rt_device_t dev,
- rt_uint8_t ctrl,
- void *buffer)
- {
- rt_err_t ret;
- rt_uint32_t c_size;
- rt_uint8_t n;
- rt_uint8_t *buf_res;
-
- ret = -RT_ERROR;
- buf_res = RT_NULL;
- switch (ctrl)
- {
- case RT_DEVICE_CTRL_SD_SYNC:
- /* Flush dirty buffer if present */
- efm_spiSd_cs(1);
- efm_spiSd_cs(0);
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_SCOUNT:
- {
- /* Allocate buffer */
- if ((buf_res = rt_malloc(SD_BLOCK_SIZE_CSD)) == RT_NULL)
- {
- sdcard_debug("SPISD: No memory for RX buffer\n");
- break;
- }
- /* Get number of sectors on the disk (32 bits) */
- if (efm_spiSd_sendCmd(CMD9, 0x00000000, buf_res))
- {
- sdcard_debug("SPISD: Get CSD failed!\n");
- break;
- }
- if ((buf_res[0] >> 6) == 0x01)
- {
- /* SDv2 */
- /* C_SIZE: Bit 48~69 */
- c_size = ((rt_uint32_t)(buf_res[7] & 0x3f) << 16) + \
- ((rt_uint32_t)buf_res[8] << 8) + buf_res[9] + 1;
- /* Result = Capacity / Sector Size */
- *(rt_uint32_t *)buffer = (rt_uint32_t)c_size << \
- (19 - SD_SECTOR_SIZE_SHIFT);
- }
- else
- {
- /* SDv1 or MMC */
- /* C_SIZE: Bit 62~73 */
- c_size = ((rt_uint32_t)(buf_res[6] & 0x03) << 10) + \
- ((rt_uint16_t)buf_res[7] << 2) + (buf_res[8] >> 6) + 1;
- /* READ_BL_LEN: Bit 80~83, C_SIZE_MULT: Bit 47~49 */
- n = ((buf_res[9] & 0x03) << 1) + ((buf_res[10] & 0x80) >> 7) + \
- 2 + (buf_res[5] & 0x0f);
- /* Result = Capacity / Sector Size */
- *(rt_uint32_t *)buffer = (rt_uint32_t)c_size << \
- (n - SD_SECTOR_SIZE_SHIFT);
- }
- ret = RT_EOK;
- break;
- }
- case RT_DEVICE_CTRL_SD_GET_SSIZE:
- /* Get sectors on the disk (16 bits) */
- *(rt_uint16_t *)buffer = SD_SECTOR_SIZE;
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_BSIZE:
- /* Get erase block size in unit of sectors (32 bits) */
- if (sdType & CT_SD2)
- {
- /* Allocate buffer */
- if ((buf_res = rt_malloc(SD_BLOCK_SIZE_SDSTAT)) == RT_NULL)
- {
- sdcard_debug("SPISD: No memory for RX buffer\n");
- break;
- }
- /* SDv2 */
- if (efm_spiSd_sendCmd(ACMD13, 0x00000000, EFM32_NO_POINTER))
- {
- sdcard_debug("SPISD: Get SD status failed!\n");
- break;
- }
- if (efm_spiSd_readBlock(buf_res, SD_BLOCK_SIZE_SDSTAT))
- {
- sdcard_debug("SPISD: Read SD status failed!\n");
- break;
- }
- /* AU_SIZE: Bit 428~431 */
- *(rt_uint32_t *)buffer = 16UL << ((buf_res[10] >> 4) + 9 - \
- SD_SECTOR_SIZE_SHIFT);
- }
- else
- {
- /* Allocate buffer */
- if ((buf_res = rt_malloc(SD_BLOCK_SIZE_CSD)) == RT_NULL)
- {
- sdcard_debug("SPISD: No memory for RX buffer\n");
- break;
- }
- /* SDv1 or MMC */
- if (efm_spiSd_sendCmd(CMD9, 0x00000000, buf_res))
- {
- sdcard_debug("SPISD: Get CSD failed!\n");
- break;
- }
- if (sdType & CT_SD1)
- {
- /* SECTOR_SIZE: Bit 39~45, WRITE_BL_LEN: Bit 22~25 (9, 10 or 11) */
- *(rt_uint32_t *)buffer = (((buf_res[10] & 0x3f) << 1) + \
- ((rt_uint32_t)(buf_res[11] & 0x80) >> 7) + 1) << \
- (8 + (buf_res[13] >> 6) - SD_SECTOR_SIZE_SHIFT);
- }
- else
- {
- /* ERASE_GRP_SIZE: Bit 42~46, ERASE_GRP_MULT: Bit 37~41 */
- *(rt_uint32_t *)buffer = \
- ((rt_uint16_t)((buf_res[10] & 0x7c) >> 2) + 1) * \
- (((buf_res[10] & 0x03) << 3) + \
- ((buf_res[11] & 0xe0) >> 5) + 1);
- }
- }
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_TYPE:
- /* Get card type flags (1 byte) */
- *(rt_uint8_t *)buffer = sdType;
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_CSD:
- /* Receive CSD as a data block (16 bytes) */
- if (efm_spiSd_sendCmd(CMD9, 0x00000000, buffer))
- {
- sdcard_debug("SPISD: Get CSD failed!\n");
- break;
- }
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_CID:
- /* Receive CID as a data block (16 bytes) */
- if (efm_spiSd_sendCmd(CMD10, 0x00000000, buffer))
- {
- sdcard_debug("SPISD: Get CID failed!\n");
- break;
- }
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_OCR:
- /* Receive OCR as an R3 resp (4 bytes) */
- if (efm_spiSd_sendCmd(CMD58, 0x00000000, buffer))
- {
- sdcard_debug("SPISD: Get OCR failed!\n");
- break;
- }
- ret = RT_EOK;
- break;
- case RT_DEVICE_CTRL_SD_GET_SDSTAT:
- /* Receive SD statsu as a data block (64 bytes) */
- if (efm_spiSd_sendCmd(ACMD13, 0x00000000, buffer))
- {
- sdcard_debug("SPISD: Get SD status failed!\n");
- break;
- }
- if (efm_spiSd_readBlock(buffer, SD_BLOCK_SIZE_SDSTAT))
- {
- sdcard_debug("SPISD: Read SD status failed!\n");
- break;
- }
- ret = RT_EOK;
- break;
- default:
- break;
- }
- if (buf_res)
- {
- rt_free(buf_res);
- }
- return ret;
- }
- /******************************************************************//**
- * @brief
- * Initialize all memory card related hardware and register the device to kernel
- *
- * @details
- *
- * @note
- *********************************************************************/
- void efm_spiSd_init(void)
- {
- struct efm32_usart_device_t *usart;
- #if defined(EFM32_G290_DK)
- /* Enable SPI access to MicroSD card */
- DVK_writeRegister(BC_SPI_CFG, 1);
- #endif
- do
- {
- /* Find SPI device */
- spi = rt_device_find(SPISD_USING_DEVICE_NAME);
- if (spi == RT_NULL)
- {
- sdcard_debug("SPISD: Can't find device %s!\n",
- SPISD_USING_DEVICE_NAME);
- break;
- }
- sdcard_debug("SPISD: Find device %s\n", SPISD_USING_DEVICE_NAME);
- /* Config chip slect pin */
- usart = (struct efm32_usart_device_t *)(spi->user_data);
- if (!(usart->state & USART_STATE_AUTOCS))
- {
- GPIO_PinModeSet(SD_CS_PORT, SD_CS_PIN, gpioModePushPull, 1);
- sdAutoCs = false;
- }
- /* Register SPI SD device */
- sd_device.init = rt_spiSd_init;
- sd_device.open = rt_spiSd_open;
- sd_device.close = rt_spiSd_close;
- sd_device.read = rt_spiSd_read;
- sd_device.write = rt_spiSd_write;
- sd_device.control = rt_spiSd_control;
- sd_device.user_data = RT_NULL;
- rt_device_register(
- &sd_device,
- SPISD_DEVICE_NAME,
- RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
- sdcard_debug("SPISD: HW init OK, card type %x\n", sdType);
- return;
- } while (0);
- /* Release buffer */
- rt_kprintf("SPISD: HW init failed!\n");
- }
- /******************************************************************//**
- * @brief
- * De-initialize memory card device
- *
- * @details
- *
- * @note
- *********************************************************************/
- void efm_spiSd_deinit(void)
- {
- /* Close SPI device */
- if (spi != RT_NULL)
- {
- spi->close(spi);
- spi = RT_NULL;
- sdcard_debug("SPISD: Close device %s\n", SPISD_USING_DEVICE_NAME);
- }
- /* Delete timer */
- if (sdTimer != RT_NULL)
- {
- rt_timer_delete(sdTimer);
- sdTimer = RT_NULL;
- sdcard_debug("SPISD: Delete timer\n");
- }
- sdcard_debug("SPISD: Deinit OK\n");
- }
- /*********************************************************************
- * Export to FINSH
- *********************************************************************/
- #ifdef RT_USING_FINSH
- #include <finsh.h>
- void list_sdcard(void)
- {
- rt_uint8_t buf_res[16];
- rt_uint32_t capacity, temp32;
- rt_uint16_t temp16;
- rt_kprintf(" SD Card on %s\n", SPISD_USING_DEVICE_NAME);
- rt_kprintf(" ------------------------------\n");
- sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_CID, buf_res);
- rt_kprintf(" Manufacturer ID:\t%x\n", buf_res[0]);
- rt_kprintf(" OEM/Application ID:\t%x%x\n", buf_res[1], buf_res[2]);
- rt_kprintf(" Product revision:\t%x\n", buf_res[8]);
- buf_res[8] = 0;
- rt_kprintf(" Product name:\t\t%s\n", &buf_res[3]);
- rt_kprintf(" Serial number:\t\t%x%x%x%x\n", \
- buf_res[9], buf_res[10], buf_res[11], buf_res[12]);
- rt_kprintf(" Manufacturing date:\t%d.%d\n", \
- 2000 + ((buf_res[13] & 0x0F) << 4) + ((buf_res[14] & 0xF0) >> 4), \
- buf_res[14] & 0x0F);
- rt_kprintf(" Card type:\t\t");
- sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_TYPE, buf_res);
- if (buf_res[0] == CT_MMC)
- {
- rt_kprintf("%s\n", "MMC");
- }
- else if (buf_res[0] == CT_SDC)
- {
- rt_kprintf("%s\n", "SDXC");
- }
- else if (buf_res[0] == CT_SD1)
- {
- rt_kprintf("%s\n", "SDSC");
- }
- else if (buf_res[0] == CT_SD2)
- {
- rt_kprintf("%s\n", "SDHC");
- }
- sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SSIZE, &temp16);
- sd_device.control(&sd_device, RT_DEVICE_CTRL_SD_GET_SCOUNT, &temp32);
- capacity = ((temp32 & 0x0000FFFF) * temp16) >> 16;
- capacity += ((temp32 >> 16) * temp16);
- capacity >>= 4;
- rt_kprintf(" Card capacity:\t\t%dMB\n", capacity);
- }
- FINSH_FUNCTION_EXPORT(list_sdcard, list the SD card.)
- #endif
- #endif /* defined(EFM32_USING_SPISD) */
- /******************************************************************//**
- * @}
- *********************************************************************/
|