diff options
Diffstat (limited to 'thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common')
8 files changed, 5922 insertions, 0 deletions
diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_i2c_tsensor.c b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_i2c_tsensor.c new file mode 100644 index 0000000..2db762e --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_i2c_tsensor.c @@ -0,0 +1,977 @@ +/** + ****************************************************************************** + * @file stm32_eval_i2c_tsensor.c + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file provides a set of functions needed to manage the I2C LM75 + * temperature sensor mounted on STM32xx-EVAL board (refer to stm32_eval.h + * to know about the boards supporting this sensor). + * It implements a high level communication layer for read and write + * from/to this sensor. The needed STM32 hardware resources (I2C and + * GPIO) are defined in stm32xx_eval.h file, and the initialization is + * performed in LM75_LowLevel_Init() function declared in stm32xx_eval.c + * file. + * + * Note: + * ----- + * This driver uses the DMA method to send and receive data on I2C bus, + * which allows higher efficiency and reliability of the communication. + * + * You can easily tailor this driver to any other development board, + * by just adapting the defines for hardware resources and + * LM75_LowLevel_Init() function. + * + * +-----------------------------------------------------------------+ + * | Pin assignment | + * +---------------------------------------+-----------+-------------+ + * | STM32 I2C Pins | STLM75 | Pin | + * +---------------------------------------+-----------+-------------+ + * | LM75_I2C_SDA_PIN/ SDA | SDA | 1 | + * | LM75_I2C_SCL_PIN/ SCL | SCL | 2 | + * | LM75_I2C_SMBUSALERT_PIN/ SMBUS ALERT | OS/INT | 3 | + * | . | GND | 4 (0V) | + * | . | GND | 5 (0V) | + * | . | GND | 6 (0V) | + * | . | GND | 7 (0V) | + * | . | VDD | 8 (3.3V)| + * +---------------------------------------+-----------+-------------+ + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval_i2c_tsensor.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_I2C_TSENSOR + * @brief This file includes the LM75 Temperature Sensor driver of + * STM32-EVAL boards. + * @{ + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Types + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Defines + * @{ + */ +#define LM75_SD_SET 0x01 /*!< Set SD bit in the configuration register */ +#define LM75_SD_RESET 0xFE /*!< Reset SD bit in the configuration register */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Variables + * @{ + */ + +__IO uint32_t LM75_Timeout = LM75_LONG_TIMEOUT; +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Function_Prototypes + * @{ + */ +static void LM75_DMA_Config(LM75_DMADirection_TypeDef Direction, uint8_t* buffer, uint8_t NumData); + +/** + * @} + */ + + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Private_Functions + * @{ + */ + +/** + * @brief DeInitializes the LM75_I2C. + * @param None + * @retval None + */ +void LM75_DeInit(void) +{ + LM75_LowLevel_DeInit(); +} + +/** + * @brief Initializes the LM75_I2C. + * @param None + * @retval None + */ +void LM75_Init(void) +{ + I2C_InitTypeDef I2C_InitStructure; + + LM75_LowLevel_Init(); + + I2C_DeInit(LM75_I2C); + + /*!< LM75_I2C Init */ + I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost; + I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; + I2C_InitStructure.I2C_OwnAddress1 = 0x00; + I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; + I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; + I2C_InitStructure.I2C_ClockSpeed = LM75_I2C_SPEED; + I2C_Init(LM75_I2C, &I2C_InitStructure); + + /*!< Enable SMBus Alert interrupt */ + I2C_ITConfig(LM75_I2C, I2C_IT_ERR, ENABLE); + + /*!< LM75_I2C Init */ + I2C_Cmd(LM75_I2C, ENABLE); +} + + +/** + * @brief Configure the DMA Peripheral used to handle communication via I2C. + * @param None + * @retval None + */ + +static void LM75_DMA_Config(LM75_DMADirection_TypeDef Direction, uint8_t* buffer, uint8_t NumData) +{ + DMA_InitTypeDef DMA_InitStructure; + + RCC_AHBPeriphClockCmd(LM75_DMA_CLK, ENABLE); + + /* Initialize the DMA_PeripheralBaseAddr member */ + DMA_InitStructure.DMA_PeripheralBaseAddr = LM75_I2C_DR; + /* Initialize the DMA_MemoryBaseAddr member */ + DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)buffer; + /* Initialize the DMA_PeripheralInc member */ + DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; + /* Initialize the DMA_MemoryInc member */ + DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; + /* Initialize the DMA_PeripheralDataSize member */ + DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; + /* Initialize the DMA_MemoryDataSize member */ + DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; + /* Initialize the DMA_Mode member */ + DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; + /* Initialize the DMA_Priority member */ + DMA_InitStructure.DMA_Priority = DMA_Priority_VeryHigh; + /* Initialize the DMA_M2M member */ + DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; + + /* If using DMA for Reception */ + if (Direction == LM75_DMA_RX) + { + /* Initialize the DMA_DIR member */ + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; + + /* Initialize the DMA_BufferSize member */ + DMA_InitStructure.DMA_BufferSize = NumData; + + DMA_DeInit(LM75_DMA_RX_CHANNEL); + + DMA_Init(LM75_DMA_RX_CHANNEL, &DMA_InitStructure); + } + /* If using DMA for Transmission */ + else if (Direction == LM75_DMA_TX) + { + /* Initialize the DMA_DIR member */ + DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; + + /* Initialize the DMA_BufferSize member */ + DMA_InitStructure.DMA_BufferSize = NumData; + + DMA_DeInit(LM75_DMA_TX_CHANNEL); + + DMA_Init(LM75_DMA_TX_CHANNEL, &DMA_InitStructure); + } +} + + +/** + * @brief Checks the LM75 status. + * @param None + * @retval ErrorStatus: LM75 Status (ERROR or SUCCESS). + */ +ErrorStatus LM75_GetStatus(void) +{ + uint32_t I2C_TimeOut = I2C_TIMEOUT; + + /*!< Clear the LM75_I2C AF flag */ + I2C_ClearFlag(LM75_I2C, I2C_FLAG_AF); + + /*!< Enable LM75_I2C acknowledgement if it is already disabled by other function */ + I2C_AcknowledgeConfig(LM75_I2C, ENABLE); + + /*---------------------------- Transmission Phase ---------------------------*/ + + /*!< Send LM75_I2C START condition */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /*!< Test on LM75_I2C EV5 and clear it */ + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) && I2C_TimeOut) /*!< EV5 */ + { + I2C_TimeOut--; + } + if (I2C_TimeOut == 0) + { + return ERROR; + } + + I2C_TimeOut = I2C_TIMEOUT; + + /*!< Send STLM75 slave address for write */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + while ((!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) && I2C_TimeOut)/* EV6 */ + { + I2C_TimeOut--; + } + + if ((I2C_GetFlagStatus(LM75_I2C, I2C_FLAG_AF) != 0x00) || (I2C_TimeOut == 0)) + { + return ERROR; + } + else + { + return SUCCESS; + } +} +/** + * @brief Read the specified register from the LM75. + * @param RegName: specifies the LM75 register to be read. + * This member can be one of the following values: + * - LM75_REG_TEMP: temperature register + * - LM75_REG_TOS: Over-limit temperature register + * - LM75_REG_THYS: Hysteresis temperature register + * @retval LM75 register value. + */ +uint16_t LM75_ReadReg(uint8_t RegName) +{ + uint8_t LM75_BufferRX[2] ={0,0}; + uint16_t tmp = 0; + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2); + + /* Enable DMA NACK automatic generation */ + I2C_DMALastTransferCmd(LM75_I2C, ENABLE); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send device address for write */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send the device's internal address to write to */ + I2C_SendData(LM75_I2C, RegName); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send START condition a second time */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send LM75 address for read */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA RX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_RX_TCFLAG); + + /*!< Store LM75_I2C received data */ + tmp = (uint16_t)(LM75_BufferRX[0] << 8); + tmp |= LM75_BufferRX[1]; + + /* return a Reg value */ + return (uint16_t)tmp; +} + +/** + * @brief Write to the specified register of the LM75. + * @param RegName: specifies the LM75 register to be written. + * This member can be one of the following values: + * - LM75_REG_TOS: Over-limit temperature register + * - LM75_REG_THYS: Hysteresis temperature register + * @param RegValue: value to be written to LM75 register. + * @retval None + */ +uint8_t LM75_WriteReg(uint8_t RegName, uint16_t RegValue) +{ + uint8_t LM75_BufferTX[2] ={0,0}; + LM75_BufferTX[0] = (uint8_t)(RegValue >> 8); + LM75_BufferTX[1] = (uint8_t)(RegValue); + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_TX, (uint8_t*)LM75_BufferTX, 2); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB) == RESET) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Transmit the slave address and enable writing operation */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Transmit the first address for r/w operations */ + I2C_SendData(LM75_I2C, RegName); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA TX Channel */ + DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Wait until BTF Flag is set before generating STOP */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA TX Channel */ + DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA TX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_TX_TCFLAG); + + return LM75_OK; +} + +/** + * @brief Read Temperature register of LM75: double temperature value. + * @param None + * @retval LM75 measured temperature value. + */ +uint16_t LM75_ReadTemp(void) +{ + uint8_t LM75_BufferRX[2] ={0,0}; + uint16_t tmp = 0; + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2); + + /* Enable DMA NACK automatic generation */ + I2C_DMALastTransferCmd(LM75_I2C, ENABLE); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send device address for write */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send the device's internal address to write to */ + I2C_SendData(LM75_I2C, LM75_REG_TEMP); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send START condition a second time */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send LM75 address for read */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA RX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_RX_TCFLAG); + + /*!< Store LM75_I2C received data */ + tmp = (uint16_t)(LM75_BufferRX[0] << 8); + tmp |= LM75_BufferRX[1]; + + /*!< Return Temperature value */ + return (uint16_t)(tmp >> 7); +} + +/** + * @brief Read the configuration register from the LM75. + * @param None + * @retval LM75 configuration register value. + */ +uint8_t LM75_ReadConfReg(void) +{ + uint8_t LM75_BufferRX[2] ={0,0}; + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2); + + /* Enable DMA NACK automatic generation */ + I2C_DMALastTransferCmd(LM75_I2C, ENABLE); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send device address for write */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send the device's internal address to write to */ + I2C_SendData(LM75_I2C, LM75_REG_CONF); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send START condition a second time */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send LM75 address for read */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA RX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_RX_TCFLAG); + + /*!< Return Temperature value */ + return (uint8_t)LM75_BufferRX[0]; +} + +/** + * @brief Write to the configuration register of the LM75. + * @param RegValue: sepecifies the value to be written to LM75 configuration + * register. + * @retval None + */ +uint8_t LM75_WriteConfReg(uint8_t RegValue) +{ + uint8_t LM75_BufferTX = 0; + LM75_BufferTX = (uint8_t)(RegValue); + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_TX, (uint8_t*)(&LM75_BufferTX), 1); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB) == RESET) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Transmit the slave address and enable writing operation */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Transmit the first address for r/w operations */ + I2C_SendData(LM75_I2C, LM75_REG_CONF); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA TX Channel */ + DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Wait until BTF Flag is set before generating STOP */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA TX Channel */ + DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA TX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_TX_TCFLAG); + + return LM75_OK; + +} + +/** + * @brief Enables or disables the LM75. + * @param NewState: specifies the LM75 new status. This parameter can be ENABLE + * or DISABLE. + * @retval None + */ +uint8_t LM75_ShutDown(FunctionalState NewState) +{ + uint8_t LM75_BufferRX[2] ={0,0}; + uint8_t LM75_BufferTX = 0; + __IO uint8_t RegValue = 0; + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_RX, (uint8_t*)LM75_BufferRX, 2); + + /* Enable DMA NACK automatic generation */ + I2C_DMALastTransferCmd(LM75_I2C, ENABLE); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send device address for write */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send the device's internal address to write to */ + I2C_SendData(LM75_I2C, LM75_REG_CONF); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send START condition a second time */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send LM75 address for read */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Receiver); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_RX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA RX Channel */ + DMA_Cmd(LM75_DMA_RX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA RX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_RX_TCFLAG); + + /*!< Get received data */ + RegValue = (uint8_t)LM75_BufferRX[0]; + + /*---------------------------- Transmission Phase ---------------------------*/ + + /*!< Enable or disable SD bit */ + if (NewState != DISABLE) + { + /*!< Enable LM75 */ + LM75_BufferTX = RegValue & LM75_SD_RESET; + } + else + { + /*!< Disable LM75 */ + LM75_BufferTX = RegValue | LM75_SD_SET; + } + + /* Test on BUSY Flag */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BUSY)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Configure DMA Peripheral */ + LM75_DMA_Config(LM75_DMA_TX, (uint8_t*)(&LM75_BufferTX), 1); + + /* Enable the I2C peripheral */ + I2C_GenerateSTART(LM75_I2C, ENABLE); + + /* Test on SB Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_SB) == RESET) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Transmit the slave address and enable writing operation */ + I2C_Send7bitAddress(LM75_I2C, LM75_ADDR, I2C_Direction_Transmitter); + + /* Test on ADDR Flag */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while (!I2C_CheckEvent(LM75_I2C, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Transmit the first address for r/w operations */ + I2C_SendData(LM75_I2C, LM75_REG_CONF); + + /* Test on TXE FLag (data sent) */ + LM75_Timeout = LM75_FLAG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Enable I2C DMA request */ + I2C_DMACmd(LM75_I2C,ENABLE); + + /* Enable DMA TX Channel */ + DMA_Cmd(LM75_DMA_TX_CHANNEL, ENABLE); + + /* Wait until DMA Transfer Complete */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while (!DMA_GetFlagStatus(LM75_DMA_TX_TCFLAG)) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Wait until BTF Flag is set before generating STOP */ + LM75_Timeout = LM75_LONG_TIMEOUT; + while ((!I2C_GetFlagStatus(LM75_I2C,I2C_FLAG_BTF))) + { + if((LM75_Timeout--) == 0) return LM75_TIMEOUT_UserCallback(); + } + + /* Send STOP Condition */ + I2C_GenerateSTOP(LM75_I2C, ENABLE); + + /* Disable DMA TX Channel */ + DMA_Cmd(LM75_DMA_TX_CHANNEL, DISABLE); + + /* Disable I2C DMA request */ + I2C_DMACmd(LM75_I2C,DISABLE); + + /* Clear DMA TX Transfer Complete Flag */ + DMA_ClearFlag(LM75_DMA_TX_TCFLAG); + + return LM75_OK; +} + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_i2c_tsensor.h b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_i2c_tsensor.h new file mode 100644 index 0000000..67b4689 --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_i2c_tsensor.h @@ -0,0 +1,173 @@ +/** + ****************************************************************************** + * @file stm32_eval_i2c_tsensor.h + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file contains all the functions prototypes for the + * stm32_eval_i2c_tsensor firmware driver. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32_EVAL_I2C_TSENSOR_H +#define __STM32_EVAL_I2C_TSENSOR_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_I2C_TSENSOR + * @{ + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Exported_Types + * @{ + */ + + /** + * @brief IOE DMA Direction + */ +typedef enum +{ + LM75_DMA_TX = 0, + LM75_DMA_RX = 1 +}LM75_DMADirection_TypeDef; + +/** + * @brief TSENSOR Status + */ +typedef enum +{ + LM75_OK = 0, + LM75_FAIL +}LM75_Status_TypDef; + +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Exported_Constants + * @{ + */ + +/* Uncomment the following line to use Timeout_User_Callback LM75_TimeoutUserCallback(). + If This Callback is enabled, it should be implemented by user in main function . + LM75_TimeoutUserCallback() function is called whenever a timeout condition + occure during communication (waiting on an event that doesn't occur, bus + errors, busy devices ...). */ +/* #define USE_TIMEOUT_USER_CALLBACK */ + +/* Maximum Timeout values for flags and events waiting loops. These timeouts are + not based on accurate values, they just guarantee that the application will + not remain stuck if the I2C communication is corrupted. + You may modify these timeout values depending on CPU frequency and application + conditions (interrupts routines ...). */ +#define LM75_FLAG_TIMEOUT ((uint32_t)0x1000) +#define LM75_LONG_TIMEOUT ((uint32_t)(10 * LM75_FLAG_TIMEOUT)) + + +/** + * @brief Block Size + */ +#define LM75_REG_TEMP 0x00 /*!< Temperature Register of LM75 */ +#define LM75_REG_CONF 0x01 /*!< Configuration Register of LM75 */ +#define LM75_REG_THYS 0x02 /*!< Temperature Register of LM75 */ +#define LM75_REG_TOS 0x03 /*!< Over-temp Shutdown threshold Register of LM75 */ +#define I2C_TIMEOUT ((uint32_t)0x3FFFF) /*!< I2C Time out */ +#define LM75_ADDR 0x90 /*!< LM75 address */ +#define LM75_I2C_SPEED 100000 /*!< I2C Speed */ + + + +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_I2C_TSENSOR_Exported_Functions + * @{ + */ +void LM75_DeInit(void); +void LM75_Init(void); +ErrorStatus LM75_GetStatus(void); +uint16_t LM75_ReadTemp(void); +uint16_t LM75_ReadReg(uint8_t RegName); +uint8_t LM75_WriteReg(uint8_t RegName, uint16_t RegValue); +uint8_t LM75_ReadConfReg(void); +uint8_t LM75_WriteConfReg(uint8_t RegValue); +uint8_t LM75_ShutDown(FunctionalState NewState); + +/** + * @brief Timeout user callback function. This function is called when a timeout + * condition occurs during communication with IO Expander. Only protoype + * of this function is decalred in IO Expander driver. Its implementation + * may be done into user application. This function may typically stop + * current operations and reset the I2C peripheral and IO Expander. + * To enable this function use uncomment the define USE_TIMEOUT_USER_CALLBACK + * at the top of this file. + */ +#ifdef USE_TIMEOUT_USER_CALLBACK + uint8_t LM75_TIMEOUT_UserCallback(void); +#else + #define LM75_TIMEOUT_UserCallback() LM75_FAIL +#endif /* USE_TIMEOUT_USER_CALLBACK */ + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_EVAL_I2C_TSENSOR_H */ +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_sdio_sd.c b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_sdio_sd.c new file mode 100644 index 0000000..424d2d3 --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_sdio_sd.c @@ -0,0 +1,2502 @@ +/** + ****************************************************************************** + * @file stm32_eval_sdio_sd.c + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file provides a set of functions needed to manage the SDIO SD + * Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h + * to know about the boards supporting this memory). + * + * + * @verbatim + * + * =================================================================== + * How to use this driver + * =================================================================== + * It implements a high level communication layer for read and write + * from/to this memory. The needed STM32 hardware resources (SDIO and + * GPIO) are defined in stm32xx_eval.h file, and the initialization is + * performed in SD_LowLevel_Init() function declared in stm32xx_eval.c + * file. + * You can easily tailor this driver to any other development board, + * by just adapting the defines for hardware resources and + * SD_LowLevel_Init() function. + * + * A - SD Card Initialization and configuration + * ============================================ + * - To initialize the SD Card, use the SD_Init() function. It + * Initializes the SD Card and put it into StandBy State (Ready + * for data transfer). This function provide the following operations: + * + * 1 - Apply the SD Card initialization process at 400KHz and check + * the SD Card type (Standard Capacity or High Capacity). You + * can change or adapt this frequency by adjusting the + * "SDIO_INIT_CLK_DIV" define inside the stm32xx_eval.h file. + * The SD Card frequency (SDIO_CK) is computed as follows: + * + * +---------------------------------------------+ + * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) | + * +---------------------------------------------+ + * + * In initialization mode and according to the SD Card standard, + * make sure that the SDIO_CK frequency don't exceed 400KHz. + * + * 2 - Get the SD CID and CSD data. All these information are + * managed by the SDCardInfo structure. This structure provide + * also ready computed SD Card capacity and Block size. + * + * 3 - Configure the SD Card Data transfer frequency. By Default, + * the card transfer frequency is set to 24MHz. You can change + * or adapt this frequency by adjusting the "SDIO_TRANSFER_CLK_DIV" + * define inside the stm32xx_eval.h file. + * The SD Card frequency (SDIO_CK) is computed as follows: + * + * +---------------------------------------------+ + * | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) | + * +---------------------------------------------+ + * + * In transfer mode and according to the SD Card standard, + * make sure that the SDIO_CK frequency don't exceed 25MHz + * and 50MHz in High-speed mode switch. + * To be able to use a frequency higher than 24MHz, you should + * use the SDIO peripheral in bypass mode. Refer to the + * corresponding reference manual for more details. + * + * 4 - Select the corresponding SD Card according to the address + * read with the step 2. + * + * 5 - Configure the SD Card in wide bus mode: 4-bits data. + * + * B - SD Card Read operation + * ========================== + * - You can read SD card by using two function: SD_ReadBlock() and + * SD_ReadMultiBlocks() functions. These functions support only + * 512-byte block length. + * - The SD_ReadBlock() function read only one block (512-byte). This + * function can transfer the data using DMA controller or using + * polling mode. To select between DMA or polling mode refer to + * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32_eval_sdio_sd.h + * file and uncomment the corresponding line. By default the SD DMA + * mode is selected + * - The SD_ReadMultiBlocks() function read only mutli blocks (multiple + * of 512-byte). + * - Any read operation should be followed by two functions to check + * if the DMA Controller and SD Card status. + * - SD_ReadWaitOperation(): this function insure that the DMA + * controller has finished all data transfer. + * - SD_GetStatus(): to check that the SD Card has finished the + * data transfer and it is ready for data. + * + * - The DMA transfer is finished by the SDIO Data End interrupt. User + * has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler(). + * Don't forget to enable the SDIO_IRQn interrupt using the NVIC controller. + * + * C - SD Card Write operation + * =========================== + * - You can write SD card by using two function: SD_WriteBlock() and + * SD_WriteMultiBlocks() functions. These functions support only + * 512-byte block length. + * - The SD_WriteBlock() function write only one block (512-byte). This + * function can transfer the data using DMA controller or using + * polling mode. To select between DMA or polling mode refer to + * "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32_eval_sdio_sd.h + * file and uncomment the corresponding line. By default the SD DMA + * mode is selected + * - The SD_WriteMultiBlocks() function write only mutli blocks (multiple + * of 512-byte). + * - Any write operation should be followed by two functions to check + * if the DMA Controller and SD Card status. + * - SD_ReadWaitOperation(): this function insure that the DMA + * controller has finished all data transfer. + * - SD_GetStatus(): to check that the SD Card has finished the + * data transfer and it is ready for data. + * + * - The DMA transfer is finished by the SDIO Data End interrupt. User + * has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler(). + * Don't forget to enable the SDIO_IRQn interrupt using the NVIC controller. + + * + * D - SD card status + * ================== + * - At any time, you can check the SD Card status and get the SD card + * state by using the SD_GetStatus() function. This function checks + * first if the SD card is still connected and then get the internal + * SD Card transfer state. + * - You can also get the SD card SD Status register by using the + * SD_SendSDStatus() function. + * + * E - Programming Model + * ===================== + * Status = SD_Init(); // Initialization Step as described in section A + * + * // SDIO Interrupt ENABLE + * NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn; + * NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; + * NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; + * NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; + * NVIC_Init(&NVIC_InitStructure); + * + * // Write operation as described in Section C + * Status = SD_WriteBlock(buffer, address, 512); + * Status = SD_WaitWriteOperation(); + * while(SD_GetStatus() != SD_TRANSFER_OK); + * + * Status = SD_WriteMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS); + * Status = SD_WaitWriteOperation(); + * while(SD_GetStatus() != SD_TRANSFER_OK); + * + * // Read operation as described in Section B + * Status = SD_ReadBlock(buffer, address, 512); + * Status = SD_WaitReadOperation(); + * while(SD_GetStatus() != SD_TRANSFER_OK); + * + * Status = SD_ReadMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS); + * Status = SD_WaitReadOperation(); + * while(SD_GetStatus() != SD_TRANSFER_OK); + * + * + * STM32 SDIO Pin assignment + * ========================= + * +-----------------------------------------------------------+ + * | Pin assignment | + * +-----------------------------+---------------+-------------+ + * | STM32 SDIO Pins | SD | Pin | + * +-----------------------------+---------------+-------------+ + * | SDIO D2 | D2 | 1 | + * | SDIO D3 | D3 | 2 | + * | SDIO CMD | CMD | 3 | + * | | VCC | 4 (3.3 V)| + * | SDIO CLK | CLK | 5 | + * | | GND | 6 (0 V) | + * | SDIO D0 | D0 | 7 | + * | SDIO D1 | D1 | 8 | + * +-----------------------------+---------------+-------------+ + * + * @endverbatim + * + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval_sdio_sd.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_SDIO_SD + * @brief This file provides all the SD Card driver firmware functions. + * @{ + */ + +/** @defgroup STM32_EVAL_SDIO_SD_Private_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SDIO_SD_Private_Defines + * @{ + */ +/** + * @brief SDIO Static flags, TimeOut, FIFO Address + */ +#define NULL 0 +#define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF) +#define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000) + +/** + * @brief Mask for errors Card Status R1 (OCR Register) + */ +#define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000) +#define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000) +#define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000) +#define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000) +#define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000) +#define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000) +#define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000) +#define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000) +#define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000) +#define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000) +#define SD_OCR_CC_ERROR ((uint32_t)0x00100000) +#define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000) +#define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000) +#define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000) +#define SD_OCR_CID_CSD_OVERWRIETE ((uint32_t)0x00010000) +#define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000) +#define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000) +#define SD_OCR_ERASE_RESET ((uint32_t)0x00002000) +#define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008) +#define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008) + +/** + * @brief Masks for R6 Response + */ +#define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000) +#define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000) +#define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000) + +#define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000) +#define SD_HIGH_CAPACITY ((uint32_t)0x40000000) +#define SD_STD_CAPACITY ((uint32_t)0x00000000) +#define SD_CHECK_PATTERN ((uint32_t)0x000001AA) + +#define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF) +#define SD_ALLZERO ((uint32_t)0x00000000) + +#define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000) +#define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000) +#define SD_CARD_LOCKED ((uint32_t)0x02000000) + +#define SD_DATATIMEOUT ((uint32_t)0xFFFFFFFF) +#define SD_0TO7BITS ((uint32_t)0x000000FF) +#define SD_8TO15BITS ((uint32_t)0x0000FF00) +#define SD_16TO23BITS ((uint32_t)0x00FF0000) +#define SD_24TO31BITS ((uint32_t)0xFF000000) +#define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF) + +#define SD_HALFFIFO ((uint32_t)0x00000008) +#define SD_HALFFIFOBYTES ((uint32_t)0x00000020) + +/** + * @brief Command Class Supported + */ +#define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080) +#define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040) +#define SD_CCCC_ERASE ((uint32_t)0x00000020) + +/** + * @brief Following commands are SD Card Specific commands. + * SDIO_APP_CMD should be sent before sending these commands. + */ +#define SDIO_SEND_IF_COND ((uint32_t)0x00000008) + +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SDIO_SD_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SDIO_SD_Private_Variables + * @{ + */ +static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1; +static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0; +static uint8_t SDSTATUS_Tab[16]; +__IO uint32_t StopCondition = 0; +__IO SD_Error TransferError = SD_OK; +__IO uint32_t TransferEnd = 0; +SD_CardInfo SDCardInfo; + +SDIO_InitTypeDef SDIO_InitStructure; +SDIO_CmdInitTypeDef SDIO_CmdInitStructure; +SDIO_DataInitTypeDef SDIO_DataInitStructure; +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SDIO_SD_Private_Function_Prototypes + * @{ + */ +static SD_Error CmdError(void); +static SD_Error CmdResp1Error(uint8_t cmd); +static SD_Error CmdResp7Error(void); +static SD_Error CmdResp3Error(void); +static SD_Error CmdResp2Error(void); +static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca); +static SD_Error SDEnWideBus(FunctionalState NewState); +static SD_Error IsCardProgramming(uint8_t *pstatus); +static SD_Error FindSCR(uint16_t rca, uint32_t *pscr); +uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes); + +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SDIO_SD_Private_Functions + * @{ + */ + +/** + * @brief DeInitializes the SDIO interface. + * @param None + * @retval None + */ +void SD_DeInit(void) +{ + SD_LowLevel_DeInit(); +} + +/** + * @brief Initializes the SD Card and put it into StandBy State (Ready for data + * transfer). + * @param None + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_Init(void) +{ + SD_Error errorstatus = SD_OK; + + /* SDIO Peripheral Low Level Init */ + SD_LowLevel_Init(); + + SDIO_DeInit(); + + errorstatus = SD_PowerON(); + + if (errorstatus != SD_OK) + { + /*!< CMD Response TimeOut (wait for CMDSENT flag) */ + return(errorstatus); + } + + errorstatus = SD_InitializeCards(); + + if (errorstatus != SD_OK) + { + /*!< CMD Response TimeOut (wait for CMDSENT flag) */ + return(errorstatus); + } + + /*!< Configure the SDIO peripheral */ + /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_TRANSFER_CLK_DIV) */ + /*!< on STM32F2xx devices, SDIOCLK is fixed to 48MHz */ + SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; + SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; + SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; + SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; + SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; + SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; + SDIO_Init(&SDIO_InitStructure); + + if (errorstatus == SD_OK) + { + /*----------------- Read CSD/CID MSD registers ------------------*/ + errorstatus = SD_GetCardInfo(&SDCardInfo); + } + + if (errorstatus == SD_OK) + { + /*----------------- Select Card --------------------------------*/ + errorstatus = SD_SelectDeselect((uint32_t) (SDCardInfo.RCA << 16)); + } + + if (errorstatus == SD_OK) + { + errorstatus = SD_EnableWideBusOperation(SDIO_BusWide_4b); + } + + return(errorstatus); +} + +/** + * @brief Gets the cuurent sd card data transfer status. + * @param None + * @retval SDTransferState: Data Transfer state. + * This value can be: + * - SD_TRANSFER_OK: No data transfer is acting + * - SD_TRANSFER_BUSY: Data transfer is acting + */ +SDTransferState SD_GetStatus(void) +{ + SDCardState cardstate = SD_CARD_TRANSFER; + + cardstate = SD_GetState(); + + if (cardstate == SD_CARD_TRANSFER) + { + return(SD_TRANSFER_OK); + } + else if(cardstate == SD_CARD_ERROR) + { + return (SD_TRANSFER_ERROR); + } + else + { + return(SD_TRANSFER_BUSY); + } +} + +/** + * @brief Returns the current card's state. + * @param None + * @retval SDCardState: SD Card Error or SD Card Current State. + */ +SDCardState SD_GetState(void) +{ + uint32_t resp1 = 0; + + if(SD_Detect()== SD_PRESENT) + { + if (SD_SendStatus(&resp1) != SD_OK) + { + return SD_CARD_ERROR; + } + else + { + return (SDCardState)((resp1 >> 9) & 0x0F); + } + } + else + { + return SD_CARD_ERROR; + } +} + +/** + * @brief Detect if SD card is correctly plugged in the memory slot. + * @param None + * @retval Return if SD is detected or not + */ +uint8_t SD_Detect(void) +{ + __IO uint8_t status = SD_PRESENT; + + /*!< Check GPIO to detect SD */ + if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET) + { + status = SD_NOT_PRESENT; + } + return status; +} + +/** + * @brief Enquires cards about their operating voltage and configures + * clock controls. + * @param None + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_PowerON(void) +{ + SD_Error errorstatus = SD_OK; + uint32_t response = 0, count = 0, validvoltage = 0; + uint32_t SDType = SD_STD_CAPACITY; + + /*!< Power ON Sequence -----------------------------------------------------*/ + /*!< Configure the SDIO peripheral */ + /*!< SDIOCLK = HCLK, SDIO_CK = HCLK/(2 + SDIO_INIT_CLK_DIV) */ + /*!< on STM32F2xx devices, SDIOCLK is fixed to 48MHz */ + /*!< SDIO_CK for initialization should not exceed 400 KHz */ + SDIO_InitStructure.SDIO_ClockDiv = SDIO_INIT_CLK_DIV; + SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; + SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; + SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; + SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; + SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; + SDIO_Init(&SDIO_InitStructure); + + /*!< Set Power State to ON */ + SDIO_SetPowerState(SDIO_PowerState_ON); + + /*!< Enable SDIO Clock */ + SDIO_ClockCmd(ENABLE); + + /*!< CMD0: GO_IDLE_STATE ---------------------------------------------------*/ + /*!< No CMD response required */ + SDIO_CmdInitStructure.SDIO_Argument = 0x0; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_GO_IDLE_STATE; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_No; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdError(); + + if (errorstatus != SD_OK) + { + /*!< CMD Response TimeOut (wait for CMDSENT flag) */ + return(errorstatus); + } + + /*!< CMD8: SEND_IF_COND ----------------------------------------------------*/ + /*!< Send CMD8 to verify SD card interface operating condition */ + /*!< Argument: - [31:12]: Reserved (shall be set to '0') + - [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V) + - [7:0]: Check Pattern (recommended 0xAA) */ + /*!< CMD Response: R7 */ + SDIO_CmdInitStructure.SDIO_Argument = SD_CHECK_PATTERN; + SDIO_CmdInitStructure.SDIO_CmdIndex = SDIO_SEND_IF_COND; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp7Error(); + + if (errorstatus == SD_OK) + { + CardType = SDIO_STD_CAPACITY_SD_CARD_V2_0; /*!< SD Card 2.0 */ + SDType = SD_HIGH_CAPACITY; + } + else + { + /*!< CMD55 */ + SDIO_CmdInitStructure.SDIO_Argument = 0x00; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + } + /*!< CMD55 */ + SDIO_CmdInitStructure.SDIO_Argument = 0x00; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + /*!< If errorstatus is Command TimeOut, it is a MMC card */ + /*!< If errorstatus is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) + or SD card 1.x */ + if (errorstatus == SD_OK) + { + /*!< SD CARD */ + /*!< Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */ + while ((!validvoltage) && (count < SD_MAX_VOLT_TRIAL)) + { + + /*!< SEND CMD55 APP_CMD with RCA as 0 */ + SDIO_CmdInitStructure.SDIO_Argument = 0x00; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + SDIO_CmdInitStructure.SDIO_Argument = SD_VOLTAGE_WINDOW_SD | SDType; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_OP_COND; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp3Error(); + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + response = SDIO_GetResponse(SDIO_RESP1); + validvoltage = (((response >> 31) == 1) ? 1 : 0); + count++; + } + if (count >= SD_MAX_VOLT_TRIAL) + { + errorstatus = SD_INVALID_VOLTRANGE; + return(errorstatus); + } + + if (response &= SD_HIGH_CAPACITY) + { + CardType = SDIO_HIGH_CAPACITY_SD_CARD; + } + + }/*!< else MMC Card */ + + return(errorstatus); +} + +/** + * @brief Turns the SDIO output signals off. + * @param None + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_PowerOFF(void) +{ + SD_Error errorstatus = SD_OK; + + /*!< Set Power State to OFF */ + SDIO_SetPowerState(SDIO_PowerState_OFF); + + return(errorstatus); +} + +/** + * @brief Intialises all cards or single card as the case may be Card(s) come + * into standby state. + * @param None + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_InitializeCards(void) +{ + SD_Error errorstatus = SD_OK; + uint16_t rca = 0x01; + + if (SDIO_GetPowerState() == SDIO_PowerState_OFF) + { + errorstatus = SD_REQUEST_NOT_APPLICABLE; + return(errorstatus); + } + + if (SDIO_SECURE_DIGITAL_IO_CARD != CardType) + { + /*!< Send CMD2 ALL_SEND_CID */ + SDIO_CmdInitStructure.SDIO_Argument = 0x0; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ALL_SEND_CID; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp2Error(); + + if (SD_OK != errorstatus) + { + return(errorstatus); + } + + CID_Tab[0] = SDIO_GetResponse(SDIO_RESP1); + CID_Tab[1] = SDIO_GetResponse(SDIO_RESP2); + CID_Tab[2] = SDIO_GetResponse(SDIO_RESP3); + CID_Tab[3] = SDIO_GetResponse(SDIO_RESP4); + } + if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_SECURE_DIGITAL_IO_COMBO_CARD == CardType) + || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) + { + /*!< Send CMD3 SET_REL_ADDR with argument 0 */ + /*!< SD Card publishes its RCA. */ + SDIO_CmdInitStructure.SDIO_Argument = 0x00; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_REL_ADDR; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp6Error(SD_CMD_SET_REL_ADDR, &rca); + + if (SD_OK != errorstatus) + { + return(errorstatus); + } + } + + if (SDIO_SECURE_DIGITAL_IO_CARD != CardType) + { + RCA = rca; + + /*!< Send CMD9 SEND_CSD with argument as card's RCA */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)(rca << 16); + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_CSD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Long; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp2Error(); + + if (SD_OK != errorstatus) + { + return(errorstatus); + } + + CSD_Tab[0] = SDIO_GetResponse(SDIO_RESP1); + CSD_Tab[1] = SDIO_GetResponse(SDIO_RESP2); + CSD_Tab[2] = SDIO_GetResponse(SDIO_RESP3); + CSD_Tab[3] = SDIO_GetResponse(SDIO_RESP4); + } + + errorstatus = SD_OK; /*!< All cards get intialized */ + + return(errorstatus); +} + +/** + * @brief Returns information about specific card. + * @param cardinfo: pointer to a SD_CardInfo structure that contains all SD card + * information. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo) +{ + SD_Error errorstatus = SD_OK; + uint8_t tmp = 0; + + cardinfo->CardType = (uint8_t)CardType; + cardinfo->RCA = (uint16_t)RCA; + + /*!< Byte 0 */ + tmp = (uint8_t)((CSD_Tab[0] & 0xFF000000) >> 24); + cardinfo->SD_csd.CSDStruct = (tmp & 0xC0) >> 6; + cardinfo->SD_csd.SysSpecVersion = (tmp & 0x3C) >> 2; + cardinfo->SD_csd.Reserved1 = tmp & 0x03; + + /*!< Byte 1 */ + tmp = (uint8_t)((CSD_Tab[0] & 0x00FF0000) >> 16); + cardinfo->SD_csd.TAAC = tmp; + + /*!< Byte 2 */ + tmp = (uint8_t)((CSD_Tab[0] & 0x0000FF00) >> 8); + cardinfo->SD_csd.NSAC = tmp; + + /*!< Byte 3 */ + tmp = (uint8_t)(CSD_Tab[0] & 0x000000FF); + cardinfo->SD_csd.MaxBusClkFrec = tmp; + + /*!< Byte 4 */ + tmp = (uint8_t)((CSD_Tab[1] & 0xFF000000) >> 24); + cardinfo->SD_csd.CardComdClasses = tmp << 4; + + /*!< Byte 5 */ + tmp = (uint8_t)((CSD_Tab[1] & 0x00FF0000) >> 16); + cardinfo->SD_csd.CardComdClasses |= (tmp & 0xF0) >> 4; + cardinfo->SD_csd.RdBlockLen = tmp & 0x0F; + + /*!< Byte 6 */ + tmp = (uint8_t)((CSD_Tab[1] & 0x0000FF00) >> 8); + cardinfo->SD_csd.PartBlockRead = (tmp & 0x80) >> 7; + cardinfo->SD_csd.WrBlockMisalign = (tmp & 0x40) >> 6; + cardinfo->SD_csd.RdBlockMisalign = (tmp & 0x20) >> 5; + cardinfo->SD_csd.DSRImpl = (tmp & 0x10) >> 4; + cardinfo->SD_csd.Reserved2 = 0; /*!< Reserved */ + + if ((CardType == SDIO_STD_CAPACITY_SD_CARD_V1_1) || (CardType == SDIO_STD_CAPACITY_SD_CARD_V2_0)) + { + cardinfo->SD_csd.DeviceSize = (tmp & 0x03) << 10; + + /*!< Byte 7 */ + tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF); + cardinfo->SD_csd.DeviceSize |= (tmp) << 2; + + /*!< Byte 8 */ + tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24); + cardinfo->SD_csd.DeviceSize |= (tmp & 0xC0) >> 6; + + cardinfo->SD_csd.MaxRdCurrentVDDMin = (tmp & 0x38) >> 3; + cardinfo->SD_csd.MaxRdCurrentVDDMax = (tmp & 0x07); + + /*!< Byte 9 */ + tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16); + cardinfo->SD_csd.MaxWrCurrentVDDMin = (tmp & 0xE0) >> 5; + cardinfo->SD_csd.MaxWrCurrentVDDMax = (tmp & 0x1C) >> 2; + cardinfo->SD_csd.DeviceSizeMul = (tmp & 0x03) << 1; + /*!< Byte 10 */ + tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8); + cardinfo->SD_csd.DeviceSizeMul |= (tmp & 0x80) >> 7; + + cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ; + cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2)); + cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen); + cardinfo->CardCapacity *= cardinfo->CardBlockSize; + } + else if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + { + /*!< Byte 7 */ + tmp = (uint8_t)(CSD_Tab[1] & 0x000000FF); + cardinfo->SD_csd.DeviceSize = (tmp & 0x3F) << 16; + + /*!< Byte 8 */ + tmp = (uint8_t)((CSD_Tab[2] & 0xFF000000) >> 24); + + cardinfo->SD_csd.DeviceSize |= (tmp << 8); + + /*!< Byte 9 */ + tmp = (uint8_t)((CSD_Tab[2] & 0x00FF0000) >> 16); + + cardinfo->SD_csd.DeviceSize |= (tmp); + + /*!< Byte 10 */ + tmp = (uint8_t)((CSD_Tab[2] & 0x0000FF00) >> 8); + + cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) * 512 * 1024; + cardinfo->CardBlockSize = 512; + } + + + cardinfo->SD_csd.EraseGrSize = (tmp & 0x40) >> 6; + cardinfo->SD_csd.EraseGrMul = (tmp & 0x3F) << 1; + + /*!< Byte 11 */ + tmp = (uint8_t)(CSD_Tab[2] & 0x000000FF); + cardinfo->SD_csd.EraseGrMul |= (tmp & 0x80) >> 7; + cardinfo->SD_csd.WrProtectGrSize = (tmp & 0x7F); + + /*!< Byte 12 */ + tmp = (uint8_t)((CSD_Tab[3] & 0xFF000000) >> 24); + cardinfo->SD_csd.WrProtectGrEnable = (tmp & 0x80) >> 7; + cardinfo->SD_csd.ManDeflECC = (tmp & 0x60) >> 5; + cardinfo->SD_csd.WrSpeedFact = (tmp & 0x1C) >> 2; + cardinfo->SD_csd.MaxWrBlockLen = (tmp & 0x03) << 2; + + /*!< Byte 13 */ + tmp = (uint8_t)((CSD_Tab[3] & 0x00FF0000) >> 16); + cardinfo->SD_csd.MaxWrBlockLen |= (tmp & 0xC0) >> 6; + cardinfo->SD_csd.WriteBlockPaPartial = (tmp & 0x20) >> 5; + cardinfo->SD_csd.Reserved3 = 0; + cardinfo->SD_csd.ContentProtectAppli = (tmp & 0x01); + + /*!< Byte 14 */ + tmp = (uint8_t)((CSD_Tab[3] & 0x0000FF00) >> 8); + cardinfo->SD_csd.FileFormatGrouop = (tmp & 0x80) >> 7; + cardinfo->SD_csd.CopyFlag = (tmp & 0x40) >> 6; + cardinfo->SD_csd.PermWrProtect = (tmp & 0x20) >> 5; + cardinfo->SD_csd.TempWrProtect = (tmp & 0x10) >> 4; + cardinfo->SD_csd.FileFormat = (tmp & 0x0C) >> 2; + cardinfo->SD_csd.ECC = (tmp & 0x03); + + /*!< Byte 15 */ + tmp = (uint8_t)(CSD_Tab[3] & 0x000000FF); + cardinfo->SD_csd.CSD_CRC = (tmp & 0xFE) >> 1; + cardinfo->SD_csd.Reserved4 = 1; + + + /*!< Byte 0 */ + tmp = (uint8_t)((CID_Tab[0] & 0xFF000000) >> 24); + cardinfo->SD_cid.ManufacturerID = tmp; + + /*!< Byte 1 */ + tmp = (uint8_t)((CID_Tab[0] & 0x00FF0000) >> 16); + cardinfo->SD_cid.OEM_AppliID = tmp << 8; + + /*!< Byte 2 */ + tmp = (uint8_t)((CID_Tab[0] & 0x000000FF00) >> 8); + cardinfo->SD_cid.OEM_AppliID |= tmp; + + /*!< Byte 3 */ + tmp = (uint8_t)(CID_Tab[0] & 0x000000FF); + cardinfo->SD_cid.ProdName1 = tmp << 24; + + /*!< Byte 4 */ + tmp = (uint8_t)((CID_Tab[1] & 0xFF000000) >> 24); + cardinfo->SD_cid.ProdName1 |= tmp << 16; + + /*!< Byte 5 */ + tmp = (uint8_t)((CID_Tab[1] & 0x00FF0000) >> 16); + cardinfo->SD_cid.ProdName1 |= tmp << 8; + + /*!< Byte 6 */ + tmp = (uint8_t)((CID_Tab[1] & 0x0000FF00) >> 8); + cardinfo->SD_cid.ProdName1 |= tmp; + + /*!< Byte 7 */ + tmp = (uint8_t)(CID_Tab[1] & 0x000000FF); + cardinfo->SD_cid.ProdName2 = tmp; + + /*!< Byte 8 */ + tmp = (uint8_t)((CID_Tab[2] & 0xFF000000) >> 24); + cardinfo->SD_cid.ProdRev = tmp; + + /*!< Byte 9 */ + tmp = (uint8_t)((CID_Tab[2] & 0x00FF0000) >> 16); + cardinfo->SD_cid.ProdSN = tmp << 24; + + /*!< Byte 10 */ + tmp = (uint8_t)((CID_Tab[2] & 0x0000FF00) >> 8); + cardinfo->SD_cid.ProdSN |= tmp << 16; + + /*!< Byte 11 */ + tmp = (uint8_t)(CID_Tab[2] & 0x000000FF); + cardinfo->SD_cid.ProdSN |= tmp << 8; + + /*!< Byte 12 */ + tmp = (uint8_t)((CID_Tab[3] & 0xFF000000) >> 24); + cardinfo->SD_cid.ProdSN |= tmp; + + /*!< Byte 13 */ + tmp = (uint8_t)((CID_Tab[3] & 0x00FF0000) >> 16); + cardinfo->SD_cid.Reserved1 |= (tmp & 0xF0) >> 4; + cardinfo->SD_cid.ManufactDate = (tmp & 0x0F) << 8; + + /*!< Byte 14 */ + tmp = (uint8_t)((CID_Tab[3] & 0x0000FF00) >> 8); + cardinfo->SD_cid.ManufactDate |= tmp; + + /*!< Byte 15 */ + tmp = (uint8_t)(CID_Tab[3] & 0x000000FF); + cardinfo->SD_cid.CID_CRC = (tmp & 0xFE) >> 1; + cardinfo->SD_cid.Reserved2 = 1; + + return(errorstatus); +} + +/** + * @brief Enables wide bus opeartion for the requeseted card if supported by + * card. + * @param WideMode: Specifies the SD card wide bus mode. + * This parameter can be one of the following values: + * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC) + * @arg SDIO_BusWide_4b: 4-bit data transfer + * @arg SDIO_BusWide_1b: 1-bit data transfer + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus) +{ + SD_Error errorstatus = SD_OK; + uint8_t tmp = 0; + + errorstatus = SD_SendSDStatus((uint32_t *)SDSTATUS_Tab); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< Byte 0 */ + tmp = (uint8_t)((SDSTATUS_Tab[0] & 0xC0) >> 6); + cardstatus->DAT_BUS_WIDTH = tmp; + + /*!< Byte 0 */ + tmp = (uint8_t)((SDSTATUS_Tab[0] & 0x20) >> 5); + cardstatus->SECURED_MODE = tmp; + + /*!< Byte 2 */ + tmp = (uint8_t)((SDSTATUS_Tab[2] & 0xFF)); + cardstatus->SD_CARD_TYPE = tmp << 8; + + /*!< Byte 3 */ + tmp = (uint8_t)((SDSTATUS_Tab[3] & 0xFF)); + cardstatus->SD_CARD_TYPE |= tmp; + + /*!< Byte 4 */ + tmp = (uint8_t)(SDSTATUS_Tab[4] & 0xFF); + cardstatus->SIZE_OF_PROTECTED_AREA = tmp << 24; + + /*!< Byte 5 */ + tmp = (uint8_t)(SDSTATUS_Tab[5] & 0xFF); + cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 16; + + /*!< Byte 6 */ + tmp = (uint8_t)(SDSTATUS_Tab[6] & 0xFF); + cardstatus->SIZE_OF_PROTECTED_AREA |= tmp << 8; + + /*!< Byte 7 */ + tmp = (uint8_t)(SDSTATUS_Tab[7] & 0xFF); + cardstatus->SIZE_OF_PROTECTED_AREA |= tmp; + + /*!< Byte 8 */ + tmp = (uint8_t)((SDSTATUS_Tab[8] & 0xFF)); + cardstatus->SPEED_CLASS = tmp; + + /*!< Byte 9 */ + tmp = (uint8_t)((SDSTATUS_Tab[9] & 0xFF)); + cardstatus->PERFORMANCE_MOVE = tmp; + + /*!< Byte 10 */ + tmp = (uint8_t)((SDSTATUS_Tab[10] & 0xF0) >> 4); + cardstatus->AU_SIZE = tmp; + + /*!< Byte 11 */ + tmp = (uint8_t)(SDSTATUS_Tab[11] & 0xFF); + cardstatus->ERASE_SIZE = tmp << 8; + + /*!< Byte 12 */ + tmp = (uint8_t)(SDSTATUS_Tab[12] & 0xFF); + cardstatus->ERASE_SIZE |= tmp; + + /*!< Byte 13 */ + tmp = (uint8_t)((SDSTATUS_Tab[13] & 0xFC) >> 2); + cardstatus->ERASE_TIMEOUT = tmp; + + /*!< Byte 13 */ + tmp = (uint8_t)((SDSTATUS_Tab[13] & 0x3)); + cardstatus->ERASE_OFFSET = tmp; + + return(errorstatus); +} + +/** + * @brief Enables wide bus opeartion for the requeseted card if supported by + * card. + * @param WideMode: Specifies the SD card wide bus mode. + * This parameter can be one of the following values: + * @arg SDIO_BusWide_8b: 8-bit data transfer (Only for MMC) + * @arg SDIO_BusWide_4b: 4-bit data transfer + * @arg SDIO_BusWide_1b: 1-bit data transfer + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_EnableWideBusOperation(uint32_t WideMode) +{ + SD_Error errorstatus = SD_OK; + + /*!< MMC Card doesn't support this feature */ + if (SDIO_MULTIMEDIA_CARD == CardType) + { + errorstatus = SD_UNSUPPORTED_FEATURE; + return(errorstatus); + } + else if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) + { + if (SDIO_BusWide_8b == WideMode) + { + errorstatus = SD_UNSUPPORTED_FEATURE; + return(errorstatus); + } + else if (SDIO_BusWide_4b == WideMode) + { + errorstatus = SDEnWideBus(ENABLE); + + if (SD_OK == errorstatus) + { + /*!< Configure the SDIO peripheral */ + SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; + SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; + SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; + SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; + SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_4b; + SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; + SDIO_Init(&SDIO_InitStructure); + } + } + else + { + errorstatus = SDEnWideBus(DISABLE); + + if (SD_OK == errorstatus) + { + /*!< Configure the SDIO peripheral */ + SDIO_InitStructure.SDIO_ClockDiv = SDIO_TRANSFER_CLK_DIV; + SDIO_InitStructure.SDIO_ClockEdge = SDIO_ClockEdge_Rising; + SDIO_InitStructure.SDIO_ClockBypass = SDIO_ClockBypass_Disable; + SDIO_InitStructure.SDIO_ClockPowerSave = SDIO_ClockPowerSave_Disable; + SDIO_InitStructure.SDIO_BusWide = SDIO_BusWide_1b; + SDIO_InitStructure.SDIO_HardwareFlowControl = SDIO_HardwareFlowControl_Disable; + SDIO_Init(&SDIO_InitStructure); + } + } + } + + return(errorstatus); +} + +/** + * @brief Selects od Deselects the corresponding card. + * @param addr: Address of the Card to be selected. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_SelectDeselect(uint32_t addr) +{ + SD_Error errorstatus = SD_OK; + + /*!< Send CMD7 SDIO_SEL_DESEL_CARD */ + SDIO_CmdInitStructure.SDIO_Argument = addr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEL_DESEL_CARD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SEL_DESEL_CARD); + + return(errorstatus); +} + +/** + * @brief Allows to read one block from a specified address in a card. The Data + * transfer can be managed by DMA mode or Polling mode. + * @note This operation should be followed by two functions to check if the + * DMA Controller and SD Card status. + * - SD_ReadWaitOperation(): this function insure that the DMA + * controller has finished all data transfer. + * - SD_GetStatus(): to check that the SD Card has finished the + * data transfer and it is ready for data. + * @param readbuff: pointer to the buffer that will contain the received data + * @param ReadAddr: Address from where data are to be read. + * @param BlockSize: the SD card Data block size. The Block size should be 512. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize) +{ + SD_Error errorstatus = SD_OK; +#if defined (SD_POLLING_MODE) + uint32_t count = 0, *tempbuff = (uint32_t *)readbuff; +#endif + + TransferError = SD_OK; + TransferEnd = 0; + StopCondition = 0; + + SDIO->DCTRL = 0x0; + + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + { + BlockSize = 512; + ReadAddr /= 512; + } + + SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; + SDIO_DataInitStructure.SDIO_DataLength = BlockSize; + SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; + SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; + SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; + SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; + SDIO_DataConfig(&SDIO_DataInitStructure); + + /*!< Send CMD17 READ_SINGLE_BLOCK */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + +#if defined (SD_POLLING_MODE) + /*!< In case of single block transfer, no need of stop transfer at all.*/ + /*!< Polling mode */ + while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))) + { + if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) + { + for (count = 0; count < 8; count++) + { + *(tempbuff + count) = SDIO_ReadData(); + } + tempbuff += 8; + } + } + + if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); + errorstatus = SD_DATA_TIMEOUT; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); + errorstatus = SD_DATA_CRC_FAIL; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_RXOVERR); + errorstatus = SD_RX_OVERRUN; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_STBITERR); + errorstatus = SD_START_BIT_ERR; + return(errorstatus); + } + while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) + { + *tempbuff = SDIO_ReadData(); + tempbuff++; + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + +#elif defined (SD_DMA_MODE) + SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE); + SDIO_DMACmd(ENABLE); + SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, BlockSize); +#endif + + return(errorstatus); +} + +/** + * @brief Allows to read blocks from a specified address in a card. The Data + * transfer can be managed by DMA mode or Polling mode. + * @note This operation should be followed by two functions to check if the + * DMA Controller and SD Card status. + * - SD_ReadWaitOperation(): this function insure that the DMA + * controller has finished all data transfer. + * - SD_GetStatus(): to check that the SD Card has finished the + * data transfer and it is ready for data. + * @param readbuff: pointer to the buffer that will contain the received data. + * @param ReadAddr: Address from where data are to be read. + * @param BlockSize: the SD card Data block size. The Block size should be 512. + * @param NumberOfBlocks: number of blocks to be read. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks) +{ + SD_Error errorstatus = SD_OK; + TransferError = SD_OK; + TransferEnd = 0; + StopCondition = 1; + + SDIO->DCTRL = 0x0; + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + { + BlockSize = 512; + ReadAddr /= 512; + } + + /*!< Set Block Size for Card */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); + + if (SD_OK != errorstatus) + { + return(errorstatus); + } + + SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; + SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize; + SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; + SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; + SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; + SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; + SDIO_DataConfig(&SDIO_DataInitStructure); + + /*!< Send CMD18 READ_MULT_BLOCK with argument data address */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_MULT_BLOCK; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_READ_MULT_BLOCK); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE); + SDIO_DMACmd(ENABLE); + SD_LowLevel_DMA_RxConfig((uint32_t *)readbuff, (NumberOfBlocks * BlockSize)); + + return(errorstatus); +} + +/** + * @brief This function waits until the SDIO DMA data transfer is finished. + * This function should be called after SDIO_ReadMultiBlocks() function + * to insure that all data sent by the card are already transferred by + * the DMA controller. + * @param None. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_WaitReadOperation(void) +{ + SD_Error errorstatus = SD_OK; + + while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK)) + {} + + if (TransferError != SD_OK) + { + return(TransferError); + } + + return(errorstatus); +} + +/** + * @brief Allows to write one block starting from a specified address in a card. + * The Data transfer can be managed by DMA mode or Polling mode. + * @note This operation should be followed by two functions to check if the + * DMA Controller and SD Card status. + * - SD_ReadWaitOperation(): this function insure that the DMA + * controller has finished all data transfer. + * - SD_GetStatus(): to check that the SD Card has finished the + * data transfer and it is ready for data. + * @param writebuff: pointer to the buffer that contain the data to be transferred. + * @param WriteAddr: Address from where data are to be read. + * @param BlockSize: the SD card Data block size. The Block size should be 512. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize) +{ + SD_Error errorstatus = SD_OK; + +#if defined (SD_POLLING_MODE) + uint32_t bytestransferred = 0, count = 0, restwords = 0; + uint32_t *tempbuff = (uint32_t *)writebuff; +#endif + + TransferError = SD_OK; + TransferEnd = 0; + StopCondition = 0; + + SDIO->DCTRL = 0x0; + + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + { + BlockSize = 512; + WriteAddr /= 512; + } + + /*!< Send CMD24 WRITE_SINGLE_BLOCK */ + SDIO_CmdInitStructure.SDIO_Argument = WriteAddr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_SINGLE_BLOCK; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_WRITE_SINGLE_BLOCK); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; + SDIO_DataInitStructure.SDIO_DataLength = BlockSize; + SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; + SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; + SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; + SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; + SDIO_DataConfig(&SDIO_DataInitStructure); + + /*!< In case of single data block transfer no need of stop command at all */ +#if defined (SD_POLLING_MODE) + while (!(SDIO->STA & (SDIO_FLAG_DBCKEND | SDIO_FLAG_TXUNDERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_STBITERR))) + { + if (SDIO_GetFlagStatus(SDIO_FLAG_TXFIFOHE) != RESET) + { + if ((512 - bytestransferred) < 32) + { + restwords = ((512 - bytestransferred) % 4 == 0) ? ((512 - bytestransferred) / 4) : (( 512 - bytestransferred) / 4 + 1); + for (count = 0; count < restwords; count++, tempbuff++, bytestransferred += 4) + { + SDIO_WriteData(*tempbuff); + } + } + else + { + for (count = 0; count < 8; count++) + { + SDIO_WriteData(*(tempbuff + count)); + } + tempbuff += 8; + bytestransferred += 32; + } + } + } + if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); + errorstatus = SD_DATA_TIMEOUT; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); + errorstatus = SD_DATA_CRC_FAIL; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_TXUNDERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_TXUNDERR); + errorstatus = SD_TX_UNDERRUN; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_STBITERR); + errorstatus = SD_START_BIT_ERR; + return(errorstatus); + } +#elif defined (SD_DMA_MODE) + SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE); + SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, BlockSize); + SDIO_DMACmd(ENABLE); +#endif + + return(errorstatus); +} + +/** + * @brief Allows to write blocks starting from a specified address in a card. + * The Data transfer can be managed by DMA mode only. + * @note This operation should be followed by two functions to check if the + * DMA Controller and SD Card status. + * - SD_ReadWaitOperation(): this function insure that the DMA + * controller has finished all data transfer. + * - SD_GetStatus(): to check that the SD Card has finished the + * data transfer and it is ready for data. + * @param WriteAddr: Address from where data are to be read. + * @param writebuff: pointer to the buffer that contain the data to be transferred. + * @param BlockSize: the SD card Data block size. The Block size should be 512. + * @param NumberOfBlocks: number of blocks to be written. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks) +{ + SD_Error errorstatus = SD_OK; + __IO uint32_t count = 0; + + TransferError = SD_OK; + TransferEnd = 0; + StopCondition = 1; + + SDIO->DCTRL = 0x0; + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + { + BlockSize = 512; + WriteAddr /= 512; + } + + /*!< To improve performance */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) (RCA << 16); + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + /*!< To improve performance */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)NumberOfBlocks; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCK_COUNT; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SET_BLOCK_COUNT); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + + /*!< Send CMD25 WRITE_MULT_BLOCK with argument data address */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)WriteAddr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_WRITE_MULT_BLOCK; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_WRITE_MULT_BLOCK); + + if (SD_OK != errorstatus) + { + return(errorstatus); + } + + SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; + SDIO_DataInitStructure.SDIO_DataLength = NumberOfBlocks * BlockSize; + SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4; + SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToCard; + SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; + SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; + SDIO_DataConfig(&SDIO_DataInitStructure); + + SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE); + SDIO_DMACmd(ENABLE); + SD_LowLevel_DMA_TxConfig((uint32_t *)writebuff, (NumberOfBlocks * BlockSize)); + + return(errorstatus); +} + +/** + * @brief This function waits until the SDIO DMA data transfer is finished. + * This function should be called after SDIO_WriteBlock() and + * SDIO_WriteMultiBlocks() function to insure that all data sent by the + * card are already transferred by the DMA controller. + * @param None. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_WaitWriteOperation(void) +{ + SD_Error errorstatus = SD_OK; + + while ((SD_DMAEndOfTransferStatus() == RESET) && (TransferEnd == 0) && (TransferError == SD_OK)) + {} + + if (TransferError != SD_OK) + { + return(TransferError); + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + return(errorstatus); +} + +/** + * @brief Gets the cuurent data transfer state. + * @param None + * @retval SDTransferState: Data Transfer state. + * This value can be: + * - SD_TRANSFER_OK: No data transfer is acting + * - SD_TRANSFER_BUSY: Data transfer is acting + */ +SDTransferState SD_GetTransferState(void) +{ + if (SDIO->STA & (SDIO_FLAG_TXACT | SDIO_FLAG_RXACT)) + { + return(SD_TRANSFER_BUSY); + } + else + { + return(SD_TRANSFER_OK); + } +} + +/** + * @brief Aborts an ongoing data transfer. + * @param None + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_StopTransfer(void) +{ + SD_Error errorstatus = SD_OK; + + /*!< Send CMD12 STOP_TRANSMISSION */ + SDIO->ARG = 0x0; + SDIO->CMD = 0x44C; + errorstatus = CmdResp1Error(SD_CMD_STOP_TRANSMISSION); + + return(errorstatus); +} + +/** + * @brief Allows to erase memory area specified for the given card. + * @param startaddr: the start address. + * @param endaddr: the end address. + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr) +{ + SD_Error errorstatus = SD_OK; + uint32_t delay = 0; + __IO uint32_t maxdelay = 0; + uint8_t cardstate = 0; + + /*!< Check if the card coomnd class supports erase command */ + if (((CSD_Tab[1] >> 20) & SD_CCCC_ERASE) == 0) + { + errorstatus = SD_REQUEST_NOT_APPLICABLE; + return(errorstatus); + } + + maxdelay = 120000 / ((SDIO->CLKCR & 0xFF) + 2); + + if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) + { + errorstatus = SD_LOCK_UNLOCK_FAILED; + return(errorstatus); + } + + if (CardType == SDIO_HIGH_CAPACITY_SD_CARD) + { + startaddr /= 512; + endaddr /= 512; + } + + /*!< According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */ + if ((SDIO_STD_CAPACITY_SD_CARD_V1_1 == CardType) || (SDIO_STD_CAPACITY_SD_CARD_V2_0 == CardType) || (SDIO_HIGH_CAPACITY_SD_CARD == CardType)) + { + /*!< Send CMD32 SD_ERASE_GRP_START with argument as addr */ + SDIO_CmdInitStructure.SDIO_Argument = startaddr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_START; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_START); + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< Send CMD33 SD_ERASE_GRP_END with argument as addr */ + SDIO_CmdInitStructure.SDIO_Argument = endaddr; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_ERASE_GRP_END; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SD_ERASE_GRP_END); + if (errorstatus != SD_OK) + { + return(errorstatus); + } + } + + /*!< Send CMD38 ERASE */ + SDIO_CmdInitStructure.SDIO_Argument = 0; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_ERASE; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_ERASE); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + for (delay = 0; delay < maxdelay; delay++) + {} + + /*!< Wait till the card is in programming state */ + errorstatus = IsCardProgramming(&cardstate); + + while ((errorstatus == SD_OK) && ((SD_CARD_PROGRAMMING == cardstate) || (SD_CARD_RECEIVING == cardstate))) + { + errorstatus = IsCardProgramming(&cardstate); + } + + return(errorstatus); +} + +/** + * @brief Returns the current card's status. + * @param pcardstatus: pointer to the buffer that will contain the SD card + * status (Card Status register). + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_SendStatus(uint32_t *pcardstatus) +{ + SD_Error errorstatus = SD_OK; + + SDIO->ARG = (uint32_t) RCA << 16; + SDIO->CMD = 0x44D; + + errorstatus = CmdResp1Error(SD_CMD_SEND_STATUS); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + *pcardstatus = SDIO->RESP1; + return(errorstatus); +} + +/** + * @brief Returns the current SD card's status. + * @param psdstatus: pointer to the buffer that will contain the SD card status + * (SD Status register). + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_SendSDStatus(uint32_t *psdstatus) +{ + SD_Error errorstatus = SD_OK; + uint32_t count = 0; + + if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) + { + errorstatus = SD_LOCK_UNLOCK_FAILED; + return(errorstatus); + } + + /*!< Set block size for card if it is not equal to current block size for card. */ + SDIO_CmdInitStructure.SDIO_Argument = 64; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< CMD55 */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; + SDIO_DataInitStructure.SDIO_DataLength = 64; + SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_64b; + SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; + SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; + SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; + SDIO_DataConfig(&SDIO_DataInitStructure); + + /*!< Send ACMD13 SD_APP_STAUS with argument as card's RCA.*/ + SDIO_CmdInitStructure.SDIO_Argument = 0; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_STAUS; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + errorstatus = CmdResp1Error(SD_CMD_SD_APP_STAUS); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))) + { + if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET) + { + for (count = 0; count < 8; count++) + { + *(psdstatus + count) = SDIO_ReadData(); + } + psdstatus += 8; + } + } + + if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); + errorstatus = SD_DATA_TIMEOUT; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); + errorstatus = SD_DATA_CRC_FAIL; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_RXOVERR); + errorstatus = SD_RX_OVERRUN; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_STBITERR); + errorstatus = SD_START_BIT_ERR; + return(errorstatus); + } + + while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) + { + *psdstatus = SDIO_ReadData(); + psdstatus++; + } + + /*!< Clear all the static status flags*/ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + return(errorstatus); +} + +/** + * @brief Allows to process all the interrupts that are high. + * @param None + * @retval SD_Error: SD Card Error code. + */ +SD_Error SD_ProcessIRQSrc(void) +{ + if (StopCondition == 1) + { + SDIO->ARG = 0x0; + SDIO->CMD = 0x44C; + TransferError = CmdResp1Error(SD_CMD_STOP_TRANSMISSION); + } + else + { + TransferError = SD_OK; + } + SDIO_ClearITPendingBit(SDIO_IT_DATAEND); + SDIO_ITConfig(SDIO_IT_DATAEND, DISABLE); + TransferEnd = 1; + return(TransferError); +} + +/** + * @brief Checks for error conditions for CMD0. + * @param None + * @retval SD_Error: SD Card Error code. + */ +static SD_Error CmdError(void) +{ + SD_Error errorstatus = SD_OK; + uint32_t timeout; + + timeout = SDIO_CMD0TIMEOUT; /*!< 10000 */ + + while ((timeout > 0) && (SDIO_GetFlagStatus(SDIO_FLAG_CMDSENT) == RESET)) + { + timeout--; + } + + if (timeout == 0) + { + errorstatus = SD_CMD_RSP_TIMEOUT; + return(errorstatus); + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + return(errorstatus); +} + +/** + * @brief Checks for error conditions for R7 response. + * @param None + * @retval SD_Error: SD Card Error code. + */ +static SD_Error CmdResp7Error(void) +{ + SD_Error errorstatus = SD_OK; + uint32_t status; + uint32_t timeout = SDIO_CMD0TIMEOUT; + + status = SDIO->STA; + + while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT)) && (timeout > 0)) + { + timeout--; + status = SDIO->STA; + } + + if ((timeout == 0) || (status & SDIO_FLAG_CTIMEOUT)) + { + /*!< Card is not V2.0 complient or card does not support the set voltage range */ + errorstatus = SD_CMD_RSP_TIMEOUT; + SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); + return(errorstatus); + } + + if (status & SDIO_FLAG_CMDREND) + { + /*!< Card is SD V2.0 compliant */ + errorstatus = SD_OK; + SDIO_ClearFlag(SDIO_FLAG_CMDREND); + return(errorstatus); + } + return(errorstatus); +} + +/** + * @brief Checks for error conditions for R1 response. + * @param cmd: The sent command index. + * @retval SD_Error: SD Card Error code. + */ +static SD_Error CmdResp1Error(uint8_t cmd) +{ + while (!(SDIO->STA & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))) + { + } + + SDIO->ICR = SDIO_STATIC_FLAGS; + + return (SD_Error)(SDIO->RESP1 & SD_OCR_ERRORBITS); +} + +/** + * @brief Checks for error conditions for R3 (OCR) response. + * @param None + * @retval SD_Error: SD Card Error code. + */ +static SD_Error CmdResp3Error(void) +{ + SD_Error errorstatus = SD_OK; + uint32_t status; + + status = SDIO->STA; + + while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))) + { + status = SDIO->STA; + } + + if (status & SDIO_FLAG_CTIMEOUT) + { + errorstatus = SD_CMD_RSP_TIMEOUT; + SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); + return(errorstatus); + } + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + return(errorstatus); +} + +/** + * @brief Checks for error conditions for R2 (CID or CSD) response. + * @param None + * @retval SD_Error: SD Card Error code. + */ +static SD_Error CmdResp2Error(void) +{ + SD_Error errorstatus = SD_OK; + uint32_t status; + + status = SDIO->STA; + + while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND))) + { + status = SDIO->STA; + } + + if (status & SDIO_FLAG_CTIMEOUT) + { + errorstatus = SD_CMD_RSP_TIMEOUT; + SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); + return(errorstatus); + } + else if (status & SDIO_FLAG_CCRCFAIL) + { + errorstatus = SD_CMD_CRC_FAIL; + SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); + return(errorstatus); + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + return(errorstatus); +} + +/** + * @brief Checks for error conditions for R6 (RCA) response. + * @param cmd: The sent command index. + * @param prca: pointer to the variable that will contain the SD card relative + * address RCA. + * @retval SD_Error: SD Card Error code. + */ +static SD_Error CmdResp6Error(uint8_t cmd, uint16_t *prca) +{ + SD_Error errorstatus = SD_OK; + uint32_t status; + uint32_t response_r1; + + status = SDIO->STA; + + while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CTIMEOUT | SDIO_FLAG_CMDREND))) + { + status = SDIO->STA; + } + + if (status & SDIO_FLAG_CTIMEOUT) + { + errorstatus = SD_CMD_RSP_TIMEOUT; + SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); + return(errorstatus); + } + else if (status & SDIO_FLAG_CCRCFAIL) + { + errorstatus = SD_CMD_CRC_FAIL; + SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); + return(errorstatus); + } + + /*!< Check response received is of desired command */ + if (SDIO_GetCommandResponse() != cmd) + { + errorstatus = SD_ILLEGAL_CMD; + return(errorstatus); + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + /*!< We have received response, retrieve it. */ + response_r1 = SDIO_GetResponse(SDIO_RESP1); + + if (SD_ALLZERO == (response_r1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED))) + { + *prca = (uint16_t) (response_r1 >> 16); + return(errorstatus); + } + + if (response_r1 & SD_R6_GENERAL_UNKNOWN_ERROR) + { + return(SD_GENERAL_UNKNOWN_ERROR); + } + + if (response_r1 & SD_R6_ILLEGAL_CMD) + { + return(SD_ILLEGAL_CMD); + } + + if (response_r1 & SD_R6_COM_CRC_FAILED) + { + return(SD_COM_CRC_FAILED); + } + + return(errorstatus); +} + +/** + * @brief Enables or disables the SDIO wide bus mode. + * @param NewState: new state of the SDIO wide bus mode. + * This parameter can be: ENABLE or DISABLE. + * @retval SD_Error: SD Card Error code. + */ +static SD_Error SDEnWideBus(FunctionalState NewState) +{ + SD_Error errorstatus = SD_OK; + + uint32_t scr[2] = {0, 0}; + + if (SDIO_GetResponse(SDIO_RESP1) & SD_CARD_LOCKED) + { + errorstatus = SD_LOCK_UNLOCK_FAILED; + return(errorstatus); + } + + /*!< Get SCR Register */ + errorstatus = FindSCR(RCA, scr); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< If wide bus operation to be enabled */ + if (NewState == ENABLE) + { + /*!< If requested card supports wide bus operation */ + if ((scr[1] & SD_WIDE_BUS_SUPPORT) != SD_ALLZERO) + { + /*!< Send CMD55 APP_CMD with argument as card's RCA.*/ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ + SDIO_CmdInitStructure.SDIO_Argument = 0x2; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + return(errorstatus); + } + else + { + errorstatus = SD_REQUEST_NOT_APPLICABLE; + return(errorstatus); + } + } /*!< If wide bus operation to be disabled */ + else + { + /*!< If requested card supports 1 bit mode operation */ + if ((scr[1] & SD_SINGLE_BUS_SUPPORT) != SD_ALLZERO) + { + /*!< Send CMD55 APP_CMD with argument as card's RCA.*/ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< Send ACMD6 APP_CMD with argument as 2 for wide bus mode */ + SDIO_CmdInitStructure.SDIO_Argument = 0x00; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_SD_SET_BUSWIDTH; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_APP_SD_SET_BUSWIDTH); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + return(errorstatus); + } + else + { + errorstatus = SD_REQUEST_NOT_APPLICABLE; + return(errorstatus); + } + } +} + +/** + * @brief Checks if the SD card is in programming state. + * @param pstatus: pointer to the variable that will contain the SD card state. + * @retval SD_Error: SD Card Error code. + */ +static SD_Error IsCardProgramming(uint8_t *pstatus) +{ + SD_Error errorstatus = SD_OK; + __IO uint32_t respR1 = 0, status = 0; + + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SEND_STATUS; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + status = SDIO->STA; + while (!(status & (SDIO_FLAG_CCRCFAIL | SDIO_FLAG_CMDREND | SDIO_FLAG_CTIMEOUT))) + { + status = SDIO->STA; + } + + if (status & SDIO_FLAG_CTIMEOUT) + { + errorstatus = SD_CMD_RSP_TIMEOUT; + SDIO_ClearFlag(SDIO_FLAG_CTIMEOUT); + return(errorstatus); + } + else if (status & SDIO_FLAG_CCRCFAIL) + { + errorstatus = SD_CMD_CRC_FAIL; + SDIO_ClearFlag(SDIO_FLAG_CCRCFAIL); + return(errorstatus); + } + + status = (uint32_t)SDIO_GetCommandResponse(); + + /*!< Check response received is of desired command */ + if (status != SD_CMD_SEND_STATUS) + { + errorstatus = SD_ILLEGAL_CMD; + return(errorstatus); + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + + /*!< We have received response, retrieve it for analysis */ + respR1 = SDIO_GetResponse(SDIO_RESP1); + + /*!< Find out card status */ + *pstatus = (uint8_t) ((respR1 >> 9) & 0x0000000F); + + if ((respR1 & SD_OCR_ERRORBITS) == SD_ALLZERO) + { + return(errorstatus); + } + + if (respR1 & SD_OCR_ADDR_OUT_OF_RANGE) + { + return(SD_ADDR_OUT_OF_RANGE); + } + + if (respR1 & SD_OCR_ADDR_MISALIGNED) + { + return(SD_ADDR_MISALIGNED); + } + + if (respR1 & SD_OCR_BLOCK_LEN_ERR) + { + return(SD_BLOCK_LEN_ERR); + } + + if (respR1 & SD_OCR_ERASE_SEQ_ERR) + { + return(SD_ERASE_SEQ_ERR); + } + + if (respR1 & SD_OCR_BAD_ERASE_PARAM) + { + return(SD_BAD_ERASE_PARAM); + } + + if (respR1 & SD_OCR_WRITE_PROT_VIOLATION) + { + return(SD_WRITE_PROT_VIOLATION); + } + + if (respR1 & SD_OCR_LOCK_UNLOCK_FAILED) + { + return(SD_LOCK_UNLOCK_FAILED); + } + + if (respR1 & SD_OCR_COM_CRC_FAILED) + { + return(SD_COM_CRC_FAILED); + } + + if (respR1 & SD_OCR_ILLEGAL_CMD) + { + return(SD_ILLEGAL_CMD); + } + + if (respR1 & SD_OCR_CARD_ECC_FAILED) + { + return(SD_CARD_ECC_FAILED); + } + + if (respR1 & SD_OCR_CC_ERROR) + { + return(SD_CC_ERROR); + } + + if (respR1 & SD_OCR_GENERAL_UNKNOWN_ERROR) + { + return(SD_GENERAL_UNKNOWN_ERROR); + } + + if (respR1 & SD_OCR_STREAM_READ_UNDERRUN) + { + return(SD_STREAM_READ_UNDERRUN); + } + + if (respR1 & SD_OCR_STREAM_WRITE_OVERRUN) + { + return(SD_STREAM_WRITE_OVERRUN); + } + + if (respR1 & SD_OCR_CID_CSD_OVERWRIETE) + { + return(SD_CID_CSD_OVERWRITE); + } + + if (respR1 & SD_OCR_WP_ERASE_SKIP) + { + return(SD_WP_ERASE_SKIP); + } + + if (respR1 & SD_OCR_CARD_ECC_DISABLED) + { + return(SD_CARD_ECC_DISABLED); + } + + if (respR1 & SD_OCR_ERASE_RESET) + { + return(SD_ERASE_RESET); + } + + if (respR1 & SD_OCR_AKE_SEQ_ERROR) + { + return(SD_AKE_SEQ_ERROR); + } + + return(errorstatus); +} + +/** + * @brief Find the SD card SCR register value. + * @param rca: selected card address. + * @param pscr: pointer to the buffer that will contain the SCR value. + * @retval SD_Error: SD Card Error code. + */ +static SD_Error FindSCR(uint16_t rca, uint32_t *pscr) +{ + uint32_t index = 0; + SD_Error errorstatus = SD_OK; + uint32_t tempscr[2] = {0, 0}; + + /*!< Set Block Size To 8 Bytes */ + /*!< Send CMD55 APP_CMD with argument as card's RCA */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)8; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + /*!< Send CMD55 APP_CMD with argument as card's RCA */ + SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) RCA << 16; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_APP_CMD; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_APP_CMD); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT; + SDIO_DataInitStructure.SDIO_DataLength = 8; + SDIO_DataInitStructure.SDIO_DataBlockSize = SDIO_DataBlockSize_8b; + SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO; + SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block; + SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable; + SDIO_DataConfig(&SDIO_DataInitStructure); + + + /*!< Send ACMD51 SD_APP_SEND_SCR with argument as 0 */ + SDIO_CmdInitStructure.SDIO_Argument = 0x0; + SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SD_APP_SEND_SCR; + SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short; + SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No; + SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable; + SDIO_SendCommand(&SDIO_CmdInitStructure); + + errorstatus = CmdResp1Error(SD_CMD_SD_APP_SEND_SCR); + + if (errorstatus != SD_OK) + { + return(errorstatus); + } + + while (!(SDIO->STA & (SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR))) + { + if (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET) + { + *(tempscr + index) = SDIO_ReadData(); + index++; + } + } + + if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT); + errorstatus = SD_DATA_TIMEOUT; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL); + errorstatus = SD_DATA_CRC_FAIL; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_RXOVERR); + errorstatus = SD_RX_OVERRUN; + return(errorstatus); + } + else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET) + { + SDIO_ClearFlag(SDIO_FLAG_STBITERR); + errorstatus = SD_START_BIT_ERR; + return(errorstatus); + } + + /*!< Clear all the static flags */ + SDIO_ClearFlag(SDIO_STATIC_FLAGS); + + *(pscr + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) | ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24); + + *(pscr) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) | ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24); + + return(errorstatus); +} + +/** + * @brief Converts the number of bytes in power of two and returns the power. + * @param NumberOfBytes: number of bytes. + * @retval None + */ +uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes) +{ + uint8_t count = 0; + + while (NumberOfBytes != 1) + { + NumberOfBytes >>= 1; + count++; + } + return(count); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_sdio_sd.h b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_sdio_sd.h new file mode 100644 index 0000000..d673165 --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_sdio_sd.h @@ -0,0 +1,397 @@ +/** + ****************************************************************************** + * @file stm32_eval_sdio_sd.h + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file contains all the functions prototypes for the SD Card + * stm32_eval_sdio_sd driver firmware library. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32_EVAL_SDIO_SD_H +#define __STM32_EVAL_SDIO_SD_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_SDIO_SD + * @{ + */ + +/** @defgroup STM32_EVAL_SDIO_SD_Exported_Types + * @{ + */ +typedef enum +{ +/** + * @brief SDIO specific error defines + */ + SD_CMD_CRC_FAIL = (1), /*!< Command response received (but CRC check failed) */ + SD_DATA_CRC_FAIL = (2), /*!< Data bock sent/received (CRC check Failed) */ + SD_CMD_RSP_TIMEOUT = (3), /*!< Command response timeout */ + SD_DATA_TIMEOUT = (4), /*!< Data time out */ + SD_TX_UNDERRUN = (5), /*!< Transmit FIFO under-run */ + SD_RX_OVERRUN = (6), /*!< Receive FIFO over-run */ + SD_START_BIT_ERR = (7), /*!< Start bit not detected on all data signals in widE bus mode */ + SD_CMD_OUT_OF_RANGE = (8), /*!< CMD's argument was out of range.*/ + SD_ADDR_MISALIGNED = (9), /*!< Misaligned address */ + SD_BLOCK_LEN_ERR = (10), /*!< Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length */ + SD_ERASE_SEQ_ERR = (11), /*!< An error in the sequence of erase command occurs.*/ + SD_BAD_ERASE_PARAM = (12), /*!< An Invalid selection for erase groups */ + SD_WRITE_PROT_VIOLATION = (13), /*!< Attempt to program a write protect block */ + SD_LOCK_UNLOCK_FAILED = (14), /*!< Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card */ + SD_COM_CRC_FAILED = (15), /*!< CRC check of the previous command failed */ + SD_ILLEGAL_CMD = (16), /*!< Command is not legal for the card state */ + SD_CARD_ECC_FAILED = (17), /*!< Card internal ECC was applied but failed to correct the data */ + SD_CC_ERROR = (18), /*!< Internal card controller error */ + SD_GENERAL_UNKNOWN_ERROR = (19), /*!< General or Unknown error */ + SD_STREAM_READ_UNDERRUN = (20), /*!< The card could not sustain data transfer in stream read operation. */ + SD_STREAM_WRITE_OVERRUN = (21), /*!< The card could not sustain data programming in stream mode */ + SD_CID_CSD_OVERWRITE = (22), /*!< CID/CSD overwrite error */ + SD_WP_ERASE_SKIP = (23), /*!< only partial address space was erased */ + SD_CARD_ECC_DISABLED = (24), /*!< Command has been executed without using internal ECC */ + SD_ERASE_RESET = (25), /*!< Erase sequence was cleared before executing because an out of erase sequence command was received */ + SD_AKE_SEQ_ERROR = (26), /*!< Error in sequence of authentication. */ + SD_INVALID_VOLTRANGE = (27), + SD_ADDR_OUT_OF_RANGE = (28), + SD_SWITCH_ERROR = (29), + SD_SDIO_DISABLED = (30), + SD_SDIO_FUNCTION_BUSY = (31), + SD_SDIO_FUNCTION_FAILED = (32), + SD_SDIO_UNKNOWN_FUNCTION = (33), + +/** + * @brief Standard error defines + */ + SD_INTERNAL_ERROR, + SD_NOT_CONFIGURED, + SD_REQUEST_PENDING, + SD_REQUEST_NOT_APPLICABLE, + SD_INVALID_PARAMETER, + SD_UNSUPPORTED_FEATURE, + SD_UNSUPPORTED_HW, + SD_ERROR, + SD_OK = 0 +} SD_Error; + +/** + * @brief SDIO Transfer state + */ +typedef enum +{ + SD_TRANSFER_OK = 0, + SD_TRANSFER_BUSY = 1, + SD_TRANSFER_ERROR +} SDTransferState; + +/** + * @brief SD Card States + */ +typedef enum +{ + SD_CARD_READY = ((uint32_t)0x00000001), + SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002), + SD_CARD_STANDBY = ((uint32_t)0x00000003), + SD_CARD_TRANSFER = ((uint32_t)0x00000004), + SD_CARD_SENDING = ((uint32_t)0x00000005), + SD_CARD_RECEIVING = ((uint32_t)0x00000006), + SD_CARD_PROGRAMMING = ((uint32_t)0x00000007), + SD_CARD_DISCONNECTED = ((uint32_t)0x00000008), + SD_CARD_ERROR = ((uint32_t)0x000000FF) +}SDCardState; + + +/** + * @brief Card Specific Data: CSD Register + */ +typedef struct +{ + __IO uint8_t CSDStruct; /*!< CSD structure */ + __IO uint8_t SysSpecVersion; /*!< System specification version */ + __IO uint8_t Reserved1; /*!< Reserved */ + __IO uint8_t TAAC; /*!< Data read access-time 1 */ + __IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */ + __IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */ + __IO uint16_t CardComdClasses; /*!< Card command classes */ + __IO uint8_t RdBlockLen; /*!< Max. read data block length */ + __IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */ + __IO uint8_t WrBlockMisalign; /*!< Write block misalignment */ + __IO uint8_t RdBlockMisalign; /*!< Read block misalignment */ + __IO uint8_t DSRImpl; /*!< DSR implemented */ + __IO uint8_t Reserved2; /*!< Reserved */ + __IO uint32_t DeviceSize; /*!< Device Size */ + __IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */ + __IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */ + __IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */ + __IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */ + __IO uint8_t DeviceSizeMul; /*!< Device size multiplier */ + __IO uint8_t EraseGrSize; /*!< Erase group size */ + __IO uint8_t EraseGrMul; /*!< Erase group size multiplier */ + __IO uint8_t WrProtectGrSize; /*!< Write protect group size */ + __IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */ + __IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */ + __IO uint8_t WrSpeedFact; /*!< Write speed factor */ + __IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */ + __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */ + __IO uint8_t Reserved3; /*!< Reserded */ + __IO uint8_t ContentProtectAppli; /*!< Content protection application */ + __IO uint8_t FileFormatGrouop; /*!< File format group */ + __IO uint8_t CopyFlag; /*!< Copy flag (OTP) */ + __IO uint8_t PermWrProtect; /*!< Permanent write protection */ + __IO uint8_t TempWrProtect; /*!< Temporary write protection */ + __IO uint8_t FileFormat; /*!< File Format */ + __IO uint8_t ECC; /*!< ECC code */ + __IO uint8_t CSD_CRC; /*!< CSD CRC */ + __IO uint8_t Reserved4; /*!< always 1*/ +} SD_CSD; + +/** + * @brief Card Identification Data: CID Register + */ +typedef struct +{ + __IO uint8_t ManufacturerID; /*!< ManufacturerID */ + __IO uint16_t OEM_AppliID; /*!< OEM/Application ID */ + __IO uint32_t ProdName1; /*!< Product Name part1 */ + __IO uint8_t ProdName2; /*!< Product Name part2*/ + __IO uint8_t ProdRev; /*!< Product Revision */ + __IO uint32_t ProdSN; /*!< Product Serial Number */ + __IO uint8_t Reserved1; /*!< Reserved1 */ + __IO uint16_t ManufactDate; /*!< Manufacturing Date */ + __IO uint8_t CID_CRC; /*!< CID CRC */ + __IO uint8_t Reserved2; /*!< always 1 */ +} SD_CID; + +/** + * @brief SD Card Status + */ +typedef struct +{ + __IO uint8_t DAT_BUS_WIDTH; + __IO uint8_t SECURED_MODE; + __IO uint16_t SD_CARD_TYPE; + __IO uint32_t SIZE_OF_PROTECTED_AREA; + __IO uint8_t SPEED_CLASS; + __IO uint8_t PERFORMANCE_MOVE; + __IO uint8_t AU_SIZE; + __IO uint16_t ERASE_SIZE; + __IO uint8_t ERASE_TIMEOUT; + __IO uint8_t ERASE_OFFSET; +} SD_CardStatus; + + +/** + * @brief SD Card information + */ +typedef struct +{ + SD_CSD SD_csd; + SD_CID SD_cid; + uint32_t CardCapacity; /*!< Card Capacity */ + uint32_t CardBlockSize; /*!< Card Block Size */ + uint16_t RCA; + uint8_t CardType; +} SD_CardInfo; + +/** + * @} + */ + +/** @defgroup STM32_EVAL_SDIO_SD_Exported_Constants + * @{ + */ + +/** + * @brief SDIO Commands Index + */ +#define SD_CMD_GO_IDLE_STATE ((uint8_t)0) +#define SD_CMD_SEND_OP_COND ((uint8_t)1) +#define SD_CMD_ALL_SEND_CID ((uint8_t)2) +#define SD_CMD_SET_REL_ADDR ((uint8_t)3) /*!< SDIO_SEND_REL_ADDR for SD Card */ +#define SD_CMD_SET_DSR ((uint8_t)4) +#define SD_CMD_SDIO_SEN_OP_COND ((uint8_t)5) +#define SD_CMD_HS_SWITCH ((uint8_t)6) +#define SD_CMD_SEL_DESEL_CARD ((uint8_t)7) +#define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8) +#define SD_CMD_SEND_CSD ((uint8_t)9) +#define SD_CMD_SEND_CID ((uint8_t)10) +#define SD_CMD_READ_DAT_UNTIL_STOP ((uint8_t)11) /*!< SD Card doesn't support it */ +#define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) +#define SD_CMD_SEND_STATUS ((uint8_t)13) +#define SD_CMD_HS_BUSTEST_READ ((uint8_t)14) +#define SD_CMD_GO_INACTIVE_STATE ((uint8_t)15) +#define SD_CMD_SET_BLOCKLEN ((uint8_t)16) +#define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) +#define SD_CMD_READ_MULT_BLOCK ((uint8_t)18) +#define SD_CMD_HS_BUSTEST_WRITE ((uint8_t)19) +#define SD_CMD_WRITE_DAT_UNTIL_STOP ((uint8_t)20) /*!< SD Card doesn't support it */ +#define SD_CMD_SET_BLOCK_COUNT ((uint8_t)23) /*!< SD Card doesn't support it */ +#define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24) +#define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25) +#define SD_CMD_PROG_CID ((uint8_t)26) /*!< reserved for manufacturers */ +#define SD_CMD_PROG_CSD ((uint8_t)27) +#define SD_CMD_SET_WRITE_PROT ((uint8_t)28) +#define SD_CMD_CLR_WRITE_PROT ((uint8_t)29) +#define SD_CMD_SEND_WRITE_PROT ((uint8_t)30) +#define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) /*!< To set the address of the first write + block to be erased. (For SD card only) */ +#define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) /*!< To set the address of the last write block of the + continuous range to be erased. (For SD card only) */ +#define SD_CMD_ERASE_GRP_START ((uint8_t)35) /*!< To set the address of the first write block to be erased. + (For MMC card only spec 3.31) */ + +#define SD_CMD_ERASE_GRP_END ((uint8_t)36) /*!< To set the address of the last write block of the + continuous range to be erased. (For MMC card only spec 3.31) */ + +#define SD_CMD_ERASE ((uint8_t)38) +#define SD_CMD_FAST_IO ((uint8_t)39) /*!< SD Card doesn't support it */ +#define SD_CMD_GO_IRQ_STATE ((uint8_t)40) /*!< SD Card doesn't support it */ +#define SD_CMD_LOCK_UNLOCK ((uint8_t)42) +#define SD_CMD_APP_CMD ((uint8_t)55) +#define SD_CMD_GEN_CMD ((uint8_t)56) +#define SD_CMD_NO_CMD ((uint8_t)64) + +/** + * @brief Following commands are SD Card Specific commands. + * SDIO_APP_CMD should be sent before sending these commands. + */ +#define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) /*!< For SD Card only */ +#define SD_CMD_SD_APP_STAUS ((uint8_t)13) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SEND_NUM_WRITE_BLOCKS ((uint8_t)22) /*!< For SD Card only */ +#define SD_CMD_SD_APP_OP_COND ((uint8_t)41) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SET_CLR_CARD_DETECT ((uint8_t)42) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) /*!< For SD Card only */ +#define SD_CMD_SDIO_RW_DIRECT ((uint8_t)52) /*!< For SD I/O Card only */ +#define SD_CMD_SDIO_RW_EXTENDED ((uint8_t)53) /*!< For SD I/O Card only */ + +/** + * @brief Following commands are SD Card Specific security commands. + * SDIO_APP_CMD should be sent before sending these commands. + */ +#define SD_CMD_SD_APP_GET_MKB ((uint8_t)43) /*!< For SD Card only */ +#define SD_CMD_SD_APP_GET_MID ((uint8_t)44) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SET_CER_RN1 ((uint8_t)45) /*!< For SD Card only */ +#define SD_CMD_SD_APP_GET_CER_RN2 ((uint8_t)46) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SET_CER_RES2 ((uint8_t)47) /*!< For SD Card only */ +#define SD_CMD_SD_APP_GET_CER_RES1 ((uint8_t)48) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SECURE_READ_MULTIPLE_BLOCK ((uint8_t)18) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SECURE_WRITE_MULTIPLE_BLOCK ((uint8_t)25) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SECURE_ERASE ((uint8_t)38) /*!< For SD Card only */ +#define SD_CMD_SD_APP_CHANGE_SECURE_AREA ((uint8_t)49) /*!< For SD Card only */ +#define SD_CMD_SD_APP_SECURE_WRITE_MKB ((uint8_t)48) /*!< For SD Card only */ + +/* Uncomment the following line to select the SDIO Data transfer mode */ +#define SD_DMA_MODE ((uint32_t)0x00000000) +/*#define SD_POLLING_MODE ((uint32_t)0x00000002)*/ + +/** + * @brief SD detection on its memory slot + */ +#define SD_PRESENT ((uint8_t)0x01) +#define SD_NOT_PRESENT ((uint8_t)0x00) + +/** + * @brief Supported SD Memory Cards + */ +#define SDIO_STD_CAPACITY_SD_CARD_V1_1 ((uint32_t)0x00000000) +#define SDIO_STD_CAPACITY_SD_CARD_V2_0 ((uint32_t)0x00000001) +#define SDIO_HIGH_CAPACITY_SD_CARD ((uint32_t)0x00000002) +#define SDIO_MULTIMEDIA_CARD ((uint32_t)0x00000003) +#define SDIO_SECURE_DIGITAL_IO_CARD ((uint32_t)0x00000004) +#define SDIO_HIGH_SPEED_MULTIMEDIA_CARD ((uint32_t)0x00000005) +#define SDIO_SECURE_DIGITAL_IO_COMBO_CARD ((uint32_t)0x00000006) +#define SDIO_HIGH_CAPACITY_MMC_CARD ((uint32_t)0x00000007) + +/** + * @} + */ + +/** @defgroup STM32_EVAL_SDIO_SD_Exported_Macros + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_SDIO_SD_Exported_Functions + * @{ + */ +void SD_DeInit(void); +SD_Error SD_Init(void); +SDTransferState SD_GetStatus(void); +SDCardState SD_GetState(void); +uint8_t SD_Detect(void); +SD_Error SD_PowerON(void); +SD_Error SD_PowerOFF(void); +SD_Error SD_InitializeCards(void); +SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo); +SD_Error SD_GetCardStatus(SD_CardStatus *cardstatus); +SD_Error SD_EnableWideBusOperation(uint32_t WideMode); +SD_Error SD_SelectDeselect(uint32_t addr); +SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize); +SD_Error SD_ReadMultiBlocks(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks); +SD_Error SD_WriteBlock(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize); +SD_Error SD_WriteMultiBlocks(uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks); +SDTransferState SD_GetTransferState(void); +SD_Error SD_StopTransfer(void); +SD_Error SD_Erase(uint32_t startaddr, uint32_t endaddr); +SD_Error SD_SendStatus(uint32_t *pcardstatus); +SD_Error SD_SendSDStatus(uint32_t *psdstatus); +SD_Error SD_ProcessIRQSrc(void); +SD_Error SD_WaitReadOperation(void); +SD_Error SD_WaitWriteOperation(void); +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_EVAL_SDIO_SD_H */ +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_flash.c b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_flash.c new file mode 100644 index 0000000..878144d --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_flash.c @@ -0,0 +1,541 @@ +/** + ****************************************************************************** + * @file stm32_eval_spi_flash.c + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file provides a set of functions needed to manage the SPI M25Pxxx + * FLASH memory mounted on STM32xx-EVAL board (refer to stm32_eval.h + * to know about the boards supporting this memory). + * It implements a high level communication layer for read and write + * from/to this memory. The needed STM32 hardware resources (SPI and + * GPIO) are defined in stm32xx_eval.h file, and the initialization is + * performed in sFLASH_LowLevel_Init() function declared in stm32xx_eval.c + * file. + * You can easily tailor this driver to any other development board, + * by just adapting the defines for hardware resources and + * sFLASH_LowLevel_Init() function. + * + * +-----------------------------------------------------------+ + * | Pin assignment | + * +-----------------------------+---------------+-------------+ + * | STM32 SPI Pins | sFLASH | Pin | + * +-----------------------------+---------------+-------------+ + * | sFLASH_CS_PIN | ChipSelect(/S)| 1 | + * | sFLASH_SPI_MISO_PIN / MISO | DataOut(Q) | 2 | + * | | VCC | 3 (3.3 V)| + * | | GND | 4 (0 V) | + * | sFLASH_SPI_MOSI_PIN / MOSI | DataIn(D) | 5 | + * | sFLASH_SPI_SCK_PIN / SCLK | Clock(C) | 6 | + * | | VCC | 7 (3.3 V)| + * | | VCC | 8 (3.3 V)| + * +-----------------------------+---------------+-------------+ + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval_spi_flash.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_SPI_FLASH + * @brief This file includes the M25Pxxx SPI FLASH driver of STM32-EVAL boards. + * @{ + */ + +/** @defgroup STM32_EVAL_SPI_FLASH_Private_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_FLASH_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_FLASH_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_FLASH_Private_Variables + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_FLASH_Private_Function_Prototypes + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_FLASH_Private_Functions + * @{ + */ + +/** + * @brief DeInitializes the peripherals used by the SPI FLASH driver. + * @param None + * @retval None + */ +void sFLASH_DeInit(void) +{ + sFLASH_LowLevel_DeInit(); +} + +/** + * @brief Initializes the peripherals used by the SPI FLASH driver. + * @param None + * @retval None + */ +void sFLASH_Init(void) +{ + SPI_InitTypeDef SPI_InitStructure; + + sFLASH_LowLevel_Init(); + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< SPI configuration */ + SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; + SPI_InitStructure.SPI_Mode = SPI_Mode_Master; + SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; + SPI_InitStructure.SPI_CPOL = SPI_CPOL_High; + SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; + SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; +#if defined (STM32F10X_LD_VL) || defined (STM32F10X_MD_VL) || defined (STM32F10X_HD_VL) + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2; +#else + SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4; +#endif + + SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; + SPI_InitStructure.SPI_CRCPolynomial = 7; + SPI_Init(sFLASH_SPI, &SPI_InitStructure); + + /*!< Enable the sFLASH_SPI */ + SPI_Cmd(sFLASH_SPI, ENABLE); +} + +/** + * @brief Erases the specified FLASH sector. + * @param SectorAddr: address of the sector to erase. + * @retval None + */ +void sFLASH_EraseSector(uint32_t SectorAddr) +{ + /*!< Send write enable instruction */ + sFLASH_WriteEnable(); + + /*!< Sector Erase */ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + /*!< Send Sector Erase instruction */ + sFLASH_SendByte(sFLASH_CMD_SE); + /*!< Send SectorAddr high nibble address byte */ + sFLASH_SendByte((SectorAddr & 0xFF0000) >> 16); + /*!< Send SectorAddr medium nibble address byte */ + sFLASH_SendByte((SectorAddr & 0xFF00) >> 8); + /*!< Send SectorAddr low nibble address byte */ + sFLASH_SendByte(SectorAddr & 0xFF); + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); +} + +/** + * @brief Erases the entire FLASH. + * @param None + * @retval None + */ +void sFLASH_EraseBulk(void) +{ + /*!< Send write enable instruction */ + sFLASH_WriteEnable(); + + /*!< Bulk Erase */ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + /*!< Send Bulk Erase instruction */ + sFLASH_SendByte(sFLASH_CMD_BE); + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); +} + +/** + * @brief Writes more than one byte to the FLASH with a single WRITE cycle + * (Page WRITE sequence). + * @note The number of byte can't exceed the FLASH page size. + * @param pBuffer: pointer to the buffer containing the data to be written + * to the FLASH. + * @param WriteAddr: FLASH's internal address to write to. + * @param NumByteToWrite: number of bytes to write to the FLASH, must be equal + * or less than "sFLASH_PAGESIZE" value. + * @retval None + */ +void sFLASH_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) +{ + /*!< Enable the write access to the FLASH */ + sFLASH_WriteEnable(); + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + /*!< Send "Write to Memory " instruction */ + sFLASH_SendByte(sFLASH_CMD_WRITE); + /*!< Send WriteAddr high nibble address byte to write to */ + sFLASH_SendByte((WriteAddr & 0xFF0000) >> 16); + /*!< Send WriteAddr medium nibble address byte to write to */ + sFLASH_SendByte((WriteAddr & 0xFF00) >> 8); + /*!< Send WriteAddr low nibble address byte to write to */ + sFLASH_SendByte(WriteAddr & 0xFF); + + /*!< while there is data to be written on the FLASH */ + while (NumByteToWrite--) + { + /*!< Send the current byte */ + sFLASH_SendByte(*pBuffer); + /*!< Point on the next byte to be written */ + pBuffer++; + } + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + /*!< Wait the end of Flash writing */ + sFLASH_WaitForWriteEnd(); +} + +/** + * @brief Writes block of data to the FLASH. In this function, the number of + * WRITE cycles are reduced, using Page WRITE sequence. + * @param pBuffer: pointer to the buffer containing the data to be written + * to the FLASH. + * @param WriteAddr: FLASH's internal address to write to. + * @param NumByteToWrite: number of bytes to write to the FLASH. + * @retval None + */ +void sFLASH_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite) +{ + uint8_t NumOfPage = 0, NumOfSingle = 0, Addr = 0, count = 0, temp = 0; + + Addr = WriteAddr % sFLASH_SPI_PAGESIZE; + count = sFLASH_SPI_PAGESIZE - Addr; + NumOfPage = NumByteToWrite / sFLASH_SPI_PAGESIZE; + NumOfSingle = NumByteToWrite % sFLASH_SPI_PAGESIZE; + + if (Addr == 0) /*!< WriteAddr is sFLASH_PAGESIZE aligned */ + { + if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */ + { + sFLASH_WritePage(pBuffer, WriteAddr, NumByteToWrite); + } + else /*!< NumByteToWrite > sFLASH_PAGESIZE */ + { + while (NumOfPage--) + { + sFLASH_WritePage(pBuffer, WriteAddr, sFLASH_SPI_PAGESIZE); + WriteAddr += sFLASH_SPI_PAGESIZE; + pBuffer += sFLASH_SPI_PAGESIZE; + } + + sFLASH_WritePage(pBuffer, WriteAddr, NumOfSingle); + } + } + else /*!< WriteAddr is not sFLASH_PAGESIZE aligned */ + { + if (NumOfPage == 0) /*!< NumByteToWrite < sFLASH_PAGESIZE */ + { + if (NumOfSingle > count) /*!< (NumByteToWrite + WriteAddr) > sFLASH_PAGESIZE */ + { + temp = NumOfSingle - count; + + sFLASH_WritePage(pBuffer, WriteAddr, count); + WriteAddr += count; + pBuffer += count; + + sFLASH_WritePage(pBuffer, WriteAddr, temp); + } + else + { + sFLASH_WritePage(pBuffer, WriteAddr, NumByteToWrite); + } + } + else /*!< NumByteToWrite > sFLASH_PAGESIZE */ + { + NumByteToWrite -= count; + NumOfPage = NumByteToWrite / sFLASH_SPI_PAGESIZE; + NumOfSingle = NumByteToWrite % sFLASH_SPI_PAGESIZE; + + sFLASH_WritePage(pBuffer, WriteAddr, count); + WriteAddr += count; + pBuffer += count; + + while (NumOfPage--) + { + sFLASH_WritePage(pBuffer, WriteAddr, sFLASH_SPI_PAGESIZE); + WriteAddr += sFLASH_SPI_PAGESIZE; + pBuffer += sFLASH_SPI_PAGESIZE; + } + + if (NumOfSingle != 0) + { + sFLASH_WritePage(pBuffer, WriteAddr, NumOfSingle); + } + } + } +} + +/** + * @brief Reads a block of data from the FLASH. + * @param pBuffer: pointer to the buffer that receives the data read from the FLASH. + * @param ReadAddr: FLASH's internal address to read from. + * @param NumByteToRead: number of bytes to read from the FLASH. + * @retval None + */ +void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead) +{ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Read from Memory " instruction */ + sFLASH_SendByte(sFLASH_CMD_READ); + + /*!< Send ReadAddr high nibble address byte to read from */ + sFLASH_SendByte((ReadAddr & 0xFF0000) >> 16); + /*!< Send ReadAddr medium nibble address byte to read from */ + sFLASH_SendByte((ReadAddr& 0xFF00) >> 8); + /*!< Send ReadAddr low nibble address byte to read from */ + sFLASH_SendByte(ReadAddr & 0xFF); + + while (NumByteToRead--) /*!< while there is data to be read */ + { + /*!< Read a byte from the FLASH */ + *pBuffer = sFLASH_SendByte(sFLASH_DUMMY_BYTE); + /*!< Point to the next location where the byte read will be saved */ + pBuffer++; + } + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +/** + * @brief Reads FLASH identification. + * @param None + * @retval FLASH identification + */ +uint32_t sFLASH_ReadID(void) +{ + uint32_t Temp = 0, Temp0 = 0, Temp1 = 0, Temp2 = 0; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "RDID " instruction */ + sFLASH_SendByte(0x9F); + + /*!< Read a byte from the FLASH */ + Temp0 = sFLASH_SendByte(sFLASH_DUMMY_BYTE); + + /*!< Read a byte from the FLASH */ + Temp1 = sFLASH_SendByte(sFLASH_DUMMY_BYTE); + + /*!< Read a byte from the FLASH */ + Temp2 = sFLASH_SendByte(sFLASH_DUMMY_BYTE); + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); + + Temp = (Temp0 << 16) | (Temp1 << 8) | Temp2; + + return Temp; +} + +/** + * @brief Initiates a read data byte (READ) sequence from the Flash. + * This is done by driving the /CS line low to select the device, then the READ + * instruction is transmitted followed by 3 bytes address. This function exit + * and keep the /CS line low, so the Flash still being selected. With this + * technique the whole content of the Flash is read with a single READ instruction. + * @param ReadAddr: FLASH's internal address to read from. + * @retval None + */ +void sFLASH_StartReadSequence(uint32_t ReadAddr) +{ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Read from Memory " instruction */ + sFLASH_SendByte(sFLASH_CMD_READ); + + /*!< Send the 24-bit address of the address to read from -------------------*/ + /*!< Send ReadAddr high nibble address byte */ + sFLASH_SendByte((ReadAddr & 0xFF0000) >> 16); + /*!< Send ReadAddr medium nibble address byte */ + sFLASH_SendByte((ReadAddr& 0xFF00) >> 8); + /*!< Send ReadAddr low nibble address byte */ + sFLASH_SendByte(ReadAddr & 0xFF); +} + +/** + * @brief Reads a byte from the SPI Flash. + * @note This function must be used only if the Start_Read_Sequence function + * has been previously called. + * @param None + * @retval Byte Read from the SPI Flash. + */ +uint8_t sFLASH_ReadByte(void) +{ + return (sFLASH_SendByte(sFLASH_DUMMY_BYTE)); +} + +/** + * @brief Sends a byte through the SPI interface and return the byte received + * from the SPI bus. + * @param byte: byte to send. + * @retval The value of the received byte. + */ +uint8_t sFLASH_SendByte(uint8_t byte) +{ + /*!< Loop while DR register in not emplty */ + while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_TXE) == RESET); + + /*!< Send byte through the SPI1 peripheral */ + SPI_I2S_SendData(sFLASH_SPI, byte); + + /*!< Wait to receive a byte */ + while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET); + + /*!< Return the byte read from the SPI bus */ + return SPI_I2S_ReceiveData(sFLASH_SPI); +} + +/** + * @brief Sends a Half Word through the SPI interface and return the Half Word + * received from the SPI bus. + * @param HalfWord: Half Word to send. + * @retval The value of the received Half Word. + */ +uint16_t sFLASH_SendHalfWord(uint16_t HalfWord) +{ + /*!< Loop while DR register in not emplty */ + while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_TXE) == RESET); + + /*!< Send Half Word through the sFLASH peripheral */ + SPI_I2S_SendData(sFLASH_SPI, HalfWord); + + /*!< Wait to receive a Half Word */ + while (SPI_I2S_GetFlagStatus(sFLASH_SPI, SPI_I2S_FLAG_RXNE) == RESET); + + /*!< Return the Half Word read from the SPI bus */ + return SPI_I2S_ReceiveData(sFLASH_SPI); +} + +/** + * @brief Enables the write access to the FLASH. + * @param None + * @retval None + */ +void sFLASH_WriteEnable(void) +{ + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Write Enable" instruction */ + sFLASH_SendByte(sFLASH_CMD_WREN); + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +/** + * @brief Polls the status of the Write In Progress (WIP) flag in the FLASH's + * status register and loop until write opertaion has completed. + * @param None + * @retval None + */ +void sFLASH_WaitForWriteEnd(void) +{ + uint8_t flashstatus = 0; + + /*!< Select the FLASH: Chip Select low */ + sFLASH_CS_LOW(); + + /*!< Send "Read Status Register" instruction */ + sFLASH_SendByte(sFLASH_CMD_RDSR); + + /*!< Loop as long as the memory is busy with a write cycle */ + do + { + /*!< Send a dummy byte to generate the clock needed by the FLASH + and put the value of the status register in FLASH_Status variable */ + flashstatus = sFLASH_SendByte(sFLASH_DUMMY_BYTE); + + } + while ((flashstatus & sFLASH_WIP_FLAG) == SET); /* Write in progress */ + + /*!< Deselect the FLASH: Chip Select high */ + sFLASH_CS_HIGH(); +} + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_flash.h b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_flash.h new file mode 100644 index 0000000..74a10a3 --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_flash.h @@ -0,0 +1,151 @@ +/** + ****************************************************************************** + * @file stm32_eval_spi_flash.h + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file contains all the functions prototypes for the stm32_eval_spi_flash + * firmware driver. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32_EVAL_SPI_FLASH_H +#define __STM32_EVAL_SPI_FLASH_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_SPI_FLASH + * @{ + */ + +/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Types + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Constants + * @{ + */ +/** + * @brief M25P SPI Flash supported commands + */ +#define sFLASH_CMD_WRITE 0x02 /*!< Write to Memory instruction */ +#define sFLASH_CMD_WRSR 0x01 /*!< Write Status Register instruction */ +#define sFLASH_CMD_WREN 0x06 /*!< Write enable instruction */ +#define sFLASH_CMD_READ 0x03 /*!< Read from Memory instruction */ +#define sFLASH_CMD_RDSR 0x05 /*!< Read Status Register instruction */ +#define sFLASH_CMD_RDID 0x9F /*!< Read identification */ +#define sFLASH_CMD_SE 0xD8 /*!< Sector Erase instruction */ +#define sFLASH_CMD_BE 0xC7 /*!< Bulk Erase instruction */ + +#define sFLASH_WIP_FLAG 0x01 /*!< Write In Progress (WIP) flag */ + +#define sFLASH_DUMMY_BYTE 0xA5 +#define sFLASH_SPI_PAGESIZE 0x100 + +#define sFLASH_M25P128_ID 0x202018 +#define sFLASH_M25P64_ID 0x202017 + +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Macros + * @{ + */ +/** + * @brief Select sFLASH: Chip Select pin low + */ +#define sFLASH_CS_LOW() GPIO_ResetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN) +/** + * @brief Deselect sFLASH: Chip Select pin high + */ +#define sFLASH_CS_HIGH() GPIO_SetBits(sFLASH_CS_GPIO_PORT, sFLASH_CS_PIN) +/** + * @} + */ + + + +/** @defgroup STM32_EVAL_SPI_FLASH_Exported_Functions + * @{ + */ +/** + * @brief High layer functions + */ +void sFLASH_DeInit(void); +void sFLASH_Init(void); +void sFLASH_EraseSector(uint32_t SectorAddr); +void sFLASH_EraseBulk(void); +void sFLASH_WritePage(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); +void sFLASH_WriteBuffer(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t NumByteToWrite); +void sFLASH_ReadBuffer(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t NumByteToRead); +uint32_t sFLASH_ReadID(void); +void sFLASH_StartReadSequence(uint32_t ReadAddr); + +/** + * @brief Low layer functions + */ +uint8_t sFLASH_ReadByte(void); +uint8_t sFLASH_SendByte(uint8_t byte); +uint16_t sFLASH_SendHalfWord(uint16_t HalfWord); +void sFLASH_WriteEnable(void); +void sFLASH_WaitForWriteEnd(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_EVAL_SPI_FLASH_H */ +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_sd.c b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_sd.c new file mode 100644 index 0000000..f652e71 --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_sd.c @@ -0,0 +1,901 @@ +/** + ****************************************************************************** + * @file stm32_eval_spi_sd.c + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file provides a set of functions needed to manage the SPI SD + * Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h + * to know about the boards supporting this memory). + * It implements a high level communication layer for read and write + * from/to this memory. The needed STM32 hardware resources (SPI and + * GPIO) are defined in stm32xx_eval.h file, and the initialization is + * performed in SD_LowLevel_Init() function declared in stm32xx_eval.c + * file. + * You can easily tailor this driver to any other development board, + * by just adapting the defines for hardware resources and + * SD_LowLevel_Init() function. + * + * +-------------------------------------------------------+ + * | Pin assignment | + * +-------------------------+---------------+-------------+ + * | STM32 SPI Pins | SD | Pin | + * +-------------------------+---------------+-------------+ + * | SD_SPI_CS_PIN | ChipSelect | 1 | + * | SD_SPI_MOSI_PIN / MOSI | DataIn | 2 | + * | | GND | 3 (0 V) | + * | | VDD | 4 (3.3 V)| + * | SD_SPI_SCK_PIN / SCLK | Clock | 5 | + * | | GND | 6 (0 V) | + * | SD_SPI_MISO_PIN / MISO | DataOut | 7 | + * +-------------------------+---------------+-------------+ + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval_spi_sd.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_SPI_SD + * @brief This file includes the SD card driver of STM32-EVAL boards. + * @{ + */ + +/** @defgroup STM32_EVAL_SPI_SD_Private_Types + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_SD_Private_Defines + * @{ + */ +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_SD_Private_Macros + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_SD_Private_Variables + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_SD_Private_Function_Prototypes + * @{ + */ +/** + * @} + */ + + +/** @defgroup STM32_EVAL_SPI_SD_Private_Functions + * @{ + */ + +/** + * @brief DeInitializes the SD/SD communication. + * @param None + * @retval None + */ +void SD_DeInit(void) +{ + SD_LowLevel_DeInit(); +} + +/** + * @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 Detect if SD card is correctly plugged in the memory slot. + * @param None + * @retval Return if SD is detected or not + */ +uint8_t SD_Detect(void) +{ + __IO uint8_t status = SD_PRESENT; + + /*!< Check GPIO to detect SD */ + if (GPIO_ReadInputData(SD_DETECT_GPIO_PORT) & SD_DETECT_PIN) + { + status = SD_NOT_PRESENT; + } + return status; +} + +/** + * @brief Returns information about specific card. + * @param cardinfo: pointer to a SD_CardInfo structure that contains all SD + * card information. + * @retval The SD Response: + * - SD_RESPONSE_FAILURE: Sequence failed + * - SD_RESPONSE_NO_ERROR: Sequence succeed + */ +SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo) +{ + SD_Error status = SD_RESPONSE_FAILURE; + + status = SD_GetCSDRegister(&(cardinfo->SD_csd)); + status = SD_GetCIDRegister(&(cardinfo->SD_cid)); + cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ; + cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2)); + cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen); + cardinfo->CardCapacity *= cardinfo->CardBlockSize; + + /*!< Returns the reponse */ + return status; +} + +/** + * @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 + */ +SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize) +{ + uint32_t i = 0; + SD_Error 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 reponse */ + 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--) + { + /*!< 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 + */ +SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize) +{ + uint32_t i = 0; + SD_Error 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++; + } + /*!< 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 reponse */ + return rvalue; +} + +/** + * @brief Writes many blocks 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. + * @param NumberOfBlocks: number of blocks to be written. + * @retval The SD Response: + * - SD_RESPONSE_FAILURE: Sequence failed + * - SD_RESPONSE_NO_ERROR: Sequence succeed + */ +SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, 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--) + { + /*!< Send CMD24 (SD_CMD_WRITE_SINGLE_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 < BlockSize; 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 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 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 Send 5 bytes command to the SD card. + * @param Cmd: The user expected command to send to SD card. + * @param Arg: The command argument. + * @param Crc: The CRC. + * @retval None + */ +void SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc) +{ + uint32_t i = 0x00; + + uint8_t Frame[6]; + + Frame[0] = (Cmd | 0x40); /*!< Construct byte 1 */ + + Frame[1] = (uint8_t)(Arg >> 24); /*!< Construct byte 2 */ + + Frame[2] = (uint8_t)(Arg >> 16); /*!< Construct byte 3 */ + + Frame[3] = (uint8_t)(Arg >> 8); /*!< Construct byte 4 */ + + Frame[4] = (uint8_t)(Arg); /*!< Construct byte 5 */ + + Frame[5] = (Crc); /*!< Construct CRC: byte 6 */ + + for (i = 0; i < 6; i++) + { + SD_WriteByte(Frame[i]); /*!< Send the Cmd bytes */ + } +} + +/** + * @brief Get SD card data response. + * @param None + * @retval The SD status: Read data response xxx0<status>1 + * - status 010: Data accecpted + * - status 101: Data rejected due to a crc error + * - status 110: Data rejected due to a Write error. + * - status 111: Data rejected due to other error. + */ +uint8_t SD_GetDataResponse(void) +{ + uint32_t i = 0; + uint8_t response, rvalue; + + while (i <= 64) + { + /*!< Read resonse */ + response = SD_ReadByte(); + /*!< Mask unused bits */ + response &= 0x1F; + switch (response) + { + case SD_DATA_OK: + { + rvalue = SD_DATA_OK; + break; + } + case SD_DATA_CRC_ERROR: + return SD_DATA_CRC_ERROR; + case SD_DATA_WRITE_ERROR: + return SD_DATA_WRITE_ERROR; + default: + { + rvalue = SD_DATA_OTHER_ERROR; + break; + } + } + /*!< Exit loop in case of data ok */ + if (rvalue == SD_DATA_OK) + break; + /*!< Increment loop counter */ + i++; + } + + /*!< Wait null data */ + while (SD_ReadByte() == 0); + + /*!< Return response */ + return response; +} + +/** + * @brief Returns the SD response. + * @param None + * @retval The SD Response: + * - SD_RESPONSE_FAILURE: Sequence failed + * - SD_RESPONSE_NO_ERROR: Sequence succeed + */ +SD_Error SD_GetResponse(uint8_t Response) +{ + uint32_t Count = 0xFFF; + + /*!< Check if response is got or a timeout is happen */ + while ((SD_ReadByte() != Response) && Count) + { + Count--; + } + if (Count == 0) + { + /*!< After time out */ + return SD_RESPONSE_FAILURE; + } + else + { + /*!< Right response got */ + return SD_RESPONSE_NO_ERROR; + } +} + +/** + * @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 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 Write a byte on the SD. + * @param Data: byte to send. + * @retval None + */ +uint8_t SD_WriteByte(uint8_t Data) +{ + /*!< Wait until the transmit buffer is empty */ + while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET) + { + } + + /*!< Send the byte */ + SPI_I2S_SendData(SD_SPI, Data); + + /*!< Wait to receive a byte*/ + while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET) + { + } + + /*!< Return the byte read from the SPI bus */ + return SPI_I2S_ReceiveData(SD_SPI); +} + +/** + * @brief Read a byte from the SD. + * @param None + * @retval The received byte. + */ +uint8_t SD_ReadByte(void) +{ + uint8_t Data = 0; + + /*!< Wait until the transmit buffer is empty */ + while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET) + { + } + /*!< Send the byte */ + SPI_I2S_SendData(SD_SPI, SD_DUMMY_BYTE); + + /*!< Wait until a data is received */ + while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET) + { + } + /*!< Get the received data */ + Data = SPI_I2S_ReceiveData(SD_SPI); + + /*!< Return the shifted data */ + return Data; +} + +/** + * @} + */ + + +/** + * @} + */ + + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ diff --git a/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_sd.h b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_sd.h new file mode 100644 index 0000000..4a63f3a --- /dev/null +++ b/thirdparty/STM32F10x_StdPeriph_Lib_V3.5.0/Utilities/STM32_EVAL/Common/stm32_eval_spi_sd.h @@ -0,0 +1,280 @@ +/** + ****************************************************************************** + * @file stm32_eval_spi_sd.h + * @author MCD Application Team + * @version V4.5.0 + * @date 07-March-2011 + * @brief This file contains all the functions prototypes for the stm32_eval_spi_sd + * firmware driver. + ****************************************************************************** + * @attention + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + * <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2> + ****************************************************************************** + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32_EVAL_SPI_SD_H +#define __STM32_EVAL_SPI_SD_H + +#ifdef __cplusplus + extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ +#include "stm32_eval.h" + +/** @addtogroup Utilities + * @{ + */ + +/** @addtogroup STM32_EVAL + * @{ + */ + +/** @addtogroup Common + * @{ + */ + +/** @addtogroup STM32_EVAL_SPI_SD + * @{ + */ + +/** @defgroup STM32_EVAL_SPI_SD_Exported_Types + * @{ + */ + +typedef enum +{ +/** + * @brief SD reponses and error flags + */ + SD_RESPONSE_NO_ERROR = (0x00), + SD_IN_IDLE_STATE = (0x01), + SD_ERASE_RESET = (0x02), + SD_ILLEGAL_COMMAND = (0x04), + SD_COM_CRC_ERROR = (0x08), + SD_ERASE_SEQUENCE_ERROR = (0x10), + SD_ADDRESS_ERROR = (0x20), + SD_PARAMETER_ERROR = (0x40), + SD_RESPONSE_FAILURE = (0xFF), + +/** + * @brief Data response error + */ + SD_DATA_OK = (0x05), + SD_DATA_CRC_ERROR = (0x0B), + SD_DATA_WRITE_ERROR = (0x0D), + SD_DATA_OTHER_ERROR = (0xFF) +} SD_Error; + +/** + * @brief Card Specific Data: CSD Register + */ +typedef struct +{ + __IO uint8_t CSDStruct; /*!< CSD structure */ + __IO uint8_t SysSpecVersion; /*!< System specification version */ + __IO uint8_t Reserved1; /*!< Reserved */ + __IO uint8_t TAAC; /*!< Data read access-time 1 */ + __IO uint8_t NSAC; /*!< Data read access-time 2 in CLK cycles */ + __IO uint8_t MaxBusClkFrec; /*!< Max. bus clock frequency */ + __IO uint16_t CardComdClasses; /*!< Card command classes */ + __IO uint8_t RdBlockLen; /*!< Max. read data block length */ + __IO uint8_t PartBlockRead; /*!< Partial blocks for read allowed */ + __IO uint8_t WrBlockMisalign; /*!< Write block misalignment */ + __IO uint8_t RdBlockMisalign; /*!< Read block misalignment */ + __IO uint8_t DSRImpl; /*!< DSR implemented */ + __IO uint8_t Reserved2; /*!< Reserved */ + __IO uint32_t DeviceSize; /*!< Device Size */ + __IO uint8_t MaxRdCurrentVDDMin; /*!< Max. read current @ VDD min */ + __IO uint8_t MaxRdCurrentVDDMax; /*!< Max. read current @ VDD max */ + __IO uint8_t MaxWrCurrentVDDMin; /*!< Max. write current @ VDD min */ + __IO uint8_t MaxWrCurrentVDDMax; /*!< Max. write current @ VDD max */ + __IO uint8_t DeviceSizeMul; /*!< Device size multiplier */ + __IO uint8_t EraseGrSize; /*!< Erase group size */ + __IO uint8_t EraseGrMul; /*!< Erase group size multiplier */ + __IO uint8_t WrProtectGrSize; /*!< Write protect group size */ + __IO uint8_t WrProtectGrEnable; /*!< Write protect group enable */ + __IO uint8_t ManDeflECC; /*!< Manufacturer default ECC */ + __IO uint8_t WrSpeedFact; /*!< Write speed factor */ + __IO uint8_t MaxWrBlockLen; /*!< Max. write data block length */ + __IO uint8_t WriteBlockPaPartial; /*!< Partial blocks for write allowed */ + __IO uint8_t Reserved3; /*!< Reserded */ + __IO uint8_t ContentProtectAppli; /*!< Content protection application */ + __IO uint8_t FileFormatGrouop; /*!< File format group */ + __IO uint8_t CopyFlag; /*!< Copy flag (OTP) */ + __IO uint8_t PermWrProtect; /*!< Permanent write protection */ + __IO uint8_t TempWrProtect; /*!< Temporary write protection */ + __IO uint8_t FileFormat; /*!< File Format */ + __IO uint8_t ECC; /*!< ECC code */ + __IO uint8_t CSD_CRC; /*!< CSD CRC */ + __IO uint8_t Reserved4; /*!< always 1*/ +} SD_CSD; + +/** + * @brief Card Identification Data: CID Register + */ +typedef struct +{ + __IO uint8_t ManufacturerID; /*!< ManufacturerID */ + __IO uint16_t OEM_AppliID; /*!< OEM/Application ID */ + __IO uint32_t ProdName1; /*!< Product Name part1 */ + __IO uint8_t ProdName2; /*!< Product Name part2*/ + __IO uint8_t ProdRev; /*!< Product Revision */ + __IO uint32_t ProdSN; /*!< Product Serial Number */ + __IO uint8_t Reserved1; /*!< Reserved1 */ + __IO uint16_t ManufactDate; /*!< Manufacturing Date */ + __IO uint8_t CID_CRC; /*!< CID CRC */ + __IO uint8_t Reserved2; /*!< always 1 */ +} SD_CID; + +/** + * @brief SD Card information + */ +typedef struct +{ + SD_CSD SD_csd; + SD_CID SD_cid; + uint32_t CardCapacity; /*!< Card Capacity */ + uint32_t CardBlockSize; /*!< Card Block Size */ +} SD_CardInfo; + +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_SD_Exported_Constants + * @{ + */ + +/** + * @brief Block Size + */ +#define SD_BLOCK_SIZE 0x200 + +/** + * @brief Dummy byte + */ +#define SD_DUMMY_BYTE 0xFF + +/** + * @brief Start Data tokens: + * Tokens (necessary because at nop/idle (and CS active) only 0xff is + * on the data/command line) + */ +#define SD_START_DATA_SINGLE_BLOCK_READ 0xFE /*!< Data token start byte, Start Single Block Read */ +#define SD_START_DATA_MULTIPLE_BLOCK_READ 0xFE /*!< Data token start byte, Start Multiple Block Read */ +#define SD_START_DATA_SINGLE_BLOCK_WRITE 0xFE /*!< Data token start byte, Start Single Block Write */ +#define SD_START_DATA_MULTIPLE_BLOCK_WRITE 0xFD /*!< Data token start byte, Start Multiple Block Write */ +#define SD_STOP_DATA_MULTIPLE_BLOCK_WRITE 0xFD /*!< Data toke stop byte, Stop Multiple Block Write */ + +/** + * @brief SD detection on its memory slot + */ +#define SD_PRESENT ((uint8_t)0x01) +#define SD_NOT_PRESENT ((uint8_t)0x00) + + +/** + * @brief Commands: CMDxx = CMD-number | 0x40 + */ +#define SD_CMD_GO_IDLE_STATE 0 /*!< CMD0 = 0x40 */ +#define SD_CMD_SEND_OP_COND 1 /*!< CMD1 = 0x41 */ +#define SD_CMD_SEND_CSD 9 /*!< CMD9 = 0x49 */ +#define SD_CMD_SEND_CID 10 /*!< CMD10 = 0x4A */ +#define SD_CMD_STOP_TRANSMISSION 12 /*!< CMD12 = 0x4C */ +#define SD_CMD_SEND_STATUS 13 /*!< CMD13 = 0x4D */ +#define SD_CMD_SET_BLOCKLEN 16 /*!< CMD16 = 0x50 */ +#define SD_CMD_READ_SINGLE_BLOCK 17 /*!< CMD17 = 0x51 */ +#define SD_CMD_READ_MULT_BLOCK 18 /*!< CMD18 = 0x52 */ +#define SD_CMD_SET_BLOCK_COUNT 23 /*!< CMD23 = 0x57 */ +#define SD_CMD_WRITE_SINGLE_BLOCK 24 /*!< CMD24 = 0x58 */ +#define SD_CMD_WRITE_MULT_BLOCK 25 /*!< CMD25 = 0x59 */ +#define SD_CMD_PROG_CSD 27 /*!< CMD27 = 0x5B */ +#define SD_CMD_SET_WRITE_PROT 28 /*!< CMD28 = 0x5C */ +#define SD_CMD_CLR_WRITE_PROT 29 /*!< CMD29 = 0x5D */ +#define SD_CMD_SEND_WRITE_PROT 30 /*!< CMD30 = 0x5E */ +#define SD_CMD_SD_ERASE_GRP_START 32 /*!< CMD32 = 0x60 */ +#define SD_CMD_SD_ERASE_GRP_END 33 /*!< CMD33 = 0x61 */ +#define SD_CMD_UNTAG_SECTOR 34 /*!< CMD34 = 0x62 */ +#define SD_CMD_ERASE_GRP_START 35 /*!< CMD35 = 0x63 */ +#define SD_CMD_ERASE_GRP_END 36 /*!< CMD36 = 0x64 */ +#define SD_CMD_UNTAG_ERASE_GROUP 37 /*!< CMD37 = 0x65 */ +#define SD_CMD_ERASE 38 /*!< CMD38 = 0x66 */ + +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_SD_Exported_Macros + * @{ + */ +/** + * @brief Select SD Card: ChipSelect pin low + */ +#define SD_CS_LOW() GPIO_ResetBits(SD_CS_GPIO_PORT, SD_CS_PIN) +/** + * @brief Deselect SD Card: ChipSelect pin high + */ +#define SD_CS_HIGH() GPIO_SetBits(SD_CS_GPIO_PORT, SD_CS_PIN) +/** + * @} + */ + +/** @defgroup STM32_EVAL_SPI_SD_Exported_Functions + * @{ + */ +void SD_DeInit(void); +SD_Error SD_Init(void); +uint8_t SD_Detect(void); +SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo); +SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize); +SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks); +SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize); +SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks); +SD_Error SD_GetCSDRegister(SD_CSD* SD_csd); +SD_Error SD_GetCIDRegister(SD_CID* SD_cid); + +void SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc); +SD_Error SD_GetResponse(uint8_t Response); +uint8_t SD_GetDataResponse(void); +SD_Error SD_GoIdleState(void); +uint16_t SD_GetStatus(void); + +uint8_t SD_WriteByte(uint8_t byte); +uint8_t SD_ReadByte(void); + +#ifdef __cplusplus +} +#endif + +#endif /* __STM32_EVAL_SPI_SD_H */ +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/** + * @} + */ + +/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |