STM32F10xx is ARM Cortex-M3 based 32bit low power processor.
In low-density (flash 16-32Kbytes), medium-density (flash 4-128Kbytes), and high density (flash 256-512 Kbytes) devices, the processor architecture is as below:
For connectivity line devices, processor architecture is as below:
The sample C project/code we show here is for SDIO access. The SD/SDIO MMC card host interface (SDIO) provides an interface between the AHB peripheral bus and MultiMediaCards (MMCs), SD memory cards, SDIO cards and CE-ATA devices.
Communication over the bus is based on command and data transfers. The basic transaction on the MultiMediaCard/SD/SD I/O bus is the command/response transaction. These types of bus transaction transfer their information directly within the command or response structure. In addition, some operations have a data token. Data transfers to/from SD/SDIO memory cards are done in data blocks. Data transfers to/from MMC are done data blocks or streams. Data transfers to/from the CE-ATA Devices are done in data blocks.
Single access:
Block read access:
Block write access:
Sequential read access:
Sequential write access:
Part of sdio_sd.c code is as below with comments removed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 |
#include "sdio_sd.h" #define NULL 0 #define SDIO_STATIC_FLAGS ((uint32_t)0x000005FF) #define SDIO_CMD0TIMEOUT ((uint32_t)0x00010000) #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) #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)0x000FFFFF) #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) #define SD_CCCC_LOCK_UNLOCK ((uint32_t)0x00000080) #define SD_CCCC_WRITE_PROT ((uint32_t)0x00000040) #define SD_CCCC_ERASE ((uint32_t)0x00000020) #define SDIO_SEND_IF_COND ((uint32_t)0x00000008) static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1; static uint32_t CSD_Tab[4], CID_Tab[4], RCA = 0; static uint32_t DeviceMode = SD_POLLING_MODE; static uint32_t TotalNumberOfBytes = 0, StopCondition = 0; uint32_t *SrcBuffer, *DestBuffer; __IO SD_Error TransferError = SD_OK; __IO uint32_t TransferEnd = 0; __IO uint32_t NumberOfBytes = 0; SD_CardInfo SDCardInfo; SDIO_InitTypeDef SDIO_InitStructure; SDIO_CmdInitTypeDef SDIO_CmdInitStructure; SDIO_DataInitTypeDef SDIO_DataInitStructure; 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); static uint8_t convert_from_bytes_to_power_of_two(uint16_t NumberOfBytes); void SD_LowLevel_DeInit(void) { GPIO_InitTypeDef GPIO_InitStructure; SDIO_ClockCmd(DISABLE); SDIO_SetPowerState(SDIO_PowerState_OFF); SDIO_DeInit(); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, DISABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOD, &GPIO_InitStructure); } void SD_LowLevel_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD | SD_DETECT_GPIO_CLK, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SD_DETECT_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(SD_DETECT_GPIO_PORT, &GPIO_InitStructure); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_SDIO, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA2, ENABLE); } void SD_LowLevel_DMA_TxConfig(uint32_t *BufferSRC, uint32_t BufferSize) { DMA_InitTypeDef DMA_InitStructure; DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4); DMA_Cmd(DMA2_Channel4, DISABLE); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)BufferSRC; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = BufferSize / 4; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel4, &DMA_InitStructure); DMA_Cmd(DMA2_Channel4, ENABLE); } void SD_LowLevel_DMA_RxConfig(uint32_t *BufferDST, uint32_t BufferSize) { DMA_InitTypeDef DMA_InitStructure; DMA_ClearFlag(DMA2_FLAG_TC4 | DMA2_FLAG_TE4 | DMA2_FLAG_HT4 | DMA2_FLAG_GL4); DMA_Cmd(DMA2_Channel4, DISABLE); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)SDIO_FIFO_ADDRESS; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)BufferDST; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = BufferSize / 4; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA2_Channel4, &DMA_InitStructure); DMA_Cmd(DMA2_Channel4, ENABLE); } uint32_t SD_DMAEndOfTransferStatus(void) { return (uint32_t)DMA_GetFlagStatus(DMA2_FLAG_TC4); } void SD_DeInit(void) { SD_LowLevel_DeInit(); } SD_Error SD_Init(void) { SD_Error errorstatus = SD_OK; SD_LowLevel_Init(); SDIO_DeInit(); errorstatus=SD_PowerON(); if (errorstatus != SD_OK) { printf(" SD_PowerON is error.\n"); return(errorstatus); } printf(" sd power_on is ok.\n"); errorstatus=SD_InitializeCards(); if (errorstatus != SD_OK) { printf(" sd_InitializeCards is error.\n"); return(errorstatus); } printf(" sd_InitializeCard is ok.\n"); 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_Enable; SDIO_Init(&SDIO_InitStructure); if (errorstatus == SD_OK) { errorstatus = SD_GetCardInfo(&SDCardInfo); } if (errorstatus == SD_OK) { printf("SD_GetCardInfo is ok.\n"); } if (errorstatus == SD_OK) { } if (errorstatus == SD_OK) { printf(" 4b BusOperation is ok.\n"); errorstatus = SD_SetDeviceMode(SD_DMA_MODE); } return(errorstatus); } # 322 "sdio_sd.c" 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); } } 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; } } uint8_t SD_Detect(void) { __IO uint8_t status = SD_PRESENT; if (GPIO_ReadInputDataBit(SD_DETECT_GPIO_PORT, SD_DETECT_PIN) != Bit_RESET) { status = SD_NOT_PRESENT; } return status; } |
The full SDIO C project/code is in below.
Use points to gain access. You can either purchase points or contribute content and use contribution points to gain access.
Jay, how much data rate can you achieve with the code you show?