rt_int32_t stm32f4xx_sdio_init(void) { struct rt_mmcsd_host *host; NVIC_InitTypeDef NVIC_InitStructure; host = mmcsd_alloc_host(); if (!host) { return -RT_ERROR; } stm32f4_sdio = rt_malloc(sizeof(struct stm32f4xx_sdio)); if (!stm32f4_sdio) { rt_kprintf("alloc stm32f4_sdio failed\n"); goto err; } rt_memset(stm32f4_sdio, 0, sizeof(struct stm32f4xx_sdio)); host->ops = &ops; host->freq_min = 375000; host->freq_max = 25000000; host->valid_ocr = VDD_32_33 | VDD_33_34; host->flags = MMCSD_BUSWIDTH_4 | MMCSD_MUTBLKWRITE; host->max_seg_size = 65535; host->max_dma_segs = 2; host->max_blk_size = 512; host->max_blk_count = 4096; stm32f4_sdio->host = host; SDIO_StructInit(&SDIO_InitStructure); /* SDIO Peripheral Low Level Init */ SD_LowLevel_Init(); 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); host->private_data = stm32f4_sdio; mmcsd_change(host); return 0; err: mmcsd_free_host(host); return -RT_ENOMEM; }
/** * @brief Initializes the SD/SD communication. * @param None * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_Init(void) { uint32_t i = 0; /*!< Initialize SD_SPI */ SD_LowLevel_Init(); /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte 0xFF, 10 times with CS high */ /*!< Rise CS and MOSI for 80 clocks cycles */ for (i = 0; i <= 9; i++) { /*!< Send dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); } /*------------Put SD in SPI mode--------------*/ /*!< SD initialized and set to SPI mode properly */ return (SD_GoIdleState()); }
/** * @brief Initializes the SD/SD communication. * @param None * @retval The SD Response: * - SD_RESPONSE_FAILURE: Sequence failed * - SD_RESPONSE_NO_ERROR: Sequence succeed */ SD_Error SD_Init(void) { uint32_t TimeOut, i = 0; SD_Error Status = SD_RESPONSE_NO_ERROR; /*!< Initialize SD_SPI 初始化SPI相应管脚与处理器的SPI接口 */ //printf("(SD Init in SPI mode)"); SD_LowLevel_Init(); SD_SPI_SetSpeedLow(); //rt_kprintf("sd_init 0\n"); /*------------Put SD in SPI mode--------------*/ /*!< SD initialized and set to SPI mode properly */ TimeOut = 0; do { /*!< SD chip select high */ SD_CS_HIGH(); /*!< Send dummy byte 0xFF, 10 times with CS high */ /*!< Rise CS and MOSI for 80 clocks cycles */ for (i = 0; i <= 9; i++) { /*!< Send dummy byte 0xFF */ SD_WriteByte(SD_DUMMY_BYTE); } Status = SD_GoIdleState(); //rt_kprintf("sd_init 1 %d\n",(uint32_t)Status); if(TimeOut > 6) { break; } TimeOut++; }while(Status); SD_SPI_SetSpeedHi(); return (Status); }
void SDCardReadWriteTextTask(void* parameters) { uint8_t outbuf[512]; uint8_t inbuf[512]; int i; //LED_LowLevel_Init(); //UART_LowLevel_Init(); SD_LowLevel_Init(); //Initialize PINS, vector table and SDIO interface //UART_SendLine("Ready...Steady\n"); printf("SSD\n\r"); LEDOn(LED2); //Initialize the SD SD_Init(); //After return from this function sd is in transfer mode. //UART_SendLine("GO\n"); printf("SDI\n\r"); LEDOff(LED2); //Write a single block to the SD for (i=0;i<512;i++) { outbuf[i]='a'; } //Single Block Test SD_WriteSingleBlock(outbuf, 3000); SD_ReadSingleBlock(inbuf, 3000); //UART_SendLine("First Block\n"); //UART_Send(inbuf,512); printf("\n\r%s", "First Block"); printf("\n\r%i,%i,%i,%i", (int)inbuf[0], (int)inbuf[1], (int)inbuf[2], (int)inbuf[3]); SD_WriteSingleBlock(outbuf, 3001); SD_ReadSingleBlock(inbuf, 3001); //UART_SendLine("Second Block\n"); //UART_Send(inbuf,512); printf("\n\r%s", "Second Block"); printf("\n\r%i,%i,%i,%i", (int)inbuf[0], (int)inbuf[1], (int)inbuf[2], (int)inbuf[3]); //Multiple block SD_StartMultipleBlockWrite(1000); SD_WriteData(outbuf,500); SD_WriteData(outbuf+50,50); SD_WriteData(outbuf,500); SD_StopMultipleBlockWrite(); SD_ReadSingleBlock(inbuf, 1000); //UART_SendLine("Mult. Block 1\n"); //UART_Send(inbuf,512); printf("\n\r%s", "Mult. Block 1"); printf("\n\r%i,%i,%i,%i", (int)inbuf[0], (int)inbuf[1], (int)inbuf[2], (int)inbuf[3]); SD_WaitTransmissionEnd(); SD_ReadSingleBlock(inbuf, 1001); //UART_SendLine("Mult. Block 2\n"); //UART_Send(inbuf,512); printf("\n\r%s", "Mult. Block 2"); printf("\n\r%i,%i,%i,%i", (int)inbuf[0], (int)inbuf[1], (int)inbuf[2], (int)inbuf[3]); // LEDOn(LED6); portTickType lastExecutionTime = xTaskGetTickCount(); for (;;) { DelayUntilTime( &lastExecutionTime, SDCardReadWriteTextTaskT); } }